Debugging SIGSEGV Backtrace
Before we begin, let's introduce the term backtrace: a series of recent function calls in your program (see $man backtrace). A backtrace lets you inspect the call stack and understand how execution reached a particular point.
While working on some code, I encountered a SIGSEGV and the program crashed. The log contained this trace, generated using backtrace():
[17101 XX 12:17:05 (+6)][23417] {sigsafe} src/common.c@1233: SIGSEGV(11), puntero 0xc0 desde 0x7f288f7c79aa
[17101 XX 12:17:05 (+6)][23417] {sigsafe} src/common.c@1252: [bt]: (0) /usr/lib64/twsmedia/libtwsmedia.so(twsmedia_widget_alarm_pool_draw+0x1680)[0x7f288f7c79aa]
[17101 XX 12:17:05 (+6)][23417] {sigsafe} src/common.c@1252: [bt]: (1) /usr/lib64/twsmedia/libtwsmedia.so(twsmedia_widget_alarm_pool_draw+0x1680)[0x7f288f7c79aa]
[17101 XX 12:17:05 (+6)][23417] {sigsafe} src/common.c@1252: [bt]: (2) /usr/sbin/mwconstructor[0x40a2c0]
[17101 XX 12:17:05 (+6)][23417] {sigsafe} src/common.c@1252: [bt]: (3) /lib64/libpthread.so.0(+0x7df5)[0x7f289173edf5]
[17101 XX 12:17:05 (+6)][23417] {sigsafe} src/common.c@1252: [bt]: (4) /lib64/libc.so.6(clone+0x6d)[0x7f288834c1ad]
The log shows where the problem occurred—but which source line is responsible?
One way to debug this is to run gdb and reproduce the bug, but that is not always practical.
What else can we do?
We can look directly inside the library's .o file to locate the source line. Let's begin:
Use nm to find the function's starting address in the .o file
nm src/twsmedia_widget.o | less
You will find a line like this:
000000000000cc9a T twsmedia_widget_alarm_pool_draw
0xcc9a is the starting address of the twsmedia_widget_alarm_pool_draw function
Add the 0x1680 offset from twsmedia_widget_alarm_pool_draw+0x1680 to that address. The result is 0xe31a.
Finally, call addr2line, to find the source line in the object's .text section
$ addr2line -j .text -e src/twsmedia_widget.o 0x000000000000e31a
src/twsmedia_widget.c:3139
We have located the problematic line: src/twsmedia_widget.c:3139.
This method will not work in every scenario, such as when function names are unavailable in the backtrace.
That’s all for now, see you later!
