Understanding Segmentation Stack Error

I do snprintf and get a seg error.

when I uploaded the kernel file to gdb as follows: gdb my_executable core ; and did bt to get the backtrace, I got the 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 a stack many times in the event of a seg failure, but I never understood correctly.

Just looking at the calls in the trace, can we say what is going wrong?

NOTE. Please do not request more code. My motive is to simply understand what stack tracing is, as it means - regardless of code. I see that from above, "memcpy" fails. I want to understand when this can happen in this situation.

+4
source share
3 answers

You are doing something in myfile.c:1102 . This, in turn, reduces the standard library to illegal memory access. The operating system notifies and removes your program using sigsegv .

Common reasons (as seen on Stackoverflow :))):

  • Write to Read Only Memory
  • Using uninitialized pointers
  • Memory access beyond the end of the allocated block

A long list of features shows who did this. So:

  • my_function is called snprintf
  • called __vfprintf
  • ...
+7
source

I suggest you run the executable under Valgrind. It can output additional call traces in case of problems in your code, such as working with already freed memory. This usually helps to understand the cause of the failure.

+2
source

This is just a trace of calls. The first function call in the program will appear at the bottom, usually it will be main , and subsequent calls to another function (from within the main one) will appear on top of it. If new calls are another subroutine (function), it is placed on top and the process continues.

GDB prints some useful information, considering it available. The first column of the stack position (upper lower). The second column contains the addresses of the calls, and the remaining information contains the name of the function being called and where it is located. Please note that they vary greatly. Sometimes the name of the symbol cannot be restored, but ?? () ?? () will appear in both # 3 and # 4 on your stack trace. When the source is available, also the line in which the function is defined will appear, like at myfile.c:225 .

0
source

All Articles