[v5,03/13] gdb: Add get_main_func_start_pc to refactor frame.c:inside_main_func.

Message ID 20260924083311.1961530-4-christina.joos@intel.com
State New
Headers
Series Add new command to print the shadow stack backtrace |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-arm fail Patch failed to apply
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 fail Patch failed to apply

Commit Message

Joos, Christina Sept. 24, 2026, 8:33 a.m. UTC
  Refactor frame.c:inside_main_func to use the new function
get_main_func_start_pc, which will be called in a following commmit.

Reviewed-by: Thiago Jung Bauermann <thiago.bauermann@linaro.org>
---
 gdb/frame.c | 37 +++++++++++++++++++++++++------------
 gdb/frame.h |  6 ++++++
 2 files changed, 31 insertions(+), 12 deletions(-)
  

Patch

diff --git a/gdb/frame.c b/gdb/frame.c
index 6b02c5f559a..6bf72b6908e 100644
--- a/gdb/frame.c
+++ b/gdb/frame.c
@@ -2650,27 +2650,26 @@  frame_debug_got_null_frame (const frame_info_ptr &this_frame,
     }
 }
 
-/* Is this (non-sentinel) frame in the "main"() function?  */
+/* See frame.h.  */
 
-static bool
-inside_main_func (const frame_info_ptr &this_frame)
+std::optional<CORE_ADDR>
+get_main_func_start_pc (gdbarch *gdbarch, const language lang)
 {
   if (current_program_space->symfile_object_file == nullptr)
-    return false;
+    return {};
 
-  CORE_ADDR sym_addr = 0;
   const char *name = main_name ();
   bound_minimal_symbol msymbol
     = lookup_minimal_symbol (current_program_space, name,
 			     current_program_space->symfile_object_file);
 
+  CORE_ADDR sym_addr = 0;
   if (msymbol.minsym != nullptr)
     sym_addr = msymbol.value_address ();
 
   /* Favor a full symbol in Fortran, for the case where the Fortran main
      is also called "main".  */
-  if (msymbol.minsym == nullptr
-      || get_frame_language (this_frame) == language_fortran)
+  if (msymbol.minsym == nullptr || lang == language_fortran)
     {
       /* In some language (for example Fortran) there will be no minimal
 	 symbol with the name of the main function.  In this case we should
@@ -2686,16 +2685,30 @@  inside_main_func (const frame_info_ptr &this_frame)
 	  sym_addr = block->start ();
 	}
       else if (msymbol.minsym == nullptr)
-	return false;
+	return {};
     }
 
   /* Convert any function descriptor addresses into the actual function
      code address.  */
-  sym_addr = (gdbarch_convert_from_func_ptr_addr
-	      (get_frame_arch (this_frame), sym_addr,
-	       current_inferior ()->top_target ()));
+  return {gdbarch_convert_from_func_ptr_addr
+	    (gdbarch,
+	     sym_addr,
+	     current_inferior ()->top_target ())};
+}
+
+/* Is this (non-sentinel) frame in the "main"() function?  */
+
+static bool
+inside_main_func (const frame_info_ptr &this_frame)
+{
+  std::optional<CORE_ADDR> sym_addr
+    = get_main_func_start_pc (get_frame_arch (this_frame),
+			      get_frame_language (this_frame));
+
+  if (!sym_addr.has_value ())
+    return false;
 
-  return sym_addr == get_frame_func (this_frame);
+  return get_frame_func (this_frame) == *sym_addr;
 }
 
 /* Test whether THIS_FRAME is inside the process entry point function.  */
diff --git a/gdb/frame.h b/gdb/frame.h
index b64650f3215..6f6259cf722 100644
--- a/gdb/frame.h
+++ b/gdb/frame.h
@@ -888,6 +888,12 @@  extern struct symbol *get_frame_function (const frame_info_ptr &);
 
 extern CORE_ADDR get_pc_function_start (CORE_ADDR);
 
+/* If possible, get the start pc of the main function of the current program
+   space for the language LANG.  */
+
+extern std::optional<CORE_ADDR> get_main_func_start_pc (gdbarch *gdbarch,
+							const language lang);
+
 extern frame_info_ptr find_relative_frame (frame_info_ptr, int *);
 
 /* Wrapper over print_stack_frame modifying current_uiout with UIOUT for