From: John Baldwin <jhb@FreeBSD.org>
To: Andrew Burgess <aburgess@redhat.com>, gdb-patches@sourceware.org
Subject: Re: [PATCHv2 6/6] gdb: native target invalid architecture detection
Date: Fri, 10 Jun 2022 09:20:25 -0700 [thread overview]
Message-ID: <4f00e7ae-18b7-5e61-af47-f1c603852103@FreeBSD.org> (raw)
In-Reply-To: <644591dc859ea741999f73aaa28a1bffa83ceab5.1654866188.git.aburgess@redhat.com>
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<TDepType *> (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<i386_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
next prev parent reply other threads:[~2022-06-10 16:20 UTC|newest]
Thread overview: 83+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-31 14:30 [PATCH 0/5] Handle trying to use a native target with the wrong binary Andrew Burgess
2022-05-31 14:30 ` [PATCH 1/5] gdb/arm: move fetch of arm_gdbarch_tdep to a more inner scope Andrew Burgess
2022-06-01 7:58 ` Luis Machado
2022-05-31 14:30 ` [PATCH 2/5] gdb/mips: rewrite show_mask_address Andrew Burgess
2022-05-31 14:30 ` [PATCH 3/5] gdb: move the type cast into gdbarch_tdep Andrew Burgess
2022-06-01 8:01 ` Luis Machado
2022-05-31 14:30 ` [PATCH 4/5] gdb: ensure the cast in gdbarch_tdep is valid Andrew Burgess
2022-05-31 16:04 ` John Baldwin
2022-05-31 17:22 ` Andrew Burgess
2022-05-31 14:30 ` [PATCH 5/5] gdb: native target invalid architecture detection Andrew Burgess
2022-05-31 16:08 ` John Baldwin
2022-05-31 16:51 ` Andrew Burgess
2022-06-01 8:25 ` Luis Machado
2022-06-01 21:06 ` John Baldwin
2022-06-01 21:21 ` Christophe Lyon
2022-06-02 14:56 ` John Baldwin
2022-06-06 14:38 ` Andrew Burgess
2022-06-06 17:48 ` Andrew Burgess
2022-06-07 11:03 ` Luis Machado
2022-06-07 18:42 ` Pedro Alves
2022-06-07 20:15 ` Pedro Alves
2022-06-08 8:18 ` Luis Machado
2022-06-08 10:17 ` Pedro Alves
2022-06-08 7:54 ` Luis Machado
2022-06-08 10:12 ` Pedro Alves
2022-06-08 11:20 ` [PATCH v2] aarch64: Add fallback if ARM_CC_FOR_TARGET not set (was: Re: [PATCH 5/5] gdb: native target invalid architecture detection) Pedro Alves
2022-06-08 12:50 ` Luis Machado
2022-06-08 13:23 ` Pedro Alves
2022-06-08 13:38 ` Andrew Burgess
2022-06-08 19:01 ` John Baldwin
2022-06-08 21:48 ` Pedro Alves
2022-06-09 16:31 ` John Baldwin
2022-06-10 13:08 ` [PATCHv2 0/6] Handle trying to use a native target with the wrong binary Andrew Burgess
2022-06-10 13:08 ` [PATCHv2 1/6] gdb/arm: move fetch of arm_gdbarch_tdep to a more inner scope Andrew Burgess
2022-06-10 13:08 ` [PATCHv2 2/6] gdb/mips: rewrite show_mask_address Andrew Burgess
2022-06-10 13:08 ` [PATCHv2 3/6] gdb/arm: avoid undefined behaviour in arm_frame_is_thumb Andrew Burgess
2022-06-10 15:21 ` Luis Machado
2022-06-10 15:49 ` Andrew Burgess
2022-06-10 16:29 ` Luis Machado
2022-06-10 13:08 ` [PATCHv2 4/6] gdb: move the type cast into gdbarch_tdep Andrew Burgess
2022-06-10 16:35 ` Luis Machado
2022-06-10 13:08 ` [PATCHv2 5/6] gdb: ensure the cast in gdbarch_tdep is valid Andrew Burgess
2022-06-10 13:08 ` [PATCHv2 6/6] gdb: native target invalid architecture detection Andrew Burgess
2022-06-10 16:20 ` John Baldwin [this message]
2022-06-10 16:31 ` Luis Machado
2022-06-13 16:15 ` [PATCHv3 0/6] Handle trying to use a native target with the wrong binary Andrew Burgess
2022-06-13 16:15 ` [PATCHv3 1/6] gdb/arm: move fetch of arm_gdbarch_tdep to a more inner scope Andrew Burgess
2022-06-13 16:15 ` [PATCHv3 2/6] gdb/mips: rewrite show_mask_address Andrew Burgess
2022-06-13 16:15 ` [PATCHv3 3/6] gdb: select suitable thread for gdbarch_adjust_breakpoint_address Andrew Burgess
2022-06-14 9:45 ` Luis Machado
2022-06-14 14:05 ` Andrew Burgess
2022-06-24 16:58 ` Pedro Alves
2022-06-13 16:15 ` [PATCHv3 4/6] gdb: move the type cast into gdbarch_tdep Andrew Burgess
2022-06-13 16:15 ` [PATCHv3 5/6] gdb: ensure the cast in gdbarch_tdep is valid Andrew Burgess
2022-06-24 18:15 ` Pedro Alves
2022-06-13 16:15 ` [PATCHv3 6/6] gdb: native target invalid architecture detection Andrew Burgess
2022-06-24 19:23 ` Pedro Alves
2022-06-27 16:27 ` Andrew Burgess
2022-06-27 21:38 ` Pedro Alves
2022-06-28 10:37 ` Andrew Burgess
2022-06-28 12:42 ` [PATCH v2] gdb+gdbserver/Linux: avoid reading registers while going through shell (was: Re: [PATCHv3 6/6] gdb: native target invalid architecture detection) Pedro Alves
2022-06-28 14:21 ` Andrew Burgess
2022-06-29 15:17 ` Simon Marchi
2022-06-29 16:22 ` [PATCH] Fix GDBserver regression due to change to avoid reading shell registers Pedro Alves
2022-06-29 16:38 ` Simon Marchi
2022-06-30 9:33 ` [PATCHv3 6/6] gdb: native target invalid architecture detection Andrew Burgess
2022-06-30 11:44 ` Pedro Alves
2022-07-11 10:47 ` Andrew Burgess
2022-06-24 10:15 ` [PATCHv3 0/6] Handle trying to use a native target with the wrong binary Andrew Burgess
2022-06-28 14:28 ` [PATCHv4 0/6] Detect invalid casts of gdbarch_tdep structures Andrew Burgess
2022-06-28 14:28 ` [PATCHv4 1/6] gdb/arm: move fetch of arm_gdbarch_tdep to a more inner scope Andrew Burgess
2022-06-28 14:28 ` [PATCHv4 2/6] gdb/mips: rewrite show_mask_address Andrew Burgess
2022-06-28 14:28 ` [PATCHv4 3/6] gdb: select suitable thread for gdbarch_adjust_breakpoint_address Andrew Burgess
2022-06-28 14:28 ` [PATCHv4 4/6] gdb: move the type cast into gdbarch_tdep Andrew Burgess
2022-06-28 14:28 ` [PATCHv4 5/6] gdbsupport: add checked_static_cast Andrew Burgess
2022-06-28 14:28 ` [PATCHv4 6/6] gdb: ensure the cast in gdbarch_tdep is valid Andrew Burgess
2022-07-11 10:46 ` [PATCHv4 0/6] Detect invalid casts of gdbarch_tdep structures Andrew Burgess
2022-07-21 18:21 ` Andrew Burgess
2022-07-22 0:50 ` Luis Machado
2022-07-23 0:02 ` [PATCH] Rename gdbarch_tdep template function to gdbarch_tdep_cast for g++ 4.8 Mark Wielaard
2022-07-25 11:19 ` Andrew Burgess
2022-07-25 11:27 ` Mark Wielaard
2022-07-26 11:05 ` Andrew Burgess
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4f00e7ae-18b7-5e61-af47-f1c603852103@FreeBSD.org \
--to=jhb@freebsd.org \
--cc=aburgess@redhat.com \
--cc=gdb-patches@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).