Zhang Jiuan’ Notes

linux平台c/c++问题跟踪办法

比如我们对以如下程序进行跟踪,现为了说明问题,程序简单了点。

//hello.c

#include <stdio.h>

int main(int argc, char **argv)
{
        int i = 0;

        int *a;

        a[10] = i;

        return 0;
}

gcc -g hello.c

./a.out 出现了core.2569

首先我们可以想到的应该是gdb了。

gdb ./a.out core.2569,好问题很容易定位,程序在        a[10] = i;停住了。

跟一下代码不难发现错误。

但有些情况程序可能没有出来core文件,又怎么跟呢?

下面我们讲一下另一种方法:

运行出错后,运行一下dmesg命令。

dmesg

输出最后如下

a.out[3295]: segfault at 0000000000400518 rip 0000000000400485 rsp 00007fff52f02790 error 7

利用addr2line定位程序出错点:

addr2line -e a.out 0000000000400485

/home/pay/test/exception/hello.c:27

好,现在打开hello.c的27行,即 a[10] = i;

现在问题原因已不再难了。

经验是需要一点一滴的积累的。

 

thx

张久安

If you enjoyed this post, make sure you subscribe to my RSS feed!

No Comments, Comment or Ping

Reply to “linux平台c/c++问题跟踪办法”

You must be logged in to post a comment.

返回顶部