4

I print my temporary strings with this to standard output:

printf "%s", nodeToString(myNode)

but I want to print this string to a file.

I tried solution stated here, but printf results still go to standard output.

Edit: Clarification for cIph3r's answer.

Here what I tried on command line:

$ gdb 

(gdb) attach 23053

(gdb) printf "%s", nodeToString(myNode)  // This works and outputs to screen

(gdb) run printf "%s", nodeToString(myNode) > outputfile  // I get this warning
The program being debugged has been started already.
Start it from the beginning? (y or n) 
Community
  • 1
  • 1
metdos
  • 13,411
  • 17
  • 77
  • 120

1 Answers1

4

gdb can perform pipelining like the bash does but I only know how to use gdb from commandline.

first, run gdb and tell what it should debug:

gdb ./prog

then in gdb, you can run the program with run

run 

here you can also run it with argumetns:

run argv1 argv2

but you can also run it with bash-pipeline commands:

run > outputfile

this is the same as

./prog > outputfile

However, if you want output gdb-output, like

(gdb) print "test"
$s1 = "test"

this is not the right way. The you do it that way:

(gdb) set logging on

but keep in mind not to do this when the program you want do debug is running

If all that won't work, you can use tee to capture stdout from gdb and redirect it to a file:

gdb ./prog | tee output.log

then the gdb-output is also saved in output.log (but all stdout)

after quitting gdb, there is a filename called output.log containing everything you saw in gdb

El Hocko
  • 2,581
  • 1
  • 13
  • 22
  • I didn't get exactly how to do this, I run gdb from emacs, and I add program to debug with attach command in gdb shell. Could you a little more specific? – metdos Feb 18 '13 at 16:15
  • ok, i missunderstood your question thought printf was code to debug – El Hocko Feb 18 '13 at 16:34
  • make sure you set loggin on, when the program is not running – El Hocko Feb 18 '13 at 16:42
  • `but keep in mind not to do this when the program you want to debug is running` Why is that? I did just that and got the output I needed. gdb 7.9 – eush77 Oct 22 '15 at 15:11