这些预处理的内核功能如何起作用?
我正在尝试在Apple M1上执行一些内核功能,并遇到了 此似乎使用C型摩克群在Compile Time中定义一些内核的代码。这些是Apple的Kern/kpc
,但是该库的标头文件在任何地方都不包含,我相信API可能是私人的。
我想知道是否有人可以解释它是如何工作的,函数定义如何(即通常在功能体内的代码)向程序揭示的?我不熟悉## proc
符号。
#define KPERF_LIST \
/* ret, name, params */ \
F(int, kpc_get_counting, void) \
F(int, kpc_force_all_ctrs_set, int) \
F(int, kpc_set_counting, uint32_t) \
F(int, kpc_set_thread_counting, uint32_t) \
F(int, kpc_set_config, uint32_t, void *) \
F(int, kpc_get_config, uint32_t, void *) \
F(int, kpc_set_period, uint32_t, void *) \
F(int, kpc_get_period, uint32_t, void *) \
F(uint32_t, kpc_get_counter_count, uint32_t) \
F(uint32_t, kpc_get_config_count, uint32_t) \
F(int, kperf_sample_get, int *) \
F(int, kpc_get_thread_counters, int, unsigned int, void *)
#define F(ret, name, ...) \
typedef ret name##proc(__VA_ARGS__); \
static name##proc *name;
KPERF_LIST
#undef F
I am trying to execute some kernel functions on the Apple M1, and have come across
this code which appears to use C macros to define some kernel functions at compile time. These are kernel functions that exist in Apple's kern/kpc
library, but the header files for this library are not included anywhere, and I believe the API may be private.
I was wondering if anyone could explain how this works, how are the function definitions (ie, the code that would usually be in the function body) revealed to the program? I am unfamiliar with the ##proc
notation.
#define KPERF_LIST \
/* ret, name, params */ \
F(int, kpc_get_counting, void) \
F(int, kpc_force_all_ctrs_set, int) \
F(int, kpc_set_counting, uint32_t) \
F(int, kpc_set_thread_counting, uint32_t) \
F(int, kpc_set_config, uint32_t, void *) \
F(int, kpc_get_config, uint32_t, void *) \
F(int, kpc_set_period, uint32_t, void *) \
F(int, kpc_get_period, uint32_t, void *) \
F(uint32_t, kpc_get_counter_count, uint32_t) \
F(uint32_t, kpc_get_config_count, uint32_t) \
F(int, kperf_sample_get, int *) \
F(int, kpc_get_thread_counters, int, unsigned int, void *)
#define F(ret, name, ...) \
typedef ret name##proc(__VA_ARGS__); \
static name##proc *name;
KPERF_LIST
#undef F
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经联系了原始代码的作者来弄清楚这一点。
宏定义的函数将在代码中使用
dlsym()
链接到内核库函数。因此,在此代码中实际上并未定义函数。相反,声明相同/相似名称的函数,然后动态链接到内核库。I have contacted the author of the original code to figure this out.
The functions defined by the macro are linked to the kernel library functions later in the code using
dlsym()
. So, the functions are not actually defined in this code; rather, functions of the same/similar name are declared and then linked to the kernel library dynamically.如果有
f(int,kpc_get_counting,void)
第一个是返回值的类型。第二个是函数的名称,以下所有是参数。
If you have
F(int, kpc_get_counting, void)
The first is the type of the returned value. The second is the name of the function and all the following are the parameters.