Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I put a breakpoint on "something is printed to the terminal" in gdb?

I would like to know from where inside a huge application a certain message is printed. The application is so big and old that it uses all conceivable ways of printing text to the terminal; for example printf(), fprintf(stdout, ...) etc.

I write to put a breakpoint on the write() system call but then I'm flooded with too many breakpoint stops because of various file I/O operations that use write() as well.

So basically I want gdb to stop whenever the program prints something to the terminal but at the same time I don't want gdb to stop when the program writes something to a file.

like image 278
martin Avatar asked Oct 08 '09 15:10

martin


People also ask

How do I set a breakpoint in a specific file in gdb?

You can also set breakpoints on function names. To do this, just type "break [functionname]". gdb will stop your program just before that function is called. Breakpoints stay set when your program ends, so you do not have to reset them unless you quit gdb and restart it.

How do I skip a breakpoint in gdb?

To skip a breakpoint a certain number of times, we use the ignore command. The ignore command takes two arguments: the breakpoint number to skip, and the number of times to skip it. (gdb) ignore 2 5 Will ignore next 5 crossings of breakpoint 2.


2 Answers

Use a conditional breakpoint that checks the first parameter. On 64-bit x86 systems the condition would be:

(gdb) b write if 1==$rdi

On 32-bit systems, it is more complex because the parameter is on the stack, meaning that you need to cast $esp to an int * and index the fd parameter. The stack at that point has the return address, the length, buffer and finally fd.

This varies greatly between hardware platforms.

like image 173
bjfrost Avatar answered Oct 14 '22 02:10

bjfrost


With gdb 7.0, you can set conditional breakpoint on syscall write():

(gdb) catch syscall write
Catchpoint 1 (syscall 'write' [4])
(gdb) condition 1 $ebx==1

$ebx contains first syscall parameter - FD number here

like image 19
ks1322 Avatar answered Oct 14 '22 03:10

ks1322