]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb/tui: return std::string from tui_get_function_from_frame
authorAndrew Burgess <aburgess@redhat.com>
Thu, 22 Jan 2026 14:09:37 +0000 (14:09 +0000)
committerAndrew Burgess <aburgess@redhat.com>
Fri, 23 Jan 2026 10:45:59 +0000 (10:45 +0000)
Update tui_get_function_from_frame to return a std::string rather than
a pointer into a static buffer.

The value returned from tui_get_function_from_frame is passed to
tui_location_tracker::set_location, which already stores the data in a
std::string; this just moves the string creation earlier.

I don't think there was anything particularly wrong with the old code,
but I'm not a huge fan of returning data in static buffers unless
there's a really good reason, and it doesn't feel like there's a
really good reason in this case.

The current approach in tui_get_function_from_frame is to call
print_address_symbolic, and then to pull the function name from the
result.  There is an argument that this approach could be improved,
but I've not done that in this commit, nor do I plan to do that any
time soon.  As such the new code should do exactly what the old code
did.

There should be no user visible changes after this commit.

Approved-By: Tom Tromey <tom@tromey.com>
gdb/tui/tui-location.c
gdb/tui/tui-location.h
gdb/tui/tui-status.c

index 9527ae50c60282fe7cc7c41a2157bb754432e612..003c022f1e8f047abae5b8109fc6c8cda6869dff 100644 (file)
@@ -29,17 +29,15 @@ tui_location_tracker tui_location;
 bool
 tui_location_tracker::set_location (struct gdbarch *gdbarch,
                                    const struct symtab_and_line &sal,
-                                   const char *procname)
+                                   std::string procname)
 {
-  gdb_assert (procname != nullptr);
-
   bool location_changed_p = set_fullname (sal.symtab);
   location_changed_p |= procname != m_proc_name;
   location_changed_p |= sal.line != m_line_no;
   location_changed_p |= sal.pc != m_addr;
   location_changed_p |= gdbarch != m_gdbarch;
 
-  m_proc_name = procname;
+  m_proc_name = std::move (procname);
   m_line_no = sal.line;
   m_addr = sal.pc;
   m_gdbarch = gdbarch;
index 6b663f1df9b3b201a03615bbb17b3aaa97dc90b9..5cf29c3716b704c9c3f7b865ade5c4723bea6ed8 100644 (file)
@@ -32,7 +32,7 @@ struct tui_location_tracker
      and false otherwise.  */
   bool set_location (struct gdbarch *gdbarch,
                     const struct symtab_and_line &sal,
-                    const char *procname);
+                    std::string procname);
 
   /* Update the current location with the with the provided argument.
      Return true if any of the fields actually changed, otherwise false.  */
index b58bf9aa990fd03411ae3f1a9eed253f80767d72..7b44fea3789aef6828664a965e9bb8fc5ab13b82 100644 (file)
@@ -204,13 +204,12 @@ tui_status_window::make_status_line () const
   return string_val;
 }
 
-/* Get a printable name for the function at the address.  The symbol
-   name is demangled if demangling is turned on.  Returns a pointer to
-   a static area holding the result.  */
-static char*
+/* Get a printable name for the function representing frame FI.  The symbol
+   name is demangled if demangling is turned on.  Returns a std::string
+   containing the function name, which could be empty.  */
+static std::string
 tui_get_function_from_frame (const frame_info_ptr &fi)
 {
-  static char name[256];
   string_file stream;
 
   print_address_symbolic (get_frame_arch (fi), get_frame_pc (fi),
@@ -219,20 +218,20 @@ tui_get_function_from_frame (const frame_info_ptr &fi)
   /* Use simple heuristics to isolate the function name.  The symbol
      can be demangled and we can have function parameters.  Remove
      them because the status line is too short to display them.  */
-  const char *d = stream.c_str ();
-  if (*d == '<')
-    d++;
-  strncpy (name, d, sizeof (name) - 1);
-  name[sizeof (name) - 1] = 0;
-
-  char *p = strchr (name, '(');
-  if (!p)
-    p = strchr (name, '>');
-  if (p)
-    *p = 0;
-  p = strchr (name, '+');
-  if (p)
-    *p = 0;
+  std::string name = stream.release ();
+  if (!name.empty () && name.front () == '<')
+    name = name.erase (0, 1);
+
+  size_t pos = name.find ('(');
+  if (pos == std::string::npos)
+    pos = name.find ('>');
+  if (pos != std::string::npos)
+    name.erase (pos);
+
+  pos = name.find ('+');
+  if (pos != std::string::npos)
+    name.erase (pos);
+
   return name;
 }
 
@@ -270,7 +269,7 @@ tui_show_frame_info (const frame_info_ptr &fi)
     {
       symtab_and_line sal = find_frame_sal (fi);
 
-      const char *func_name;
+      std::string func_name;
       std::optional<CORE_ADDR> tmp_pc = get_frame_pc_if_available (fi);
       /* find_frame_sal does not always set PC, but we want to ensure
         that it is available in the SAL.  */
@@ -284,7 +283,7 @@ tui_show_frame_info (const frame_info_ptr &fi)
 
       struct gdbarch *gdbarch = get_frame_arch (fi);
       status_changed_p
-       = tui_location.set_location (gdbarch, sal, func_name);
+       = tui_location.set_location (gdbarch, sal, std::move (func_name));
 
       /* If the status information has not changed, then frame information has
         not changed.  If frame information has not changed, then the windows'