[英]How to know what address a program in Linux crashes at?
I have a program running in Linux and It's been mysteriously crashing. 我有一个在Linux上运行的程序,它崩溃了。 I already know one way to know where it crashes at is to use GDB. 我已经知道了解崩溃的一种方法是使用GDB。 But I don't want to attach to it every time I restart it (do this a lot since I'm testing it). 但是我不想每次重新启动时都附加到它上(因为我正在测试它,所以要做很多事情)。 Is there an alternative way to do this? 有替代方法吗?
First use ulimit -c unlimited
to allow crashed programs to write core dumps. 首先使用ulimit -c unlimited
允许崩溃的程序编写核心转储。
After the program crashes, you'll find a core dump file, called core
, or perhaps core.<pid>
if your program is multithreaded. 程序崩溃后,您将找到一个名为core
或core.<pid>
的核心转储文件(如果您的程序是多线程的)。
You can load this into GDB to examine the state at the point of the crash with gdb program core
. 您可以使用gdb program core
将其加载到GDB中,以检查崩溃时的状态。
First do a ulimit -c unlimited
, so the program will leave a core dump. 首先执行ulimit -c unlimited
,这样程序将离开核心转储。 Then, when it crashes, invoke gdb
with the core dump, to read the state of the program at the moment of the crash. 然后,当它崩溃时,请使用核心转储调用gdb
以在崩溃时读取程序的状态。
You can configure your OS to dump a core file any time a program crashes. 您可以将操作系统配置为在程序崩溃时转储核心文件。 You can then examine the core to determine the crash location. 然后,您可以检查核心以确定崩溃位置。
-> compile the code with gdb flags enabled. ->在启用gdb标志的情况下编译代码。
gcc -o < binary name > -g < file.c > (assuming it is ac/c++ program) gcc -o <二进制名称> -g <file.c>(假设它是ac / c ++程序)
-> run the executable withing gdb. ->使用gdb运行可执行文件。 gdb < binary name > gdb <二进制名称>
after this there are ways to find the crash location: 1. stepwise execution. 之后,有几种方法可以找到崩溃位置:1.逐步执行。 2. run the code, it crashes (as expected), type "where" within gdb (without quotes) it gives the backtrace. 2.运行代码,它崩溃(按预期方式),在gdb中键入“ where”(不带引号),它给出回溯。 from that, you can find out the address. 由此,您可以找到地址。
here is a nice quick guide to gdb : http://www.cs.cmu.edu/~gilpin/tutorial/ 这是gdb的不错的快速指南: http : //www.cs.cmu.edu/~gilpin/tutorial/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.