debugging 我可以告诉LLDB删除活动断点吗?

8gsdolmq  于 2023-08-06  发布在  其他
关注(0)|答案(2)|浏览(104)

当LLDB触发断点X时,是否有一个命令将禁用或删除X,然后继续?

lmyy7pcs

lmyy7pcs1#

这是个有趣的想法。在lldb中没有内置的命令来执行此操作,但它很容易实现为用Python编写的用户定义命令。如果线程由于断点而停止,则SBThread::GetStopReason()将是eStopReasonBreakpointSBThread::GetStopReasonDataCount()将返回2 --表示断点ID和位置ID可用。SBThread::GetStopReasonDataAtIndex(0)将给予断点ID,SBThread::GetStopReasonDataAtIndex(1)将提供位置ID。(单个用户指定的断点可以解析为多个位置。例如内联函数,或者出现在单个程序中的多个库中的函数名。)
下面是一个快速而肮脏的python命令的例子。我把它放在~/lldb中,保存lldb用户定义命令,然后在~/.lldbinit文件中有一行类似于command script import ~/lldb/disthis.py的代码。
在使用中,它看起来像这样:

% lldb a.out
(lldb) target create "a.out"
Current executable set to 'a.out' (x86_64).
(lldb) br s -n main
Breakpoint 1: where = a.out`main + 15 at a.c:4, address = 0x0000000100000f4f
(lldb) r
Process 67487 launched: '/private/tmp/a.out' (x86_64)
Process 67487 stopped
* thread #1: tid = 0x290c51, 0x0000000100000f4f a.out`main + 15 at a.c:4, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
    #0: 0x0000000100000f4f a.out`main + 15 at a.c:4
   1    #include <stdio.h>
   2    int main()
   3    {
-> 4        puts ("HI");
   5        puts ("HI");
   6    }
(lldb) com scr imp ~/lldb/disthis.py
(lldb) disthis
Breakpoint 1.1 disabled.
(lldb) br li
Current breakpoints:
1: name = 'main', locations = 1
  1.1: where = a.out`main + 15 at a.c:4, address = 0x0000000100000f4f, unresolved, hit count = 1  Options: disabled 

(lldb)

字符串
很简单。

# import this into lldb with a command like
# command script import disthis.py

import lldb

def disthis(debugger, command, *args):
    """Usage: disthis
Disables the breakpoint the currently selected thread is stopped at."""

    target = None
    thread = None

    if len(args) == 2:
        # Old lldb invocation style
        result = args[0]
        if debugger and debugger.GetSelectedTarget() and debugger.GetSelectedTarget().GetProcess():
            target = debugger.GetSelectedTarget()
            process = target.GetProcess()
            thread = process.GetSelectedThread()
    elif len(args) == 3:
        # New (2015 & later) lldb invocation style where we're given the execution context
        exe_ctx = args[0]
        result = args[1]
        target = exe_ctx.GetTarget()
        thread = exe_ctx.GetThread()
    else:
        print "Unknown python function invocation from lldb."
        return

    if thread == None:
        print >>result, "error: process is not paused, or has not been started yet."
        result.SetStatus (lldb.eReturnStatusFailed)
        return

    if thread.GetStopReason() != lldb.eStopReasonBreakpoint:
        print >>result, "error: not stopped at a breakpoint."
        result.SetStatus (lldb.eReturnStatusFailed)
        return

    if thread.GetStopReasonDataCount() != 2:
        print >>result, "error: Unexpected number of StopReasonData returned, expected 2, got %d" % thread.GetStopReasonDataCount()
        result.SetStatus (lldb.eReturnStatusFailed)
        return

    break_num = thread.GetStopReasonDataAtIndex(0)
    location_num = thread.GetStopReasonDataAtIndex(1)

    if break_num == 0 or location_num == 0:
        print >>result, "error: Got invalid breakpoint number or location number"
        result.SetStatus (lldb.eReturnStatusFailed)
        return

    bkpt = target.FindBreakpointByID (break_num)
    if location_num > bkpt.GetNumLocations():
        print >>result, "error: Invalid location number"
        result.SetStatus (lldb.eReturnStatusFailed)
        return

    bkpt_loc = bkpt.GetLocationAtIndex(location_num - 1)
    if bkpt_loc.IsValid() != True:
        print >>result, "error: Got invalid BreakpointLocation"
        result.SetStatus (lldb.eReturnStatusFailed)
        return

    bkpt_loc.SetEnabled(False)
    print >>result, "Breakpoint %d.%d disabled." % (break_num, location_num)
    return

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

t0ybt7op

t0ybt7op2#

下面是Jason的python 2代码作为python 3代码:

# import this into lldb with a command like
# command script import disthis.py

import lldb

def disthis(debugger, command, *args):
    """Usage: disthis
Disables the breakpoint the currently selected thread is stopped at."""

    target = None
    thread = None

    if len(args) == 2:
        # Old lldb invocation style
        result = args[0]
        if debugger and debugger.GetSelectedTarget() and debugger.GetSelectedTarget().GetProcess():
            target = debugger.GetSelectedTarget()
            process = target.GetProcess()
            thread = process.GetSelectedThread()
    elif len(args) == 3:
        # New (2015 & later) lldb invocation style where we're given the execution context
        exe_ctx = args[0]
        result = args[1]
        target = exe_ctx.GetTarget()
        thread = exe_ctx.GetThread()
    else:
        print("Unknown python function invocation from lldb.")
        return

    if thread == None:
        print(result, "error: process is not paused, or has not been started yet.", file=result)
        result.SetStatus (lldb.eReturnStatusFailed)
        return

    if thread.GetStopReason() != lldb.eStopReasonBreakpoint:
        print(result, "error: not stopped at a breakpoint.", file=result)
        result.SetStatus (lldb.eReturnStatusFailed)
        return

    if thread.GetStopReasonDataCount() != 2:
        print(result, "error: Unexpected number of StopReasonData returned, expected 2, got %d" % thread.GetStopReasonDataCount(), file=result)
        result.SetStatus (lldb.eReturnStatusFailed)
        return

    break_num = thread.GetStopReasonDataAtIndex(0)
    location_num = thread.GetStopReasonDataAtIndex(1)

    if break_num == 0 or location_num == 0:
        print(result, "error: Got invalid breakpoint number or location number", file=result)
        result.SetStatus (lldb.eReturnStatusFailed)
        return

    bkpt = target.FindBreakpointByID (break_num)
    if location_num > bkpt.GetNumLocations():
        print(result, "error: Invalid location number", file=result)
        result.SetStatus (lldb.eReturnStatusFailed)
        return

    bkpt_loc = bkpt.GetLocationAtIndex(location_num - 1)
    if bkpt_loc.IsValid() != True:
        print(result, "error: Got invalid BreakpointLocation", file=result)
        result.SetStatus (lldb.eReturnStatusFailed)
        return

    bkpt_loc.SetEnabled(False)
    print("Breakpoint %d.%d disabled." % (break_num, location_num), file=result)
    return

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

字符串
请注意,我只更改了打印语句。我只测试了成功和未在断点处的错误。

相关问题