public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Hannes Domani <ssbssa@yahoo.de>
To: gdb-patches@sourceware.org
Subject: [PATCH 2/3] Move return_value_info and refactor code setting its members
Date: Sat, 10 Dec 2022 17:23:25 +0100	[thread overview]
Message-ID: <20221210162326.854-2-ssbssa@yahoo.de> (raw)
In-Reply-To: <20221210162326.854-1-ssbssa@yahoo.de>

This moves the function and return_buf members from finish_command_fsm
to return_value_info, and refactors the code setting its members
into functions get_callee_info and get_return_value_info.

And all of that is moved before step_command_fsm, since it will use
them in the next patch.
---
 gdb/infcmd.c | 157 ++++++++++++++++++++++++++++-----------------------
 1 file changed, 86 insertions(+), 71 deletions(-)

diff --git a/gdb/infcmd.c b/gdb/infcmd.c
index a5fcf2f8ea5..9c96d9a39ee 100644
--- a/gdb/infcmd.c
+++ b/gdb/infcmd.c
@@ -800,6 +800,86 @@ nexti_command (const char *count_string, int from_tty)
   step_1 (1, 1, count_string);
 }
 
+/* The captured function return value/type and its position in the
+   value history.  */
+
+struct return_value_info
+{
+  /* The function that we're stepping out of.  */
+  struct symbol *function;
+
+  /* If the current function uses the "struct return convention",
+     this holds the address at which the value being returned will
+     be stored, or zero if that address could not be determined or
+     the "struct return convention" is not being used.  */
+  CORE_ADDR return_buf;
+
+  /* The captured return value.  May be NULL if we weren't able to
+     retrieve it.  See get_return_value.  */
+  struct value *value;
+
+  /* The return type.  In some cases, we'll not be able extract the
+     return value, but we always know the type.  */
+  struct type *type;
+
+  /* If we captured a value, this is the value history index.  */
+  int value_history_index;
+};
+
+/* Get calle function symbol and return convention.  */
+
+static void
+get_callee_info (return_value_info *rv, frame_info_ptr frame)
+{
+  rv->function = find_pc_function (get_frame_pc (frame));
+
+  /* Determine the return convention.  If it is RETURN_VALUE_STRUCT_CONVENTION,
+     attempt to determine the address of the return buffer.  */
+  if (rv->function != nullptr)
+    {
+      enum return_value_convention return_value;
+      struct gdbarch *gdbarch = get_frame_arch (frame);
+
+      struct type * val_type
+	= check_typedef (rv->function->type ()->target_type ());
+
+      return_value = gdbarch_return_value
+	(gdbarch, read_var_value (rv->function, nullptr, frame),
+	 val_type, nullptr, nullptr, nullptr);
+
+      if (return_value == RETURN_VALUE_STRUCT_CONVENTION
+	  && val_type->code () != TYPE_CODE_VOID)
+	rv->return_buf
+	  = gdbarch_get_return_buf_addr (gdbarch, val_type, frame);
+    }
+}
+
+/* Get return value and add it to value history.  */
+
+static void
+get_return_value_info (return_value_info *rv)
+{
+  rv->type = rv->function->type ()->target_type ();
+  if (rv->type == nullptr)
+    internal_error (_("finish_command: function has no target type"));
+
+  if (check_typedef (rv->type)->code () != TYPE_CODE_VOID)
+    {
+      struct value *func;
+
+      func = read_var_value (rv->function, nullptr, get_current_frame ());
+
+      if (rv->return_buf != 0)
+	/* Retrieve return value from the buffer where it was saved.  */
+	rv->value = value_at (rv->type, rv->return_buf);
+      else
+	rv->value = get_return_value (rv->function, func);
+
+      if (rv->value != nullptr)
+	rv->value_history_index = record_latest_value (rv->value);
+    }
+}
+
 /* Data for the FSM that manages the step/next/stepi/nexti
    commands.  */
 
@@ -1517,23 +1597,6 @@ get_return_value (struct symbol *func_symbol, struct value *function)
   return value;
 }
 
-/* The captured function return value/type and its position in the
-   value history.  */
-
-struct return_value_info
-{
-  /* The captured return value.  May be NULL if we weren't able to
-     retrieve it.  See get_return_value.  */
-  struct value *value;
-
-  /* The return type.  In some cases, we'll not be able extract the
-     return value, but we always know the type.  */
-  struct type *type;
-
-  /* If we captured a value, this is the value history index.  */
-  int value_history_index;
-};
-
 /* Helper for print_return_value.  */
 
 static void
@@ -1603,19 +1666,10 @@ struct finish_command_fsm : public thread_fsm
      the caller.  */
   breakpoint_up breakpoint;
 
-  /* The function that we're stepping out of.  */
-  struct symbol *function = nullptr;
-
   /* If the FSM finishes successfully, this stores the function's
      return value.  */
   struct return_value_info return_value_info {};
 
-  /* If the current function uses the "struct return convention",
-     this holds the address at which the value being returned will
-     be stored, or zero if that address could not be determined or
-     the "struct return convention" is not being used.  */
-  CORE_ADDR return_buf;
-
   explicit finish_command_fsm (struct interp *cmd_interp)
     : thread_fsm (cmd_interp)
   {
@@ -1637,32 +1691,14 @@ finish_command_fsm::should_stop (struct thread_info *tp)
 {
   struct return_value_info *rv = &return_value_info;
 
-  if (function != nullptr
+  if (rv->function != nullptr
       && bpstat_find_breakpoint (tp->control.stop_bpstat,
 				 breakpoint.get ()) != nullptr)
     {
       /* We're done.  */
       set_finished ();
 
-      rv->type = function->type ()->target_type ();
-      if (rv->type == nullptr)
-	internal_error (_("finish_command: function has no target type"));
-
-      if (check_typedef (rv->type)->code () != TYPE_CODE_VOID)
-	{
-	  struct value *func;
-
-	  func = read_var_value (function, nullptr, get_current_frame ());
-
-	  if (return_buf != 0)
-	    /* Retrieve return value from the buffer where it was saved.  */
-	      rv->value = value_at (rv->type, return_buf);
-	  else
-	      rv->value = get_return_value (function, func);
-
-	  if (rv->value != nullptr)
-	    rv->value_history_index = record_latest_value (rv->value);
-	}
+      get_return_value_info (rv);
     }
   else if (tp->control.stop_step)
     {
@@ -1876,29 +1912,7 @@ finish_command (const char *arg, int from_tty)
 
   /* Find the function we will return from.  */
   frame_info_ptr callee_frame = get_selected_frame (nullptr);
-  sm->function = find_pc_function (get_frame_pc (callee_frame));
-  sm->return_buf = 0;    /* Initialize buffer address is not available.  */
-
-  /* Determine the return convention.  If it is RETURN_VALUE_STRUCT_CONVENTION,
-     attempt to determine the address of the return buffer.  */
-  if (sm->function != nullptr)
-    {
-      enum return_value_convention return_value;
-      struct gdbarch *gdbarch = get_frame_arch (callee_frame);
-
-      struct type * val_type
-	= check_typedef (sm->function->type ()->target_type ());
-
-      return_value = gdbarch_return_value (gdbarch,
-					   read_var_value (sm->function, nullptr,
-							   callee_frame),
-					   val_type, nullptr, nullptr, nullptr);
-
-      if (return_value == RETURN_VALUE_STRUCT_CONVENTION
-	  && val_type->code () != TYPE_CODE_VOID)
-	sm->return_buf = gdbarch_get_return_buf_addr (gdbarch, val_type,
-						      callee_frame);
-    }
+  get_callee_info (&sm->return_value_info, callee_frame);
 
   /* Print info on the selected frame, including level number but not
      source.  */
@@ -1908,10 +1922,11 @@ finish_command (const char *arg, int from_tty)
 	gdb_printf (_("Run back to call of "));
       else
 	{
-	  if (sm->function != nullptr && TYPE_NO_RETURN (sm->function->type ())
+	  if (sm->return_value_info.function != nullptr
+	      && TYPE_NO_RETURN (sm->return_value_info.function->type ())
 	      && !query (_("warning: Function %s does not return normally.\n"
 			   "Try to finish anyway? "),
-			 sm->function->print_name ()))
+			 sm->return_value_info.function->print_name ()))
 	    error (_("Not confirmed."));
 	  gdb_printf (_("Run till exit from "));
 	}
-- 
2.35.1


  reply	other threads:[~2022-12-10 16:22 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20221210162326.854-1-ssbssa.ref@yahoo.de>
2022-12-10 16:23 ` [PATCH 1/3] Change thread_fsm::return_value to thread_fsm::print_return_values Hannes Domani
2022-12-10 16:23   ` Hannes Domani [this message]
2022-12-10 16:23   ` [PATCH 3/3] [RFC] Implement printing of return values of stepped-over functions Hannes Domani
2022-12-10 16:33     ` Eli Zaretskii

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=20221210162326.854-2-ssbssa@yahoo.de \
    --to=ssbssa@yahoo.de \
    --cc=gdb-patches@sourceware.org \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).