例如:
dprintf main,"hello\n"
run
生成与以下内容相同的输出:
break main
commands
silent
printf "hello\n"
continue
end
run
使用是否有显着的优势dprintf
over commands
,例如它的速度要快得多(如果是的话为什么?),或者有一些不同的功能?
我认为 dprintf 理论上可能会更快,因为理论上它可以使用某种机制编译和注入代码类似于compile codeGDB命令 https://stackoverflow.com/questions/5480868/how-to-call-assembly-in-gdb/31709579#31709579.
或者它主要是一个方便的命令?
Source
在 7.9.1 源代码中,breakpoint.c:dprintf_command
,它定义了dprintf
, calls create_breakpoint
这也是break_command
调用,因此它们似乎都使用相同的底层机制。
主要区别在于dprintf
通过dprintf_breakpoint_ops
结构体,它有不同的回调并在以下位置初始化initialize_breakpoint_ops
.
dprintf
存储命令字符串列表,就像commands
命令,具体取决于设置。他们是:
- set at
update_dprintf_command_list
- 后被调用
type == bp_dprintf
检查里面init_breakpoint_sal
- 被调用的是
create_breakpoint
.
当到达断点时:
-
bpstat_stop_status
被调用并调用b->ops->after_condition_true (bs);
到达断点
-
after_condition_true
for dprintf
is dprintf_after_condition_true
-
bpstat_do_actions_1
运行命令
快速基准测试
loop.c
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
uintmax_t i, j, period, max;
if (argc > 1) {
max = strtoumax(argv[1], NULL, 10);
} else {
max = 10;
}
if (argc > 2) {
period = strtoumax(argv[2], NULL, 10);
} else {
period = 1;
}
i = 0;
j = 0;
while (1) {
if (period != 0 && i % period == 0) {
printf("%ju\n", j);
j++;
}
i++; /* line 25 */
if (i == max)
break;
}
}
编译并运行:
gcc -ggdb3 -O0 -std=c99 -Wall -Wextra -pedantic -o loop.out loop.c
loop.gdb
b 25
commands
silent
printf "%d\n", i
c
end
基准:
$ time gdb -n -q -batch -ex 'tb 39' -ex run -ex 'compile code printf("hello: %d\n", i)' -ex c -args ./loop.out 10000 0
所以我们看到两者dprintf
and commands
大大减慢速度,并且dprintf
仅比同等产品快一点commands
.
我问过有没有办法使用compile code
加快速度:GDB编译代码并且执行后不删除注入的代码 https://stackoverflow.com/questions/76233877/gdb-compile-code-and-dont-remove-injected-code-after-execution
在 Ubuntu 22.10、GDB 12.1 上测试。