我需要print addresses of all local variables in C ,为此我正在尝试使用 GDB 脚本。
我正在使用以下 gdb 脚本。首先,我在 main 设置了一个断点,一旦遇到,我就在下一行设置一个断点,然后在程序的每一行step进入它。
甚至可以用next代替step来执行到下一行。但是我需要使用 step 进入功能,因为 next 不会那样做。
b main
commands 1
while 1
info locals //some code needed to print addresses
b
step
end
end
run
但是,命令“step”也会进入库函数。有没有办法有条件地运行“step”命令,这样它就不会进入库函数?我的 GCC Plugin 将返回程序中使用的函数和变量列表。 .我是否可以使用仅在遇到用户定义的函数时才执行 step 而在其他情况下执行 next 的 if 语句?
commands 1
while 1
info locals
b
if //function name belongs to a predefined set
step
else
next
end
end
end
我想了解更多有关 GDB 脚本语言的信息,但找不到足够的资料。我还需要知道我们是否可以声明数组、字符串并对它们执行比较和其他操作。
最佳答案
I will have a list of functions and variables used in the program as it is returned by my GCC Plugin.
因为你有一个你声明的函数名称列表,在脚本的开头为列表中的每个函数添加断点,现在运行它,在每个中断之后运行你的逻辑以打印地址,然后继续。
例如:
程序:
void fn1()
{
int j = 0;
printf("I am in fn 1");
}
void fn2()
{
int k = 0;
printf("I am in fn 2");
}
int main()
{
int i = 0;
fn1();
printf("I am in main");
fn2();
}
gdb 成绩单:
(gdb) b main
Breakpoint 1 at 0x400575: file surya.c, line 15.
(gdb) b fn1
Breakpoint 2 at 0x400535: file surya.c, line 3.
(gdb) b fn2
Breakpoint 3 at 0x400555: file surya.c, line 9.
(gdb) r
Starting program: /home/mohit/test/a.out
warning: the debug information found in "/lib64/ld-2.19.so" does not match "/lib64/ld-linux-x86-64.so.2" (CRC mismatch).
Breakpoint 1, main () at surya.c:15
15 int i = 0;
(gdb) p &i
$1 = (int *) 0x7fffffffdf7c
(gdb) c
Continuing.
Breakpoint 2, fn1 () at surya.c:3
3 int j = 0;
(gdb) p &j
$2 = (int *) 0x7fffffffdf5c
(gdb) c
Continuing.
Breakpoint 3, fn2 () at surya.c:9
9 int k = 0;
(gdb) p &k
$3 = (int *) 0x7fffffffdf5c
(gdb)
关于c - 编写 GDB 脚本来收集数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30910750/