Search code examples
c++gdb

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


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.


Solution

  • 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.