Bug 13448

Summary: char[] thread local variables displayed incorrectly
Product: gdb Reporter: Ben Cohen <ben.cohen>
Component: threadsAssignee: Jan Kratochvil <jan>
Status: RESOLVED FIXED    
Severity: normal CC: ben.cohen, jan, tromey
Priority: P2    
Version: HEAD   
Target Milestone: ---   
Host: Target:
Build: Last reconfirmed:
Attachments: Example of incorrect "print myname" and then correct after bt
Fix+testcase and a testcase for the patch which regressed it.

Description Ben Cohen 2011-11-29 12:29:21 UTC
Created attachment 6079 [details]
Example of incorrect "print myname" and then correct after bt

Thread-local variables that are of type char[] aren't printed correctly for some threads.

Compile and run the following file and use gdb to attach or debug a core dump.  Then "print myname" or "thread apply all print myname" prints some unknown number rather than that variable.  See the attached file for an example of the output.

If you subsequently do for example a backtrace then the same print command will work after that.

If you change the program so that either of the threads does "while (1) {}" rather than sleeping then "t a a p myname" works correctly from the start.  Perhaps it is something to do with the threads being in a system call?

If you use for example a char, int or int[] thread local variable this bug doesn't happen.

This is a bug in 7.3.1 but not in 7.1 or 7.2 (I haven't tried 7.3).


/* thread_local_var.c: Compile with:
 *     gcc -o thread_local_var thread_local_var.c -pthread -ggdb
 */
#include <string.h>
#include <pthread.h>

__thread char myname[128] = "(unnamed thread)";

void *th(void* x)
{
  strcpy(myname, "a new thread");
  while(1) sleep(1);
}

main()
{
  pthread_t t;

  strcpy(myname, "this is the main thread");
  pthread_create(&t, NULL, th, NULL);
  while(1) sleep(1);
}
Comment 1 Ben Cohen 2011-11-29 14:49:51 UTC
I've just tried the gdb-7.3.50.20111129 snapshot and that still has the bug.
Comment 2 Ben Cohen 2011-11-29 14:51:10 UTC
I've just tried the gdb-7.3.50.20111129 snapshot and that still has the bug.
Comment 3 Tom Tromey 2011-11-29 18:13:24 UTC
I am not sure what changed since 7.2, but the bug is that
decode_locdesc (ugh) returns 0 for "myname", because it has
a non-trivial DW_AT_location.  This leaves it out of the psymtab,
so "print myname" finds the minimal symbol, which doesn't have a type.
A "bt" causes full symbols to be read, making this work again.
Comment 4 Jan Kratochvil 2011-11-29 23:14:14 UTC
9602d20e52e1a2291bd9edbdf98e4d82995a385e is the first bad commit
commit 9602d20e52e1a2291bd9edbdf98e4d82995a385e
Author: Jerome Guitton <guitton@adacore.com>
Date:   Mon Jul 26 09:29:59 2010 +0000

    gdb/
    	* dwarf2read.c (add_partial_symbol): Do not add a global variable if
    	its adress is null. Add comment to explain why.
    	(new_symbol): Ditto.
Comment 5 Jan Kratochvil 2011-12-01 02:34:05 UTC
Created attachment 6081 [details]
Fix+testcase and a testcase for the patch which regressed it.

Not regression tested yet.
Comment 6 Jan Kratochvil 2011-12-02 01:08:51 UTC
[patch] Regressed TLS variables type (PR 13448)
http://sourceware.org/ml/gdb-patches/2011-12/msg00030.html
Comment 7 Sourceware Commits 2011-12-02 17:01:25 UTC
CVSROOT:	/cvs/src
Module name:	src
Changes by:	jkratoch@sourceware.org	2011-12-02 17:01:21

Modified files:
	gdb            : ChangeLog dwarf2read.c 
	gdb/testsuite  : ChangeLog 
Added files:
	gdb/testsuite/gdb.dwarf2: dw2-var-zero-addr.S 
	                          dw2-var-zero-addr.exp 
	gdb/testsuite/gdb.threads: tls-var-main.c tls-var.c tls-var.exp 

Log message:
	gdb/
	PR threads/13448
	* dwarf2read.c (decode_locdesc): Handle DW_OP_const8u.
	For DW_OP_GNU_push_tls_address increment the value, new comment for it.
	
	gdb/testsuite/
	PR threads/13448
	* gdb.dwarf2/dw2-var-zero-addr.S: New file.
	* gdb.dwarf2/dw2-var-zero-addr.exp: New file.
	* gdb.threads/tls-var-main.c: New file.
	* gdb.threads/tls-var.c: New file.
	* gdb.threads/tls-var.exp: New file.

Patches:
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/ChangeLog.diff?cvsroot=src&r1=1.13567&r2=1.13568
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/dwarf2read.c.diff?cvsroot=src&r1=1.583&r2=1.584
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/testsuite/ChangeLog.diff?cvsroot=src&r1=1.2956&r2=1.2957
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/testsuite/gdb.dwarf2/dw2-var-zero-addr.S.diff?cvsroot=src&r1=NONE&r2=1.1
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/testsuite/gdb.dwarf2/dw2-var-zero-addr.exp.diff?cvsroot=src&r1=NONE&r2=1.1
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/testsuite/gdb.threads/tls-var-main.c.diff?cvsroot=src&r1=NONE&r2=1.1
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/testsuite/gdb.threads/tls-var.c.diff?cvsroot=src&r1=NONE&r2=1.1
http://sourceware.org/cgi-bin/cvsweb.cgi/src/gdb/testsuite/gdb.threads/tls-var.exp.diff?cvsroot=src&r1=NONE&r2=1.1
Comment 8 Jan Kratochvil 2011-12-02 17:02:10 UTC
Checked in.