]> git.ipfire.org Git - thirdparty/binutils-gdb.git/log
thirdparty/binutils-gdb.git
3 years agoAdd memory tagging testcases users/luisgpm/aarch64-mte-v3
Luis Machado [Mon, 15 Jun 2020 18:55:02 +0000 (15:55 -0300)] 
Add memory tagging testcases

Add an AArch64-specific test and a more generic memory tagging test that
other architectures can run.

Even though architectures not supporting memory tagging can run the memory
tagging tests, the runtime check will make the tests bail out early, as it
would make no sense to proceed without proper support.

It is also tricky to do any further runtime tests for memory tagging, given
we'd need to deal with tags, and those are arch-specific.  Therefore the
test in gdb.base is more of a smoke test.

If an architecture wants to implement memory tagging, then it makes sense to
have tests within gdb.arch instead.

gdb/testsuite/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

* gdb.arch/aarch64-mte.c: New file.
* gdb.arch/aarch64-mte.exp: New test.
* gdb.base/memtag.c: New file.
* gdb.base/memtag.exp: New test.
* lib/gdb.exp (supports_memtag): New function.

3 years agoAdd NEWS entry.
Luis Machado [Mon, 15 Jun 2020 18:54:00 +0000 (15:54 -0300)] 
Add NEWS entry.

Mention the new packets and memory tagging features.

gdb/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

* NEWS: Mention memory tagging changes.

3 years agoDocument new "x" and "print" memory tagging extensions
Luis Machado [Mon, 15 Jun 2020 18:51:21 +0000 (15:51 -0300)] 
Document new "x" and "print" memory tagging extensions

Document the changes to the "print" and "x" commands to support memory
tagging.

gdb/doc/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

* gdb.texinfo (Data): Document memory tagging changes to the "print"
command.
(Examining Memory): Document memory tagging changes to the "x"
command.
(Memory Tagging): Update with more information on changes to the "x"
and "print" commands.

3 years agoExtend "x" and "print" commands to support memory tagging
Luis Machado [Mon, 15 Jun 2020 18:50:55 +0000 (15:50 -0300)] 
Extend "x" and "print" commands to support memory tagging

Extend the "x" and "print" commands to make use of memory tagging
functionality, if supported by the architecture.

The "print" command will point out any possible tag mismatches it finds
when dealing with pointers, in case such a pointer is tagged.  No additional
modifiers are needed.

Suppose we have a pointer "p" with value 0x1234 (logical tag 0x0) and that we
have an allocation tag of 0x1 for that particular area of memory. This is the
expected output:

(gdb) p/x p
Logical tag (0x0) does not match the allocation tag (0x1).
$1 = 0x1234

The "x" command has a new 'm' modifier that will enable displaying of
allocation tags alongside the data dump.  It will display one allocation
tag per line.

AArch64 has a tag granule of 16 bytes, which means we can have one tag for
every 16 bytes of memory. In this case, this is what the "x" command will
display with the new 'm' modifier:

(gdb) x/32bxm p
<Allocation Tag 0x1 for range [0x1230,0x1240)>
0x1234: 0x01 0x02 0x00 0x00 0x00 0x00 0x00 0x00
0x123c: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
<Allocation Tag 0x1 for range [0x1240,0x1250)>
0x1244: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00
0x124c: 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00

(gdb) x/4gxm a
<Allocation Tag 0x1 for range [0x1230,0x1240)>
0x1234: 0x0000000000000201 0x0000000000000000
<Allocation Tag 0x1 for range [0x1240,0x1250)>
0x1244: 0x0000000000000000 0x0000000000000000

gdb/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

* printcmd.c (decode_format): Handle the 'm' modifier.
(do_examine): Display allocation tags when required/supported.
(should_validate_memtags): New function.
(print_command_1): Display memory tag mismatches.
* valprint.h (struct format_data) <print_tags>: New field.

3 years agoDocumentation for the new mtag commands
Luis Machado [Mon, 15 Jun 2020 18:50:10 +0000 (15:50 -0300)] 
Documentation for the new mtag commands

Document the new "mtag" command prefix and all of its subcommands.

gdb/doc/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

* gdb.textinfo (Memory Tagging): New subsection.
(AArch64 Memory Tagging Extension): New subsection.

3 years agoNew mtag commands
Luis Machado [Mon, 15 Jun 2020 18:49:37 +0000 (15:49 -0300)] 
New mtag commands

Add new commands under the "mtag" prefix to allow users to inspect, modify and
check memory tags in different ways.

The available subcommands are the following:

- mtag showltag <expression>: Shows the logical tag for a particular address.

- mtag setltag <expression> <tag>: Prints the address tagged with the logical
  tag <tag>.

- mtag showatag <expression>: Shows the allocation tag for a particular address.

- mtag setatag <expression> <length> <tags>: Sets one or more allocation tags to
  the specified tags.

- mtag check <expression>: Check if the logical tag in <address> matches its
  allocation tag.

These commands make use of the memory tagging gdbarch methods, and are still
available, but disabled, when memory tagging is not supported by the
architecture.

I've pondered about a way to make these commands invisible when memory tagging
is not available, but given the check is at runtime (and support may come and go
based on a process' configuration), that is a bit too late in the process to
either not include the commands or get rid of them.

Ideas are welcome.

gdb/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

* printcmd.c: Include gdbsupport/rsp-low.h.
(mtaglist): New static global.
(process_print_command_args): Factored out of
print_command_1.
(print_command_1): Use process_print_command_args.
(show_addr_not_tagged, show_memtag_unsupported, mtag_command)
(mtag_showtag_command, mtag_showltag_command, mtag_showatag_command)
(parse_setltag_input, mtag_setltag_command, parse_setatag_input)
(mtag_setatag_command, mtag_check_command): New functions.
(_initialize_printcmd): Add "mtag" prefix and subcommands.

gdbsupport/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

* rsp-low.cc (fromhex): Change error message text to not be
RSP-specific.

3 years agoAArch64: Add gdbserver MTE support
Luis Machado [Mon, 15 Jun 2020 18:38:43 +0000 (15:38 -0300)] 
AArch64: Add gdbserver MTE support

Adds the AArch64-specific memory tagging support (MTE) by implementing the
required hooks and checks for GDBserver.

gdbserver/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

* Makefile.in (SFILES): Add /../gdb/nat/aarch64-mte-linux-ptrace.c.
* configure.srv (aarch64*-*-linux*): Add arch/aarch64-mte-linux.o and
nat/aarch64-mte-linux-ptrace.o.
* linux-aarch64-low.cc: Include nat/aarch64-mte-linux-ptrace.h.
(class aarch64_target) <supports_memory_tagging>
<fetch_memtags, store_memtags>: New method overrides.
(aarch64_target::supports_memory_tagging)
(aarch64_target::fetch_memtags)
(aarch64_target::store_memtags): New methods.

3 years agoAArch64: Report tag violation error information
Luis Machado [Mon, 15 Jun 2020 18:44:20 +0000 (15:44 -0300)] 
AArch64: Report tag violation error information

Whenever a memory tag violation occurs, we get a SIGSEGV. Additional
information can be obtained through the siginfo data structure.

For AArch64 the Linux kernel may expose the fault address and tag
information, if we have a synchronous event. Otherwise there is
no fault address available.

gdb/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

* aarch64-linux-tdep.c
(aarch64_linux_handle_segmentation_fault): New function.
(aarch64_linux_init_abi): Register
aarch64_linux_handle_segmentation_fault as segmentation fault hook.
* arch/aarch64-linux.h (SEGV_MTEAERR): Define.
(SEGV_MTESERR): Define.

3 years agoAArch64: Add unit testing for logical tag set/get operations
Luis Machado [Mon, 15 Jun 2020 18:11:07 +0000 (15:11 -0300)] 
AArch64: Add unit testing for logical tag set/get operations

Add some unit testing to exercise setting/getting logical tags in the
AArch64 implementation.

gdb/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

* aarch64-linux-tdep.c: Include gdbsupport/selftest.h.
(aarch64_linux_ltag_tests): New function.
(_initialize_aarch64_linux_tdep): Register aarch64_linux_ltag_tests.

3 years agoAArch64: Implement the memory tagging gdbarch hooks
Luis Machado [Fri, 19 Jun 2020 20:37:33 +0000 (17:37 -0300)] 
AArch64: Implement the memory tagging gdbarch hooks

This patch implements the memory tagging gdbarch hooks for AArch64, for
the MTE feature.

gdb/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

* aarch64-linux-tdep.c: Include target.h, arch-utils.h, value.h.
(aarch64_linux_get_atag, aarch64_linux_tagged_address_p)
(aarch64_linux_memtag_mismatch_p, aarch64_linux_set_memtags)
(aarch64_linux_get_memtag, aarch64_linux_memtag_to_string): New
functions.
(aarch64_linux_init_abi): Initialize MTE-related gdbarch hooks.
* arch/aarch64-mte-linux.c (make_ltag_bits, make_ltag)
(aarch64_linux_set_ltag, aarch64_linux_get_ltag): New functions.
* arch/aarch64-mte-linux.h (MTE_LOGICAL_TAG_START_BIT)
(MTE_LOGICAL_MAX_VALUE): Define.
(make_ltag_bits, make_ltag, aarch64_linux_set_ltag)
(aarch64_linux_get_ltag): New prototype.

3 years agoRefactor parsing of /proc/<pid>/smaps
Luis Machado [Mon, 15 Jun 2020 17:24:53 +0000 (14:24 -0300)] 
Refactor parsing of /proc/<pid>/smaps

The Linux kernel exposes the information about MTE-protected pages via the
proc filesystem, more specifically through the smaps file.

What we're looking for is a mapping with the 'mt' flag, which tells us that
mapping was created with a PROT_MTE flag and, thus, is capable of using memory
tagging.

We already parse that file for other purposes (core file
generation/filtering), so this patch refactors the code to make the parsing
of the smaps file reusable for memory tagging.

The function linux_address_in_memtag_page uses the refactored code to allow
querying for memory tag support in a particular address, and it gets used in the
next patch.

gdb/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

* linux-tdep.c (struct smaps_vmflags) <memory_tagging>: New flag
bit.
(struct smaps_data): New struct.
(decode_vmflags): Handle the 'mt' flag.
(parse_smaps_data): New function, refactored from
linux_find_memory_regions_full.
(linux_address_in_memtag_page): New function.
(linux_find_memory_regions_full): Refactor into parse_smaps_data.
* linux-tdep.h (linux_address_in_memtag_page): New prototype.

3 years agoAArch64: Implement memory tagging target methods for AArch64
Luis Machado [Fri, 19 Jun 2020 20:33:13 +0000 (17:33 -0300)] 
AArch64: Implement memory tagging target methods for AArch64

The patch implements the memory tagging target hooks for AArch64, so we
can handle MTE.

gdb/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

* Makefile.in (ALL_64_TARGET_OBS): Add arch/aarch64-mte-linux.o.
(HFILES_NO_SRCDIR): Add arch/aarch64-mte-linux.h and
nat/aarch64-mte-linux-ptrace.h.
* aarch64-linux-nat.c: Include nat/aarch64-mte-linux-ptrace.h.
(aarch64_linux_nat_target) <supports_memory_tagging>: New method
override.
<fetch_memtags>: New method override.
<store_memtags>: New method override.
(aarch64_linux_nat_target::supports_memory_tagging): New method.
(aarch64_linux_nat_target::fetch_memtags): New method.
(aarch64_linux_nat_target::store_memtags): New method.
* arch/aarch64-mte-linux.c: New file.
* arch/aarch64-mte-linux.h: Include gdbsupport/common-defs.h.
(MTE_GRANULE_SIZE): Define.
(get_tag_granules): New prototype.
* configure.nat (NATDEPFILES): Add nat/aarch64-mte-linux-ptrace.o.
* configure.tgt (aarch64*-*-linux*): Add arch/aarch64-mte-linux.o.
* nat/aarch64-mte-linux-ptrace.c: New file.
* nat/aarch64-mte-linux-ptrace.h: New file.

3 years agoAArch64: Add MTE ptrace requests
Luis Machado [Fri, 19 Jun 2020 19:09:11 +0000 (16:09 -0300)] 
AArch64: Add MTE ptrace requests

This patch adds the required ptrace request definitions into a new include
file that will be used by the next patches.

They are PTRACE_PEEKMTETAGS and PTRACE_POKEMTETAGS.

gdb/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

* Makefile.in (HFILES_NO_SRCDIR): Add nat/aarch64-mte-linux-ptrace.h.
* nat/aarch64-mte-linux-ptrace.h: New file.

3 years agoAArch64: Add MTE register set support for GDB and gdbserver
Luis Machado [Mon, 15 Jun 2020 16:59:40 +0000 (13:59 -0300)] 
AArch64: Add MTE register set support for GDB and gdbserver

AArch64 MTE support in the Linux kernel exposes a new register
through ptrace.  This patch adds the required code to support it.

include/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

* elf/common.h (NT_ARM_TAGGED_ADDR_CTRL): Define.

gdb/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

* aarch64-linux-nat.c (fetch_mteregs_from_thread): New function.
(store_mteregs_to_thread): New function.
(aarch64_linux_nat_target::fetch_registers): Update to call
fetch_mteregs_from_thread.
(aarch64_linux_nat_target::store_registers): Update to call
store_mteregs_to_thread.
* aarch64-tdep.c (aarch64_mte_register_names): New struct.
(aarch64_cannot_store_register): Handle MTE registers.
(aarch64_gdbarch_init): Initialize and setup MTE registers.
* aarch64-tdep.h (gdbarch_tdep) <mte_reg_base>: New field.
<has_mte>: New method.
* arch/aarch64-linux.h (AARCH64_LINUX_SIZEOF_MTE): Define.

gdbserver/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

* linux-aarch64-low.cc (aarch64_fill_mteregset): New function.
(aarch64_store_mteregset): New function.
(aarch64_regsets): Add MTE register set entry.
(aarch64_sve_regsets): Add MTE register set entry.

3 years agoAArch64: Add target description/feature for MTE registers
Luis Machado [Mon, 15 Jun 2020 16:52:27 +0000 (13:52 -0300)] 
AArch64: Add target description/feature for MTE registers

This patch adds a target description and feature "mte" for aarch64.

It includes one new register, tag_ctl, that can be used to configure the
tag generation rules and sync/async modes.  It is 64-bit in size.

The patch also adjusts the code that creates the target descriptions at
runtime based on CPU feature checks.

gdb/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

* aarch64-linux-nat.c
(aarch64_linux_nat_target::read_description): Take MTE flag into
account.
Slight refactor to hwcap flag checking.
* aarch64-linux-tdep.c
(aarch64_linux_core_read_description): Likewise.
* aarch64-tdep.c (tdesc_aarch64_list): Add one more dimension for
MTE.
(aarch64_read_description): Add mte_p parameter and update to use it.
Update the documentation.
(aarch64_gdbarch_init): Update call to aarch64_read_description.
* aarch64-tdep.h (aarch64_read_description): Add mte_p parameter.
* arch/aarch64.c: Include ../features/aarch64-mte.c.
(aarch64_create_target_description): Add mte_p parameter and update
the code to use it.
* arch/aarch64.h (aarch64_create_target_description): Add mte_p
parameter.
* features/Makefile (FEATURE_XMLFILES): Add aarch64-mte.xml.
* features/aarch64-mte.c: New file, generated.
* features/aarch64-mte.xml: New file.

gdbserver/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

* linux-aarch64-ipa.cc (get_ipa_tdesc): Update call to
aarch64_linux_read_description.
(initialize_low_tracepoint): Likewise.
* linux-aarch64-low.cc (aarch64_target::low_arch_setup): Take MTE flag
into account.
* linux-aarch64-tdesc.cc (tdesc_aarch64_list): Add one more dimension
for MTE.
(aarch64_linux_read_description): Add mte_p parameter and update to
use it.
* linux-aarch64-tdesc.h (aarch64_linux_read_description): Add mte_p
parameter.

3 years agoAArch64: Add MTE CPU feature check support
Luis Machado [Mon, 15 Jun 2020 16:39:30 +0000 (13:39 -0300)] 
AArch64: Add MTE CPU feature check support

This patch is a preparation for the next patches implementing MTE. It just adds
a HWCAP2 constant for MTE, creates a new generic arch/aarch64-mte-linux.h file
and includes that file in the source files that will use it.

gdb/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

* Makefile.in (HFILES_NO_SRCDIR): Add arch/aarch64-mte-linux.h.
* aarch64-linux-nat.c: Include arch/aarch64-mte-linux.h.
* aarch64-linux-tdep.c: Likewise
* arch/aarch64-mte-linux.h: New file.

gdbserver/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

* linux-aarch64-low.cc: Include arch/aarch64-mte-linux.h.

3 years agoDocumentation for memory tagging remote packets
Luis Machado [Mon, 15 Jun 2020 18:43:03 +0000 (15:43 -0300)] 
Documentation for memory tagging remote packets

Document the remote packet changes to support memory tagging.

gdb/doc/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

* gdb.texinfo (General Query Packets): Document qMemTags and
QMemTags.
Document the "memory-tagging" feature.

3 years agoUnit tests for gdbserver memory tagging remote packets
Luis Machado [Mon, 15 Jun 2020 18:40:47 +0000 (15:40 -0300)] 
Unit tests for gdbserver memory tagging remote packets

Add some unit testing to exercise the functions handling the qMemTags and
QMemTags packets as well as feature support.

gdbserver/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

* server.cc (test_memory_tagging_functions): New function.
(captured_main): Register test_memory_tagging_functions.

3 years agoGDBserver remote packet support for memory tagging
Luis Machado [Mon, 15 Jun 2020 18:34:06 +0000 (15:34 -0300)] 
GDBserver remote packet support for memory tagging

This patch adds the generic remote bits to gdbserver so it can check for memory
tagging support and handle fetch tags and store tags requests.

gdbserver/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

* remote-utils.cc (decode_m_packet_params): Renamed from ...
(decode_m_packet): ... this, which now calls decode_m_packet_params.
(decode_M_packet): Use decode_m_packet_params.
* remote-utils.h (decode_m_packet_params): New prototype.
* server.cc (create_fmemtags_reply, parse_smemtags_request): New
functions.
(handle_general_set): Handle the QMemTags packet.
(parse_fmemtags_request): New function.
(handle_query): Handle the qMemTags packet and advertise memory
tagging support.
(captured_main): Initialize memory tagging flag.
* server.h (struct client_state): Initialize memory tagging flag.
* target.cc (process_stratum_target::supports_memory_tagging)
(process_stratum_target::fetch_memtags)
(process_stratum_target::store_memtags): New methods.
* target.h: Include gdbsupport/byte-vector.h.
(class process_stratum_target) <supports_memory_tagging>
<fetch_memtags, store_memtags>: New class virtual methods.
(target_supports_memory_tagging): Define.

3 years agoUnit testing for GDB-side remote memory tagging handling
Luis Machado [Mon, 15 Jun 2020 18:22:13 +0000 (15:22 -0300)] 
Unit testing for GDB-side remote memory tagging handling

Include some unit testing for the functions handling the new qMemTags and
QMemTags packets.

gdb/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

* remote: Include gdbsupport/selftest.h.
(test_memory_tagging_functions): New function.
(_initialize_remote): Register test_memory_tagging_functions.

3 years agoAdd GDB-side remote target support for memory tagging
Luis Machado [Mon, 15 Jun 2020 18:18:55 +0000 (15:18 -0300)] 
Add GDB-side remote target support for memory tagging

This patch adds memory tagging support to GDB's remote side, with
packet string checks, new packet support and an implementation of
the two new tags methods fetch_atags and store_atags.

GDBserver needs to know how to read/write allocation tags, since that is
done via ptrace.  It doesn't need to know about logical tags.

The new packets are:

qMemTags:<address>:<length>
--

Reads tags from the address range [<address>, <address + length>)

QMemTags:<address>:<length>:<uninterpreted tag bytes>
--
Writes the tags represented by the uninterpreted bytes to the address range
[<address>, <address + length>).

The interpretation of what to do with the tag bytes is up to the arch-specific
code.

Note that these new packets consider the case of packet size overflow as an
error, given the common use case is to read/write only a few memory tags at
a time.  Having to use a couple new packets for multi-part transfers wouldn't
make sense for the little use it would have.

gdb/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

* remote.c (PACKET_memory_tagging_feature): New enum.
(remote_memory_tagging_p): New function.
(remote_protocol_features): New "memory-tagging" entry.
(remote_target::remote_query_supported): Handle memory tagging
support.
(remote_target::supports_memory_tagging): Implement.
(create_fmemtags_request, parse_fmemtags_reply)
(create_smemtags_request): New functions.
(remote_target::fetch_memtags): Implement.
(remote_target::store_memtags): Implement.
(_initialize_remote): Add new "memory-tagging-feature"
config command.

3 years agoNew gdbarch memory tagging hooks
Luis Machado [Fri, 19 Jun 2020 20:36:14 +0000 (17:36 -0300)] 
New gdbarch memory tagging hooks

We need some new gdbarch hooks to help us manipulate memory tags without having
to have GDB calls the target methods directly.

This patch adds the following hooks:

gdbarch_memtag_to_string
--
Returns a printable string corresponding to the tag.

gdbarch_tagged_address_p
--
Checks if a particular address is protected with memory tagging.

gdbarch_memtag_mismatch_p
--
Checks if there is a mismatch between the logical tag of a pointer and the
allocation tag.

gdbarch_set_memtags:
--
Sets either the allocation tag or the logical tag for a particular value.

gdbarch_get_memtag:
--
Gets either the allocation tag or the logical tag for a particular value.

gdbarch_granule_size
--
Sets the memory tag granule size, which represents the number of bytes a
particular allocation tag covers. For example, this is 16 bytes for
AArch64's MTE.

I've used struct value as opposed to straight CORE_ADDR so other architectures
can use the infrastructure without having to rely on fixed types.

gdb/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

* arch-utils.c (default_memtag_to_string, +default_tagged_address_p)
(default_memtag_mismatch_p, default_set_memtags)
(default_get_memtag): New functions.
* arch-utils.h (default_memtag_to_string, default_tagged_address_p)
(default_memtag_mismatch_p, default_set_memtags)
(default_get_memtag): New prototypes.
* gdbarch.c: Regenerate.
* gdbarch.h: Regenerate.
* gdbarch.sh (memtag_to_string, tagged_address_p, memtag_mismatch_p)
(set_memtags, get_memtag, memtag_granule_size): New gdbarch hooks.
(enum memtag_type): New enum.

3 years agoNew target methods for memory tagging support
Luis Machado [Fri, 19 Jun 2020 20:31:23 +0000 (17:31 -0300)] 
New target methods for memory tagging support

This patch starts adding some of the generic pieces to accomodate memory
tagging.

We have three new target methods:

- supports_memory_tagging: Checks if the target supports memory tagging. This
  defaults to false for targets that don't support memory tagging.

- fetch_memtags: Fetches the allocation tags associated with a particular
  memory range [address, address + length).

  The default is to return 0 without returning any tags. This should only
  be called if memory tagging is supported.

- store_memtags: Stores a set of allocation tags for a particular memory
  range [address, address + length).

  The default is to return 0. This should only
  be called if memory tagging is supported.

It also adds a control option for enabling/disabling memory tagging
manually: set memory-tagging on/off.

The default is "on", with GDB making its use conditional to the
architecture supporting memory tagging.

gdb/ChangeLog:

YYYY-MM-DD  Luis Machado  <luis.machado@linaro.org>

* printcmd.c (memtag): New static global.
(show_memtag): New function.
(_initialize_printcmd): Add set/show memory-tagging command.
* remote.c (remote_target) <supports_memory_tagging>: New method
override.
<fetch_memtags>: New method override.
<store_memtags>: New method override.
(remote_target::supports_memory_tagging): New method.
(remote_target::fetch_memtags): New method.
(remote_target::store_memtags): New method.
* target-delegates.c
(dummy_target) <supports_memory_tagging>: New method override.
<fetch_memtags>: New method override.
<store_memtags>: New method override.
(debug_target) <supports_memory_tagging>: New method override.
<fetch_memtags>: New method override.
<store_memtags>: New method override.
(target_ops::supports_memory_tagging): New method.
(target_ops::fetch_memtags): New method.
(target_ops::store_memtags): New method.
(dummy_target::supports_memory_tagging): New method.
(dummy_target::fetch_memtags): New method.
(dummy_target::store_memtags): New method.
(debug_target::supports_memory_tagging): New method.
(debug_target::fetch_memtags): New method.
(debug_target::store_memtags): New method.
* target.h (struct target_ops) <supports_memory_tagging>: New virtual
method.
<fetch_memtags: New virtual method.
<store_memtags>: New virtual method.
(target_supports_memory_tagging): Define.
(target_fetch_memtags): Define.
(target_store_memtags): Define.

3 years agogdb: fix issues with handling DWARF v5 rnglists & .dwo files.
Caroline Tice [Wed, 1 Jul 2020 19:39:08 +0000 (12:39 -0700)] 
gdb: fix issues with handling DWARF v5 rnglists & .dwo files.

While experimenting with GDB on DWARF 5 with split debug (dwo files),
I discovered that GDB was not reading the rnglist index
properly (it needed to be reprocessed in the same way the loclist
index does), and that there was no code for reading rnglists out of
dwo files at all.  Also, the rnglist address reading function
(dwarf2_rnglists_process) was adding the base address to all rnglist
entries, when it's only supposed to add it to the DW_RLE_offset_pair
entries (http://dwarfstd.org/doc/DWARF5.pdf, p. 53), and was not
handling several entry types.

- Added 'reprocessing' for reading rnglist index (as is done for loclist
  index).
- Added code for reading rnglists out of .dwo files.
- Added several missing rnglist forms to dwarf2_rnglists_process.
- Fixed bug that was alwayas adding base address for rnglists (only
  one form needs that).
- Updated dwarf2_rnglists_process to read rnglist out of dwo file when
  appropriate.
- Added new functions cu_debug_rnglist_section & read_rnglist_index.
- Added new testcase, dw5-rnglist-test.{cc,exp}

Special note about the new testcase:

In order for the test case to test anything meaningful, it must be
compiled with clang, not GCC.  The way to do this is as follows:

$ make check RUNTESTFLAGS="CC_FOR_TARGET=/path/to/clang CXX_FOR_TARGET=/path/to/clang++ dw5-rnglist-test.exp"

This following version of clang was used for this testing:

clang version 9.0.1-11
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

Change-Id: I3053c5ddc345720b8ed81e23a88fe537ab38748d

3 years ago[gdb/testsuite] Add pseudo line number program instruction: line
Tom de Vries [Thu, 16 Jul 2020 13:34:00 +0000 (15:34 +0200)] 
[gdb/testsuite] Add pseudo line number program instruction: line

There's an idiom in dwarf assembly test-cases:
...
set line1 [gdb_get_line_number "line 1"]
set line2 [gdb_get_line_number "line 2"]
set line3 [gdb_get_line_number "line 3"]
  ...
            {DW_LNS_advance_line [expr $line1 - 1]}
  ...
            {DW_LNS_advance_line [expr $line2 - $line1]}
  ...
            {DW_LNS_advance_line [expr $line3 - $line2]}
...

Add a pseudo line number program instruction "line", such that we can simply
write:
...
            {line $line1}
  ...
            {line $line2}
  ...
            {line $line3}
...

Build and reg-tested on x86_64-linux.

gdb/testsuite/ChangeLog:

2020-07-16  Tom de Vries  <tdevries@suse.de>

* lib/dwarf.exp (program): Initialize _line.
(DW_LNE_end_sequence): Reinitialize _line.
(DW_LNS_advance_line): Update _line.
(line): New proc.
* gdb.dwarf2/dw2-inline-many-frames.exp: Use line.
* gdb.dwarf2/dw2-inline-small-func.exp: Same.
* gdb.dwarf2/dw2-inline-stepping.exp: Same.
* gdb.dwarf2/dw2-is-stmt-2.exp: Same.
* gdb.dwarf2/dw2-is-stmt.exp: Same.
* gdb.dwarf2/dw2-ranges-func.exp: Same.

3 years agoAutomatic date update in version.in
GDB Administrator [Thu, 16 Jul 2020 00:00:06 +0000 (00:00 +0000)] 
Automatic date update in version.in

3 years agogdb/testsuite: Update test pattern in ptype-on-functions.exp
Andrew Burgess [Wed, 15 Jul 2020 15:05:24 +0000 (16:05 +0100)] 
gdb/testsuite: Update test pattern in ptype-on-functions.exp

It was pointed out that the recently added test
gdb.fortran/ptype-on-functions.exp fails on older versions of
gfortran.  This is because the ABI for passing string lengths changed
from a 4-byte to 8-byte value (on some targets).

This change is documented here:
  https://gcc.gnu.org/gcc-8/changes.html.

  Character variables longer than HUGE(0) elements are now possible on
  64-bit targets. Note that this changes the procedure call ABI for
  all procedures with character arguments on 64-bit targets, as the
  type of the hidden character length argument has changed. The hidden
  character length argument is now of type INTEGER(C_SIZE_T).

This commit just relaxes the pattern to accept any size of integer for
the string length argument.

gdb/testsuite/ChangeLog:

* gdb.fortran/ptype-on-functions.exp: Make the result pattern more
generic.

3 years agox86: Don't display eiz with no scale
H.J. Lu [Wed, 15 Jul 2020 13:49:45 +0000 (06:49 -0700)] 
x86: Don't display eiz with no scale

Change

67 48 8b 1c 25 ef cd ab 89  mov    0x89abcdef(,%eiz,1),%rbx

to

67 48 8b 1c 25 ef cd ab 89  mov    0x89abcdef,%rbx

in AT&T syntax and

67 48 8b 1c 25 ef cd ab 89  mov    rbx,QWORD PTR [eiz*1+0x89abcdef]

to

67 48 8b 1c 25 ef cd ab 89  mov    rbx,QWORD PTR ds:0x89abcdef

in Intel syntax.

gas/

PR gas/26237
* testsuite/gas/i386/evex-no-scale-64.d: Updated.
* testsuite/gas/i386/addr32.d: Likewise.
* testsuite/gas/i386/x86-64-addr32-intel.d: Likewise.
* testsuite/gas/i386/x86-64-addr32.d: Likewise.

opcodes/

PR gas/26237
* i386-dis.c (OP_E_memory): Don't display eiz with no scale
without base nor index registers.

3 years agoFix the generation of relocs for missing build notes.
Nick Clifton [Wed, 15 Jul 2020 11:52:53 +0000 (12:52 +0100)] 
Fix the generation of relocs for missing build notes.

* write.c (create_note_reloc): Add desc2_size parameter.  Zero out
the addend field of REL relocations.  Store the full addend into
the note for REL relocations.

3 years agoPR26239, memory leak in _bfd_dwarf2_slurp_debug_info
Alan Modra [Wed, 15 Jul 2020 01:29:20 +0000 (10:59 +0930)] 
PR26239, memory leak in _bfd_dwarf2_slurp_debug_info

PR 26239
* coffgen.c (_bfd_coff_close_and_cleanup): Free dwarf2 info.

3 years agoFix an illegal memory access in the BFD library which can be triggered by attempting...
Nick Clifton [Wed, 15 Jul 2020 10:09:59 +0000 (11:09 +0100)] 
Fix an illegal memory access in the BFD library which can be triggered by attempting to parse a corrupt PE format file.

PR26240
* coffgen.c (coff_get_normalized_symtab): Fix off-by-one error in
check for aux entries that overflow the buufer.

3 years ago[gdb/testsuite] Handle callq -> call disassembly change
Tom de Vries [Wed, 15 Jul 2020 09:17:41 +0000 (11:17 +0200)] 
[gdb/testsuite] Handle callq -> call disassembly change

We're currently running into:
...
FAIL: gdb.trace/entry-values.exp: disassemble bar
...

Since commit 36938cabf0 "x86: avoid attaching suffixes to unambiguous insns",
"callq" is disassembled as "call", and the test-case expects "callq".

Fix this by expecting "call" instead.

Tested on x86_64-linux.

gdb/testsuite/ChangeLog:

2020-07-15  Tom de Vries  <tdevries@suse.de>

* gdb.trace/entry-values.exp: Expect "call" instead of "callq" if
is_amd64_regs_target.

3 years agogdb/fortran: Handle dynamic string types when printing types
Andrew Burgess [Tue, 14 Jul 2020 09:10:07 +0000 (10:10 +0100)] 
gdb/fortran: Handle dynamic string types when printing types

After commit:

  commit 8c2e4e0689ea244d0ed979171a3d09c9176b8175
  Date:   Sun Jul 12 22:58:51 2020 -0400

      gdb: add accessors to struct dynamic_prop

An existing bug was exposed in the Fortran type printing code.  When
GDB is asked to print the type of a function that takes a dynamic
string argument GDB will try to read the upper bound of the string.

The read of the upper bound is written as:

    if (type->bounds ()->high.kind () == PROP_UNDEFINED)
      // Treat the upper bound as unknown.
    else
      // Treat the upper bound as known and constant.

However, this is not good enough.  When printing a function type the
dynamic argument types will not have been resolved.  As a result the
dynamic property is not PROP_UNDEFINED, but nor is it constant.

By rewriting this code to specifically check for the PROP_CONST case,
and treating all other cases as the upper bound being unknown we avoid
incorrectly treating the dynamic property as being constant.

gdb/ChangeLog:

* f-typeprint.c (f_type_print_base): Allow for dynamic types not
being resolved.

gdb/testsuite/ChangeLog:

* gdb.fortran/ptype-on-functions.exp: Add more tests.
* gdb.fortran/ptype-on-functions.f90: Likewise.

3 years agox86: move putop() case labels to restore alphabetic sorting
Jan Beulich [Wed, 15 Jul 2020 06:55:53 +0000 (08:55 +0200)] 
x86: move putop() case labels to restore alphabetic sorting

... as far as non-fall-through behavior permits.

3 years agox86: make PUSH/POP disassembly uniform
Jan Beulich [Wed, 15 Jul 2020 06:55:20 +0000 (08:55 +0200)] 
x86: make PUSH/POP disassembly uniform

Irrespective of their encoding the resulting output should look the
same. Therefore wire the handling of PUSH/POP with GPR operands
encoded in the main opcode byte to the same logic used for other
operands. This frees up yet another macro character.

3 years agox86-64: adjust stack insn test case
Jan Beulich [Wed, 15 Jul 2020 06:54:40 +0000 (08:54 +0200)] 
x86-64: adjust stack insn test case

The value chosen for the 16-/32-bit immediate cases didn't work well
with the subsequent insn's REX prefix - we ought to pick a value the
upper two bytes of which evaluate to a 2-byte insn. Bump the values
accordingly, allowing the subsequent insn to actually have the intended
REX.W.

3 years agox86: avoid attaching suffixes to unambiguous insns
Jan Beulich [Wed, 15 Jul 2020 06:53:55 +0000 (08:53 +0200)] 
x86: avoid attaching suffixes to unambiguous insns

"Unambiguous" is is in particular taking as reference the assembler,
which also accepts certain insns - despite them allowing for varying
operand size, and hence in principle being ambiguous - without any
suffix. For example, from the very beginning of the life of x86-64 I had
trouble understanding why a plain and simple RET had to be printed as
RETQ. In case someone really used the 16-bit form, RETW disambiguates
the two quite fine.

3 years agommix bfd: fix bfd_assert for R_MMIX_PUSHJ_STUBBABLE against undef'd symbol
Hans-Peter Nilsson [Wed, 15 Jul 2020 04:22:28 +0000 (06:22 +0200)] 
mmix bfd: fix bfd_assert for R_MMIX_PUSHJ_STUBBABLE against undef'd symbol

Spotted when inspecting gcc testsuite logs, but this already is
covered by the ld-mmix testsuite, it's just that the assert is ignored
since the regexp match is for a substring and not anchored.

With the anchors added but not the bugfix, the ld.log shows that the
asserts cause a non-match as intended:

Executing on host: sh -c {./ld-new   -LX/src/ld/testsuite/ld-mmix  -m elf64mmix -o tmpdir/dump tmpdir/undef-2.o tmpdir/start.o  2>&1}  /dev/null dump.tmp (timeout = 300)
./ld-new: BFD (GNU Binutils) 2.34.50.20200629 assertion fail X/src/bfd/elf64-mmix.c:2845
./ld-new: BFD (GNU Binutils) 2.34.50.20200629 assertion fail X/src/bfd/elf64-mmix.c:2845
./ld-new: BFD (GNU Binutils) 2.34.50.20200629 assertion fail X/src/bfd/elf64-mmix.c:2845
./ld-new: tmpdir/undef-2.o:(.text+0x0): undefined reference to `undefd'
failed with: <./ld-new: BFD (GNU Binutils) 2.34.50.20200629 assertion fail X/src/bfd/elf64-mmix.c:2845
./ld-new: BFD (GNU Binutils) 2.34.50.20200629 assertion fail X/src/bfd/elf64-mmix.c:2845
./ld-new: BFD (GNU Binutils) 2.34.50.20200629 assertion fail X/src/bfd/elf64-mmix.c:2845
./ld-new: tmpdir/undef-2.o:(.text+0x0): undefined reference to `undefd'>, expected: <\A[^\n\r]*undefined reference to `undefd'\Z>
FAIL: ld-mmix/undef-2

Gone with the fix of course, leaving just the intended "undefined
reference" like.

I'm not going to add anchors manually for all the "error:" strings in
the test-suite, not even in the mmix parts.  Sorry, but I'll just do
it for *these* specific undefined-reference tests.

Just a thought: maybe the run_dump_test "error:" string should
*automatically* get anchor marks prepended and appended for a single
line match as in the patch, "\A[^\n\r]*" prepended and \Z appended
unless either anchor mark or \r or \n is present in the regexp?

Committed.

bfd:
* elf64-mmix.c (mmix_elf_relax_section): Improve accounting for
R_MMIX_PUSHJ_STUBBABLE relocs against undefined symbols.

ld/testsuite:
* testsuite/ld-mmix/undef-1.d, testsuite/ld-mmix/undef-1m.d,
testsuite/ld-mmix/undef-2.d, testsuite/ld-mmix/undef-2m.d: Add
start- and end-anchors to error-string to match just a
single-line error-message.

3 years agogdb/testsuite/lib/dwarf.exp: fix addr_size parameter comments
Simon Marchi [Wed, 15 Jul 2020 03:18:01 +0000 (23:18 -0400)] 
gdb/testsuite/lib/dwarf.exp: fix addr_size parameter comments

The comments modified in this patch claim that the addr_size parameters
can take the value 32 or 64 (suggesting the value is in bits).  In fact,
the expected value is in bytes, either 4 or 8.

The actual value in the DWARF info is in bytes.  And we can see that the
default values used (if addr_size == "default") are:

if {$_cu_addr_size == "default"} {
    if {[is_64_target]} {
set _cu_addr_size 8
    } else {
set _cu_addr_size 4
    }
}

gdb/testsuite/ChangeLog:

* lib/dwarf.exp (Dwarf::cu, Dwarf::tu, Dwarf::lines): Change valid
values in documentation for addr_size to 4 and 8.

Change-Id: I4a02dca2bb7992198864e545ef099f020f54ff2f

3 years agoAutomatic date update in version.in
GDB Administrator [Wed, 15 Jul 2020 00:00:06 +0000 (00:00 +0000)] 
Automatic date update in version.in

3 years agoPR26198 MinGW failure to link regular object file and LTO object file
Markus Böck [Tue, 14 Jul 2020 23:01:30 +0000 (08:31 +0930)] 
PR26198 MinGW failure to link regular object file and LTO object file

PR 26198
* coffgen.c (_bfd_coff_section_already_linked): Allow for plugin
objects both before and after normal object files.
* elflink.c (_bfd_elf_section_already_linked): Likewise.

3 years agox86-64: Zero-extend lower 32 bits displacement to 64 bits
H.J. Lu [Tue, 14 Jul 2020 16:57:52 +0000 (09:57 -0700)] 
x86-64: Zero-extend lower 32 bits displacement to 64 bits

Since the addr32 (0x67) prefix zero-extends the lower 32 bits address to
64 bits, change disassembler to zero-extend the lower 32 bits displacement
to 64 bits when there is no base nor index registers.

gas/

PR gas/26237
* testsuite/gas/i386/addr32.s: Add tests for 32-bit wrapped around
address.
* testsuite/gas/i386/x86-64-addr32.s: Likewise.
* testsuite/gas/i386/addr32.d: Updated.
* testsuite/gas/i386/x86-64-addr32-intel.d: Likewise.
* testsuite/gas/i386/x86-64-addr32.d: Likewise.
* testsuite/gas/i386/ilp32/x86-64-addr32-intel.d: Likewise.
* testsuite/gas/i386/ilp32/x86-64-addr32.d: Likewise.

opcodes/

PR gas/26237
* i386-dis.c (OP_E_memory): Without base nor index registers,
32-bit displacement to 64 bits.

3 years agogdb: Improve formatting of 'show endian' messages
Andrew Burgess [Fri, 3 Jul 2020 10:22:03 +0000 (11:22 +0100)] 
gdb: Improve formatting of 'show endian' messages

This commit changes the output of 'show endian'.  Here is a
session before this commit:

    (gdb) show endian
    The target endianness is set automatically (currently little endian)
    (gdb) set endian big
    The target is assumed to be big endian
    (gdb) show endian
    The target is assumed to be big endian
    (gdb)

After this commit the session now looks like this:

    (gdb) show endian
    The target endianness is set automatically (currently little endian).
    (gdb) set endian big
    The target is set to big endian.
    (gdb) show endian
    The target is set to big endian.
    (gdb)

The changes are:

  1. Each line ends with '.', and
  2. After setting the endianness GDB is now a little more assertive;
  'target is set to' not 'target is assumed to be', the user did just
  tell us after all!

3 years agogdb: Improve formatting of 'show architecture' messages
Andrew Burgess [Thu, 25 Jun 2020 16:59:34 +0000 (17:59 +0100)] 
gdb: Improve formatting of 'show architecture' messages

This commit changes the output of 'show architecture'.  Here is a
session before this commit:

  (gdb) show architecture
  The target architecture is set automatically (currently i386)
  (gdb) set architecture mips
  The target architecture is assumed to be mips
  (gdb) show architecture
  The target architecture is assumed to be mips
  (gdb)

After this commit the session now looks like this:

  (gdb) show architecture
  The target architecture is set to "auto" (currently "i386").
  (gdb) set architecture mips
  The target architecture is set to "mips".
  (gdb) show architecture
  The target architecture is set to "mips".
  (gdb)

The changes are:

  1. The value is now enclosed in quotes,
  2. Each line ends with '.', and
  3. After setting the architecture GDB is now a little more
  assertive; 'architecture is set to' not 'is assumed to be', the user
  did just tell us after all!

gdb/ChangeLog:

* arch-utils.c (show_architecture): Update formatting of messages.

gdb/testsuite/ChangeLog:

* gdb.arch/amd64-osabi.exp: Update.
* gdb.arch/arm-disassembler-options.exp: Update.
* gdb.arch/powerpc-disassembler-options.exp: Update.
* gdb.arch/ppc64-symtab-cordic.exp: Update.
* gdb.arch/s390-disassembler-options.exp: Update.
* gdb.base/all-architectures.exp.tcl: Update.
* gdb.base/attach-pie-noexec.exp: Update.
* gdb.base/catch-syscall.exp: Update.
* gdb.xml/tdesc-arch.exp: Update.

3 years agoarc: Detect usage of illegal double register pairs
Claudiu Zissulescu [Tue, 14 Jul 2020 11:51:15 +0000 (14:51 +0300)] 
arc: Detect usage of illegal double register pairs

ARC can use odd-even double register pairs in some selected
instructions. Although the GNU assembler doesn't allow even-odd
registers to be used, there may be cases when the disassembler is
presented with such situation.  This patch add a test and detects such
cases.

opcodes/
2020-07-14  Claudiu Zissulescu  <claziss@gmail.com>

* arc-dis.c (print_insn_arc): Detect and emit a warning when a
faulty double register pair is detected.

binutils/
2020-07-14  Claudiu Zissulescu  <claziss@gmail.com>

* testsuite/binutils-all/arc/double_regs.s: New test.
* testsuite/binutils-all/arc/objdump.exp: Add the above test.

Signed-off-by: Claudiu Zissulescu <claziss@gmail.com>
3 years agox86/Intel: debug registers are named DRn
Jan Beulich [Tue, 14 Jul 2020 08:43:38 +0000 (10:43 +0200)] 
x86/Intel: debug registers are named DRn

%db<n> is an AT&T invention; the Intel documentation and MASM have only
ever specified DRn (in line with CRn and TRn). (In principle gas also
shouldn't accept the names in Intel mode, but at least for now I've kept
things as they are. Perhaps as a first step this should just be warned
about.)

3 years agox86: drop Rm and the 'L' macro
Jan Beulich [Tue, 14 Jul 2020 08:43:03 +0000 (10:43 +0200)] 
x86: drop Rm and the 'L' macro

Rm (and hence OP_R()) can be dropped by making 'Z' force modrm.mod to 3
(for OP_E()) instead of ignoring it. While at it move 'Z' handling to
its designated place (after 'Y'; 'W' handling will be moved by a later
change).

Moves to/from TRn are illegal in 64-bit mode and thus get converted to
honor this at the same time (also getting them in line with moves
to/from CRn/DRn ModRM.mod handling wise). This then also frees up the L
macro.

3 years agox86: drop Rdq, Rd, and MaskR
Jan Beulich [Tue, 14 Jul 2020 08:42:33 +0000 (10:42 +0200)] 
x86: drop Rdq, Rd, and MaskR

Rdq, Rd, and MaskR can be replaced by Edq, Ed / Rm, and MaskE
respectively, as OP_R() doesn't enforce ModRM.mod == 3, and hence where
MOD matters but hasn't been decoded yet it needs to be anyway. (The case
of converting to Rm is temporary until a subsequent change.)

3 years agox86: simplify decode of opcodes valid only without any (embedded) prefix
Jan Beulich [Tue, 14 Jul 2020 08:42:03 +0000 (10:42 +0200)] 
x86: simplify decode of opcodes valid only without any (embedded) prefix

In this case there's no need to go through prefix_table[] at all - the
.prefix_requirement == PREFIX_OPCODE machinery takes care of this case
already.

A couple of further adjustments are needed though:
- Gv / Ev and alike then can't be used (needs to be Gdq / Edq instead),
- dq_mode and friends shouldn't lead to PREFIX_DATA getting set in
  used_prefixes.

3 years agox86: also use %BW / %DQ for kshift*
Jan Beulich [Tue, 14 Jul 2020 08:41:30 +0000 (10:41 +0200)] 
x86: also use %BW / %DQ for kshift*

3 years ago[gdb/testsuite] Fix clone-new-thread-event.c with glibc 2.30
Tom de Vries [Tue, 14 Jul 2020 08:36:17 +0000 (10:36 +0200)] 
[gdb/testsuite] Fix clone-new-thread-event.c with glibc 2.30

Starting glibc 2.30, unistd.h declares gettid (for _GNU_SOURCE).

This clashes with a static gettid in test source
clone-new-thread-event.c:
...
gdb compile failed, gdb.threads/clone-new-thread-event.c:46:1: error: \
  static declaration of 'gettid' follows non-static declaration
   46 | gettid (void)
      | ^~~~~~
In file included from /usr/include/unistd.h:1170,
                 from gdb.threads/clone-new-thread-event.c:27:
/usr/include/bits/unistd_ext.h:34:16: note: previous declaration of 'gettid' \
  was here
   34 | extern __pid_t gettid (void) __THROW;
      |                ^~~~~~
...

Fix this by renaming the static gettid to local_gettid.

Tested on x86_64-linux.

gdb/testsuite/ChangeLog:

2020-07-14  Tom de Vries  <tdevries@suse.de>

* gdb.threads/clone-new-thread-event.c (gettid): Rename to ...
(local_gettid): ... this.
(fn): Update.

3 years agox86: simplify decode of opcodes valid with (embedded) 66 prefix only
Jan Beulich [Tue, 14 Jul 2020 08:33:40 +0000 (10:33 +0200)] 
x86: simplify decode of opcodes valid with (embedded) 66 prefix only

The only valid (embedded or explicit) prefix being the data size one
(which is a fairly common pattern), avoid going through prefix_table[].
Instead extend the "required prefix" logic to also handle PREFIX_DATA
alone in a table entry, now used to identify this case. This requires
moving the (adjusted) ->prefix_requirement logic ahead of the printing
of stray prefixes, as the latter needs to observe the new setting of
PREFIX_DATA in used_prefixes.

Also add PREFIX_OPCODE on related entries when previously there was
mistakenly no decode step through prefix_table[].

3 years agox86: drop further EVEX table entries that can be served by VEX ones
Jan Beulich [Tue, 14 Jul 2020 08:32:51 +0000 (10:32 +0200)] 
x86: drop further EVEX table entries that can be served by VEX ones

A few cases were missed by 6df22cf64c93 ("x86: drop EVEX table entries
that can be served by VEX ones").

3 years agox86: drop need_vex_reg
Jan Beulich [Tue, 14 Jul 2020 08:32:19 +0000 (10:32 +0200)] 
x86: drop need_vex_reg

It was quite odd for the prior operand handling to have to clear this
flag for the actual operand handling to print nothing. Have the actual
operand handling determine whether the operand is actually present.
With this {d,q}_scalar_swap_mode become unused and hence also get dropped.

3 years agox86: drop Vex128 and Vex256
Jan Beulich [Tue, 14 Jul 2020 08:31:49 +0000 (10:31 +0200)] 
x86: drop Vex128 and Vex256

These are only used when VEX.L or EVEX.L'L have already been decoded,
and hence the "normal" length dependent name determination is quite
fine. Adjust a few enumerators to make clear that vex_len_table[] has
been consulted; be consistent and do so for all *f128 and *i128 insns
in one go.

3 years agox86: replace %LW by %DQ
Jan Beulich [Tue, 14 Jul 2020 08:30:26 +0000 (10:30 +0200)] 
x86: replace %LW by %DQ

This makes more visible what the two alternatives will be that result
from this macro.

3 years agox86: merge/move logic determining the EVEX disp8 shift
Jan Beulich [Tue, 14 Jul 2020 08:29:55 +0000 (10:29 +0200)] 
x86: merge/move logic determining the EVEX disp8 shift

Fold redundant case blocks and move the extra adjustments logic into
the single case block that actually needs it - there's no need to go
through the extra logic for all the other cases. Also utilize there that
vex.b cannot be set at this point, due to earlier logic. Reduce the
comment there, which was partly stale anyway.

3 years agox86: extend %BW use to VP{COMPRESS,EXPAND}{B,W}
Jan Beulich [Tue, 14 Jul 2020 08:29:25 +0000 (10:29 +0200)] 
x86: extend %BW use to VP{COMPRESS,EXPAND}{B,W}

Unlike the earlier ones these also need their operands adjusted. Replace
the (mis-described: there's nothing "scalar" here) {b,w}_scalar_mode by
a single new mode, with the actual unit width controlled by EVEX.W.

3 years agox86-64: fix {,V}PCMPESTR{I,M} disassembly in Intel mode
Jan Beulich [Tue, 14 Jul 2020 08:28:12 +0000 (10:28 +0200)] 
x86-64: fix {,V}PCMPESTR{I,M} disassembly in Intel mode

The operands don't allow disambiguating the insn in 64-bit mode, and
hence suffixes need to be emitted not just in AT&T mode. Achieve this
by re-using %LQ while dropping PCMPESTR_Fixup().

3 years agox86: fold VCMP_Fixup() into CMP_Fixup()
Jan Beulich [Tue, 14 Jul 2020 08:27:32 +0000 (10:27 +0200)] 
x86: fold VCMP_Fixup() into CMP_Fixup()

There's no reason to have two functions and two tables, when the AVX
functionality here is a proper superset of the SSE one.

3 years agox86: don't disassemble MOVBE with two suffixes
Jan Beulich [Tue, 14 Jul 2020 08:26:51 +0000 (10:26 +0200)] 
x86: don't disassemble MOVBE with two suffixes

MOVBE_Fixup() is entirely redundant with the S macro already used on the
mnemonics, leading to double suffixes in suffix-always mode. Drop the
function.

3 years agox86: avoid attaching suffix to register-only CRC32
Jan Beulich [Tue, 14 Jul 2020 08:25:43 +0000 (10:25 +0200)] 
x86: avoid attaching suffix to register-only CRC32

Just like other insns with GPR operands, CRC32 with only register
operands should not get a suffix added unless in suffix-always mode.
Do away with CRC32_Fixup() altogether, using other more generic logic
instead.

3 years agox86-64: don't hide an empty but meaningless REX prefix
Jan Beulich [Tue, 14 Jul 2020 08:24:26 +0000 (10:24 +0200)] 
x86-64: don't hide an empty but meaningless REX prefix

Unlike for non-zero values passed to USED_REX(), where rex_used gets
updated only when the respective bit was actually set in the encoding,
zero getting passed in is not further guarded, yet such a (potentially
"empty") REX prefix takes effect only when there are registers numbered
4 and up.

3 years agox86: drop dead code from OP_IMREG()
Jan Beulich [Tue, 14 Jul 2020 08:23:36 +0000 (10:23 +0200)] 
x86: drop dead code from OP_IMREG()

There's only a very limited set of modes that this function gets invoked
with - avoid it being more generic than it needs to be. This may, down
the road, allow actually doing away with the function altogether.

This eliminates a first improperly used "USED_REX (0)".

3 years agox86-64: fold ILP32 test expectations
Jan Beulich [Tue, 14 Jul 2020 08:22:45 +0000 (10:22 +0200)] 
x86-64: fold ILP32 test expectations

Various of the test expectations get adjusted later in this and a
subsequent series, so in order to avoid having to adjust more instances
than necessary fold respective test ILP32 expectations with their main
64-bit counterparts where they're identical anyway.

3 years agogdbserver: fix memory leak when handling qsupported packet
Simon Marchi [Tue, 14 Jul 2020 02:27:00 +0000 (22:27 -0400)] 
gdbserver: fix memory leak when handling qsupported packet

When building gdbserver with AddressSanitizer, I get this annoying
little leak when gdbserver exits:

==307817==ERROR: LeakSanitizer: detected memory leaks

    Direct leak of 14 byte(s) in 1 object(s) allocated from:
        #0 0x7f7fd4256459 in __interceptor_malloc /build/gcc/src/gcc/libsanitizer/asan/asan_malloc_linux.cpp:145
        #1 0x563bef981b80 in xmalloc /home/simark/src/binutils-gdb/gdbserver/../gdb/alloc.c:60
        #2 0x563befb53301 in xstrdup /home/simark/src/binutils-gdb/libiberty/xstrdup.c:34
        #3 0x563bef9d742b in handle_query /home/simark/src/binutils-gdb/gdbserver/server.cc:2286
        #4 0x563bef9ed0b7 in process_serial_event /home/simark/src/binutils-gdb/gdbserver/server.cc:4061
        #5 0x563bef9f1d9e in handle_serial_event(int, void*) /home/simark/src/binutils-gdb/gdbserver/server.cc:4402
        #6 0x563befb0ec65 in handle_file_event /home/simark/src/binutils-gdb/gdbsupport/event-loop.cc:548
        #7 0x563befb0f49f in gdb_wait_for_event /home/simark/src/binutils-gdb/gdbsupport/event-loop.cc:673
        #8 0x563befb0d4a1 in gdb_do_one_event() /home/simark/src/binutils-gdb/gdbsupport/event-loop.cc:215
        #9 0x563bef9e721a in start_event_loop /home/simark/src/binutils-gdb/gdbserver/server.cc:3484
        #10 0x563bef9eb90a in captured_main /home/simark/src/binutils-gdb/gdbserver/server.cc:3875
        #11 0x563bef9ec2c7 in main /home/simark/src/binutils-gdb/gdbserver/server.cc:3961
        #12 0x7f7fd3330001 in __libc_start_main (/usr/lib/libc.so.6+0x27001)

    SUMMARY: AddressSanitizer: 14 byte(s) leaked in 1 allocation(s).

This is due to the handling of unknown qsupported features in
handle_query.  The `qsupported` vector is built, containing all the
feature names received from GDB.  As we iterate on them, when we
encounter unknown ones, we move them at the beginning of the vector, in
preparation of passing this vector of unknown features down to the
target (which may know about them).

When moving these unknown features to other slots in the vector, we
overwrite other pointers without freeing them, which therefore leak.

An easy fix would be to add a `free` when doing the move.  However, I
think this is a good opportunity to sprinkle a bit of automatic memory
management in this code.

So, use a vector of std::string which owns all the entries.  And use a
separate vector (that doesn't own the entries) for the unknown ones,
which is then passed to target_process_qsupported.

Given that the `c_str` method of std::string returns a `const char *`,
it follows that process_stratum_target::process_qsupported must accept a
`const char **` instead of a `char **`.  And while at it, change the
pointer + size paramters to use an array_view instead.

gdbserver/ChangeLog:

* server.cc (handle_query): Use std::vector of
std::string for `qsupported` vector.  Use separate
vector for unknowns.
* target.h (class process_stratum_target) <process_qsupported>:
Change parameters to array_view of const char *.
(target_process_qsupported): Remove `count` parameter.
* target.cc (process_stratum_target::process_qsupported): Change
parameters to array_view of const char *.
* linux-x86-low.cc (class x86_target) <process_qsupported>:
Likewise.

Change-Id: I97f133825faa6d7abbf83a58504eb0ba77462812

3 years agoAutomatic date update in version.in
GDB Administrator [Tue, 14 Jul 2020 00:00:12 +0000 (00:00 +0000)] 
Automatic date update in version.in

3 years agoFix frame-apply.html collision in GDB manual.
Paul Carroll [Mon, 13 Jul 2020 19:31:05 +0000 (12:31 -0700)] 
Fix frame-apply.html collision in GDB manual.

The addition of an anchor for the "frame apply" command was causing
the HTML documentation to include files named both "frame-apply.html"
and "Frame-Apply.html", which collide on case-insensitive file
systems.  This patch removes the redundant anchor and adjusts the two
xrefs to it.

2020-07-13 Paul Carroll <pcarroll@codesourcery.com>

PR gdb/25716

gdb/doc/
* gdb.texinfo (Frame Apply): Remove anchor for 'frame
apply' and adjust xrefs to it.

3 years agoSkip directory tests in gdb.base/info_sources.exp on remote host
Sandra Loosemore [Mon, 13 Jul 2020 18:34:04 +0000 (11:34 -0700)] 
Skip directory tests in gdb.base/info_sources.exp on remote host

When testing on a remote host, source files from build are copied to
an arbitrary location on host.  Tests that try to pattern-match host
pathnames against directory prefixes on build don't generally work.

2020-07-13  Sandra Loosemore  <sandra@codesourcery.com>

gdb/testsuite/
* gdb.base/info_sources.exp: Skip directory match tests on
remote hosts.

3 years agox86: Remove 32-bit sign extension in offset_in_range
H.J. Lu [Mon, 13 Jul 2020 17:18:39 +0000 (10:18 -0700)] 
x86: Remove 32-bit sign extension in offset_in_range

When encoding a 32-bit offset, there is no need to sign-extend it to 64
bits since only the lower 32 bits are used.

* config/tc-i386.c (offset_in_range): Remove 32-bit sign
extension.

3 years agoFix gdb.base/savedregs.exp with clang
Gary Benson [Mon, 13 Jul 2020 15:01:00 +0000 (16:01 +0100)] 
Fix gdb.base/savedregs.exp with clang

gdb.base/savedregs.exp fails to run with clang, because of:

 gdb compile failed, /gdbtest/src/gdb/testsuite/gdb.base/savedregs.c:36:37:
       warning: operator '<<' has lower precedence than '+'; '+' will be
       evaluated first [-Wshift-op-parentheses]
   return callee (a1 << a2 * a3 / a4 + a6 & a6 % a7 - a8) + done;
                     ~~ ~~~~~~~~~~~~~^~~~
 /gdbtest/build/gdb/testsuite/gdb.base/savedregs.c:36:37: note: place
       parentheses around the '+' expression to silence this warning
   return callee (a1 << a2 * a3 / a4 + a6 & a6 % a7 - a8) + done;
                                     ^
                        (                )
 1 warning generated.

This commit fixes it by adding the suggested parentheses.

gdb/testsuite/ChangeLog:

* gdb.base/savedregs.exp (caller): Use parentheses to
make expected expression evaluation ordering explicit.

3 years agoFix gdb.arch/i386-sse.exp with clang
Gary Benson [Mon, 13 Jul 2020 14:14:07 +0000 (15:14 +0100)] 
Fix gdb.arch/i386-sse.exp with clang

gdb.arch/i386-sse.exp fails to run with clang, because of:

 gdb compile failed, /gdbtest/src/gdb/testsuite/gdb.arch/i386-sse.c:56:40: warning:
       passing 'int *' to parameter of type 'unsigned int *' converts between
       pointers to integer types with different sign [-Wpointer-sign]
   if (!x86_cpuid (1, NULL, NULL, NULL, &edx))
                                        ^~~~
 /gdbtest/src/gdb/testsuite/../nat/x86-cpuid.h:35:41: note: passing
       argument to parameter '__edx' here
             unsigned int *__ecx, unsigned int *__edx)
                                               ^
 1 warning generated.

Fix it by declaring edx unsigned.

gdb/testsuite/ChangeLog:

* gdb.arch/i386-sse.c (have_sse) <edx>: Make unsigned.

3 years agoUpdated French translation for the gas/ and binutils/ sub-directories
Nick Clifton [Mon, 13 Jul 2020 13:49:58 +0000 (14:49 +0100)] 
Updated French translation for the gas/ and binutils/ sub-directories

3 years agoUse volatile pointers when attempting to trigger SIGSEGVs
Gary Benson [Mon, 13 Jul 2020 13:47:44 +0000 (14:47 +0100)] 
Use volatile pointers when attempting to trigger SIGSEGVs

Clang fails to compile a number of files with the following warning:
indirection of non-volatile null pointer will be deleted, not trap
[-Wnull-dereference].  This commit qualifies the relevant pointers
with 'volatile'.

gdb/testsuite/ChangeLog:

* gdb.base/bigcore.c (main): Use a volatile pointer when
attempting to trigger a SIGSEGV.
* gdb.base/gcore-relro-pie.c (break_here): Likewise.
* gdb.base/gcore-tls-pie.c (break_here): Likewise.
* gdb.base/savedregs.c (thrower): Likewise.
* gdb.mi/mi-syn-frame.c (bar): Likewise.

3 years agoSkip VLA structure field tests when compiling with clang
Gary Benson [Mon, 13 Jul 2020 13:47:44 +0000 (14:47 +0100)] 
Skip VLA structure field tests when compiling with clang

Clang fails to compile gdb.base/vla-datatypes.c with the following
error: fields must have a constant size: 'variable length array in
structure' extension will never be supported.  This commit factors
the affected tests out into a new testcase, vla-struct-fields.{exp,c},
which is skipped when the testcase is compiled using clang,

gdb/testsuite/ChangeLog:

* gdb.base/vla-datatypes.c (vla_factory): Factor out sections
defining and using VLA structure fields into...
* gdb.base/vla-struct-fields.c: New file.
* gdb.base/vla-datatypes.exp: Factor out VLA structure field
tests into...
* gdb.base/vla-struct-fields.exp: New file.

3 years agox86_64-cygwin tests
Alan Modra [Mon, 13 Jul 2020 10:27:28 +0000 (19:57 +0930)] 
x86_64-cygwin tests

Tests just having "xfail: x86_64-*-cygwin" aren't good, since
presumably if a test fails on x86_64-cygwin then it also fails on
x86_64-*-pe* and x86_64-*-mingw*.

binutils/
* testsuite/lib/binutils-common.exp (is_pecoff_format): Accept
optional machine-os arg.
ld/
* testsuite/ld-scripts/default-script1.d: Don't skip, xfail
using is_pecoff_format.
* testsuite/ld-scripts/default-script2.d: Likewise.
* testsuite/ld-scripts/default-script3.d: Likewise.
* testsuite/ld-scripts/default-script4.d: Likewise.
* testsuite/ld-scripts/pr20302.d: Remove x86_64-*-cygwin from notarget.
* testsuite/ld-scripts/provide-6.d: Remove x86_64-*-cygwin from xfail.
* testsuite/ld-scripts/provide-8.d: Likewise.

3 years agogas DWARF2 test XPASSes
Alan Modra [Mon, 13 Jul 2020 10:27:05 +0000 (19:57 +0930)] 
gas DWARF2 test XPASSes

git commit af2b318648 introduced a number of XPASSes.  This removes
them.  (It also introduces a FAIL on ft32-elf but the comment in the
.d file didn't adequately explain why the failure should be expected.)

* testsuite/gas/elf/dwarf2-7.d: Remove most xfails.
* testsuite/gas/elf/dwarf2-12.d: Likewise.
* testsuite/gas/elf/dwarf2-13.d: Likewise.
* testsuite/gas/elf/dwarf2-14.d: Likewise.

3 years ago[gdb/testsuite] Handle missing gold linker in gdb.base/morestack.exp
Tom de Vries [Mon, 13 Jul 2020 12:14:47 +0000 (14:14 +0200)] 
[gdb/testsuite] Handle missing gold linker in gdb.base/morestack.exp

When running test-case gdb.base/morestack.exp without the gold linker
installed, we run into:
...
Running src/gdb/testsuite/gdb.base/morestack.exp ...
gdb compile failed, collect2: fatal error: cannot find 'ld'
compilation terminated.
FAIL: gdb.base/morestack.exp: continue

                === gdb Summary ===

nr of expected passes            1
nr of unexpected failures        1
nr of untested testcases         1
...

The test-case needs the gold linker to run correctly (as explained in commit
b8d38ee425 "testsuite: Fix false FAIL for gdb.base/morestack.exp"), but
only prefers it, and doesn't require it.

Fix this by requiring the gold linker in the test-case.  Furthermore, silence
the compilation error by introducing a caching proc have_fuse_ld_gold and
using it in this and other test-cases that use -fuse-ld=gold.

Tested on x86_64-linux.

gdb/testsuite/ChangeLog:

2020-07-13  Tom de Vries  <tdevries@suse.de>

* lib/gdb.exp (have_fuse_ld_gold): New caching proc.
* gdb.base/gcore-tls-pie.exp: Use have_fuse_ld_gold.
* gdb.base/gold-gdb-index.exp: Same.
* gdb.base/morestack.exp: Same.

3 years agogdb: make type::bounds work for array and string types
Simon Marchi [Mon, 13 Jul 2020 03:05:08 +0000 (23:05 -0400)] 
gdb: make type::bounds work for array and string types

Getting the bounds of an array (or string) type is a common operation,
and is currently done through its index type:

    my_array_type->index_type ()->bounds ()

I think it would make sense to let the `type::bounds` methods work for
arrays and strings, as a shorthand for this.  It's natural that when
asking for the bounds of an array, we get the bounds of the range type
used as its index type.  In a way, it's equivalent as the now-removed
TYPE_ARRAY_{LOWER,UPPER}_BOUND_IS_UNDEFINED and
TYPE_ARRAY_{LOWER,UPPER}_BOUND_VALUE, except it returns the
`range_bounds` object.  The caller is then responsible for getting the
property it needs in it.

I updated all the spots I could find that could take advantage of this.

Note that this also makes `type::bit_stride` work on array types, since
`type::bit_stride` uses `type::bounds`.  `my_array_type->bit_stride ()`
now returns the bit stride of the array's index type.  So some spots
are also changed to take advantage of this.

gdb/ChangeLog:

* gdbtypes.h (struct type) <bounds>: Handle array and string
types.
* ada-lang.c (assign_aggregate): Use type::bounds on
array/string type.
* c-typeprint.c (c_type_print_varspec_suffix): Likewise.
* c-varobj.c (c_number_of_children): Likewise.
(c_describe_child): Likewise.
* eval.c (evaluate_subexp_for_sizeof): Likewise.
* f-typeprint.c (f_type_print_varspec_suffix): Likewise.
(f_type_print_base): Likewise.
* f-valprint.c (f77_array_offset_tbl): Likewise.
(f77_get_upperbound): Likewise.
(f77_print_array_1): Likewise.
* guile/scm-type.c (gdbscm_type_range): Likewise.
* m2-typeprint.c (m2_array): Likewise.
(m2_is_long_set_of_type): Likewise.
* m2-valprint.c (get_long_set_bounds): Likewise.
* p-typeprint.c (pascal_type_print_varspec_prefix): Likewise.
* python/py-type.c (typy_range): Likewise.
* rust-lang.c (rust_internal_print_type): Likewise.
* type-stack.c (type_stack::follow_types): Likewise.
* valarith.c (value_subscripted_rvalue): Likewise.
* valops.c (value_cast): Likewise.

Change-Id: I5c0c08930bffe42fd69cb4bfcece28944dd88d1f

3 years agogdb: remove TYPE_ARRAY_BIT_STRIDE
Simon Marchi [Mon, 13 Jul 2020 02:58:53 +0000 (22:58 -0400)] 
gdb: remove TYPE_ARRAY_BIT_STRIDE

Remove it and update all callers to use the equivalent accessor methods.
A subsequent patch will make type::bit_stride work for array types
(effectively replacing this macro), but I wanted to keep this patch a
simple mechanical change.

gdb/ChangeLog:

* gdbtypes.c (TYPE_ARRAY_BIT_STRIDE): Remove.  Update all
callers to use the equivalent accessor methods.

Change-Id: I09e14bd45075f98567adce8a0b93edea7722f812

3 years agogdb: remove TYPE_BIT_STRIDE
Simon Marchi [Mon, 13 Jul 2020 02:58:53 +0000 (22:58 -0400)] 
gdb: remove TYPE_BIT_STRIDE

Remove the macro and add a `bit_stride` method to `struct range_bounds`,
which does the byte -> bit conversion if needed.

Add a convenience `bit_stride` method to `struct type` as well.  I don't
really understand why the bit/byte stride is stored in the data
structure for bounds.  Maybe it was just put there because
`range_bounds` was already a data structure specific to TYPE_CODE_RANGE
types?  If the stride is indeed not related to the bounds, then I find
it more logical to do `my_range_type->bit_stride ()` than
`my_range_type->bounds ()->bit_stride ()`, hence the convenience
function on `struct type`.

gdb/ChangeLog:

* gdbtypes.h (struct range_bounds) <bit_stride>: New method.
(struct type) <bit_stride>: New method.
(TYPE_BIT_STRIDE): Remove.
* gdbtypes.c (update_static_array_size): Use type::bit_stride.

Change-Id: I6ecc1cfefdc20711fa8f188a94a05c1e116c9922

3 years agogdb: remove TYPE_ARRAY_{LOWER,UPPER}_BOUND_VALUE
Simon Marchi [Mon, 13 Jul 2020 02:58:53 +0000 (22:58 -0400)] 
gdb: remove TYPE_ARRAY_{LOWER,UPPER}_BOUND_VALUE

Remove the macros, use the various equivalent getters instead.

gdb/ChangeLog:

* gdbtypes.h (TYPE_ARRAY_LOWER_BOUND_VALUE,
TYPE_ARRAY_UPPER_BOUND_VALUE): Remove.  Update all
callers to use the equivalent accessor methods instead.

Change-Id: I7f96d988f872170e7a2f58095832710e62b85cfd

3 years agogdb: remove TYPE_ARRAY_{UPPER,LOWER}_BOUND_IS_UNDEFINED
Simon Marchi [Mon, 13 Jul 2020 02:58:52 +0000 (22:58 -0400)] 
gdb: remove TYPE_ARRAY_{UPPER,LOWER}_BOUND_IS_UNDEFINED

Remove the macros, use the various equivalent getters instead.

gdb/ChangeLog:

* gdbtypes.h (TYPE_ARRAY_UPPER_BOUND_IS_UNDEFINED,
TYPE_ARRAY_LOWER_BOUND_IS_UNDEFINED): Remove.  Update all
callers to use the equivalent accessor methods instead.

Change-Id: Ifb4c36f440b82533bde5d15a5cbb2fc91f467292

3 years agogdb: remove TYPE_LOW_BOUND_KIND and TYPE_HIGH_BOUND_KIND
Simon Marchi [Mon, 13 Jul 2020 02:58:52 +0000 (22:58 -0400)] 
gdb: remove TYPE_LOW_BOUND_KIND and TYPE_HIGH_BOUND_KIND

Remove the macros, use the getters of `struct dynamic_prop` instead.

gdb/ChangeLog:

* gdbtypes.h (TYPE_LOW_BOUND_KIND,
TYPE_HIGH_BOUND_KIND): Remove.  Update all callers
to use dynamic_prop::kind.

Change-Id: Icb1fc761f675bfac934209f8102392504d905c44

3 years agogdb: remove TYPE_LOW_BOUND_UNDEFINED and TYPE_HIGH_BOUND_UNDEFINED
Simon Marchi [Mon, 13 Jul 2020 02:58:52 +0000 (22:58 -0400)] 
gdb: remove TYPE_LOW_BOUND_UNDEFINED and TYPE_HIGH_BOUND_UNDEFINED

Remove the macros, use the getters of `struct dynamic_prop` instead.

gdb/ChangeLog:

* gdbtypes.h (TYPE_LOW_BOUND_UNDEFINED,
TYPE_HIGH_BOUND_UNDEFINED): Remove.  Update all callers
to get the bound property's kind and check against
PROP_UNDEFINED.

Change-Id: I6a7641ac1aa3fa7fca0c21f00556f185f2e2d68c

3 years agogdb: remove TYPE_HIGH_BOUND and TYPE_LOW_BOUND
Simon Marchi [Mon, 13 Jul 2020 02:58:51 +0000 (22:58 -0400)] 
gdb: remove TYPE_HIGH_BOUND and TYPE_LOW_BOUND

Remove the macros, use the getters of `struct dynamic_prop` instead.

gdb/ChangeLog:

* gdbtypes.h (TYPE_LOW_BOUND, TYPE_HIGH_BOUND): Remove.  Update
all callers to use type::range_bounds followed by
dynamic_prop::{low,high}.

Change-Id: I31beeed65d94d81ac4f999244a8b859e2ee961d1

3 years agogdb: add accessors to struct dynamic_prop
Simon Marchi [Mon, 13 Jul 2020 02:58:51 +0000 (22:58 -0400)] 
gdb: add accessors to struct dynamic_prop

Add setters, to ensure that the kind and value of the property are
always kept in sync (a caller can't forget one or the other).  Add
getters, such that we can assert that when a caller accesses a data bit
of the property, the property is indeed of the corresponding kind.

Note that because of the way `struct dynamic_prop` is allocated
currently, we can't make the `m_kind` and `m_data` fields private.  That
would make the type non-default-constructible, and we would have to call
the constructor when allocating them.  However, I still prefixed them
with `m_` to indicate that they should not be accessed from outside the
class (and also to be able to use the name `kind` for the method).

gdb/ChangeLog:

* gdbtypes.h (struct dynamic_prop) <kind, set_undefined,
const_val, set_const_val, baton, set_locexpr, set_loclist,
set_addr_offset, variant_parts, set_variant_parts,
original_type, set_original_type>: New methods.
<kind>: Rename to...
<m_kind>: ... this.  Update all users to use the new methods
instead.
<data>: Rename to...
<m_data>: ... this.  Update all users to use the new methods
instead.

Change-Id: Ib72a8eb440dfeb1a5421d0933334230d7f2478f9

3 years agogdb: make get_discrete_bounds check for non-constant range bounds
Simon Marchi [Mon, 13 Jul 2020 02:58:51 +0000 (22:58 -0400)] 
gdb: make get_discrete_bounds check for non-constant range bounds

The next patch adds getters to the `dynamic_prop` structure.  These
getters validate that the accessed data matches the property kind (for
example, to access the `const_val` field, the property must be of kind
`PROP_CONST`).  It found one instance where we are accessing the
`const_val` data of a property that has the undefined kind.

This happens in function `get_discrete_bounds`, and is exposed by test
gdb.base/ptype.exp, amongst others.  Without this patch, we would get:

    $ ./gdb -q -nx --data-directory=data-directory testsuite/outputs/gdb.base/ptype/ptype -ex "ptype t_char_array"
    Reading symbols from testsuite/outputs/gdb.base/ptype/ptype...
    type = char [
    /home/smarchi/src/binutils-gdb/gdb/gdbtypes.h:526: internal-error: LONGEST dynamic_prop::const_val() const: Assertion `m_kind == PROP_CONST' failed.
    A problem internal to GDB has been detected,
    further debugging may prove unreliable.
    Quit this debugging session? (y or n)

The `get_discrete_bounds` function returns the bounds of a type (not
only range types).  For range types, it naturally uses the bound
properties that are intrinsic to the range type.  It accesses these
properties using TYPE_LOW_BOUND and TYPE_HIGH_BOUND, which assume the
properties are defined and have constant values.  This is sometimes not
the case, and the passed range type (as in the example above) has an
undefined high/upper bound.

Given its current interface (returning two LONGEST values for low and
high), `get_discrete_bounds` can't really work if the range type's
bounds are not both defined and both constant values.

This patch changes the function to return -1 (failure to get the bounds)
if any of the range type's bounds is not a constant value.  It is
sufficient to fix the issue and it seems to keep the callers happy, at
least according to the testsuite.

A bit in `get_array_bounds` could be removed, since
`get_discrete_bounds` no longer returns 1 if a bound is undefined.

gdb/ChangeLog:

* gdbtypes.c (get_discrete_bounds): Return failure if
the range type's bounds are not both defined and constant
values.
(get_array_bounds): Update comment.  Remove undefined bound check.

Change-Id: I047a3beee2c1e275f888cfc4778228339922bde9

3 years agogdb: remove TYPE_RANGE_DATA macro
Simon Marchi [Mon, 13 Jul 2020 02:58:51 +0000 (22:58 -0400)] 
gdb: remove TYPE_RANGE_DATA macro

Remove it in favor of using type::bounds directly.

gdb/ChangeLog:

* gdbtypes.h (TYPE_RANGE_DATA): Remove.  Update callers to use
the type::bounds method directly.

Change-Id: Id4fab22af0a94cbf505f78b01b3ee5b3d682fba2

3 years agogdb: add type::bounds / type::set_bounds
Simon Marchi [Mon, 13 Jul 2020 02:58:50 +0000 (22:58 -0400)] 
gdb: add type::bounds / type::set_bounds

Add the `bounds` and `set_bounds` methods on `struct type`, in order to
remove the `TYPE_RANGE_DATA` macro.  In this patch, the
`TYPE_RANGE_DATA` macro is changed to use `type::bounds`, so all the
call sites that are used to set a range type's bounds are changed to use
`type::set_bounds`.  The next patch will remove `TYPE_RANGE_DATA`
completely.

gdb/ChangeLog:

* gdbtypes.h (struct type) <bounds, set_bounds>: New methods.
(TYPE_RANGE_DATA): Use type::bounds.  Change all uses that
are used to set the range type's bounds to use set_bounds.

Change-Id: I62e15506239b98404e62bbea8120db184ed87847

3 years agoAutomatic date update in version.in
GDB Administrator [Mon, 13 Jul 2020 00:00:11 +0000 (00:00 +0000)] 
Automatic date update in version.in

3 years agoAutomatic date update in version.in
GDB Administrator [Sun, 12 Jul 2020 00:00:12 +0000 (00:00 +0000)] 
Automatic date update in version.in

3 years agorust: Fix rust modules test
Daniel Xu [Sat, 4 Jul 2020 03:59:53 +0000 (20:59 -0700)] 
rust: Fix rust modules test

I noticed that the modules test was failing. Some choice use of `nm`
revealed `TWENTY_THREE` was not in the final binary. Fix by taking a
pointer to the global, forcing the linker to keep the symbol in.

gdb/testsuite/ChangeLog
2020-07-11  Daniel Xu  <dxu@dxuuu.xyz>

PR rust/26121
* gdb.rust/modules.rs: Prevent linker from discarding test
symbol.

Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>
3 years agoFine tune exec-file-mismatch help and documentation.
Philippe Waroquiers [Sat, 27 Jun 2020 20:08:50 +0000 (22:08 +0200)] 
Fine tune exec-file-mismatch help and documentation.

It was deemed better to explicitly mention in help and doc that build IDs
are used for comparison, and that symbols are loaded when asking to
load the exec-file.

This is V2, fixing 2 typos and replacing 'If the user asks to load'
by 'If the user confirms loading', as suggested by Pedro.

gdb/ChangeLog
2020-07-11  Philippe Waroquiers  <philippe.waroquiers@skynet.be>

* exec.c (_initialize_exec): Update exec-file-mismatch help.

gdb/doc/ChangeLog
2020-07-11  Philippe Waroquiers  <philippe.waroquiers@skynet.be>

* gdb.texinfo (Attach): Update exec-file-mismatch doc.

3 years agox86: Support GNU_PROPERTY_X86_FEATURE_2_TMM
H.J. Lu [Sat, 11 Jul 2020 11:04:08 +0000 (04:04 -0700)] 
x86: Support GNU_PROPERTY_X86_FEATURE_2_TMM

Support GNU_PROPERTY_X86_FEATURE_2_TMM in

https://gitlab.com/x86-psABIs/x86-64-ABI/-/merge_requests/1

 #define GNU_PROPERTY_X86_FEATURE_2_TMM      (1U << 10)

binutils/

* readelf.c (decode_x86_feature_2): Handle
GNU_PROPERTY_X86_FEATURE_2_TMM.

gas/

* config/tc-i386.c (output_insn): Check i.xstate to set
GNU_PROPERTY_X86_FEATURE_2_TMM.
* testsuite/gas/i386/i386.exp: Run x86-64-property-7,
x86-64-property-8 and x86-64-property-9.
* testsuite/gas/i386/x86-64-property-7.d: New file.
* testsuite/gas/i386/x86-64-property-7.s: Likewise.
* testsuite/gas/i386/x86-64-property-8.d: Likewise.
* testsuite/gas/i386/x86-64-property-8.s: Likewise.
* testsuite/gas/i386/x86-64-property-9.d: Likewise.
* testsuite/gas/i386/x86-64-property-9.s: Likewise.

include/

* elf/common.h (GNU_PROPERTY_X86_FEATURE_2_TMM): New.

3 years agoAutomatic date update in version.in
GDB Administrator [Sat, 11 Jul 2020 00:00:13 +0000 (00:00 +0000)] 
Automatic date update in version.in

3 years agoFix crash if connection drops in scoped_restore_current_thread's ctor, part 2
Pedro Alves [Thu, 9 Jul 2020 17:14:09 +0000 (18:14 +0100)] 
Fix crash if connection drops in scoped_restore_current_thread's ctor, part 2

Running the testsuite against an Asan-enabled build of GDB makes
gdb.base/multi-target.exp expose this bug.

scoped_restore_current_thread's ctor calls get_frame_id to record the
selected frame's ID to restore later.  If the frame ID hasn't been
computed yet, it will be computed on the spot, and that will usually
require accessing the target's memory and registers.  If the remote
connection closes, while we're computing the frame ID, the remote
target exits its inferiors, unpushes itself, and throws a
TARGET_CLOSE_ERROR error.  Exiting the inferiors deletes the
inferior's threads.

scoped_restore_current_thread increments the current thread's refcount
to prevent the thread from being deleted from under its feet.
However, the code that does that isn't considering the case of the
thread being deleted from within get_frame_id.  It only increments the
refcount _after_ get_frame_id returns.  So if the current thread is
indeed deleted, the

     tp->incref ();

statement references a stale TP pointer.

Incrementing the refcounts earlier fixes it.

We should probably also let the TARGET_CLOSE_ERROR error propagate in
this case.  That alone would fix it, though it seems better to tweak
the refcount handling too.  And to avoid having to manually decref
before throwing, convert to use gdb::ref_ptr.

Unfortunately, we can't define inferior_ref in inferior.h and then use
it in scoped_restore_current_thread, because
scoped_restore_current_thread is defined before inferior is
(inferior.h includes gdbthread.h).  To break that dependency, we would
have to move scoped_restore_current_thread to its own header.  I'm not
doing that here.

gdb/ChangeLog:

* gdbthread.h (inferior_ref): Define.
(scoped_restore_current_thread) <m_thread>: Now a thread_info_ref.
(scoped_restore_current_thread) <m_inf>: Now an inferior_ref.
* thread.c
(scoped_restore_current_thread::restore):
Adjust to gdb::ref_ptr.
(scoped_restore_current_thread::~scoped_restore_current_thread):
Remove manual decref handling.
(scoped_restore_current_thread::scoped_restore_current_thread):
Adjust to use
inferior_ref::new_reference/thread_info_ref::new_reference.
Incref the thread before calling get_frame_id instead of after.
Let TARGET_CLOSE_ERROR propagate.

3 years agoFix crash if connection drops in scoped_restore_current_thread's ctor, part 1
Pedro Alves [Wed, 8 Jul 2020 14:43:02 +0000 (15:43 +0100)] 
Fix crash if connection drops in scoped_restore_current_thread's ctor, part 1

Running the testsuite against an Asan-enabled build of GDB makes
gdb.base/multi-target.exp expose this bug.

scoped_restore_current_thread's ctor calls get_frame_id to record the
selected frame's ID to restore later.  If the frame ID hasn't been
computed yet, it will be computed on the spot, and that will usually
require accessing the target's memory and registers, which requires
remote accesses.  If the remote connection closes while we're
computing the frame ID, the remote target exits its inferiors,
unpushes itself, and throws a TARGET_CLOSE_ERROR error.

If that happens, GDB can currently crash, here:

> ==18555==ERROR: AddressSanitizer: heap-use-after-free on address 0x621004670aa8 at pc 0x0000007ab125 bp 0x7ffdecaecd20 sp 0x7ffdecaecd10
> READ of size 4 at 0x621004670aa8 thread T0
>     #0 0x7ab124 in dwarf2_frame_this_id src/binutils-gdb/gdb/dwarf2/frame.c:1228
>     #1 0x983ec5 in compute_frame_id src/binutils-gdb/gdb/frame.c:550
>     #2 0x9841ee in get_frame_id(frame_info*) src/binutils-gdb/gdb/frame.c:582
>     #3 0x1093faa in scoped_restore_current_thread::scoped_restore_current_thread() src/binutils-gdb/gdb/thread.c:1462
>     #4 0xaee5ba in fetch_inferior_event(void*) src/binutils-gdb/gdb/infrun.c:3968
>     #5 0xaa990b in inferior_event_handler(inferior_event_type, void*) src/binutils-gdb/gdb/inf-loop.c:43
>     #6 0xea61b6 in remote_async_serial_handler src/binutils-gdb/gdb/remote.c:14161
>     #7 0xefca8a in run_async_handler_and_reschedule src/binutils-gdb/gdb/ser-base.c:137
>     #8 0xefcd23 in fd_event src/binutils-gdb/gdb/ser-base.c:188
>     #9 0x15a7416 in handle_file_event src/binutils-gdb/gdbsupport/event-loop.cc:548
>     #10 0x15a7c36 in gdb_wait_for_event src/binutils-gdb/gdbsupport/event-loop.cc:673
>     #11 0x15a5dbb in gdb_do_one_event() src/binutils-gdb/gdbsupport/event-loop.cc:215
>     #12 0xbfe62d in start_event_loop src/binutils-gdb/gdb/main.c:356
>     #13 0xbfe935 in captured_command_loop src/binutils-gdb/gdb/main.c:416
>     #14 0xc01d39 in captured_main src/binutils-gdb/gdb/main.c:1253
>     #15 0xc01dc9 in gdb_main(captured_main_args*) src/binutils-gdb/gdb/main.c:1268
>     #16 0x414ddd in main src/binutils-gdb/gdb/gdb.c:32
>     #17 0x7f590110b82f in __libc_start_main ../csu/libc-start.c:291
>     #18 0x414bd8 in _start (build/binutils-gdb/gdb/gdb+0x414bd8)

What happens is that above, we're in dwarf2_frame_this_id, just after
the dwarf2_frame_cache call.  The "cache" variable that the
dwarf2_frame_cache function returned is already stale.  It's been
released here, from within the dwarf2_frame_cache:

(top-gdb) bt
#0  reinit_frame_cache () at src/gdb/frame.c:1855
#1  0x00000000014ff7b0 in switch_to_no_thread () at src/gdb/thread.c:1301
#2  0x0000000000f66d3e in switch_to_inferior_no_thread (inf=0x615000338180) at src/gdb/inferior.c:626
#3  0x00000000012f3826 in remote_unpush_target (target=0x6170000c5900) at src/gdb/remote.c:5521
#4  0x00000000013097e0 in remote_target::readchar (this=0x6170000c5900, timeout=2) at src/gdb/remote.c:9137
#5  0x000000000130be4d in remote_target::getpkt_or_notif_sane_1 (this=0x6170000c5900, buf=0x6170000c5918, forever=0, expecting_notif=0, is_notif=0x0) at src/gdb/remote.c:9683
#6  0x000000000130c8ab in remote_target::getpkt_sane (this=0x6170000c5900, buf=0x6170000c5918, forever=0) at src/gdb/remote.c:9790
#7  0x000000000130bc0d in remote_target::getpkt (this=0x6170000c5900, buf=0x6170000c5918, forever=0) at src/gdb/remote.c:9623
#8  0x000000000130838e in remote_target::remote_read_bytes_1 (this=0x6170000c5900, memaddr=0x7fffffffcdc0, myaddr=0x6080000ad3bc "", len_units=64, unit_size=1, xfered_len_units=0x7fff6a29b9a0) at src/gdb/remote.c:8860
#9  0x0000000001308bd2 in remote_target::remote_read_bytes (this=0x6170000c5900, memaddr=0x7fffffffcdc0, myaddr=0x6080000ad3bc "", len=64, unit_size=1, xfered_len=0x7fff6a29b9a0) at src/gdb/remote.c:8987
#10 0x0000000001311ed1 in remote_target::xfer_partial (this=0x6170000c5900, object=TARGET_OBJECT_MEMORY, annex=0x0, readbuf=0x6080000ad3bc "", writebuf=0x0, offset=140737488342464, len=64, xfered_len=0x7fff6a29b9a0) at src/gdb/remote.c:10988
#11 0x00000000014ba969 in raw_memory_xfer_partial (ops=0x6170000c5900, readbuf=0x6080000ad3bc "", writebuf=0x0, memaddr=140737488342464, len=64, xfered_len=0x7fff6a29b9a0) at src/gdb/target.c:918
#12 0x00000000014bb720 in target_xfer_partial (ops=0x6170000c5900, object=TARGET_OBJECT_RAW_MEMORY, annex=0x0, readbuf=0x6080000ad3bc "", writebuf=0x0, offset=140737488342464, len=64, xfered_len=0x7fff6a29b9a0) at src/gdb/target.c:1148
#13 0x00000000014bc3b5 in target_read_partial (ops=0x6170000c5900, object=TARGET_OBJECT_RAW_MEMORY, annex=0x0, buf=0x6080000ad3bc "", offset=140737488342464, len=64, xfered_len=0x7fff6a29b9a0) at src/gdb/target.c:1380
#14 0x00000000014bc593 in target_read (ops=0x6170000c5900, object=TARGET_OBJECT_RAW_MEMORY, annex=0x0, buf=0x6080000ad3bc "", offset=140737488342464, len=64) at src/gdb/target.c:1419
#15 0x00000000014bbd4d in target_read_raw_memory (memaddr=0x7fffffffcdc0, myaddr=0x6080000ad3bc "", len=64) at src/gdb/target.c:1252
#16 0x0000000000bf27df in dcache_read_line (dcache=0x6060001eddc0, db=0x6080000ad3a0) at src/gdb/dcache.c:336
#17 0x0000000000bf2b72 in dcache_peek_byte (dcache=0x6060001eddc0, addr=0x7fffffffcdd8, ptr=0x6020001231b0 "") at src/gdb/dcache.c:403
#18 0x0000000000bf3103 in dcache_read_memory_partial (ops=0x6170000c5900, dcache=0x6060001eddc0, memaddr=0x7fffffffcdd8, myaddr=0x6020001231b0 "", len=8, xfered_len=0x7fff6a29bf20) at src/gdb/dcache.c:484
#19 0x00000000014bafe9 in memory_xfer_partial_1 (ops=0x6170000c5900, object=TARGET_OBJECT_STACK_MEMORY, readbuf=0x6020001231b0 "", writebuf=0x0, memaddr=140737488342488, len=8, xfered_len=0x7fff6a29bf20) at src/gdb/target.c:1034
#20 0x00000000014bb212 in memory_xfer_partial (ops=0x6170000c5900, object=TARGET_OBJECT_STACK_MEMORY, readbuf=0x6020001231b0 "", writebuf=0x0, memaddr=140737488342488, len=8, xfered_len=0x7fff6a29bf20) at src/gdb/target.c:1076
#21 0x00000000014bb6b3 in target_xfer_partial (ops=0x6170000c5900, object=TARGET_OBJECT_STACK_MEMORY, annex=0x0, readbuf=0x6020001231b0 "", writebuf=0x0, offset=140737488342488, len=8, xfered_len=0x7fff6a29bf20) at src/gdb/target.c:1133
#22 0x000000000164564d in read_value_memory (val=0x60f000029440, bit_offset=0, stack=1, memaddr=0x7fffffffcdd8, buffer=0x6020001231b0 "", length=8) at src/gdb/valops.c:956
#23 0x0000000001680fff in value_fetch_lazy_memory (val=0x60f000029440) at src/gdb/value.c:3764
#24 0x0000000001681efd in value_fetch_lazy (val=0x60f000029440) at src/gdb/value.c:3910
#25 0x0000000001676143 in value_optimized_out (value=0x60f000029440) at src/gdb/value.c:1411
#26 0x0000000000e0fcb8 in frame_register_unwind (next_frame=0x6210066bfde0, regnum=16, optimizedp=0x7fff6a29c200, unavailablep=0x7fff6a29c240, lvalp=0x7fff6a29c2c0, addrp=0x7fff6a29c300, realnump=0x7fff6a29c280, bufferp=0x7fff6a29c3a0 "@\304)j\377\177") at src/gdb/frame.c:1144
#27 0x0000000000e10418 in frame_unwind_register (next_frame=0x6210066bfde0, regnum=16, buf=0x7fff6a29c3a0 "@\304)j\377\177") at src/gdb/frame.c:1196
#28 0x0000000000f00431 in i386_unwind_pc (gdbarch=0x6210043d0110, next_frame=0x6210066bfde0) at src/gdb/i386-tdep.c:1969
#29 0x0000000000e39724 in gdbarch_unwind_pc (gdbarch=0x6210043d0110, next_frame=0x6210066bfde0) at src/gdb/gdbarch.c:3056
#30 0x0000000000c2ea90 in dwarf2_tailcall_sniffer_first (this_frame=0x6210066bfde0, tailcall_cachep=0x6210066bfee0, entry_cfa_sp_offsetp=0x0) at src/gdb/dwarf2/frame-tailcall.c:423
#31 0x0000000000c36bdb in dwarf2_frame_cache (this_frame=0x6210066bfde0, this_cache=0x6210066bfdf8) at src/gdb/dwarf2/frame.c:1198
#32 0x0000000000c36eb3 in dwarf2_frame_this_id (this_frame=0x6210066bfde0, this_cache=0x6210066bfdf8, this_id=0x6210066bfe40) at src/gdb/dwarf2/frame.c:1226

Note that remote_target::readchar in frame #4 throws
TARGET_CLOSE_ERROR after the remote_unpush_target in frame #3 returns.

The problem is that the TARGET_CLOSE_ERROR is swallowed by
value_optimized_out in frame #25.

If we fix that one, then we run into dwarf2_tailcall_sniffer_first
swallowing the exception in frame #30 too.

The attached patch fixes it by making those spots swallow fewer kinds
of errors.

gdb/ChangeLog:

* frame-tailcall.c (dwarf2_tailcall_sniffer_first): Only swallow
NO_ENTRY_VALUE_ERROR / MEMORY_ERROR / OPTIMIZED_OUT_ERROR /
NOT_AVAILABLE_ERROR.
* value.c (value_optimized_out): Only swallow MEMORY_ERROR /
OPTIMIZED_OUT_ERROR / NOT_AVAILABLE_ERROR.

3 years agoFix GDB busy loop when interrupting non-stop program (PR 26199)
Simon Marchi [Sat, 4 Jul 2020 12:33:19 +0000 (13:33 +0100)] 
Fix GDB busy loop when interrupting non-stop program (PR 26199)

When interrupting a program in non-stop, the program gets interrupted
correctly, but GDB busy loops (the event loop is always woken up).

Here is how to reproduce it:

 1. Start GDB: ./gdb -nx --data-directory=data-directory -ex "set non-stop 1" --args  /bin/sleep 60
 2. Run the program with "run"
 3. Interrupt with ^C.
 4. Look into htop, see GDB taking 100% CPU

Debugging `handle_file_event`, we see that the event source that wakes
up the event loop is the linux-nat one:

 (top-gdb) p file_ptr.proc
 $5 = (handler_func *) 0xb9cccd <handle_target_event(int, gdb_client_data)>
 ^^^^^^^^^^^^^^^^^^^
 |
 \-- the linux-nat callback

Debugging fetch_inferior_event and do_target_wait, we see that we
don't actually call `wait` on the linux-nat target, because
inferior_matches returns false:

 auto inferior_matches = [&wait_ptid] (inferior *inf)
   {
     return (inf->process_target () != NULL
     && (threads_are_executing (inf->process_target ())
 || threads_are_resumed_pending_p (inf))
     && ptid_t (inf->pid).matches (wait_ptid));
   };

because `threads_are_executing` is false.

What happens is:

 1. User types ctrl-c, that writes in the linux-nat pipe, waking up
    the event source.

 2. linux-nat's wait gets called, the SIGINT event is returned, but
    before returning, it marks the pipe again, in order for wait to
    get called again:

    /* If we requested any event, and something came out, assume there
       may be more.  If we requested a specific lwp or process, also
       assume there may be more.  */
    if (target_is_async_p ()
&& ((ourstatus->kind != TARGET_WAITKIND_IGNORE
     && ourstatus->kind != TARGET_WAITKIND_NO_RESUMED)
    || ptid != minus_one_ptid))
      async_file_mark ();

 3. The SIGINT event is handled, the program is stopped, the stop
    notification is printed.

 4. The event loop is woken up again because of the `async_file_mark`
    of step 2.

 5. Because `inferior_matches` returns false, we never call
    linux-nat's wait, so the pipe stays readable.

 6. Goto 4.

Pedro says:

This commit fixes it by letting do_target_wait call target_wait even
if threads_are_executing is false.  This will normally result in the
target returning TARGET_WAITKIND_NO_RESUMED, and _not_ marking its
event source again.  This results in infrun only calling into the
target only once (i.e., breaking the busy loop).

Note that the busy loop bug didn't trigger in all-stop mode because
all-stop handles this by unregistering the target from the event loop
as soon as it was all stopped -- see
inf-loop.c:inferior_event_handler's INF_EXEC_COMPLETE handling.  If we
remove that non-stop check from inferior_event_handler, and replace
the target_has_execution check for threads_are_executing instead, it
also fixes the issue for non-stop.  I considered that as the final
solution, but decided that the solution proposed here instead is just
simpler and more future-proof design.  With the
TARGET_WAITKIND_NO_RESUMED handling fixes done in the previous
patches, I think it should be possible to always keep the target
registered in the event loop, meaning we could eliminate the
target_async(0) call from inferior_event_handler as well as most of
the target_async(1) calls in the target backends.  That would allow in
the future e.g., the remote target reporting asynchronous
notifications even if all threads are stopped.  I haven't attempted
that, though.

gdb/ChangeLog:
yyyy-mm-dd  Simon Marchi  <simon.marchi@polymtl.ca>
    Pedro Alves  <pedro@palves.net>

PR gdb/26199
* infrun.c (threads_are_resumed_pending_p): Delete.
(do_target_wait): Remove threads_are_executing and
threads_are_resumed_pending_p checks from the inferior_matches
lambda.  Update comments.

3 years agoTestcase for previous handle_no_resumed fixes
Pedro Alves [Sat, 4 Jul 2020 18:26:59 +0000 (19:26 +0100)] 
Testcase for previous handle_no_resumed fixes

This adds a testcase that covers the scenarios described in the
previous two commits.

gdb/testsuite/ChangeLog:

PR gdb/26199
* gdb.multi/multi-target.c (exit_thread): New.
(thread_start): Break loop if EXIT_THREAD.
* gdb.multi/multi-target.exp (test_no_unwaited_for): New proc.
(top level) Call test_no_resumed.