所以我有这个可执行文件,使用 -g
选项编译,它会触发加载未对齐的用户空间访问警告。
Unaligned userspace access in "softtest" pid=1407 pc=0x0041515c ins=0x011e
Unaligned userspace access in "softtest" pid=1406 pc=0x0041515c ins=0x011e
Unaligned userspace access in "softtest" pid=1406 pc=0x004148c2 ins=0x012e
Unaligned userspace access in "softtest" pid=1407 pc=0x0041515c ins=0x011e
Unaligned userspace access in "softtest" pid=1406 pc=0x0041515c ins=0x011e
Unaligned userspace access in "softtest" pid=1407 pc=0x0041515c ins=0x011e
Unaligned userspace access in "softtest" pid=1406 pc=0x004148c2 ins=0x012e
Unaligned userspace access in "softtest" pid=1407 pc=0x0041515c ins=0x011e
Unaligned userspace access in "softtest" pid=1406 pc=0x0041515c ins=0x011e
Unaligned userspace access in "softtest" pid=1406 pc=0x004148c2 ins=0x012e
Unaligned userspace access in "softtest" pid=1407 pc=0x0041515c ins=0x011e
Unaligned userspace access in "softtest" pid=1406 pc=0x0041515c ins=0x011e
Unaligned userspace access in "softtest" pid=1407 pc=0x0041515c ins=0x011e
Unaligned userspace access in "softtest" pid=1406 pc=0x004148c2 ins=0x012e
错误消息提供了一些信息:程序计数器和指令,但我不知道如何将 PC 转换为我的代码中的文件和行。
我可以嗅出这是在执行一些内存复制或其他操作的循环中,因为地址通常是相同的。
所以问题是:我如何使用 Linux 工具找出导致这种未对齐访问的文件和代码行?
有什么意见吗?
最佳答案
查看addr2line
实用程序
DESCRIPTION
addr2line translates addresses into file names and line numbers.
Given an address in an executable or an offset in a section of a
relocatable object, it uses the debugging information
to figure out which file name and line number are associated with it.
一个简单的 c 示例:
1 #include <stdio.h>
2
3 int main() {
4 int* a = 0;
5
6 printf("%d", *a);
7 return 0;
8 }
使用编译
gcc -Wall -ggdb3 g.c
gdb 给出了这个输出:
$ gdb -q a.out
Reading symbols from /tmp/tmp.M0766CSHGm/a.out...done.
(gdb) r
Starting program: /tmp/tmp.M0766CSHGm/a.out
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400538 in main () at g.c:6
6 printf("%d", *a);
将该地址与 addr2line 一起使用:
$ addr2line 0x0000000000400538
/tmp/tmp.M0766CSHGm/g.c:6
关于c - 仅使用程序计数器和可执行文件调查未对齐的用户空间访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6263663/