jingham created this revision.
jingham added reviewers: JDevlieghere, kastiglione.
Herald added a project: All.
jingham requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

In a situation where you have a lot of breakpoints set, some enabled and some 
not, and you want to continue till you hit one breakpoint but don't want to hit 
any of the others in the course of doing that, you have to hand disable the 
other breakpoints, then continue, then re-enable the ones that were enabled 
when you started (but not the others).  That's tedious but easily done with 
scripting.  I wrote up this little example for somebody and it seems generally 
useful.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D125148

Files:
  lldb/examples/python/cont_to_bkpt.py


Index: lldb/examples/python/cont_to_bkpt.py
===================================================================
--- /dev/null
+++ lldb/examples/python/cont_to_bkpt.py
@@ -0,0 +1,63 @@
+import lldb
+
+class ContinueToBreakpoint:
+    def __init__(self, debugger, unused):
+        self.dbg = debugger
+        self.interp = debugger.GetCommandInterpreter()
+        
+    def __call__(self, debugger, command, exe_ctx, result):
+        bkpt_strs = command.split()
+        bkpt_ids = []
+        for str in bkpt_strs:
+            try:
+                int_val = int(str)
+            except:
+                result.SetError("Input must be breakpoint id's: 
{0}".format(str))
+                result.SetStatus(lldb.eReturnStatusFailed)
+                return
+
+            bkpt_ids.append(int(str))
+
+        if len(bkpt_ids) == 0:
+            result.SetError("No breakpoint to run to")
+            result.SetStatus(lldb.eReturnStatusFailed)
+            return
+        target = exe_ctx.target
+        if  not exe_ctx.target.IsValid():
+            result.SetError("Need a valid target")
+            result.SetStatus(lldb.eReturnStatusFailed)
+            return
+        if not exe_ctx.process.IsValid():
+            result.SetError("Need a valid process")
+            result.SetStatus(lldb.eReturnStatusFailed)
+            return
+        
+        disabled_bkpts = []
+        for idx in range(0, exe_ctx.target.num_breakpoints):
+            bkpt = target.GetBreakpointAtIndex(idx)
+            bkpt_id = bkpt.GetID()
+            if not bkpt_id in bkpt_ids:
+                if bkpt.enabled:
+                    disabled_bkpts.append(bkpt)
+                    bkpt.enabled = False
+        old_async = debugger.GetAsync()
+        debugger.SetAsync(False)
+        exe_ctx.process.Continue()
+        strm = lldb.SBStream()
+        if exe_ctx.process.state == lldb.eStateExited:
+            result.PutCString("process exited with state: 
{0}".format(exe_ctx.process.exit_state))
+        else:
+            thread = exe_ctx.process.GetSelectedThread()
+            thread.GetStatus(strm)
+            result.PutCString(strm.GetData())
+
+        result.SetStatus(lldb.eReturnStatusSuccessFinishResult)
+        for bkpt in disabled_bkpts:
+            bkpt.enabled = True;
+        debugger.SetAsync(old_async)
+
+    def get_short_help(self):
+        return "takes a list of breakpoint ID's and continues the process 
until one of the breakpoint IDs passed in is hit"
+
+def __lldb_init_module(debugger, unused):
+    debugger.HandleCommand("command script add -c {0}.ContinueToBreakpoint 
continue_to_bkpts".format(__name__))


Index: lldb/examples/python/cont_to_bkpt.py
===================================================================
--- /dev/null
+++ lldb/examples/python/cont_to_bkpt.py
@@ -0,0 +1,63 @@
+import lldb
+
+class ContinueToBreakpoint:
+    def __init__(self, debugger, unused):
+        self.dbg = debugger
+        self.interp = debugger.GetCommandInterpreter()
+        
+    def __call__(self, debugger, command, exe_ctx, result):
+        bkpt_strs = command.split()
+        bkpt_ids = []
+        for str in bkpt_strs:
+            try:
+                int_val = int(str)
+            except:
+                result.SetError("Input must be breakpoint id's: {0}".format(str))
+                result.SetStatus(lldb.eReturnStatusFailed)
+                return
+
+            bkpt_ids.append(int(str))
+
+        if len(bkpt_ids) == 0:
+            result.SetError("No breakpoint to run to")
+            result.SetStatus(lldb.eReturnStatusFailed)
+            return
+        target = exe_ctx.target
+        if  not exe_ctx.target.IsValid():
+            result.SetError("Need a valid target")
+            result.SetStatus(lldb.eReturnStatusFailed)
+            return
+        if not exe_ctx.process.IsValid():
+            result.SetError("Need a valid process")
+            result.SetStatus(lldb.eReturnStatusFailed)
+            return
+        
+        disabled_bkpts = []
+        for idx in range(0, exe_ctx.target.num_breakpoints):
+            bkpt = target.GetBreakpointAtIndex(idx)
+            bkpt_id = bkpt.GetID()
+            if not bkpt_id in bkpt_ids:
+                if bkpt.enabled:
+                    disabled_bkpts.append(bkpt)
+                    bkpt.enabled = False
+        old_async = debugger.GetAsync()
+        debugger.SetAsync(False)
+        exe_ctx.process.Continue()
+        strm = lldb.SBStream()
+        if exe_ctx.process.state == lldb.eStateExited:
+            result.PutCString("process exited with state: {0}".format(exe_ctx.process.exit_state))
+        else:
+            thread = exe_ctx.process.GetSelectedThread()
+            thread.GetStatus(strm)
+            result.PutCString(strm.GetData())
+
+        result.SetStatus(lldb.eReturnStatusSuccessFinishResult)
+        for bkpt in disabled_bkpts:
+            bkpt.enabled = True;
+        debugger.SetAsync(old_async)
+
+    def get_short_help(self):
+        return "takes a list of breakpoint ID's and continues the process until one of the breakpoint IDs passed in is hit"
+
+def __lldb_init_module(debugger, unused):
+    debugger.HandleCommand("command script add -c {0}.ContinueToBreakpoint continue_to_bkpts".format(__name__))
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to