Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map segmentation fault address to the corresponding C code?

Tags:

c

linux

I see a segmentation fault like this segfault at 157e8250 ip 157e8250 sp bfa3fdfb how do I map the this address to the corresponding code where the fault occurs?

like image 296
arun Avatar asked Dec 09 '22 20:12

arun


2 Answers

  1. you should add the "-g" option of gcc

  2. use gnu tool "addr2line", like,

    # addr2line -e a.out 0x80484c8 -f
    
    -e: executable file name
    -f: generate function name 
    
like image 71
bbg Avatar answered Apr 27 '23 00:04

bbg


Use the addr2line utility from the GNU binutils suite. For example, this prints out the filename, line number, and function name (demangling it if it's a C++ function) for the faulting address 0x157e8250:

addr2line -e my_executable_file -C -f 0x157e8250

You can also pass on any number of addresses in the command line, e.g. if you have a stack trace of only instruction addresses.

Make sure you compile your program with the -g compiler option to generate debugging symbols, otherwise addr2line will not be able to symbolicate anything for you.

like image 31
Adam Rosenfield Avatar answered Apr 27 '23 01:04

Adam Rosenfield