From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 7445 invoked by alias); 22 Aug 2018 14:30:42 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 7307 invoked by uid 89); 22 Aug 2018 14:30:33 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-24.9 required=5.0 tests=AWL,BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,SPF_HELO_PASS,SPF_PASS autolearn=ham version=3.3.2 spammy= X-HELO: smtp.polymtl.ca Received: from smtp.polymtl.ca (HELO smtp.polymtl.ca) (132.207.4.11) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Wed, 22 Aug 2018 14:30:31 +0000 Received: from simark.ca (simark.ca [158.69.221.121]) (authenticated bits=0) by smtp.polymtl.ca (8.14.7/8.14.7) with ESMTP id w7MEUPgH023755 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT) for ; Wed, 22 Aug 2018 10:30:29 -0400 Received: by simark.ca (Postfix, from userid 112) id ED6FF1EABA; Wed, 22 Aug 2018 10:30:24 -0400 (EDT) Received: from simark.ca (localhost [127.0.0.1]) by simark.ca (Postfix) with ESMTP id 96DC91E76F; Wed, 22 Aug 2018 10:30:23 -0400 (EDT) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Date: Wed, 22 Aug 2018 14:30:00 -0000 From: Simon Marchi To: Xavier Roirand Cc: gdb-patches@sourceware.org, brobecker@adacore.com Subject: Re: [RFA 4/5] Darwin: fix thread ptid started by fork_inferior In-Reply-To: <1534932677-9496-5-git-send-email-roirand@adacore.com> References: <1534932677-9496-1-git-send-email-roirand@adacore.com> <1534932677-9496-5-git-send-email-roirand@adacore.com> Message-ID: <48a44daa04048ef58c7a0abf57e4a6e5@polymtl.ca> X-Sender: simon.marchi@polymtl.ca User-Agent: Roundcube Webmail/1.3.6 X-IsSubscribed: yes X-SW-Source: 2018-08/txt/msg00530.txt.bz2 On 2018-08-22 06:11, Xavier Roirand wrote: > When debugging a program on Mac OS X Darwin, gdb stops with: > > Temporary breakpoint 1 at 0x100000fb4: file /tmp/helloworld.c, line 1. > Starting program: /private/tmp/helloworld > [New Thread 0xb03 of process 65066] > [New Thread 0xd03 of process 65066] > During startup program terminated with signal SIGTRAP, Trace/breakpoint > trap. > > When the inferior is started a thread with lwp=tid=0 is created > and has to be fixed later by darwin_init_thread_list(). Because > this is not done, GDB does not understand that the SIGTRAP is > coming from GDB and not the program itself. I think I have seen this error in my testing yesterday, though it seemed intermittent. If I started a few times in a row, I would often get that SIGTRAP, but it would eventually work... I can't really comment on the validity of the fix, so I will assume it's right (I am not at work right now so I can't test on the Mac). But linux-nat does something similar (add a pid-only thread, then change it to its real ptid), so there is a precedent. > diff --git a/gdb/darwin-nat.c b/gdb/darwin-nat.c > index 96f70cf..9ad4a87 100644 > --- a/gdb/darwin-nat.c > +++ b/gdb/darwin-nat.c > @@ -344,8 +344,22 @@ darwin_check_new_threads (struct inferior *inf) > pti->gdb_port = new_id; > pti->msg_state = DARWIN_RUNNING; > > - /* Add the new thread. */ > - add_thread_with_info (ptid_t (inf->pid, 0, new_id), pti); > + if (old_nbr == 0 && new_ix == 0) > + { > + /* A ptid is created when the inferior is started (see > + fork-child.c) with lwp=tid=0. This ptid will be > renamed > + later by darwin_init_thread_list (), so find this > previous > + thread silently added. */ > + > + struct thread_info *tp = find_thread_ptid (ptid_t > (inf->pid, 0, 0)); > + tp->priv.reset (pti); Put a gdb_assert (tp != nullptr), so that if for some reason the thread is not found (because of a GDB bug), we fail with a failed assertion rather than a segfault. > + } > + else > + { > + /* Add the new thread. */ > + add_thread_with_info (ptid_t (inf->pid, 0, new_id), > pti); > + } > + > new_thread_vec.push_back (pti); > new_ix++; > continue; > @@ -1733,6 +1747,8 @@ thread_info_from_private_thread_info > (darwin_thread_info *pti) > static void > darwin_init_thread_list (struct inferior *inf) > { > + ptid_t new_ptid; > + > darwin_check_new_threads (inf); > > darwin_inferior *priv = get_darwin_inferior (inf); > @@ -1743,7 +1759,11 @@ darwin_init_thread_list (struct inferior *inf) > struct thread_info *first_thread > = thread_info_from_private_thread_info (first_pti); > > - inferior_ptid = first_thread->ptid; > + /* Note: fork_inferior automatically add a thread but it uses a > wrong ptid. > + Fix up. */ > + new_ptid = ptid_t (inf->pid, 0, first_pti->gdb_port); > + thread_change_ptid (inferior_ptid, new_ptid); > + inferior_ptid = new_ptid; You can declare new_ptid when initializing it. Simon