when execute my program in openwrt device, I get this message :

prog[876]: segfault at 3000747a0 ip 08052d3f sp bfca6a0c error 4 prog[8048000+f000]

my purpose is to know in which line code or function that caused this segfault

it is possible to get that from message ? or if there is another way to do that ?

有帮助吗?

解决方案

As mentioned in comments, compiling with debug symbols and running gdb on a later core dump is a way to get the information you want. If you are familiar with gdb, using it is a general, quick, and direct way of locating the code corresponding to the location given in the segfault message, particularly if that location is in your code rather than in a library.

Below is an alternative you can use, in case you aren't familiar with gdb. For this alternative, you need an output console or a log device; I presume one or the other is available to your OpenWrt embedded device.

Create a header file with the following line, or add it to an existing header file:

#define z fprintf(stderr,"%s@%3d \n",__FILE__,__LINE__);

Note, replace the identifier z with some other short identifer (eg Z, o, cry etc) if you've used z as a variable name in your code. Replace fprintf with a syslog call if appropriate. Using a semicolon at the end of the line reduces typing needed to use this to one character per line.

After putting the line in a header file that you #include in any questionable code file, near the beginnings of several functions add the character z (or whatever short identifier you chose) at the front of a line of code, then compile and run the code. As the code executes and encounters z codes, it will print out the current file and current line number. As each run narrows down the suspect range of code, you can remove some z from outside that range and add z codes more densely in the suspect range.

If the segfault is due to overwriting memory, eg part of the stack or heap, adding any debugging code (as above) can unpredictably shift the location at which a problem manifests. In that case, you can fall back on gdb. However, sometimes it may be helpful to add a little more code to the z macro, such as printing out values of any global variables that are relevant, or testing if some range of memory still meets some validity criterion or testing if some variables have changed value. For example:

#define z fprintf(stderr,"%s@%3d \n",__FILE__,__LINE__); testfvalid();
#define z fprintf(stderr,"%s@%3d %d %p  %d %d\n",__FILE__,__LINE__, i, fde+i, fde[i].path, fde[i].name);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top