在调试Xcode/lldb时,是否有一种方法来设置执行点? 更具体地说,是在触发断点后,手动将执行点移动到代码的另一行?
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'
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,而不是在编辑器窗口中移动指示器。