如何在“某些内容已打印到终端"上设置断点?在gdb中? [英] how can I put a breakpoint on "something is printed to the terminal" in gdb?
问题描述
我想知道从巨大应用程序内部的哪个位置打印特定消息.该应用程序又大又旧,以至于它使用所有可以想象的方式将文本打印到终端;例如 printf(), fprintf(stdout, ...) 等.
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.
我写是为了在 write() 系统调用上放置一个断点,但由于各种文件 I/O 操作也使用 write(),我被太多的断点停止所淹没.
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.
所以基本上我希望 gdb 在程序向终端打印内容时停止,但同时我不希望 gdb 在程序向文件写入内容时停止.
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.
推荐答案
使用条件断点检查第一个参数.在 64 位 x86 系统上,条件是:
Use a conditional breakpoint that checks the first parameter. On 64-bit x86 systems the condition would be:
(gdb) b 写 if 1==$rdi
(gdb) b write if 1==$rdi
在 32 位系统上,它更复杂,因为参数在堆栈上,这意味着您需要将 $esp 转换为 int * 并索引 fd 参数.此时的堆栈有返回地址、长度、缓冲区,最后是 fd.
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.
这篇关于如何在“某些内容已打印到终端"上设置断点?在gdb中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!