Bug 14261 - Break point not set at the 1st executable instruction in case of code compiled with clang on ARM Architecture.
Summary: Break point not set at the 1st executable instruction in case of code compile...
Status: RESOLVED FIXED
Alias: None
Product: gdb
Classification: Unclassified
Component: tdep (show other bugs)
Version: 7.4
: P2 normal
Target Milestone: 7.9
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-06-18 12:12 UTC by Karthik
Modified: 2014-12-12 02:14 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
Assembly files with R(0123) which works fine and Assembly generated by clang which uses R(4-12) as well. (3.18 KB, application/zip)
2012-06-18 12:12 UTC, Karthik
Details
tentative patch (651 bytes, patch)
2014-10-30 14:53 UTC, Christian Bruel
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Karthik 2012-06-18 12:12:33 UTC
Created attachment 6456 [details]
Assembly files with R(0123) which works fine and Assembly generated by clang which uses R(4-12) as well.

Dear Developers,
We are using clang(LLVM) compiled binary on ARM and debugging the same using GDB. When a break point is set at a function, it is set at the 1st line instead of 1st executable instruction.
E.g.
For e.g. In the below function-

int main()          //line 1
{                   //line 2
                    //line 3
  int j =0;         //line 4
  return j;         //line 5
}                   //line 6


When we compile the above code and run gdb with the following commands-

gdb a.out
break main

The break point is set at line 2 instead of line 4.


Upon Debugging we found that in GDB Arm-tdep.c -

static CORE_ADDR
arm_skip_prologue (struct gdbarch *gdbarch, CORE_ADDR pc)


      if ((inst & 0xffffc000) == 0xe5cd0000	/* strb r(0123),[sp,#nn] */
	  || (inst & 0xffffc0f0) == 0xe1cd00b0	/* strh r(0123),[sp,#nn] */
	  || (inst & 0xffffc000) == 0xe58d0000)	/* str  r(0123),[sp,#nn] */
	continue;

We skip the prologue only in case strb uses registers r0,r1,r2 or r3 in prologue code. 

clang (LLVM) generates strb instruction with r4,r5...etc not just r(0123) in prologue code.

In clang case the usage of any other register other than r(0123) in strb/strh/str instruction is resulting in generation of wrong prologue_end resulting in break point not being set at 1st executable instruction.When we modified the strb/strh/str instructions to use r(0123) gdb functionality of break/list/watch functions correctly.

Attached is the assembly and source code of a function compiled using clang in which r12/ r4 is used in strb/strh/str/ldr etc instructions used during parameter assignment 
which results in failure of the above check in arm_skip_prologue resulting in wrong prologue end info.



We would like to know if we are checking against r(0123) for some specific purpose or because some specification mandates this usage ?
Comment 1 Will Newton 2013-10-30 23:18:23 UTC
The prologue scanner appears to be checking for stores of varargs arguments in the function prologue which is why this refers to r0-r3. It is not clear from looking at the disassembly that you attached that these stores are actually part of the prologue, they look like they are initializations of stack variables.

Also your example with main does not correspond to the example in the zip.

Can you confirm that this problem is still valid?
Comment 2 Christian Bruel 2014-10-30 14:53:01 UTC
Created attachment 7865 [details]
tentative patch
Comment 3 Christian Bruel 2014-10-30 14:56:44 UTC
oops, wrong bz# attachment... sorry
Comment 4 Yao Qi 2014-12-10 05:23:22 UTC
This patch <https://sourceware.org/ml/gdb-patches/2014-11/msg00744.html> should fix this bug, but I can't test.
Comment 5 Sourceware Commits 2014-12-12 00:52:04 UTC
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "gdb and binutils".

The branch, master has been updated
       via  21daaaaffcbda47b724858dd99ee2082043ef2da (commit)
       via  f303bc3e6ca29f0413376e38164dc5cdc0893d4b (commit)
      from  65840e31a7a7cd64c81ac47bcb17319536ce3ba2 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=21daaaaffcbda47b724858dd99ee2082043ef2da

commit 21daaaaffcbda47b724858dd99ee2082043ef2da
Author: Yao Qi <yao@codesourcery.com>
Date:   Thu Nov 27 11:37:54 2014 +0800

    Improve arm_skip_prologue by using arm_analyze_prologue
    
    Hi,
    I see many fails in dw2-dir-file-name.exp on arm target when test
    case is compiled with -marm, however, these fails are disappeared when
    test case is compiled with -mthumb.
    
    The difference of pass and fail shown below is that "0x000085d4 in" isn't
    printed out, but test case expects to see it.
    
    -Breakpoint 2, compdir_missing__ldir_missing__file_basename () at tmp-dw2-dir-file-name.c:999^M
    -(gdb) FAIL: gdb.dwarf2/dw2-dir-file-name.exp: compdir_missing__ldir_missing__file_basename: continue to breakpoint: compdir_missing__ldir_missing__file_basename
    +Breakpoint 2, 0x000085d4 in compdir_missing__ldir_missing__file_basename () at tmp-dw2-dir-file-name.c:999^M
    +(gdb) PASS: gdb.dwarf2/dw2-dir-file-name.exp: compdir_missing__ldir_missing__file_basename: continue to breakpoint: compdir_missing__ldir_missing__file_basename
    
    This difference is caused by setting breakpoint at the first instruction
    in the function (actually, the first instruction in prologue, at [1]),
    so that frame_show_address returns false, and print_frame doesn't print the
    address.
    
       0x00008620 <+0>:     push    {r11}           ; (str r11, [sp, #-4]!)  <--[1]
       0x00008624 <+4>:     add     r11, sp, #0
       0x00008628 <+8>:     ldr     r3, [pc, #24]   ; 0x8648 <compdir_missing__ldir_missing__file_basename+40>
       0x0000862c <+12>:    ldr     r3, [r3]
       0x00008630 <+16>:    add     r3, r3, #1
       0x00008634 <+20>:    ldr     r2, [pc, #12]   ; 0x8648 <compdir_missing__ldir_missing__file_basename+40>
    
    Then, it must be the arm_skip_prologue's fault that unable to skip
    instructions in prologue.  At the end of arm_skip_prologue, it matches
    several instructions, such as "str  r(0123),[r11,#-nn]" and
    "str  r(0123),[sp,#nn]", but "push {r11}" isn't handled.
    
    These instruction matching code in arm_skip_prologue, which can be regarded
    as leftover of development for many years, should be merged to
    arm_analyze_prologue and use arm_analyze_prologue in arm_skip_prologue.
    Here is the something like the history of arm_{skip,scan,analyze}_prologue.
    Around 2002, there are arm_skip_prologue and arm_scan_prologue, but code are
    duplicated to some extent.  When match an instruction, both functions should
    be modified, for example in Michael Snyder's patch
    https://sourceware.org/ml/gdb-patches/2002-05/msg00205.html and Michael
    expressed the willingness to merge both into one.  Daniel added code call
    thumb_analyze_prologue in arm_skip_prologue in 2006, but didn't handle its
    counterpart arm_analyze_prologue, which is added in 2010
    <https://sourceware.org/ml/gdb-patches/2010-03/msg00820.html>
    however, the instructions matching at the bottom of arm_skip_prologue wasn't
    cleaned up.  This patch is to merge them into arm_analyze_prologue.
    
    gdb:
    
    2014-12-12  Yao Qi  <yao@codesourcery.com>
    
    	PR tdep/14261
    	* arm-tdep.c (arm_skip_prologue): Remove unused local variable
    	'skip_pc'.  Remove code skipping prologue instructions, use
    	arm_analyze_prologue instead.
    	(arm_analyze_prologue): Stop the scanning for unrecognized
    	instruction when skipping prologue.

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=f303bc3e6ca29f0413376e38164dc5cdc0893d4b

commit f303bc3e6ca29f0413376e38164dc5cdc0893d4b
Author: Yao Qi <yao@codesourcery.com>
Date:   Fri Nov 28 15:19:12 2014 +0800

    Don't scan prologue past epilogue
    
    This patch is to stop prologue analysis past epilogue in for arm mode,
    while we've already had done the same to thumb mode (see
    thumb_instruction_restores_sp).  This is useful to parse functions
    with empty body (epilogue follows prologue).
    
    gdb:
    
    2014-12-12  Yao Qi  <yao@codesourcery.com>
    
    	* arm-tdep.c (arm_instruction_restores_sp): New function.
    	(arm_analyze_prologue): Call arm_instruction_restores_sp.
    	(arm_in_function_epilogue_p): Move code to
    	arm_instruction_restores_sp.

-----------------------------------------------------------------------

Summary of changes:
 gdb/ChangeLog  |   16 +++++++
 gdb/arm-tdep.c |  128 +++++++++++++++++++-------------------------------------
 2 files changed, 59 insertions(+), 85 deletions(-)
Comment 6 Yao Qi 2014-12-12 02:14:34 UTC
Close as it is fixed.