Search code examples
cstaticlldb

How can I look up a static variable in a function with lldb?


I have a function like this (I've omitted most of the function for brevity):

const char *
insn_name(VALUE i)
{
    static const char x[] =
        "nop" "\0"
        "getlocal" "\0"
        "setlocal" "\0";
    ...
}

I would like to get access to x from lldb. nm is able to find the symbol:

$ nm -C miniruby | grep insn_name.x
00000001002cafa0 s _insn_name.x
00000001002ccbb0 s _insn_name.x
00000001002cdaf0 s _insn_name.x

I can print the address from lldb just fine:

(lldb) p (char*)0x1002cafa0
(char *) $45 = 0x00000001002cafa0 "nop"

I can look up information about the address:

(lldb) target modules lookup -Av -a 0x1002cafa0
      Address: miniruby[0x00000001002cafa0] (miniruby.__TEXT.__const + 11440)
      Summary: miniruby`insn_name.x
       Module: file = "/Users/aaron/git/ruby/miniruby", arch = "x86_64"
       Symbol: id = {0x0000263f}, range = [0x00000001002cafa0-0x00000001002cbb30), name="insn_name.x"

lldb must know about this thing, but I can't seem to figure out the command to look it up by name. Eventually I would like to find this address via Python in an lldb extension.

Thank you.


Solution

  • Like this:

    image lookup -s insn_name.x
    

    where image is a builtin lldb synonym for target modules.