Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Sandra Loosemore <sandra@codesourcery.com>
To: Yao Qi <yao@codesourcery.com>, <gdb-patches@sourceware.org>
Subject: [2/3 patch, nios2] clean up prologue/epilogue detection code, v2
Date: Tue, 25 Nov 2014 00:03:00 -0000	[thread overview]
Message-ID: <5473C736.6050501@codesourcery.com> (raw)
In-Reply-To: <5473C4BA.2050903@codesourcery.com>

[-- Attachment #1: Type: text/plain, Size: 233 bytes --]

This part of the revised Nios II prologue/epilogue refactoring patch set 
fixes the epilogue detection code to deal with the multiple stack 
adjustments that GCC may now emit in some cases.  OK to commit on top of 
part 1?

-Sandra


[-- Attachment #2: gdb-nios2-epilogue-2.log --]
[-- Type: text/x-log, Size: 138 bytes --]

2014-11-24  Sandra Loosemore  <sandra@codesourcery.com>

	gdb/
	* nios2-tdep.c (nios2_in_epilogue_p): Handle multiple stack
	adjustments.

[-- Attachment #3: gdb-nios2-epilogue-2.patch --]
[-- Type: text/x-patch, Size: 2943 bytes --]

diff -u b/gdb/nios2-tdep.c b/gdb/nios2-tdep.c
--- b/gdb/nios2-tdep.c
+++ b/gdb/nios2-tdep.c
@@ -589,26 +589,42 @@
 		     CORE_ADDR start_pc)
 {
   unsigned long mach = gdbarch_bfd_arch_info (gdbarch)->mach;
+  /* Maximum number of possibly-epilogue instructions to check.
+     Note that this number should not be too large, else we can
+     potentially end up iterating through unmapped memory.  */
+  int ninsns, max_insns = 5;
   unsigned int insn;
   const struct nios2_opcode *op = NULL;
   unsigned int uimm;
   int imm;
   int ra, rb, rc;
   enum branch_condition cond;
+  CORE_ADDR pc;
 
   /* There has to be a previous instruction in the function.  */
-  if (current_pc > start_pc)
-    {
-      int ok = 0;
+  if (current_pc <= start_pc)
+    return 0;
 
-      /* Check whether the previous instruction was a stack adjustment.
-	 Possible instructions here include:
+  /* Find the previous instruction before current_pc.
+     For the moment we will assume that all instructions are the
+     same size here.  */
+  pc = current_pc - NIOS2_OPCODE_SIZE;
+
+  /* Beginning with the previous instruction we just located, check whether
+     we are in a sequence of at least one stack adjustment instruction.
+     Possible instructions here include:
 	 ADDI sp, sp, n
 	 ADD sp, sp, rn
 	 LDW sp, n(sp)  */
-      op = nios2_fetch_insn (gdbarch, current_pc - NIOS2_OPCODE_SIZE, &insn);
+  for (ninsns = 0; ninsns < max_insns; ninsns++)
+    {
+      int ok = 0;
+
+      /* Fetch the insn at pc.  */
+      op = nios2_fetch_insn (gdbarch, pc, &insn);
       if (op == NULL)
 	return 0;
+      pc += op->size;
 
       /* Was it a stack adjustment?  */
       if (nios2_match_addi (insn, op, mach, &ra, &rb, &imm))
@@ -618,18 +634,27 @@
       else if (nios2_match_ldw (insn, op, mach, &ra, &rb, &imm))
 	ok = (rb == NIOS2_SP_REGNUM);
       if (!ok)
-	return 0;
-
-      /* Then check if it's followed by a return or a tail call.  */
-      op = nios2_fetch_insn (gdbarch, current_pc, &insn);
-      if (op == NULL)
-       return 0;
-      if (nios2_match_jmpr (insn, op, mach, &ra)
-         || nios2_match_jmpi (insn, op, mach, &uimm)
-         || (nios2_match_branch (insn, op, mach, &ra, &rb, &imm, &cond)
-             && cond == branch_none))
-       return 1;
+	break;
     }
+
+  /* No stack adjustments found.  */
+  if (ninsns == 0)
+    return 0;
+
+  /* We found more stack adjustments than we expect GCC to be generating.
+     Since it looks like a stack unwind might be in progress tell GDB to
+     treat it as such.  */
+  if (ninsns == max_insns)
+    return 1;
+
+  /* The next instruction following the stack adjustments must be a
+     return, jump, or unconditional branch.  */
+  if (nios2_match_jmpr (insn, op, mach, &ra)
+      || nios2_match_jmpi (insn, op, mach, &uimm)
+      || (nios2_match_branch (insn, op, mach, &ra, &rb, &imm, &cond)
+	  && cond == branch_none))
+    return 1;
+
   return 0;
 }
 

  parent reply	other threads:[~2014-11-25  0:03 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-08 23:01 [patch, nios2] clean up prologue/epilogue detection code Sandra Loosemore
2014-11-11  3:33 ` Yao Qi
2014-11-24 23:52   ` Sandra Loosemore
2014-11-25  0:03     ` [1/3 patch, nios2] clean up prologue/epilogue detection code, v2 Sandra Loosemore
2014-11-25  2:50       ` Yao Qi
2014-11-25  0:03     ` [3/3 " Sandra Loosemore
2014-11-25  3:00       ` Yao Qi
2014-11-25  0:03     ` Sandra Loosemore [this message]
2014-11-25  3:29       ` [2/3 " Yao Qi

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=5473C736.6050501@codesourcery.com \
    --to=sandra@codesourcery.com \
    --cc=gdb-patches@sourceware.org \
    --cc=yao@codesourcery.com \
    /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