c - 仅使用程序计数器和可执行文件调查未对齐的用户空间访问

标签 c linux memory gdb kernel

所以我有这个可执行文件,使用 -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/

相关文章:

c - 修改作为参数传递的* int []

linux - 在不丢失其他行的情况下编辑文本中的部分行

c - 如何判断线程是否响应唤醒调用?

c++ - vector 数组是完全连续的内存吗?

go - 如何改进实现的文件下载器

使用 as88 汇编程序从汇编代码调用 C 函数

c - 如何过滤掉大于 100 的值,以便不将其放入总和中

c - C 中的指针和变量

linux - 来自 KornShell 脚本的回显未在终端中显示

c# - 如何编辑窗口中的单个像素?