gdb/lldb如何调用一个函数并在其中设置断点

4

我在一个长时间运行的程序中有一个全局函数:

int test()
{
    int a = 12;
    int c = 10;
    printf("a=%d",a);
    a += c ;
    printf("a=%d", a);
    return a;
}

我调试程序并断点,然后执行以下命令:
(lldb) call test()
a=12a=22(int) $0 = 22
(lldb)

我希望在我点击call test()后,每一行都能在test()方法中断,而不是立即返回结果。有人知道如何做吗?
------------------------------------ 下面是答案 ------------------------------------
@Jason Molenda的答案是正确的,使用expr -i0 -- test()代替call test()
(lldb) b test
Breakpoint 1: 4 locations.
(lldb) expr -i0 -- test()
error: Execution was interrupted, reason: breakpoint 1.1.
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.
(lldb) 

现在它在test()中出错了,但是引发了一个错误!!! 怎样避免这个错误?

2个回答

2

expression命令在lldb中(callexpression的别名)有数十个选项,其中之一是lldb是否应该在执行表达式时停在断点上:--ignore-breakpoints false,或-i false,或-i 0

(lldb) br s -n printf
Breakpoint 2: where = libsystem_c.dylib`printf, address = 0x00007fff89ee7930
(lldb) expr -- (void)printf("hi\n")
hi
(lldb) expr -i0 -- (void)printf("hi\n")
error: Execution was interrupted, reason: breakpoint 2.1.
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.
Process 15259 stopped
* thread #1: tid = 0xf0daf, 0x00007fff89ee7930 libsystem_c.dylib`printf, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
    #0: 0x00007fff89ee7930 libsystem_c.dylib`printf
libsystem_c.dylib`printf:
-> 0x7fff89ee7930:  pushq  %rbp
   0x7fff89ee7931:  movq   %rsp, %rbp
   0x7fff89ee7934:  pushq  %r15
   0x7fff89ee7936:  pushq  %r14
(lldb)  

对于默认行为(是否在断点处停止),我们进行了一些思考,这似乎是大多数人期望的行为。

正如我所说,call命令只是expression的别名。如果您想更改其行为,可以使用自己的别名覆盖它。例如,command alias call expr -i false --就能做到。您可以将其放入您的~/.lldbinit文件中,然后就可以使用了。


0

这不是开玩笑吧?我只是检查了标准断点以确保:

(gdb) b test
Breakpoint 2 at 0x400671: file t.c, line 6.
(gdb) call test()

Breakpoint 2, test () at t.c:6
6       printf("a\n");

不,这不是一个玩笑。在lldb(Xcode)中,我按照您上面的回答操作了,但它无法打断每一行。 - isaacselement
我得到了这个:(gdb)调用test()断点1,test()位于t.c的第5行 程序在从GDB调用的函数中停止调试。 将放弃包含该函数(test)的表达式的评估。 当函数执行完成时,GDB将静默停止。 (gdb) - dbrank0
是的,这很正常。你可以逐行执行这个函数,你可以完成它。这是预期的行为,不是吗? - Vladimir Kunschikov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接