gdb print to file

c, debugging, gdb, printf, redirect

Solution

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

Problem

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) ```

Original source

Related problems