在 Xcode/lldb 中移动执行点

8

在调试Xcode/lldb时,是否有一种方法来设置执行点? 更具体地说,是在触发断点后,手动将执行点移动到代码的另一行?

4个回答

15
如果您想在方法中向上或向下移动它,可以单击并拖动绿色箭头到特定位置。所以,如果您想在断点前备份一行,请单击生成的绿色箭头并将其拖动上移。如果您点击运行,将再次触发断点。

10
在 Xcode 9.1 中,绿色箭头已被更改为绿色汉堡包图标,并移动到右边缘。 - Jerry Krinock
这一定是Xcode调试中最酷但最不为人知的功能。 - matt
@JerryKrinock 您的评论比得到最多赞的回复更“时髦”。 - Bruno Bieri

6
在Xcode 6中,你可以使用j lineNumber - 参见下面的文档:
(lldb) help j
     Sets the program counter to a new address.  This command takes 'raw' input
     (no need to quote stuff).

Syntax: _regexp-jump [<line>]
_regexp-jump [<+-lineoffset>]
_regexp-jump [<file>:<line>]
_regexp-jump [*<addr>]


'j' is an abbreviation for '_regexp-jump'

2

lldb 的一个很棒的特点就是可以通过一点点 Python 脚本轻松扩展功能。例如,我很容易地编写了一个新的 jump 命令:

import lldb

def jump(debugger, command, result, dict):
  """Usage: jump LINE-NUMBER
Jump to a specific source line of the current frame.
Finds the first code address for a given source line, sets the pc to that value.  
Jumping across any allocation/deallocation boundaries (may not be obvious with ARC!), or with optimized code, quickly leads to undefined/crashy behavior. """

  if lldb.frame and len(command) >= 1:
    line_num = int(command)
    context = lldb.frame.GetSymbolContext (lldb.eSymbolContextEverything)
    if context and context.GetCompileUnit():
      compile_unit = context.GetCompileUnit()
      line_index = compile_unit.FindLineEntryIndex (0, line_num, compile_unit.GetFileSpec(), False)
      target_line = compile_unit.GetLineEntryAtIndex (line_index)
      if target_line and target_line.GetStartAddress().IsValid():
        addr = target_line.GetStartAddress().GetLoadAddress (lldb.target)
        if addr != lldb.LLDB_INVALID_ADDRESS:
          if lldb.frame.SetPC (addr):
            print "PC has been set to 0x%x for %s:%d" % (addr, target_line.GetFileSpec().GetFilename(), target_line.GetLine())

def __lldb_init_module (debugger, dict):
  debugger.HandleCommand('command script add -f %s.jump jump' % __name__)

我把这个文件放在了一个专门存放Python命令的lldb目录下,即~/lldb/,然后在~/.lldbinit文件中加载它:

command script import ~/lldb/jump.py

现在我有一个命令jump(按j键也可以),它将跳转到指定的行号,例如:

(lldb) j 5
PC has been set to 0x100000f0f for a.c:5
(lldb)

如果您在~/.lldbinit文件中加载了它,这个新的jump命令将在命令行lldb和Xcode中都可用--您需要使用Xcode中的调试器控制台窗格来移动pc,而不是在编辑器窗口中移动指示器。


你是如何测试/调试这个脚本的? - Iulian Onofrei

0

您可以使用lldb命令register write pc移动程序计数器(pc)。但它是基于指令的。

这里有一个出色的lldb/gdb比较链接,对于lldb概述非常有用。


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