0

I am in the process of debugging a C++ program. I want to log all times the program goes through a specific breakpoint and capture the stack alongside some variable values. This happens multiple times, and want to collect this into a log file and process it after the execution. At the same time, I don't want the execution to go through the whole thing and want to observe the output and stop it at an appropriate time.

What I do is I run the program and attach gdb. Then in order to log the output I do the following:

(gdb) set trace-commands on
(gdb) set pagination off
(gdb) set logging overwrite on
(gdb) set logging file gdb.log
(gdb) set logging on

(gdb) break function_name
(gdb) commands
> print *variable_name
> backtrace
> continue
> end

Then I continue, and the process resumes execution. In the cli I can see the variable value and the backtrace output. In the gdb.log file I can see the variable value but not the backtrace.

If I type backtrace in the CLI, I can see its output being redirected in the gdb.log file. What doesn't seem to work is logging output of backtrace when invoked through the "commands" macro(?).

Can someone help me out? Thank you

3
  • Do not tag C for questions that do not involve C. Commented Apr 23 at 17:38
  • 1
    I just tried this myself, and it worked for me. What gdb version do you use? Commented Apr 23 at 17:44
  • I also tried locally and it worked for me on gdb 15.0.50. I'd suggest typing those gdb commands into a file, then launching gdb with gdb -x <command_file>. Then those commands are executed consistently from file. Commented Apr 24 at 14:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.