如何从字符串变量执行GDB命令?

时间:2014-08-22 01:32:04

标签: scripting debugging gdb

假设调试过程有一个字符串变量,如下所示:

char* cmd_str = "set confirm on";

如何在GDB中从cmd_str执行命令?

(gdb) $cmd = cmd_str
(gdb) ???

1 个答案:

答案 0 :(得分:4)

您可以使用gdb' eval命令,该命令在其参数上运行printf,然后将结果作为命令进行评估。

(gdb) list
1   #include <stdlib.h>
2   main()
3   {
4       char *a = "set confirm off";
5   
6       pause();
7   }
(gdb) break 6
Breakpoint 1 at 0x400540: file cmdtest.c, line 6.
(gdb) run
Starting program: ./cmdtest 
Breakpoint 1, main () at cmd.c:6
6       pause();

(gdb) show confirm
Whether to confirm potentially dangerous operations is on.
(gdb) printf "%s", a
set confirm off(gdb) 
(gdb) eval "%s", a
(gdb) show confirm
Whether to confirm potentially dangerous operations is off.