From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id uP5pB9HJx2DNUQAAWB0awg (envelope-from ) for ; Mon, 14 Jun 2021 17:27:45 -0400 Received: by simark.ca (Postfix, from userid 112) id 1C45B1F163; Mon, 14 Jun 2021 17:27:45 -0400 (EDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-0.6 required=5.0 tests=MAILING_LIST_MULTI, RDNS_DYNAMIC,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id DCC6E1E54D for ; Mon, 14 Jun 2021 17:27:43 -0400 (EDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A9F613973009 for ; Mon, 14 Jun 2021 21:27:43 +0000 (GMT) Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) by sourceware.org (Postfix) with ESMTPS id D9CD8397305B for ; Mon, 14 Jun 2021 21:24:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D9CD8397305B Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=palves.net Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wr1-f51.google.com with SMTP id r9so15983288wrz.10 for ; Mon, 14 Jun 2021 14:24:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=bGPoxSeXqzMWBc8mxCPnO1X3bbfdLXtrRVBK6kivul0=; b=O00XgjQrTG3ijfgFLCCmwEqE65BCEfQu49V+B6IygEAX+jraGTbi6H935QfFqMmN1f eMx7Z61xja4F4R42DoYd6/pKc46WHDzSrMy438grtsmljL7oz9iG1XaBGkpx6yCbOUQI F1cnq9HgypavcUKM+ZJlhzRB40ViOYtWqBtp2G5C43mbEdtR25UqoFKZc6M2UnDrdODW jOUCgcorCpw8MXyUmT/F6zSqBOquM6rNYNQJwUBhBiNmbF3vMQPY5UK9yovjNQpKx2te hg9xxmcss2Gyyfp6FbKCyarmAEfSmOUR7f32epK+NedUVSSdK4wzedQAYE6AC2wXWIYA 6+5A== X-Gm-Message-State: AOAM533AbOaXcMO0VAWo7e9ZEP8YG+epTOvHfNtM3SrLoqk3+Xw5HIKR NU0u/39bLQ35qf+O158z7lU9hgmzGRW7uw== X-Google-Smtp-Source: ABdhPJy+ZALDTbFakC6n/vXhlHsgRrKfSmn7K5ujkVkUgq5FwtFZ5YJZKWl0FqAIbko17eeOtBeAUg== X-Received: by 2002:adf:df8a:: with SMTP id z10mr22186385wrl.62.1623705881312; Mon, 14 Jun 2021 14:24:41 -0700 (PDT) Received: from localhost ([2001:8a0:f932:6a00:6b6e:c7b6:c5a7:aac3]) by smtp.gmail.com with ESMTPSA id z10sm14705497wmb.26.2021.06.14.14.24.40 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 14 Jun 2021 14:24:40 -0700 (PDT) From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH v2 14/16] convert previous_inferior_ptid to strong reference to thread_info Date: Mon, 14 Jun 2021 22:24:08 +0100 Message-Id: <20210614212410.1612666-15-pedro@palves.net> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210614212410.1612666-1-pedro@palves.net> References: <20210614212410.1612666-1-pedro@palves.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+public-inbox=simark.ca@sourceware.org Sender: "Gdb-patches" While working on a patch, I spotted a regression in the gdb.multi/multi-target-no-resumed.exp.exp testcase. Debugging the issue, I realized that the problem was related to how I was using previous_inferior_ptid to look up the thread the user had last selected. The problem is that previous_inferior_ptid alone doesn't tell you which target that ptid is from, and I was just always using the current target, which was incorrect. Two different targets may have threads with the same ptid. I decided to fix this by replacing previous_inferior_ptid with a strong reference to the thread, called previous_thread. A new update_previous_thread function is added can be used to updated previous_thread from inferior_ptid. This must be called in several places that really want to get rid of previous_thread thread, and reset the thread id counter, otherwise we get regressions like these: (gdb) info threads -gid Id GId Target Id Frame - * 1 1 Thread 2974541.2974541 "tids-gid-reset" main () at src/gdb/testsuite/gdb.multi/tids-gid-reset.c:21 - (gdb) PASS: gdb.multi/tids-gid-reset.exp: single-inferior: after restart: info threads -gid + * 1 2 Thread 2958361.2958361 "tids-gid-reset" main () at src/gdb/testsuite/gdb.multi/tids-gid-reset.c:21 + (gdb) FAIL: gdb.multi/tids-gid-reset.exp: single-inferior: after restart: info threads -gid and: Core was generated by `build/gdb/testsuite/outputs/gdb.reverse/sigall-precsave/si'. Program terminated with signal SIGTRAP, Trace/breakpoint trap. #0 gen_ABRT () at src/gdb/testsuite/gdb.reverse/sigall-reverse.c:398 398 kill (getpid (), SIGABRT); +[Current thread is 1 (LWP 2662066)] Restored records from core file build/gdb/testsuite/outputs/gdb.reverse/sigall-precsave/sigall.precsave. #0 gen_ABRT () at src/gdb/testsuite/gdb.reverse/sigall-reverse.c:398 398 kill (getpid (), SIGABRT); continue Continuing. -Program received signal SIGABRT, Aborted. +Thread 1 received signal SIGABRT, Aborted. 0x00007ffff7dfd55b in kill () at ../sysdeps/unix/syscall-template.S:78 78 ../sysdeps/unix/syscall-template.S: No such file or directory. -(gdb) PASS: gdb.reverse/sigall-precsave.exp: sig-test-1: get signal ABRT +(gdb) FAIL: gdb.reverse/sigall-precsave.exp: sig-test-1: get signal ABRT I.e., GDB was failing to restart the thread counter back to 1, because the previous_thread thread was being help due to the strong reference. Tested on GNU/Linux native, gdbserver and gdbserver + "maint set target-non-stop on". gdb/ChangeLog: yyyy-mm-dd Pedro Alves * infcmd.c (kill_command, detach_command, disconnect_command): Call update_previous_thread. * infrun.c (previous_inferior_ptid): Delete. (previous_thread): New. (update_previous_thread): New. (proceed, init_wait_for_inferior): Call update_previous_thread. (normal_stop): Adjust to compare previous_thread and inferior_thread. Call update_previous_thread. * infrun.h (update_previous_thread): Declare. * target.c (target_pre_inferior, target_preopen): Call update_previous_thread. Change-Id: I4f06dd81f00848bb7d6d26a94ff091e2a4e646d9 --- gdb/infcmd.c | 5 +++++ gdb/infrun.c | 25 ++++++++++++++++++------- gdb/infrun.h | 4 ++++ gdb/target.c | 5 +++++ 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/gdb/infcmd.c b/gdb/infcmd.c index 8190ba36565..99ce0ec78fa 100644 --- a/gdb/infcmd.c +++ b/gdb/infcmd.c @@ -2401,6 +2401,8 @@ kill_command (const char *arg, int from_tty) target_kill (); + update_previous_thread (); + if (print_inferior_events) printf_unfiltered (_("[Inferior %d (%s) killed]\n"), infnum, pid_str.c_str ()); @@ -2756,6 +2758,8 @@ detach_command (const char *args, int from_tty) target_detach (current_inferior (), from_tty); + update_previous_thread (); + /* The current inferior process was just detached successfully. Get rid of breakpoints that no longer make sense. Note we don't do this within target_detach because that is also used when @@ -2794,6 +2798,7 @@ disconnect_command (const char *args, int from_tty) target_disconnect (args, from_tty); no_shared_libraries (NULL, from_tty); init_thread_list (); + update_previous_thread (); if (deprecated_detach_hook) deprecated_detach_hook (); } diff --git a/gdb/infrun.c b/gdb/infrun.c index 1eb7526d246..3f40fa39b5a 100644 --- a/gdb/infrun.c +++ b/gdb/infrun.c @@ -143,8 +143,18 @@ show_step_stop_if_no_debug (struct ui_file *file, int from_tty, /* proceed and normal_stop use this to notify the user when the inferior stopped in a different thread than it had been running in. */ +static thread_info_ref previous_thread; -static ptid_t previous_inferior_ptid; +/* See infrun.h. */ + +void +update_previous_thread () +{ + if (inferior_ptid == null_ptid) + previous_thread = nullptr; + else + previous_thread = thread_info_ref::new_reference (inferior_thread ()); +} /* If set (default for legacy reasons), when following a fork, GDB will detach from one of the fork branches, child or parent. @@ -3072,7 +3082,7 @@ proceed (CORE_ADDR addr, enum gdb_signal siggnal) } /* We'll update this if & when we switch to a new thread. */ - previous_inferior_ptid = inferior_ptid; + update_previous_thread (); regcache = get_current_regcache (); gdbarch = regcache->arch (); @@ -3336,7 +3346,7 @@ init_wait_for_inferior (void) nullify_last_target_wait_ptid (); - previous_inferior_ptid = inferior_ptid; + update_previous_thread (); } @@ -8559,11 +8569,11 @@ normal_stop (void) after this event is handled, so we're not really switching, only informing of a stop. */ if (!non_stop - && previous_inferior_ptid != inferior_ptid - && target_has_execution () && last.kind != TARGET_WAITKIND_SIGNALLED && last.kind != TARGET_WAITKIND_EXITED - && last.kind != TARGET_WAITKIND_NO_RESUMED) + && last.kind != TARGET_WAITKIND_NO_RESUMED + && target_has_execution () + && previous_thread != inferior_thread ()) { SWITCH_THRU_ALL_UIS () { @@ -8572,7 +8582,8 @@ normal_stop (void) target_pid_to_str (inferior_ptid).c_str ()); annotate_thread_changed (); } - previous_inferior_ptid = inferior_ptid; + + update_previous_thread (); } if (last.kind == TARGET_WAITKIND_NO_RESUMED) diff --git a/gdb/infrun.h b/gdb/infrun.h index 7ebb9fc9f4e..5d791bdc5b4 100644 --- a/gdb/infrun.h +++ b/gdb/infrun.h @@ -87,6 +87,10 @@ enum exec_direction_kind /* The current execution direction. */ extern enum exec_direction_kind execution_direction; +/* Call this to point 'previous_thread' at the thread returned by + inferior_thread, or at nullptr, if there's no selected thread. */ +extern void update_previous_thread (); + extern void start_remote (int from_tty); /* Clear out all variables saying what to do when inferior is diff --git a/gdb/target.c b/gdb/target.c index 63aa3e95218..68d7e7c12d7 100644 --- a/gdb/target.c +++ b/gdb/target.c @@ -2505,6 +2505,8 @@ target_pre_inferior (int from_tty) current_inferior ()->highest_thread_num = 0; + update_previous_thread (); + agent_capability_invalidate (); } @@ -2533,6 +2535,9 @@ target_preopen (int from_tty) error (_("Program not killed.")); } + /* Release reference to old previous thread. */ + update_previous_thread (); + /* Calling target_kill may remove the target from the stack. But if it doesn't (which seems like a win for UDI), remove it now. */ /* Leave the exec target, though. The user may be switching from a -- 2.26.2