相关疑难解决方法(0)

LD_PRELOAD只适用于malloc,而不是免费的

我试图通过LD_PRELOAD设置malloc/free/calloc/realloc等与一些插入器.在我的小测试中,malloc即使free被检测到,也似乎只是插入(见输出).

我希望输出包含一行"NANO:free(x)" - 但这行不见了.

特定

// compile with: gcc test.cc
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[]) {

    void* p = malloc(123);
    printf("HOST p=%p\n", p);
    free(p);
}
Run Code Online (Sandbox Code Playgroud)

// compile with:  g++ -O2 -Wall -fPIC -ldl -o libnano.so -shared main.cc
#include <stdio.h>
#include <dlfcn.h>

typedef void *(*MallocFunc)(size_t size);
typedef void (*FreeFunc)(void*);

// Original functions
static MallocFunc real_malloc = NULL;
static FreeFunc real_free = NULL;

static void __nano_init(void) {
    // Override original functions
    real_malloc = …
Run Code Online (Sandbox Code Playgroud)

c++ malloc free dynamic-linking ld-preload

6
推荐指数
1
解决办法
1915
查看次数

标签 统计

c++ ×1

dynamic-linking ×1

free ×1

ld-preload ×1

malloc ×1