我需要print addresses of all local variables in C,为此我尝试使用GDB脚本。
我正在使用以下gdb脚本。首先,我在main设置一个断点,一旦遇到它,我就在下一行设置一个断点,然后在程序的每一行设置步骤。
甚至可以使用 next 代替步骤来执行到下一行。但是我需要使用步骤进入函数,因为next不这样做。
b main
commands 1
while 1
info locals //some code needed to print addresses
b
step
end
end
run 但是,命令"step“也会逐步进入库函数。是否有条件地运行“步骤”命令,使其不进入库函数?我将有一个在程序中使用的函数和变量列表,因为它是由我的GCC Plugin返回的。只有在遇到用户定义的函数时,才能使用执行step的if语句,否则可以使用next吗?
commands 1
while 1
info locals
b
if //function name belongs to a predefined set
step
else
next
end
end
end我想了解更多关于GDB脚本语言的知识,但是我找不到足够的材料。我还需要知道我们是否可以声明数组、字符串和对它们执行比较和其他操作。
发布于 2015-06-24 07:16:22
我将有一个函数和变量的列表在程序中使用,因为它是由我的GCC插件返回。
由于您已经声明了一个函数名列表,在脚本的开头向列表中的每个函数添加断点,现在运行它,每次中断后运行您的逻辑以打印地址,然后继续。
例如:
prog:
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) https://stackoverflow.com/questions/30910750
复制相似问题