如何执行下一个内部命令列表然后执行另一个命令?
How to execute next inside commands list and then another command?
当我在GDB中设置断点并附加一个命令列表时,如果我在这个命令列表中执行一个"next",下面的命令将被忽略,这是正常的(见https://sourceware.org/gdb/current/onlinedocs/gdb/Break-Commands.html#Break-Commands).
然而,它可能对我来说非常有用来覆盖这个限制...那么,是否可以在命令块中执行 "next" 以及以下命令?
例如:
break 8
commands
next
set i = i+1
continue
end
So, is it possible to execute a "next" in the commands block and also the following commands ?
GDB 没有,没有。
您不能从断点命令列表中 next
或 cont
,但您可以在 Python 中写入 "stop event handler",然后从那里恢复较差的执行。请参阅下面的独立示例:
buggy.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
i = atoi(getenv("i"));
if (i > 0) {
i++;
} else {
i--;
}
printf("i: %d\n", i);
return 0;
}
休息后的下一个-conditional.gdb
set confirm 0
set python print-stack full
python import gdb
file buggy
break 8
python
conditional_bpnum = int(gdb.parse_and_eval('$bpnum'))
def stop_handler(event):
if not isinstance(event, gdb.BreakpointEvent):
return
if conditional_bpnum not in set(x.number for x in event.breakpoints):
return
gdb.write('i: %d\n' % (gdb.parse_and_eval('i'),))
gdb.execute('next')
gdb.write('GDB: incrementing "i" from debugger\n')
gdb.execute('set variable i = i+1')
gdb.execute('continue')
gdb.events.stop.connect(stop_handler)
end
run
quit
示例会话
$ gcc -Os -g3 buggy.c -o buggy
$ i=0 gdb -q -x next-after-break-on-conditional.gdb
Breakpoint 1 at 0x4004e3: file buggy.c, line 8.
Breakpoint 1, main () at buggy.c:9
9 i++;
i: 0
11 i--;
GDB: incrementing "i" from debugger
i: 1
[Inferior 1 (process 7405) exited normally]
实施说明
stop_handler()
将在 GDB 停止时被调用,因此您 必须 在发出命令之前测试 GDB 是否针对特定断点停止。
如果我用 -O3
编译,我会得到 i
的可怕 "value has been optimized out" 错误并且 set variable i = i+1
会失败。所以像往常一样注意这一点。 (Fedora 21 上的 gcc-4.9.2、gdb-7.8.2、x86-64)
参考文献
- GDB Manual > Setting Breakpoints: "The debugger convenience variable ‘$bpnum’ records the number of the breakpoint you've set most recently"
- GDB Manual > Events in Python:
gdb.events.stop.connect(stop_handler)
- GDB Manual > Breakpoints in Python:
gdb.Breakpoint.num
当我在GDB中设置断点并附加一个命令列表时,如果我在这个命令列表中执行一个"next",下面的命令将被忽略,这是正常的(见https://sourceware.org/gdb/current/onlinedocs/gdb/Break-Commands.html#Break-Commands).
然而,它可能对我来说非常有用来覆盖这个限制...那么,是否可以在命令块中执行 "next" 以及以下命令?
例如:
break 8
commands
next
set i = i+1
continue
end
So, is it possible to execute a "next" in the commands block and also the following commands ?
GDB 没有,没有。
您不能从断点命令列表中 next
或 cont
,但您可以在 Python 中写入 "stop event handler",然后从那里恢复较差的执行。请参阅下面的独立示例:
buggy.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
i = atoi(getenv("i"));
if (i > 0) {
i++;
} else {
i--;
}
printf("i: %d\n", i);
return 0;
}
休息后的下一个-conditional.gdb
set confirm 0
set python print-stack full
python import gdb
file buggy
break 8
python
conditional_bpnum = int(gdb.parse_and_eval('$bpnum'))
def stop_handler(event):
if not isinstance(event, gdb.BreakpointEvent):
return
if conditional_bpnum not in set(x.number for x in event.breakpoints):
return
gdb.write('i: %d\n' % (gdb.parse_and_eval('i'),))
gdb.execute('next')
gdb.write('GDB: incrementing "i" from debugger\n')
gdb.execute('set variable i = i+1')
gdb.execute('continue')
gdb.events.stop.connect(stop_handler)
end
run
quit
示例会话
$ gcc -Os -g3 buggy.c -o buggy
$ i=0 gdb -q -x next-after-break-on-conditional.gdb
Breakpoint 1 at 0x4004e3: file buggy.c, line 8.
Breakpoint 1, main () at buggy.c:9
9 i++;
i: 0
11 i--;
GDB: incrementing "i" from debugger
i: 1
[Inferior 1 (process 7405) exited normally]
实施说明
stop_handler()
将在 GDB 停止时被调用,因此您 必须 在发出命令之前测试 GDB 是否针对特定断点停止。
如果我用 -O3
编译,我会得到 i
的可怕 "value has been optimized out" 错误并且 set variable i = i+1
会失败。所以像往常一样注意这一点。 (Fedora 21 上的 gcc-4.9.2、gdb-7.8.2、x86-64)
参考文献
- GDB Manual > Setting Breakpoints: "The debugger convenience variable ‘$bpnum’ records the number of the breakpoint you've set most recently"
- GDB Manual > Events in Python:
gdb.events.stop.connect(stop_handler)
- GDB Manual > Breakpoints in Python:
gdb.Breakpoint.num