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
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
_______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits