I have a system developed in C++ on Linux platform. I am doing some debugging of this system. I want to look for the complete sequence of function calls to a function. Lets assume the functions are called in the following sequence
function_1 -> function_2 -> function_3 -> function_4
If I put a break point at function_4, the execution will be holded at that point. I want to see that functions_1, function_2 and function_3 are called before function_4. If there any gdb command to trace these function calls?
Thanks,
Ankur
You want a backtrace. The gdb command bt will show exactly what you are interested in.
bt: backtrace
http://sourceware.org/gdb/onlinedocs/gdb/Backtrace.html
If function_1() calls function_2() which calls function_3() etc
You can set your breakpoint in function_4() and you use the command
where
To print a backtrace of the stack
Another tool that may be useful is valgrind with the callgrind tool
Related
For example:
dprintf main,"hello\n"
run
Generates the same output as:
break main
commands
silent
printf "hello\n"
continue
end
run
Is there a significant advantage to using dprintf over commands, e.g. it is considerably faster (if so why?), or has some different functionality?
I imagine that dprinf could be in theory faster as it could in theory compile and inject code with a mechanism analogous to the compile code GDB command.
Or is it mostly a convenience command?
Source
In the 7.9.1 source, breakpoint.c:dprintf_command, which defines dprintf, calls create_breakpoint which is also what break_command calls, so they both seem to use the same underlying mechanism.
The main difference is that dprintf passes the dprintf_breakpoint_ops structure, which has different callbacks and gets initialized at initialize_breakpoint_ops.
dprintf stores list of command strings much like that of commands command, depending on the settings. They are:
set at update_dprintf_command_list
which gets called on after a type == bp_dprintf check inside init_breakpoint_sal
which gets called by create_breakpoint.
When a breakpoint is reached:
bpstat_stop_status gets called and invokes b->ops->after_condition_true (bs); for the breakpoint reached
after_condition_true for dprintf is dprintf_after_condition_true
bpstat_do_actions_1 runs the commands
There are two main differences.
First, dprintf has some additional output modes that can be used to make it work in other ways. See help set dprintf-channel, or the manual, for more information. I think these modes are the reason that dprintf was added as a separate entity; though at the same time they are fairly specialized and unlikely to be of general interest.
More usefully, though, dprintf doesn't interfere with next. If you write a breakpoint and use commands, and then next over such a breakpoint, gdb will forget about the next and act as if you had typed continue. This is a longstanding oddity in the gdb scripting language. dprintf doesn't suffer from this problem. (If you need similar functionality from an ordinary breakpoint, you can do this from Python.)
I use C++ 11 features actively. I have program created in Visual Studio 2013 that relies on lambdas to run multiple threads (lambda represents task, and thread receives lambda instance that it has to run). Lambda is defined in static library and linked in executable file that calls it from thread created by this executable file.
When I try to debug Linux version of this application with GDB, it looks like GDB can not step into method that contains lambda. It can not set breakpoints in this function, and when I try to step into, it even steps in methods that lambda calls from its body, but after return from these methods it doesn't go to lambda body, it goes to next method that lambda calls, etc.
Are there any way to debug lambdas body with GDB?
I've seen them in stack traces before, so it does at least know about them. I've never tried setting a normal breakpoint in one. It's sort of a hack, but you can set a breakpoint in one (or anywhere) by using asm volatile("int $3"); on x86(-64).
Here's an example program:
int main(){
auto f = [](){
asm volatile("int $3");
};
f();
return 0;
}
Here's it's backtrace when it hits that breakpoint:
#0 0x0000000000400577 in main::{lambda()#1}::operator()() const ()
#1 0x000000000040058d in main ()
From my experience, gdb cannot step into lambdas -- it just skips over them. Not only that, stepping into a lambda definition seems to confuse gdb and it proceeds to the end of the current function. You can, however, place a breakpoint explicitly inside a lambda, and if you hit that point, you will stop. This is obviously far from ideal.
Step into Lamba's .run() go next/step (jump initializations) until the following call:
std::forward<>(args)(...)
Step into this one. It will lead you to you lambda body code.
#defineing _GLIBCXX_DEBUG forces GCC to catch a large class of runtime errors in C++, such as out-of-bounds STL access, invalid iterators, etc.
Unfortunately, when the error happens, the message printed is not very helpful. I know how to print a backtrace with a function, and __FILE__ and __LINE__ with a macro myself.
Is there an easy way to convince GCC to do that, or to specify a function/macro for it to call when the kind of errors that _GLIBCXX_DEBUG catches actually occur?
I assume you mean you want messages that print the context of use in your code, rather than the filename and line number of some internal header file used by GCC.
There appears to be a single macro in .../debug/macros.h that all the checking code uses called _GLIBCXX_DEBUG_VERIFY. You could modify it to suit your needs.
Edit: Jonathan Wakely points out that all checks are fatal.
When a Debug Mode check fails it calls abort(), so it dumps a core file which you can easily examine with a debugger to see where it failed. If you run the program in a debugger it will stop when it aborts and you can print the stack trace with backtrace.
To make that automatic you would need to change the call to abort() (in libstdc++-v3/src/c++11/debug.cc). I think you could change it to call std::terminate() and then install your own terminate_handler with set_terminate to make it print a backtrace.
I want to dump a backtrace from a C++ program in Linux in a similar format as it is done in gdb. I tried to use the backtrace() and backtrace_symbols() functions for this purpose. These returned function names and offsets. I can use the __cxa_demangle() function to get a readable function name.
Is there any way to get the file/line positions too, as it is done by gdb?
How it's better to invoke gdb from program to print its stacktrace?`
Methode #4, shows a way to get filename and line. But uses a extern program..
What I want is a mix of what can be obtained by a static code analysis like Doxygen and the stackframe you can see when using GDB. I know which problematic function I'm debugging and I want to see the neighbourhood of the function calls that guided the execution to this function call. For instance, running a simple HelloWorld! would output something like:
main:
Greeter::Greeter()
Greeter::printHello()
Greeter::printWorld()
denoting that from the main function, the constructor was called and then the printHello and printWorld functions where called. Notice that in GDB if I break at printWorld I won't be able to see in the stackframe that printHello was called.
Any ideas about how to trace function calls without going through the pain of inserting log messages in a myriad of source files?
Thanks!!
The -finstrument-functions option to gcc instructs the compiler to call a user-provided profiling function at every function entry and exit.
You could use this to write a function that just logs every function entry and exit.
From reading the question I understand that you want a list of all relevant functions executed in order as they're executed.
Unfortunately there is no application to generate this list automatically, but there are helper macros to save you a lot of time. Define a single macro called LOGFUNCTION or whatever you want and define it as:
#define LOGFUNCTION printf("In %s (%s:%d)\n", __PRETTY_FUNCTION__, __FILE__, __LINE__);
Now you do have to paste the line LOGFUNCTION wherever you want a trace to be added.
wherever you see fit.
see http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html and http://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html
GDB features a stack trace, it does what you ask for.
What he wants is to obtain tha info (for example, backtrace from gdb) but printed in a 'nicer' format than gdb do.
I think you can't. I mean, maybe there is some type of app that trace your application and do something like that, but I never hear about something like that.
The best thing you can do is use GDB, maybe create some type of bash script that use gdb to obtain the info and print it out in the way you like.
Of course, your application MUST be compiled with debug symbols (-g param to gcc).
I'm not entirely sure what the problem is with gdb's backtrace, but maybe a profiler is closer to what you want? For example, using valgrind:
valgrind --tool cachegrind ./myprogram
kcachegrind callgrind.out.NNNN
Have you tried to use gprof to generate a call graph? You can also convert gprof output to something easier on the eye with gprof2dot for example.