我需要能够在程序终止后将指令指针(从回溯)转换为文件和行号。在执行期间,我可以存储我稍后需要用于此离线分析的任何其他信息,但分析必须在离线状态下进行。
现在,我发出指令指针,我发出一组结构,其中包含dl_iterate_phdr
的一些信息。我包括模块名称,基地址的dlpi_addr
字段以及标记为可执行文件的第一个p_memsz
字段phdr
字段。
然后,在我的离线工具中,我使用我保存的大小和地址表识别给定地址的模块。我从指令地址中减去dlpi_addr
并将其传递给addr2line
。
这似乎在大部分时间都有效,但有时我认为它稍微偏离(即,它发现了一个相邻的符号 - 我知道行号信息并不总是正确的)。它也完全失败了主要的可执行文件。我很确定我没有正确处理所有的偏移。
一般来说,我需要做些什么?我知道有几个库可以执行此操作(lldb
,libdwarf
),但它们没有详细记录。我也试图在主程序和离线工具之间传递尽可能少的信息,所以我不想将内存映射作为字符串转储,例如。
以下是一些示例数据:
dl_iterate_phdr回调代码:
for ( ElfW(Half) i = 0; i < info->dlpi_phnum; i++ )
{
const ElfW(Phdr)& header = info->dlpi_phdr[i];
// Find the executable segment
if ( (header.p_flags & PF_X) && (header.p_type == PT_LOAD) )
{
if ( info->dlpi_addr == getauxval(AT_SYSINFO_EHDR) )
{
// vdso section.
continue;
}
printf("Module %d\n", modules->m_Count);
printf("Module name: %s\n", info->dlpi_name);
printf("Start address: %p\n", info->dlpi_addr);
printf("Length: %jx\n", (uintmax_t)header.p_memsz);
...
}
}
输出
Module 0
Module name:
Start address: (nil)
Length: 7276
Module 1
Module name: /lib/x86_64-linux-gnu/libdl.so.2
Start address: 0x7ff933d63000
Length: 2060
Module 2
Module name: /lib/x86_64-linux-gnu/libpthread.so.0
Start address: 0x7ff933b45000
Length: 188c4
Module 3
Module name: /usr/lib/x86_64-linux-gnu/libstdc++.so.6
Start address: 0x7ff933841000
Length: e549d
Module 4
Module name: /lib/x86_64-linux-gnu/libm.so.6
Start address: 0x7ff93353b000
Length: 1042cc
Module 5
Module name: /lib/x86_64-linux-gnu/libgcc_s.so.1
Start address: 0x7ff933325000
Length: 1548c
Module 6
Module name: /lib/x86_64-linux-gnu/libc.so.6
Start address: 0x7ff932f60000
Length: 1b9cc0
Module 7
Module name: /lib64/ld-linux-x86-64.so.2
Start address: 0x7ff933f67000
Length: 22118
Module 8
Module name: /path/to/another.so
Start address: 0x7ff932cc3000
Length: b682
Module 9
Module name: /path/to/yet/another.so
Start address: 0x7ff932a1c000
Length: 164fc
cat / proc // maps
00400000-00408000 r-xp 00000000 fc:01 56098817 /path/to/main/executable
00607000-00608000 r--p 00007000 fc:01 56098817 /path/to/main/executable
00608000-00609000 rw-p 00008000 fc:01 56098817 /path/to/main/executable
010cc000-010ed000 rw-p 00000000 00:00 0 [heap]
7ff924000000-7ff924021000 rw-p 00000000 00:00 0
7ff924021000-7ff928000000 ---p 00000000 00:00 0
7ff92c000000-7ff92c021000 rw-p 00000000 00:00 0
7ff92c021000-7ff930000000 ---p 00000000 00:00 0
7ff931a1a000-7ff931a1b000 ---p 00000000 00:00 0
7ff931a1b000-7ff93221b000 rw-p 00000000 00:00 0
7ff93221b000-7ff93221c000 ---p 00000000 00:00 0
7ff93221c000-7ff932a1c000 rw-p 00000000 00:00 0
7ff932a1c000-7ff932a33000 r-xp 00000000 fc:01 56360961 /path/to/yet/another.so
7ff932a33000-7ff932c32000 ---p 00017000 fc:01 56360961 /path/to/yet/another.so
7ff932c32000-7ff932c33000 r--p 00016000 fc:01 56360961 /path/to/yet/another.so
7ff932c33000-7ff932cc3000 rw-p 00017000 fc:01 56360961 /path/to/yet/another.so
7ff932cc3000-7ff932ccf000 r-xp 00000000 fc:01 56229891 /path/to/another.so
7ff932ccf000-7ff932ece000 ---p 0000c000 fc:01 56229891 /path/to/another.so
7ff932ece000-7ff932ecf000 r--p 0000b000 fc:01 56229891 /path/to/another.so
7ff932ecf000-7ff932ed0000 rw-p 0000c000 fc:01 56229891 /path/to/another.so
7ff932ed0000-7ff932f60000 rw-p 00000000 00:00 0
7ff932f60000-7ff93311a000 r-xp 00000000 fc:01 17039726 /lib/x86_64-linux-gnu/libc-2.19.so
7ff93311a000-7ff93331a000 ---p 001ba000 fc:01 17039726 /lib/x86_64-linux-gnu/libc-2.19.so
7ff93331a000-7ff93331e000 r--p 001ba000 fc:01 17039726 /lib/x86_64-linux-gnu/libc-2.19.so
7ff93331e000-7ff933320000 rw-p 001be000 fc:01 17039726 /lib/x86_64-linux-gnu/libc-2.19.so
7ff933320000-7ff933325000 rw-p 00000000 00:00 0
7ff933325000-7ff93333b000 r-xp 00000000 fc:01 17039378 /lib/x86_64-linux-gnu/libgcc_s.so.1
7ff93333b000-7ff93353a000 ---p 00016000 fc:01 17039378 /lib/x86_64-linux-gnu/libgcc_s.so.1
7ff93353a000-7ff93353b000 rw-p 00015000 fc:01 17039378 /lib/x86_64-linux-gnu/libgcc_s.so.1
7ff93353b000-7ff933640000 r-xp 00000000 fc:01 17039591 /lib/x86_64-linux-gnu/libm-2.19.so
7ff933640000-7ff93383f000 ---p 00105000 fc:01 17039591 /lib/x86_64-linux-gnu/libm-2.19.so
7ff93383f000-7ff933840000 r--p 00104000 fc:01 17039591 /lib/x86_64-linux-gnu/libm-2.19.so
7ff933840000-7ff933841000 rw-p 00105000 fc:01 17039591 /lib/x86_64-linux-gnu/libm-2.19.so
7ff933841000-7ff933927000 r-xp 00000000 fc:01 8390197 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19
7ff933927000-7ff933b26000 ---p 000e6000 fc:01 8390197 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19
7ff933b26000-7ff933b2e000 r--p 000e5000 fc:01 8390197 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19
7ff933b2e000-7ff933b30000 rw-p 000ed000 fc:01 8390197 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19
7ff933b30000-7ff933b45000 rw-p 00000000 00:00 0
7ff933b45000-7ff933b5e000 r-xp 00000000 fc:01 17039713 /lib/x86_64-linux-gnu/libpthread-2.19.so
7ff933b5e000-7ff933d5d000 ---p 00019000 fc:01 17039713 /lib/x86_64-linux-gnu/libpthread-2.19.so
7ff933d5d000-7ff933d5e000 r--p 00018000 fc:01 17039713 /lib/x86_64-linux-gnu/libpthread-2.19.so
7ff933d5e000-7ff933d5f000 rw-p 00019000 fc:01 17039713 /lib/x86_64-linux-gnu/libpthread-2.19.so
7ff933d5f000-7ff933d63000 rw-p 00000000 00:00 0
7ff933d63000-7ff933d66000 r-xp 00000000 fc:01 17039596 /lib/x86_64-linux-gnu/libdl-2.19.so
7ff933d66000-7ff933f65000 ---p 00003000 fc:01 17039596 /lib/x86_64-linux-gnu/libdl-2.19.so
7ff933f65000-7ff933f66000 r--p 00002000 fc:01 17039596 /lib/x86_64-linux-gnu/libdl-2.19.so
7ff933f66000-7ff933f67000 rw-p 00003000 fc:01 17039596 /lib/x86_64-linux-gnu/libdl-2.19.so
7ff933f67000-7ff933f8a000 r-xp 00000000 fc:01 17039716 /lib/x86_64-linux-gnu/ld-2.19.so
7ff934047000-7ff934049000 rw-p 00000000 00:00 0
7ff93405e000-7ff93407e000 rw-p 00000000 00:00 0
7ff9340b2000-7ff9340c0000 rw-p 00000000 00:00 0
7ff9340dc000-7ff934152000 rw-p 00000000 00:00 0
7ff93415f000-7ff934166000 rw-p 00000000 00:00 0
7ff934172000-7ff934175000 rw-p 00000000 00:00 0
7ff93417b000-7ff93417c000 rw-p 00000000 00:00 0
7ff934185000-7ff934189000 rw-p 00000000 00:00 0
7ff934189000-7ff93418a000 r--p 00022000 fc:01 17039716 /lib/x86_64-linux-gnu/ld-2.19.so
7ff93418a000-7ff93418b000 rw-p 00023000 fc:01 17039716 /lib/x86_64-linux-gnu/ld-2.19.so
7ff93418b000-7ff93418c000 rw-p 00000000 00:00 0
7ffd31892000-7ffd318b6000 rw-p 00000000 00:00 0 [stack]
7ffd31913000-7ffd31915000 r--p 00000000 00:00 0 [vvar]
7ffd31915000-7ffd31917000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
readelf -l executable
Program Headers:
Type Offset VirtAddr PhysAddr
FileSiz MemSiz Flags Align
PHDR 0x0000000000000040 0x0000000000400040 0x0000000000400040
0x00000000000001f8 0x00000000000001f8 R E 8
INTERP 0x0000000000000238 0x0000000000400238 0x0000000000400238
0x000000000000001c 0x000000000000001c R 1
[Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
LOAD 0x0000000000000000 0x0000000000400000 0x0000000000400000
0x0000000000007276 0x0000000000007276 R E 200000
LOAD 0x0000000000007db8 0x0000000000607db8 0x0000000000607db8
0x0000000000000389 0x00000000000004c0 RW 200000
DYNAMIC 0x0000000000007dd8 0x0000000000607dd8 0x0000000000607dd8
0x0000000000000220 0x0000000000000220 RW 8
NOTE 0x0000000000000254 0x0000000000400254 0x0000000000400254
0x0000000000000044 0x0000000000000044 R 4
GNU_EH_FRAME 0x0000000000004a40 0x0000000000404a40 0x0000000000404a40
0x000000000000078c 0x000000000000078c R 4
GNU_STACK 0x0000000000000000 0x0000000000000000 0x0000000000000000
0x0000000000000000 0x0000000000000000 RW 10
GNU_RELRO 0x0000000000007db8 0x0000000000607db8 0x0000000000607db8
0x0000000000000248 0x0000000000000248 R 1
示例数学:
谢谢!
答案 0 :(得分:1)
一般来说,我需要做些什么?
您所描述的内容是正确的, 应该。
很遗憾,您还没有提供任何详细信息,以便我们猜出是什么原因造成这种情况对您无效。
主要的可执行文件也完全失败了。
它不应该(对于非PIE
可执行文件,dlpi_addr
应始终为0)。
你应该尝试创建一个minimal example,这样我们就可以告诉你你做错了什么。从一个简单的主二进制文件开始,显示您获得的地址,nm
的输出,dlpi_addr
等。
答案 1 :(得分:0)
dlpi_addr
不是映射的起始地址,而是相对于映射它们的p_vaddr
程序头中的PT_LOAD
地址的偏移量。将这些值一起添加,您将拥有绝对的虚拟地址范围。