wrap function, 在debug時很好用,
說穿了就是把呼叫某個function call把它導到自己寫的function
適用於無法改source code的 .a
要怎魔使用, 先請教男人對ld的看法....
其中有一段是這麼說的
--wrap symbol
Use a wrapper function for symbol. Any undefined reference to symâ€
bol will be resolved to "__wrap_symbol". Any undefined reference
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.
You may wish to provide a "__real_malloc" function as well, so that
links without the --wrap option will succeed. If you do this, you
should not put the definition of "__real_malloc" in the same file
as "__wrap_malloc"; if you do, the assembler may resolve the call
before the linker has a chance to wrap it to "malloc".
以上面為例, 當遇到一堆.a都直接呼叫native API的malloc跟free, 這時候要如何檢查有無memory leak??
如果有wrap functions (__wrap_malloc)在呼叫真正的malloc (__real_malloc)後, 把address跟size印出來,
用完後呼叫wrap functions (__wrap_free)真正釋放(__real_free)前再把address印出
這樣就可以檢查有無leaking的現象, 當然如果可以配合thread info或__builtin_return_address會更好
要注意的是在LDFLAGS加入-Wl,--wrap,malloc --wrap,free 這兩段!!!
留言列表