Skip to contentSkip to conversation

GDB: Support getting current task struct in UML

PATCH Superseded Glenn Washburn 2 participants 7 messages Feb 14, 2023 – Feb 15, 2023

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover GDB: Support getting current task struct in UML

Hi all,

I noticed that running "call $lx_current()" with the kernel GDB helper scripts was crashing when running in UML. So, I've fixed this. I'm not 100% sure this is correct or covers all corner cases, but I've done some minimal testing. Perhaps someone on the linux-um list can review/comment on whether this looks good or not.

There's also a trivial indentation fix in the same area.

Glenn

Glenn Washburn (2):
scripts/gdb: Correct indentation in get_current_task scripts/gdb: Support getting current task struct in UML

 scripts/gdb/linux/cpus.py | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)
Signature · 7 lines
2.30.2


_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um
#1 lore raw

PATCH 1/2 scripts/gdb: Correct indentation in get_current_task

There is an extra space in a couple blocks in get_current_task. Though python does not care, let's make the spacing consistent.

---
 scripts/gdb/linux/cpus.py | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
scripts/gdb/linux/cpus.py +9 -9
index 15fc4626d236..289f0bf4d2a1 100644
@@ -163,16 +163,16 @@ def get_current_task(cpu):
     task_ptr_type = task_type.get_type().pointer()
 
     if utils.is_target_arch("x86"):
-         var_ptr = gdb.parse_and_eval("&current_task")
-         return per_cpu(var_ptr, cpu).dereference()
+        var_ptr = gdb.parse_and_eval("&current_task")
+        return per_cpu(var_ptr, cpu).dereference()
     elif utils.is_target_arch("aarch64"):
-         current_task_addr = gdb.parse_and_eval("$SP_EL0")
-         if((current_task_addr >> 63) != 0):
-             current_task = current_task_addr.cast(task_ptr_type)
-             return current_task.dereference()
-         else:
-             raise gdb.GdbError("Sorry, obtaining the current task is not allowed "
-                                "while running in userspace(EL0)")
+        current_task_addr = gdb.parse_and_eval("$SP_EL0")
+        if((current_task_addr >> 63) != 0):
+            current_task = current_task_addr.cast(task_ptr_type)
+            return current_task.dereference()
+        else:
+            raise gdb.GdbError("Sorry, obtaining the current task is not allowed "
+                               "while running in userspace(EL0)")
     else:
         raise gdb.GdbError("Sorry, obtaining the current task is not yet "
                            "supported with this arch")
Signature · 7 lines
2.30.2


_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um
maintainer
#2 lore raw

Re: [PATCH 1/2] scripts/gdb: Correct indentation in get_current_task

Quoted 20 lines · Glenn Washburn

There is an extra space in a couple blocks in get_current_task. Though python does not care, let's make the spacing consistent.

---
 scripts/gdb/linux/cpus.py | 18 +++++++++---------
 1 file changed, 9 insertions(+), 9 deletions(-)
scripts/gdb/linux/cpus.py +2 -2
index 15fc4626d236..289f0bf4d2a1 100644
@@ -163,16 +163,16 @@ def get_current_task(cpu):
     task_ptr_type = task_type.get_type().pointer()
 
     if utils.is_target_arch("x86"):
-         var_ptr = gdb.parse_and_eval("&current_task")
-         return per_cpu(var_ptr, cpu).dereference()
+        var_ptr = gdb.parse_and_eval("&current_task")
+        return per_cpu(var_ptr, cpu).dereference()

scripts/gdb/linux/cpus.py def get_current_task(cpu): · line 170

Quoted 10 lines
scripts/gdb/linux/cpus.py +2 -7
     elif utils.is_target_arch("aarch64"):
-         current_task_addr = gdb.parse_and_eval("$SP_EL0")
-         if((current_task_addr >> 63) != 0):
-             current_task = current_task_addr.cast(task_ptr_type)
-             return current_task.dereference()
-         else:
-             raise gdb.GdbError("Sorry, obtaining the current task is not allowed "
-                                "while running in userspace(EL0)")
+        current_task_addr = gdb.parse_and_eval("$SP_EL0")
+        if((current_task_addr >> 63) != 0):

At this chance: "if (current_task_addr >> 63) != 0:"

scripts/gdb/linux/cpus.py def get_current_task(cpu): · line 178

Quoted 8 lines
scripts/gdb/linux/cpus.py +5 -0
+            current_task = current_task_addr.cast(task_ptr_type)
+            return current_task.dereference()
+        else:
+            raise gdb.GdbError("Sorry, obtaining the current task is not allowed "
+                               "while running in userspace(EL0)")
     else:
         raise gdb.GdbError("Sorry, obtaining the current task is not yet "
                            "supported with this arch")

Jan

Signature · 8 lines
Siemens AG, Technology
Competence Center Embedded Linux


_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um
#3 lore raw

PATCH 2/2 scripts/gdb: Support getting current task struct in UML

In reply to Glenn Washburn

A running x86 UML kernel reports with architecture "i386:x86-64" as it is a sub-architecture. However, a difference with bare-metal x86 kernels is in how it manages tasks and the current task struct. To identify that the inferior is a UML kernel and not bare-metal, the symbol "uml_kmalloc" is checked for. If it exists, then do the UML specific way of getting the current task struct.

---
 scripts/gdb/linux/cpus.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
scripts/gdb/linux/cpus.py +6 -2
index 289f0bf4d2a1..90908653f6c7 100644
@@ -163,8 +163,12 @@ def get_current_task(cpu):
     task_ptr_type = task_type.get_type().pointer()
 
     if utils.is_target_arch("x86"):
-        var_ptr = gdb.parse_and_eval("&current_task")
-        return per_cpu(var_ptr, cpu).dereference()
+        if gdb.lookup_global_symbol("uml_kmalloc"):
+            var_ptr = gdb.parse_and_eval("(struct task_struct *)cpu_tasks[0].task")
+            return var_ptr.dereference()
+        else:
+            var_ptr = gdb.parse_and_eval("&current_task")
+            return per_cpu(var_ptr, cpu).dereference()
     elif utils.is_target_arch("aarch64"):
         current_task_addr = gdb.parse_and_eval("$SP_EL0")
         if((current_task_addr >> 63) != 0):
Signature · 7 lines
2.30.2


_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um
maintainer
#4 lore raw

Re: [PATCH 2/2] scripts/gdb: Support getting current task struct in UML

Quoted 17 lines · Glenn Washburn

A running x86 UML kernel reports with architecture "i386:x86-64" as it is a sub-architecture. However, a difference with bare-metal x86 kernels is in how it manages tasks and the current task struct. To identify that the inferior is a UML kernel and not bare-metal, the symbol "uml_kmalloc" is checked for. If it exists, then do the UML specific way of getting the current task struct.

---
 scripts/gdb/linux/cpus.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
scripts/gdb/linux/cpus.py +0 -0
index 289f0bf4d2a1..90908653f6c7 100644

scripts/gdb/linux/cpus.py def get_current_task(cpu): · line 167

Quoted 7 lines
scripts/gdb/linux/cpus.py +2 -2
@@ -163,8 +163,12 @@ def get_current_task(cpu):
     task_ptr_type = task_type.get_type().pointer()
 
     if utils.is_target_arch("x86"):
-        var_ptr = gdb.parse_and_eval("&current_task")
-        return per_cpu(var_ptr, cpu).dereference()
+        if gdb.lookup_global_symbol("uml_kmalloc"):
+            var_ptr = gdb.parse_and_eval("(struct task_struct *)cpu_tasks[0].task")

Can't we use an UML indicator that is related to the topic? Why not cpu_tasks itself?

scripts/gdb/linux/cpus.py def get_current_task(cpu): · line 174

Quoted 7 lines
scripts/gdb/linux/cpus.py +4 -0
+            return var_ptr.dereference()
+        else:
+            var_ptr = gdb.parse_and_eval("&current_task")
+            return per_cpu(var_ptr, cpu).dereference()
     elif utils.is_target_arch("aarch64"):
         current_task_addr = gdb.parse_and_eval("$SP_EL0")
         if((current_task_addr >> 63) != 0):

Jan

Signature · 8 lines
Siemens AG, Technology
Competence Center Embedded Linux


_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um
#5 lore raw

Re: [PATCH 2/2] scripts/gdb: Support getting current task struct in UML

Quoted 20 lines · On Wed, 15 Feb 2023 08:45:20 +0100 Jan Kiszka <[email protected]> wrote:
Quoted 19 lines · Glenn Washburn

A running x86 UML kernel reports with architecture "i386:x86-64" as it is a sub-architecture. However, a difference with bare-metal x86 kernels is in how it manages tasks and the current task struct. To identify that the inferior is a UML kernel and not bare-metal, the symbol "uml_kmalloc" is checked for. If it exists, then do the UML specific way of getting the current task struct.

---
 scripts/gdb/linux/cpus.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
scripts/gdb/linux/cpus.py +0 -0
index 289f0bf4d2a1..90908653f6c7 100644
@@ -163,8 +163,12 @@ def get_current_task(cpu):
     task_ptr_type = task_type.get_type().pointer()
 
Quoted 9 lines

scripts/gdb/linux/cpus.py def get_current_task(cpu):

Quoted 6 lines
scripts/gdb/linux/cpus.py +2 -2
     if utils.is_target_arch("x86"):
-        var_ptr = gdb.parse_and_eval("&current_task")
-        return per_cpu(var_ptr, cpu).dereference()
+        if gdb.lookup_global_symbol("uml_kmalloc"):
+            var_ptr = gdb.parse_and_eval("(struct task_struct
 *)cpu_tasks[0].task")

Can't we use an UML indicator that is related to the topic? Why not cpu_tasks itself?

Yes, I like that idea, and makes a lot of sense seeing as then we're sure cpu_tasks exists right before using it. The one thing that I like about using a symbol with a "uml_" prefix is that it makes it obvious that this branch is for UML kernels. I ultimately decided against a comment because using uml_kmalloc seemed self-documenting. Do you think a comment is warranted if I make the suggested change?

Glenn

Quoted 10 lines
Quoted 7 lines
scripts/gdb/linux/cpus.py +4 -0
+            return var_ptr.dereference()
+        else:
+            var_ptr = gdb.parse_and_eval("&current_task")
+            return per_cpu(var_ptr, cpu).dereference()
     elif utils.is_target_arch("aarch64"):
         current_task_addr = gdb.parse_and_eval("$SP_EL0")
         if((current_task_addr >> 63) != 0):

Jan

_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um

maintainer
#6 lore raw

Re: [PATCH 2/2] scripts/gdb: Support getting current task struct in UML

Quoted 33 lines · Glenn Washburn
Quoted 20 lines · On Wed, 15 Feb 2023 08:45:20 +0100 Jan Kiszka <[email protected]> wrote:
Quoted 19 lines · Glenn Washburn

A running x86 UML kernel reports with architecture "i386:x86-64" as it is a sub-architecture. However, a difference with bare-metal x86 kernels is in how it manages tasks and the current task struct. To identify that the inferior is a UML kernel and not bare-metal, the symbol "uml_kmalloc" is checked for. If it exists, then do the UML specific way of getting the current task struct.

---
 scripts/gdb/linux/cpus.py | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
scripts/gdb/linux/cpus.py +0 -0
index 289f0bf4d2a1..90908653f6c7 100644
@@ -163,8 +163,12 @@ def get_current_task(cpu):
     task_ptr_type = task_type.get_type().pointer()
 
Quoted 9 lines

scripts/gdb/linux/cpus.py def get_current_task(cpu):

Quoted 6 lines
scripts/gdb/linux/cpus.py +2 -2
     if utils.is_target_arch("x86"):
-        var_ptr = gdb.parse_and_eval("&current_task")
-        return per_cpu(var_ptr, cpu).dereference()
+        if gdb.lookup_global_symbol("uml_kmalloc"):
+            var_ptr = gdb.parse_and_eval("(struct task_struct
 *)cpu_tasks[0].task")

Can't we use an UML indicator that is related to the topic? Why not cpu_tasks itself?

Quoted 6 lines

Yes, I like that idea, and makes a lot of sense seeing as then we're sure cpu_tasks exists right before using it. The one thing that I like about using a symbol with a "uml_" prefix is that it makes it obvious that this branch is for UML kernels. I ultimately decided against a comment because using uml_kmalloc seemed self-documenting. Do you think a comment is warranted if I make the suggested change?

"uml" is clear, "kmalloc" is not so much again. No perfect fit, but I would leave a commit in any case.

Jan

Signature · 8 lines
Siemens AG, Technology
Competence Center Embedded Linux


_______________________________________________
linux-um mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-um