arch_prtcl C++ 原型

arch_prtcl C++ prototype

对于 arch_prctl 男人 page 说:

As of version 2.7, glibc provides no prototype for arch_prctl(). You have to declare it yourself for now. This may be fixed in future glibc versions.

用 clang 编译 C 程序运行良好但会抛出警告

warning: implicit declaration of function 'arch_prctl' is invalid in C99

但是尝试使用 clang++ 编译 C++ 失败

error: use of undeclared identifier 'arch_prctl'

那么,我该如何自己声明需要的原型呢?

来自同一手册页:

The arch_prctl() function sets architecture-specific process or thread state. code selects a subfunction and passes argument addr to it; addr is interpreted as either an unsigned long for the "set" operations, or as an unsigned long *, for the "get" operations.

因此,如果您正在执行集合操作,您可能应该这样声明:

int arch_prctl(int code, unsigned long addr);

对于获取:

int arch_prctl(int code, unsigned long *addr);

然而,Linux 内核中的系统调用被定义为 this:

/* X86_64 only */
/* kernel/process_64.c */
asmlinkage long sys_arch_prctl(int, unsigned long);

后来 casts 它指向一个指针:

long sys_arch_prctl(int code, unsigned long addr)
{
    return arch_prctl(current, code, (unsigned long __user *) addr);
}

因此,只要您知道这两种不同的语义并正确使用它们,编写任何一个声明都应该可以正常工作。

我花了一些时间才意识到,glibc 可能根本不支持 arch_prctl。所以必须自己做相应的系统调用:

#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>

int arch_prctl(int code, unsigned long addr)
{    
    return syscall(SYS_arch_prctl, code, addr);
}