The wrap_malloc function is unable to intercept certain functions, such as backtrace_symbols

63 Views Asked by At

Here's an example of the code I wrote

#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;
}

Compile method:

gcc main.c -Wl,--wrap=malloc -Wl,--wrap=free -Wl,--wrap=calloc  -Wl,--wrap=realloc

Run the result:

/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xe7) [0x7fb37d52fc87] 0x55d268841940
__wrap_free

Cross-compiler version:

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) 

What do I have to do to get backtrace_symbols to call __wrap_malloc?

I hope you can help me by analyzing this problem, and it would be nice to help me solve it if possible.

0

There are 0 best solutions below