public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Andrew Burgess <aburgess@redhat.com>
To: gdb-patches@sourceware.org
Cc: Andrew Burgess <aburgess@redhat.com>
Subject: [PATCH] gdb: add gdbarch_stack_grows_down function
Date: Thu,  9 May 2024 15:59:04 +0100	[thread overview]
Message-ID: <e892c05f4a97f54914158e71a57b51d912e490cf.1715266700.git.aburgess@redhat.com> (raw)

In another patch I'm working on I needed to ask: does the stack grow
down, or grow up?

Looking around I found in infcall.c some code where we needed to ask
the same question, what we do there is ask:

  gdbarch_inner_than (gdbarch, 1, 2)

which should do the job.  However, I don't particularly like copying
this, it feels like we're asking something slightly different that
just happens to align with the question we're actually asking.

I propose adding a new function `gdbarch_stack_grows_down`.  This is
not going to be a gdbarch method that can be overridden, instead, this
will just call the gdbarch_inner_than function.  We already have some
gdbarch methods like this, checkout arch-utils.c for examples.

I think it's now clearer what we're actually doing.

There should be no user visible changes after this commit.
---
 gdb/gdbarch.h | 13 +++++++++++++
 gdb/infcall.c | 10 ++++------
 2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/gdb/gdbarch.h b/gdb/gdbarch.h
index 77d3406779f..70a6f43b06f 100644
--- a/gdb/gdbarch.h
+++ b/gdb/gdbarch.h
@@ -370,4 +370,17 @@ gdbarch_num_cooked_regs (gdbarch *arch)
   return gdbarch_num_regs (arch) + gdbarch_num_pseudo_regs (arch);
 }
 
+/* Return true if stacks for ARCH grow down, otherwise return true.  */
+
+static inline bool
+gdbarch_stack_grows_down (gdbarch *arch)
+{
+  /* The current assumption is that stacks either grow down, or they grow
+     up, so one of these checks should be true.  */
+  gdb_assert (gdbarch_inner_than (arch, 1, 2)
+	      || gdbarch_inner_than (arch, 2, 1));
+
+  return gdbarch_inner_than (arch, 1, 2);
+}
+
 #endif
diff --git a/gdb/infcall.c b/gdb/infcall.c
index 23d5652dd21..edac9a74179 100644
--- a/gdb/infcall.c
+++ b/gdb/infcall.c
@@ -947,7 +947,7 @@ reserve_stack_space (const type *values_type, CORE_ADDR &sp)
   struct gdbarch *gdbarch = get_frame_arch (frame);
   CORE_ADDR addr = 0;
 
-  if (gdbarch_inner_than (gdbarch, 1, 2))
+  if (gdbarch_stack_grows_down (gdbarch))
     {
       /* Stack grows downward.  Align STRUCT_ADDR and SP after
 	 making space.  */
@@ -1128,7 +1128,7 @@ call_function_by_hand_dummy (struct value *function,
 	   address.  AMD64 called that region the "red zone".  Skip at
 	   least the "red zone" size before allocating any space on
 	   the stack.  */
-	if (gdbarch_inner_than (gdbarch, 1, 2))
+	if (gdbarch_stack_grows_down (gdbarch))
 	  sp -= gdbarch_frame_red_zone_size (gdbarch);
 	else
 	  sp += gdbarch_frame_red_zone_size (gdbarch);
@@ -1156,11 +1156,9 @@ call_function_by_hand_dummy (struct value *function,
 	   to pay :-).  */
 	if (sp == old_sp)
 	  {
-	    if (gdbarch_inner_than (gdbarch, 1, 2))
-	      /* Stack grows down.  */
+	    if (gdbarch_stack_grows_down (gdbarch))
 	      sp = gdbarch_frame_align (gdbarch, old_sp - 1);
 	    else
-	      /* Stack grows up.  */
 	      sp = gdbarch_frame_align (gdbarch, old_sp + 1);
 	  }
 	/* SP may have underflown address zero here from OLD_SP.  Memory access
@@ -1193,7 +1191,7 @@ call_function_by_hand_dummy (struct value *function,
 	  {
 	    CORE_ADDR lastval_addr = lastval->address ();
 
-	    if (gdbarch_inner_than (gdbarch, 1, 2))
+	    if (gdbarch_stack_grows_down (gdbarch))
 	      {
 		gdb_assert (sp >= lastval_addr);
 		sp = lastval_addr;

base-commit: cba95c27876724059c3e99ea1857fb19b9cf8220
-- 
2.25.4


             reply	other threads:[~2024-05-09 14:59 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-09 14:59 Andrew Burgess [this message]
2024-05-09 19:43 ` Tom Tromey
2024-05-10  8:54   ` Andrew Burgess
2024-05-10  9:43     ` Tom Tromey
2024-05-10 13:31       ` Andrew Burgess

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=e892c05f4a97f54914158e71a57b51d912e490cf.1715266700.git.aburgess@redhat.com \
    --to=aburgess@redhat.com \
    --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).