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

标签 c++ c debugging linux-kernel printk

我想在 c/c++ 中找到一个宏,它获取结构 typedef 和一个指向结构的指针作为输入并打印所有它的内容是这样的:

假设我有一个名为 struct account 的结构。

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

我这样使用它:

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

我想要在 linux 内核/c 语言中有一个看起来像这样的宏:

PRINT_STRUCT_CONTENT(struct_type, pointer)

并打印以下内容:

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

想法是在内核中有一种 dir python 函数


Write a Macro to stringify the contents of a struct

Your most general C macro for printing variable values in different types

Is there a C preprocessor macro to print out a struct?

Writing a while loop in the C preprocessor

最佳答案

您可以使用 print_hex_dumpprint_hex_dump_bytes
示例:

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

关于c++ - 如何打印以筛选结构及其所有内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14572015/

相关文章:

c++ - 将指针插入原始内存

.net - 无法找到入口点 (cpp)

c - 检查泛型数组是否为 MaxHeap 的算法

c - 具有结构指针的共享内存互斥体

windows - 如何在 64 位操作系统上同时安装适用于 32 位和 64 位的 Windows 调试工具?

python - 我可以从 pdb 中引发异常吗? (用于调试)

debugging - HTTP 调试记录器和自动回复器

c++ - c++ std 的 inf 是否表现得与常识性无穷大一样

c++ - 如何将比较器与 is_transparent 类型一起使用?

c - 从服务器获取数据时知道要接收多少