Loops 如何在GDB脚本中循环直到程序完成?

Loops 如何在GDB脚本中循环直到程序完成?,loops,while-loop,macros,gdb,Loops,While Loop,Macros,Gdb,如果程序完成,我需要在什么条件下停止循环?简单地在无限循环中循环。程序完成后,它将退出 define traverse while(CONDITION) if $start == 0 set $start = 1 print_node print_rss_item else continue print_node

如果程序完成,我需要在什么条件下停止循环?

简单地在无限循环中循环。程序完成后,它将退出

define traverse
    while(CONDITION)
        if $start == 0
            set $start = 1
            print_node
            print_rss_item
        else
            continue
            print_node
            print_rss_item
        end
    end
end

查看您的gdb脚本:

define traverse
    while(1)
        if $start == 0
            set $start = 1
            print_node
            print_rss_item
        else
            continue
            print_node
            print_rss_item
        end
    end
end
有几点需要注意:

  • gdb脚本(即所谓的“调试器”)和调试对象始终在切换模式下运行:每当gdb脚本运行时,调试对象暂停且不运行;每当调试对象运行时,gdb脚本暂停且不运行

  • 为什么会这样?这是因为每当调试对象处于暂停模式(读取“ptrace()”API及其各种选项:PEEKUSER、POKEUSER、ptrace_CONT)时,调试程序实际上可以干净地(和内存一致地)读取调试对象的内存,而不必担心损坏,从而读取其所有变量值等

  • 当调试器未运行时,即执行“继续”操作,其中控制权传递给被调试器,因此被调试器可以继续运行并更改自己的内存,而不必担心被另一个进程错误读取,因为这不会发生

    那么,我们如何知道调试对象何时结束?当“继续”失败,因此gdbscript将不会继续运行时。但是,如果在没有任何断点的情况下设置调试对象,然后执行gdb“run”命令,您将发现调试对象连续运行,而gdb脚本没有机会执行

    因此,如果脚本正在运行,那么debuggee将处于停止模式,反之亦然。如果调试对象通过调用exit()结束,gdb脚本也不会运行

    例如:

    这清楚地表明最后打印的计数器是286,尽管我指定了10000作为限制

    在不运行调试对象的情况下运行宏:

    Breakpoint 1, write () at ../sysdeps/unix/syscall-template.S:81
    81  in ../sysdeps/unix/syscall-template.S
    $40571 = 285
    drwxrwxr-x   2 tthtlc tthtlc    4096 Feb 18 00:00 yocto_slide
    [Inferior 1 (process 7395) exited normally]
    $40572 = 286
    The program is not being run.
    (gdb) 
    
    我们可以看到gdbscript将不会运行


    如果执行“gdb/bin/ls”并后跟“myloop\u print 10000”(假设宏在.gdbinit中定义),那么您将运行gdbscript以完成10000个循环,而调试对象从未运行过。

    我假设,
    $start=1
    是停止循环的子句。那么您希望
    而($start==0)
    ?这个链接应该会有所帮助
    defining the macro (inside .gdbinit file):
    
    define myloop_print
    set $total = $arg0
    set $i = 0
       while($i<$total)
         set $i = $i + 1
         print $i, $i
         cont
       end
    end
    
    Breakpoint 1, write () at ../sysdeps/unix/syscall-template.S:81
    81  in ../sysdeps/unix/syscall-template.S
    $40571 = 285
    drwxrwxr-x   2 tthtlc tthtlc    4096 Feb 18 00:00 yocto_slide
    [Inferior 1 (process 7395) exited normally]
    $40572 = 286
    The program is not being run.
    (gdb) 
    
    (gdb) myloop_print 10000
    $40573 = 1
    The program is not being run.
    (gdb)