This is an archive of the discontinued LLVM Phabricator instance.

[lldb] Use __lldb_init_module instead of "if lldb.debugger" idiom
ClosedPublic

Authored by kastiglione on Jan 13 2022, 10:33 AM.

Details

Summary

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 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.

Diff Detail

Event Timeline

kastiglione requested review of this revision.Jan 13 2022, 10:33 AM
kastiglione created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptJan 13 2022, 10:33 AM
jingham added inline comments.Jan 13 2022, 10:41 AM
lldb/docs/use/python-reference.rst
604–613

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.
lldb/examples/darwin/heap_find/heap.py
1518

Do you know what these commented-out lines are for?

fix a few more

kastiglione added inline comments.Jan 13 2022, 10:46 AM
lldb/docs/use/python-reference.rst
604–613

good point

lldb/examples/darwin/heap_find/heap.py
1518

I don't, no. Of the three, only the section_ptr_refs exists in this file. The other two, heap and stack_ptr_refs do not exist. I can remove the latter two, and leave the commented out line for the existing section_ptr_refs function.

JDevlieghere added inline comments.Jan 13 2022, 10:52 AM
lldb/docs/use/python-reference.rst
604–613

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)

restore if __name__ == '__main__' example

updates from reviewers

lldb/docs/use/python-reference.rst
604–613

I just noticed that myself. Thanks, I will add the part about Destroy.

kastiglione edited the summary of this revision. (Show Details)Jan 13 2022, 11:01 AM
kastiglione marked 2 inline comments as done.Jan 13 2022, 11:05 AM
This revision is now accepted and ready to land.Jan 13 2022, 11:48 AM
clayborg added inline comments.Jan 13 2022, 2:12 PM
lldb/docs/use/python-reference.rst
600

We need to initialize the debugger first before doing anything or no plug-ins will be available:

610

We also need to terminate using the static terminate function.

add @clayborg's improvements

kastiglione marked 2 inline comments as done.Jan 13 2022, 4:36 PM

thanks @clayborg

This revision was landed with ongoing or failed builds.Jan 13 2022, 4:38 PM
This revision was automatically updated to reflect the committed changes.

Thanks for doing this!