简体   繁体   English

如何打印以筛选结构及其所有内容?

[英]How to print to screen a struct and all its content?

I would like to find a macro in c / c++ which gets struct typedef and a pointer to a struct as inputs and prints all its content like that: 我想在c / c++找到一个宏,它获取struct typedef和一个指向struct的指针作为输入,并打印所有内容,如下所示:

let say I have a struct named struct account . 假设我有一个名为struct account

struct account {
   int account_number;
   char *first_name;
   char *last_name;
   float balance;
};

and I use it this way: 我这样使用它:

struct account my_account= {    
    .account_number = 321321,
    .first_name = 0x12345678,
    .last_name = 0,
    .balance = 222 };

I want a macro in linux kernel/ c lanaguge that look like that: 我想要一个linux kernel / c lanaguge中的宏看起来像这样:

PRINT_STRUCT_CONTENT(struct_type, pointer)

and printk the following: 并打印以下内容:

my_account= {
account_number = 321321,
first_name = 0x12345678,
last_name = 0,
balance = 222
}

the idea is to have kind of dir python function in the kernel 我的想法是在内核中使用一种dir python函数


Write a Macro to stringify the contents of a struct 编写宏来字符串化结构的内容

Your most general C macro for printing variable values in different types 您最常用的C宏,用于打印不同类型的变量值

Is there a C preprocessor macro to print out a struct? 是否有一个C预处理器宏来打印出一个结构?

Writing a while loop in the C preprocessor 在C预处理器中编写while循环

You can use print_hex_dump or print_hex_dump_bytes 您可以使用print_hex_dumpprint_hex_dump_bytes
Example: 例:

struct mystruct *p;
print_hex_dump_bytes("mystruct: ", DUMP_PREFIX_ADDRESS, p, sizeof(*p));

You might want to take a look at the kernel trace event API. 您可能想要查看内核跟踪事件 API。 It's slightly more complex than a simple printk , but it's dynamically switchable and does some prett(-ier) printing. 它比简单的printk稍微复杂一点,但它可以动态切换并进行一些prett(-ier)打印。 And still very low-overhead. 并且开销仍然非常低。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM