From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx2.freebsd.org (mx2.freebsd.org [IPv6:2610:1c1:1:606c::19:2]) by sourceware.org (Postfix) with ESMTPS id E193A385736D for ; Fri, 10 Jun 2022 16:20:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org E193A385736D Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=FreeBSD.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [96.47.72.80]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits)) (Client CN "mx1.freebsd.org", Issuer "R3" (verified OK)) by mx2.freebsd.org (Postfix) with ESMTPS id C3E907CFD0; Fri, 10 Jun 2022 16:20:27 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Received: from smtp.freebsd.org (smtp.freebsd.org [IPv6:2610:1c1:1:606c::24b:4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "smtp.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4LKR5R4vzHz4Sqd; Fri, 10 Jun 2022 16:20:27 +0000 (UTC) (envelope-from jhb@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1654878027; 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: in-reply-to:in-reply-to:references:references; bh=Ek+LBnTnWrfDTeV9JRp6EQAPaCQp/vrzNDgPPzwyLUs=; b=PjQjD4JsuOmEJG8MJQ2TlZDedwpLRlUfJQMvB8j5fK1FJ5yEEYc4LKBZFDNTWzRgzpAiFz C6eCjAlx0jwdRT0NPSYcwkv3achrZE6YjWxBeQrPUdM1H4TS6ojQlgdI5NC1M0R5znNSx0 chSrqgBaAc7oGDEUlaPDxPf/EjQt4zBU40FzC+henPTLjeo9MPZlRZbCbUhDd2yQkrDV8g AGKy5g+kp3v4WCBvJHZqM2qqg1hto3JBrDaWO98+ndthukQLRsC16hX786adnogfri+kpv 4pVaMX8bCnWuYd+WxOgD1mvMdYxcEe/60mYMpy1vIFkRwKkewjEbFhBwCaRHig== Received: from [IPV6:2601:648:8680:ed60:99d0:1b32:8e6e:2ffa] (unknown [IPv6:2601:648:8680:ed60:99d0:1b32:8e6e:2ffa]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) (Authenticated sender: jhb) by smtp.freebsd.org (Postfix) with ESMTPSA id 14B742EB93; Fri, 10 Jun 2022 16:20:26 +0000 (UTC) (envelope-from jhb@FreeBSD.org) Message-ID: <4f00e7ae-18b7-5e61-af47-f1c603852103@FreeBSD.org> Date: Fri, 10 Jun 2022 09:20:25 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.9.0 Subject: Re: [PATCHv2 6/6] gdb: native target invalid architecture detection Content-Language: en-US To: Andrew Burgess , gdb-patches@sourceware.org References: <644591dc859ea741999f73aaa28a1bffa83ceab5.1654866188.git.aburgess@redhat.com> From: John Baldwin In-Reply-To: <644591dc859ea741999f73aaa28a1bffa83ceab5.1654866188.git.aburgess@redhat.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1654878027; 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: in-reply-to:in-reply-to:references:references; bh=Ek+LBnTnWrfDTeV9JRp6EQAPaCQp/vrzNDgPPzwyLUs=; b=YFzmZ4ZHEqj9HYAkwr1CUm8rTXFAPZhsNt8UwaBgnVhCGtXlq8n5ZesiH6pO3ZqJIpDMy/ cWaVGw/GPwJrxByAmZTtm+t4MqhoA/f8j2EoK8+YJAEF/Cxkhd/avNnUGz7vBEAIHioEce IHV7AXc4VqCumsYHZljsil9hosqAWIKTPgOYgHCuPDoAnIH5n0cuHV2lKAiWYWZrkpDuo8 ADwWp3zznHRkt2FLvYYBVIl3o+iM073cIuoemdi03l16FA6rcaGc564mFZI9gLEcrOGcC9 laz3UBsHKyXKNOAMILxdHOVdWYQ+WgijdT8pbzCzlywMT1bnBlaJaN9QynP7dg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1654878027; a=rsa-sha256; cv=none; b=euwaLvYCWxMmMerAoyAYECZ0UYX/TPEeyzkjr4RpmpY1GfqssyyJPlx/ibIEkhjLKXecnB RD3p8c5oGW01RmekEv9ILtfUlplZFaBGRYQNZIMf3wMnlqn/83GeDKXpypErEzelxmL8BA jfSXivXGhJ+1vJGbAZFkIa6hQfHR9/gM0eIeZuYzCFz+i4njbvS4sH5qSCxSoDkVnSGwqV 6wyJEBLzZSEpZvkF7axukbPJTw10fSkk5H8L4HH+CR2tkHHQcR/KHNHM13D5+G4fotQutP MXvX2bgzyhGCrmi4wWkHQflw3c2OyLtslb37DgRKbaJGp6k2UF5iF6UbilExuQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none X-Spam-Status: No, score=-5.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, NICE_REPLY_A, SPF_HELO_NONE, SPF_PASS, 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.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 10 Jun 2022 16:20:33 -0000 On 6/10/22 6:08 AM, Andrew Burgess via Gdb-patches wrote: > If GDB is asked to start a new inferior, or attach to an existing > process, using a binary file for an architecture that does not match > the current native target, then, currently, GDB will assert. Here's > an example session using current HEAD of master with GDB built for an > x86-64 GNU/Linux native target, the binary being used is a RISC-V ELF: > > $ ./gdb/gdb -q --data-directory ./gdb/data-directory/ > (gdb) file /tmp/hello.rv32imc.x > Reading symbols from /tmp/hello.rv32imc.x... > (gdb) start > Temporary breakpoint 1 at 0x101b2: file hello.rv32.c, line 23. > Starting program: /tmp/hello.rv32imc.x > ../../src/gdb/gdbarch.h:166: internal-error: gdbarch_tdep: Assertion `dynamic_cast (tdep) != nullptr' failed. > A problem internal to GDB has been detected, > further debugging may prove unreliable. > > The same error is encountered if, instead of starting a new inferior, > the user tries to attach to an x86-64 process with a RISC-V binary set > as the current executable. > > These errors are not specific to the x86-64/RISC-V pairing I'm using > here, any attempt to use a binary for one architecture with a native > target of a different architecture will result in a similar error. > > Clearly, attempting to use this cross-architecture combination is a > user error, but I think GDB should do better than an assert; ideally a > nice error should be printed. > > The problem we run into is that, when the user starts a new inferior, > or attaches to an inferior, the inferior stops. At this point GDB > attempts to handle the stop, and this involves reading registers from > the inferior. > > These register reads end up being done through the native target, so > in the example above, we end up in the amd64_supply_fxsave function. > However, these functions need a gdbarch. The gdbarch is fetched from > the register set, which was constructed using the gdbarch from the > binary currently in use. And so we end up in amd64_supply_fxsave > using a RISC-V gdbarch. > > When we call: > > i386_gdbarch_tdep *tdep = gdbarch_tdep (gdbarch); > > this will assert as the gdbarch_tdep data within the RISC-V gdbarch is > of the type riscv_gdbarch_tdep not i386_gdbarch_tdep. > > The solution I propose in this commit is to add a new target_ops > method supports_architecture_p. This method will return true if a > target can safely be used with a specific architecture, otherwise, the > method returns false. > > I imagine that a result of true from this method doesn't guarantee > that GDB can start an inferior of a given architecture, it just means > that GDB will not crash if such an attempt is made. A result of false > is a hard stop; attempting to use this target with this architecture > is not supported, and may cause GDB to crash. > > This distinction is important I think for things like remote targets, > and possibly simulator targets. We might imagine that GDB can ask a > remote (or simulator) to start with a particular executable, and the > target might still refuse for some reason. But my thinking is that > these refusals should be well handled (i.e. GDB should give a user > friendly error), rather than crashing, as is the case with the native > targets. > > For example, if I start gdbserver on an x86-64 machine like this: > > gdbserver --multi :54321 > > Then use GDB to try and load a RISC-V binary, like this: > > $ ./gdb/gdb -q --data-directory ./gdb/data-directory/ > (gdb) file /tmp/hello.rv32imc.x > Reading symbols from /tmp/hello.rv32imc.x... > (gdb) target extended-remote :54321 > Remote debugging using :54321 > (gdb) run > Starting program: /tmp/hello.rv32imc.x > Running the default executable on the remote target failed; try "set remote exec-file"? > (gdb) > > Though the error is not very helpful in diagnosing the problem, we can > see that GDB has not crashed, but has given the user an error. > > And so, the supports_architecture_p method is created to return true > by default, then I override this in inf_child_target, where I compare > the architecture in question with the default_bfd_arch. > > Finally, I've added calls to supports_architecture_p for the > run (which covers run, start, starti) and attach commands. > > This leaves just one question, what about native targets that support > multiple architectures? > > These targets can be split into two groups. First, we have targets > like x86-64, which also supports i386 binaries. This case is easy to > handle, as far as BFD is concerned there is only one architecture, > bfd_arch_i386, and we then use machine types to split this > architecture into x86-64 and i386 (and others). As the new > supports_architecture_p function only checks the bfd architecture, > then there is nothing additional needed for this case. > > The second group of multi-architecture targets requires more work. > The only targets that I'm aware of that fall into this group are the > rs6000-aix-nat.c, ppc-*-nat.c targets, and the aarch64-linux-nat.c > target. > > The first group (rs600/ppc) support bfd_arch_rs6000 and > bfd_arch_powerpc, while the second (aarch64) supports bfd_arch_arm and > bfd_arch_aarch64. > > To deal with these targets I have overridden the > supports_architecture_p function in each of the separate target files, > these overrides check both of the supported architectures. > > One final note, in the rs6000/ppc case, the FreeBSD target supports > both architectures, and so we override supports_architecture_p. In > contrast, the aarch64_fbsd_nat_target target does not (yet) support > bfd_arch_arm, and so there is no supports_architecture_p here. This > can always be added later if/when support is added. > > You will notice a lack of tests for this change. I'm not sure of a > good way that I can build a binary for a different architecture as > part of a test, but if anyone has any ideas then I'll be happy to add > a test here. The gdb.base/multi-arch.exp test exists, which for > AArch64 will test compiling and running something as both AArch64 and > ARM, but this doesn't cover the error case, just that the overridden > supports_architecture_p works in that case. Thanks, the *BSD changes all look good to me. At some point I will probably get around to fixing aarch64-fbsd-nat.c to support 32-bit arm. -- John Baldwin