tools/gdb: Allow utils.container_of with str input

After we introduced NxDQueue and NxSQueue, we're using them like `NxDQueue(g_active_connections, "struct socket_conn_s", "node")` and leads to `utils.container_of(ptr, str, str)`, so maybe we need to allow str input for `utils.container_of`

Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
This commit is contained in:
Zhe Weng 2024-09-25 17:46:28 +08:00 committed by Xiang Xiao
parent bb461b532b
commit a4e5689c7f

View file

@ -94,8 +94,12 @@ def offset_of(typeobj: gdb.Type, field: str) -> Union[int, None]:
return None
def container_of(ptr: gdb.Value, typeobj: gdb.Type, member: str) -> gdb.Value:
def container_of(
ptr: gdb.Value, typeobj: Union[gdb.Type, str], member: str
) -> gdb.Value:
"""Return pointer to containing data structure"""
if isinstance(typeobj, str):
typeobj = lookup_type(typeobj)
return gdb.Value(int(ptr.address) - offset_of(typeobj, member)).cast(
typeobj.pointer()
)
@ -112,9 +116,7 @@ class ContainerOf(gdb.Function):
super().__init__("container_of")
def invoke(self, ptr, typename, elementname):
return container_of(
ptr, gdb.lookup_type(typename.string()).pointer(), elementname.string()
)
return container_of(ptr, typename.string(), elementname.string())
ContainerOf()