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.129.124]) by sourceware.org (Postfix) with ESMTPS id DBF40385840E for ; Thu, 16 Mar 2023 16:40:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DBF40385840E 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=1678984854; 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=bSzoFDOSRTTLmHy+8Gz6TpcRLXMFuzGOq/FiPRqh9CQ=; b=Asp2YcdWnApEi49sVy3LgF0DoMrjslswGoakoNErGY+tQpBCRvZ3EFdVZFeRv89mLcTfN9 OxTSj1LXnEBzKv42+VomBPw122255KklogmS7eK1WFsSM9ACu4MvMar5gvtArrb6DoFaY7 ALgRO9pzIufot0MWlNsss+DsDinXsoc= Received: from mail-ed1-f72.google.com (mail-ed1-f72.google.com [209.85.208.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-9-V95iLMIFP86YSyfk90R9Dg-1; Thu, 16 Mar 2023 12:40:53 -0400 X-MC-Unique: V95iLMIFP86YSyfk90R9Dg-1 Received: by mail-ed1-f72.google.com with SMTP id b1-20020aa7dc01000000b004ad062fee5eso3812522edu.17 for ; Thu, 16 Mar 2023 09:40:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678984852; 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=bSzoFDOSRTTLmHy+8Gz6TpcRLXMFuzGOq/FiPRqh9CQ=; b=0opKD4+BX1JQaHcIJw3l+FYFUBEh70K7/BvdmqHCsOrEI+YQamuCHXjuunNgyVycAH F30wA30vl/q5lmMjvnrP2bSv2y/0u+Q8GFfYVzOuGpXsFeUti0gVg3wrW7sI3/ctyr/d JpwVu5jyKIbk+NnINEvHKOykRdrtUewFSL2543MUuhW2eM4uwVCIeayzYwNqfIS/5OKw UfXEptgPpkY1Y0b2RbmLkwoAgTwkpxVjsmEJKokgdd694lCMO5icPv/NmUJSurIGm97C L+YVfHpPqObvTYQyBGRH8ffuvjDwVt1cww3SIOb7+4aEt9Lm1wl8UWHRqu1B0ijg+8vJ U6aw== X-Gm-Message-State: AO0yUKWYgCCpwQjyE8KVzHcXPaNev9R5sFyYNDOposyVIWArs5li0lbs B3RACRFlQG80J+swkGF5wvpg4EeAA1onpQ2k1kRjiNt8/KwNooxHmBU8dnxg32fXRaUZaoLtm8I bcC5ujgg5a1dc1f9jMPm8sZ8tuRHfuZ6BovC5ql0mxAIjTBQG0AckrQC3xKGAeL6t3+NxV0puwL p75nTHwg== X-Received: by 2002:a17:906:7189:b0:92d:6078:3878 with SMTP id h9-20020a170906718900b0092d60783878mr3354007ejk.33.1678984851724; Thu, 16 Mar 2023 09:40:51 -0700 (PDT) X-Google-Smtp-Source: AK7set/GgbPUOD8lmfGyNFrbDr0uWVWh2ls+uRXOrbfetKgAaDZXmx1jd/4dqHEdONa2tOFaRTP1UA== X-Received: by 2002:a17:906:7189:b0:92d:6078:3878 with SMTP id h9-20020a170906718900b0092d60783878mr3353975ejk.33.1678984851278; Thu, 16 Mar 2023 09:40:51 -0700 (PDT) Received: from localhost (95.72.115.87.dyn.plus.net. [87.115.72.95]) by smtp.gmail.com with ESMTPSA id f6-20020a170906738600b008ea8effe947sm4022001ejl.225.2023.03.16.09.40.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Mar 2023 09:40:50 -0700 (PDT) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCHv2 1/4] gdb: more debug output for displaced stepping Date: Thu, 16 Mar 2023 16:39:29 +0000 Message-Id: <09efdbfc7f9f0bf0402c222237c85f837b011082.1678984664.git.aburgess@redhat.com> 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,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 List-Id: While investigating a displaced stepping issue I wanted an easy way to see what GDB thought the original instruction was, and what instruction GDB replaced that with when performing the displaced step. We do print out the address that is being stepped, so I can track down the original instruction. And we do print out the bytes of the new instruction, so I can figure out what the replacement instruction was, but it's not really easy. In this commit, within displaced_step_buffers::prepare, I add new debug output which disassembles both the original instruction, and the replacement instruction, and prints both these instructions, along with the bytes that make these instructions, to the debug output. This new debug improved on some debug that was already present in infrun.c, however, the old infrun.c debug (a) didn't actually disassemble the instruction, it just printed the bytes, and (b) there was an assumption that all instructions are 4-bytes long, which clearly isn't correct. I think it makes more sense to have all this debug in the displaced-stepping.c file, so I've removed the infrun.c code, and added my new debug in displaced-stepping.c. Here's an example of what the output looks like on x86-64 (this is with 'set debug displaced on'). The two interesting lines contain the strings 'original insn' and 'replacement insn': (gdb) step [displaced] displaced_step_prepare_throw: displaced-stepping 2073893.2073893.0 now [displaced] prepare: selected buffer at 0x401052 [displaced] prepare: saved 0x401052: 1e fa 31 ed 49 89 d1 5e 48 89 e2 48 83 e4 f0 50 [displaced] prepare: original insn 0x401030: ff 25 e2 2f 00 00 jmp *0x2fe2(%rip) # 0x404018 [displaced] fixup_riprel: %rip-relative addressing used. [displaced] fixup_riprel: using temp reg 2, old value 0x7ffff7f8a578, new value 0x401036 [displaced] amd64_displaced_step_copy_insn: copy 0x401030->0x401052: ff a1 e2 2f 00 00 68 00 00 00 00 e9 e0 ff ff ff [displaced] prepare: replacement insn 0x401052: ff a1 e2 2f 00 00 jmp *0x2fe2(%rcx) [displaced] displaced_step_prepare_throw: prepared successfully thread=2073893.2073893.0, original_pc=0x401030, displaced_pc=0x401052 [displaced] finish: restored 2073893.2073893.0 0x401052 [displaced] amd64_displaced_step_fixup: fixup (0x401030, 0x401052), insn = 0xff 0xa1 ... [displaced] amd64_displaced_step_fixup: restoring reg 2 to 0x7ffff7f8a578 0x00007ffff7e402c0 in puts () from /lib64/libc.so.6 (gdb) One final note. For many targets that support displaced stepping (in fact all targets except ARM) the replacement instruction is always a single instruction. But on ARM the replacement could actually be a series of instructions. The debug code tries to handle this by disassembling the entire displaced stepping buffer. Obviously this might actually print more than is necessary, but there's (currently) no easy way to know how many instructions to disassemble; that knowledge is all locked in the architecture specific code. Still I don't think it really hurts, if someone is looking at this debug then hopefully they known what to expect. --- gdb/displaced-stepping.c | 72 ++++++++++++++++++++++++++++++++++++++++ gdb/displaced-stepping.h | 3 ++ gdb/infrun.c | 35 ------------------- gdb/infrun.h | 3 -- 4 files changed, 75 insertions(+), 38 deletions(-) diff --git a/gdb/displaced-stepping.c b/gdb/displaced-stepping.c index 9f98ea8c35b..5b94b2f068d 100644 --- a/gdb/displaced-stepping.c +++ b/gdb/displaced-stepping.c @@ -28,6 +28,7 @@ #include "inferior.h" #include "regcache.h" #include "target/target.h" +#include "disasm.h" /* Default destructor for displaced_step_copy_insn_closure. */ @@ -43,6 +44,24 @@ show_debug_displaced (struct ui_file *file, int from_tty, gdb_printf (file, _("Displace stepping debugging is %s.\n"), value); } +/* See displaced-stepping.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; +} + displaced_step_prepare_status displaced_step_buffers::prepare (thread_info *thread, CORE_ADDR &displaced_pc) { @@ -125,6 +144,26 @@ displaced_step_buffers::prepare (thread_info *thread, CORE_ADDR &displaced_pc) displaced_step_dump_bytes (buffer->saved_copy.data (), len).c_str ()); + /* Display the instruction we are going to copy. */ + if (debug_displaced) + { + string_file tmp_stream; + int dislen = gdb_print_insn (arch, buffer->original_pc, &tmp_stream, + nullptr); + + gdb::byte_vector insn_buf (dislen); + read_memory (buffer->original_pc, insn_buf.data (), insn_buf.size ()); + + std::string insn_bytes + = displaced_step_dump_bytes (insn_buf.data (), insn_buf.size ()); + /* Extra spaces at the start of this debug line ensure alignment with + the 'replacement insn' line below. */ + displaced_debug_printf (" original insn %s: %s\t%s", + paddress (arch, buffer->original_pc), + insn_bytes.c_str (), + tmp_stream.string ().c_str ()); + } + /* Save this in a local variable first, so it's released if code below throws. */ displaced_step_copy_insn_closure_up copy_insn_closure @@ -139,6 +178,39 @@ displaced_step_buffers::prepare (thread_info *thread, CORE_ADDR &displaced_pc) return DISPLACED_STEP_PREPARE_STATUS_CANT; } + /* Display the new displaced instruction(s). */ + if (debug_displaced) + { + string_file tmp_stream; + CORE_ADDR addr = buffer->addr; + + /* If displaced stepping is going to use h/w single step then we know + that the replacement instruction can only be a single instruction, + in that case set the end address at the next byte. + + Otherwise the displaced stepping copy instruction routine could + have generated multiple instructions, and all we know is that they + must fit within the LEN bytes of the buffer. */ + CORE_ADDR end + = addr + (gdbarch_displaced_step_hw_singlestep (arch) ? 1 : len); + + while (addr < end) + { + int dislen = gdb_print_insn (arch, addr, &tmp_stream, nullptr); + + gdb::byte_vector insn_buf (dislen); + read_memory (buffer->addr, insn_buf.data (), insn_buf.size ()); + + std::string insn_bytes + = displaced_step_dump_bytes (insn_buf.data (), insn_buf.size ()); + std::string insn_str = tmp_stream.release (); + displaced_debug_printf ("replacement insn %s: %s\t%s", + paddress (arch, addr), insn_bytes.c_str (), + insn_str.c_str ()); + addr += dislen; + } + } + /* This marks the buffer as being in use. */ buffer->current_thread = thread; diff --git a/gdb/displaced-stepping.h b/gdb/displaced-stepping.h index e154927ad92..b3c5cdb1109 100644 --- a/gdb/displaced-stepping.h +++ b/gdb/displaced-stepping.h @@ -207,4 +207,7 @@ struct displaced_step_buffers std::vector m_buffers; }; +/* 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); + #endif /* DISPLACED_STEPPING_H */ diff --git a/gdb/infrun.c b/gdb/infrun.c index 33aa0c8794b..e73e8f16902 100644 --- a/gdb/infrun.c +++ b/gdb/infrun.c @@ -1724,24 +1724,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 @@ -2710,23 +2692,6 @@ resume_1 (enum gdb_signal sig) step = false; } - if (debug_displaced - && tp->control.trap_expected - && use_displaced_stepping (tp) - && !step_over_info_valid_p ()) - { - struct regcache *resume_regcache = get_thread_regcache (tp); - struct gdbarch *resume_gdbarch = resume_regcache->arch (); - CORE_ADDR actual_pc = regcache_read_pc (resume_regcache); - gdb_byte buf[4]; - - read_memory (actual_pc, buf, sizeof (buf)); - displaced_debug_printf ("run %s: %s", - paddress (resume_gdbarch, actual_pc), - displaced_step_dump_bytes - (buf, sizeof (buf)).c_str ()); - } - if (tp->control.may_range_step) { /* If we're resuming a thread with the PC out of the step 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 *); -- 2.25.4