All of lore.kernel.org
 help / color / mirror / Atom feed
From: Glenn Washburn <development@efficientek.com>
To: grub-devel@gnu.org, Daniel Kiper <dkiper@net-space.pl>
Cc: Robbie Harwood <rharwood@redhat.com>,
	Peter Jones <pjones@redhat.com>,
	Glenn Washburn <development@efficientek.com>
Subject: [PATCH v5 09/14] gdb: Add more support for debugging on EFI platforms
Date: Fri, 23 Dec 2022 22:19:30 -0600	[thread overview]
Message-ID: <20221224041935.787292-10-development@efficientek.com> (raw)
In-Reply-To: <20221224041935.787292-1-development@efficientek.com>

If the configure option --enable-efi-debug is given, then enable the
printing early in EFI startup of the command needed to load symbols for
the GRUB EFI kernel. This is needed because EFI firmware determines where
to load the GRUB EFI at runtime, and so the relevant addresses are not
known ahead of time. This is not printed when secure boot is enabled.

The command is a custom command defined in the gdb_grub GDB script. So
GDB should be started with the script as an argument to the -x option or
sourced into an active GDB session before running the outputted command.

Also a command named "gdbinfo" is enabled which allows the user to print
the gdb command string on-demand, which can be valuable as the printing
early in EFI startup is quickly replaced by other text. So if using a
physical screen it may appear too briefly to be registered.

Co-developed-by: Peter Jones <pjones@redhat.com>
Signed-off-by: Glenn Washburn <development@efficientek.com>
---
 config.h.in                 |  3 +++
 configure.ac                | 11 ++++++++++
 grub-core/Makefile.core.def |  1 +
 grub-core/kern/efi/debug.c  | 40 ++++++++++++++++++++++++++++++++++
 grub-core/kern/efi/efi.c    |  4 ++--
 grub-core/kern/efi/init.c   |  7 +++++-
 include/grub/efi/debug.h    | 43 +++++++++++++++++++++++++++++++++++++
 include/grub/efi/efi.h      |  2 +-
 8 files changed, 107 insertions(+), 4 deletions(-)
 create mode 100644 grub-core/kern/efi/debug.c
 create mode 100644 include/grub/efi/debug.h

diff --git a/config.h.in b/config.h.in
index 4d1e50eba7..9f88be1593 100644
--- a/config.h.in
+++ b/config.h.in
@@ -13,6 +13,9 @@
 #define MM_DEBUG @MM_DEBUG@
 #endif
 
+/* Define to 1 to enable printing of gdb command to load module symbols.  */
+#define PRINT_GDB_SYM_LOAD_CMD @EFI_DEBUG@
+
 /* Define to 1 to enable disk cache statistics.  */
 #define DISK_CACHE_STATS @DISK_CACHE_STATS@
 #define BOOT_TIME_STATS @BOOT_TIME_STATS@
diff --git a/configure.ac b/configure.ac
index 93626b7982..4bf477b072 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1521,6 +1521,17 @@ else
 fi
 AC_SUBST([MM_DEBUG])
 
+# EFI debugging.
+AC_ARG_ENABLE([efi-debug],
+	      AS_HELP_STRING([--enable-efi-debug],
+                             [include aides for debugging the EFI binary]))
+if test x$enable_efi_debug = xyes; then
+  EFI_DEBUG=1
+else
+  EFI_DEBUG=0
+fi
+AC_SUBST([EFI_DEBUG])
+
 AC_ARG_ENABLE([cache-stats],
 	      AS_HELP_STRING([--enable-cache-stats],
                              [enable disk cache statistics collection]))
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index 4951b049ec..9891253aac 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -206,6 +206,7 @@ kernel = {
 
   efi = disk/efi/efidisk.c;
   efi = kern/efi/efi.c;
+  efi = kern/efi/debug.c;
   efi = kern/efi/init.c;
   efi = kern/efi/mm.c;
   efi = term/efi/console.c;
diff --git a/grub-core/kern/efi/debug.c b/grub-core/kern/efi/debug.c
new file mode 100644
index 0000000000..c4f5561587
--- /dev/null
+++ b/grub-core/kern/efi/debug.c
@@ -0,0 +1,40 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+/* debug.c - aides for debugging the EFI application */
+
+#include <grub/efi/debug.h>
+#include <grub/command.h>
+#include <grub/i18n.h>
+
+__attribute__ ((unused)) static grub_err_t
+grub_cmd_gdbinfo (struct grub_command *cmd __attribute__ ((unused)),
+		  int argc __attribute__ ((unused)),
+		  char **args __attribute__ ((unused)))
+{
+  grub_efi_print_gdb_info ();
+  return 0;
+}
+
+void
+grub_efi_register_debug_commands (void)
+{
+#if PRINT_GDB_SYM_LOAD_CMD
+  grub_register_command_lockdown ("gdbinfo", grub_cmd_gdbinfo, 0,
+				  N_("Print infomation useful for GDB debugging"));
+#endif
+}
diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c
index cf49d6357e..17bd06f7e6 100644
--- a/grub-core/kern/efi/efi.c
+++ b/grub-core/kern/efi/efi.c
@@ -299,7 +299,7 @@ grub_efi_get_variable (const char *var, const grub_efi_guid_t *guid,
 /* Search the mods section from the PE32/PE32+ image. This code uses
    a PE32 header, but should work with PE32+ as well.  */
 grub_addr_t
-grub_efi_modules_addr (void)
+grub_efi_section_addr (const char *section_name)
 {
   grub_efi_loaded_image_t *image;
   struct grub_msdos_image_header *header;
@@ -328,7 +328,7 @@ grub_efi_modules_addr (void)
        i < coff_header->num_sections;
        i++, section++)
     {
-      if (grub_strcmp (section->name, "mods") == 0)
+      if (grub_strcmp (section->name, section_name) == 0)
 	break;
     }
 
diff --git a/grub-core/kern/efi/init.c b/grub-core/kern/efi/init.c
index b67bc73a1b..edacaddf06 100644
--- a/grub-core/kern/efi/init.c
+++ b/grub-core/kern/efi/init.c
@@ -19,6 +19,7 @@
 
 #include <grub/efi/efi.h>
 #include <grub/efi/console.h>
+#include <grub/efi/debug.h>
 #include <grub/efi/disk.h>
 #include <grub/efi/sb.h>
 #include <grub/lockdown.h>
@@ -104,7 +105,7 @@ grub_addr_t grub_modbase;
 void
 grub_efi_init (void)
 {
-  grub_modbase = grub_efi_modules_addr ();
+  grub_modbase = grub_efi_section_addr ("mods");
   /* First of all, initialize the console so that GRUB can display
      messages.  */
   grub_console_init ();
@@ -123,11 +124,15 @@ grub_efi_init (void)
       grub_lockdown ();
       grub_shim_lock_verifier_setup ();
     }
+  else
+    grub_efi_print_gdb_info ();
 
   efi_call_4 (grub_efi_system_table->boot_services->set_watchdog_timer,
 	      0, 0, 0, NULL);
 
   grub_efidisk_init ();
+
+  grub_efi_register_debug_commands ();
 }
 
 void (*grub_efi_net_config) (grub_efi_handle_t hnd,
diff --git a/include/grub/efi/debug.h b/include/grub/efi/debug.h
new file mode 100644
index 0000000000..dcf0bdbc4d
--- /dev/null
+++ b/include/grub/efi/debug.h
@@ -0,0 +1,43 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2022  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+/* debug.h - declare variables and functions for EFI debugging support */
+
+#ifndef GRUB_EFI_DEBUG_HEADER
+#define GRUB_EFI_DEBUG_HEADER	1
+
+#include <grub/efi/efi.h>
+#include <grub/misc.h>
+
+
+void grub_efi_register_debug_commands (void);
+
+void
+grub_efi_print_gdb_info (void)
+{
+#if PRINT_GDB_SYM_LOAD_CMD
+  grub_addr_t text;
+
+  text = grub_efi_section_addr (".text");
+  if (!text)
+    return;
+
+  grub_printf ("dynamic_load_symbols %p\n", (void *)text);
+#endif
+}
+
+#endif /* ! GRUB_EFI_DEBUG_HEADER */
diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h
index e61272de53..586ac856b5 100644
--- a/include/grub/efi/efi.h
+++ b/include/grub/efi/efi.h
@@ -109,7 +109,7 @@ grub_err_t grub_arch_efi_linux_boot_image(grub_addr_t addr, grub_size_t size,
                                            char *args);
 #endif
 
-grub_addr_t grub_efi_modules_addr (void);
+grub_addr_t grub_efi_section_addr (const char *section);
 
 void grub_efi_mm_init (void);
 void grub_efi_mm_fini (void);
-- 
2.34.1



  parent reply	other threads:[~2022-12-24  4:20 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-24  4:19 [PATCH v5 00/14] GDB script fixes and improvements Glenn Washburn
2022-12-24  4:19 ` [PATCH v5 01/14] gdb: Fix redirection issue in dump_module_sections Glenn Washburn
2022-12-24  4:19 ` [PATCH v5 02/14] gdb: Prevent wrapping when writing to .segments.tmp Glenn Washburn
2022-12-24  4:19 ` [PATCH v5 03/14] gdb: If no modules have been loaded, do not try to load module symbols Glenn Washburn
2022-12-24  4:19 ` [PATCH v5 04/14] gdb: Move runtime module loading into runtime_load_module Glenn Washburn
2022-12-24  4:19 ` [PATCH v5 05/14] gdb: Conditionally run GDB script logic for dynamically or statically positioned GRUB Glenn Washburn
2022-12-24  4:19 ` [PATCH v5 06/14] gdb: Only connect to remote target once when first sourced Glenn Washburn
2022-12-24  4:19 ` [PATCH v5 07/14] gdb: Replace module symbol loading implementation with Python one Glenn Washburn
2022-12-24  4:19 ` [PATCH v5 08/14] gdb: Add functions to make loading from dynamically positioned targets easier Glenn Washburn
2022-12-24  4:19 ` Glenn Washburn [this message]
2022-12-24  4:19 ` [PATCH v5 10/14] gdb: Allow running user-defined commands at GRUB start Glenn Washburn
2022-12-24  4:19 ` [PATCH v5 11/14] gdb: Fix issue with breakpoints defined before the GRUB image is loaded Glenn Washburn
2022-12-24  4:19 ` [PATCH v5 12/14] gdb: Add extra early initialization symbols for i386-pc Glenn Washburn
2022-12-24  4:19 ` [PATCH v5 13/14] gdb: Modify gdb prompt when running gdb_grub script Glenn Washburn
2022-12-24  4:19 ` [PATCH v5 14/14] docs: Add debugging chapter to development documentation Glenn Washburn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221224041935.787292-10-development@efficientek.com \
    --to=development@efficientek.com \
    --cc=dkiper@net-space.pl \
    --cc=grub-devel@gnu.org \
    --cc=pjones@redhat.com \
    --cc=rharwood@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.