如何在 lldb 中打印断点 ID

How to print breakpoint id in lldb

如果我使用命令设置自动继续断点,我如何打印断点 ID,以便我知道哪个断点正在显示该命令。

在下面的列表中我需要

br command add "print brk-id" 27

但是执行此操作的命令是什么,我没有在文档中或此处找到它。

    Current breakpoints:
27: regex = 'coordinateReadingItemAtURL', module = Foundation, locations = 28, resolved = 28, hit count = 16 Options: enabled auto-continue
    Breakpoint commands:
      po $rdx

  27.1: where = Foundation`-[NSFileCoordinator coordinateReadingItemAtURL:options:error:byAccessor:], address = 0x00007fff534d2642, resolved, hit count = 3
  27.2: where = Foundation`-[NSFileCoordinator(NSPrivate) _coordinateReadingItemAtURL:options:error:byAccessor:], address = 0x00007fff534d2695, resolved, hit count = 3
  27.3: where = Foundation`__85-[NSFileCoordinator(NSPrivate) _coordinateReadingItemAtURL:options:error:byAccessor:]_block_invoke_2, address = 0x00007fff534d4d44, resolved, hit count = 2
  27.4: where = Foundation`__85-[NSFileCoordinator(NSPrivate) _coordinateReadingItemAtURL:options:error:byAccessor:]_block_invoke.404, address = 0x00007fff534d52bf, resolved, hit count = 2
  27.5: where = Foundation`__73-[NSFileCoordinator coordinateReadingItemAtURL:options:error:byAccessor:]_block_invoke, address = 0x00007fff534d5330, resolved, hit count = 2
  27.6: where = Foundation`__73-[NSFileCoordinator coordinateReadingItemAtURL:options:error:byAccessor:]_block_invoke_2, address = 0x00007fff534d5559, resolved, hit count = 2
  27.7: where = Foundation`__85-[NSFileCoordinator(NSPrivate) _coordinateReadingItemAtURL:options:error:byAccessor:]_block_invoke_2.405, address = 0x00007fff534d60e3, resolved, hit count = 2

我认为没有命令会 打印断点 ID。我知道的最接近的是 thread info:

br command add -o "thread info"

这会打印一堆数据,断点id在最后:

thread #1: tid = 0x2220c5, 0x000000010b6cc4dd SomeSDK`-[ABClass method:], queue = 'com.apple.main-thread', stop reason = breakpoint 1.1

此数据由 thread-format 设置控制。您可以将默认值更改为更简洁,例如:

settings set thread-format "thread #${thread.index}{, stop reason = ${thread.stop-reason}}"

由此,thread info显示:

thread #1, stop reason = breakpoint 1.1

请记住,此设置用于其他地方,更改可以在其他地方显示。

最后,您可以求助于 Python API。此命令将打印断点 ID:

br command add -s python -o 'print "{}.{}".format(bp_loc.GetBreakpoint().GetID(), bp_loc.GetID())'

解释一下,Python断点命令其实是一个函数,你可以运行break list看看lldb会怎么调用它。其中一个参数是 bp_loc,这是一个 SBBreakpointLocation。要打印完整的断点 ID,此代码结合了两个 ID 值:bp_loc.GetBreakpoint().GetID()bp_loc.GetID().

为了便于重复使用,您可以将其放在某个文件中,例如 helpers.py

# helpers.py
def brk_id(frame, bp_loc, internal_dict):
    bp_id = bp_loc.GetBreakpoint().GetID()
    loc_id = bp_loc.GetID()
    print "{}.{}".format(bp_id, loc_id)

然后在你的 ~/.lldbinit 中导入它:

command script import path/to/helpers.py

现在您可以轻松使用辅助功能了:

br command add -F helpers.brk_id