Fix PR30369 regression on aarch64/arm (PR30506)

Message ID 20230607094010.57892-1-luis.machado@arm.com
State New
Headers
Series Fix PR30369 regression on aarch64/arm (PR30506) |

Commit Message

Luis Machado June 7, 2023, 9:40 a.m. UTC
  From: Tom de Vries <tdevries@suse.de>

The gdb.dwarf2/dw2-prologue-end-2.exp test was failing for both AArch64 and
Arm.

As Tom pointed out here (https://inbox.sourceware.org/gdb-patches/6663707c-4297-c2f2-a0bd-f3e84fc62aad@suse.de/),
there are issues with both the prologue skipper for AArch64 and Arm and an
incorrect assumption by the testcase.

This patch fixes both of AArch64's and Arm's prologue skippers to not skip past
the end of a function.  It also incorporates a fix to the testcase so it
doesn't assume the prologue skipper will stop at the first instruction of the
functions/labels.

Regression-tested on aarch64-linux/arm-linux Ubuntu 20.04/22.04 and
x86_64-linux Ubuntu 20.04.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30506

Co-Authored-By: Tom de Vries <tdevries@suse.de>
Co-Authored-By: Luis Machado <luis.machado@arm.com>
---
 gdb/aarch64-tdep.c                            | 10 +++++++--
 gdb/arm-tdep.c                                | 21 ++++++++++++++++---
 .../gdb.dwarf2/dw2-prologue-end-2.exp         | 10 ++++-----
 3 files changed, 31 insertions(+), 10 deletions(-)
  

Comments

Tom de Vries June 7, 2023, 9:55 a.m. UTC | #1
On 6/7/23 11:40, Luis Machado wrote:
> From: Tom de Vries <tdevries@suse.de>
> 
> The gdb.dwarf2/dw2-prologue-end-2.exp test was failing for both AArch64 and
> Arm.
> 
> As Tom pointed out here (https://inbox.sourceware.org/gdb-patches/6663707c-4297-c2f2-a0bd-f3e84fc62aad@suse.de/),
> there are issues with both the prologue skipper for AArch64 and Arm and an
> incorrect assumption by the testcase.
> 
> This patch fixes both of AArch64's and Arm's prologue skippers to not skip past
> the end of a function.  It also incorporates a fix to the testcase so it
> doesn't assume the prologue skipper will stop at the first instruction of the
> functions/labels.
> 
> Regression-tested on aarch64-linux/arm-linux Ubuntu 20.04/22.04 and
> x86_64-linux Ubuntu 20.04.
> 

Hi Luis,

thanks for picking this up.

LGTM.

Thanks,
- Tom

> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30506
> 
> Co-Authored-By: Tom de Vries <tdevries@suse.de>
> Co-Authored-By: Luis Machado <luis.machado@arm.com>
> ---
>   gdb/aarch64-tdep.c                            | 10 +++++++--
>   gdb/arm-tdep.c                                | 21 ++++++++++++++++---
>   .../gdb.dwarf2/dw2-prologue-end-2.exp         | 10 ++++-----
>   3 files changed, 31 insertions(+), 10 deletions(-)
> 
> diff --git a/gdb/aarch64-tdep.c b/gdb/aarch64-tdep.c
> index d8603c45fd3..84a90b63b55 100644
> --- a/gdb/aarch64-tdep.c
> +++ b/gdb/aarch64-tdep.c
> @@ -917,12 +917,15 @@ aarch64_analyze_prologue_test (void)
>   static CORE_ADDR
>   aarch64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
>   {
> -  CORE_ADDR func_addr, limit_pc;
> +  CORE_ADDR func_addr, func_end_addr, limit_pc;
>   
>     /* See if we can determine the end of the prologue via the symbol
>        table.  If so, then return either PC, or the PC after the
>        prologue, whichever is greater.  */
> -  if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
> +  bool func_addr_found
> +    = find_pc_partial_function (pc, NULL, &func_addr, &func_end_addr);
> +
> +  if (func_addr_found)
>       {
>         CORE_ADDR post_prologue_pc
>   	= skip_prologue_using_sal (gdbarch, func_addr);
> @@ -942,6 +945,9 @@ aarch64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
>     if (limit_pc == 0)
>       limit_pc = pc + 128;	/* Magic.  */
>   
> +  limit_pc
> +    = func_end_addr == 0? limit_pc : std::min (limit_pc, func_end_addr - 4);
> +
>     /* Try disassembling prologue.  */
>     return aarch64_analyze_prologue (gdbarch, pc, limit_pc, NULL);
>   }
> diff --git a/gdb/arm-tdep.c b/gdb/arm-tdep.c
> index d5128754f02..a0f59557072 100644
> --- a/gdb/arm-tdep.c
> +++ b/gdb/arm-tdep.c
> @@ -1769,12 +1769,18 @@ arm_skip_stack_protector(CORE_ADDR pc, struct gdbarch *gdbarch)
>   static CORE_ADDR
>   arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
>   {
> -  CORE_ADDR func_addr, limit_pc;
> +  CORE_ADDR func_addr, func_end_addr, limit_pc;
>   
>     /* See if we can determine the end of the prologue via the symbol table.
>        If so, then return either PC, or the PC after the prologue, whichever
>        is greater.  */
> -  if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
> +  bool func_addr_found
> +    = find_pc_partial_function (pc, NULL, &func_addr, &func_end_addr);
> +
> +  /* Whether the function is thumb mode or not.  */
> +  bool func_is_thumb = false;
> +
> +  if (func_addr_found)
>       {
>         CORE_ADDR post_prologue_pc
>   	= skip_prologue_using_sal (gdbarch, func_addr);
> @@ -1811,7 +1817,8 @@ arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
>   	     associate prologue code with the opening brace; so this
>   	     lets us skip the first line if we think it is the opening
>   	     brace.  */
> -	  if (arm_pc_is_thumb (gdbarch, func_addr))
> +	  func_is_thumb = arm_pc_is_thumb (gdbarch, func_addr);
> +	  if (func_is_thumb)
>   	    analyzed_limit = thumb_analyze_prologue (gdbarch, func_addr,
>   						     post_prologue_pc, NULL);
>   	  else
> @@ -1837,6 +1844,14 @@ arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
>     if (limit_pc == 0)
>       limit_pc = pc + 64;          /* Magic.  */
>   
> +  /* Set the correct adjustment based on whether the function is thumb mode or
> +     not.  We use it to get the address of the last instruction in the
> +     function (as opposed to the first address of the next function).  */
> +  CORE_ADDR adjustment = func_is_thumb? 2 : 4;
> +
> +  limit_pc
> +    = func_end_addr == 0? limit_pc : std::min (limit_pc,
> +					       func_end_addr - adjustment);
>   
>     /* Check if this is Thumb code.  */
>     if (arm_pc_is_thumb (gdbarch, pc))
> diff --git a/gdb/testsuite/gdb.dwarf2/dw2-prologue-end-2.exp b/gdb/testsuite/gdb.dwarf2/dw2-prologue-end-2.exp
> index 488f85f9674..c506cfd55cc 100644
> --- a/gdb/testsuite/gdb.dwarf2/dw2-prologue-end-2.exp
> +++ b/gdb/testsuite/gdb.dwarf2/dw2-prologue-end-2.exp
> @@ -95,15 +95,15 @@ if { $break_addr == "" } {
>   
>   # Get the "foo_label" address.
>   
> -set foo_label_addr ""
> -gdb_test_multiple "print /x &foo_label" "" {
> +set bar_label_addr ""
> +gdb_test_multiple "print /x &bar_label" "" {
>       -re -wrap "= ($hex)" {
> -	set foo_label_addr $expect_out(1,string)
> +	set bar_label_addr $expect_out(1,string)
>   	pass $gdb_test_name
>       }
>   }
>   
> -if { $foo_label_addr == "" } {
> +if { $bar_label_addr == "" } {
>       return
>   }
>   
> @@ -115,4 +115,4 @@ gdb_test "print &foo_end == &bar_label" " = 1"
>   # Check that the breakpoint is set at the expected address.  Regression test
>   # for PR30369.
>   
> -gdb_assert { $break_addr == $foo_label_addr }
> +gdb_assert { $break_addr < $bar_label_addr }
  
Luis Machado June 7, 2023, 11:01 a.m. UTC | #2
On 6/7/23 10:55, Tom de Vries wrote:
> On 6/7/23 11:40, Luis Machado wrote:
>> From: Tom de Vries <tdevries@suse.de>
>>
>> The gdb.dwarf2/dw2-prologue-end-2.exp test was failing for both AArch64 and
>> Arm.
>>
>> As Tom pointed out here (https://inbox.sourceware.org/gdb-patches/6663707c-4297-c2f2-a0bd-f3e84fc62aad@suse.de/),
>> there are issues with both the prologue skipper for AArch64 and Arm and an
>> incorrect assumption by the testcase.
>>
>> This patch fixes both of AArch64's and Arm's prologue skippers to not skip past
>> the end of a function.  It also incorporates a fix to the testcase so it
>> doesn't assume the prologue skipper will stop at the first instruction of the
>> functions/labels.
>>
>> Regression-tested on aarch64-linux/arm-linux Ubuntu 20.04/22.04 and
>> x86_64-linux Ubuntu 20.04.
>>
> 
> Hi Luis,
> 
> thanks for picking this up.
> 
> LGTM.
> 

Great. Thanks.

Pushed now.

> Thanks,
> - Tom
> 
>> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30506
>>
>> Co-Authored-By: Tom de Vries <tdevries@suse.de>
>> Co-Authored-By: Luis Machado <luis.machado@arm.com>
>> ---
>>   gdb/aarch64-tdep.c                            | 10 +++++++--
>>   gdb/arm-tdep.c                                | 21 ++++++++++++++++---
>>   .../gdb.dwarf2/dw2-prologue-end-2.exp         | 10 ++++-----
>>   3 files changed, 31 insertions(+), 10 deletions(-)
>>
>> diff --git a/gdb/aarch64-tdep.c b/gdb/aarch64-tdep.c
>> index d8603c45fd3..84a90b63b55 100644
>> --- a/gdb/aarch64-tdep.c
>> +++ b/gdb/aarch64-tdep.c
>> @@ -917,12 +917,15 @@ aarch64_analyze_prologue_test (void)
>>   static CORE_ADDR
>>   aarch64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
>>   {
>> -  CORE_ADDR func_addr, limit_pc;
>> +  CORE_ADDR func_addr, func_end_addr, limit_pc;
>>     /* See if we can determine the end of the prologue via the symbol
>>        table.  If so, then return either PC, or the PC after the
>>        prologue, whichever is greater.  */
>> -  if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
>> +  bool func_addr_found
>> +    = find_pc_partial_function (pc, NULL, &func_addr, &func_end_addr);
>> +
>> +  if (func_addr_found)
>>       {
>>         CORE_ADDR post_prologue_pc
>>       = skip_prologue_using_sal (gdbarch, func_addr);
>> @@ -942,6 +945,9 @@ aarch64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
>>     if (limit_pc == 0)
>>       limit_pc = pc + 128;    /* Magic.  */
>> +  limit_pc
>> +    = func_end_addr == 0? limit_pc : std::min (limit_pc, func_end_addr - 4);
>> +
>>     /* Try disassembling prologue.  */
>>     return aarch64_analyze_prologue (gdbarch, pc, limit_pc, NULL);
>>   }
>> diff --git a/gdb/arm-tdep.c b/gdb/arm-tdep.c
>> index d5128754f02..a0f59557072 100644
>> --- a/gdb/arm-tdep.c
>> +++ b/gdb/arm-tdep.c
>> @@ -1769,12 +1769,18 @@ arm_skip_stack_protector(CORE_ADDR pc, struct gdbarch *gdbarch)
>>   static CORE_ADDR
>>   arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
>>   {
>> -  CORE_ADDR func_addr, limit_pc;
>> +  CORE_ADDR func_addr, func_end_addr, limit_pc;
>>     /* See if we can determine the end of the prologue via the symbol table.
>>        If so, then return either PC, or the PC after the prologue, whichever
>>        is greater.  */
>> -  if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
>> +  bool func_addr_found
>> +    = find_pc_partial_function (pc, NULL, &func_addr, &func_end_addr);
>> +
>> +  /* Whether the function is thumb mode or not.  */
>> +  bool func_is_thumb = false;
>> +
>> +  if (func_addr_found)
>>       {
>>         CORE_ADDR post_prologue_pc
>>       = skip_prologue_using_sal (gdbarch, func_addr);
>> @@ -1811,7 +1817,8 @@ arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
>>            associate prologue code with the opening brace; so this
>>            lets us skip the first line if we think it is the opening
>>            brace.  */
>> -      if (arm_pc_is_thumb (gdbarch, func_addr))
>> +      func_is_thumb = arm_pc_is_thumb (gdbarch, func_addr);
>> +      if (func_is_thumb)
>>           analyzed_limit = thumb_analyze_prologue (gdbarch, func_addr,
>>                                post_prologue_pc, NULL);
>>         else
>> @@ -1837,6 +1844,14 @@ arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
>>     if (limit_pc == 0)
>>       limit_pc = pc + 64;          /* Magic.  */
>> +  /* Set the correct adjustment based on whether the function is thumb mode or
>> +     not.  We use it to get the address of the last instruction in the
>> +     function (as opposed to the first address of the next function).  */
>> +  CORE_ADDR adjustment = func_is_thumb? 2 : 4;
>> +
>> +  limit_pc
>> +    = func_end_addr == 0? limit_pc : std::min (limit_pc,
>> +                           func_end_addr - adjustment);
>>     /* Check if this is Thumb code.  */
>>     if (arm_pc_is_thumb (gdbarch, pc))
>> diff --git a/gdb/testsuite/gdb.dwarf2/dw2-prologue-end-2.exp b/gdb/testsuite/gdb.dwarf2/dw2-prologue-end-2.exp
>> index 488f85f9674..c506cfd55cc 100644
>> --- a/gdb/testsuite/gdb.dwarf2/dw2-prologue-end-2.exp
>> +++ b/gdb/testsuite/gdb.dwarf2/dw2-prologue-end-2.exp
>> @@ -95,15 +95,15 @@ if { $break_addr == "" } {
>>   # Get the "foo_label" address.
>> -set foo_label_addr ""
>> -gdb_test_multiple "print /x &foo_label" "" {
>> +set bar_label_addr ""
>> +gdb_test_multiple "print /x &bar_label" "" {
>>       -re -wrap "= ($hex)" {
>> -    set foo_label_addr $expect_out(1,string)
>> +    set bar_label_addr $expect_out(1,string)
>>       pass $gdb_test_name
>>       }
>>   }
>> -if { $foo_label_addr == "" } {
>> +if { $bar_label_addr == "" } {
>>       return
>>   }
>> @@ -115,4 +115,4 @@ gdb_test "print &foo_end == &bar_label" " = 1"
>>   # Check that the breakpoint is set at the expected address.  Regression test
>>   # for PR30369.
>> -gdb_assert { $break_addr == $foo_label_addr }
>> +gdb_assert { $break_addr < $bar_label_addr }
>
  
Tom Tromey June 9, 2023, 2:25 p.m. UTC | #3
>>>>> "Luis" == Luis Machado via Gdb-patches <gdb-patches@sourceware.org> writes:

Luis> +    = func_end_addr == 0? limit_pc : std::min (limit_pc,
Luis> +					       func_end_addr - adjustment);

Missing space before the "?".  There's another instance as well.

Tom
  
Luis Machado June 9, 2023, 2:44 p.m. UTC | #4
On 6/9/23 15:25, Tom Tromey wrote:
>>>>>> "Luis" == Luis Machado via Gdb-patches <gdb-patches@sourceware.org> writes:
> 
> Luis> +    = func_end_addr == 0? limit_pc : std::min (limit_pc,
> Luis> +					       func_end_addr - adjustment);
> 
> Missing space before the "?".  There's another instance as well.
> 
> Tom

Oops. How about the following to address this and other such issues I found?

diff --git a/gdb/aarch64-tdep.c b/gdb/aarch64-tdep.c
index 84a90b63b55..21dd6c7a38c 100644
--- a/gdb/aarch64-tdep.c
+++ b/gdb/aarch64-tdep.c
@@ -946,7 +946,7 @@ aarch64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
     limit_pc = pc + 128;	/* Magic.  */
 
   limit_pc
-    = func_end_addr == 0? limit_pc : std::min (limit_pc, func_end_addr - 4);
+    = func_end_addr == 0 ? limit_pc : std::min (limit_pc, func_end_addr - 4);
 
   /* Try disassembling prologue.  */
   return aarch64_analyze_prologue (gdbarch, pc, limit_pc, NULL);
diff --git a/gdb/arm-tdep.c b/gdb/arm-tdep.c
index a0f59557072..6d4c9d04cbc 100644
--- a/gdb/arm-tdep.c
+++ b/gdb/arm-tdep.c
@@ -1504,7 +1504,7 @@ thumb_analyze_prologue (struct gdbarch *gdbarch,
 	      arm_debug_printf ("Found pacbti instruction at %s",
 				paddress (gdbarch, start));
 	      arm_debug_printf ("RA is %s",
-				*ra_signed_state? "signed" : "not signed");
+				*ra_signed_state ? "signed" : "not signed");
 	      cache->ra_signed_state = ra_signed_state;
 	    }
 
@@ -1847,11 +1847,11 @@ arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
   /* Set the correct adjustment based on whether the function is thumb mode or
      not.  We use it to get the address of the last instruction in the
      function (as opposed to the first address of the next function).  */
-  CORE_ADDR adjustment = func_is_thumb? 2 : 4;
+  CORE_ADDR adjustment = func_is_thumb ? 2 : 4;
 
   limit_pc
-    = func_end_addr == 0? limit_pc : std::min (limit_pc,
-					       func_end_addr - adjustment);
+    = func_end_addr == 0 ? limit_pc : std::min (limit_pc,
+						func_end_addr - adjustment);
 
   /* Check if this is Thumb code.  */
   if (arm_pc_is_thumb (gdbarch, pc))
@@ -8625,7 +8625,7 @@ arm_displaced_init_closure (struct gdbarch *gdbarch, CORE_ADDR from,
   arm_gdbarch_tdep *tdep = gdbarch_tdep<arm_gdbarch_tdep> (gdbarch);
   unsigned int i, len, offset;
   enum bfd_endian byte_order_for_code = gdbarch_byte_order_for_code (gdbarch);
-  int size = dsc->is_thumb? 2 : 4;
+  int size = dsc->is_thumb ? 2 : 4;
   const gdb_byte *bkp_insn;
 
   offset = 0;
@@ -10935,13 +10935,13 @@ arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
   gdb_printf (file, _("arm_dump_tdep: vfp_register_count = %i\n"),
 	      (int) tdep->vfp_register_count);
   gdb_printf (file, _("arm_dump_tdep: have_s_pseudos = %s\n"),
-	      tdep->have_s_pseudos? "true" : "false");
+	      tdep->have_s_pseudos ? "true" : "false");
   gdb_printf (file, _("arm_dump_tdep: s_pseudo_base = %i\n"),
 	      (int) tdep->s_pseudo_base);
   gdb_printf (file, _("arm_dump_tdep: s_pseudo_count = %i\n"),
 	      (int) tdep->s_pseudo_count);
   gdb_printf (file, _("arm_dump_tdep: have_q_pseudos = %s\n"),
-	      tdep->have_q_pseudos? "true" : "false");
+	      tdep->have_q_pseudos ? "true" : "false");
   gdb_printf (file, _("arm_dump_tdep: q_pseudo_base = %i\n"),
 	      (int) tdep->q_pseudo_base);
   gdb_printf (file, _("arm_dump_tdep: q_pseudo_count = %i\n"),
@@ -10949,7 +10949,7 @@ arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
   gdb_printf (file, _("arm_dump_tdep: have_neon = %i\n"),
 	      (int) tdep->have_neon);
   gdb_printf (file, _("arm_dump_tdep: have_mve = %s\n"),
-	      tdep->have_mve? "yes" : "no");
+	      tdep->have_mve ? "yes" : "no");
   gdb_printf (file, _("arm_dump_tdep: mve_vpr_regnum = %i\n"),
 	      tdep->mve_vpr_regnum);
   gdb_printf (file, _("arm_dump_tdep: mve_pseudo_base = %i\n"),
@@ -10971,13 +10971,13 @@ arm_dump_tdep (struct gdbarch *gdbarch, struct ui_file *file)
   gdb_printf (file, _("arm_dump_tdep: Lowest pc = 0x%lx\n"),
 	      (unsigned long) tdep->lowest_pc);
   gdb_printf (file, _("arm_dump_tdep: have_pacbti = %s\n"),
-	      tdep->have_pacbti? "yes" : "no");
+	      tdep->have_pacbti ? "yes" : "no");
   gdb_printf (file, _("arm_dump_tdep: pacbti_pseudo_base = %i\n"),
 	      tdep->pacbti_pseudo_base);
   gdb_printf (file, _("arm_dump_tdep: pacbti_pseudo_count = %i\n"),
 	      tdep->pacbti_pseudo_count);
   gdb_printf (file, _("arm_dump_tdep: is_m = %s\n"),
-	      tdep->is_m? "yes" : "no");
+	      tdep->is_m ? "yes" : "no");
 }
 
 #if GDB_SELF_TEST
@@ -12328,7 +12328,7 @@ arm_record_ld_st_reg_offset (arm_insn_decode_record *arm_insn_r)
 	      break;
 
 	      case 1:
-		offset_12 = (!shift_imm)?0:u_regval[0] >> shift_imm;
+		offset_12 = (!shift_imm) ? 0 : u_regval[0] >> shift_imm;
 	      break;
 
 	      case 2:
  
Tom Tromey June 9, 2023, 3:26 p.m. UTC | #5
>>>>> "Luis" == Luis Machado via Gdb-patches <gdb-patches@sourceware.org> writes:

Luis> Oops. How about the following to address this and other such issues I found?

Looks good, thank you.

Reviewed-By: Tom Tromey <tom@tromey.com>

Tom
  
Luis Machado June 9, 2023, 3:28 p.m. UTC | #6
On 6/9/23 16:26, Tom Tromey wrote:
>>>>>> "Luis" == Luis Machado via Gdb-patches <gdb-patches@sourceware.org> writes:
> 
> Luis> Oops. How about the following to address this and other such issues I found?
> 
> Looks good, thank you.
> 
> Reviewed-By: Tom Tromey <tom@tromey.com>
> 
> Tom

Thanks. Pushed now.
  

Patch

diff --git a/gdb/aarch64-tdep.c b/gdb/aarch64-tdep.c
index d8603c45fd3..84a90b63b55 100644
--- a/gdb/aarch64-tdep.c
+++ b/gdb/aarch64-tdep.c
@@ -917,12 +917,15 @@  aarch64_analyze_prologue_test (void)
 static CORE_ADDR
 aarch64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
 {
-  CORE_ADDR func_addr, limit_pc;
+  CORE_ADDR func_addr, func_end_addr, limit_pc;
 
   /* See if we can determine the end of the prologue via the symbol
      table.  If so, then return either PC, or the PC after the
      prologue, whichever is greater.  */
-  if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
+  bool func_addr_found
+    = find_pc_partial_function (pc, NULL, &func_addr, &func_end_addr);
+
+  if (func_addr_found)
     {
       CORE_ADDR post_prologue_pc
 	= skip_prologue_using_sal (gdbarch, func_addr);
@@ -942,6 +945,9 @@  aarch64_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
   if (limit_pc == 0)
     limit_pc = pc + 128;	/* Magic.  */
 
+  limit_pc
+    = func_end_addr == 0? limit_pc : std::min (limit_pc, func_end_addr - 4);
+
   /* Try disassembling prologue.  */
   return aarch64_analyze_prologue (gdbarch, pc, limit_pc, NULL);
 }
diff --git a/gdb/arm-tdep.c b/gdb/arm-tdep.c
index d5128754f02..a0f59557072 100644
--- a/gdb/arm-tdep.c
+++ b/gdb/arm-tdep.c
@@ -1769,12 +1769,18 @@  arm_skip_stack_protector(CORE_ADDR pc, struct gdbarch *gdbarch)
 static CORE_ADDR
 arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
 {
-  CORE_ADDR func_addr, limit_pc;
+  CORE_ADDR func_addr, func_end_addr, limit_pc;
 
   /* See if we can determine the end of the prologue via the symbol table.
      If so, then return either PC, or the PC after the prologue, whichever
      is greater.  */
-  if (find_pc_partial_function (pc, NULL, &func_addr, NULL))
+  bool func_addr_found
+    = find_pc_partial_function (pc, NULL, &func_addr, &func_end_addr);
+
+  /* Whether the function is thumb mode or not.  */
+  bool func_is_thumb = false;
+
+  if (func_addr_found)
     {
       CORE_ADDR post_prologue_pc
 	= skip_prologue_using_sal (gdbarch, func_addr);
@@ -1811,7 +1817,8 @@  arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
 	     associate prologue code with the opening brace; so this
 	     lets us skip the first line if we think it is the opening
 	     brace.  */
-	  if (arm_pc_is_thumb (gdbarch, func_addr))
+	  func_is_thumb = arm_pc_is_thumb (gdbarch, func_addr);
+	  if (func_is_thumb)
 	    analyzed_limit = thumb_analyze_prologue (gdbarch, func_addr,
 						     post_prologue_pc, NULL);
 	  else
@@ -1837,6 +1844,14 @@  arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)
   if (limit_pc == 0)
     limit_pc = pc + 64;          /* Magic.  */
 
+  /* Set the correct adjustment based on whether the function is thumb mode or
+     not.  We use it to get the address of the last instruction in the
+     function (as opposed to the first address of the next function).  */
+  CORE_ADDR adjustment = func_is_thumb? 2 : 4;
+
+  limit_pc
+    = func_end_addr == 0? limit_pc : std::min (limit_pc,
+					       func_end_addr - adjustment);
 
   /* Check if this is Thumb code.  */
   if (arm_pc_is_thumb (gdbarch, pc))
diff --git a/gdb/testsuite/gdb.dwarf2/dw2-prologue-end-2.exp b/gdb/testsuite/gdb.dwarf2/dw2-prologue-end-2.exp
index 488f85f9674..c506cfd55cc 100644
--- a/gdb/testsuite/gdb.dwarf2/dw2-prologue-end-2.exp
+++ b/gdb/testsuite/gdb.dwarf2/dw2-prologue-end-2.exp
@@ -95,15 +95,15 @@  if { $break_addr == "" } {
 
 # Get the "foo_label" address.
 
-set foo_label_addr ""
-gdb_test_multiple "print /x &foo_label" "" {
+set bar_label_addr ""
+gdb_test_multiple "print /x &bar_label" "" {
     -re -wrap "= ($hex)" {
-	set foo_label_addr $expect_out(1,string)
+	set bar_label_addr $expect_out(1,string)
 	pass $gdb_test_name
     }
 }
 
-if { $foo_label_addr == "" } {
+if { $bar_label_addr == "" } {
     return
 }
 
@@ -115,4 +115,4 @@  gdb_test "print &foo_end == &bar_label" " = 1"
 # Check that the breakpoint is set at the expected address.  Regression test
 # for PR30369.
 
-gdb_assert { $break_addr == $foo_label_addr }
+gdb_assert { $break_addr < $bar_label_addr }