在用 C++ 编写一些代码来打印回溯时,我遇到了 this answer ,其中包括复制类型的定义:
/* This structure mirrors the one found in /usr/include/asm/ucontext.h */
typedef struct _sig_ucontext {
unsigned long uc_flags;
struct ucontext *uc_link;
stack_t uc_stack;
struct sigcontext uc_mcontext;
sigset_t uc_sigmask;
} sig_ucontext_t;
我不能只包括 <asm/ucontext.h>
因为它被简单地定义为 ucontext
它与来自 <sys/ucontext.h>
的类似命名类型发生冲突(包含在必要的 <signal.h>
中):
/* Userlevel context. */
typedef struct ucontext
{
unsigned long int uc_flags;
struct ucontext *uc_link;
stack_t uc_stack;
mcontext_t uc_mcontext;
__sigset_t uc_sigmask;
struct _libc_fpstate __fpregs_mem;
} ucontext_t;
我在这里只需要 struct sigcontext uc_mcontext
来自 asm
的成员版本。有没有比复制这个结构更好的方法来检索那个值?这对我来说似乎非常骇人听闻而且容易出错。
最佳答案
一个潜在的解决方法是使 asm/ucontext.h
成为源文件中的第一个包含项,并滥用预处理器重命名冲突的定义:
#define ucontext asm_ucontext
#include <asm/ucontext.h>
#undef asm_ucontext
然后您可以使用 asm_ucontext
来引用 ucontext
的替代定义。
一种不太老套的方法是使用 sed 自动从系统文件中提取必要的定义:
$ echo '#include <asm/ucontext.h>' | gcc -x c - -E -o- | sed -n '/^struct ucontext/,/^};/{ s/ucontext/asm_ucontext/; p}' | tee asm_ucontext.h
struct asm_ucontext {
unsigned long uc_flags;
struct asm_ucontext *uc_link;
stack_t uc_stack;
struct sigcontext uc_mcontext;
sigset_t uc_sigmask;
};
关于c++ - 在回溯期间检索 sigcontext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29390618/