From patchwork Tue Apr 22 17:28:25 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Marchi X-Patchwork-Id: 640 X-Patchwork-Delegate: alves.ped@gmail.com Return-Path: X-Original-To: siddhesh@wilcox.dreamhost.com Delivered-To: siddhesh@wilcox.dreamhost.com Received: from homiemail-mx22.g.dreamhost.com (mx2.sub5.homie.mail.dreamhost.com [208.113.200.128]) by wilcox.dreamhost.com (Postfix) with ESMTP id 83F0A360ECE for ; Tue, 22 Apr 2014 10:28:10 -0700 (PDT) Received: by homiemail-mx22.g.dreamhost.com (Postfix, from userid 14314964) id 1D0E84E36B5C; Tue, 22 Apr 2014 10:28:10 -0700 (PDT) X-Original-To: gdb@patchwork.siddhesh.in Delivered-To: x14314964@homiemail-mx22.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-mx22.g.dreamhost.com (Postfix) with ESMTPS id EACE34E36B3B for ; Tue, 22 Apr 2014 10:28:09 -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:from:to:cc:subject:date:message-id :mime-version:content-type; q=dns; s=default; b=rZBoXuPreoPZqqbJ yqazuyCvvYnvhYZr3kf2y/lyhAbVJeQvs5rBECai3vzuQLse3y4AyboJLjUCI2V/ CQYyVyh5Rrjrki8Wcw8EEawIb4g3O4j4gBNIwaDjj8zMipUYwGjqu5buWHp41e6z eDhr3kxP0GsNPLJ/Lgj1FNiGIpQ= 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:from:to:cc:subject:date:message-id :mime-version:content-type; s=default; bh=gpekrLVz7pNssKLac5GSFX a8u3o=; b=e6tHV5YdHa+sTwLiAaSwDEn0PZo35D+giVJzBqKIt2MMsB8k/hLFid PPnaJkr3z0DRryNQPL6kzwhW+WJjGxNoe1MNfwrre9W5e9wrYKJ2RWRGoTelzIAF KwoJQ/DARj1V58HnHzk0rhq55eLjiKP8MtLX73Ko5HnnQf3UHyLf8= Received: (qmail 13531 invoked by alias); 22 Apr 2014 17:28:07 -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 13521 invoked by uid 89); 22 Apr 2014 17:28:07 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.1 required=5.0 tests=AWL, BAYES_00, SPF_PASS autolearn=ham version=3.3.2 X-HELO: usevmg20.ericsson.net Received: from usevmg20.ericsson.net (HELO usevmg20.ericsson.net) (198.24.6.45) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-SHA encrypted) ESMTPS; Tue, 22 Apr 2014 17:28:05 +0000 Received: from EUSAAHC008.ericsson.se (Unknown_Domain [147.117.188.96]) by usevmg20.ericsson.net (Symantec Mail Security) with SMTP id F8.66.05011.13856535; Tue, 22 Apr 2014 13:53:21 +0200 (CEST) Received: from simark-hp.dyn.mo.ca.am.ericsson.se (147.117.188.8) by smtps-am.internal.ericsson.com (147.117.188.96) with Microsoft SMTP Server (TLS) id 14.3.174.1; Tue, 22 Apr 2014 13:28:02 -0400 From: Simon Marchi To: CC: Simon Marchi Subject: [PATCH] gdbserver: Get the pid from /proc when attaching to a non-initial lwp Date: Tue, 22 Apr 2014 13:28:25 -0400 Message-ID: <1398187705-17237-1-git-send-email-simon.marchi@ericsson.com> MIME-Version: 1.0 X-IsSubscribed: yes X-DH-Original-To: gdb@patchwork.siddhesh.in gdbserver fails to attach to a second inferior that is multi-threaded. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=16255 For the second inferior (and so on), current_inferior is still set to the first inferior when this code is ran. As a result, non-initial lwps of the second inferior get assigned the pid of the first inferior. One solution could be to switch current_inferior temporarily. Another one (which I chose) is to go get the pid (tgid in the linux terminology) in /proc. I augmented the gdb.server/ext-attach.exp test case to attach to two inferiors simultaneously and made the test program multi-threaded. gdb/gdbserver/ChangeLog: 2014-04-22 Simon Marchi PR gdb/16255 * linux-low.c (linux_attach_lwp_1): Get pid of non-initial lwp from from /proc. gdb/testsuite/ChangeLog: 2014-04-22 Simon Marchi * gdb.server/ext-attach.c (thread_func): New function. Waits just like main (). (main): Start a second thread and join it. * gdb.server/ext-attach.exp: Start a second inferior and test attaching to it as well. --- gdb/gdbserver/linux-low.c | 6 +----- gdb/testsuite/gdb.server/ext-attach.c | 17 +++++++++++++++ gdb/testsuite/gdb.server/ext-attach.exp | 38 +++++++++++++++++++++++---------- 3 files changed, 45 insertions(+), 16 deletions(-) diff --git a/gdb/gdbserver/linux-low.c b/gdb/gdbserver/linux-low.c index c847c62..7fcf73e 100644 --- a/gdb/gdbserver/linux-low.c +++ b/gdb/gdbserver/linux-low.c @@ -672,11 +672,7 @@ linux_attach_lwp_1 (unsigned long lwpid, int initial) ptid = ptid_build (lwpid, lwpid, 0); else { - /* Note that extracting the pid from the current inferior is - safe, since we're always called in the context of the same - process as this new thread. */ - int pid = pid_of (current_inferior); - ptid = ptid_build (pid, lwpid, 0); + ptid = ptid_build (linux_proc_get_tgid (lwpid), lwpid, 0); } new_lwp = add_lwp (ptid); diff --git a/gdb/testsuite/gdb.server/ext-attach.c b/gdb/testsuite/gdb.server/ext-attach.c index 803d430..0fb0a64 100644 --- a/gdb/testsuite/gdb.server/ext-attach.c +++ b/gdb/testsuite/gdb.server/ext-attach.c @@ -19,13 +19,30 @@ attached to by gdb. It loops for a while, but not forever. */ #include +#include + +static void * +thread_func (void *arg) +{ + int i; + + for (i = 0; i < 120; i++) + sleep (1); + + return NULL; +} int main () { int i; + pthread_t thread; + + pthread_create (&thread, NULL, thread_func, NULL); for (i = 0; i < 120; i++) sleep (1); + pthread_join (thread, NULL); + return 0; } diff --git a/gdb/testsuite/gdb.server/ext-attach.exp b/gdb/testsuite/gdb.server/ext-attach.exp index 5f7bac4..2488b38 100644 --- a/gdb/testsuite/gdb.server/ext-attach.exp +++ b/gdb/testsuite/gdb.server/ext-attach.exp @@ -31,7 +31,7 @@ if [is_remote target] then { return 0 } -if {[prepare_for_testing $testfile.exp $testfile $srcfile debug]} { +if {[prepare_for_testing $testfile.exp $testfile $srcfile {debug additional_flags=-lpthread}]} { return -1 } @@ -46,16 +46,18 @@ gdb_test_no_output "set remote exec-file $target_exec" "set remote exec-file" # Start the program running and then wait for a bit, to be sure # that it can be attached to. -set testpid [eval exec $binfile &] +set testpid1 [eval exec $binfile &] +set testpid2 [eval exec $binfile &] exec sleep 2 if { [istarget "*-*-cygwin*"] } { - # testpid is the Cygwin PID, GDB uses the Windows PID, which might be + # testpid{1,2} are the Cygwin PID, GDB uses the Windows PID, which might be # different due to the way fork/exec works. - set testpid [ exec ps -e | gawk "{ if (\$1 == $testpid) print \$4; }" ] + set testpid1 [ exec ps -e | gawk "{ if (\$1 == $testpid1) print \$4; }" ] + set testpid2 [ exec ps -e | gawk "{ if (\$1 == $testpid2) print \$4; }" ] } -gdb_test "attach $testpid" \ - "Attaching to program: .*, process $testpid.*(in|at).*" \ +gdb_test "attach $testpid1" \ + "Attaching to program: .*, process $testpid1.*(in|at).*" \ "attach to remote program 1" if { [gdb_target_supports_trace] } then { @@ -71,10 +73,24 @@ gdb_test "backtrace" ".*main.*" "backtrace 1" gdb_test "detach" "Detaching from program.*process.*" gdb_test "backtrace" "No stack\\." "backtrace with no program" -gdb_test "attach $testpid" \ - "Attaching to program: .*, process $testpid.*(in|at).*" \ - "attach to remote program 2" -gdb_test "backtrace" ".*main.*" "backtrace 2" +# Now try to attach again, and try to attach to two inferiors +gdb_test "attach $testpid1" \ + "Attaching to program: .*, process $testpid1.*(in|at).*" \ + "attach to remote program 2.1" +gdb_test "backtrace" ".*main.*" "backtrace 2.1" + +gdb_test "add-inferior -exec $target_exec" \ + "Added inferior 2.*" \ + "add second inferior" +gdb_test "inferior 2" ".*Switching to inferior 2.*" "switch to second inferior" + +gdb_test "attach $testpid2" \ + "Attaching to program: .*, process $testpid2.*(in|at).*" \ + "attach to remote program 2.2" +gdb_test "backtrace" ".*main.*" "backtrace 2.2" + +gdb_test "kill" "" "kill inferior 2" "Kill the program being debugged.*" "y" +gdb_test "inferior 1" ".*Switching to inferior 1.*" +gdb_test "kill" "" "kill inferior 1" "Kill the program being debugged.*" "y" -gdb_test "kill" "" "kill" "Kill the program being debugged.*" "y" gdb_test_no_output "monitor exit"