Commit Graph

113287 Commits

Author SHA1 Message Date
GDB Administrator
e7a881b9d4 Automatic date update in version.in 2023-02-07 00:00:16 +00:00
Simon Marchi
f9b677528f gdb: adjust comment on target_desc_info::from_user_p
Remove the stale reference to INFO, which is now "this target
description info" now.

Change-Id: I35dbdb089048ed7cfffe730d3134ee391b176abf
2023-02-06 14:12:27 -05:00
Andrew Burgess
85df9457b7 gdb/doc: extend the documentation for the 'handle' command
The documentation for the 'handle' command does not cover all of the
features of the command, and in one case, is just wrong.

The user can specify 'all' as signal name, the documentation implies
that this will change the behaviour of all signals, in reality, this
changes all signals except SIGINT and SIGTRAP (the signals used by
GDB).  I've updated the docs to list this limitation.

The 'handle' command also allows the user to specify multiple signals
for a single command, e.g. 'handle SIGFPE SIGILL nostop pass print',
however the documentation doesn't describe this, so I've updated the
docs to describe this feature.
2023-02-06 14:56:42 +00:00
Alan Modra
8478900290 ppc32 and "LOAD segment with RWX permissions"
When using a bss-plt we'll always trigger the RWX warning, which
disturbs gcc test results.  On the other hand, there may be reason to
want the warning when gcc is configured with --enable-secureplt.
So turning off the warning entirely for powerpc might not be the best
solution.  Instead, we'll turn off the warning whenever a bss-plt is
generated, unless the user explicitly asked for the warning.

bfd/
	* elf32-ppc.c (ppc_elf_select_plt_layout): Set
	no_warn_rwx_segments on generating a bss plt, unless explicity
	enabled by the user.  Also show the bss-plt warning when
	--warn-rwx-segments is given without --bss-plt.
include/
	* bfdlink.h (struct bfd_link_info): Add user_warn_rwx_segments.
ld/
	* lexsup.c (parse_args): Set user_warn_rwx_segments.
	* testsuite/ld-elf/elf.exp: Pass --secure-plt for powerpc to
	the rwx tests.
2023-02-06 23:06:44 +10:30
Tom de Vries
9af467b824 [gdb/testsuite] Fix gdb.threads/schedlock.exp on fast cpu
Occasionally, I run into:
...
(gdb) PASS: gdb.threads/schedlock.exp: schedlock=on: cmd=continue: \
  set scheduler-locking on
continue^M
Continuing.^M
PASS: gdb.threads/schedlock.exp: schedlock=on: cmd=continue: \
  continue (with lock)
[Thread 0x7ffff746e700 (LWP 1339) exited]^M
No unwaited-for children left.^M
(gdb) Quit^M
(gdb) FAIL: gdb.threads/schedlock.exp: schedlock=on: cmd=continue: \
  stop all threads (with lock) (timeout)
...

What happens is that this loop which is supposed to run "just short of forever":
...
    /* Don't run forever.  Run just short of it :)  */
    while (*myp > 0)
      {
        /* schedlock.exp: main loop.  */
        MAYBE_CALL_SOME_FUNCTION(); (*myp) ++;
      }
...
finishes after 0x7fffffff iterations (when a signed wrap occurs), which on my
system takes only about 1.5 seconds.

Fix this by:
- changing the pointed-at type of myp from signed to unsigned, which makes the
  wrap defined behaviour (and which also make the loop run twice as long,
  which is already enough to make it impossible for me to reproduce the FAIL.
  But let's try to solve this more structurally).
- changing the pointed-at type of myp from int to long long, making the wrap
  unlikely.
- making sure the loop runs forever, by setting the loop condition to 1.
- making sure the loop still contains different lines (as far as debug info is
  concerned) by incrementing a volatile counter in the loop.
- making sure the program doesn't run forever in case of trouble, by adding an
  "alarm (30)".

Tested on x86_64-linux.

PR testsuite/30074
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30074
2023-02-06 12:52:50 +01:00
Andrew Burgess
980dbf3622 gdb: error if 'thread' or 'task' keywords are overused
When creating a breakpoint or watchpoint, the 'thread' and 'task'
keywords can be used to create a thread or task specific breakpoint or
watchpoint.

Currently, a thread or task specific breakpoint can only apply for a
single thread or task, if multiple threads or tasks are specified when
creating the breakpoint (or watchpoint), then the last specified id
will be used.

The exception to the above is that when the 'thread' keyword is used
during the creation of a watchpoint, GDB will give an error if
'thread' is given more than once.

In this commit I propose making this behaviour consistent, if the
'thread' or 'task' keywords are used more than once when creating
either a breakpoint or watchpoint, then GDB will give an error.

I haven't updated the manual, we don't explicitly say that these
keywords can be repeated, and (to me), given the keyword takes a
single id, I don't think it makes much sense to repeat the keyword.
As such, I see this more as adding a missing error to GDB, rather than
making some big change.  However, I have added an entry to the NEWS
file as I guess it is possible that some people might hit this new
error with an existing (I claim, badly written) GDB script.

I've added some new tests to check for the new error.

Just one test needed updating, gdb.linespec/keywords.exp, this test
did use the 'thread' keyword twice, and expected the breakpoint to be
created.  Looking at what this test was for though, it was checking
the use of '-force-condition', and I don't think that being able to
repeat 'thread' was actually a critical part of this test.

As such, I've updated this test to expect the error when 'thread' is
repeated.
2023-02-06 11:02:48 +00:00
Alan Modra
d4a8b80189 Resetting section vma after _bfd_dwarf2_find_nearest_line
There are failure paths in _bfd_dwarf2_slurp_debug_info that can
result in altered section vmas.  Also, when setting ET_REL section
vmas it's not too difficult to handle cases where the original vma was
non-zero, so do that too.

This patch was really in response to an addr2line buffer overflow
processing a fuzzed mips relocatable object file.  The file had a
number of .debug_info sections with relocations that included lo16 and
hi16 relocs, and in that order.  At least one section VMA was
non-zero.  This resulted in processing of DWARF info twice, once via
the call to _bfd_dwarf2_find_nearest_line in
_bfd_mips_elf_find_nearest_line, and because that failed leaving VMAs
altered, the second via the call in _bfd_elf_find_nearest_line.  The
first call left entries on mips_hi16_list pointing at buffers
allocated during the first call, the second call processed the
mips_hi16_list after the buffers had been freed.  (At least when
running with asan and under valgrind.  Under gdb with a non-asan
addr2line the second call allocated exactly the same buffer and the
bug didn't show.)  Now I don't really care too much what happens with
fuzzed files, but the logic in _bfd_dwarf2_find_nearest_line is meant
to result in only one read of .debug_info, not multiple reads of the
same info when there are errors.  This patch fixes that problem.

	* dwarf2.c (struct adjusted_section): Add orig_vma.
	(unset_sections): Reset vma to it.
	(place_sections): Handle non-zero vma too.  Save orig_vma.
	(_bfd_dwarf2_slurp_debug_info): Tidy.  Correct outdated comment.
	On error returns after calling place_sections, call
	unset_sections.
	(_bfd_dwarf2_find_nearest_line_with_alt): Simplify call to
	unset_sections.
2023-02-06 10:31:26 +10:30
Romain Geissler
023b960d59 [PR 30082] Pass $JANSSON_LIBS and $ZSTD_LIBS to ld-bootstrap/bootrap.exp 2023-02-06 10:31:26 +10:30
GDB Administrator
f6d5083f59 Automatic date update in version.in 2023-02-06 00:00:11 +00:00
GDB Administrator
4b8f97828b Automatic date update in version.in 2023-02-05 00:00:08 +00:00
Andrew Burgess
79436bfc5a gdb/testsuite: don't try to set non-stop mode on a running target
The test gdb.threads/thread-specific-bp.exp tries to set non-stop mode
on a running target, something which the manual makes clear is not
allowed.

This commit restructures the test a little, we now set the non-stop
mode as part of the GDBFLAGS, so the mode will be set before GDB
connects to the target.  As a consequence I'm able to move the
with_test_prefix out of the check_thread_specific_breakpoint proc.
The check_thread_specific_breakpoint proc is now called within a loop.

After this commit the gdb.threads/thread-specific-bp.exp test still
has some failures, this is because of an issue GDB currently has
printing "Thread ... exited" messages.  This problem should be
addressed by this patch:

  https://sourceware.org/pipermail/gdb-patches/2022-December/194694.html

when it is merged.
2023-02-04 16:15:38 +00:00
Dimitar Dimitrov
ba268471bf ld: pru: Add optional section alignments
The Texas Instruments SoCs with AARCH64 host processors have stricter
alignment requirements than ones with ARM32 host processors.  It's not
only the requirement for resource_table to be aligned to 8.  But also
any loadable segment size must be a multiple of 4 [1].

The current PRU default linker script may output a segment size not
aligned to 4, which would cause firmware load failure on AARCH64 hosts.

Fix this by using COMMONPAGESIZE and MAXPAGESIZE to signify respectively
the section memory size requirement and the resource table section's
start address alignment.  This would avoid penalizing the ARM32 hosts,
for which the default values (1 and 1) are sufficient.

For AARCH64 hosts, the alignments would be overwritten from GCC spec
files using the linker command line, e.g.:
  -z common-page-size=4 -z max-page-size=8

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/remoteproc/pru_rproc.c?h=v6.1#n555

ld/ChangeLog:

	* scripttempl/pru.sc (_data_end): Remove the alignment.
	(.data): Align output section size to COMMONPAGESIZE.
	(.resource_table): Ditto.

Signed-off-by: Dimitar Dimitrov <dimitar@dinux.eu>
2023-02-04 14:12:47 +02:00
Dimitar Dimitrov
a9c9f0650c ld: pru: Merge the bss input sections into data
The popular method to load PRU firmware is through the remoteproc Linux
kernel driver.  In order to save a few bytes from the firmware, the PRU
CRT0 is spared from calling memset for the bss segment [1].  Instead the
host loader is supposed to zero out the bss segment.  This is important
for PRU, which typically has only 8KB for instruction memory.

The legacy non-mainline PRU host driver relied on the default
behaviour of the kernel core remoteproc [2].  That default is to zero
out the loadable memory regions not backed by file storage (i.e. the
bss sections).  This worked for the libgloss' CRT0.

But the PRU loader merged in mainline Linux explicitly changes the
default behaviour [3].  It no longer is zeroing out memory regions.
Hence the bss sections are not initialized - neither by CRT0, nor by the
host loader.

This patch fixes the issue by aligning the GNU LD default linker script
with the mainline Linux kernel expectation.  Since the mainline kernel
driver is submitted by the PRU manufacturer itself (Text Instruments),
we can consider that as defining the ABI.

This change has been tested on Beaglebone AI-64 [4].  Static counter
variables in the firmware are now always starting from zero, as
expected.  There was only one new toolchain test failure in orphan3.d,
due to reordering of the output sections.  I believe this is a harmless
issue.  I could not rewrite the PASS criteria to ignore the output
section ordering, so I have disabled that test case for PRU.

[1] https://sourceware.org/git/?p=newlib-cygwin.git;a=blob;f=libgloss/pru/crt0.S;h=b3f0d53a93acc372f461007553e7688ca77753c9;hb=HEAD#l40
[2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/remoteproc/remoteproc_elf_loader.c?h=v6.1#n228
[3] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/remoteproc/pru_rproc.c?h=v6.1#n641
[4] https://beagleboard.org/ai-64

ld/ChangeLog:

	* scripttempl/pru.sc (.data): Merge .bss input sections into the
	.data output section.
	* testsuite/ld-elf/orphan3.d: Disable for PRU.

Signed-off-by: Dimitar Dimitrov <dimitar@dinux.eu>
2023-02-04 14:12:13 +02:00
GDB Administrator
6979017396 Automatic date update in version.in 2023-02-04 00:00:09 +00:00
Guillermo E. Martinez
7f6ebecd56 bpf: fix error conversion from long unsigned int to unsigned int [-Werror=overflow]
Regenerating BPF target using the maintainer mode emits:
.../opcodes/bpf-opc.c:57:11: error: conversion from ‘long unsigned int’ to ‘unsigned int’ changes value from ‘18446744073709486335’ to ‘4294902015’ [-Werror=overflow]
  57 |   64, 64, 0xffffffffffff00ff, { { F (F_IMM32) }, { F (F_OFFSET16) }, { F (F_SRCLE) }, { F (F_OP_CODE) }, { F (F_DSTLE) }, { F (F_OP_SRC) }, { F (F_OP_CLASS) }, { 0 } }

The use of a narrow size to handle the mask CGEN in instruction format
is causing this error.  Additionally eBPF `call' instructions
constructed by expressions using symbols (BPF_PSEUDO_CALL) emits
annotations in `src' field of the instruction, used to identify BPF
target endianness.

cpu/
	* bpf.cpu (define-call-insn): Remove `src' field from
	instruction mask.

include/
	*opcode/cge.h (CGEN_IFMT): Adjust mask bit width.

opcodes/
	* bpf-opc.c: Regenerate.
2023-02-03 11:18:50 -06:00
Simon Marchi
5a19bfd673 gdb: make target_desc_info_from_user_p a method of target_desc_info
Move the implementation over to target_desc_info.  Remove the
target_desc_info forward declaration in target-descriptions.h, it's no
longer needed.

Change-Id: Ic95060341685afe0b73af591ca6efe32f5e7e892
2023-02-03 11:06:26 -05:00
Simon Marchi
5776836631 gdb: remove copy_inferior_target_desc_info
This function is now trivial, we can just copy inferior::tdesc_info
where needed.

Change-Id: I25185e2cd4ba1ef24a822d9e0eebec6e611d54d6
2023-02-03 11:06:26 -05:00
Simon Marchi
6b0b81b9f2 gdb: remove get_tdesc_info
Remove this function, since it's now a trivial access to
inferior::tdesc_info.

Change-Id: I3e88a8214034f1a4163420b434be11f51eef462c
2023-02-03 11:06:26 -05:00
Simon Marchi
79846903c6 gdb: change inferior::tdesc_info to non-pointer
I initially made this field a unique pointer, to have automatic memory
management.  But I then thought that the field didn't really need to be
allocated separately from struct inferior.  So make it a regular
non-pointer field of inferior.

Remove target_desc_info_free, as it's no longer needed.

Change-Id: Ica2b97071226f31c40e86222a2f6922454df1229
2023-02-03 11:06:26 -05:00
Simon Marchi
650160322c gdb: move target_desc_info to inferior.h
In preparation for the following patch, where struct inferior needs to
"see" struct target_desc_info, move target_desc_info to the header file.

I initially moved the structure to target-descriptions.h, and later made
inferior.h include target-descriptions.h.  This worked, but it then
occured to me that target_desc_info is really an inferior property that
involves a target description, so I think it makes sense to have it in
inferior.h.

Change-Id: I3e81d04faafcad431e294357389f3d4c601ee83d
2023-02-03 11:06:26 -05:00
Simon Marchi
e3ee979c1f gdb: use assignment to initialize variable in tdesc_parse_xml
Since allocate_target_description returns a target_desc_up, use
assignment to initialize the description variable.

Change-Id: Iab3311642c09b95648984f305936f4a4cde09440
2023-02-03 08:23:39 -05:00
Jan Beulich
9a4a449986 x86: drop LOCK from XCHG when optimizing
Like with segment overrides on LEA, optimize away such a redundant
instruction prefix.
2023-02-03 08:23:05 +01:00
Jan Beulich
33740f219e x86-64: respect {nooptimize} when building VEX prefix
Swapping operands for commutative insns occurs outside of
optimize_encoding() and hence needs explicit checking for a request to
avoid any optimizations.
2023-02-03 08:22:35 +01:00
Jan Beulich
739d764954 x86: respect {nooptimize} for LEA
Dropping a meaningless segment prefix occurs outside of
optimize_encoding() and hence needs explicit checking for a request to
avoid any optimizations.
2023-02-03 08:22:12 +01:00
Jan Beulich
6919639125 x86-64: respect MOVABS when choosing alternative encodings
The alternative encoding is valid for MOV, but there's no such thing for
MOVABS.
2023-02-03 08:21:11 +01:00
Jan Beulich
0655669b86 RISC-V: don't disassemble unrecognized insns as .byte
Insn width granularity being 16 bits, producing byte granular output
isn't very useful. With there being a way to specific otherwise
unknown insns to the assembler, use that same representation (to be
precise: its <length>,<encoding> flavor) for disassembly.
2023-02-03 08:20:32 +01:00
Alan Modra
96e034530e Add ECOFF Symbolic Header sanity checks
Anti-fuzzer measures.  The checks don't ensure the various elements in
the header are distinct, but that isn't important as far as making
sure we don't overrun the buffer containing all the elements.  Also,
we now don't care about offsets where the corresponding count is zero.

	* ecoff.c (_bfd_ecoff_slurp_symbolic_info): Sanity check offsets
	in debug->symbolic_header.
2023-02-03 16:11:18 +10:30
GDB Administrator
e0df1fd784 Automatic date update in version.in 2023-02-03 00:00:08 +00:00
Simon Marchi
18b4d0736b gdb: initial support for ROCm platform (AMDGPU) debugging
This patch adds the foundation for GDB to be able to debug programs
offloaded to AMD GPUs using the AMD ROCm platform [1].  The latest
public release of the ROCm release at the time of writing is 5.4, so
this is what this patch targets.

The ROCm platform allows host programs to schedule bits of code for
execution on GPUs or similar accelerators.  The programs running on GPUs
are typically referred to as `kernels` (not related to operating system
kernels).

Programs offloaded with the AMD ROCm platform can be written in the HIP
language [2], OpenCL and OpenMP, but we're going to focus on HIP here.
The HIP language consists of a C++ Runtime API and kernel language.
Here's an example of a very simple HIP program:

    #include "hip/hip_runtime.h"
    #include <cassert>

    __global__ void
    do_an_addition (int a, int b, int *out)
    {
      *out = a + b;
    }

    int
    main ()
    {
      int *result_ptr, result;

      /* Allocate memory for the device to write the result to.  */
      hipError_t error = hipMalloc (&result_ptr, sizeof (int));
      assert (error == hipSuccess);

      /* Run `do_an_addition` on one workgroup containing one work item.  */
      do_an_addition<<<dim3(1), dim3(1), 0, 0>>> (1, 2, result_ptr);

      /* Copy result from device to host.  Note that this acts as a synchronization
         point, waiting for the kernel dispatch to complete.  */
      error = hipMemcpyDtoH (&result, result_ptr, sizeof (int));
      assert (error == hipSuccess);

      printf ("result is %d\n", result);
      assert (result == 3);

      return 0;
    }

This program can be compiled with:

    $ hipcc simple.cpp -g -O0 -o simple

... where `hipcc` is the HIP compiler, shipped with ROCm releases.  This
generates an ELF binary for the host architecture, containing another
ELF binary with the device code.  The ELF for the device can be
inspected with:

    $ roc-obj-ls simple
    1       host-x86_64-unknown-linux                                           file://simple#offset=8192&size=0
    1       hipv4-amdgcn-amd-amdhsa--gfx906                                     file://simple#offset=8192&size=34216
    $ roc-obj-extract 'file://simple#offset=8192&size=34216'
    $ file simple-offset8192-size34216.co
    simple-offset8192-size34216.co: ELF 64-bit LSB shared object, *unknown arch 0xe0* version 1, dynamically linked, with debug_info, not stripped
                                                                                 ^
                       amcgcn architecture that my `file` doesn't know about ----´

Running the program gives the very unimpressive result:

    $ ./simple
    result is 3

While running, this host program has copied the device program into the
GPU's memory and spawned an execution thread on it.  The goal of this
GDB port is to let the user debug host threads and these GPU threads
simultaneously.  Here's a sample session using a GDB with this patch
applied:

    $ ./gdb -q -nx --data-directory=data-directory ./simple
    Reading symbols from ./simple...
    (gdb) break do_an_addition
    Function "do_an_addition" not defined.
    Make breakpoint pending on future shared library load? (y or [n]) y
    Breakpoint 1 (do_an_addition) pending.
    (gdb) r
    Starting program: /home/smarchi/build/binutils-gdb-amdgpu/gdb/simple
    [Thread debugging using libthread_db enabled]
    Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
    [New Thread 0x7ffff5db7640 (LWP 1082911)]
    [New Thread 0x7ffef53ff640 (LWP 1082913)]
    [Thread 0x7ffef53ff640 (LWP 1082913) exited]
    [New Thread 0x7ffdecb53640 (LWP 1083185)]
    [New Thread 0x7ffff54bf640 (LWP 1083186)]
    [Thread 0x7ffdecb53640 (LWP 1083185) exited]
    [Switching to AMDGPU Wave 2:2:1:1 (0,0,0)/0]

    Thread 6 hit Breakpoint 1, do_an_addition (a=<error reading variable: DWARF-2 expression error: `DW_OP_regx' operations must be used either alone or in conjunction with DW_OP_piece or DW_OP_bit_piece.>,
        b=<error reading variable: DWARF-2 expression error: `DW_OP_regx' operations must be used either alone or in conjunction with DW_OP_piece or DW_OP_bit_piece.>,
        out=<error reading variable: DWARF-2 expression error: `DW_OP_regx' operations must be used either alone or in conjunction with DW_OP_piece or DW_OP_bit_piece.>) at simple.cpp:24
    24        *out = a + b;
    (gdb) info inferiors
      Num  Description       Connection           Executable
    * 1    process 1082907   1 (native)           /home/smarchi/build/binutils-gdb-amdgpu/gdb/simple
    (gdb) info threads
      Id   Target Id                                    Frame
      1    Thread 0x7ffff5dc9240 (LWP 1082907) "simple" 0x00007ffff5e9410b in ?? () from /opt/rocm-5.4.0/lib/libhsa-runtime64.so.1
      2    Thread 0x7ffff5db7640 (LWP 1082911) "simple" __GI___ioctl (fd=3, request=3222817548) at ../sysdeps/unix/sysv/linux/ioctl.c:36
      5    Thread 0x7ffff54bf640 (LWP 1083186) "simple" __GI___ioctl (fd=3, request=3222817548) at ../sysdeps/unix/sysv/linux/ioctl.c:36
    * 6    AMDGPU Wave 2:2:1:1 (0,0,0)/0                do_an_addition (
        a=<error reading variable: DWARF-2 expression error: `DW_OP_regx' operations must be used either alone or in conjunction with DW_OP_piece or DW_OP_bit_piece.>,
        b=<error reading variable: DWARF-2 expression error: `DW_OP_regx' operations must be used either alone or in conjunction with DW_OP_piece or DW_OP_bit_piece.>,
        out=<error reading variable: DWARF-2 expression error: `DW_OP_regx' operations must be used either alone or in conjunction with DW_OP_piece or DW_OP_bit_piece.>) at simple.cpp:24
    (gdb) bt
    Python Exception <class 'gdb.error'>: Unhandled dwarf expression opcode 0xe1
    #0  do_an_addition (a=<error reading variable: DWARF-2 expression error: `DW_OP_regx' operations must be used either alone or in conjunction with DW_OP_piece or DW_OP_bit_piece.>,
        b=<error reading variable: DWARF-2 expression error: `DW_OP_regx' operations must be used either alone or in conjunction with DW_OP_piece or DW_OP_bit_piece.>,
        out=<error reading variable: DWARF-2 expression error: `DW_OP_regx' operations must be used either alone or in conjunction with DW_OP_piece or DW_OP_bit_piece.>) at simple.cpp:24
    (gdb) continue
    Continuing.
    result is 3
    warning: Temporarily disabling breakpoints for unloaded shared library "file:///home/smarchi/build/binutils-gdb-amdgpu/gdb/simple#offset=8192&size=67208"
    [Thread 0x7ffff54bf640 (LWP 1083186) exited]
    [Thread 0x7ffff5db7640 (LWP 1082911) exited]
    [Inferior 1 (process 1082907) exited normally]

One thing to notice is the host and GPU threads appearing under
the same inferior.  This is a design goal for us, as programmers tend to
think of the threads running on the GPU as part of the same program as
the host threads, so showing them in the same inferior in GDB seems
natural.  Also, the host and GPU threads share a global memory space,
which fits the inferior model.

Another thing to notice is the error messages when trying to read
variables or printing a backtrace.  This is expected for the moment,
since the AMD GPU compiler produces some DWARF that uses some
non-standard extensions:

  https://llvm.org/docs/AMDGPUDwarfExtensionsForHeterogeneousDebugging.html

There were already some patches posted by Zoran Zaric earlier to make
GDB support these extensions:

  https://inbox.sourceware.org/gdb-patches/20211105113849.118800-1-zoran.zaric@amd.com/

We think it's better to get the basic support for AMD GPU in first,
which will then give a better justification for GDB to support these
extensions.

GPU threads are named `AMDGPU Wave`: a wave is essentially a hardware
thread using the SIMT (single-instruction, multiple-threads) [3]
execution model.

GDB uses the amd-dbgapi library [4], included in the ROCm platform, for
a few things related to AMD GPU threads debugging.  Different components
talk to the library, as show on the following diagram:

    +---------------------------+     +-------------+     +------------------+
    | GDB   | amd-dbgapi target | <-> |     AMD     |     |    Linux kernel  |
    |       +-------------------+     |   Debugger  |     +--------+         |
    |       | amdgcn gdbarch    | <-> |     API     | <=> | AMDGPU |         |
    |       +-------------------+     |             |     | driver |         |
    |       | solib-rocm        | <-> | (dbgapi.so) |     +--------+---------+
    +---------------------------+     +-------------+

  - The amd-dbgapi target is a target_ops implementation used to control
    execution of GPU threads.  While the debugging of host threads works
    by using the ptrace / wait Linux kernel interface (as usual), control
    of GPU threads is done through a special interface (dubbed `kfd`)
    exposed by the `amdgpu` Linux kernel module.  GDB doesn't interact
    directly with `kfd`, but instead goes through the amd-dbgapi library
    (AMD Debugger API on the diagram).

    Since it provides execution control, the amd-dbgapi target should
    normally be a process_stratum_target, not just a target_ops.  More
    on that later.

  - The amdgcn gdbarch (describing the hardware architecture of the GPU
    execution units) offloads some requests to the amd-dbgapi library,
    so that knowledge about the various architectures doesn't need to be
    duplicated and baked in GDB.  This is for example for things like
    the list of registers.

  - The solib-rocm component is an solib provider that fetches the list of
    code objects loaded on the device from the amd-dbgapi library, and
    makes GDB read their symbols.  This is very similar to other solib
    providers that handle shared libraries, except that here the shared
    libraries are the pieces of code loaded on the device.

Given that Linux host threads are managed by the linux-nat target, and
the GPU threads are managed by the amd-dbgapi target, having all threads
appear in the same inferior requires the two targets to be in that
inferior's target stack.  However, there can only be one
process_stratum_target in a given target stack, since there can be only
one target per slot.  To achieve it, we therefore resort the hack^W
solution of placing the amd-dbgapi target in the arch_stratum slot of
the target stack, on top of the linux-nat target.  Doing so allows the
amd-dbgapi target to intercept target calls and handle them if they
concern GPU threads, and offload to beneath otherwise.  See
amd_dbgapi_target::fetch_registers for a simple example:

    void
    amd_dbgapi_target::fetch_registers (struct regcache *regcache, int regno)
    {
      if (!ptid_is_gpu (regcache->ptid ()))
        {
          beneath ()->fetch_registers (regcache, regno);
          return;
        }

      // handle it
    }

ptids of GPU threads are crafted with the following pattern:

  (pid, 1, wave id)

Where pid is the inferior's pid and "wave id" is the wave handle handed
to us by the amd-dbgapi library (in practice, a monotonically
incrementing integer).  The idea is that on Linux systems, the
combination (pid != 1, lwp == 1) is not possible.  lwp == 1 would always
belong to the init process, which would also have pid == 1 (and it's
improbable for the init process to offload work to the GPU and much less
for the user to debug it).  We can therefore differentiate GPU and
non-GPU ptids this way.  See ptid_is_gpu for more details.

Note that we believe that this scheme could break down in the context of
containers, where the initial process executed in a container has pid 1
(in its own pid namespace).  For instance, if you were to execute a ROCm
program in a container, then spawn a GDB in that container and attach to
the process, it will likely not work.  This is a known limitation.  A
workaround for this is to have a dummy process (like a shell) fork and
execute the program of interest.

The amd-dbgapi target watches native inferiors, and "attaches" to them
using amd_dbgapi_process_attach, which gives it a notifier fd that is
registered in the event loop (see enable_amd_dbgapi).  Note that this
isn't the same "attach" as in PTRACE_ATTACH, but being ptrace-attached
is a precondition for amd_dbgapi_process_attach to work.  When the
debugged process enables the ROCm runtime, the amd-dbgapi target gets
notified through that fd, and pushes itself on the target stack of the
inferior.  The amd-dbgapi target is then able to intercept target_ops
calls.  If the debugged process disables the ROCm runtime, the
amd-dbgapi target unpushes itself from the target stack.

This way, the amd-dbgapi target's footprint stays minimal when debugging
a process that doesn't use the AMD ROCm platform, it does not intercept
target calls.

The amd-dbgapi library is found using pkg-config.  Since enabling
support for the amdgpu architecture (amdgpu-tdep.c) depends on the
amd-dbgapi library being present, we have the following logic for
the interaction with --target and --enable-targets:

 - if the user explicitly asks for amdgcn support with
   --target=amdgcn-*-* or --enable-targets=amdgcn-*-*, we probe for
   the amd-dbgapi and fail if not found

 - if the user uses --enable-targets=all, we probe for amd-dbgapi,
   enable amdgcn support if found, disable amdgcn support if not found

 - if the user uses --enable-targets=all and --with-amd-dbgapi=yes,
   we probe for amd-dbgapi, enable amdgcn if found and fail if not found

 - if the user uses --enable-targets=all and --with-amd-dbgapi=no,
   we do not probe for amd-dbgapi, disable amdgcn support

 - otherwise, amd-dbgapi is not probed for and support for amdgcn is not
   enabled

Finally, a simple test is included.  It only tests hitting a breakpoint
in device code and resuming execution, pretty much like the example
shown above.

[1] https://docs.amd.com/category/ROCm_v5.4
[2] https://docs.amd.com/bundle/HIP-Programming-Guide-v5.4
[3] https://en.wikipedia.org/wiki/Single_instruction,_multiple_threads
[4] https://docs.amd.com/bundle/ROCDebugger-API-Guide-v5.4

Change-Id: I591edca98b8927b1e49e4b0abe4e304765fed9ee
Co-Authored-By: Zoran Zaric <zoran.zaric@amd.com>
Co-Authored-By: Laurent Morichetti <laurent.morichetti@amd.com>
Co-Authored-By: Tony Tye <Tony.Tye@amd.com>
Co-Authored-By: Lancelot SIX <lancelot.six@amd.com>
Co-Authored-By: Pedro Alves <pedro@palves.net>
2023-02-02 10:02:34 -05:00
Simon Marchi
8b588f4212 gdb: make gdb_printing_disassembler::stream public
In the ROCm port, we need to access the underlying stream of a
gdb_printing_disassembler, so make it public.  The reason we need to
access it is to know whether it supports style escape code.  We then
pass that information to a temporary string_file we use while
symbolizing addresses.

Change-Id: Ib95755a4a45b8f6478787993e9f904df60dd8dc1
Approved-By: Andrew Burgess <aburgess@redhat.com>
2023-02-02 10:02:34 -05:00
Simon Marchi
17467c1030 gdb/solib-svr4: don't disable probes interface if probe not found
In ROCm-GDB, we install an solib provider for the GPU code objects on
top of the svr4 provider for the host, in order to add solibs
representing the GPU code objects to the solib list containing the host
process' shared libraries.  We override the target_so_ops::handle_event
function pointer with our own, in which we call svr4_so_ops.handle_event
(which contains svr4_handle_solib_event) manually.  When the host
(un)loads a library, the ROCm part of handle_event is a no-op.  When the
GPU (un)loads a code object, we want the host side (svr4) to be a no-op.

The problem is that when handle_event is called because of a GPU event,
svr4_handle_solib_event gets called while not stopped at an svr4
probe.  It then assumes this means there's a problem with the probes
interface and disables it through the following sequence of events:

  - solib_event_probe_at return nullptr
  - svr4_handle_solib_event returns early
  - the make_scope_exit callback calls disable_probes_interface

We could fix that by making the ROCm handle_event callback check if an
svr4 probe is that the stop address, and only call
svr4_so_ops.handle_event if so.  However, it doesn't feel right to
include some svr4 implementation detail in the ROCm event handler.

Instead, this patch changes svr4_handle_solib_event to not assume it is
an error if called while not at an svr4 probe location, and therefore
not disable the probes interface.  That just means moving the
make_scope_exit call below where we lookup the probe by pc.

Change-Id: Ie8ddf5beffa2e92b8ebfdd016454546252519244
Co-Authored-By: Lancelot SIX <lancelot.six@amd.com>
2023-02-02 10:02:34 -05:00
Simon Marchi
d246d904ad gdb: add gdbarch_up
Add a gdbarch_up unique pointer type, that calls gdbarch_free on
deletion.  This is used in the ROCm support patch at the end of this
series.

Change-Id: I4b808892d35d69a590ce83180f41afd91705b2c8
Approved-By: Andrew Burgess <aburgess@redhat.com>
2023-02-02 10:02:34 -05:00
Simon Marchi
9056c917b3 gdb: add inferior_pre_detach observable
Add an observable notified in target_detach just before calling the
detach method on the inferior's target stack.  This allows observer to
do some work on the inferior while it's still ptrace-attached, in the
case of a native Linux inferior.  Specifically, the amd-dbgapi target
will need it in order to call amd_dbgapi_process_detach before the
process gets ptrace-detached.

Change-Id: I28b6065e251012a4c2db8a600fe13ba31671e3c9
Approved-By: Andrew Burgess <aburgess@redhat.com>
2023-02-02 10:02:34 -05:00
Simon Marchi
7b065306be gdbsupport: add type definitions for pid, lwp and tid
A following patch will want to declare variables of the same type as
some ptid_t components.  To make that easy (and avoid harcoding those
types everywhere), define some type definitions in the ptid_t struct for
each of them.  Use them throughout ptid.h.

I initially used pid_t, lwp_t and tid_t, but there is the risk of some
system defining the pid_t type using a macro instead of a typedef, which
would break things.  So, use the _type suffix instead.

Change-Id: I820b0bea9dafcb4914f1c9ba4bb96b5c666c8dec
Approved-By: Andrew Burgess <aburgess@redhat.com>
2023-02-02 10:02:34 -05:00
Pedro Alves
b82d4ec99e gdb: make install_breakpoint return a non-owning reference
A following patch will want to install a breakpoint and then keep a
non-owning reference to it.  Make install_breakpoint return a non-owning
reference, to make that easy.

Co-Authored-By: Simon Marchi <simon.marchi@efficios.com>
Change-Id: I2e8106a784021ff276ce251e24708cbdccc2d479
Approved-By: Andrew Burgess <aburgess@redhat.com>
2023-02-02 10:02:33 -05:00
Lancelot SIX
1a8505cb53 gdb: add supports_arch_info callback to gdbarch_register
In the ROCm GDB port, there are some amdgcn architectures known by BFD
that we don't actually support in GDB.  We don't want
gdbarch_printable_names to return these architectures.

gdbarch_printable_names is used for a few things:

 - completion of the "set architecture" command
 - the gdb.architecture_names function in Python
 - foreach-arch selftests

Add an optional callback to gdbarch_register that is a predicate
indicating whether the gdbarch supports the given bfd_arch_info.  by
default, it is nullptr, meaning that the gdbarch accepts all "mach"s for
that architecture known by BFD.

Change-Id: I712f94351b0b34ed1f42e5cf7fc7ba051315d860
Co-Authored-By: Simon Marchi <simon.marchi@efficios.com>
Approved-By: Andrew Burgess <aburgess@redhat.com>
2023-02-02 10:02:33 -05:00
Tom de Vries
4a075b258e [gas] Update .loc syntax comment in dwarf2dbg.c
I noticed that a comment in gas/dwarf2dbg.c describing .loc syntax was missing
the "view VALUE" option.

Fix this by adding the missing option.
2023-02-02 15:07:44 +01:00
Enze Li
839c8aaa43 gdb: remove gdb_indent.sh
GDB has been converted to a C++ program for many years[1], and the
gdb_indent.sh will not be used any more. Therefore, remove the script as
obvious.

[1] https://sourceware.org/gdb/wiki/cxx-conversion

Approved-By: Simon Marchi <simark@simark.ca>
2023-02-02 20:57:04 +08:00
Indu Bhagat
1bd123bdf3 ld/doc: use "stack trace" instead of "unwind" for SFrame
SFrame format is meant for generating stack traces only.

ld/
	* ld.texi: Replace the use of "unwind" with "stack trace".
2023-02-02 00:49:44 -08:00
Indu Bhagat
53d8d3f0c1 bfd: use "stack trace" instead of "unwind" for SFrame
SFrame format is meant for generating stack traces only.

bfd/
	* elf-bfd.h: Replace the use of "unwind" with "stack trace".
	* elf-sframe.c: Likewise.
	* elf64-x86-64.c: Likewise.
	* elfxx-x86.c: Likewise.

include/
	* elf/common.h: Likewise.
2023-02-02 00:49:29 -08:00
Indu Bhagat
3e3e792a29 gas: use "stack trace" instead of "unwind" for SFrame
SFrame format is meant for generating stack traces only.

gas/
	* as.c: Replace the use of "unwind" with "stack trace".
	* config/tc-aarch64.c: Likewise.
	* config/tc-aarch64.h: Likewise.
	* config/tc-i386.c: Likewise.
	* config/tc-i386.h: Likewise.
	* gen-sframe.c: Likewise.
	* gen-sframe.h: Likewise.
	* testsuite/gas/cfi-sframe/cfi-sframe-aarch64-2.s: Likewise.
	* testsuite/gas/cfi-sframe/cfi-sframe-common-8.s: Likewise.
	* testsuite/gas/cfi-sframe/common-empty-2.s: Likewise.
	* testsuite/gas/cfi-sframe/common-empty-3.s: Likewise.
2023-02-02 00:49:07 -08:00
Indu Bhagat
91def06c4d sframe: use "stack trace" instead of "unwind" for SFrame
SFrame format is meant for generating stack traces only.

include/
	* sframe.h: Fix comments in the header file.
2023-02-02 00:47:38 -08:00
Indu Bhagat
469b6d54d3 libsframe/doc: use "stack trace" instead of "unwind" for SFrame
SFrame format is meant for generating stack traces only.

libsframe/
	* doc/sframe-spec.texi: Use "stack trace" instead of "unwind".
2023-02-02 00:47:22 -08:00
Alan Modra
0709900ede ld-elf/merge test update
The merge test fais on numerous targets because they don't support the
necessary pc-relative relocs.  This patch removes that part of the
merge test, and makes references to the merged strings from .data
rather than .text to better support targets that relax text by
default.
2023-02-02 13:40:14 +10:30
GDB Administrator
269c4cda85 Automatic date update in version.in 2023-02-02 00:00:12 +00:00
Alan Modra
02ed5b2723 obj-elf.h BYTES_IN_WORD
Don't define this.  It is defined just before elf-bfd.h is included,
but doesn't have any relevance there.  Instead is for aout64.h where
the default is 4 anyway.
2023-02-02 08:48:58 +10:30
Alan Modra
2ccd2276a4 gas obj_end
Provide a way for config/obj-* to clean up at end of assembly, and do
so for ELF.

	* obj.h (struct format_ops): Add "end".
	* config/obj-aout.c (aout_format_ops): Init new field.
	* config/obj-coff.c (coff_format_ops): Likewise.
	* config/obj-ecoff.c (ecoff_format_ops): Likewise.
	* config/obj-elf.c (elf_format_ops): Likewise.
	(elf_begin): Move later in file.  Clear some more variables.
	(comment_section): Make file scope.
	(free_section_idx): Rewrite.
	(elf_adjust_symtab): Expand str_htab_create call and use
	free_section_idx as delete function.
	(elf_frob_file_after_relocs): Don't clean up groups.indexes here.
	(elf_end): New function.
	* config/obj-elf.h (obj_end): Define.
	* config/obj-multi.h (obj_end): Define.
	* output-file.c (output_file_close): Call obj_end.
2023-02-02 08:48:29 +10:30
Thiago Jung Bauermann
43e5fbd8b7 gdbserver: Add PID parameter to linux_get_auxv and linux_get_hwcap
This patch doesn't change gdbserver behaviour, but after later changes are
made it avoids a null pointer dereference when HWCAP needs to be obtained
for a specific process while current_thread is nullptr.

Fixing linux_read_auxv, linux_get_hwcap and linux_get_hwcap2 to take a PID
parameter seems more correct than setting current_thread in one particular
code path.

Changes are propagated to allow passing the new parameter through the call
chain.

Approved-By: Simon Marchi <simon.marchi@efficios.com>
2023-02-01 20:42:50 +00:00
Thiago Jung Bauermann
cbd02f9fa5 gdbserver: Add assert in find_register_by_number
It helped me during development, catching bugs closer to when they actually
happened.

Also remove the equivalent gdb_assert in regcache_raw_read_unsigned, since
it's checking the same condition a few frames above.

Suggested-By: Simon Marchi <simon.marchi@efficios.com>
Approved-By: Simon Marchi <simon.marchi@efficios.com>
2023-02-01 19:48:07 +00:00
Andrew Burgess
cded17bfca gdb/testsuite: fix fetch_src_and_symbols.exp with native-gdbserver board
I noticed that the gdb.debuginfod/fetch_src_and_symbols.exp script
doesn't work with the native-gdbserver board, I see this error:

  ERROR: tcl error sourcing /tmp/build/gdb/testsuite/../../../src/gdb/testsuite/gdb.debuginfod/fetch_src_and_symbols.exp.
  ERROR: gdbserver does not support run without extended-remote
      while executing
  "error "gdbserver does not support $command without extended-remote""
      (procedure "gdb_test_multiple" line 51)
      invoked from within

This was introduced with this commit:

  commit 7dd38e31d6
  Date:   Fri Jan 6 18:45:27 2023 -0500

      gdb/linespec.c: Fix missing source file during breakpoint re-set

The problem is that the above commit introduces a direct use of the
"run" command, which doesn't work with 'target remote' targets, as
exercised by the native-gdbserver board.

To avoid this, in this commit I switch to using runto_main.  However,
calling runto_main will, by default, delete all the currently set
breakpoints.  As the point of the above commit was to check that a
breakpoint set before stating an inferior would be correctly re-set,
we need to avoid this breakpoint deleting behaviour.

To do this I make use of with_override, and override the
delete_breakpoints proc with a dummy proc which does nothing.

By reverting the GDB changes in commit 7dd38e31d6 I have confirmed
that even after my changes in this commit, the test still fails.  But
with the fixes in commit 7dd38e31d6, this test now passed using the
unix, native-gdbserver, and native-extended-gdbserver boards.
2023-02-01 17:32:16 +00:00