overboming
overboming

Reputation: 1542

How do I know which illegal address the program access when a segmentation fault happens

Plus, The program runs on a arm device running Linux, I can print out stack info and register values in the sig-seg handler I assign. The problem is I can't add -g option to the source file, since the bug may won't reproduce due to performance downgrade.

Upvotes: 7

Views: 4863

Answers (6)

Brian Postow
Brian Postow

Reputation: 12187

If the -g option makes the error disappear, then knowing where it crashes is unlikely to be useful anyway. It's probably writing to an uninitialized pointer in function A, and then function B tries to legitimately use that memory, and dies. Memory errors are a pain.

Upvotes: 0

antirez
antirez

Reputation: 18514

You may also want to use the backtrace() function if available, that will provide the call stack at the time of the crash. This can be used in order to dump the stack like it happens in an high level programming language when a C program gets a segmentation fault, bus error, or other memory violation error.

backtrace() is available both on Linux and Mac OS X

Upvotes: 1

nategoose
nategoose

Reputation: 12392

If you are worried about using -g on the binary that you load on the device, you may be able to use gdbserver on the ARM device with a stripped version of the executable and run arm-gdb on your development machine with the unstripped version of the executable. The stripped version and the unstripped version need to match up to do this, so do this:

# You may add your own optimization flags
arm-gcc -g program.c -o program.debug 
arm-strip --strip-debug program.debug -o program
# or
arm-strip --strip-unneeded program.debug -o program

You'll need to read the gdb and gdbserver documentation to figure out how to use them. It's not that difficult, but it isn't as polished as it could be. Mainly it's very easy to accidentally tell gdb to do something that it ends up thinking you meant to do locally, so it will switch out of remote debugging mode.

Upvotes: 1

caf
caf

Reputation: 239171

Compiling with the -g option to gcc does not cause a "performance downgrade". All it does is cause debugging symbols to be included; it does not affect the optimisation or code generation.

If you install your SIGSEGV handler using the sa_sigaction member of the sigaction struct passed to sigaction(), then the si_addr member of the siginfo_t structure passed to your handler contains the faulting address.

Upvotes: 15

ereOn
ereOn

Reputation: 55776

I tend to use valgrind which indicates leaks and memory access faults.

Upvotes: 4

overboming
overboming

Reputation: 1542

This seems to work http://tlug.up.ac.za/wiki/index.php/Obtaining_a_stack_trace_in_C_upon_SIGSEGV

static void signal_segv(int signum, siginfo_t* info, void*ptr) {
// info->si_addr is the illegal address
}

Upvotes: 1

Related Questions