Skip to contentSkip to conversation

scripts/gdb: Add $lx_per_cpu_ptr()

PATCH Applied Brendan Jackman 2 participants 2 messages Feb 20, 2025 – Feb 21, 2025 GDB kernel debugging helper scripts

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 scripts/gdb: Add $lx_per_cpu_ptr()

We currently have $lx_per_cpu() which works fine for stuff that kernel code would access via per_cpu(). But this doesn't work for stuff that kernel code accesses via per_cpu_ptr():

(gdb) p $lx_per_cpu(node_data[1].node_zones[2]->per_cpu_pageset) Cannot access memory at address 0xffff11105fbd6c28

This is because we take the address of the pointer and use that as the offset, instead of using the stored value.

Add a GDB version that mirrors the kernel API, which uses the pointer value.

To be consistent with per_cpu_ptr(), we need to return the pointer value instead of dereferencing it for the user. Therefore, move the existing dereference out of the per_cpu() Python helper and do that only in the $lx_per_cpu() implementation.

---
Since I think this is an uncontroversial patch I'm sending v2 immediately instead of waiting for more review comments. If that's annoying, let me know and I'll wait a few days next time :)

Changes in v2:
- Formatting fixup (was missing a blank line)
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 scripts/gdb/linux/cpus.py | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
scripts/gdb/linux/cpus.py +20 -2
index 13eb8b3901b8fc5e2500138c7d204ac5f7c9858c..1a50a4195def6a82f1391f5e162a5d063e794db8 100644
@@ -46,7 +46,7 @@ def per_cpu(var_ptr, cpu):
             # !CONFIG_SMP case
             offset = 0
     pointer = var_ptr.cast(utils.get_long_type()) + offset
-    return pointer.cast(var_ptr.type).dereference()
+    return pointer.cast(var_ptr.type)
 
 
 cpu_mask = {}
@@ -149,11 +149,29 @@ Note that VAR has to be quoted as string."""
         super(PerCpu, self).__init__("lx_per_cpu")
 
     def invoke(self, var, cpu=-1):
-        return per_cpu(var.address, cpu)
+        return per_cpu(var.address, cpu).dereference()
 
 
 PerCpu()
 
+
+class PerCpuPtr(gdb.Function):
+    """Return per-cpu pointer.
+
+$lx_per_cpu_ptr("VAR"[, CPU]): Return the per-cpu pointer called VAR for the
+given CPU number. If CPU is omitted, the CPU of the current context is used.
+Note that VAR has to be quoted as string."""
+
+    def __init__(self):
+        super(PerCpuPtr, self).__init__("lx_per_cpu_ptr")
+
+    def invoke(self, var, cpu=-1):
+        return per_cpu(var, cpu)
+
+
+PerCpuPtr()
+
+
 def get_current_task(cpu):
     task_ptr_type = task_type.get_type().pointer()
 

---
base-commit: f527c5c2abb60ddfd6b929d479b985071500419d
change-id: 20250219-lx-per-cpu-ptr-c9dfd3098721

Best regards,

Signature · 1 line
Brendan Jackman <[email protected]>
maintainer Reviewed-by on v2 1/1
#1 lore raw
Quoted 80 lines · Brendan Jackman

We currently have $lx_per_cpu() which works fine for stuff that kernel code would access via per_cpu(). But this doesn't work for stuff that kernel code accesses via per_cpu_ptr():

(gdb) p $lx_per_cpu(node_data[1].node_zones[2]->per_cpu_pageset) Cannot access memory at address 0xffff11105fbd6c28

This is because we take the address of the pointer and use that as the offset, instead of using the stored value.

Add a GDB version that mirrors the kernel API, which uses the pointer value.

To be consistent with per_cpu_ptr(), we need to return the pointer value instead of dereferencing it for the user. Therefore, move the existing dereference out of the per_cpu() Python helper and do that only in the $lx_per_cpu() implementation.

---
Since I think this is an uncontroversial patch I'm sending v2 immediately instead of waiting for more review comments. If that's annoying, let me know and I'll wait a few days next time :)

Changes in v2:
- Formatting fixup (was missing a blank line)
- Link to v1: https://lore.kernel.org/r/[email protected]

---
 scripts/gdb/linux/cpus.py | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
scripts/gdb/linux/cpus.py +20 -3
index 13eb8b3901b8fc5e2500138c7d204ac5f7c9858c..1a50a4195def6a82f1391f5e162a5d063e794db8 100644
@@ -46,7 +46,7 @@ def per_cpu(var_ptr, cpu):
             # !CONFIG_SMP case
             offset = 0
     pointer = var_ptr.cast(utils.get_long_type()) + offset
-    return pointer.cast(var_ptr.type).dereference()
+    return pointer.cast(var_ptr.type)
 
 
 cpu_mask = {}
@@ -149,11 +149,29 @@ Note that VAR has to be quoted as string."""
         super(PerCpu, self).__init__("lx_per_cpu")
 
     def invoke(self, var, cpu=-1):
-        return per_cpu(var.address, cpu)
+        return per_cpu(var.address, cpu).dereference()
 
 
 PerCpu()
 
+
+class PerCpuPtr(gdb.Function):
+    """Return per-cpu pointer.
+
+$lx_per_cpu_ptr("VAR"[, CPU]): Return the per-cpu pointer called VAR for the
+given CPU number. If CPU is omitted, the CPU of the current context is used.
+Note that VAR has to be quoted as string."""
+
+    def __init__(self):
+        super(PerCpuPtr, self).__init__("lx_per_cpu_ptr")
+
+    def invoke(self, var, cpu=-1):
+        return per_cpu(var, cpu)
+
+
+PerCpuPtr()
+
+
 def get_current_task(cpu):
     task_ptr_type = task_type.get_type().pointer()
 
 
---

base-commit: f527c5c2abb60ddfd6b929d479b985071500419d
change-id: 20250219-lx-per-cpu-ptr-c9dfd3098721

Quoted 1 line

Best regards,

Jan

Signature · 2 lines
Siemens AG, Foundational Technologies
Linux Expert Center