问题描述
我有下一个项目:
main.cpp
I have next some project: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;
}
shared.cpp:
shared.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;
}
}
和Makefile:
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 = normal ./a.out
I use tcmalloc for debug this test program, which load dynamically libshared.so:foo and execute it.run command:LD_PRELOAD=/usr/local/lib/libtcmalloc.so HEAPCHECK=normal ./a.out
最大的漏洞:
- 使用本地文件./a.out。
- 在1个对象中有40个字节的泄漏:
- @ 7fe3460bd9ba 0x00007fe3460bd9ba
- @ 400b43 main
- @ 7fe346c33ec5 __libc_start_main
- @ 400999 _start
- @ 0 _init
- Using local file ./a.out.
- Leak of 40 bytes in 1 objects allocated from:
- @ 7fe3460bd9ba 0x00007fe3460bd9ba
- @ 400b43 main
- @ 7fe346c33ec5 __libc_start_main
- @ 400999 _start
- @ 0 _init
为什么我得到地址0x00007fe3460bd9ba而不是foo函数中的行?
请帮助
Why I get address 0x00007fe3460bd9ba instead of line in foo function?please help
P.s。我尝试使用gdb与LD_PRELOAD = ... / tcmalloc.so,但我得到:
有人是ptrace()我们;将自己关闭打开perftools堆泄漏检查
P.s. I tried to use gdb with LD_PRELOAD=.../tcmalloc.so, but I get:"Someone is ptrace()ing us; will turn itself off Turning perftools heap leak checking off"
推荐答案
尝试删除dlclose调用。
Try removing dlclose call.
堆检查器&分析器不能处理卸载的
共享对象。
It's known issue that heap checker & profilers can't handle unloadedshared objects.
这篇关于为什么tcmalloc不打印通过dlopen提供的函数名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!