gpt4 book ai didi

c++ - 为什么 tcmalloc 不打印通过 dlopen 提供的函数名称

转载 作者:太空宇宙 更新时间:2023-11-04 13:31:43 26 4
gpt4 key购买 nike

我有下一个项目: main.cpp

#include <iostream>
#include <cstddef>
#include <dlfcn.h>

int main()
{
void* handle = dlopen("./shared_libs/libshared.so", RTLD_LAZY);
if (NULL == handle)
{
std::cerr << "Cannot open library: " << dlerror() << '\n';
return -1;
}


typedef int (*foo_t)(const std::size_t);
foo_t foo = reinterpret_cast<foo_t>(dlsym(handle, "foo"));


const char* dlsym_error = dlerror();
if (dlsym_error)
{
std::cerr << "Cannot load symbol 'foo': " << dlsym_error << '\n';
dlclose(handle);
return -2;
}


std::cout << "call foo" << std::endl;
foo(10);


dlclose(handle);


return 0;
}

共享.cpp:

#include <cstddef>
#include <iostream>


extern "C"
{
int foo(const std::size_t size)
{
int b = size / size;
int* a = new int[size];
std::cout << "leaky code here" << std::endl;
}
}

和生成文件:

all:
g++ -fPIC -g -c shared.cpp
g++ -shared -o shared_libs/libshared.so -g shared.o
g++ -L shared_libs/ -g main.cpp -ldl

我使用 tcmalloc 调试这个测试程序,它动态加载 libshared.so:foo 并执行 it.run 命令:LD_PRELOAD=/usr/local/lib/libtcmalloc.so HEAPCHECK=正常./a.out

1 个最大的漏洞:

  • 使用本地文件 ./a.out。
  • 从以下位置分配的 1 个对象中有 40 字节泄漏:
  • @7fe3460bd9ba 0x00007fe3460bd9ba
  • @400b43 主
  • @7fe346c33ec5 __libc_start_main
  • @400999 _开始
  • @0 _init

为什么我得到地址 0x00007fe3460bd9ba 而不是 foo 函数中的行?请帮忙

附言我尝试将 gdb 与 LD_PRELOAD=.../tcmalloc.so 一起使用,但我得到:“有人正在 ptrace() 测试我们;将自行关闭 Turning perftools heap leak checking off”

最佳答案

尝试删除 dlclose 调用。

这是一个已知问题,堆检查器和分析器无法处理卸载共享对象。

关于c++ - 为什么 tcmalloc 不打印通过 dlopen 提供的函数名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31166005/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com