This is an archive of the discontinued LLVM Phabricator instance.

[lldb] Add lldb.find helper function
AcceptedPublic

Authored by kastiglione on Feb 21 2022, 6:01 PM.

Details

Summary

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.

Diff Detail

Event Timeline

kastiglione requested review of this revision.Feb 21 2022, 6:01 PM
kastiglione created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptFeb 21 2022, 6:01 PM

Nice. tbh when I'm SB API scripting, I am often sitting in include/lldb/API and grepping for keywords, because I can't remember exactly which class provides the API, or what arguments it takes or whatever.

mib accepted this revision.Feb 21 2022, 10:13 PM

LGTM! Could you add a little test to exercise the command ?

This revision is now accepted and ready to land.Feb 21 2022, 10:13 PM

@jasonmolenda one advantage this has over grepping is that the python specific extensions to SB are visible.

kastiglione added inline comments.Feb 22 2022, 10:30 AM
lldb/bindings/python/python-extensions.swig
528–533

oh, I need to figure out the right way to manage imports in this swig extension. It seems that the re module is used (as a default argument) before importing it, and yet this code works.

mib added inline comments.Feb 22 2022, 10:33 AM
lldb/bindings/python/python-extensions.swig
528–533

Can't you just import it after the %pythoncode %{ statement instead of doing it in the function ?

kastiglione added inline comments.Feb 22 2022, 10:36 AM
lldb/bindings/python/python-extensions.swig
528–533

That's what I plan to try. I figure it's best to put a %pythoncode %{ at the top of the file, where all imports can go, like a normal source file.

mib added inline comments.Feb 22 2022, 10:40 AM
lldb/bindings/python/python-extensions.swig
528–533

👍🏼

@jasonmolenda one advantage this has over grepping is that the python specific extensions to SB are visible.

That's true too. I mostly meant my comment as in, "I find this feature very helpful when writing SB API python, and this provides it to people who don't have an lldb checkout sitting around".

This is really great, this is going to save me a lot of time.

I wonder if we should take this even further and make it a first class command in lldb, similar to apropos. (I wouldn't make it part of apropos, because it can already be overwhelming.) By making it its own command, we can have the same command for all the supported scripting languages. Similar to script it would look at the current scripting language by default, but you could also search other supported language with a flag. I think the implementation could remain the same. Every script interpreter plugin could have a find method that it implements (or doesn't). For the Python case, it could call the find command you have here.

What do you think?

It's too bad script doesn't have subcommands, because that might have been a natural place for such a command. Are there any commands you think this could be added? Or would this have to be a new top level command? A new top-level command seems unfortunate.

Speaking of apropos, this tool could have an apropos-like mode where it also searches docstrings in addition to method/property names. I am not sure how useful that would be, but it's worth mentioning.

Could you use help --scripting (help -s) or maybe help --api? Then you could also do apropos -s if you ended up adding an apropos feature in the script interface, which would be consistent and fairly discoverable. Then for folks that used it all the time they could alias it to something shorter (though h -s isn't really so bad...)

It's too bad script doesn't have subcommands, because that might have been a natural place for such a command. Are there any commands you think this could be added? Or would this have to be a new top level command? A new top-level command seems unfortunate.

Speaking of apropos, this tool could have an apropos-like mode where it also searches docstrings in addition to method/property names. I am not sure how useful that would be, but it's worth mentioning.

I like Jim's suggestion

mib added a comment.Feb 25 2022, 10:43 AM

I like Jim's suggestion

Same same !

mib added a comment.Feb 25 2022, 10:45 AM

Jim's suggestion made me think that if we make a subcommand for this, it would be nice to have some completion handling. I know it would probably require you to move away from the python function but this is just a suggestion for later improvements.