1

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.

dromodel
  • 9,581
  • 12
  • 47
  • 65
  • You could pipe the output through a script that prints a timestamp after every newline. – Adam Sep 15 '14 at 20:39
  • I mentioned running it from a shell because that's the only way I know of to pipe the output. How can you pipe output from within GDB? – dromodel Sep 15 '14 at 20:56
  • You can run the piped command in the shell then attach GDB to the program once it has started. – Adam Sep 15 '14 at 21:10

1 Answers1

1

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.

Community
  • 1
  • 1
Employed Russian
  • 199,314
  • 34
  • 295
  • 362