Search code examples
breakpointslldb

How to print breakpoint id in lldb


If I set an auto-continue breakpoint with a command how can I also print out the breakpoint id so I know which breakpoint is displaying the command.

In the list below I need to

br command add "print brk-id" 27

But what is the command to do so, I'm not finding it in the docs or here on SO.

    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

Solution

  • I don't think there's a command that will print only the breakpoint id. The closest I know of is thread info:

    br command add -o "thread info"
    

    This will print a bunch of data, with the breakpoint id at the very end:

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

    This data is controlled by the thread-format setting. You could change the default to be more succinct, for example:

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

    With this, thread info shows:

    thread #1, stop reason = breakpoint 1.1
    

    Keep in mind this setting is used in other places, and changes can show elsewhere.

    Finally, you could resort to the Python API. This command will print the breakpoint id:

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

    To explain this, Python breakpoint commands are actually a function, you can run break list to see how lldb will call it. One of the arguments is bp_loc, which is a SBBreakpointLocation. To get print the full breakpoint ID, this code combines the two ID values: bp_loc.GetBreakpoint().GetID() and bp_loc.GetID().

    For easy reuse, you could put this in a file somewhere, for example 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)
    

    Then in your ~/.lldbinit you import it like so:

    command script import path/to/helpers.py
    

    And now you can use the helper function easily:

    br command add -F helpers.brk_id