[Lldb-commits] [PATCH] D120292: [lldb] Add lldb.find helper function

Dave Lee via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Mon Feb 21 18:01:45 PST 2022


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

Add `lldb.find()`, a function for interactive lookup and discovery of SB API.

>From the lldb console, this function can be used as follows:

  (lldb) script lldb.find("register")
  lldb.SBFrame.FindRegister
  lldb.SBFrame.GetRegisters
  lldb.SBFrame.get_registers_access
  lldb.SBFrame.register
  lldb.SBFrame.registers
  
  (lldb) sc lldb.find("Disassemble")
  Help on function Disassemble in lldb.SBFrame:
  
  lldb.SBFrame.Disassemble = Disassemble(self) -> 'char const *'
      Disassemble(SBFrame self) -> char const *

In the first case, multiple matches were printed. In the second case, only one
match was found, and so its `help()` documentation was printed.

The results compose with the builtin `help()` function:

  (lldb) sc help(lldb.SBFrame.FindRegister)
  ...

If called often enough, a `command regex` can come in handy:

  command regex findapi
  s/(.+)/script lldb.find("%1")/

Which allows it to be called as `findapi register`.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D120292

Files:
  lldb/bindings/python/python-extensions.swig


Index: lldb/bindings/python/python-extensions.swig
===================================================================
--- lldb/bindings/python/python-extensions.swig
+++ lldb/bindings/python/python-extensions.swig
@@ -523,9 +523,39 @@
 
     def __ne__(self, other):
         return not self.__eq__(other)
-%}
 
-%pythoncode %{
+
+def find(pattern, flags=re.I):
+    """
+    Find matching SB methods/properties. See also Python's builtin `help()`.
+    """
+    import lldb
+    import re
+
+    regex = re.compile(pattern, flags)
+
+    # Gather matching methods and properties.
+    methods = []
+
+    for attr in dir(lldb):
+        value = getattr(lldb, attr)
+        if not isinstance(value, type):
+            continue
+
+        class_name = attr
+        class_type = value
+        for method_name in dir(class_type):
+            if regex.search(method_name):
+                methods.append(f"lldb.{class_name}.{method_name}")
+
+    if len(methods) == 1:
+        # For one match, print its help.
+        help(methods[0])
+        return
+
+    # Print the name of each matching method or property.
+    print(*methods, sep="\n")
+
 
 class SBSyntheticValueProvider(object):
     def __init__(self,valobj):
@@ -547,10 +577,6 @@
         return False
 
 
-%}
-
-%pythoncode %{
-
 # given an lldb.SBBasicType it returns a tuple
 # (is_numeric, is_signed)
 # the value of is_signed is undefined if is_numeric == false


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D120292.410423.patch
Type: text/x-patch
Size: 1433 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20220222/0d7c2cfd/attachment-0001.bin>


More information about the lldb-commits mailing list