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

From: Florian Rommel
Date: Thu Apr 25 2024 - 11:45:49 EST


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.

Signed-off-by: Florian Rommel <mail@xxxxxxxxxxxx>
---
scripts/gdb/linux/cpus.py | 6 +-----
1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/scripts/gdb/linux/cpus.py b/scripts/gdb/linux/cpus.py
index 2b51a3abd363..2f11c4f9c345 100644
--- a/scripts/gdb/linux/cpus.py
+++ b/scripts/gdb/linux/cpus.py
@@ -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.")
--
2.44.0