lldb:如何从特定的库/框架调用函数 [英] lldb: how to call a function from a specific library/framework

查看:147
本文介绍了lldb:如何从特定的库/框架调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:在项目中,我们拥有特定于框架/动态库的本地化功能。这是他们有相同的名字,但从不同的捆绑包/文件夹获取资源



我想从特定的库调用函数,类似于:

  lldb> p my_audio_engine.framework :: GetL10nString(stringId); 
lldb> expr --shlib my_audio_engine.framework - GetL10nString();
lldb> p my_audio_engine`L10N_Utils :: GetString(40000)

但所有这些变体都不起作用。



在希望存在相同语义(如果存在)的标记中添加gdb也可以在lldb上运行。

解决方案

lldb的表达式解析器当前没有与gdb的foo.c :: function元符号等效的功能来编码来自特定源文件的函数。



请随时在bugreporter.apple.com上提交请求此问题的错误。它会被骗到我刚刚提交的那个,但是dups是功能的投票,我们还没有得到这个,但除了我之外没有人要求它。



对于随机数,您必须手动完成。这是一个愚蠢的例子,用于调用printf,我碰巧知道它在OS X的libsystem_c.dylib中。首先,我在我感兴趣的共享库中找到地址:

 (lldb)image lookup -vn printf libsystem_c.dylib 
在/usr/lib/system/libsystem_c.dylib中找到1个匹配:
地址:libsystem_c.dylib [0x0000000000042948](libsystem_c.dylib .__ TEXT .__ text + 266856)
摘要:libsystem_c.dylib`printf
模块:file =/usr/lib/system/libsystem_c.dylib,arch =x86_64
符号:id = {0x00000653},范围= [0x00007fff91307948-0x00007fff91307a2c),name =printf

第一个地址(地址下的地址)是dylib中函数的地址,而不是它在正在运行的程序中加载的地址。这并不是很有用。如果我想将它应用到文件地址,我可以计算库的负载偏移量,但幸运的是,符号地址范围中的第一个地址是正在运行的程序中的地址,所以我不必这样做。 0x00007fff91307948是我想要的地址。



现在我想打电话给那个地址。我这样做是分两步进行的,因为它使得投射更容易,例如:

 (lldb)expr typedef int(* $ printf_type) (const char *,...)
(lldb)expr $ printf_type $ printf_function =($ printf_type)0x00007fff91307948

现在我有一个函数可以反复调用:

 (lldb)expr $ printf_function( Hello world%d times.\\\
,400)
Hello world 400 times。
(int)$ 2 = 23

如果您打算一遍又一遍地执行此操作,您可以编写一个Python函数,从感兴趣的库中查找符号,并构造调用正确函数的表达式。 Python API包括调用来从特定模块获取符号(lldb--说可载入的二进制图像),获取它们的地址,评估表达式等。

Problem: In project we have localization functions which are specific to a framework/dynamic library. That is they have identical name but fetch resources from different bundles/folders

I'd want to call a function from a specific library, something similar to:

lldb> p my_audio_engine.framework::GetL10nString( stringId );
lldb> expr --shlib my_audio_engine.framework -- GetL10nString();
lldb> p my_audio_engine`L10N_Utils::GetString(40000)

but all these variants don't work.

Adding gdb in tags hoping the same semantic if exists will work on lldb as well.

解决方案

lldb's expression parser doesn't currently have the equivalent of gdb's foo.c::function meta-symbol to encode a function from a specific source file.

Please feel free to file a bug requesting this at bugreporter.apple.com. It will get duped to the one I filed a while ago, but dups are votes for features, and we haven't gotten around to this one yet 'cause nobody but me asked for it...

For the nonce, you will have to do this by hand. Here's a silly example for calling printf, which I happen to know is in libsystem_c.dylib on OS X. First, I find the address in the shared library I am interested in:

(lldb) image lookup -vn printf libsystem_c.dylib
1 match found in /usr/lib/system/libsystem_c.dylib:
        Address: libsystem_c.dylib[0x0000000000042948] (libsystem_c.dylib.__TEXT.__text + 266856)
        Summary: libsystem_c.dylib`printf
         Module: file = "/usr/lib/system/libsystem_c.dylib", arch = "x86_64"
         Symbol: id = {0x00000653}, range = [0x00007fff91307948-0x00007fff91307a2c), name="printf"

The first address (the one under Address) is the address of the function in the dylib, not where it got loaded in the running program. That's not immediately useful. I could calculate the library's load offset if I wanted to and apply it to the file address, but fortunately the first address in the Symbol's address range is the address in the running program so I don't have to. 0x00007fff91307948 is the address I want.

Now I want to call that address. I do this in two steps because it makes the casting easier, like:

(lldb) expr typedef int (*$printf_type)(const char *, ...)
(lldb) expr $printf_type $printf_function = ($printf_type) 0x00007fff91307948

Now I have a function I can call over and over:

(lldb) expr $printf_function("Hello world %d times.\n", 400)
Hello world 400 times.
(int) $2 = 23

If you are going to do this over and over, you can write a Python function that finds the symbol out of the library of interest, and constructs the expression that calls the right function. The Python API's include calls to get symbols from a particular module (lldb-speak for loadable binary images), get their addresses, evaluate expressions, etc.

这篇关于lldb:如何从特定的库/框架调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆