unix 如何从valgrind日志中的???from中解码地址

icnyk63a  于 2023-03-02  发布在  Unix
关注(0)|答案(1)|浏览(138)

'下面是Valgrind运行后生成的堆栈跟踪,但由于???符号的原因,无法找到代码中的可疑行。

==703294== Invalid free() / delete / delete[] / realloc()
==703294==    at 0x4C3AE59: operator delete[](void*) (vg_replace_malloc.c:1115)
==703294==    by 0x1676C7C6: __cxa_finalize (in /usr/lib64/libc-2.28.so)
==703294==    by 0xDBE8F36: ??? (in /xyz/bin/libxyz.so)
==703294==    by 0x4008E0D: _dl_fini (dl-fini.c:142)
==703294==    by 0x1676C29B: __run_exit_handlers (in /usr/lib64/libc-2.28.so)
==703294==    by 0x1676C3CF: exit (in /usr/lib64/libc-2.28.so)
==703294==    by 0x16755D8B: (below main) (in /usr/lib64/libc-2.28.so)
(...)

在valgrind运行过程中,是否需要添加任何方法或标志,以便对地址或代码行进行解码,而不是在日志中获取???。

ovfsdjhp

ovfsdjhp1#

有关构建共享库的详细信息,请查看here
如果我的图书馆里有这个

#include "lib.h"

class InvalidFree
{
public:
  InvalidFree() : mem(new int[1]) {}
  ~InvalidFree() { delete mem; }
private:
  int *mem;
};

InvalidFree invalidFree;

int foo()
{
  return 42; 
}

注意,在上面的代码中,我混淆了new []delete,我应该使用delete []
我用g++ -o liblib.so -shared -O3 lib.cpp -fPIC编译库
然后瓦尔格林给了我

==25378== Mismatched free() / delete / delete []
==25378==    at 0x402E0FB: operator delete(void*, unsigned long) (vg_replace_malloc.c:593)
==25378==    by 0x5B7DED9: __cxa_finalize (in /usr/lib64/libc-2.17.so)
==25378==    by 0x403C132: ??? (in /example/path/liblib.so)
==25378==    by 0x400FFC9: _dl_fini (in /usr/lib64/ld-2.17.so)
==25378==    by 0x5B7DB68: __run_exit_handlers (in /usr/lib64/libc-2.17.so)
==25378==    by 0x5B7DBB6: exit (in /usr/lib64/libc-2.17.so)
==25378==    by 0x5B663DB: (below main) (in /usr/lib64/libc-2.17.so)
==25378==  Address 0x5f22c80 is 0 bytes inside a block of size 4 alloc'd
==25378==    at 0x402D57F: operator new[](unsigned long) (vg_replace_malloc.c:431)
==25378==    by 0x403C07D: _GLOBAL__sub_I_lib.cpp (in /example/path/liblib.so)
==25378==    by 0x400F902: _dl_init (in /usr/lib64/ld-2.17.so)
==25378==    by 0x4001159: ??? (in /usr/lib64/ld-2.17.so)

如果我在库的编译器选项中将-O3更改为-g,则会得到

==14347== Mismatched free() / delete / delete []
==14347==    at 0x402DF1B: operator delete(void*) (vg_replace_malloc.c:584)
==14347==    by 0x522495E: InvalidFree::~InvalidFree() (lib.cpp:7)
==14347==    by 0x5C7EED9: __cxa_finalize (in /usr/lib64/libc-2.17.so)
==14347==    by 0x5224862: ??? (in /example/path/liblib.so)
==14347==    by 0x400FFC9: _dl_fini (in /usr/lib64/ld-2.17.so)
==14347==    by 0x5C7EB68: __run_exit_handlers (in /usr/lib64/libc-2.17.so)
==14347==    by 0x5C7EBB6: exit (in /usr/lib64/libc-2.17.so)
==14347==    by 0x5C673DB: (below main) (in /usr/lib64/libc-2.17.so)
==14347==  Address 0x6012040 is 0 bytes inside a block of size 4 alloc'd
==14347==    at 0x402D57F: operator new[](unsigned long) (vg_replace_malloc.c:431)
==14347==    by 0x5224939: InvalidFree::InvalidFree() (lib.cpp:6)
==14347==    by 0x52248EB: __static_initialization_and_destruction_0(int, int) (lib.cpp:12)
==14347==    by 0x5224920: _GLOBAL__sub_I_lib.cpp (lib.cpp:17)
==14347==    by 0x400F902: _dl_init (in /usr/lib64/ld-2.17.so)
==14347==    by 0x4001159: ??? (in /usr/lib64/ld-2.17.so)

相关问题