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 2D5D83858C50 for ; Tue, 28 Mar 2023 15:08:53 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2D5D83858C50 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=1680016132; 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: in-reply-to:in-reply-to:references:references; bh=ZdYHgi2YI/O+zNtGBO5jgTGNQLacS2t5ymv0gYPufhk=; b=A93rVruspt0if+KMFDAgmC7YRRBryXM0ylYdqjqpN8W9GlZJMsgYdOBxWfk8X+SDunG+J6 /b6TsW4N7K0S/b6mWv151FeCtwpjemEI6s1Vf12FeiokX0pojPz3qyFLtxFL/1t18wx4so AIMEYN6rY/R2NwadWa9aF40QcwHICa4= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-493-yvzemdxdN2m98PB7AuAbug-1; Tue, 28 Mar 2023 11:08:51 -0400 X-MC-Unique: yvzemdxdN2m98PB7AuAbug-1 Received: by mail-wm1-f69.google.com with SMTP id bi5-20020a05600c3d8500b003edda1368d7so6586075wmb.8 for ; Tue, 28 Mar 2023 08:08:51 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680016130; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ZdYHgi2YI/O+zNtGBO5jgTGNQLacS2t5ymv0gYPufhk=; b=vnJWd3MQk0zxJBujPrVkZc0Uveu8Qmvw0DHu4fhPsUHe4DgvNug/VpYk6IbtE4EUEa t+E5kHWcdtElTabs3oZgCsKVmlA5jlApqpTNx489be2UyR6lcoDWhBtbfTZBlR1oEXWb hj04hUt1KMYtsLBtetX/Oemh+E33MVdbWc7dIs9DTGkNC5VANP7EH9DLwUvIH+wDxRPO dA9T9SJdw6cphiLeB1lon3K2vYiwaqaoSE1vedS/fPk9PoQzV6Wvtgi7I93Do3p60S0O a0BIJ3ZMY0wddwWJMh9Jk2SPO1qPn8PJrXY31Xqm3BvjZG+dXmMd5jGLzKg8KLBRAX8+ nANA== X-Gm-Message-State: AO0yUKU7FXwkoN7bmZ7W9eVHlzu2n4vR+QT59GdV3RBms7hIgv6SkfGT NxXaI2M5/qIx+Be3cEV30H83b7zR+1gIfNUix1cHuWmkBFDrUS2Eyj/Xo1HxvxSgecnE83T6Rm3 YS89NP2kI3WDKRmZzBEnSuw7NWmsszQ== X-Received: by 2002:a05:600c:2909:b0:3eb:383c:1870 with SMTP id i9-20020a05600c290900b003eb383c1870mr13010817wmd.11.1680016129932; Tue, 28 Mar 2023 08:08:49 -0700 (PDT) X-Google-Smtp-Source: AK7set8eiSWrTFram2yiFH/rJkEewIygS29BRuQRcDn+RZzvAOpyfDsKqmAUXxPRo8/cE4g231t2Kg== X-Received: by 2002:a05:600c:2909:b0:3eb:383c:1870 with SMTP id i9-20020a05600c290900b003eb383c1870mr13010787wmd.11.1680016129489; Tue, 28 Mar 2023 08:08:49 -0700 (PDT) Received: from localhost (95.72.115.87.dyn.plus.net. [87.115.72.95]) by smtp.gmail.com with ESMTPSA id m31-20020a05600c3b1f00b003ef5bb63f13sm13060516wms.10.2023.03.28.08.08.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Mar 2023 08:08:49 -0700 (PDT) From: Andrew Burgess To: Simon Marchi , gdb-patches@sourceware.org Cc: Pedro Alves Subject: Re: [PATCHv3 1/3] gdb: more debug output for displaced stepping In-Reply-To: <6e0638df-b0b1-29e8-a9ba-acf091f717c5@simark.ca> References: <20744f2c843ca8bffb773634350b8479a58c05e5.1679919937.git.aburgess@redhat.com> <6e0638df-b0b1-29e8-a9ba-acf091f717c5@simark.ca> Date: Tue, 28 Mar 2023 16:08:47 +0100 Message-ID: <87h6u4eww0.fsf@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain 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: Simon Marchi writes: > On 3/27/23 08:32, Andrew Burgess wrote: >> 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, I just need to go find the information >> myself. >> >> 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. >> >> Also, the code that prints the bytes of the replacement instruction >> only prints 4 bytes, which clearly isn't always going to be correct. >> >> In this commit I remove the existing code that prints the bytes of the >> replacement instruction, and add two new blocks of code to >> displaced_step_prepare_throw. This new code prints the original >> instruction, and the replacement instruction. In each case we print >> both the bytes that make up the instruction and the completely >> disassembled instruction. >> >> 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 2892655.2892655.0 now >> [displaced] displaced_step_prepare_throw: original insn 0x401030: ff 25 e2 2f 00 00 jmp *0x2fe2(%rip) # 0x404018 >> [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] 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] displaced_step_prepare_throw: prepared successfully thread=2892655.2892655.0, original_pc=0x401030, displaced_pc=0x401052 >> [displaced] displaced_step_prepare_throw: replacement insn 0x401052: ff a1 e2 2f 00 00 jmp *0x2fe2(%rcx) >> [displaced] finish: restored 2892655.2892655.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. >> >> Obviously we can imagine schemes where the architecture specific >> displaced stepping code could communicate back how many bytes its >> replacement sequence was, and then our debug print code could use this >> to limit the disassembly. But this seems like a lot of effort just to >> save printing a few additional instructions in some debug output. >> >> I'm not proposing to do anything about this issue for now. >> --- >> gdb/infrun.c | 85 +++++++++++++++++++++++++++++++++++++++++----------- >> 1 file changed, 68 insertions(+), 17 deletions(-) >> >> diff --git a/gdb/infrun.c b/gdb/infrun.c >> index 5c9babb9104..8c56a9a4dfb 100644 >> --- a/gdb/infrun.c >> +++ b/gdb/infrun.c >> @@ -74,6 +74,7 @@ >> #include "gdbsupport/common-debug.h" >> #include "gdbsupport/buildargv.h" >> #include "extension.h" >> +#include "disasm.h" >> >> /* Prototypes for local functions */ >> >> @@ -1807,6 +1808,31 @@ displaced_step_prepare_throw (thread_info *tp) >> CORE_ADDR original_pc = regcache_read_pc (regcache); >> CORE_ADDR displaced_pc; >> >> + /* Display the instruction we are going to displaced step. */ >> + if (debug_displaced) >> + { >> + string_file tmp_stream; >> + int dislen = gdb_print_insn (gdbarch, original_pc, &tmp_stream, >> + nullptr); >> + >> + if (dislen > 0) >> + { >> + 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 ()); >> + >> + displaced_debug_printf ("original insn %s: %s \t %s", >> + paddress (gdbarch, original_pc), >> + insn_bytes.c_str (), >> + tmp_stream.string ().c_str ()); > > If the bytes disassemble to more than one instruction, does tmp_stream > contain new lines characters? Just wondering what the output would look > like (not a big deal in any case). No. gdb_print_insn will only disassemble a single instruction and return its length. In this bit of debug, we assume the original instruction is always a single instruction. If that's not true then I've seriously not understood how displaced stepping works. For the replacement instructions the call to gdb_print_insn is placed inside a loop which calls gdb_print_insn multiple times, so you'll see multiple lines like: [displaced] displaced_step_prepare_throw: replacement insn
: I use this trick: CORE_ADDR end = addr + (gdbarch_displaced_step_hw_singlestep (gdbarch) ? 1 : gdbarch_displaced_step_buffer_length (gdbarch)); Which means for targets that do a 1:1 replacement we only disassemble a single instruction. But for everyone else we'll always disassemble the entire displaced step buffer. Currently this is just ARM. > > Approved-By: Simon Marchi Thanks, Andrew