Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] Always fetch Ada "main" name from the executable
@ 2026-07-30 17:03 Tom Tromey
  2026-07-31 10:48 ` Pedro Alves
  2026-08-05 17:07 ` Andrew Burgess
  0 siblings, 2 replies; 5+ messages in thread
From: Tom Tromey @ 2026-07-30 17:03 UTC (permalink / raw)
  To: gdb-patches; +Cc: Tom Tromey

The gdb.ada/file-then-restart.exp test was failing with gnat-llvm.  I
tracked this down to the "main" name not being stored in a readonly
section, meaning that the code in ada_main_name using trust_readonly
did not work.

However, it seems to me that gdb should always prefer the data from
the executable in this particular case.  So, rather than relying on
trust_readonly, this patch changes gdb to do this directly.
---
 gdb/ada-lang.c | 32 ++++++++++++++++++++------------
 1 file changed, 20 insertions(+), 12 deletions(-)

diff --git a/gdb/ada-lang.c b/gdb/ada-lang.c
index 3c6c9af488f..906c5cd3465 100644
--- a/gdb/ada-lang.c
+++ b/gdb/ada-lang.c
@@ -775,8 +775,6 @@ ada_get_decoded_type (struct type *type)
 const char *
 ada_main_name ()
 {
-  static gdb::unique_xmalloc_ptr<char> main_program_name;
-
   /* For Ada, the name of the main procedure is stored in a specific
      string constant, generated by the binder.  Look for that symbol,
      extract its address, and then read that string.  If we didn't find
@@ -786,21 +784,31 @@ ada_main_name ()
     = lookup_minimal_symbol (current_program_space,
 			     ADA_MAIN_PROGRAM_SYMBOL_NAME);
 
-  if (msym.minsym != NULL)
+  if (msym.minsym != nullptr)
     {
+      static gdb_byte main_program_name[1024];
+
       CORE_ADDR main_program_name_addr = msym.value_address ();
       if (main_program_name_addr == 0)
 	error (_("Invalid address for Ada main program name."));
 
-      /* Force trust_readonly, because we always want to fetch this
-	 string from the executable, not from inferior memory.  If the
-	 user changes the exec-file and invokes "start", we want to
-	 pick the "main" from the new executable, not one that may
-	 come from the still-live inferior.  */
-      scoped_restore save_trust_readonly
-	= make_scoped_restore (&trust_readonly, true);
-      main_program_name = target_read_string (main_program_name_addr, 1024);
-      return main_program_name.get ();
+      /* We always want to fetch this string from the executable, not
+	 from inferior memory.  If the user changes the exec-file and
+	 invokes "start", we want to pick the "main" from the new
+	 executable, not one that may come from the still-live
+	 inferior.  */
+      ULONGEST xferred = 0;
+      const auto &sections = current_program_space->target_sections ();
+      if ((section_table_xfer_memory_partial (main_program_name, nullptr,
+					      main_program_name_addr,
+					      sizeof (main_program_name),
+					      &xferred,
+					      sections)
+	   == TARGET_XFER_OK)
+	  && xferred > 0
+	  && (strnlen ((char *) main_program_name, sizeof (main_program_name))
+	      < sizeof (main_program_name)))
+	return (char *) main_program_name;
     }
 
   /* The main procedure doesn't seem to be in Ada.  */

base-commit: 6d1be0b90e837e4c82eaaf6f9e8c7da7227902e1
-- 
2.55.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-08-07 15:03 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-07-30 17:03 [PATCH] Always fetch Ada "main" name from the executable Tom Tromey
2026-07-31 10:48 ` Pedro Alves
2026-08-05 17:07 ` Andrew Burgess
2026-08-07 13:55   ` Tom Tromey
2026-08-07 15:03     ` Andrew Burgess

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox