gpt4 book ai didi

c - 尝试使用陷阱标志和陷阱信号处理程序单步执行程序,在 vsyscall 上崩溃

转载 作者:太空狗 更新时间:2023-10-29 11:18:52 28 4
gpt4 key购买 nike

我想创建程序执行的完整指令跟踪,以收集一些统计数据等。我首先尝试使用 linux 的 ptrace 功能逐步执行程序(使用教程 here)。这将创建两个进程,跟踪进程和调试器,它们通过信号进行通信。我每秒只能收到大约 16K 条指令(在 1.6GHz Atom 上),所以这对于任何重要的事情来说都太慢了。

我认为通过信号进行的进程间通信太慢,所以我尝试在与执行相同的进程中设置调试:设置陷阱标志,并创建信号处理程序。当使用软件中断进行系统调用时,应该保存陷阱标志,内核会使用它自己的标志——我是这么想的。但是我的程序不知何故被信号 SIGTRAP 杀死了。

这是我设置的:

#include <stdio.h>
#include <unistd.h>
#include <signal.h>

int cycle = 0;
void trapHandler(int signum) {
if (cycle % 262144 == 0) {
write(STDOUT_FILENO," trap\n",6);
}
cycle += 1;
}

void startTrace() {
// set up signal handler
signal(SIGTRAP, trapHandler);

// set trap flag
asm volatile("pushfl\n"
"orl $0x100, (%esp)\n"
"popfl\n"
);
}

void printRock() {
char* s = "Rock\n";
asm(
"movl $5, %%edx\n" // message length
"movl %0, %%ecx\n" // message to write
"movl $1, %%ebx\n" // file descriptor (stdout)
"movl $4, %%eax\n" // system call number (sys_write)
"int $0x80\n" // sycall
: // no output regs
: "r"(s) // input text
: "edx","ecx","ebx","eax"
);
}

int main() {
startTrace();

// some computation
int x = 0;
int i;
for (i = 0; i < 100000; i++) {
x += i*2;
}

printRock();
write(STDOUT_FILENO,"Paper\n",6);
write(STDOUT_FILENO,"Scissors\n",9);
}

运行时,这会给出:

 trap
trap
trap
Rock
Paper
trap
Trace/breakpoint trap (core dumped)

所以现在我们每秒获得大约 250K 条指令,速度仍然很慢,但可以执行非平凡的指令。但是在两次写入调用之间似乎发生了核心转储。在 GDB 中,我们看到它发生的位置:

Dump of assembler code for function __kernel_vsyscall:
0xb76f3414 <+0>: push %ecx
0xb76f3415 <+1>: push %edx
0xb76f3416 <+2>: push %ebp
0xb76f3417 <+3>: mov %esp,%ebp
0xb76f3419 <+5>: sysenter
0xb76f341b <+7>: nop
0xb76f341c <+8>: nop
0xb76f341d <+9>: nop
0xb76f341e <+10>: nop
0xb76f341f <+11>: nop
0xb76f3420 <+12>: nop
0xb76f3421 <+13>: nop
0xb76f3422 <+14>: int $0x80
=> 0xb76f3424 <+16>: pop %ebp
0xb76f3425 <+17>: pop %edx
0xb76f3426 <+18>: pop %ecx
0xb76f3427 <+19>: ret

回溯:

Program terminated with signal SIGTRAP, Trace/breakpoint trap.
#0 0xb77c5424 in __kernel_vsyscall ()
#1 0xb76d0553 in __write_nocancel () at ../sysdeps/unix/syscall-template.S:81
#2 0x0804847d in trapHandler (signum=5) at count.c:8
#3 <signal handler called>
#4 0xb77c5424 in __kernel_vsyscall ()
#5 0xb76d0553 in __write_nocancel () at ../sysdeps/unix/syscall-template.S:81
#6 0x08048537 in main () at count.c:49

似乎通过 int 80 发生的系统调用很好,但是写调用以某种方式使用内核的 VIDSO/vsyscall 中断(我不知道这个功能,更详细地描述了 here )。这可能与使用 sysenter 而不是 int 80 有关,也许陷阱标志在进入内核时仍然存在。我不太明白递归 __kernel_vsyscall 调用是怎么回事。我也不明白为什么 __kernel_vsyscall 函数中有一个 int 80 调用。

有没有人建议发生了什么事,以及如何解决这个问题?也许可以禁用 VDSO/vsyscall?或者是否可以使用 int 80 而不是 sysenter 覆盖 __kernel_vsyscall 函数?

最佳答案

回答自己的问题。我没有弄清楚发生了什么或详细解释它,但我找到了一个解决方法:禁用 VDSO。这可以通过

sudo sysctl vm.vdso_enabled=0

有了这个,整个程序的单步执行就可以工作了,包括跨系统调用。免责声明:如果事情变糟,请不要怪我。

编辑:更新我的 Linux(32 位 x86)之后,这个错误不再发生。也许这是一个已修复的错误。

关于c - 尝试使用陷阱标志和陷阱信号处理程序单步执行程序,在 vsyscall 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27080741/

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