From patchwork Fri Dec 22 11:33:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Alexandra_H=C3=A1jkov=C3=A1?= X-Patchwork-Id: 82756 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AAF063858404 for ; Fri, 22 Dec 2023 11:34:18 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id AF41A3858C5E for ; Fri, 22 Dec 2023 11:34:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AF41A3858C5E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org AF41A3858C5E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703244846; cv=none; b=iws7mzt0ON+3UxcgwWq5xTHoKElC9joZkaY56DOnDPuKVA/AL3e2E1tqWt2zgN3D/fEzcTa4r/YysE3pybtU13JTR1ViM1P5sHBc8KwXbZ5bdl8OUJ09U1kiKQnnQPKtRfv7pJHBJc6oTkX+c2naHqUtb5BjUEpJr+0DxcKGK6M= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1703244846; c=relaxed/simple; bh=p9npi9tL3uJi4TU2+n+fGlSivImzvP1L3GagYiqlHEo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=oolH4TRum1lcXbv8f4zZ8hWBgZu3aId6zG4l1SMnTnUF2w8Rw6UrxJ2sWng6mGUezYMySZnupfwQgaYdyGcE2/BUjSZRbkHUz+CLPwaVRG2NLXRI7pPaZIAm7JDH0x0isbsF4X8ywuAAyLkPEVAAmnX2cct1dcAD21iv/hFUodY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1703244844; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=vxw/Za7WzRN1Y3lcnN9S47zfkV4tJn0xsVxNJsRmMTM=; b=FW5F2TN5kgNz+YjJIh2Y04f1RCgsaeoRWkUuC3FIhxoAxAjZ+7SOqCrl34Hfkd9ZbWSrIA wYhKWl4bAxIUnqZ2INdiBGBH8nJqiqbyHNS35e8w2Q00GrU3rVUW9KM4FNpUNn+eOBfYMO Aks/8AVvAnh6P9P/IAAk1kp+Bkhcqw8= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-583-aSWb1NPcMP2uxVIS0N5KvQ-1; Fri, 22 Dec 2023 06:34:02 -0500 X-MC-Unique: aSWb1NPcMP2uxVIS0N5KvQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5ADA784AEEB for ; Fri, 22 Dec 2023 11:34:02 +0000 (UTC) Received: from fedora.redhat.com (unknown [10.45.242.11]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9525151D5 for ; Fri, 22 Dec 2023 11:34:01 +0000 (UTC) From: =?utf-8?q?Alexandra_H=C3=A1jkov=C3=A1?= To: gdb-patches@sourceware.org Subject: [PATCH] remote.c: Make packet_check_result return a structure Date: Fri, 22 Dec 2023 12:33:59 +0100 Message-ID: <20231222113359.1224157-1-ahajkova@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.2 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, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, 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 X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org packet_check_result currently returns an packet_result enum. If GDB will recieve an error in a format E.errtext, which is possible for some q packets, such errtext is lost if treated by packet_check_result. Introduce a new structure which bundles enum packet_result with possible error message or error code returned by the GDBserver. I plan to make more GDBserver response checking functions to use packet_check_result to make remote.c code more consistent. This will also allow to use E.errtext more in the future. There's no infrastructure to test this with a test case so I tested this by modifying store_registers_using_G function to get an error message: [remote] Sending packet: $GG00000000 ... gdbserver: Wrong sized register packet (expected 1792 bytes, got 1793) gdbserver: Invalid hex digit 71 Killing process(es): 1104002 [remote] Packet received: E01 Could not write registers; remote failure reply '01' Reviewed-by: Thiago Jung Bauermann --- gdb/remote.c | 110 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 79 insertions(+), 31 deletions(-) diff --git a/gdb/remote.c b/gdb/remote.c index dcc1a0d0639..15c90927a3c 100644 --- a/gdb/remote.c +++ b/gdb/remote.c @@ -149,13 +149,50 @@ get_target_type_name (bool target_connected) /* Analyze a packet's return value and update the packet config accordingly. */ -enum packet_result +enum packet_status { PACKET_ERROR, PACKET_OK, PACKET_UNKNOWN }; +/* Keeps packet's return value. If packet's return value is PACKET_ERROR, + err_msg contains an error message string from E.string or the number + stored as a string from E.num. */ +struct packet_result +{ + packet_result (enum packet_status status, std::string err_msg) + : m_status (status), + m_err_msg (std::move (err_msg)) + { + gdb_assert (status == PACKET_ERROR); + } + + packet_result (enum packet_status status) + : m_status (status) + { + gdb_assert (status != PACKET_ERROR); + } + +private: + enum packet_status m_status; + std::string m_err_msg; + +public: + enum packet_status + status () const + { + return this->m_status; + } + + const char * + err_msg () const + { + gdb_assert (this->m_status == PACKET_ERROR); + return this->m_err_msg.c_str (); + } +}; + /* Enumeration of packets for a remote target. */ enum { @@ -732,8 +769,8 @@ struct remote_features /* Check result value in BUF for packet WHICH_PACKET and update the packet's support configuration accordingly. */ - packet_result packet_ok (const char *buf, const int which_packet); - packet_result packet_ok (const gdb::char_vector &buf, const int which_packet); + packet_status packet_ok (const char *buf, const int which_packet); + packet_status packet_ok (const gdb::char_vector &buf, const int which_packet); /* Configuration of a remote target's memory read packet. */ memory_packet_config m_memory_read_packet_config; @@ -1254,7 +1291,7 @@ class remote_target : public process_stratum_target int unit_size, ULONGEST *xfered_len); - packet_result remote_send_printf (const char *format, ...) + packet_status remote_send_printf (const char *format, ...) ATTRIBUTE_PRINTF (2, 3); target_xfer_status remote_flash_write (ULONGEST address, @@ -2418,7 +2455,10 @@ add_packet_config_cmd (const unsigned int which_packet, const char *name, } } -static enum packet_result +/* Check GDBserver's reply packet. Return packet_result + structure which contains the packet_status enum + and an error message for the PACKET_ERROR case. */ +static packet_result packet_check_result (const char *buf) { if (buf[0] != '\0') @@ -2428,42 +2468,46 @@ packet_check_result (const char *buf) if (buf[0] == 'E' && isxdigit (buf[1]) && isxdigit (buf[2]) && buf[3] == '\0') + { /* "Enn" - definitely an error. */ - return PACKET_ERROR; + return { PACKET_ERROR, buf + 1 }; + } /* Always treat "E." as an error. This will be used for more verbose error messages, such as E.memtypes. */ if (buf[0] == 'E' && buf[1] == '.') - return PACKET_ERROR; + { + return { PACKET_ERROR, buf + 2 }; + } /* The packet may or may not be OK. Just assume it is. */ - return PACKET_OK; + return { PACKET_OK }; } else + { /* The stub does not support the packet. */ - return PACKET_UNKNOWN; + return { PACKET_UNKNOWN }; + } } -static enum packet_result +static struct packet_result packet_check_result (const gdb::char_vector &buf) { return packet_check_result (buf.data ()); } -packet_result +packet_status remote_features::packet_ok (const char *buf, const int which_packet) { packet_config *config = &m_protocol_packets[which_packet]; packet_description *descr = &packets_descriptions[which_packet]; - enum packet_result result; - if (config->detect != AUTO_BOOLEAN_TRUE && config->support == PACKET_DISABLE) internal_error (_("packet_ok: attempt to use a disabled packet")); - result = packet_check_result (buf); - switch (result) + packet_result result = packet_check_result (buf); + switch (result.status ()) { case PACKET_OK: case PACKET_ERROR: @@ -2498,10 +2542,10 @@ remote_features::packet_ok (const char *buf, const int which_packet) break; } - return result; + return result.status (); } -packet_result +packet_status remote_features::packet_ok (const gdb::char_vector &buf, const int which_packet) { return packet_ok (buf.data (), which_packet); @@ -3000,7 +3044,7 @@ remote_target::set_syscall_catchpoint (int pid, bool needed, int any_count, gdb::array_view syscall_counts) { const char *catch_packet; - enum packet_result result; + enum packet_status result; int n_sysno = 0; if (m_features.packet_support (PACKET_QCatchSyscalls) == PACKET_DISABLE) @@ -8791,9 +8835,10 @@ remote_target::send_g_packet () xsnprintf (rs->buf.data (), get_remote_packet_size (), "g"); putpkt (rs->buf); getpkt (&rs->buf); - if (packet_check_result (rs->buf) == PACKET_ERROR) + packet_result pkt_status = packet_check_result (rs->buf); + if (pkt_status.status () == PACKET_ERROR) error (_("Could not read registers; remote failure reply '%s'"), - rs->buf.data ()); + pkt_status.err_msg ()); /* We can get out of synch in various cases. If the first character in the buffer is not a hex character, assume that has happened @@ -9099,9 +9144,10 @@ remote_target::store_registers_using_G (const struct regcache *regcache) bin2hex (regs, p, rsa->sizeof_g_packet); putpkt (rs->buf); getpkt (&rs->buf); - if (packet_check_result (rs->buf) == PACKET_ERROR) + packet_result pkt_status = packet_check_result (rs->buf); + if (pkt_status.status () == PACKET_ERROR) error (_("Could not write registers; remote failure reply '%s'"), - rs->buf.data ()); + pkt_status.err_msg ()); } /* Store register REGNUM, or all registers if REGNUM == -1, from the contents @@ -9683,7 +9729,7 @@ remote_target::remote_read_bytes (CORE_ADDR memaddr, FORMAT and the remaining arguments, then gets the reply. Returns whether the packet was a success, a failure, or unknown. */ -packet_result +packet_status remote_target::remote_send_printf (const char *format, ...) { struct remote_state *rs = get_remote_state (); @@ -9705,8 +9751,9 @@ remote_target::remote_send_printf (const char *format, ...) rs->buf[0] = '\0'; getpkt (&rs->buf); + packet_result pkt_status = packet_check_result (rs->buf); - return packet_check_result (rs->buf); + return pkt_status.status (); } /* Flash writing can take quite some time. We'll set @@ -9718,7 +9765,7 @@ void remote_target::flash_erase (ULONGEST address, LONGEST length) { int addr_size = gdbarch_addr_bit (current_inferior ()->arch ()) / 8; - enum packet_result ret; + enum packet_status ret; scoped_restore restore_timeout = make_scoped_restore (&remote_timeout, remote_flash_timeout); @@ -11308,7 +11355,7 @@ remote_target::verify_memory (const gdb_byte *data, CORE_ADDR lma, ULONGEST size if (target_has_execution () && m_features.packet_support (PACKET_qCRC) != PACKET_DISABLE) { - enum packet_result result; + enum packet_status result; /* Make sure the remote is pointing at the right process. */ set_general_process (); @@ -12210,7 +12257,7 @@ remote_target::get_thread_local_address (ptid_t ptid, CORE_ADDR lm, struct remote_state *rs = get_remote_state (); char *p = rs->buf.data (); char *endp = p + get_remote_packet_size (); - enum packet_result result; + enum packet_status result; strcpy (p, "qGetTLSAddr:"); p += strlen (p); @@ -12256,7 +12303,7 @@ remote_target::get_tib_address (ptid_t ptid, CORE_ADDR *addr) struct remote_state *rs = get_remote_state (); char *p = rs->buf.data (); char *endp = p + get_remote_packet_size (); - enum packet_result result; + enum packet_status result; strcpy (p, "qGetTIBAddr:"); p += strlen (p); @@ -13821,7 +13868,7 @@ remote_target::get_trace_status (struct trace_status *ts) { /* Initialize it just to avoid a GCC false warning. */ char *p = NULL; - enum packet_result result; + enum packet_status result; struct remote_state *rs = get_remote_state (); if (m_features.packet_support (PACKET_qTStatus) == PACKET_DISABLE) @@ -14201,7 +14248,7 @@ remote_target::set_trace_buffer_size (LONGEST val) struct remote_state *rs = get_remote_state (); char *buf = rs->buf.data (); char *endbuf = buf + get_remote_packet_size (); - enum packet_result result; + enum packet_status result; gdb_assert (val >= 0 || val == -1); buf += xsnprintf (buf, endbuf - buf, "QTBuffer:size:"); @@ -15527,9 +15574,10 @@ remote_target::store_memtags (CORE_ADDR address, size_t len, putpkt (rs->buf); getpkt (&rs->buf); + packet_result pkt_status = packet_check_result (rs->buf); /* Verify if the request was successful. */ - return packet_check_result (rs->buf.data ()) == PACKET_OK; + return pkt_status.status () == PACKET_OK; } /* Return true if remote target T is non-stop. */