Bug 21470 - gdb producer-sniffs for "clang"
Summary: gdb producer-sniffs for "clang"
Status: NEW
Alias: None
Product: gdb
Classification: Unclassified
Component: symtab (show other bugs)
Version: unknown
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-05-07 17:15 UTC by Tom Tromey
Modified: 2023-01-31 23:34 UTC (History)
1 user (show)

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


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tom Tromey 2017-05-07 17:15:51 UTC
It came up on a rust bug (https://github.com/rust-lang/rust/issues/41252)
that gdb producer-sniffs for "clang" (e.g. in i386-tdep.c).

A simple fix would be to allow "rustc" as well here.
There doesn't seem to be a generic way to sniff for "LLVM back end".

However, I wonder whether it makes sense now to switch to a producer
blacklist rather than a whitelist.
Comment 1 Tom Tromey 2017-05-07 22:50:49 UTC
I'm testing a patch for this, that changes gdb to use a blacklist instead.
I'm not sure if this will be acceptable, since the initial blacklist is
empty.
Comment 2 Tom Tromey 2017-05-08 13:46:12 UTC
Maybe this was wishful thinking since there are some regressions if
I switch to an empty blacklist.  I'm still looking into whether
maybe there are just certain old versions of gcc (4.x perhaps) that
were bad here.
Otherwise, I have a different patch that just adds rustc to the check.
Comment 3 Tom Tromey 2017-05-10 23:53:51 UTC
One specific regression is gdb.ada/ref_param.exp, which fails because
gdb starts believing the DWARF line table.  But I think the line table
is correct and the test might be wrong.

CU: /home/tromey/gdb/binutils-gdb/gdb/testsuite/gdb.ada/ref_param/pck.adb:
File name                            Line number    Starting address
pck.adb                                       18            0x4012be

pck.adb                                       18            0x4012be

pck.adb                                       20            0x4012c6
pck.adb                                       21            0x4012d7
pck.adb                                       23            0x4012e5


With my current patch, the breakpoint is set on line 18.
Without it, it is set at line 20.

Doesn't line 18 seem correct though?
Comment 4 Tom de Vries 2023-01-29 10:23:57 UTC
In commit 154f2735ad4 ("[gdb/testsuite] Fix gdb.ada/access_tagged_param.exp for aarch64") I did:
...
-if ![runto call_me] then {
+if ![runto pck.adb:20] then {
...
to make the test-case robust against problems in prologue analysis.

Of course problems in prologue analysis need to be fixed, but we don't want to be exposed to this in lots of unrelated test-cases, which then fail on some but not on other architectures, hence the fix.

Anyway, let's change this back, and look at what's generated:
...
(gdb) break call_me^M
Breakpoint 1 at 0x401f9a: file /home/vries/gdb_versions/devel/binutils-gdb.git/gdb/testsuite/gdb.ada/ref_param/pck.adb, line 20.^M
...

So, it looks like gdb decided that the first 3 insns are prologue:
...
0000000000401f92 <pck__call_me>:
  401f92:       55                      push   %rbp
  401f93:       48 89 e5                mov    %rsp,%rbp
  401f96:       48 89 7d f8             mov    %rdi,-0x8(%rbp)
  401f9a:       48 8b 45 f8             mov    -0x8(%rbp),%rax
...

Using additional_flags=-fdump-rtl-all and looking at pck.adb.309r.final we can confirm that the first 2 insns are prologue:
...
(note 4 1 32 2 [bb 2] NOTE_INSN_BASIC_BLOCK)
(insn/f 32 4 33 2 (set (mem:DI (pre_dec:DI (reg/f:DI 7 sp)) [0  S8 A8])
        (reg/f:DI 6 bp)) "/home/vries/gdb_versions/devel/binutils-gdb.git/gdb/testsuite/gdb.ada/ref_param/pck.adb":18 57 {*pushdi2_rex64}
     (nil))
(insn/f 33 32 34 2 (set (reg/f:DI 6 bp)
        (reg/f:DI 7 sp)) "/home/vries/gdb_versions/devel/binutils-gdb.git/gdb/testsuite/gdb.ada/ref_param/pck.adb":18 81 {*movdi_internal}
     (nil))
(insn 34 33 35 2 (set (mem/v:BLK (scratch:DI) [0  A8])
        (unspec:BLK [
                (mem/v:BLK (scratch:DI) [0  A8])
            ] UNSPEC_MEMORY_BLOCKAGE)) "/home/vries/gdb_versions/devel/binutils-gdb.git/gdb/testsuite/gdb.ada/ref_param/pck.adb":18 687 {*memory_blockage}
     (nil))
(note 35 34 2 2 NOTE_INSN_PROLOGUE_END)
(insn 2 35 3 2 (set (mem/f/c:DI (plus:DI (reg/f:DI 6 bp)
                (const_int -8 [0xfffffffffffffff8])) [5 d+0 S8 A64])
        (reg:DI 5 di [ d ])) "/home/vries/gdb_versions/devel/binutils-gdb.git/gdb/testsuite/gdb.ada/ref_param/pck.adb":18 81 {*movdi_internal}
     (nil))
(note 3 2 6 2 NOTE_INSN_FUNCTION_BEG)
...
and the third insn is in the twilight zone between prologue end and function begin, and depending on your definition of prologue, can be still considered part of the prologue.

Note that due to compiling at -O0, there's no .debug_loc contribution, so the DW_AT_location expression for the function parameter 'D' is consider only valid after the prologue, and in this sense the third insn is certainly part of the prologue:
...
(gdb) si
pck.call_me (d=...) at pck.adb:18
18         procedure Call_Me (D : in out Data) is
(gdb) p d
$1 = (null)
(gdb) si
0x0000000000401f93      18         procedure Call_Me (D : in out Data) is
(gdb) p d
$2 = (null)
(gdb) si
0x0000000000401f96      18         procedure Call_Me (D : in out Data) is
(gdb) p d
$3 = (null)
(gdb) si
20            if D.One > D.Two then
(gdb) p d
$4 = (one => 1, two => 2, three => 3, four => 4, five => 5, six => 6)
(gdb) 
...

So, say we start to trust the dwarf line table (to be in a certain format that allows us to determine where the prologue ends).

Let's do that using "maint set skip-prologue line" (https://sourceware.org/pipermail/gdb-patches/2022-August/191343.html):
...
clean_restart

gdb_test_no_output "maint set skip-prologue line"

gdb_load ${binfile}
...

Now we have the incorrect:
...
(gdb) break call_me^M
Breakpoint 1 at 0x401f92: file /home/vries/gdb_versions/devel/binutils-gdb.git/gdb/testsuite/gdb.ada/ref_param/pck.adb, line 18.^M
...

The line table as decoded by readelf is:
...
File name         Line number    Starting address    View    Stmt
pck.adb                    18            0x401f92               x
pck.adb                    18            0x401f92       1       x
pck.adb                    20            0x401f9a               x
pck.adb                    21            0x401fab               x
pck.adb                    23            0x401fb9               x
pck.adb                     -            0x401fbd
...
and the summary of that by gdb is:
...
INDEX  LINE   ADDRESS            IS-STMT PROLOGUE-END 
0      18     0x0000000000401f92 Y                    
1      18     0x0000000000401f92 Y                    
2      20     0x0000000000401f9a Y                    
3      21     0x0000000000401fab Y                    
4      23     0x0000000000401fb9 Y                    
5      END    0x0000000000401fbd Y                    
...

This is with gcc 7.5.0.  With gcc 12.2.1, I get a line table without the entry at index 1, and things do work as expected.

My hunch at this point is that this is due to gcc commit c029fcb5680 ("Reset force_source_line in final.c"), which is first present in release 11.1.0.
Comment 5 Tom de Vries 2023-01-29 10:30:12 UTC
(In reply to Tom Tromey from comment #3)
> One specific regression is gdb.ada/ref_param.exp, which fails because
> gdb starts believing the DWARF line table.  But I think the line table
> is correct and the test might be wrong.
> 
> CU: /home/tromey/gdb/binutils-gdb/gdb/testsuite/gdb.ada/ref_param/pck.adb:
> File name                            Line number    Starting address
> pck.adb                                       18            0x4012be
> 
> pck.adb                                       18            0x4012be
> 
> pck.adb                                       20            0x4012c6
> pck.adb                                       21            0x4012d7
> pck.adb                                       23            0x4012e5
> 
> 
> With my current patch, the breakpoint is set on line 18.
> Without it, it is set at line 20.
> 
> Doesn't line 18 seem correct though?

From the analysis in the previous comment, I'd say that line 20 is correct, and line 18 is incorrect.  That is, the line table is incorrect due to a gcc bug.
Comment 6 Tom de Vries 2023-01-31 23:34:37 UTC
(In reply to Tom de Vries from comment #4)
> My hunch at this point is that this is due to gcc commit c029fcb5680 ("Reset
> force_source_line in final.c"), which is first present in release 11.1.0.

Confirmed, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108615 .