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

Update examples and docs to demonstrate using `__lldb_init_module` instead of
the idiom that checks for `lldb.debugger` at the top-level.

  if __name__ == '__main__':
      ...
  elif lldb.debugger:
      ...

Is replaced with:

  if __name__ == '__main__':
      ...
  
  def __lldb_init_module(debugger, internal_dict):
      ...

This change is for two reasons. First, it's generally encouraged not to only
use the convenience singletons (`lldb.{debugger,process,target,etc}`)
interactively from the `script` command. Second, there's a bug where
registering a python class as a command (using `command script add -c ...`),
result in the command not being runnable. Note that registering function-backed
commands does not have this bug.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D117237

Files:
  lldb/docs/use/python-reference.rst
  lldb/examples/darwin/heap_find/heap.py
  lldb/examples/python/delta.py
  lldb/examples/python/gdbremote.py
  lldb/examples/python/memory.py
  lldb/examples/python/types.py

Index: lldb/examples/python/types.py
===================================================================
--- lldb/examples/python/types.py
+++ lldb/examples/python/types.py
@@ -351,7 +351,7 @@
             continue
         verify_types(target, options)
 
-elif getattr(lldb, 'debugger', None):
-    lldb.debugger.HandleCommand(
+def __lldb_init_module(debugger, internal_dict):
+    debugger.HandleCommand(
         'command script add -f types.check_padding_command check_padding')
     print('"check_padding" command installed, use the "--help" option for detailed help')
Index: lldb/examples/python/memory.py
===================================================================
--- lldb/examples/python/memory.py
+++ lldb/examples/python/memory.py
@@ -270,8 +270,9 @@
 
 if __name__ == '__main__':
     print('error: this script is designed to be used within the embedded script interpreter in LLDB')
-elif getattr(lldb, 'debugger', None):
+
+def __lldb_init_module(debugger, internal_dict):
     memfind_command.__doc__ = create_memfind_options().format_help()
-    lldb.debugger.HandleCommand(
+    debugger.HandleCommand(
         'command script add -f memory.memfind_command memfind')
     print('"memfind" command installed, use the "--help" option for detailed help')
Index: lldb/examples/python/gdbremote.py
===================================================================
--- lldb/examples/python/gdbremote.py
+++ lldb/examples/python/gdbremote.py
@@ -1619,13 +1619,11 @@
     else:
         parse_gdb_log(sys.stdin, options)
 
-else:
-    import lldb
-    if lldb.debugger:
-        # This initializer is being run from LLDB in the embedded command interpreter
-        # Add any commands contained in this module to LLDB
-        lldb.debugger.HandleCommand(
-            'command script add -f gdbremote.start_gdb_log start_gdb_log')
-        lldb.debugger.HandleCommand(
-            'command script add -f gdbremote.stop_gdb_log stop_gdb_log')
-        print('The "start_gdb_log" and "stop_gdb_log" commands are now installed and ready for use, type "start_gdb_log --help" or "stop_gdb_log --help" for more information')
+def __lldb_init_module(debugger, internal_dict):
+    # This initializer is being run from LLDB in the embedded command interpreter
+    # Add any commands contained in this module to LLDB
+    debugger.HandleCommand(
+        'command script add -f gdbremote.start_gdb_log start_gdb_log')
+    debugger.HandleCommand(
+        'command script add -f gdbremote.stop_gdb_log stop_gdb_log')
+    print('The "start_gdb_log" and "stop_gdb_log" commands are now installed and ready for use, type "start_gdb_log --help" or "stop_gdb_log --help" for more information')
Index: lldb/examples/python/delta.py
===================================================================
--- lldb/examples/python/delta.py
+++ lldb/examples/python/delta.py
@@ -125,11 +125,10 @@
     import sys
     parse_time_log_args(sys.argv[1:])
 
-else:
-    import lldb
-    if lldb.debugger:
+
+def __lldb_init_module(debugger, internal_dict):
         # This initializer is being run from LLDB in the embedded command interpreter
         # Add any commands contained in this module to LLDB
-        lldb.debugger.HandleCommand(
+        debugger.HandleCommand(
             'command script add -f delta.parse_time_log parse_time_log')
         print('The "parse_time_log" command is now installed and ready for use, type "parse_time_log --help" for more information')
Index: lldb/examples/darwin/heap_find/heap.py
===================================================================
--- lldb/examples/darwin/heap_find/heap.py
+++ lldb/examples/darwin/heap_find/heap.py
@@ -1494,30 +1494,31 @@
 if __name__ == '__main__':
     lldb.debugger = lldb.SBDebugger.Create()
 
-# Make the options so we can generate the help text for the new LLDB
-# command line command prior to registering it with LLDB below. This way
-# if clients in LLDB type "help malloc_info", they will see the exact same
-# output as typing "malloc_info --help".
-ptr_refs.__doc__ = get_ptr_refs_options().format_help()
-cstr_refs.__doc__ = get_cstr_refs_options().format_help()
-malloc_info.__doc__ = get_malloc_info_options().format_help()
-objc_refs.__doc__ = get_objc_refs_options().format_help()
-lldb.debugger.HandleCommand(
-    'command script add -f %s.ptr_refs ptr_refs' %
-    __name__)
-lldb.debugger.HandleCommand(
-    'command script add -f %s.cstr_refs cstr_refs' %
-    __name__)
-lldb.debugger.HandleCommand(
-    'command script add -f %s.malloc_info malloc_info' %
-    __name__)
-lldb.debugger.HandleCommand(
-    'command script add -f %s.find_variable find_variable' %
-    __name__)
-# lldb.debugger.HandleCommand('command script add -f %s.heap heap' % package_name)
-# lldb.debugger.HandleCommand('command script add -f %s.section_ptr_refs section_ptr_refs' % package_name)
-# lldb.debugger.HandleCommand('command script add -f %s.stack_ptr_refs stack_ptr_refs' % package_name)
-lldb.debugger.HandleCommand(
-    'command script add -f %s.objc_refs objc_refs' %
-    __name__)
-print('"malloc_info", "ptr_refs", "cstr_refs", "find_variable", and "objc_refs" commands have been installed, use the "--help" options on these commands for detailed help.')
+def __lldb_init_module(debugger, internal_dict):
+    # Make the options so we can generate the help text for the new LLDB
+    # command line command prior to registering it with LLDB below. This way
+    # if clients in LLDB type "help malloc_info", they will see the exact same
+    # output as typing "malloc_info --help".
+    ptr_refs.__doc__ = get_ptr_refs_options().format_help()
+    cstr_refs.__doc__ = get_cstr_refs_options().format_help()
+    malloc_info.__doc__ = get_malloc_info_options().format_help()
+    objc_refs.__doc__ = get_objc_refs_options().format_help()
+    debugger.HandleCommand(
+        'command script add -f %s.ptr_refs ptr_refs' %
+        __name__)
+    debugger.HandleCommand(
+        'command script add -f %s.cstr_refs cstr_refs' %
+        __name__)
+    debugger.HandleCommand(
+        'command script add -f %s.malloc_info malloc_info' %
+        __name__)
+    debugger.HandleCommand(
+        'command script add -f %s.find_variable find_variable' %
+        __name__)
+    # debugger.HandleCommand('command script add -f %s.heap heap' % package_name)
+    # debugger.HandleCommand('command script add -f %s.section_ptr_refs section_ptr_refs' % package_name)
+    # debugger.HandleCommand('command script add -f %s.stack_ptr_refs stack_ptr_refs' % package_name)
+    debugger.HandleCommand(
+        'command script add -f %s.objc_refs objc_refs' %
+        __name__)
+    print('"malloc_info", "ptr_refs", "cstr_refs", "find_variable", and "objc_refs" commands have been installed, use the "--help" options on these commands for detailed help.')
Index: lldb/docs/use/python-reference.rst
===================================================================
--- lldb/docs/use/python-reference.rst
+++ lldb/docs/use/python-reference.rst
@@ -583,27 +583,7 @@
 
 where debugger and internal_dict are as above, that function will get run when
 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.
 
 Now we can create a module called ls.py in the file ~/ls.py that will implement
 a function that can be used by LLDB's python command code:
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to