这些预处理的内核功能如何起作用?

发布于 2025-02-06 12:27:01 字数 1382 浏览 2 评论 0原文

我正在尝试在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

离线来电— 2025-02-13 12:27:01

我已经联系了原始代码的作者来弄清楚这一点。

宏定义的函数将在代码中使用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.

不顾 2025-02-13 12:27:01

如果有
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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文