gpt4 book ai didi

c - 使用 libunwind 实现异常

转载 作者:行者123 更新时间:2023-12-04 17:30:40 27 4
gpt4 key购买 nike

在编译器上工作,需要一些帮助来理解和使用 libunwind。这是我到目前为止所拥有的:

#define UNW_LOCAL_ONLY
#include <libunwind.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

typedef void *data_t;
typedef struct exception_stack_st *exception_stack_t;

struct exception_stack_st {
unw_cursor_t catch_block;
exception_stack_t prev;
};

/* PROTOTYPES */
void foo(void);
void bar(void);
void set_try(void);
void clear_try(void);
bool check_exception(void);
void throw_exception(data_t);
void print_backtrace();

/* GLOBALS */
exception_stack_t exception_stack = NULL;
data_t curr_exception = NULL;

int main(void) {
foo();
}

void foo(void) {
printf("In foo\n");
set_try();
if(check_exception()) {
printf("EXCEPTION: %s\n", (char*)curr_exception);
goto CATCH;
}
bar();
printf("This should never run\n");
CATCH:
clear_try();
printf("Leaving foo\n");
}

void bar(void) {
printf("In bar\n");
throw_exception("Throwing an exception in bar");
printf("Leaving bar\n");
}

void set_try(void) {
unw_cursor_t cursor;
unw_context_t context;
unw_word_t ip, offp;
char buf[1024];
unw_getcontext(&context);
unw_init_local(&cursor, &context);
unw_step(&cursor);
unw_get_reg(&cursor, UNW_REG_IP, &ip);
unw_get_proc_name(&cursor, buf, 1024, &offp);
printf("%s+0x%lx IP %lx\n", buf, offp, ip);

exception_stack_t cb = malloc(sizeof(struct exception_stack_st));
cb->catch_block = cursor;
cb->prev = exception_stack;
exception_stack = cb;
}

void clear_try(void) {
if (exception_stack != NULL)
exception_stack = exception_stack->prev;
curr_exception = NULL;
}

void throw_exception(data_t exception) {
unw_word_t ip, offp;
char buf[1024];
curr_exception = exception;
unw_get_reg(&(exception_stack->catch_block), UNW_REG_IP, &ip);
unw_get_proc_name(&(exception_stack->catch_block), buf, 1024, &offp);
printf("%s+0x%lx IP %lx\n", buf, offp, ip);
unw_resume(&(exception_stack->catch_block));
printf("PANIC: unw_resume returned.\n");
exit(1);
}

bool check_exception(void) {
return curr_exception != NULL;
}

void print_backtrace() {
unw_cursor_t cursor;
unw_context_t context;
char buf[1024];
unw_getcontext(&context);
unw_init_local(&cursor, &context);
printf("BACKTRACE:\n");
while(unw_step(&cursor) > 0) {
unw_get_proc_name(&cursor, buf, 1024, NULL);
printf("%s\n", buf);
}
}

好吧,这已经很困惑了,但是一些上下文可能有助于证明这些奇怪的选择是合理的。我想做的是在调用 foo 中的 set_try 之后在调用堆栈中的任何一点调用 throw_exception 以展开堆栈并恢复CPU 状态在调用 set_try 之后但在条件之前。虽然这目前只是一个小的 C 程序,但我打算在编译器中使用这些函数的一般结构,该编译器将生成必要的函数调用(类似于在 C++ 中使用 g++ 完成异常的方式),这就是为什么我有labels+goto 作为一种快速模拟我将生成的程序集的方法。我试过使用 libunwind 的 setjmp 实现,但它不太适合我的用例。

我遇到的问题与展开调用堆栈后 unw_resume 恢复的位置有关。 printf("This should never run\n") 似乎每次都运行,无论如何。我的理解是它应该将堆栈和 CPU 状态恢复到调用 unw_getcontext 时存储的任何状态,我认为存储的状态是正确的,因为 IP 寄存器的值(或PC 寄存器,因为这是 x86_64)在我调用 set_trythrow_exception 时在游标中完全相同。在调用 set_try 之后和条件之前,我什至跳入 gdb 多次查看 PC 寄存器,它每次都与打印输出相匹配。

我的问题是:

  • 我是不是误解了unw_resume
  • 我是否需要修改 PC (UNW_REG_IP) 寄存器?
  • 是否有其他地方(除了 nongnu.org 文档)我可以寻求有关 libunwind 的帮助?

提前致谢!

最佳答案

首先是个好消息:修复后,您的程序会产生(我假设预期的)输出:

./a.out
In foo
foo+0x31 IP 557615f273a1
In bar
foo+0x31 IP 557615f273a1
foo+0x31 IP 557615f273a1
EXCEPTION: Throwing an exception in bar
Leaving foo

现在是坏消息:您的原始程序存在一些单独的问题:

  1. 用于初始化游标的上下文(机器状态)必须在游标使用期间保持有效。这在 unw_init_local man page 中明确说明.

    违反此规则导致您的程序在 unw_resume 调用期间在我的机器上发送 SIGSEGV。

  2. unw_step 更新游标,但不更新上下文,后者实际用于恢复 unw_resume< 中的机器状态.

    这可以在 unw_resume documentation 中更清楚地说明.

要解决问题 1,只需将 unw_context_t 移动到 exception_stack_st 中,如下所示:

struct exception_stack_st {
unw_context_t context;
unw_cursor_t cursor;
exception_stack_t prev;
};

并将它们一起初始化:

  exception_stack_t cb = malloc(sizeof(*cb));
unw_getcontext(&cb->context);
unw_init_local(&cb->cursor, &cb->context);

要解决问题 2,您需要在您打算返回的帧中建立机器上下文

这需要将 set_try 变成一个宏,或者一个始终内联的函数,并去掉 unw_step

关于c - 使用 libunwind 实现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60014693/

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