【问题标题】:LLDB: List source codeLLDB:列出源代码
【发布时间】:2013-07-03 00:55:54
【问题描述】:

我最常用的gdb 命令是l,然后是n,然后是l -

如何在 lldb 中获得相同的结果?

满足于必须输入一些行号才能在某处查看代码。在将大量变量转储到终端后,我想看看我在代码中的位置。而且我过去常常使用l - 来查看我在哪里,因为随后对l 的调用会使我向下滚动(lldb 也这样做,但关键是不会响应l -)。

也许我遗漏了一些东西,我可以将它放入某种“模式”,它会在单独的缓冲区中显示相应的源位置始终。那会很好,但我什至没有要求。

【问题讨论】:

    标签: c++ debugging gdb lldb


    【解决方案1】:

    在 Xcode 4.6 中,lldb 的 l 别名是 source list 的简单快捷方式。

    在树源的顶部,这已被改进为更像 gdb。如果你在http://lldb.llvm.org/ 上查看source/Interpreter/CommandInterpreter.cpp,你会发现l 现在是这些情况下的正则表达式命令别名:

    if (list_regex_cmd_ap->AddRegexCommand("^([0-9]+)[[:space:]]*$", "source list --line %1") &&
        list_regex_cmd_ap->AddRegexCommand("^(.*[^[:space:]])[[:space:]]*:[[:space:]]*([[:digit:]]+)[[:space:]]*$", "source list --file '%1' --line %2") &&
        list_regex_cmd_ap->AddRegexCommand("^\\*?(0x[[:xdigit:]]+)[[:space:]]*$", "source list --address %1") &&
        list_regex_cmd_ap->AddRegexCommand("^-[[:space:]]*$", "source list --reverse") &&
        list_regex_cmd_ap->AddRegexCommand("^-([[:digit:]]+)[[:space:]]*$", "source list --reverse --count %1") &&
        list_regex_cmd_ap->AddRegexCommand("^(.+)$", "source list --name \"%1\"") &&
        list_regex_cmd_ap->AddRegexCommand("^$", "source list"))
    

    在这些情况下,您会得到如下行为:

    显示当前帧:

    (lldb) f
    #0: 0x0000000100000f2b a.out`main + 27 at a.c:15
       12   
       13   
       14   
    -> 15       puts ("hi"); // line 15
       16   
       17       puts ("hi"); // line 17
       18   }
    

    显示前十行:

    (lldb) l -
       5    
       6    
       7    
       8    
       9        puts ("hi"); // line 9
       10   
       11   
    

    您还可以使用stop-line-count-afterstop-line-count-before 设置来控制在帧停止处显示多少源上下文。

    请注意,您可以在 ~/.lldbinit 文件中创建自己的正则表达式命令别名,其行为与树顶 lldb 的 l 相同。有关语法和示例,请参阅help command regex

    【讨论】:

    • 嗯。我不知道为什么l - 早些时候没有为我工作。现在可以了。
    • 我在玩一个玩具源文件,我注意到如果我反复使用l 到源文件的末尾,l - 似乎没有再倒退。如果您接近源文件的末尾,您可能已经看到了。
    • 我明白了。这肯定是一个烦人的错误,但我很高兴它在大多数情况下都能正常工作。
    • 赞成,因为f 是我在代码中打印当前行所需的神奇 lldb 命令。
    【解决方案2】:

    LLDB:[如何] 列出源代码

    ie:对于寻找 “如何让 lldb 再次显示我在哪一行 ?(因为我最近的命令已经掩盖了它)”的任何人,它就是f。输入f 再次查看您在代码中的位置。

    f
    

    frame select
    

    来源:LLDB: List source code

    另见lldb中的帮助菜单:

    help f
    

    显示以下内容:

    (lldb) help f
         Select the current stack frame by index from within the current thread (see 
         'thread backtrace'.)
    
    Syntax: f <cmd-options> [<frame-index>]
    
    Command Options Usage:
      f [-r <offset>] [<frame-index>]
    
           -r <offset> ( --relative <offset> )
                A relative frame index offset from the current frame index.
         
         This command takes options and free-form arguments.  If your arguments resemble option 
         specifiers (i.e., they start with a - or --), you must use ' -- ' between the end of 
         the command options and the beginning of the arguments.
    
    'f' is an abbreviation for 'frame select'
    

    该帮助菜单的底部显示“fframe select 的缩写”。

    注意gdb中,等价的命令很简单:

    f
    

    frame
    

    【讨论】:

    • 与这个主题有关,你知道如何在每一步之后停止 lldb 显示当前帧吗?我已经挖了很多年了,没有运气。 github.com/snare/voltron/issues/286
    • @RichieHH,看来您已经解决了这个问题?
    • 是的。我原以为它应该是一个停止钩子,但它的“硬编码”取决于设置 stop-line-count-before 和 stop-line-count-after 的值。将两者都设置为 0 有效。
    【解决方案3】:
    user@hostname> lldb -o "image lookup -rvn file" -o "quit" "Name of exec-file" | grep "CompileUnit"
    
    user@hostname> lldb -o "image lookup -rvs file" -o "quit" "Name of exec-file" | grep "CompileUnit"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-26
      • 2017-03-20
      • 2019-02-15
      • 1970-01-01
      • 1970-01-01
      • 2019-11-04
      • 2021-10-14
      • 2016-09-03
      相关资源
      最近更新 更多