Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I see the output of printf inside gdb?

Tags:

c

linux

gdb

This is what I tried,but seems not working:

(gdb) call printf("%d",6)
$8 = 1
like image 344
assem Avatar asked Mar 25 '11 05:03

assem


2 Answers

You can't see the result is because stdout(FILE*) has a output buffer. It won't output any to the screen until the buffer is full or '\n' is encountered.

so call printf like this:

(gdb) call (int)printf("%d\n", 6)
6
$6 = 2

BTW, The "$6 = 2" which is result value of printf.

like image 66
iCoder Avatar answered Oct 09 '22 05:10

iCoder


It looks like it worked fine - printf returned 1, indicating that it successfully printed a single character to standard output.

Note that standard output isn't necessarily displayed in the same terminal that gdb is running in - it'll be displayed wherever the program you're debugging has its standard output (it's just as if the program itself had called printf() - the call command in gdb calls the function in the program's context).

like image 28
caf Avatar answered Oct 09 '22 05:10

caf