From patchwork Wed May 21 12:17:34 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Kratochvil X-Patchwork-Id: 1049 Return-Path: X-Original-To: siddhesh@wilcox.dreamhost.com Delivered-To: siddhesh@wilcox.dreamhost.com Received: from homiemail-mx23.g.dreamhost.com (mx2.sub5.homie.mail.dreamhost.com [208.113.200.128]) by wilcox.dreamhost.com (Postfix) with ESMTP id 73DDE3600B4 for ; Wed, 21 May 2014 05:17:46 -0700 (PDT) Received: by homiemail-mx23.g.dreamhost.com (Postfix, from userid 14314964) id 2611F63CAC3BB; Wed, 21 May 2014 05:17:46 -0700 (PDT) X-Original-To: gdb@patchwork.siddhesh.in Delivered-To: x14314964@homiemail-mx23.g.dreamhost.com Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by homiemail-mx23.g.dreamhost.com (Postfix) with ESMTPS id F1BC063D4338C for ; Wed, 21 May 2014 05:17:45 -0700 (PDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:date:from:to:cc:subject:message-id:references :mime-version:content-type:in-reply-to; q=dns; s=default; b=K1le rc4Y0zhautD2OTtHExr39gbhOB1qvcNyygqsDos3JaDg3MEVozsbVI2CqU39wCjL DGj896ogw9T+djCABVLiVVMrrvl/tWS+LAlBFePsCXH/SJsW3DM3ZAOqi2mJPOjh CwXe0kaQrzJQOQRmsVQ7RV45e+KvKkvPDSEeCUk= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:date:from:to:cc:subject:message-id:references :mime-version:content-type:in-reply-to; s=default; bh=52pPVq+NZU k3D+h1ZMUsD61+xR0=; b=bbj0yZuBaOa+KdDUxUrXmtLIIGzwH/F/4PKLvuKUBB AAvjlMQPUJ90XojxxyWWNLIJYKkLOdSwzuanknrV6csUYm6y9CN4CJhMO7BAIBAp uRJTiiRI4UMmEWT2WjMxwGjTTP22Kmc3eLClQnrth1EBLe79ELTTGA1O7sQo/I8g M= Received: (qmail 22490 invoked by alias); 21 May 2014 12:17:44 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 22477 invoked by uid 89); 21 May 2014 12:17:43 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.8 required=5.0 tests=AWL, BAYES_00, KAM_STOCKTIP, RP_MATCHES_RCVD, SPF_HELO_PASS, SPF_PASS autolearn=no version=3.3.2 X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 21 May 2014 12:17:42 +0000 Received: from int-mx02.intmail.prod.int.phx2.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s4LCHdh5009200 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 21 May 2014 08:17:40 -0400 Received: from host2.jankratochvil.net (ovpn-116-107.ams2.redhat.com [10.36.116.107]) by int-mx02.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id s4LCHYeL029458 (version=TLSv1/SSLv3 cipher=AES128-SHA bits=128 verify=NO); Wed, 21 May 2014 08:17:37 -0400 Date: Wed, 21 May 2014 14:17:34 +0200 From: Jan Kratochvil To: Tom Tromey Cc: gdb-patches@sourceware.org Subject: Re: [patch] Fix TLS access for -static -pthread Message-ID: <20140521121734.GA14288@host2.jankratochvil.net> References: <20140410115204.GB16411@host2.jankratochvil.net> <87d2f8trjo.fsf@fleche.redhat.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <87d2f8trjo.fsf@fleche.redhat.com> User-Agent: Mutt/1.5.23 (2014-03-12) X-IsSubscribed: yes X-DH-Original-To: gdb@patchwork.siddhesh.in On Tue, 20 May 2014 17:10:03 +0200, Tom Tromey wrote: > This bit of code (and another like it later on) should have a longer > comment, probably at least including the link above, but also some text > about how this is a hack but why it is ok. I have put there: + /* This code path handles the case of -static -pthread executables: + https://sourceware.org/ml/libc-help/2014-03/msg00024.html + For older GNU libc r_debug.r_map is NULL. For GNU libc after + PR libc/16831 due to GDB PR threads/16954 LOAD_MODULE is also NULL. + The constant number 1 depends on GNU __libc_setup_tls + initialization of l_tls_modid to 1. */ > Otherwise this is ok. Reposting with the comment update. Jan gdb/ 2014-05-21 Jan Kratochvil Fix TLS access for -static -pthread. * linux-thread-db.c (struct thread_db_info): Add td_thr_tlsbase_p. (try_thread_db_load_1): Initialize it. (thread_db_get_thread_local_address): Call it if LM is zero. * target.c (target_translate_tls_address): Remove LM_ADDR zero check. * target.h (struct target_ops) (to_get_thread_local_address): Add load_module_addr comment. gdb/gdbserver/ 2014-05-21 Jan Kratochvil Fix TLS access for -static -pthread. * gdbserver/thread-db.c (struct thread_db): Add td_thr_tlsbase_p. (thread_db_get_tls_address): Call it if LOAD_MODULE is zero. (thread_db_load_search, try_thread_db_load_1): Initialize it. gdb/testsuite/ 2014-04-10 Jan Kratochvil Fix TLS access for -static -pthread. * gdb.threads/staticthreads.c (tlsvar): New. (thread_function, main): Initialize it. * gdb.threads/staticthreads.exp: Try gdb_compile_pthreads for $have_tls. Add clean_restart. <$have_tls != "">: Check TLSVAR. diff --git a/gdb/gdbserver/thread-db.c b/gdb/gdbserver/thread-db.c index ae0d191..3ea0cc3 100644 --- a/gdb/gdbserver/thread-db.c +++ b/gdb/gdbserver/thread-db.c @@ -88,6 +88,9 @@ struct thread_db td_err_e (*td_thr_tls_get_addr_p) (const td_thrhandle_t *th, psaddr_t map_address, size_t offset, psaddr_t *address); + td_err_e (*td_thr_tlsbase_p) (const td_thrhandle_t *th, + unsigned long int modid, + psaddr_t *base); const char ** (*td_symbol_list_p) (void); }; @@ -503,7 +506,10 @@ thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset, if (thread_db == NULL || !thread_db->all_symbols_looked_up) return TD_ERR; - if (thread_db->td_thr_tls_get_addr_p == NULL) + /* If td_thr_tls_get_addr is missing rather do not expect td_thr_tlsbase + could work. */ + if (thread_db->td_thr_tls_get_addr_p == NULL + || (load_module == 0 && thread_db->td_thr_tlsbase_p == NULL)) return -1; lwp = get_thread_lwp (thread); @@ -514,12 +520,28 @@ thread_db_get_tls_address (struct thread_info *thread, CORE_ADDR offset, saved_inferior = current_inferior; current_inferior = thread; - /* Note the cast through uintptr_t: this interface only works if - a target address fits in a psaddr_t, which is a host pointer. - So a 32-bit debugger can not access 64-bit TLS through this. */ - err = thread_db->td_thr_tls_get_addr_p (&lwp->th, - (psaddr_t) (uintptr_t) load_module, - offset, &addr); + + if (load_module != 0) + { + /* Note the cast through uintptr_t: this interface only works if + a target address fits in a psaddr_t, which is a host pointer. + So a 32-bit debugger can not access 64-bit TLS through this. */ + err = thread_db->td_thr_tls_get_addr_p (&lwp->th, + (psaddr_t) (uintptr_t) load_module, + offset, &addr); + } + else + { + /* This code path handles the case of -static -pthread executables: + https://sourceware.org/ml/libc-help/2014-03/msg00024.html + For older GNU libc r_debug.r_map is NULL. For GNU libc after + PR libc/16831 due to GDB PR threads/16954 LOAD_MODULE is also NULL. + The constant number 1 depends on GNU __libc_setup_tls + initialization of l_tls_modid to 1. */ + err = thread_db->td_thr_tlsbase_p (&lwp->th, 1, &addr); + addr = (char *) addr + offset; + } + current_inferior = saved_inferior; if (err == TD_OK) { @@ -571,6 +593,7 @@ thread_db_load_search (void) tdb->td_ta_set_event_p = &td_ta_set_event; tdb->td_ta_event_getmsg_p = &td_ta_event_getmsg; tdb->td_thr_tls_get_addr_p = &td_thr_tls_get_addr; + tdb->td_thr_tlsbase_p = &td_thr_tlsbase; return 1; } @@ -639,6 +662,7 @@ try_thread_db_load_1 (void *handle) CHK (0, tdb->td_ta_set_event_p = dlsym (handle, "td_ta_set_event")); CHK (0, tdb->td_ta_event_getmsg_p = dlsym (handle, "td_ta_event_getmsg")); CHK (0, tdb->td_thr_tls_get_addr_p = dlsym (handle, "td_thr_tls_get_addr")); + CHK (0, tdb->td_thr_tlsbase_p = dlsym (handle, "td_thr_tlsbase")); #undef CHK diff --git a/gdb/linux-thread-db.c b/gdb/linux-thread-db.c index ca614a3..c0f7b1a 100644 --- a/gdb/linux-thread-db.c +++ b/gdb/linux-thread-db.c @@ -196,6 +196,9 @@ struct thread_db_info td_err_e (*td_thr_tls_get_addr_p) (const td_thrhandle_t *th, psaddr_t map_address, size_t offset, psaddr_t *address); + td_err_e (*td_thr_tlsbase_p) (const td_thrhandle_t *th, + unsigned long int modid, + psaddr_t *base); }; /* List of known processes using thread_db, and the required @@ -799,6 +802,7 @@ try_thread_db_load_1 (struct thread_db_info *info) info->td_ta_event_getmsg_p = dlsym (info->handle, "td_ta_event_getmsg"); info->td_thr_event_enable_p = dlsym (info->handle, "td_thr_event_enable"); info->td_thr_tls_get_addr_p = dlsym (info->handle, "td_thr_tls_get_addr"); + info->td_thr_tlsbase_p = dlsym (info->handle, "td_thr_tlsbase"); if (thread_db_find_new_threads_silently (inferior_ptid) != 0) { @@ -1811,21 +1815,39 @@ thread_db_get_thread_local_address (struct target_ops *ops, info = get_thread_db_info (ptid_get_pid (ptid)); - /* glibc doesn't provide the needed interface. */ - if (!info->td_thr_tls_get_addr_p) - throw_error (TLS_NO_LIBRARY_SUPPORT_ERROR, - _("No TLS library support")); - - /* Caller should have verified that lm != 0. */ - gdb_assert (lm != 0); - /* Finally, get the address of the variable. */ - /* Note the cast through uintptr_t: this interface only works if - a target address fits in a psaddr_t, which is a host pointer. - So a 32-bit debugger can not access 64-bit TLS through this. */ - err = info->td_thr_tls_get_addr_p (&thread_info->private->th, - (psaddr_t)(uintptr_t) lm, - offset, &address); + if (lm != 0) + { + /* glibc doesn't provide the needed interface. */ + if (!info->td_thr_tls_get_addr_p) + throw_error (TLS_NO_LIBRARY_SUPPORT_ERROR, + _("No TLS library support")); + + /* Note the cast through uintptr_t: this interface only works if + a target address fits in a psaddr_t, which is a host pointer. + So a 32-bit debugger can not access 64-bit TLS through this. */ + err = info->td_thr_tls_get_addr_p (&thread_info->private->th, + (psaddr_t)(uintptr_t) lm, + offset, &address); + } + else + { + /* If glibc doesn't provide the needed interface throw an error + that LM is zero - normally cases it should not be. */ + if (!info->td_thr_tlsbase_p) + throw_error (TLS_LOAD_MODULE_NOT_FOUND_ERROR, + _("TLS load module not found")); + + /* This code path handles the case of -static -pthread executables: + https://sourceware.org/ml/libc-help/2014-03/msg00024.html + For older GNU libc r_debug.r_map is NULL. For GNU libc after + PR libc/16831 due to GDB PR threads/16954 LOAD_MODULE is also NULL. + The constant number 1 depends on GNU __libc_setup_tls + initialization of l_tls_modid to 1. */ + err = info->td_thr_tlsbase_p (&thread_info->private->th, + 1, &address); + address = (char *) address + offset; + } #ifdef THREAD_DB_HAS_TD_NOTALLOC /* The memory hasn't been allocated, yet. */ diff --git a/gdb/target.c b/gdb/target.c index d08e2ea..71292d3 100644 --- a/gdb/target.c +++ b/gdb/target.c @@ -757,10 +757,6 @@ target_translate_tls_address (struct objfile *objfile, CORE_ADDR offset) /* Fetch the load module address for this objfile. */ lm_addr = gdbarch_fetch_tls_load_module_address (target_gdbarch (), objfile); - /* If it's 0, throw the appropriate exception. */ - if (lm_addr == 0) - throw_error (TLS_LOAD_MODULE_NOT_FOUND_ERROR, - _("TLS load module not found")); addr = target->to_get_thread_local_address (target, ptid, lm_addr, offset); diff --git a/gdb/target.h b/gdb/target.h index 23a7566..9371529 100644 --- a/gdb/target.h +++ b/gdb/target.h @@ -605,7 +605,8 @@ struct target_ops thread-local storage for the thread PTID and the shared library or executable file given by OBJFILE. If that block of thread-local storage hasn't been allocated yet, this function - may return an error. */ + may return an error. LOAD_MODULE_ADDR may be zero for statically + linked multithreaded inferiors. */ CORE_ADDR (*to_get_thread_local_address) (struct target_ops *ops, ptid_t ptid, CORE_ADDR load_module_addr, diff --git a/gdb/testsuite/gdb.threads/staticthreads.c b/gdb/testsuite/gdb.threads/staticthreads.c index e834d7f..5c8eabe 100644 --- a/gdb/testsuite/gdb.threads/staticthreads.c +++ b/gdb/testsuite/gdb.threads/staticthreads.c @@ -28,10 +28,17 @@ sem_t semaphore; +#ifdef HAVE_TLS +__thread int tlsvar; +#endif + void * thread_function (void *arg) { - printf ("Thread executing\n"); +#ifdef HAVE_TLS + tlsvar = 2; +#endif + printf ("Thread executing\n"); /* tlsvar-is-set */ while (sem_wait (&semaphore) != 0) { if (errno != EINTR) @@ -57,6 +64,9 @@ main (int argc, char **argv) return -1; } +#ifdef HAVE_TLS + tlsvar = 1; +#endif /* Create a thread, wait for it to complete. */ { diff --git a/gdb/testsuite/gdb.threads/staticthreads.exp b/gdb/testsuite/gdb.threads/staticthreads.exp index 80b0ba8..9fa625a 100644 --- a/gdb/testsuite/gdb.threads/staticthreads.exp +++ b/gdb/testsuite/gdb.threads/staticthreads.exp @@ -22,11 +22,16 @@ standard_testfile set static_flag "-static" -if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" \ - executable \ - [list debug "additional_flags=${static_flag}" \ - ]] != "" } { - return -1 +foreach have_tls { "-DHAVE_TLS" "" } { + if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" \ + executable \ + [list debug "additional_flags=${static_flag} ${have_tls}" \ + ]] == "" } { + break + } + if { $have_tls == "" } { + return -1 + } } clean_restart ${binfile} @@ -89,3 +94,18 @@ gdb_test_multiple "quit" "$test" { pass "$test" } } +clean_restart ${binfile} + + +if { "$have_tls" != "" } { + if ![runto_main] { + return -1 + } + gdb_breakpoint [gdb_get_line_number "tlsvar-is-set"] + gdb_continue_to_breakpoint "tlsvar-is-set" ".* tlsvar-is-set .*" + gdb_test "p tlsvar" " = 2" "tlsvar in thread" + gdb_test "thread 1" ".*" + # Unwind from pthread_join. + gdb_test "up 10" " in main .*" + gdb_test "p tlsvar" " = 1" "tlsvar in main" +}