gpt4 book ai didi

c - 使用 bfd 从跟踪中恢复位置信息

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

我正在使用 libld ( http://linux.die.net/man/3/dladdr ) 中的 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有被跟踪的函数名、所属的编译文件和一些地址(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"

最佳答案

我查找了更多示例,似乎我遗漏了两件事,打开后调用函数 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);
...

现在可以了。

关于c - 使用 bfd 从跟踪中恢复位置信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34054445/

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