From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 3F8A93858C5E for ; Mon, 5 Jun 2023 09:11:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3F8A93858C5E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1685956276; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=UpXr3H3rgFySkm+NGuoDLvZDXdFE/6MNr+Jt9oR7W5g=; b=b67nZ6PP+XKVgzLf2cC3vBdgid6L5zlQMN+N6TVhR8Ym0stPTLQGJ/pIE1MeuhYZLL6WFZ AK/CUDXaf6uN/TrFnJWrijZmc9F3NjK9R7/cPUcOEYpq6A499EVWPUeAcCiUWWW/LbfCzx ajuWY6VqMPlX3iwVm64ZmQBuO6jbYb0= Received: from mail-lf1-f69.google.com (mail-lf1-f69.google.com [209.85.167.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-592-Arw4-7DbPfeCIrJodtPCig-1; Mon, 05 Jun 2023 05:11:15 -0400 X-MC-Unique: Arw4-7DbPfeCIrJodtPCig-1 Received: by mail-lf1-f69.google.com with SMTP id 2adb3069b0e04-4f4c62e0c9eso2939990e87.3 for ; Mon, 05 Jun 2023 02:11:15 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685956274; x=1688548274; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=UpXr3H3rgFySkm+NGuoDLvZDXdFE/6MNr+Jt9oR7W5g=; b=ewvf8WJIzwKf98dAsU9YBXsT9M8bQrKnsjKK7MsHUmFBNdPb7dKp1LHpeZWMDMYn8l Rhv3PMiA6Q9nVhBMlYrKjOfHxU7oDAldhhDZRUuIHf/D1Grn5jDkw9EDnYIDZSmwUW6A 3pqI+TEJbYImfbbioZ96agPgSyv3fnWb5ptzRgEpobz7aiqJSHSsVJun1+Hy+2cIS2GQ xk+q41S5ZXtLdv98KGJCI/Bpu0OSrW0OPNTGzQ9vX7/u3317+PUkEMivvN/yQpCXHZE6 GCeXxFabH78ohZ4yzFsjou1wUeDpb5TmxCxBz+NmGIvDfOiRxDtEG3LEQCEb7UGLh8Ba Xt7g== X-Gm-Message-State: AC+VfDzrjPjO337ne8In+nUNNMt0sP8B1rzNlroq55QkV7BotZtdGekA 32LuJvmS0KinSEhAjor0zg00cIGu9xBL6OXStQciBeliVXGIBQi8cj2AkmoKfbAWCQHA8ba/Fw2 ouI02R9x1yvmN6PN5/Ts8+kjgz2bZ5vf/q34vLYXHFwyM3ol1zjnTuwnV50U3k9cvYi5AfyGPe8 +QpEChqg== X-Received: by 2002:a19:ad41:0:b0:4f6:2e5c:de65 with SMTP id s1-20020a19ad41000000b004f62e5cde65mr313487lfd.28.1685956274162; Mon, 05 Jun 2023 02:11:14 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5wnIcSMtCVRWa9r6fvKJ5DFjPBDceAE/z1F4RM+Qo1b+d4Vf1MtPKQn3DipdmVVl52dCVJTw== X-Received: by 2002:a19:ad41:0:b0:4f6:2e5c:de65 with SMTP id s1-20020a19ad41000000b004f62e5cde65mr313474lfd.28.1685956273695; Mon, 05 Jun 2023 02:11:13 -0700 (PDT) Received: from localhost (11.72.115.87.dyn.plus.net. [87.115.72.11]) by smtp.gmail.com with ESMTPSA id w16-20020adfd4d0000000b0030aefa3a957sm9176556wrk.28.2023.06.05.02.11.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Jun 2023 02:11:13 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 1/3] gdb: split inferior and thread setup when opening a core file Date: Mon, 5 Jun 2023 10:11:07 +0100 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: I noticed that in corelow.c, when a core file is opened, both the thread and inferior setup is done in add_to_thread_list. In this patch I propose hoisting the inferior setup out of add_to_thread_list into core_target_open. The only thing about this change that gave me cause for concern is that in add_to_thread_list, we only setup the inferior after finding the first section with a name like ".reg/NN". If we find no such section then the inferior will never be setup. Is this important? Well, I don't think so. Back in core_target_open, if there is no current thread (which there will not be if no ".reg/NN" section was found), then we look for a thread in the current inferior. If there are no threads (which there will not be if no ".reg/NN" is found), then we once again setup the current inferior. What I think this means, is that, in all cases, the current inferior will end up being setup. By moving the inferior setup code earlier in core_target_open and making it non-conditional, we can remove the later code that sets up the inferior, we now know this will always have been done. There should be no user visible changes after this commit. --- gdb/corelow.c | 62 ++++++++++++++++++++++++--------------------------- 1 file changed, 29 insertions(+), 33 deletions(-) diff --git a/gdb/corelow.c b/gdb/corelow.c index db489b4280e..7312d40374f 100644 --- a/gdb/corelow.c +++ b/gdb/corelow.c @@ -351,40 +351,24 @@ core_target::close () /* Look for sections whose names start with `.reg/' so that we can extract the list of threads in a core file. */ +/* If ASECT is a section whose name begins with '.reg/' then extract the + lwpid after the '/' and create a new thread in INF. + + If REG_SECT is not nullptr, and the both ASECT and REG_SECT point at the + same position in the parent bfd object then switch to the newly created + thread, otherwise, the selected thread is left unchanged. */ + static void -add_to_thread_list (asection *asect, asection *reg_sect) +add_to_thread_list (asection *asect, asection *reg_sect, inferior *inf) { - int core_tid; - int pid, lwpid; - bool fake_pid_p = false; - struct inferior *inf; - if (!startswith (bfd_section_name (asect), ".reg/")) return; - core_tid = atoi (bfd_section_name (asect) + 5); - - pid = bfd_core_file_pid (core_bfd); - if (pid == 0) - { - fake_pid_p = true; - pid = CORELOW_PID; - } - - lwpid = core_tid; - - inf = current_inferior (); - if (inf->pid == 0) - { - inferior_appeared (inf, pid); - inf->fake_pid_p = fake_pid_p; - } - - ptid_t ptid (pid, lwpid); - + int lwpid = atoi (bfd_section_name (asect) + 5); + ptid_t ptid (inf->pid, lwpid); thread_info *thr = add_thread (inf->process_target (), ptid); -/* Warning, Will Robinson, looking at BFD private data! */ + /* Warning, Will Robinson, looking at BFD private data! */ if (reg_sect != NULL && asect->filepos == reg_sect->filepos) /* Did we find .reg? */ @@ -541,12 +525,27 @@ core_target_open (const char *arg, int from_tty) previous session, and the frame cache being stale. */ registers_changed (); + /* Find (or fake) the pid for the process in this core file, and + initialise the current inferior with that pid. */ + bool fake_pid_p = false; + int pid = bfd_core_file_pid (core_bfd); + if (pid == 0) + { + fake_pid_p = true; + pid = CORELOW_PID; + } + + inferior *inf = current_inferior (); + gdb_assert (inf->pid == 0); + inferior_appeared (inf, pid); + inf->fake_pid_p = fake_pid_p; + /* Build up thread list from BFD sections, and possibly set the current thread to the .reg/NN section matching the .reg section. */ asection *reg_sect = bfd_get_section_by_name (core_bfd, ".reg"); for (asection *sect : gdb_bfd_sections (core_bfd)) - add_to_thread_list (sect, reg_sect); + add_to_thread_list (sect, reg_sect, inf); if (inferior_ptid == null_ptid) { @@ -556,13 +555,10 @@ core_target_open (const char *arg, int from_tty) which was the "main" thread. The latter case shouldn't usually happen, but we're dealing with input here, which can always be broken in different ways. */ - thread_info *thread = first_thread_of_inferior (current_inferior ()); + thread_info *thread = first_thread_of_inferior (inf); if (thread == NULL) - { - inferior_appeared (current_inferior (), CORELOW_PID); - thread = add_thread_silent (target, ptid_t (CORELOW_PID)); - } + thread = add_thread_silent (target, ptid_t (CORELOW_PID)); switch_to_thread (thread); } -- 2.25.4