Skip to contentSkip to conversation

scripts/gdb: Fixes for $lx_current and $lx_per_cpu

PATCH Applied Florian Rommel 1 participant 5 messages Apr 25, 2024 GDB kernel debugging helper scripts

lore
Share & tools
lore
#0 lore raw

cover scripts/gdb: Fixes for $lx_current and $lx_per_cpu

Hi all,

This series fixes several bugs in the GDB scripts related to the $lx_current and $lx_per_cpu functions. The changes were tested with GDB 10, 11, 12, 13, and 14.

Patch 1 fixes false-negative results when probing for KGDB

Patch 2 fixes the $lx_per_cpu function, which is currently non-functional in QEMU-GDB and KGDB.

Patch 3 fixes an additional bug in $lx_per_cpu that occurs with KGDB.

Patch 4 fixes the incorrect detection of the current CPU number in KGDB, which silently breaks $lx_per_cpu and $lx_current.

Regards,
Flo

Florian Rommel (4):
scripts/gdb: Fix failing KGDB detection during probe scripts/gdb: Fix parameter handling in $lx_per_cpu scripts/gdb: Make get_thread_info accept pointers scripts/gdb: Fix detection of current CPU in KGDB

 scripts/gdb/linux/cpus.py  | 11 +++--------
 scripts/gdb/linux/tasks.py |  2 +-
 scripts/gdb/linux/utils.py |  2 +-
 3 files changed, 5 insertions(+), 10 deletions(-)
Signature · 1 line
2.44.0
#1 lore raw

PATCH 1/4 scripts/gdb: Fix failing KGDB detection during probe

The KGDB probe function sometimes failed to detect KGDB for SMP machines as it assumed that task 2 (kthreadd) is running on CPU 0, which is not necessarily the case. Now, the detection is agnostic to kthreadd's CPU.

---
 scripts/gdb/linux/utils.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/gdb/linux/utils.py +1 -1
index 7d5278d815fa..245ab297ea84 100644
@@ -196,7 +196,7 @@ def get_gdbserver_type():
     def probe_kgdb():
         try:
             thread_info = gdb.execute("info thread 2", to_string=True)
-            return "shadowCPU0" in thread_info
+            return "shadowCPU" in thread_info
         except gdb.error:
             return False
 
Signature · 1 line
2.44.0
#2 lore raw

PATCH 2/4 scripts/gdb: Fix parameter handling in $lx_per_cpu

In reply to Florian Rommel

Before, the script tried to get the address by constructing a pointer to the parameter (by name). However, since GDB now passes the parameter as a GdbValue, we cannot get its name. Instead, we retrieve the address through GdbValue's address attribute.

Before:

Quoted 1 line
Quoted 1 line
Quoted 1 line

p $lx_per_cpu(cpu_info)

Traceback (most recent call last):
File "./scripts/gdb/linux/cpus.py", line 152, in invoke

    var_ptr = gdb.parse_and_eval("&" + var_name.string())
                                       ^^^^^^^^^^^^^^^^^

gdb.error: Trying to read string with inappropriate type `struct cpuinfo_x86'.

---
 scripts/gdb/linux/cpus.py | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
scripts/gdb/linux/cpus.py +2 -3
index cba589e5b57d..2b51a3abd363 100644
@@ -152,9 +152,8 @@ Note that VAR has to be quoted as string."""
     def __init__(self):
         super(PerCpu, self).__init__("lx_per_cpu")
 
-    def invoke(self, var_name, cpu=-1):
-        var_ptr = gdb.parse_and_eval("&" + var_name.string())
-        return per_cpu(var_ptr, cpu)
+    def invoke(self, var, cpu=-1):
+        return per_cpu(var.address, cpu)
 
 
 PerCpu()
Signature · 1 line
2.44.0
#3 lore raw

PATCH 3/4 scripts/gdb: Make get_thread_info accept pointers

In reply to Florian Rommel

get_thread_info ($lx_thread_info) only accepted a dereferenced task parameter. Passing a pointer to a task_struct (like $lx_per_cpu does with KGDB) threw an exception.

With this patch, both (dereferenced values and pointers) are accepted.

Before (on x86, KGDB):

Quoted 1 line
Quoted 1 line
Quoted 1 line

p $lx_per_cpu(cpu_info)

Traceback (most recent call last):
File "./scripts/gdb/linux/cpus.py", line 158, in invoke
return per_cpu(var_ptr, cpu)
^^^^^^^^^^^^^^^^^^^^^
File "./scripts/gdb/linux/cpus.py", line 42, in per_cpu
cpu = get_current_cpu()
^^^^^^^^^^^^^^^^^
File "./scripts/gdb/linux/cpus.py", line 33, in get_current_cpu

    return tasks.get_thread_info(tasks.get_task_by_pid(tid))['cpu']
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "./scripts/gdb/linux/tasks.py", line 88, in get_thread_info
    if task.type.fields()[0].type == thread_info_type.get_type():
       ~~~~~~~~~~~~~~~~~~^^^

IndexError: list index out of range

---
 scripts/gdb/linux/tasks.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
scripts/gdb/linux/tasks.py +1 -1
index 6793d6e86e77..62348397c1f5 100644
@@ -85,7 +85,7 @@ thread_info_type = utils.CachedType("struct thread_info")
 
 def get_thread_info(task):
     thread_info_ptr_type = thread_info_type.get_type().pointer()
-    if task.type.fields()[0].type == thread_info_type.get_type():
+    if task_type.get_type().fields()[0].type == thread_info_type.get_type():
         return task['thread_info']
     thread_info = task['stack'].cast(thread_info_ptr_type)
     return thread_info.dereference()
Signature · 1 line
2.44.0
#4 lore raw

PATCH 4/4 scripts/gdb: Fix detection of current CPU in KGDB

In reply to Florian Rommel

Directly read the current CPU number from the kgdb_active variable.

Before, the active CPU was obtained through the current task, which required searching the task list for the pid of GDB's selected thread. Obtaining the pid was buggy:
GDB may use selected_thread().ptid[1] (LWPID) instead of .ptid[2] (TID) to store the threads pid; see
https://sourceware.org/gdb/current/onlinedocs/gdb.html/Threads-In-Python.html
As a result, the detection could return the wrong CPU number, leading to incorrect results for $lx_per_cpu and $lx_current.

As a side effect, the patch significantly speeds up $lx_per_cpu and $lx_current in KGDB by avoiding the task-list iteration.

---
 scripts/gdb/linux/cpus.py | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
scripts/gdb/linux/cpus.py +1 -5
index 2b51a3abd363..2f11c4f9c345 100644
@@ -26,11 +26,7 @@ def get_current_cpu():
     if utils.get_gdbserver_type() == utils.GDBSERVER_QEMU:
         return gdb.selected_thread().num - 1
     elif utils.get_gdbserver_type() == utils.GDBSERVER_KGDB:
-        tid = gdb.selected_thread().ptid[2]
-        if tid > (0x100000000 - MAX_CPUS - 2):
-            return 0x100000000 - tid - 2
-        else:
-            return tasks.get_thread_info(tasks.get_task_by_pid(tid))['cpu']
+        return gdb.parse_and_eval("kgdb_active.counter")
     else:
         raise gdb.GdbError("Sorry, obtaining the current CPU is not yet "
                            "supported with this gdb server.")
Signature · 1 line
2.44.0