grub-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH v5 07/14] gdb: Replace module symbol loading implementation with


From: Glenn Washburn
Subject: [PATCH v5 07/14] gdb: Replace module symbol loading implementation with Python one
Date: Fri, 23 Dec 2022 22:19:28 -0600

Remove gmodule.pl and rewrite as a python in gdb_helper.py. This removes
PERL dependency for the GRUB GDB script, but adds Python as a dependency.
This is more desirable because Python is tightly integrated with GDB and
can do things not even available to GDB native scripting language. GDB must
be built with Python, however this is not a major limitation because every
major distro non-end-of-life versions build GDB with Python support. And GDB
has had support for Python since around 7.1-ish, which is about a decade.

This re-implementation has an added feature. If there is a user defined
command named "onload_<module name>", then that command will be executed
after the symbols for the specified module are loaded. When debugging a
module it can be desirable to set break points on code in the module.
This is difficult in GRUB because, at GDB start, the module is not loaded
and on EFI platforms its not known ahead of time where the module will
be loaded. So allow users to create an "onload_<modname>" command which
will be run when the module with name "modname" is loaded.

Another addition is a new convenience function is defined
$is_user_command(), which returns true if its string argument is
the name of a user-defined command.

A secondary benefit of these changes is that the script does not write
temporary files and has better error handling capabilities.

Signed-off-by: Glenn Washburn <development@efficientek.com>
---
 grub-core/Makefile.core.def |  4 +-
 grub-core/gdb_grub.in       | 54 ++----------------------
 grub-core/gdb_helper.py.in  | 82 +++++++++++++++++++++++++++++++++++++
 grub-core/gmodule.pl.in     | 30 --------------
 4 files changed, 87 insertions(+), 83 deletions(-)
 create mode 100644 grub-core/gdb_helper.py.in
 delete mode 100644 grub-core/gmodule.pl.in

diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index 95942fc8c9..4951b049ec 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -20,8 +20,8 @@ transform_data = {
 
 transform_data = {
   installdir = platform;
-  name = gmodule.pl;
-  common = gmodule.pl.in;
+  name = gdb_helper.py;
+  common = gdb_helper.py.in;
 };
 
 transform_data = {
diff --git a/grub-core/gdb_grub.in b/grub-core/gdb_grub.in
index 4ca939f69a..fc201204de 100644
--- a/grub-core/gdb_grub.in
+++ b/grub-core/gdb_grub.in
@@ -1,6 +1,6 @@
 ###
 ### Load debuging information about GNU GRUB 2 modules into GDB
-### automatically. Needs readelf, Perl and gmodule.pl script
+### automatically. Needs readelf, Python and gdb_helper.py script
 ###
 ### Has to be launched from the writable and trusted
 ### directory containing *.image and *.module
@@ -9,63 +9,15 @@
 ### Lubomir Kundrak <lkudrak@skosi.org>
 ###
 
-# Add section numbers and addresses to .segments.tmp
-define dump_module_sections_helper
-       set $mod = $arg0
-       printf "%s", $mod->name
-       set $segment = $mod->segment
-       while ($segment)
-               printf " %i 0x%lx", $segment->section, $segment->addr
-               set $segment = $segment->next
-       end
-       printf "\n"
-end
+source gdb_helper.py
 
-define dump_module_sections
-       # Set unlimited width so that lines don't get wrapped writing
-       # to .segments.tmp
-       with width 0 -- \
-       with trace-commands off -- \
-       pipe dump_module_sections_helper $arg0 | sh -c 'cat >>.segments.tmp'
-end
-document dump_module_sections
-       Gather information about module whose mod structure was
-       given for use with match_and_load_symbols
-end
-
-# Generate and execute GDB commands and delete temporary files
-# afterwards
-define match_and_load_symbols
-       shell perl gmodule.pl <.segments.tmp >.loadsym.gdb
-       source .loadsym.gdb
-       shell rm -f .segments.tmp .loadsym.gdb
-end
-document match_and_load_symbols
-       Launch script, that matches section names with information
-       generated by dump_module_sections and load debugging info
-       apropriately
-end
-
-###
-
-define load_module
-       dump_module_sections $arg0
-       match_and_load_symbols
-end
-document load_module
-       Load debugging information for module given as argument.
-end
 
 define load_all_modules
-       shell rm -f .segments.tmp
        set $this = grub_dl_head
        while ($this != 0)
-               dump_module_sections $this
+               load_module $this
                set $this = $this->next
        end
-       if (grub_dl_head != 0)
-               match_and_load_symbols
-       end
 end
 document load_all_modules
        Load debugging information for all loaded modules.
diff --git a/grub-core/gdb_helper.py.in b/grub-core/gdb_helper.py.in
new file mode 100644
index 0000000000..4306ef448a
--- /dev/null
+++ b/grub-core/gdb_helper.py.in
@@ -0,0 +1,82 @@
+import os
+import re
+import subprocess
+
+##### Convenience functions #####
+
+class IsUserCommand (gdb.Function):
+  """Set the second argument to true value if first argument is the name
+of a user-defined command.
+"""
+
+  def __init__ (self):
+    super (IsUserCommand, self).__init__ ("is_user_command")
+
+  def invoke (self, fmt, *args):
+    name = fmt.string () % tuple(a.string () for a in args)
+    for line in gdb.execute ("help user-defined", to_string=True).splitlines 
():
+      line_parts = line.split(' -- ', 1)
+      if len (line_parts) > 1 and line_parts[0] == name:
+        return True
+    return False
+
+is_user_command = IsUserCommand ()
+
+##### Commands #####
+
+class GrubLoadModuleSymbols (gdb.Command):
+  """Load module symbols at correct locations.
+Takes one argument which is a pointer to a grub_dl_t struct."""
+
+  def __init__ (self):
+    super (GrubLoadModuleSymbols, self).__init__ ("load_module",
+                                                 gdb.COMMAND_USER,
+                                                 gdb.COMPLETE_EXPRESSION)
+
+  def invoke (self, arg, from_tty):
+    self.dont_repeat ()
+    args = gdb.string_to_argv (arg)
+    self.mod = gdb.parse_and_eval (args[0])
+    sections = self.get_section_offsets ()
+    section_names = self.get_section_names ()
+
+    sym_load_cmd_parts = ["add-symbol-file",
+                         "%s.module" % (self.mod['name'].string (),)]
+    for idx, addr in sections:
+      section_name = section_names[idx]
+      if section_name == ".text":
+        sym_load_cmd_parts.append (addr)
+      else:
+        sym_load_cmd_parts.extend (["-s", section_name, addr])
+    gdb.execute (' '.join (sym_load_cmd_parts))
+
+    if is_user_command.invoke (gdb.Value ("onload_%s"), self.mod['name']):
+      gdb.execute ("onload_%s (grub_dl_t)%s" % (self.mod['name'].string (),
+                                               self.mod.format_string 
(format='x')))
+
+  def get_section_offsets (self):
+    sections = []
+    segment = self.mod['segment']
+    while segment:
+      sections.append ((int (segment['section']), 
segment['addr'].format_string (format='x')))
+      segment = segment['next']
+    return sections
+
+  def get_section_names (self):
+    re_index = re.compile ("^\s+\[\s*(\d+)\] (\S*)")
+    names = {}
+    modfilename = "%s.mod" % (self.mod['name'].string (),)
+
+    if not os.path.exists (modfilename):
+      raise RuntimeError ("%s not found in current directory" % (modfilename,))
+
+    c = subprocess.run (["readelf", "-SW", modfilename], text=True, 
capture_output=True)
+    for line in c.stdout.splitlines ()[4:]:
+      m = re_index.match (line)
+      if not m:
+        continue
+      idx, name = m.groups ()
+      names[int (idx)] = name
+    return names
+
+grub_load_module = GrubLoadModuleSymbols ()
diff --git a/grub-core/gmodule.pl.in b/grub-core/gmodule.pl.in
deleted file mode 100644
index 78aa1e64eb..0000000000
--- a/grub-core/gmodule.pl.in
+++ /dev/null
@@ -1,30 +0,0 @@
-###
-### Generate GDB commands, that load symbols for specified module,
-### with proper section relocations. See .gdbinit
-###
-### $Id: gmodule.pl,v 1.2 2006/05/14 11:38:42 lkundrak Exp lkundrak $
-### Lubomir Kundrak <lkudrak@skosi.org>
-###
-
-use strict;
-
-while (<>) {
-       my ($name, %sections) = split;
-
-       print "add-symbol-file $name.module";
-
-       open (READELF, "readelf -S $name.mod |") or die;
-       while (<READELF>) {
-               /\[\s*(\d+)\]\s+(\.\S+)/ or next;
-
-               if ($2 eq '.text') {
-                       print " $sections{$1}";
-                       next;
-               }
-
-               print " -s $2 $sections{$1}"
-                       if ($sections{$1} ne '0x0' and $sections{$1} ne '');
-       };
-       close (READELF);
-       print "\n";
-}
-- 
2.34.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]