我想跟踪从另一个第 3 方库发出的对某些第 3 方库的调用。
示例:我想跟踪对库 A 的调用。我的应用程序静态链接库 B,而库 B 又静态链接到库 A。所以基本上我拥有的是 libAB.a
在动态链接的情况下,我可以编写带有函数包装器的库 A2,我想跟踪库 A 并使用 LD_PRELOAD=A2.so。然后,我的包装器将被调用,我将看到跟踪。 就我而言,我不能使用动态链接。
是否可以使用静态链接实现相同的目的?
在理想情况下,我想将我的应用程序与 libAB.a 和跟踪库 libA2.a 链接起来并获取跟踪。
谢谢,
罗布斯塔
最佳答案
好的,我找到了:)
男人
--wrap symbol
Use a wrapper function for symbol. Any undefined reference to symbol will be resolved to "__wrap_symbol". Any undefined ref‐
erence to "__real_symbol" will be resolved to symbol.
This can be used to provide a wrapper for a system function. The wrapper function should be called "__wrap_symbol". If it
wishes to call the system function, it should call "__real_symbol".
Here is a trivial example:
void *
__wrap_malloc (size_t c)
{
printf ("malloc called with %zu\n", c);
return __real_malloc (c);
}
If you link other code with this file using --wrap malloc, then all calls to "malloc" will call the function "__wrap_malloc"
instead. The call to "__real_malloc" in "__wrap_malloc" will call the real "malloc" function.
关于c - 是否可以将调用包装到静态链接的第 3 方库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3053472/