Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get GDB to display a time stamp for every line of output?

Tags:

c++

gdb

I'm debugging an application which doesn't time stamp it's output. Can GDB do this and if not how might I go about adding time stamps to all messages, absent modifying every printf in the source (my app is in c++)?

The best I've got so far is to run it on the shell, streaming the output to awk, and then attaching GDB to the program to debug. I'd prefer some way inside of GDB however.

like image 925
dromodel Avatar asked Sep 06 '25 15:09

dromodel


2 Answers

Can GDB do this

Not easily (if at all): GDB does not capture the output of your program, it merely gives it control of the terminal, and the program is free to print whatever it wants.

The best I've got so far is to run it on the shell, streaming the output to awk, and then attaching GDB to the program to debug. I'd prefer some way inside of GDB however.

You should be able to do the awk redirection within GDB:

(gdb) run | awk ...

Note however that this piping shouldn't work either (within or outside GDB): by default, the application should detect that its output is going to a pipe, and start doing full buffering on stdout (the stderr should remain unbuffered).

To prevent buffering, you can call setvbuf.

An alternative solution (for cases where you can't modify the application) is to use expect or unbuffer, as in this answer.

like image 83
Employed Russian Avatar answered Sep 08 '25 09:09

Employed Russian


This is possible using set debug timestamp

set debug timestamp Turns on or off display of timestamps with GDB debugging info. When enabled, seconds and microseconds are displayed before each debugging message.

https://sourceware.org/gdb/current/onlinedocs/gdb.html/Debugging-Output.html

like image 39
srgsanky Avatar answered Sep 08 '25 09:09

srgsanky