From patchwork Mon Mar 27 12:32:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 66957 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 93BD638708A7 for ; Mon, 27 Mar 2023 12:33:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 93BD638708A7 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1679920403; bh=gLz+JduIp+jzmvnb/nqG/1KckidE0UjPlDkDTbF33rM=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=yTbiH95HbtJxLOFWD0L0mXP5UpC9YQpyBtgLxYdNIhjzWsNOT3JqlkPA2xbxi2aKS nOQaGRK7/oV8IyQxpgD187y6IXBP5G0pJ8diTBP7a3rb52VCbAWWJHUGBxLyXVxgFx RKjtMMQe8E9d+i9aBPf3VuwH8Xn4gJhL2sK3NQxY= 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 A1D363858298 for ; Mon, 27 Mar 2023 12:32:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A1D363858298 Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-349-bdwoWCuBOJyXYVO0xWimXg-1; Mon, 27 Mar 2023 08:32:29 -0400 X-MC-Unique: bdwoWCuBOJyXYVO0xWimXg-1 Received: by mail-wm1-f72.google.com with SMTP id n33-20020a05600c3ba100b003ee6335b6adso4640606wms.3 for ; Mon, 27 Mar 2023 05:32:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679920347; 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=gLz+JduIp+jzmvnb/nqG/1KckidE0UjPlDkDTbF33rM=; b=xxFcV8/ff45qk7lzlk/5klbSurzCQOND6oefD/iGwFDk81fyXfXrL2DErfZVgOFv3s W1QEKVHuD5rg3p4ES0a+NLT6DtYjRsCLCLWnPPlWMXyKh1xbJA+ayTHVvGx4H5KLkTwo O1II/oVCLWknPO1XCs7s2fnU9s981LbXBz9e20GdLZRWPKhXil9Nr3cULGvdv+v/rHq0 Nqb9tSt8pbkxtZFOFe/zy+hQx70bEwjYPju2wglHw/n1ur8HxKQok13thViY+TTNHeGq 20OgqvJcXvL4RxdD4C6vh/tJbqCsfYVwGJcKd3P8RqnPTIp4/b1516QirRaoc9RI/0sO UK6w== X-Gm-Message-State: AO0yUKWuTN7MPHx9WMsvAWTGXCjR8AhIJZJNzXXPiWJezavdTMMdhzZu OcITBKiN2pryeCzhTLEMCxpmyeAdPbraYJ2UKBywnq9BIH3T8uMP9r1L4H+utnRhKDENk/qtqon QHujZIVBLGuWnTmEnwGzMEyh3jtS4Qn+puWQLIO0QkAt7ge2qaw2oHBe4NIXTehx+KShnnO82/s MDh06wQA== X-Received: by 2002:a7b:c398:0:b0:3ed:da87:3349 with SMTP id s24-20020a7bc398000000b003edda873349mr8819378wmj.1.1679920347510; Mon, 27 Mar 2023 05:32:27 -0700 (PDT) X-Google-Smtp-Source: AK7set9crAytYp22TW/m5HC74GG7MXDIfzwBkCXhZ6AIQWhgxC5zBc3xX/5ukAqMMhN3ZjfliA900w== X-Received: by 2002:a7b:c398:0:b0:3ed:da87:3349 with SMTP id s24-20020a7bc398000000b003edda873349mr8819357wmj.1.1679920347100; Mon, 27 Mar 2023 05:32:27 -0700 (PDT) Received: from localhost (95.72.115.87.dyn.plus.net. [87.115.72.95]) by smtp.gmail.com with ESMTPSA id m15-20020a05600c3b0f00b003ee91eda67bsm8823537wms.12.2023.03.27.05.32.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Mar 2023 05:32:26 -0700 (PDT) To: gdb-patches@sourceware.org Cc: Pedro Alves , Simon Marchi , Andrew Burgess Subject: [PATCHv3 2/3] gdb: move displaced_step_dump_bytes into gdbsupport (and rename) Date: Mon, 27 Mar 2023 13:32:20 +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 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, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, 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 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: , X-Patchwork-Original-From: Andrew Burgess via Gdb-patches From: Andrew Burgess Reply-To: Andrew Burgess Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" It was pointed out during review of another patch that the function displaced_step_dump_bytes really isn't specific to displaced stepping, and should really get a more generic name and move into gdbsupport/. This commit does just that. The function is renamed to bytes_to_string and is moved into gdbsupport/common-utils.{cc,h}. The function implementation doesn't really change. Much... ... I have updated the function to take an array view, which makes it slightly easier to call in a couple of places where we already have a gdb::bytes_vector. I've then added an inline wrapper to convert a raw pointer and length into an array view, which is used in places where we don't easily have a gdb::bytes_vector (or similar). Updated all users of displaced_step_dump_bytes. There should be no user visible changes after this commit. --- gdb/amd64-tdep.c | 2 +- gdb/displaced-stepping.c | 3 +-- gdb/i386-tdep.c | 2 +- gdb/infrun.c | 24 ++---------------------- gdb/infrun.h | 3 --- gdb/rs6000-tdep.c | 2 +- gdb/s390-tdep.c | 2 +- gdbsupport/array-view.h | 1 + gdbsupport/common-utils.cc | 18 ++++++++++++++++++ gdbsupport/common-utils.h | 15 +++++++++++++++ 10 files changed, 41 insertions(+), 31 deletions(-) diff --git a/gdb/amd64-tdep.c b/gdb/amd64-tdep.c index 81665e52d29..228b7518cb0 100644 --- a/gdb/amd64-tdep.c +++ b/gdb/amd64-tdep.c @@ -1526,7 +1526,7 @@ amd64_displaced_step_copy_insn (struct gdbarch *gdbarch, displaced_debug_printf ("copy %s->%s: %s", paddress (gdbarch, from), paddress (gdbarch, to), - displaced_step_dump_bytes (buf, len).c_str ()); + bytes_to_string (buf, len).c_str ()); /* This is a work around for a problem with g++ 4.8. */ return displaced_step_copy_insn_closure_up (dsc.release ()); diff --git a/gdb/displaced-stepping.c b/gdb/displaced-stepping.c index 9f98ea8c35b..534e031a88e 100644 --- a/gdb/displaced-stepping.c +++ b/gdb/displaced-stepping.c @@ -122,8 +122,7 @@ displaced_step_buffers::prepare (thread_info *thread, CORE_ADDR &displaced_pc) displaced_debug_printf ("saved %s: %s", paddress (arch, buffer->addr), - displaced_step_dump_bytes - (buffer->saved_copy.data (), len).c_str ()); + bytes_to_string (buffer->saved_copy).c_str ()); /* Save this in a local variable first, so it's released if code below throws. */ diff --git a/gdb/i386-tdep.c b/gdb/i386-tdep.c index 96c04c1a3d6..e93479c35a3 100644 --- a/gdb/i386-tdep.c +++ b/gdb/i386-tdep.c @@ -830,7 +830,7 @@ i386_displaced_step_copy_insn (struct gdbarch *gdbarch, displaced_debug_printf ("%s->%s: %s", paddress (gdbarch, from), paddress (gdbarch, to), - displaced_step_dump_bytes (buf, len).c_str ()); + bytes_to_string (buf, len).c_str ()); /* This is a work around for a problem with g++ 4.8. */ return displaced_step_copy_insn_closure_up (closure.release ()); diff --git a/gdb/infrun.c b/gdb/infrun.c index 8c56a9a4dfb..b021f40ae6e 100644 --- a/gdb/infrun.c +++ b/gdb/infrun.c @@ -1725,24 +1725,6 @@ displaced_step_reset (displaced_step_thread_state *displaced) using displaced_step_reset_cleanup = FORWARD_SCOPE_EXIT (displaced_step_reset); -/* See infrun.h. */ - -std::string -displaced_step_dump_bytes (const gdb_byte *buf, size_t len) -{ - std::string ret; - - for (size_t i = 0; i < len; i++) - { - if (i == 0) - ret += string_printf ("%02x", buf[i]); - else - ret += string_printf (" %02x", buf[i]); - } - - return ret; -} - /* Prepare to single-step, using displaced stepping. Note that we cannot use displaced stepping when we have a signal to @@ -1820,8 +1802,7 @@ displaced_step_prepare_throw (thread_info *tp) gdb::byte_vector insn_buf (dislen); read_memory (original_pc, insn_buf.data (), insn_buf.size ()); - std::string insn_bytes - = displaced_step_dump_bytes (insn_buf.data (), insn_buf.size ()); + std::string insn_bytes = bytes_to_string (insn_buf); displaced_debug_printf ("original insn %s: %s \t %s", paddress (gdbarch, original_pc), @@ -1902,8 +1883,7 @@ displaced_step_prepare_throw (thread_info *tp) gdb::byte_vector insn_buf (dislen); read_memory (addr, insn_buf.data (), insn_buf.size ()); - std::string insn_bytes - = displaced_step_dump_bytes (insn_buf.data (), insn_buf.size ()); + std::string insn_bytes = bytes_to_string (insn_buf); std::string insn_str = tmp_stream.release (); displaced_debug_printf ("replacement insn %s: %s \t %s", paddress (gdbarch, addr), diff --git a/gdb/infrun.h b/gdb/infrun.h index 5219063586d..9b3c8962939 100644 --- a/gdb/infrun.h +++ b/gdb/infrun.h @@ -270,9 +270,6 @@ extern void update_signals_program_target (void); $_exitsignal. */ extern void clear_exit_convenience_vars (void); -/* Dump LEN bytes at BUF in hex to a string and return it. */ -extern std::string displaced_step_dump_bytes (const gdb_byte *buf, size_t len); - extern void update_observer_mode (void); extern void signal_catch_update (const unsigned int *); diff --git a/gdb/rs6000-tdep.c b/gdb/rs6000-tdep.c index 52dcc89b2df..8e37c3d5183 100644 --- a/gdb/rs6000-tdep.c +++ b/gdb/rs6000-tdep.c @@ -940,7 +940,7 @@ ppc_displaced_step_copy_insn (struct gdbarch *gdbarch, displaced_debug_printf ("copy %s->%s: %s", paddress (gdbarch, from), paddress (gdbarch, to), - displaced_step_dump_bytes (buf, len).c_str ()); + bytes_to_string (buf, len).c_str ()); /* This is a work around for a problem with g++ 4.8. */ return displaced_step_copy_insn_closure_up (closure.release ()); diff --git a/gdb/s390-tdep.c b/gdb/s390-tdep.c index cab1757c5ab..081a8b68867 100644 --- a/gdb/s390-tdep.c +++ b/gdb/s390-tdep.c @@ -469,7 +469,7 @@ s390_displaced_step_copy_insn (struct gdbarch *gdbarch, displaced_debug_printf ("copy %s->%s: %s", paddress (gdbarch, from), paddress (gdbarch, to), - displaced_step_dump_bytes (buf, len).c_str ()); + bytes_to_string (buf, len).c_str ()); /* This is a work around for a problem with g++ 4.8. */ return displaced_step_copy_insn_closure_up (closure.release ()); diff --git a/gdbsupport/array-view.h b/gdbsupport/array-view.h index 3d8248b08b7..d07c8bc53fc 100644 --- a/gdbsupport/array-view.h +++ b/gdbsupport/array-view.h @@ -21,6 +21,7 @@ #include "traits.h" #include #include +#include "gdbsupport/gdb_assert.h" /* An array_view is an abstraction that provides a non-owning view over a sequence of contiguous objects. diff --git a/gdbsupport/common-utils.cc b/gdbsupport/common-utils.cc index e382fb28d8f..8e13d2f218b 100644 --- a/gdbsupport/common-utils.cc +++ b/gdbsupport/common-utils.cc @@ -445,3 +445,21 @@ hex2bin (const char *hex) return bin; } + +/* See gdbsupport/common-utils.h. */ + +std::string +bytes_to_string (gdb::array_view bytes) +{ + std::string ret; + + for (size_t i = 0; i < bytes.size (); i++) + { + if (i == 0) + ret += string_printf ("%02x", bytes[i]); + else + ret += string_printf (" %02x", bytes[i]); + } + + return ret; +} diff --git a/gdbsupport/common-utils.h b/gdbsupport/common-utils.h index 97dcb9fa8ce..8ee4549a43d 100644 --- a/gdbsupport/common-utils.h +++ b/gdbsupport/common-utils.h @@ -24,6 +24,7 @@ #include #include "gdbsupport/byte-vector.h" #include "gdbsupport/gdb_unique_ptr.h" +#include "gdbsupport/array-view.h" #include "poison.h" #include "gdb_string_view.h" @@ -194,6 +195,20 @@ extern int hex2bin (const char *hex, gdb_byte *bin, int count); /* Like the above, but return a gdb::byte_vector. */ gdb::byte_vector hex2bin (const char *hex); +/* Build a string containing the contents of BYTES. Each byte is + represented as a 2 character hex string, with spaces separating each + individual byte. */ + +extern std::string bytes_to_string (gdb::array_view bytes); + +/* See bytes_to_string above. This takes a BUFFER pointer and LENGTH + rather than an array view. */ + +static inline std::string bytes_to_string (gdb_byte *buffer, size_t length) +{ + return bytes_to_string ({buffer, length}); +} + /* A fast hashing function. This can be used to hash data in a fast way when the length is known. If no fast hashing library is available, falls back to iterative_hash from libiberty. START_VALUE can be set to