Bug 25807 - [readnow] FAIL: gdb.threads/tls.exp: print a_thread_local
Summary: [readnow] FAIL: gdb.threads/tls.exp: print a_thread_local
Status: NEW
Alias: None
Product: gdb
Classification: Unclassified
Component: symtab (show other bugs)
Version: HEAD
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-04-09 11:52 UTC by Tom de Vries
Modified: 2024-01-26 13:59 UTC (History)
1 user (show)

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


Attachments
Tentative patch (1.13 KB, patch)
2020-04-09 11:57 UTC, Tom de Vries
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Tom de Vries 2020-04-09 11:52:50 UTC
When running test-case gdb.threads/tls.exp with target board -readnow, we have:
...
(gdb) print a_thread_local^M
Cannot find thread-local storage for process 0, executable file tls/tls:^M
Cannot find thread-local variables on this target^M
(gdb) FAIL: gdb.threads/tls.exp: print a_thread_local
...
while with native we have:
...
(gdb) print a_thread_local^M
Cannot read `a_thread_local' without registers^M
(gdb) PASS: gdb.threads/tls.exp: print a_thread_local
...

The difference in behaviour can be explained as follows.  Without -readnow, we have two a_thread_locals, the def and the decl:
...
$ gdb -batch outputs/gdb.threads/tls/tls \
    -ex "maint expand-symtabs" \
    -ex "print a_thread_local" \
    -ex "maint print symbols" \
    | grep "a_thread_local;"
Cannot read `a_thread_local' without registers
 int a_thread_local; computed at runtime
 int a_thread_local; unresolved
...
and with -readnow, we have the opposite order:
...
$ gdb -readnow -batch outputs/gdb.threads/tls/tls  \
    -ex "maint expand-symtabs" \
    -ex "print a_thread_local" \
    -ex "maint print symbols" \
    | grep "a_thread_local;"
Cannot find thread-local storage for process 0, executable file tls/tls:
Cannot find thread-local variables on this target
 int a_thread_local; unresolved
 int a_thread_local; computed at runtime
...

[ With the tentative fix for PR25764, submitted here ( https://sourceware.org/pipermail/gdb-patches/2020-April/167458.html ), we seem to stabilize that search order and make the test fail without -readnow as well. ]

Anyway, this seems to be a variant of PR24985 - "Cannot print value of global variable because decl in one CU shadows def in other CU", in the sense that we probably prefer to find the "computed at runtime" symbol rather than the "unresolved" symbol.

But, the tentative patch at PR24985 comment 3 doesn't fix this, because that patch exploits the fact that the decl has an incomplete type, while in this case the type is complete.

So, I'm filing this as a separate PR.
Comment 1 Tom de Vries 2020-04-09 11:57:08 UTC
Created attachment 12449 [details]
Tentative patch
Comment 2 Tom de Vries 2020-04-09 14:31:06 UTC
Patch submitted: https://sourceware.org/pipermail/gdb-patches/2020-April/167489.html
Comment 3 Sourceware Commits 2020-04-23 13:42:52 UTC
The master branch has been updated by Tom de Vries <vries@sourceware.org>:

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

commit de82891ce5b6d2c8109f512cd0732325f4cd0557
Author: Tom de Vries <tdevries@suse.de>
Date:   Thu Apr 23 15:42:47 2020 +0200

    [gdb/symtab] Prefer def over decl (inter-CU case)
    
    When running test-case gdb.threads/tls.exp with target board -readnow, we
    have:
    ...
    (gdb) print a_thread_local^M
    Cannot find thread-local storage for process 0, executable file tls/tls:^M
    Cannot find thread-local variables on this target^M
    (gdb) FAIL: gdb.threads/tls.exp: print a_thread_local
    ...
    while with native we have:
    ...
    (gdb) print a_thread_local^M
    Cannot read `a_thread_local' without registers^M
    (gdb) PASS: gdb.threads/tls.exp: print a_thread_local
    ...
    
    The difference in behaviour can be explained as follows.  Without -readnow, we
    have two a_thread_locals, the def and the decl, each in a different CU:
    ...
    $ gdb -batch outputs/gdb.threads/tls/tls \
        -ex "maint expand-symtabs" \
        -ex "print a_thread_local" \
        -ex "maint print symbols" \
        | grep "a_thread_local;"
    Cannot read `a_thread_local' without registers
     int a_thread_local; computed at runtime
     int a_thread_local; unresolved
    ...
    and with -readnow, we have the opposite order:
    ...
    $ gdb -readnow -batch outputs/gdb.threads/tls/tls  \
        -ex "maint expand-symtabs" \
        -ex "print a_thread_local" \
        -ex "maint print symbols" \
        | grep "a_thread_local;"
    Cannot find thread-local storage for process 0, executable file tls/tls:
    Cannot find thread-local variables on this target
     int a_thread_local; unresolved
     int a_thread_local; computed at runtime
    ...
    
    Fix the FAIL by preferring the def over the decl (something we already do
    intra-CU since the fix for PR24971, commit 93e55f0a03 "[gdb/symtab] Prefer var
    def over decl").
    
    Build and reg-tested on x86_64-linux.
    
    gdb/ChangeLog:
    
    2020-04-23  Tom de Vries  <tdevries@suse.de>
    
            PR symtab/25807
            * block.c (best_symbol, better_symbol): Promote to external.
            * block.h (best_symbol, better_symbol): Declare.
            * symtab.c (lookup_symbol_in_objfile_symtabs): Prefer def over
            decl.
    
    gdb/testsuite/ChangeLog:
    
    2020-04-23  Tom de Vries  <tdevries@suse.de>
    
            * gdb.base/decl-before-def-decl.c: New test.
            * gdb.base/decl-before-def-def.c: New test.
            * gdb.base/decl-before-def.exp: New file.
Comment 4 Hannes Domani 2024-01-26 13:59:46 UTC
(In reply to Sourceware Commits from comment #3)
> The master branch has been updated by Tom de Vries <vries@sourceware.org>:
> 
> https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;
> h=de82891ce5b6d2c8109f512cd0732325f4cd0557
> 
> commit de82891ce5b6d2c8109f512cd0732325f4cd0557
> Author: Tom de Vries <tdevries@suse.de>
> Date:   Thu Apr 23 15:42:47 2020 +0200
> 
>     [gdb/symtab] Prefer def over decl (inter-CU case)

Can this be closed?