我使用dladdr from libld (http://linux.die.net/man/3/dladdr)获取函数调用的跟踪。这里是使用一个跟踪元素的最小示例:
#include<iostream>
#include <dlfcn.h> // link with -ldl -rdynamic a
void f(){
void **frame = static_cast<void **>(__builtin_frame_address(0));
void **bp = static_cast<void **>(*frame);
void *ip = frame[1];
Dl_info info;
dladdr(ip, &info);
std::cout << info.dli_sname << " " << info.dli_fname << " " << info.dli_saddr << std::endl;
ip = bp[1];
bp = static_cast<void**>(bp[0]);
dladdr(ip, &info);
std::cout << info.dli_sname << " " << info.dli_fname << " " << info.dli_saddr << std::endl;
}
int main(){
f();
}其中产出:
main ./a.out 0x402800
__libc_start_main /lib64/libc.so.6 0x7febf6bf2610也就是说,Dl_info具有跟踪的函数名、它所属的编译文件以及手册页中描述为“指定符号的确切地址”的 address (0x7f...)。
这个地址包含源文件位置的信息(从哪里调用函数)。事实上,在一些实用程序的帮助下,我可以获得这些信息:
$ addr2line -e a.out
/home/user/test.cpp:34(给出在源文件中定义main的确切行)。只要程序是用-g选项编译的,这就可以工作了。
现在我想要的是从程序中提取这些信息。据推测,这在BFD库中是可能的.。
这是我的尝试,基于BFD示例,例如:http://opensource.apple.com/source/X11libs/X11libs-40.2/cairo/cairo-1.8.6/util/backtrace-symbols.c
1)首先,我必须定义一个函数find_addr_sect,稍后由bfd_map_over_sections (通过指针)调用该函数。
static void find_addr_sect(bfd *abfd, asection *section, void *obj){
bfd_data *data = (bfd_data *)obj;
bfd_vma vma;
bfd_size_type size;
if (data->found)
return;
if (!(bfd_get_section_vma(abfd, section)))
return;
vma = bfd_get_section_vma(abfd, section);
if (data->pc < vma)
return;
size = bfd_get_section_size(section);
if (data->pc >= vma + size)
return;
data->found = bfd_find_nearest_line(abfd, section, syms,
data->pc - vma,
&data->filename,
&data->function,
&data->line);
}2)我将指向函数的代码放在函数中(这取代了上面的函数void f()。
void f(){
void **frame = static_cast<void **>(__builtin_frame_address(0));
void **bp = static_cast<void **>(*frame);
void *ip = frame[1];
Dl_info info;
dladdr(ip, &info);
std::cout << info.dli_sname << " " << info.dli_fname << " " << info.dli_saddr << std::endl;
////////////////////
// this will try to find the location of main (first in the stack)
bfd *abfd = bfd_openr(info.dli_fname, NULL); assert(abfd); // the executable file is opened successfully
// bfd_data data;
bfd_map_over_sections(abfd, find_addr_sect, nullptr); // !! doesn't call `find_addr_sect` at all.
///////////////////
ip = bp[1];
bp = static_cast<void**>(bp[0]);
dladdr(ip, &info);
std::cout << info.dli_sname << " " << info.dli_fname << " " << info.dli_saddr << std::endl;
}遗憾的是,我被困在这里是因为bfd_map_over_sections电话什么都不做。我用错了bfd_map_over_sections,为什么?
很抱歉使用了C++,在这是一个C问题。它缩短了我的大部分代码,我更习惯了。
编辑:,我添加了这一行,我可以确认问题的一个线索是节数为零。
unsigned int numSections = -1;
numSections = bfd_count_sections(abfd);
std::cout << "num sections " << numSections << std::endl; // gives "0"发布于 2015-12-03 02:41:55
我寻找了更多的示例,似乎遗漏了两件事:打开后调用函数bfd_check_format,以及在bfd_data结构中填充和传递地址信息。
...
bfd *abfd = bfd_openr(info.dli_fname, NULL); assert(abfd);
// char **matching;
// bfd_data data;// = (bfd_data *)obj;
if (!bfd_check_format (abfd, bfd_object)){
bfd_close (abfd); assert(0);
}
...后来,bfd_data变量被用作find_addr_sect的输入和输出。因此
...
bfd_data data;// = (bfd_data *)obj;
data.pc = (bfd_hostptr_t)info.dli_saddr;
data.found = FALSE;
bfd_map_over_sections(abfd, find_addr_sect, &data);
...现在起作用了。
https://stackoverflow.com/questions/34054445
复制相似问题