Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ equivalent to Python's traceback library

I need to get some debugging libraries/tools to trace back the stack information print out to the stdout.

Python's traceback library can be an example.

What can be the C++ equivalent to Python's traceback library?

like image 875
prosseek Avatar asked Mar 04 '11 00:03

prosseek


People also ask

What is traceback library in Python?

Traceback is a python module that provides a standard interface to extract, format and print stack traces of a python program. When it prints the stack trace it exactly mimics the behaviour of a python interpreter.

Is traceback included in Python?

What Is a Python Traceback? A traceback is a report containing the function calls made in your code at a specific point. Tracebacks are known by many names, including stack trace, stack traceback, backtrace, and maybe others. In Python, the term used is traceback.

How do you make a Backtrace in Python?

Method 2: By using print_exception() method. This method prints exception information and stack trace entries from traceback object tb to file. Parameters: This method accepts the following parameters: if tb argument is not None, it prints a header Traceback (most recent call last):

When should the traceback () function be called?

1 traceback() If an error occurs, the easiest thing to do is to immediately call the traceback() function. This function returns the function call stack just before the error occurred so that you can see what level of function calls the error occurred.


2 Answers

This is platform-specific, and also depends on how you're compiling code. If you compile code with gcc using -fomit-frame-pointer it's very hard to get a useful backtrace, generally requiring heuristics. If you're using any libraries that use that flag you'll also run into problems--it's often used for heavily optimized libraries (eg. nVidia's OpenGL libraries).

This isn't a self-contained solution, as it's part of a larger engine, but the code is helpful:

  • https://svn.stepmania.com/svn/trunk/stepmania/src/archutils/Unix/Backtrace.cpp (Linux, OSX)
  • https://svn.stepmania.com/svn/trunk/stepmania/src/archutils/Win32/Crash.cpp (CrashHandler::do_backtrace for Win32)
  • https://svn.stepmania.com/svn/trunk/stepmania/src/archutils/Darwin/DarwinThreadHelpers.cpp (OSX)

This includes backtracing with the frame pointer with gcc when available and heuristic backtracing when it isn't; this can tend to give spurious entries in the trace, but for getting a backtrace for a crash report it's much better than losing the trace entirely.

There's other related code in those directories you'd want to look at to make use of that code (symbol lookups, signal handling); those links are a good starting point.

like image 141
Glenn Maynard Avatar answered Oct 16 '22 10:10

Glenn Maynard


Try google core dumper, it will give you a core dump when you need it.

like image 26
hhafez Avatar answered Oct 16 '22 10:10

hhafez