[bootlin/training-materials updates] master: debugging: labs: gdb: rework python lab and provide almost entire skeleton (8c27d5de)

Clément Léger clement.leger at bootlin.com
Thu Nov 17 13:10:31 CET 2022

Repository : https://github.com/bootlin/training-materials
On branch  : master
Link       : https://github.com/bootlin/training-materials/commit/8c27d5ded6439b9ef72803af883194dbb68d4a61


commit 8c27d5ded6439b9ef72803af883194dbb68d4a61
Author: Clément Léger <clement.leger at bootlin.com>
Date:   Thu Nov 17 13:08:30 2022 +0100

    debugging: labs: gdb: rework python lab and provide almost entire skeleton
    Signed-off-by: Clément Léger <clement.leger at bootlin.com>


 lab-data/debugging/nfsroot/root/gdb/linked_list.py | 38 ++++++++++++++++++++++
 .../debugging-application-crash.tex                | 32 ++++++------------
 2 files changed, 48 insertions(+), 22 deletions(-)

diff --git a/lab-data/debugging/nfsroot/root/gdb/linked_list.py b/lab-data/debugging/nfsroot/root/gdb/linked_list.py
new file mode 100644
index 00000000..6a375486
--- /dev/null
+++ b/lab-data/debugging/nfsroot/root/gdb/linked_list.py
@@ -0,0 +1,38 @@
+import gdb
+class StructNamePrinter:
+    "Print a struct name"
+    def __init__(self, val):
+        self.val = val
+    def to_string(self):
+	return "TODO"
+def struct_name_lookup_function(val):
+	if str(val.type) == 'struct name':
+		return StructNamePrinter(val)
+	return None
+class PrintSList (gdb.Command):
+	def __init__(self):
+		super(PrintSList, self).__init__("printslist", gdb.COMMAND_USER)
+	def invoke(self, arg, from_tty):
+		args = gdb.string_to_argv(arg)
+		if len(args) < 2:
+			print("Usage: printslist <list head> <next field name>")
+			return
+		list = gdb.parse_and_eval(args[0])
+		next_field = args[1]
+		elem = list['slh_first']
+		while elem != 0:
+			print(elem.dereference())
+			elem = TODO
\ No newline at end of file
diff --git a/labs/debugging-application-crash/debugging-application-crash.tex b/labs/debugging-application-crash/debugging-application-crash.tex
index 950e86db..8120064c 100644
--- a/labs/debugging-application-crash/debugging-application-crash.tex
+++ b/labs/debugging-application-crash/debugging-application-crash.tex
@@ -98,31 +98,19 @@ When developping and debugging applications, sometimes we often uses the same
 set of commands over and over under GDB. Rather than doing so, we can create 
 python scripts that are integrated with GDB.
-In order to display our program list from GDB, create a python GDB script named
-\code{linked_list.py} that displays this list. You will add a new command which
-will be of type \code{COMMAND_USER} and will be named \code{printslist}. This
-command will use the \code{name_list} struct to traverse the list. You can use
-\code{gdb.parse_and_eval("name_list")} to obtain a python \code{GDB.Value}
-object that can be inspected. If the object is a struct then you can access
-the fields using \code{value['field']}.
+In order to display our program list from GDB, we provide a python GDB script
+named \code{linked_list.py} that displays this list. You will need to fill two
+parts of this script to display a complete list correctly. This python script
+takes the list head name and the next field name as parameters.
-Once done, in GDB, check that it works by sourcing your python script and
-executing it:
+The part to be filled in are the pretty printer struct formatting and the
+iteration on the list. We would like to display each \code{struct name} as
+\code{index: name}. In order to access a struct field in gdb python, you can use
-(gdb) source linked_list.py
-(gdb) printslist
-In order to display something more user friendly, we will create a pretty
-printer for the \code{struct name} and modify the printslist command to take
-two arguments which are the list name and the field name to be used for list
-traversal. In our case, the command would be called
-like this:
+Once done, you can use the following commands to test your script:
+(gdb) source linked_list.py
 (gdb) printslist name_list next
-You can use the \code{gdb.string_to_argv(args)} to convert the args into an
-array of arguments.
\ No newline at end of file

More information about the training-materials-updates mailing list