From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id 9CC6B3858298 for ; Mon, 21 Aug 2023 10:53:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9CC6B3858298 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id CFE092065C for ; Mon, 21 Aug 2023 10:53:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1692615230; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=v/Lte6SQHU741TRqd20iJE8GnK2+imF88w64Uld1yH0=; b=KWhw5pkw0UyVhVI3ENNA6J0ZAS8AZZgfSUSwO2pI75+fraBAu2LwqFbFj50FY3NV0d3KEW QLK07Pt79VlAV7Ae+W0/lYX2Sg8Mzh5N3rGlwdNsJswZuUdoxsyfaHUtyKJBmZm9VTMqeb FmTFlf0tAtUYM57c6Ad1+ByFna2McjI= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1692615230; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=v/Lte6SQHU741TRqd20iJE8GnK2+imF88w64Uld1yH0=; b=o27NvNqrJ/JXZqBCw5L5NjI1kRUPBBU1p1IY1qW387ZbhawJxWP6TtMd3j8KtyqfeR5xf1 hXAmEgpaKaauqgBw== Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id B305F139BC for ; Mon, 21 Aug 2023 10:53:50 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id KH+/Kj5C42QMCwAAMHmgww (envelope-from ) for ; Mon, 21 Aug 2023 10:53:50 +0000 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH 2/2] [gdb/build] Return const reference in thread_info_to_thread_handle Date: Mon, 21 Aug 2023 12:53:56 +0200 Message-Id: <20230821105356.869-2-tdevries@suse.de> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20230821105356.869-1-tdevries@suse.de> References: <20230821105356.869-1-tdevries@suse.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,SPF_HELO_NONE,SPF_PASS,TXREP 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: In remote_target::thread_info_to_thread_handle we return a copy: ... gdb::byte_vector remote_target::thread_info_to_thread_handle (struct thread_info *tp) { remote_thread_info *priv = get_remote_thread_info (tp); return priv->thread_handle; } ... Fix this by returning a const reference instead: ... const gdb::optional & remote_target::thread_info_to_thread_handle (struct thread_info *tp) ... Returning a gdb::optional allows us to return a nullptr, or std::nullopt in std::optional terms, something that is required by thread_db_target::thread_info_to_thread_handle. In gdb we use gdb::optional instead std::optional, because std::optional is availabe starting c++17 and we support c++11 and c++14, but gdb::nullopt is currently not available, though a submission is available [1]. So we use a kludge gdb_optional_byte_vector_nullopt. Tested on x86_64-linux. This fixes the build when building with -std=c++20. [1] https://sourceware.org/pipermail/gdb-patches/2020-May/168800.html --- gdb/linux-thread-db.c | 12 +++++++----- gdb/remote.c | 14 +++++++------- gdb/target-debug.h | 8 ++++++++ gdb/target-delegates.c | 16 ++++++++-------- gdb/target.c | 15 +++++++++++++-- gdb/target.h | 9 ++++++--- 6 files changed, 49 insertions(+), 25 deletions(-) diff --git a/gdb/linux-thread-db.c b/gdb/linux-thread-db.c index 7d9fd57da0c..f238b26445b 100644 --- a/gdb/linux-thread-db.c +++ b/gdb/linux-thread-db.c @@ -107,7 +107,8 @@ class thread_db_target final : public target_ops thread_info *thread_handle_to_thread_info (const gdb_byte *thread_handle, int handle_len, inferior *inf) override; - gdb::byte_vector thread_info_to_thread_handle (struct thread_info *) override; + const gdb::optional & + thread_info_to_thread_handle (struct thread_info *) override; }; static std::string libthread_db_search_path = LIBTHREAD_DB_SEARCH_PATH; @@ -312,6 +313,7 @@ struct thread_db_thread_info : public private_thread_info /* Cached thread state. */ td_thrhandle_t th {}; thread_t tid {}; + gdb::optional thread_handle; }; static thread_db_thread_info * @@ -1724,20 +1726,20 @@ thread_db_target::thread_handle_to_thread_info (const gdb_byte *thread_handle, /* Return the thread handle associated the thread_info pointer TP. */ -gdb::byte_vector +const gdb::optional & thread_db_target::thread_info_to_thread_handle (struct thread_info *tp) { thread_db_thread_info *priv = get_thread_db_thread_info (tp); if (priv == NULL) - return gdb::byte_vector (); + return gdb_optional_byte_vector_nullopt (); int handle_size = sizeof (priv->tid); gdb::byte_vector rv (handle_size); memcpy (rv.data (), &priv->tid, handle_size); - - return rv; + priv->thread_handle.emplace (std::move (rv)); + return priv->thread_handle; } /* Get the address of the thread local variable in load module LM which diff --git a/gdb/remote.c b/gdb/remote.c index 7abe08439b5..a3650bc7003 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -784,8 +784,8 @@ class remote_target : public process_stratum_target int handle_len, inferior *inf) override; - gdb::byte_vector thread_info_to_thread_handle (struct thread_info *tp) - override; + const gdb::optional & + thread_info_to_thread_handle (struct thread_info *tp) override; void stop (ptid_t) override; @@ -1452,7 +1452,7 @@ struct remote_thread_info : public private_thread_info /* Thread handle, perhaps a pthread_t or thread_t value, stored as a sequence of bytes. */ - gdb::byte_vector thread_handle; + gdb::optional thread_handle; /* Whether the target stopped for a breakpoint/watchpoint. */ enum target_stop_reason stop_reason = TARGET_STOPPED_BY_NO_REASON; @@ -14538,10 +14538,10 @@ remote_target::thread_handle_to_thread_info (const gdb_byte *thread_handle, if (tp->inf == inf && priv != NULL) { - if (handle_len != priv->thread_handle.size ()) + if (handle_len != priv->thread_handle->size ()) error (_("Thread handle size mismatch: %d vs %zu (from remote)"), - handle_len, priv->thread_handle.size ()); - if (memcmp (thread_handle, priv->thread_handle.data (), + handle_len, priv->thread_handle->size ()); + if (memcmp (thread_handle, priv->thread_handle->data (), handle_len) == 0) return tp; } @@ -14550,7 +14550,7 @@ remote_target::thread_handle_to_thread_info (const gdb_byte *thread_handle, return NULL; } -gdb::byte_vector +const gdb::optional & remote_target::thread_info_to_thread_handle (struct thread_info *tp) { remote_thread_info *priv = get_remote_thread_info (tp); diff --git a/gdb/target-debug.h b/gdb/target-debug.h index acb01d47e7c..9afef943fab 100644 --- a/gdb/target-debug.h +++ b/gdb/target-debug.h @@ -236,4 +236,12 @@ target_debug_print_gdb_byte_vector_r (gdb::byte_vector &vector) { target_debug_print_const_gdb_byte_vector_r (vector); } + +static void +target_debug_print_const_gdb_optional_gdb_byte_vector_r (const gdb::optional &vector) +{ + if (!vector.has_value ()) + return; + target_debug_print_const_gdb_byte_vector_r (*vector); +} #endif /* TARGET_DEBUG_H */ diff --git a/gdb/target-delegates.c b/gdb/target-delegates.c index 06f22d57c27..fe293142a23 100644 --- a/gdb/target-delegates.c +++ b/gdb/target-delegates.c @@ -89,7 +89,7 @@ struct dummy_target : public target_ops const char *extra_thread_info (thread_info *arg0) override; const char *thread_name (thread_info *arg0) override; thread_info *thread_handle_to_thread_info (const gdb_byte *arg0, int arg1, inferior *arg2) override; - gdb::byte_vector thread_info_to_thread_handle (struct thread_info *arg0) override; + const gdb::optional & thread_info_to_thread_handle (struct thread_info *arg0) override; void stop (ptid_t arg0) override; void interrupt () override; void pass_ctrlc () override; @@ -263,7 +263,7 @@ struct debug_target : public target_ops const char *extra_thread_info (thread_info *arg0) override; const char *thread_name (thread_info *arg0) override; thread_info *thread_handle_to_thread_info (const gdb_byte *arg0, int arg1, inferior *arg2) override; - gdb::byte_vector thread_info_to_thread_handle (struct thread_info *arg0) override; + const gdb::optional & thread_info_to_thread_handle (struct thread_info *arg0) override; void stop (ptid_t arg0) override; void interrupt () override; void pass_ctrlc () override; @@ -1871,28 +1871,28 @@ debug_target::thread_handle_to_thread_info (const gdb_byte *arg0, int arg1, infe return result; } -gdb::byte_vector +const gdb::optional & target_ops::thread_info_to_thread_handle (struct thread_info *arg0) { return this->beneath ()->thread_info_to_thread_handle (arg0); } -gdb::byte_vector +const gdb::optional & dummy_target::thread_info_to_thread_handle (struct thread_info *arg0) { - return gdb::byte_vector (); + return gdb_optional_byte_vector_nullopt (); } -gdb::byte_vector +const gdb::optional & debug_target::thread_info_to_thread_handle (struct thread_info *arg0) { gdb_printf (gdb_stdlog, "-> %s->thread_info_to_thread_handle (...)\n", this->beneath ()->shortname ()); - gdb::byte_vector result + const gdb::optional & result = this->beneath ()->thread_info_to_thread_handle (arg0); gdb_printf (gdb_stdlog, "<- %s->thread_info_to_thread_handle (", this->beneath ()->shortname ()); target_debug_print_struct_thread_info_p (arg0); gdb_puts (") = ", gdb_stdlog); - target_debug_print_gdb_byte_vector (result); + target_debug_print_const_gdb_optional_gdb_byte_vector_r (result); gdb_puts ("\n", gdb_stdlog); return result; } diff --git a/gdb/target.c b/gdb/target.c index 16f43d072cd..32383ad69b0 100644 --- a/gdb/target.c +++ b/gdb/target.c @@ -55,6 +55,17 @@ #include "valprint.h" #include "cli/cli-decode.h" +/* See target.h. */ + +const gdb::optional & +gdb_optional_byte_vector_nullopt () +{ + /* For a generic solution, see this submission ( + https://sourceware.org/pipermail/gdb-patches/2020-May/168800.html ). */ + static const gdb::optional gdb_optional_byte_vector_nullopt = {}; + return gdb_optional_byte_vector_nullopt; +} + static void generic_tls_error (void) ATTRIBUTE_NORETURN; static void default_terminal_info (struct target_ops *, const char *, int); @@ -2640,12 +2651,12 @@ target_thread_handle_to_thread_info (const gdb_byte *thread_handle, /* See target.h. */ -gdb::byte_vector +const gdb::byte_vector & target_thread_info_to_thread_handle (struct thread_info *tip) { target_ops *target = current_inferior ()->top_target (); - return target->thread_info_to_thread_handle (tip); + return *target->thread_info_to_thread_handle (tip); } void diff --git a/gdb/target.h b/gdb/target.h index 6ae400e2cc2..82d474881d1 100644 --- a/gdb/target.h +++ b/gdb/target.h @@ -679,8 +679,8 @@ struct target_ops inferior *inf) TARGET_DEFAULT_RETURN (NULL); /* See target_thread_info_to_thread_handle. */ - virtual gdb::byte_vector thread_info_to_thread_handle (struct thread_info *) - TARGET_DEFAULT_RETURN (gdb::byte_vector ()); + virtual const gdb::optional &thread_info_to_thread_handle (struct thread_info *) + TARGET_DEFAULT_RETURN (gdb_optional_byte_vector_nullopt ()); virtual void stop (ptid_t) TARGET_DEFAULT_IGNORE (); virtual void interrupt () @@ -1924,7 +1924,7 @@ extern struct thread_info *target_thread_handle_to_thread_info /* Given a thread, return the thread handle, a target-specific sequence of bytes which serves as a thread identifier within the program being debugged. */ -extern gdb::byte_vector target_thread_info_to_thread_handle +extern const gdb::byte_vector &target_thread_info_to_thread_handle (struct thread_info *); /* Attempts to find the pathname of the executable file @@ -2558,4 +2558,7 @@ extern void target_prepare_to_generate_core (void); /* See to_done_generating_core. */ extern void target_done_generating_core (void); +/* Type specific nullopt, gdb_optional.h has no support for gdb::nullopt. */ +extern const gdb::optional &gdb_optional_byte_vector_nullopt (); + #endif /* !defined (TARGET_H) */ -- 2.35.3