了解分段故障的堆栈跟踪
我正在执行 snprintf
并遇到段错误.
I am doing an snprintf
and getting a seg fault.
当我像这样将核心文件加载到gdb上时: gdb my_executable core
;并做了 bt
来获取回溯,我得到了:
when I loaded the core file on gdb like this: gdb my_executable core
; and did bt
to get the backtrace, I got following:
Program terminated with signal 11, Segmentation fault.
#0 0x88207fc2 in memcpy () from /usr/lib/libc.so.6
(gdb) bt
#0 0x88207fc2 in memcpy () from /usr/lib/libc.so.6
#1 0x88205eb6 in __sfvwrite () from /usr/lib/libc.so.6
#2 0x881fbc95 in strchr () from /usr/lib/libc.so.6
#3 0xbfbe6c14 in ?? ()
#4 0xbfbe69d8 in ?? ()
#5 0x881ed91e in localeconv () from /usr/lib/libc.so.6
#6 0x881fec05 in __vfprintf () from /usr/lib/libc.so.6
#7 0x881f7d80 in snprintf () from /usr/lib/libc.so.6
#8 0x08052b64 in my_function (files=0xbfbed710, filename=<value optimized out>) at myfile.c:1102
#9 0x08053bfb in main (argc=4, argv=0xbfbedd90) at myfile.c:225
在出现段错误的情况下,我会多次看到这样的堆栈,但从未正确理解.
I see such stack many times in case of seg fault but never understood correctly.
仅查看跟踪中的呼叫,我们就能知道出了什么问题吗?
Just looking the calls in trace, can we tell what is going wrong?
注意:请不要索取更多代码.我的动机只是简单地理解这样的堆栈跟踪意味着什么-与代码无关.我看到最上面的"memcpy"失败了.我想了解在这种情况下何时会发生这种情况.
NOTE: Please do not ask for more code. My motive is simply understand what the stack-trace like this means - irrespective of code. I see that on the top "memcpy" is failing. I want to understand when that can happen in this situation.
您的函数在 myfile.c:1102
处执行了某些操作.反过来,这会欺骗标准库,使其非法访问内存.操作系统会通过 sigsegv
注意到并拍打您的程序.
You function does something at myfile.c:1102
. This in turn tricks the standard library into illegally accessing memory. The operating system notices and slaps your program with sigsegv
.
常见原因(如Stackoverflow所示))为:
Common reasons, (as seen on Stackoverflow :)) ) are:
- 写到只读存储器
- 使用未初始化的指针
- 访问已分配块末尾的内存
长长的函数列表显示了执行此操作的人.所以:
The long list of functions shows you who did it. So:
-
my_function
称为snprintf
- 调用了
__ vfprintf
- ...