提问人:刘肃超 提问时间:10/18/2023 最后编辑:刘肃超 更新时间:10/18/2023 访问量:51
wrap_malloc 函数无法拦截某些函数,例如 backtrace_symbols
The wrap_malloc function is unable to intercept certain functions, such as backtrace_symbols
问:
这是我编写的代码示例
#include <stdio.h>
#include <stdlib.h>
#include <execinfo.h>
void __real_free(void *ptr);
void *__real_calloc(size_t num, size_t size);
void *__real_malloc(size_t size);
void *__real_realloc(void *mem_address, size_t newsize);
void *__wrap_malloc(size_t size) {
printf("__wrap_malloc11\n");
return (char *)__real_malloc(size);
}
void __wrap_free(void *ptr) {
printf("__wrap_free\n");
__real_free(ptr);
}
void *__wrap_calloc(size_t num, size_t size) {
printf("__wrap_calloc\n");
return __real_calloc(num,size);
}
void *__wrap_realloc(void *mem_address, size_t size) {
printf("__wrap_realloc\n");
return __real_realloc(mem_address,size);
}
int main(int argc, char* argv[]) {
void *callstack[128];
int frames = backtrace(callstack, 128);
char **strs = backtrace_symbols(callstack, frames);
if (strs) {
printf("The caller is:%s %p\n", strs[1], strs);
free(strs);
}
return 0;
}
编译方法:
gcc main.c -Wl,--wrap=malloc -Wl,--wrap=free -Wl,--wrap=calloc -Wl,--wrap=realloc
运行结果:
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7) [0x7fb37d52fc87] 0x55d268841940
__wrap_free
交叉编译器版本:
gcc -v
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/7/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 7.5.0-3ubuntu1~18.04' --with-bugurl=file:///usr/share/doc/gcc-7/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++ --prefix=/usr --with-gcc-major-version-only --program-suffix=-7 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --enable-default-pie --with-system-zlib --with-target-system-zlib --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)
我需要做些什么才能让backtrace_symbols打电话给__wrap_malloc?
我希望你能通过分析这个问题来帮助我,如果可能的话,帮助我解决它就好了。
答: 暂无答案
评论
backtrace_symbols()
malloc()
calloc()
realloc()
strdup()
--wrap
backtrace_symbols()
malloc()