[Lldb-commits] [PATCH] D117237: [lldb] Use __lldb_init_module instead of "if lldb.debugger" idiom

Jonas Devlieghere via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Thu Jan 13 10:52:45 PST 2022


JDevlieghere added inline comments.


================
Comment at: lldb/docs/use/python-reference.rst:586
 the module is loaded allowing you to add whatever commands you want into the
-current debugger. Note that this function will only be run when using the LLDB
-command command script import, it will not get run if anyone imports your
-module from another module. If you want to always run code when your module is
-loaded from LLDB or when loaded via an import statement in python code you can
-test the lldb.debugger object, since you imported the module at the top of the
-python ls.py module. This test must be in code that isn't contained inside of
-any function or class, just like the standard test for __main__ like all python
-modules usually do. Sample code would look like:
-
-::
-
-  if __name__ == '__main__':
-      # Create a new debugger instance in your module if your module
-      # can be run from the command line. When we run a script from
-      # the command line, we won't have any debugger object in
-      # lldb.debugger, so we can just create it if it will be needed
-      lldb.debugger = lldb.SBDebugger.Create()
-  elif lldb.debugger:
-      # Module is being run inside the LLDB interpreter
-      lldb.debugger.HandleCommand('command script add -f ls.ls ls')
-      print 'The "ls" python command has been installed and is ready for use.'
+current debugger.
 
----------------
kastiglione wrote:
> jingham wrote:
> > I think it's okay to show the "if __name__  == '__main__': part of this example, but it should be:
> > 
> > 
> > ```
> > if __name__ == '__main__':
> >       # Create a new debugger instance in your module if your module
> >       # can be run from the command line. When we run a script from
> >       # the command line, we won't have any debugger object in
> >       # lldb.debugger, so we can just create it if it will be needed
> >       lldb.debugger = lldb.SBDebugger.Create()
> >       # Now do whatever work this module would do when run as a command
> >       # Now dispose of the debugger you just made.
> >       lldb.SBDebugger.Destroy(debugger)
> > 
> > 
> > ```That seems useful trick.
> good point
Let's also discourage people from setting `lldb.debugger` and instead do what crashlog does:

```
      debugger = lldb.SBDebugger.Create()
      # Now do whatever work this module would do when run as a command
      # Now dispose of the debugger you just made.
      lldb.SBDebugger.Destroy(debugger)
```


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117237/new/

https://reviews.llvm.org/D117237



More information about the lldb-commits mailing list