我尝试在 Trace32 中以用户友好的方式打印 Linux 链表。
1. 是否有任何已知的方法可用?
如果没有,那么让我展示一个带有模块列表的示例。
我有全局变量
static struct list_head modules;
在哪里
struct list_head {
struct list_head *next, *prev;
};
所以,在 T32 中,我在做时只看到 next 和 prev 指针列表,v.v modules
实际上没有有用的信息。但是,模块列表的每个节点都是容器类型的一部分。在这种情况下,结构模块
struct module {
...
struct list_head list;
...
}
通常,提取容器指针 Linux 使用container_of宏。
/**
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*
*/
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
在我们的示例中,我们知道指向struct list_head
哪个list
成员的指针,struct module
然后我们应该调用container_of(modules->next, struct module, list)
以获取指向容器的指针。
为了能够在 T32 中存档,我需要计算list
容器类型中成员的偏移量。
任何人都知道如何实现这一目标?