top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Printing backtrace whenever my program crashes?

+1 vote
583 views

I have a C/C++ executable what I want that when ever there is a crash i.e. core is generated then it should print the back trace of the crash before exit which will help me in debugging.

Please suggest how to proceed.

posted Aug 13, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

I could not understand the real requirement of this but assuming that you want to debug the followings are my suggestion
1. Get the core and open that in gdb i.e. gdb <binary> -c <core> and take the backtrace using bt
2. Use the following macro in the code

  #define TRACE_MSG fprintf(stderr, __FUNCTION__     \
                                             "() [%s:%d] here I am\n", \
                             __FILE__, __LINE__)

use it intelligently as it can generate lot of traces
3. Include debug.h in the code and use debug_backtrace() - check the following link for more detail http://web.stanford.edu/~ouster/cgi-bin/cs140-spring14/pintos/pintos_10.html

answer Aug 13, 2014 by Salil Agrawal
0 votes

In Linux/Unix, Create the binary in the debug mode(With -g option)
1) use pstack. It will help you in debugging .EG:
pstack corefile
2) If pstack is not in your machine. use command
gdb exename -c corefilename
and after that type bt
It will give you the necessary information. which could help you in debugging.

No code changes required for it.

answer Aug 13, 2014 by Double S
Similar Questions
+1 vote

I found that most released apps(including system apps) can be debugged on the emulator by the way following:
1. install the app on the emulator, and launch it
2. run adb shell ps to get the pid of the app
3. run adb jdwp, if you find the pid in the output, then you can attach to the app by running
adb forward tcp:1234 jdwp:$pid_you_find and jdb -connect com.sun.jdi.SocketAttach:hostname=localhost,port=1234
4. you can use commands such as "classes" to see the classes of the app, even "stop" to set breakpoint and "eval" to run many functions and see the members of the objects.
I have 2 questions:
1. Why these tricks fail on the phones? What's the difference between the rom of emulator and phones?
2. How to avoid my app being debugged through such way?

...