Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find a function's memory address with lldb?

Tags:

gdb

lldb

In GDB, I can use "info line func_name" to get the memory address of func_name, and then use "set $PC=memory_address" to start debugging this function. How do I do the same within lldb? Thanks in advance!

like image 744
XinXin LIU Avatar asked Aug 17 '12 09:08

XinXin LIU


Video Answer


1 Answers

The command in lldb is "image lookup". I think an example of "info func" <-> "image lookup" was recently added to the lldb/gdb command page - http://lldb.llvm.org/lldb-gdb.html

e.g.

(lldb) im loo -n puts
1 match found in /usr/lib/system/libsystem_c.dylib:
        Address: libsystem_c.dylib[0x0000000000011d9a] (libsystem_c.dylib.__TEXT.__text + 69850)
        Summary: libsystem_c.dylib`puts
(lldb) 

although this is only showing you the offset in libsystem_c.dylib here (0x11d9a) -- to see the actual load address you would need to use the "-v" option to image lookup which will show the range of addresses that puts covers. Or you could do this more directly with the back tick notation in lldb,

(lldb) reg read pc
     rip = 0x0000000100000f2b  a.out`main + 11 at a.c:3
(lldb) reg write pc `(void(*)())puts`
(lldb) reg read pc
     rip = 0x00007fff99ce1d9a  libsystem_c.dylib`puts

OK I had to cast puts() because lldb needed a function prototype here - not super convenient, but if it's one of your own functions that isn't needed:

(lldb) reg write pc `main`
(lldb) reg read pc
     rip = 0x0000000100000f20  a.out`main at a.c:2
like image 56
Jason Molenda Avatar answered Oct 26 '22 01:10

Jason Molenda