Commit Graph

29894 Commits

Author SHA1 Message Date
Tom Tromey de54374205 Use macros for TUI window names
Christian pointed out that tui-layout.c hard-codes various window
names.  This patch changes the code to use the macros from tui-data.h
instead.  For each window, I searched for uses of the name; but I only
found any in tui-layout.c.  This also adds a new macro to account for
the "status" window.

gdb/ChangeLog
2020-06-16  Tom Tromey  <tom@tromey.com>

	* tui/tui-data.h (STATUS_NAME): New macro.
	* tui/tui-layout.c (tui_remove_some_windows)
	(initialize_known_windows, tui_register_window)
	(tui_layout_split::remove_windows, initialize_layouts)
	(tui_new_layout_command): Don't use hard-coded window names.
2020-06-16 18:02:20 -06:00
Tom Tromey a350efd4fb Fix crash when exiting TUI with gdb -tui
PR tui/25348 points out that, when "gdb -tui" is used, then exiting
the TUI will cause a crash.

This happens because tui_setup_io stashes some readline variables --
but because this happens before readline is initialized, some of these
are NULL.  Then, when exiting the TUI, the NULL values are "restored",
causing a crash in readline.

This patch fixes the problem by ensuring that readline is initialized
first.  Back in commit 11061048d ("Give a name to the TUI SingleKey
keymap"), a call to rl_initialize was removed from
tui_initialize_readline; this patch resurrects the call, but moves it
to the end of the function, so as not to remove the ability to modify
the SingleKey map from .inputrc.

gdb/ChangeLog
2020-06-16  Tom Tromey  <tom@tromey.com>

	PR tui/25348:
	* tui/tui.c (tui_ensure_readline_initialized): Rename from
	tui_initialize_readline.  Only run once.  Call rl_initialize.
	* tui/tui.h (tui_ensure_readline_initialized): Rename from
	tui_initialize_readline.
	* tui/tui-io.c (tui_setup_io): Call
	tui_ensure_readline_initialized.
	* tui/tui-interp.c (tui_interp::init): Update.
2020-06-16 18:02:20 -06:00
Tom Tromey 39ec04904f Fix C-x 1 from gdb prompt
Pedro pointed out on irc that C-x 1 from the gdb prompt will cause a
crash.  This happened because of a bug in remove_windows -- it would
always remove all the windows from the layout.  This patch fixes this
bug, and also arranges to have C-x 1 preserve the status window.

gdb/ChangeLog
2020-06-16  Tom Tromey  <tom@tromey.com>

	* tui/tui-layout.c (tui_layout_split::remove_windows): Fix logic.
	Also preserve the status window.
2020-06-16 18:02:20 -06:00
Tom Tromey d2d1ea20ae Fix crash when TUI window creation fails
If a TUI window is written in Python, and if the window construction
function fails, then gdb will crash.  This patch fixes the crash.

gdb/ChangeLog
2020-06-16  Tom Tromey  <tom@tromey.com>

	* python/py-tui.c (tui_py_window::~tui_py_window): Handle case
	where m_window==nullptr.

gdb/testsuite/ChangeLog
2020-06-16  Tom Tromey  <tom@tromey.com>

	* gdb.python/tui-window.py (failwin): New function.  Register it
	as a TUI window type.
	* gdb.python/tui-window.exp: Create new "fail" layout.  Test it.
2020-06-16 17:48:38 -06:00
Tom Tromey 6692031743 Change target_read_string API
This simplifies the target_read_string API a bit.

Note that some code was using safe_strerror on the error codes
returned by target_read_string.  It seems to me that this is incorrect
(if it was ever correct, it must have been quite a long time ago).

gdb/ChangeLog
2020-06-15  Tom Tromey  <tromey@adacore.com>

	* windows-nat.c (windows_nat::handle_output_debug_string):
	Update.
	(windows_nat::handle_ms_vc_exception): Update.
	* target.h (target_read_string): Change API.
	* target.c (target_read_string): Change API.
	* solib-svr4.c (open_symbol_file_object, svr4_read_so_list):
	Update.
	* solib-frv.c (frv_current_sos): Update.
	* solib-dsbt.c (dsbt_current_sos): Update.
	* solib-darwin.c (darwin_current_sos): Update.
	* linux-thread-db.c (inferior_has_bug): Update.
	* expprint.c (print_subexp_standard): Update.
	* ada-lang.c (ada_main_name, ada_tag_name_from_tsd)
	(ada_exception_message_1): Update.
2020-06-15 06:28:10 -06:00
Tom Tromey a5d871ddaf Remove a use of target_read_string
linux-tdep.c:dump_mapping_p uses target_read_string, but in a way that
does not really make sense.  It's better to use target_read_memory
here.

gdb/ChangeLog
2020-06-15  Tom Tromey  <tromey@adacore.com>

	* linux-tdep.c (dump_mapping_p): Use target_read_memory.
2020-06-15 06:28:10 -06:00
Tom Tromey 670e35fad9 Rewrite target_read_string
This rewrites target_read_string in terms of read_string.

gdb/ChangeLog
2020-06-15  Tom Tromey  <tromey@adacore.com>

	* valprint.c (read_string): Update comment.
	* target.c (MIN): Remove.
	(target_read_string): Rewrite.
2020-06-15 06:28:09 -06:00
Tom Tromey f5272a3bb3 Remove read_memory_string
read_memory_string is redundant and only called in a couple of spots.
This patch removes it in favor of target_read_string.

gdb/ChangeLog
2020-06-15  Tom Tromey  <tromey@adacore.com>

	* corefile.c (read_memory_string): Remove.
	* ada-valprint.c (ada_value_print_ptr): Update.
	* ada-lang.h (ada_tag_name): Change return type.
	* ada-lang.c (type_from_tag): Update.
	(ada_tag_name_from_tsd): Change return type.  Use
	target_read_string.
	(ada_tag_name): Likewise.
	* gdbcore.h (read_memory_string): Don't declare.
2020-06-15 06:28:09 -06:00
Hannes Domani 2c074f4902 Handle Windows drives in rbreak paths
Fixes this testsuite fail on Windows:
FAIL: gdb.base/fullpath-expand.exp: rbreak XXX/fullpath-expand-func.c:func

If the found colon is actually part of a Windows drive, look for another.

gdb/ChangeLog:

2020-06-14  Hannes Domani  <ssbssa@yahoo.de>

	* symtab.c (rbreak_command): Ignore Windows drive colon.
2020-06-14 17:38:23 +02:00
Simon Marchi 6a17d503c4 gdb: mention removed GDBserver host support in NEWS
gdb/ChangeLog:

	* NEWS: Mention removed GDBserver host support.

Change-Id: Ib9e212e525d12ac7f3f9b5c056adc5bf9c4d52cd
2020-06-12 16:06:46 -04:00
Nelson Chu 453c733fcf RISC-V: Update the rebuild-csr-xml.sh.
We add new arguments defined and aborted verisons for DECLARE_CSR to
support privileged versions controling in binutils.  Therefore, the
rebuild-csr-xml.sh should be updated, too.

	gdb/
	* features/riscv/rebuild-csr-xml.sh: Updated.
2020-06-12 09:44:02 +08:00
Tom Tromey 2b4e6a3f4b Fix hex floating point lexing
PR gdb/18318 notes that gdb will sometimes incorrectly handle hex
floating point input.  This turns out to be a bug in the C lexer; the
'p' was not being correctly recognized, and so the exponent was not
always passed to the floating point "from_string" method.

Tested by the buildbot "Fedora-x86_64-m64" builder.

gdb/ChangeLog
2020-06-11  Tom Tromey  <tom@tromey.com>

	PR gdb/18318:
	* c-exp.y (lex_one_token): Handle 'p' like 'e'.

gdb/testsuite/ChangeLog
2020-06-11  Tom Tromey  <tom@tromey.com>

	PR gdb/18318:
	* gdb.base/printcmds.exp (test_float_accepted): Add more hex
	floating point tests.
2020-06-11 10:34:31 -06:00
Jonny Grant 4412332f4b gdb: add mailing list and IRC information to --help
A few user-vibisble changes to the --help output:

 * Remove unnecessary quotes around bug url.
 * Mention the mailing list and IRC channel as places where users can
   ask GDB-related questions.
 * Add empty lines between items in the footer, to improve readability.
 * Remove unnecessary new line at the end of output.

2020-06-09  Jonny Grant  <jg@jguk.org>
2020-06-09  Simon Marchi  <simon.marchi@polymtl.ca>

	* main.c (captured_main_1): Don't print new line after help.
	(print_gdb_help): add mailing list and IRC channel information
	to --help.  Add new lines between items in the footer.  Remove
	quotes around bug url.

Signed-off-by: Jonathan Grant <jg@jguk.org>
Change-Id: Ibd0746a348d558fb35b5cd7e366f107742806565
2020-06-11 10:30:36 -04:00
Keith Seitz 2f33032a93 Compute proper length for dynamic types of TYPE_CODE_TYPEDEF
This patch fixes gdb/21356 in which we hit an assertion in
value_contents_bits_eq:

(gdb) p container_object2
(gdb) p container_object2
$1 = {_container_member2 = 15, _vla_struct_object2 = {_some_member = 0,
    _vla_field = {
../../src/gdb/value.c:829: internal-error: \
  int value_contents_bits_eq(const value*, int, const value*, int, int): \
  Assertion `offset1 + length \
             <= TYPE_LENGTH (val1->enclosing_type) * TARGET_CHAR_BIT' failed.

This is happening because TYPE_LENGTH (val1->enclosing_type) is erroneously
based on enclosing_type, which is a typedef, instead of the actual underlying
type.

This can be traced back to resolve_dynamic_struct, where the size of the
type is computed:
...
        TYPE_FIELD_TYPE (resolved_type, i)
          = resolve_dynamic_type_internal (TYPE_FIELD_TYPE (resolved_type, i),
                                           &pinfo, 0);
        gdb_assert (TYPE_FIELD_LOC_KIND (resolved_type, i)
                    == FIELD_LOC_KIND_BITPOS);

        new_bit_length = TYPE_FIELD_BITPOS (resolved_type, i);
        if (TYPE_FIELD_BITSIZE (resolved_type, i) != 0)
          new_bit_length += TYPE_FIELD_BITSIZE (resolved_type, i);
        else
          new_bit_length += (TYPE_LENGTH (TYPE_FIELD_TYPE (resolved_type, i))
                             * TARGET_CHAR_BIT);
...

In this function, resolved_type is TYPE_CODE_TYPEDEF which is not what we
want to use to calculate the size of the actual field.

This patch fixes this and the similar problem in resolve_dynamic_union.

gdb/ChangeLog:
2020-06-11  Keith Seitz  <keiths@redhat.com>

	PR gdb/21356
	* gdbtypes.c (resolve_dynamic_union, resolve_dynamic_struct):
	Resolve typedefs for type length calculations.

gdb/testsuite/ChangeLog:
2020-06-11  Keith Seitz  <keiths@redhat.com>

	PR gdb/21356
	* gdb.base/vla-datatypes.c (vla_factory): Add typedef for struct
	vla_struct.
	Add new struct vla_typedef and union vla_typedef_union and
	corresponding instantiation objects.
	Initialize new objects.
	* gdb.base/vla-datatypes.exp: Add tests for vla_typedef_struct_object
	and vla_typedef_union_object.
	Fixup type for vla_struct_object.
2020-06-11 14:34:44 +02:00
Simon Marchi ec16513e31 gdb: fix whitespaces in ChangeLog
Replace 8 spaces with tabs.

Change-Id: I60f2df3a2866f75f0307077bf6cb1fa3918f8180
2020-06-10 23:00:20 -04:00
Tom de Vries 7ab9679411 [gdb/symtab] Enable ada .gdb_index
Currently the .gdb_index is not enabled for ada executables (PR24713).

Fix this by adding the required support in write_psymbols, similar to how that
is done for .debug_names in debug_names::insert.

Tested on x86_64-linux, with native and target board cc-with-gdb-index.

gdb/ChangeLog:

2020-06-10  Tom de Vries  <tdevries@suse.de>

	PR ada/24713
	* dwarf2/index-write.c (struct mapped_symtab): Add m_string_obstack.
	(write_psymbols): Enable .gdb_index for ada.
	* dwarf2/read.c: Remove comment stating .gdb_index is unsupported for
	ada.

gdb/testsuite/ChangeLog:

2020-06-10  Tom de Vries  <tdevries@suse.de>

	* gdb.ada/ptype_union.exp: Remove PR24713 workaround.
2020-06-10 14:46:53 +02:00
Tom de Vries e5f3ece2ab [gdb/symtab] Fix name lookup in dw2_map_matching_symbols
In commit 9a0bacfb08 "[gdb/symtab] Handle .gdb_index in ada language mode", a
missing part of dw2_map_matching_symbols was added, containing a call to
dw2_expand_symtabs_matching_symbol.

However, the callback passed to that call has one problem: the callback has an
argument "offset_type namei", which is ignored.  Instead, match_name is passed
as argument to dw2_symtab_iter_init, where a name lookup is done, which may or
may not yield the same value as namei.

Fix this by creating a new version of dw2_symtab_iter_init that takes a
"offset_type namei" argument instead of "const char *name", and passing namei.

Tested on x86_64-linux, with native and target board cc-with-gdb-index.

gdb/ChangeLog:

2020-06-10  Tom de Vries  <tdevries@suse.de>

	* dwarf2/read.c (dw2_symtab_iter_init_common): Factor out of ...
	(dw2_symtab_iter_init): ... here.  Add variant with "offset_type
	namei" instead of "const char *name" argument.
	(dw2_map_matching_symbols): Use "offset_type namei" variant of
	dw2_symtab_iter_init.
2020-06-10 14:46:53 +02:00
Simon Marchi 940da03e32 gdb: remove TYPE_FIELD_TYPE macro
Remove the `TYPE_FIELD_TYPE` macro, changing all the call sites to use
`type::field` and `field::type` directly.

gdb/ChangeLog:

	* gdbtypes.h (TYPE_FIELD_TYPE): Remove.  Change all call sites
	to use type::field and field::type instead.

Change-Id: Ifda6226a25c811cfd334a756a9fbc5c0afdddff3
2020-06-08 15:26:31 -04:00
Simon Marchi b6cdac4b80 gdb: remove FIELD_TYPE macro
Remove the `FIELD_TYPE` macro, changing all the call sites to use
`field::type` directly.

gdb/ChangeLog:

	* gdbtypes.h (FIELD_TYPE): Remove.  Change all call sites
	to use field::type instead.

Change-Id: I7673fedaa276e485189c87991a9043495da22ef5
2020-06-08 15:26:06 -04:00
Simon Marchi 5d14b6e5d6 gdb: add field::type / field::set_type
Add the `type` and `set_type` methods on `struct field`, in order to
remoremove the `FIELD_TYPE` macro.  In this patch, the `FIELD_TYPE`
macro is changed to use `field::type`, so all the call sites that are
useused to set the field's type are changed to use `field::set_type`.
The next patch will remove `FIELD_TYPE` completely.

Note that because of the name clash between the existing field named
`type` and the new method, I renamed the field `m_type`.  It is not
private per-se, because we can't make `struct field` a non-POD yet, but
it should be considered private anyway (not accessed outside `struct
field`).

gdb/ChangeLog:

	* gdbtypes.h (struct field) <type, set_type>: New methods.
	Rename `type` field to...
	<m_type>: ... this.  Change references throughout to use type or
	set_type methods.
	(FIELD_TYPE): Use field::type.  Change call sites that modify
	the field's type to use field::set_type instead.

Change-Id: Ie21f866e3b7f8a51ea49b722d07d272a724459a0
2020-06-08 15:26:04 -04:00
Simon Marchi 3d967001ec gdb: remove TYPE_INDEX_TYPE macro
Remove `TYPE_INDEX_TYPE` macro, changing all the call sites to use
`type::index_type` directly.

gdb/ChangeLog:

	* gdbtypes.h (TYPE_INDEX_TYPE): Remove.  Change all call sites
	to use type::index_type instead.

Change-Id: I56715df0bdec89463cda6bd341dac0e01b2faf84
2020-06-08 15:26:01 -04:00
Simon Marchi 262abc0d67 gdb: add type::index_type / type::set_index_type
Add the `index_type` and `set_index_type` methods on `struct type`, in
order to remove the `TYPE_INDEX_TYPE` macro.  In this patch, the
`TYPE_INDEX_TYPE` macro is changed to use `type::index_type`, so all the
call sites that are used to set the type's index type are changed to use
`type::set_index_type`.  The next patch will remove `TYPE_INDEX_TYPE`
completely.

gdb/ChangeLog:

	* gdbtypes.h (struct type) <index_type, set_index_type>: New
	methods.
	(TYPE_INDEX_TYPE): Use type::index_type.
	* gdbtypes.c (create_array_type_with_stride): Likewise.

Change-Id: I93bdca9de9f3e143d2ccea59310c63745315e18d
2020-06-08 15:25:50 -04:00
Tom Tromey 82836c928f Remove unused parameter from generic_val_print_float
generic_val_print_float has an "embedded_offset" parameter, but it can
only ever be 0.  I believe it is a leftover from the val_print
removal.  This patch removes this parameter.

gdb/ChangeLog
2020-06-07  Tom Tromey  <tom@tromey.com>

	* valprint.c (generic_val_print_float): Remove "embedded_offset"
	parameter.
	(generic_value_print): Update.
2020-06-07 08:22:46 -06:00
Andrew Burgess 940dace9cf Revert "gdb/python: Avoid use after free in py-tui.c"
This reverts commit 982a38f60b.

I missed that the title being assigned too was a std::string, and so
there is no leak.
2020-06-05 21:12:55 +01:00
Andrew Burgess 982a38f60b gdb/python: Avoid use after free in py-tui.c
When setting the window title of a tui frame we do this:

  gdb::unique_xmalloc_ptr<char> value
    = python_string_to_host_string (<python-object>);
  ...
  win->window->title = value.get ();

The problem here is that 'get ()' only borrows the pointer from value,
when value goes out of scope the pointer will be freed.  As a result,
the tui frame will be left with a pointer to undefined memory
contents.

Instead we should be using 'value.release ()' to take ownership of the
pointer from value.

gdb/ChangeLog:

	* python/py-tui.c (gdbpy_tui_set_title): Use release, not get, to
	avoid use after free.
2020-06-05 19:21:20 +01:00
Tom de Vries 82f06518c4 [gdb/NEWS] Fix typos
Fix a few typos in gdb/NEWS.

gdb/ChangeLog:

2020-06-05  Tom de Vries  <tdevries@suse.de>

	* NEWS: Fix typos.
2020-06-05 12:24:20 +02:00
Simon Marchi f8c4185131 gdb: really share partial symtabs when using .gdb_index or .debug_names
Fix/follow-up to commit 17ee85fc2a ("Share DWARF partial symtabs").

In the non-index case, where GDB builds partial symbols from scratch,
two objfiles around the same BFD correctly share partial symtabs.  The
first objfile, which has to do all the work, saves a reference to the
created partial symtabs in the shared per_bfd object (at the end of
dwarf2_build_psymtabs).  The second objfile, when it reaches
dwarf2_build_psymtabs, sees that there are already partial symtabs built
for this BFD and just uses it.

However, that commit missed implementing the same sharing for cases
where GDB uses .gdb_index or .debug_names to build the partial symtabs.

This patch fixes it by having the first objfile to use the BFD set
per_bfd->partial_symtabs at the end of dwarf2_read_gdb_index /
dwarf2_read_debug_names.  For the subsequent objfiles using that BFD,
the partial symtabs are then picked up in dwarf2_initialize_objfile.

This patch adds a test that mimics how the issue was originally
triggered:

  1. Load the test file twice, such that the second objfile re-uses the
     per_bfd object created for the first objfile.
  2. Run to some point where in the backtrace there is a frame for a
     function that's in a CU that's not yet read in.
  3. Check that this frame's information is complete in the "backtrace"
     output.

Step 2 requires an address -> symbol lookup which uses the addrmap at
objfile->partial_symtabs->psymtabs_addrmap.  If the
objfile->partial_symtabs link is not properly setup (as is the case
before this patch), the symbol for that frame won't be found and we'll
get a frame with incomplete information.

The test fails without the fix when using boards "cc-with-gdb-index" and
"cc-with-debug-names".

gdb/ChangeLog:

	* dwarf2/read.c (dwarf2_read_gdb_index): Save partial_symtabs in
	the per_bfd object.
	(dwarf2_read_debug_names): Likewise.
	(dwarf2_initialize_objfile): Use partial_symtabs from per_bfd
	object when re-using a per_bfd object with an index.

gdb/testsuite/ChangeLog:

	* gdb.dwarf2/share-psymtabs-bt.exp: New file.
	* gdb.dwarf2/share-psymtabs-bt.c: New file.
	* gdb.dwarf2/share-psymtabs-bt-2.c: New file.

Change-Id: Ibb26210e2dfc03b80ba9fa56b875ba4cc58c0352
2020-06-04 13:58:48 -04:00
Tom de Vries f9b5d5ea18 [gdb/symtab] Fix missing breakpoint location for inlined function
Consider the test-case contained in this patch.

With -readnow, we have two breakpoint locations:
...
$ gdb -readnow -batch breakpoint-locs -ex "b N1::C1::baz" -ex "info break"
Breakpoint 1 at 0x4004cb: N1::C1::baz. (2 locations)
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   <MULTIPLE>
1.1                         y   0x00000000004004cb in N1::C1::baz() \
                                                     at breakpoint-locs.h:6
1.2                         y   0x00000000004004f0 in N1::C1::baz() \
                                                     at breakpoint-locs.h:6
...

But without -readnow, we have instead only one breakpoint location:
...
$ gdb -batch breakpoint-locs -ex "b N1::C1::baz" -ex "info break"
Breakpoint 1 at 0x4004f0: file breakpoint-locs.h, line 6.
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x00000000004004f0 in N1::C1::baz() \
                                                     at breakpoint-locs.h:6
...

The relevant dwarf is this bit:
...
 <0><d2>: Abbrev Number: 1 (DW_TAG_compile_unit)
    <d8>   DW_AT_name        : breakpoint-locs.cc
 <1><f4>: Abbrev Number: 2 (DW_TAG_namespace)
    <f5>   DW_AT_name        : N1
 <2><fe>: Abbrev Number: 3 (DW_TAG_class_type)
    <ff>   DW_AT_name        : C1
 <3><109>: Abbrev Number: 4 (DW_TAG_subprogram)
    <10a>   DW_AT_name        : baz
    <110>   DW_AT_linkage_name: _ZN2N12C13bazEv
 <2><116>: Abbrev Number: 5 (DW_TAG_subprogram)
    <117>   DW_AT_name        : foo
    <11d>   DW_AT_linkage_name: _ZN2N13fooEv
 <1><146>: Abbrev Number: 8 (DW_TAG_subprogram)
    <147>   DW_AT_specification: <0x116>
    <14b>   DW_AT_low_pc      : 0x4004c7
    <153>   DW_AT_high_pc     : 0x10
 <2><161>: Abbrev Number: 9 (DW_TAG_inlined_subroutine)
    <162>   DW_AT_abstract_origin: <0x194>
    <166>   DW_AT_low_pc      : 0x4004cb
    <16e>   DW_AT_high_pc     : 0x9
 <1><194>: Abbrev Number: 12 (DW_TAG_subprogram)
    <195>   DW_AT_specification: <0x109>
    <199>   DW_AT_inline      : 3       (declared as inline and inlined)
...

The missing breakpoint location is specified by DIE 0x161, which is ignored by
the partial DIE reader because it's a child of a DW_TAG_subprogram DIE (at
0x146, for foo).

Fix this by not ignoring the DIE during partial DIE reading.

Tested on x86_64-linux.

gdb/ChangeLog:

2020-06-03  Tom de Vries  <tdevries@suse.de>

	PR symtab/26046
	* dwarf2/read.c (scan_partial_symbols): Recurse into DW_TAG_subprogram
	children for C++.
	(load_partial_dies): Don't skip DW_TAG_inlined_subroutine child of
	DW_TAG_subprogram.

gdb/testsuite/ChangeLog:

2020-06-03  Tom de Vries  <tdevries@suse.de>

	PR symtab/26046
	* gdb.cp/breakpoint-locs-2.cc: New test.
	* gdb.cp/breakpoint-locs.cc: New test.
	* gdb.cp/breakpoint-locs.exp: New file.
	* gdb.cp/breakpoint-locs.h: New test.
2020-06-03 23:50:16 +02:00
Andrew Burgess f6eee2d098 gdb: Convert language skip_trampoline field to a method
This commit changes the language_data::skip_trampoline function
pointer member variable into a member function of language_defn.

There should be no user visible changes after this commit.

gdb/ChangeLog:

	* ada-lang.c (ada_language_data): Delete skip_trampoline
	initializer.
	* c-lang.c (c_language_data): Likewise.
	(cplus_language_data): Likewise.
	(cplus_language::skip_trampoline): New member function.
	(asm_language_data): Delete skip_trampoline initializer.
	(minimal_language_data): Likewise.
	* d-lang.c (d_language_data): Likewise.
	* f-lang.c (f_language_data): Likewise.
	* go-lang.c (go_language_data): Likewise.
	* language.c (unk_lang_trampoline): Delete function.
	(skip_language_trampoline): Update.
	(unknown_language_data): Delete skip_trampoline initializer.
	(auto_language_data): Likewise.
	* language.h (language_data): Delete skip_trampoline field.
	(language_defn::skip_trampoline): New function.
	* m2-lang.c (m2_language_data): Delete skip_trampoline
	initializer.
	* objc-lang.c (objc_skip_trampoline): Delete function, move
	implementation to objc_language::skip_trampoline.
	(objc_language_data): Delete skip_trampoline initializer.
	(objc_language::skip_trampoline): New member function with
	implementation from objc_skip_trampoline.
	* opencl-lang.c (opencl_language_data): Delete skip_trampoline
	initializer.
	* p-lang.c (pascal_language_data): Likewise.
	* rust-lang.c (rust_language_data): Likewise.
2020-06-02 13:53:11 +01:00
Andrew Burgess 0a50df5dab gdb: Convert language la_demangle field to a method
This commit changes the language_data::la_demangle function pointer
member variable into a member function of language_defn.

The only slightly "weird" change in this commit is in f-lang.c, where
I have given the Fortran language a demangle method that is identical
to the default language_defn::demangle.  The only reason for this is
to give me somewhere to copy the comment that was previously embedded
within the f_language_data structure.

There should be no user visible changes after this commit.

gdb/ChangeLog:

	* ada-lang.c (ada_language_data): Delete la_demangle initializer.
	(ada_language::demangle): New member function.
	* c-lang.c (c_language_data): Delete la_demangle initializer.
	(cplus_language_data): Delete la_demangle initializer.
	(cplus_language::demangle): New member function.
	(asm_language_data): Delete la_demangle initializer.
	(minimal_language_data): Delete la_demangle initializer.
	* d-lang.c (d_language_data): Delete la_demangle initializer.
	(d_language::demangle): New member function.
	* f-lang.c (f_language_data): Delete la_demangle initializer.
	(f_language::demangle): New member function.
	* go-lang.c (go_language_data): Delete la_demangle initializer.
	(go_language::demangle): New member function.
	* language.c (language_demangle): Update.
	(unk_lang_demangle): Delete.
	(unknown_language_data): Delete la_demangle initializer.
	(unknown_language::demangle): New member function.
	(auto_language_data): Delete la_demangle initializer.
	(auto_language::demangle): New member function.
	* language.h (language_data): Delete la_demangle field.
	(language_defn::demangle): New function.
	* m2-lang.c (m2_language_data): Delete la_demangle initializer.
	* objc-lang.c (objc_language_data): Delete la_demangle
	initializer.
	(objc_language::demangle): New member function.
	* opencl-lang.c (opencl_language_data): Delete la_demangle
	initializer.
	* p-lang.c (pascal_language_data): Likewise.
	* rust-lang.c (rust_language_data): Likewise.
	(rust_language::demangle): New member functi
2020-06-02 13:53:11 +01:00
Andrew Burgess fbfb0a463f gdb: Convert language la_print_type field to a method
This commit changes the language_data::la_print_type function pointer
member variable into a member function of language_defn.

There should be no user visible changes after this commit.

gdb/ChangeLog:

	* ada-lang.c (ada_language_data): Delete la_print_type
	initializer.
	(ada_language::print_type): New member function.
	* c-lang.c (c_language_data): Delete la_print_type initializer.
	(c_language::print_type): New member function.
	(cplus_language_data): Delete la_print_type initializer.
	(cplus_language::print_type): New member function.
	(asm_language_data): Delete la_print_type initializer.
	(asm_language::print_type): New member function.
	(minimal_language_data): Delete la_print_type initializer.
	(minimal_language::print_type): New member function.
	* d-lang.c (d_language_data): Delete la_print_type initializer.
	(d_language::print_type): New member function.
	* f-lang.c (f_language_data): Delete la_print_type initializer.
	(f_language::print_type): New member function.
	* go-lang.c (go_language_data): Delete la_print_type initializer.
	(go_language::print_type): New member function.
	* language.c (unk_lang_print_type): Delete.
	(unknown_language_data): Delete la_print_type initializer.
	(unknown_language::print_type): New member function.
	(auto_language_data): Delete la_print_type initializer.
	(auto_language::print_type): New member function.
	* language.h (language_data): Delete la_print_type field.
	(language_defn::print_type): New function.
	(LA_PRINT_TYPE): Update.
	* m2-lang.c (m2_language_data): Delete la_print_type initializer.
	(m2_language::print_type): New member function.
	* objc-lang.c (objc_language_data): Delete la_print_type
	initializer.
	(objc_language::print_type): New member function.
	* opencl-lang.c (opencl_print_type): Delete, implementation moved
	to opencl_language::print_type.
	(opencl_language_data): Delete la_print_type initializer.
	(opencl_language::print_type): New member function, implementation
	from opencl_print_type.
	* p-lang.c (pascal_language_data): Delete la_print_type
	initializer.
	(pascal_language::print_type): New member function.
	* rust-lang.c (rust_print_type): Delete, implementation moved to
	rust_language::print_type.
	(rust_language_data): Delete la_print_type initializer.
	(rust_language::print_type): New member function, implementation
	from rust_print_type.
2020-06-02 13:53:11 +01:00
Andrew Burgess 6f8270197a gdb: Convert language la_sniff_from_mangled_name field to a method
This commit changes the language_data::la_sniff_from_mangled_name
function pointer member variable into a member function of
language_defn.

Previously the la_sniff_from_mangled_name pointer was NULL for some
languages, however, all uses of this function pointer were through the
function language_sniff_from_mangled_name which provided a default
implementation.

This default implementation now becomes the implementation in the base
class language_defn, which is then overridden as required in various
language sub-classes.

There should be no user visible changes after this commit.

gdb/ChangeLog:

	* ada-lang.c (ada_sniff_from_mangled_name): Delete function,
	implementation moves to...
	(ada_language::sniff_from_mangled_name): ...here.  Update return
	type.
	(ada_language_data): Delete la_sniff_from_mangled_name
	initializer.
	* c-lang.c (c_language_data): Likewise.
	(cplus_language_data): Likewise.
	(cplus_language::sniff_from_mangled_name): New member function,
	implementation taken from gdb_sniff_from_mangled_name.
	(asm_language_data): Delete la_sniff_from_mangled_name
	initializer.
	(minimal_language_data): Likewise.
	* cp-support.c (gdb_sniff_from_mangled_name): Delete,
	implementation moves to cplus_language::sniff_from_mangled_name.
	* cp-support.h (gdb_sniff_from_mangled_name): Delete declaration.
	* d-lang.c (d_sniff_from_mangled_name): Delete, implementation
	moves to...
	(d_language::sniff_from_mangled_name): ...here.
	(d_language_data): Delete la_sniff_from_mangled_name initializer.
	* f-lang.c (f_language_data): Likewise.
	* go-lang.c (go_sniff_from_mangled_name): Delete, implementation
	moves to...
	(go_language::sniff_from_mangled_name): ...here.
	(go_language_data): Delete la_sniff_from_mangled_name initializer.
	* language.c (language_sniff_from_mangled_name): Delete.
	(unknown_language_data): Delete la_sniff_from_mangled_name
	initializer.
	(auto_language_data): Likewise.
	* language.h (language_data): Delete la_sniff_from_mangled_name
	field.
	(language_defn::sniff_from_mangled_name): New function.
	(language_sniff_from_mangled_name): Delete declaration.
	* m2-lang.c (m2_language_data): Delete la_sniff_from_mangled_name
	field.
	* objc-lang.c (objc_sniff_from_mangled_name): Delete,
	implementation moves to...
	(objc_language::sniff_from_mangled_name): ...here.
	(objc_language_data): Delete la_sniff_from_mangled_name initializer.
	* opencl-lang.c (opencl_language_data): Likewise.
	* p-lang.c (pascal_language_data): Likewise.
	* rust-lang.c (rust_sniff_from_mangled_name): Delete,
	implementation moves to...
	(rust_language::sniff_from_mangled_name): ...here.
	(rust_language_data): Delete la_sniff_from_mangled_name
	initializer.
	* symtab.c (symbol_find_demangled_name): Call
	sniff_from_mangled_name member function.
2020-06-02 13:53:11 +01:00
Andrew Burgess fb8006fd35 gdb: Convert language la_search_name_hash field to a method
This commit changes the language_data::la_search_name_hash
function pointer member variable into a member function of
language_defn.

There should be no user visible changes after this commit.

gdb/ChangeLog:

	* ada-lang.c (ada_language_data): Delete la_search_name_hash
	initializer.
	* c-lang.c (c_language_data): Likewise.
	(cplus_language_data): Likewise.
	(cplus_language::search_name_hash): New member function.
	(asm_language_data): Delete la_search_name_hash initializer.
	(minimal_language_data): Likewise.
	* d-lang.c (d_language_data): Likewise.
	* dictionary.c (default_search_name_hash): Rename to...
	(language_defn::search_name_hash): ...this.
	* f-lang.c (f_language_data): Likewise.
	(f_language::search_name_hash): New member function.
	* go-lang.c (go_language_data): Delete la_search_name_hash
	initializer.
	* language.c (unknown_language_data): Likewise.
	(auto_language_data): Likewise.
	* language.h (struct language_data): Delete la_search_name_hash
	field.
	(language_defn::search_name_hash): Declare new member function.
	(default_search_name_hash): Delete declaration.
	* m2-lang.c (m2_language_data): Delete la_search_name_hash
	initializer.
	* objc-lang.c (objc_language_data): Likewise.
	* opencl-lang.c (opencl_language_data): Likewise.
	* p-lang.c (pascal_language_data): Likewise.
	* rust-lang.c (rust_language_data): Likewise.
	* symtab.c (search_name_hash): Update call.
2020-06-02 13:53:10 +01:00
Andrew Burgess 8e25bafe93 gdb: Convert language la_get_compile_instance field to a method
This commit changes the language_data::la_get_compile_instance
function pointer member variable into a member function of
language_defn.  Unlike previous commits converting fields of
language_data to member function in language_defn, this field is NULL
for some languages.  As a result I had to change the API slightly so
that the base language_defn class provides an implementation.

There should be no user visible changes after this commit.

gdb/ChangeLog:

	* ada-lang.c (ada_language_data): Delete la_get_compile_instance
	initializer.
	* c-lang.c (class compile_instance): Declare.
	(c_language_data): Delete la_get_compile_instance initializer.
	(c_language::get_compile_instance): New member function.
	(cplus_language_data): Delete la_get_compile_instance initializer.
	(cplus_language::get_compile_instance): New member function.
	(asm_language_data): Delete la_get_compile_instance initializer.
	(minimal_language_data): Likewise.
	* c-lang.h (c_get_compile_context): Update comment.
	(cplus_get_compile_context): Update comment.
	* compile/compile.c (compile_to_object): Update calls, don't rely
	on function pointer being NULL.
	* d-lang.c (d_language_data): Delete la_get_compile_instance
	initializer.
	* f-lang.c (f_language_data): Likewise.
	* go-lang.c (go_language_data): Likewise.
	* language.c (unknown_language_data): Likewise.
	(auto_language_data): Likewise.
	* language.h (language_data): Delete la_get_compile_instance field.
	(language_defn::get_compile_instance): New member function.
	* m2-lang.c (m2_language_data): Delete la_get_compile_instance
	initializer.
	* objc-lang.c (objc_language_data): Likewise.
	* opencl-lang.c (opencl_language_data): Likewise.
	* p-lang.c (pascal_language_data): Likewise.
	* rust-lang.c (rust_language_data): Likewise.
2020-06-02 13:53:10 +01:00
Andrew Burgess 4009ee92c4 gdb: Convert language la_iterate_over_symbols field to a method
This commit changes the language_data::la_iterate_over_symbols
function pointer member variable into a member function of
language_defn.

There should be no user visible changes after this commit.

gdb/ChangeLog:

	* ada-lang.c (ada_add_all_symbols): Update comment.
	(ada_iterate_over_symbols): Delete, move implementation to...
	(ada_language::iterate_over_symbols): ...here, a new member
	function, rewrite to use range based for loop.
	(ada_language_data): Delete la_iterate_over_symbols initializer.
	* c-lang.c (c_language_data): Likewise.
	(cplus_language_data): Likewise.
	(asm_language_data): Likewise.
	(minimal_language_data): Likewise.
	* d-lang.c (d_language_data): Likewise.
	* f-lang.c (f_language_data): Likewise.
	* go-lang.c (go_language_data): Likewise.
	* language.c (unknown_language_data): Likewise.
	(auto_language_data): Likewise.
	* language.h (language_data): Delete la_iterate_over_symbols field.
	(language_defn::iterate_over_symbols): New member function.
	(LA_ITERATE_OVER_SYMBOLS): Update.
	* linespec.c (iterate_over_all_matching_symtabs): Update.
	* m2-lang.c (m2_language_data): Delete la_iterate_over_symbols
	initializer.
	* objc-lang.c (objc_language_data): Likewise.
	* opencl-lang.c (opencl_language_data): Likewise.
	* p-lang.c (pascal_language_data): Likewise.
	* rust-lang.c (rust_language_data): Likewise.
2020-06-02 13:53:10 +01:00
Andrew Burgess 54f4ca4610 gdb: Convert language la_lookup_transparent_type field to a method
This commit changes the language_data::la_lookup_transparent_type
function pointer member variable into a member function of
language_defn.

There should be no user visible changes after this commit.

gdb/ChangeLog:

	* ada-lang.c (ada_language_data): Delete
	la_lookup_transparent_type initializer.
	* c-lang.c (c_language_data): Likewise.
	(cplus_language_data): Likewise.
	(cplus_language::lookup_transparent_type): New member function.
	(asm_language_data): Delete la_lookup_transparent_type
	initializer.
	(minimal_language_data): Likewise.
	* d-lang.c (d_language_data): Likewise.
	* f-lang.c (f_language_data): Likewise.
	* go-lang.c (go_language_data): Likewise.
	* language.c (unknown_language_data): Likewise.
	(auto_language_data): Likewise.
	* language.h (struct language_data): Delete
	la_lookup_transparent_type field.
	(language_defn::lookup_transparent_type): New member function.
	* m2-lang.c (m2_language_data): Delete la_lookup_transparent_type
	initializer.
	* objc-lang.c (objc_language_data): Likewise.
	* opencl-lang.c (opencl_language_data): Likewise.
	* p-lang.c (pascal_language_data): Likewise.
	* rust-lang.c (rust_language_data): Likewise.
	* symtab.c (symbol_matches_domain): Update call.
2020-06-02 13:53:10 +01:00
Andrew Burgess 1fb314aaa3 gdb: Convert language la_language_arch_info field to a method
This commit changes the language_data::la_language_arch_info function
pointer member variable into a member function of language_defn.

There should be no user visible changes after this commit.

gdb/ChangeLog:

	* ada-lang.c (ada_language_arch_info): Delete function, move
	implementation to...
	(ada_language::language_arch_info): ...here, a new member
	function.
	(ada_language_data): Delete la_language_arch_info.
	* c-lang.c (c_language_data): Likewise.
	(c_language::language_arch_info): New member function.
	(cplus_language_arch_info): Delete function, move
	implementation to...
	(cplus_language::language_arch_info): ...here, a new member
	function.
	(cplus_language_data): Delete la_language_arch_info.
	(asm_language_data): Likewise.
	(asm_language::language_arch_info): New member function.
	(minimal_language_data): Delete la_language_arch_info.
	(minimal_language::language_arch_info): New member function.
	* d-lang.c (d_language_arch_info): Delete function, move
	implementation to...
	(d_language::language_arch_info): ...here, a new member
	function.
	(d_language_data): Delete la_language_arch_info.
	* f-lang.c (f_language_arch_info): Delete function, move
	implementation to...
	(f_language::language_arch_info): ...here, a new member
	function.
	(f_language_data): Delete la_language_arch_info.
	* go-lang.c (go_language_arch_info): Delete function, move
	implementation to...
	(go_language::language_arch_info): ...here, a new member
	function.
	(go_language_data): Delete la_language_arch_info.
	* language.c (unknown_language_data): Likewise.
	(unknown_language::language_arch_info): New member function.
	(auto_language_data): Delete la_language_arch_info.
	(auto_language::language_arch_info): New member function.
	(language_gdbarch_post_init): Update call to
	la_language_arch_info.
	* language.h (language_data): Delete la_language_arch_info
	function pointer.
	(language_defn::language_arch_info): New function.
	* m2-lang.c (m2_language_arch_info): Delete function, move
	implementation to...
	(m2_language::language_arch_info): ...here, a new member
	function.
	(m2_language_data): Delete la_language_arch_info.
	* objc-lang.c (objc_language_arch_info): Delete function, move
	implementation to...
	(objc_language::language_arch_info): ...here, a new member
	function.
	(objc_language_data): Delete la_language_arch_info.
	* opencl-lang.c (opencl_language_arch_info): Delete function, move
	implementation to...
	(opencl_language::language_arch_info): ...here, a new member
	function.
	(opencl_language_data): Delete la_language_arch_info.
	* p-lang.c (pascal_language_arch_info): Delete function, move
	implementation to...
	(pascal_language::language_arch_info): ...here, a new member
	function.
	(pascal_language_data): Delete la_language_arch_info.
	* rust-lang.c (rust_language_arch_info): Delete function, move
	implementation to...
	(rust_language::language_arch_info): ...here, a new member
	function.
	(rust_language_data): Delete la_language_arch_info.
2020-06-02 13:53:10 +01:00
Andrew Burgess 48448202d7 gdb: Convert language la_pass_by_reference field to a method
This commit changes the language_data::la_pass_by_reference function
pointer member variable into a member function of language_defn.

The interesting thing in this commit is that I have removed the
default_pass_by_reference function entirely.  This function only ever
returned a language_pass_by_ref_info struct in its default state, so
all uses of this function can be replaced by just default
initialisation of a language_pass_by_ref_info variable.

There should be no user visible changes after this commit.

gdb/ChangeLog:

	* ada-lang.c (ada_language_data): Delete la_pass_by_reference
	initializer.
	* c-lang.c (c_language_data): Likewise.
	(cplus_language_data): Likewise.
	(cplus_language::pass_by_reference_info): New method.
	(asm_language_data): Delete la_pass_by_reference initializer.
	(minimal_language_data): Likewise.
	* cp-abi.c (cp_pass_by_reference): Remove use of
	default_pass_by_reference.
	* d-lang.c (d_language_data): Likewise.
	* f-lang.c (f_language_data): Likewise.
	* gnu-v3-abi.c (gnuv3_pass_by_reference): Remove use of
	default_pass_by_reference.
	* go-lang.c (go_language_data): Likewise.
	* language.c (language_pass_by_reference): Update.
	(default_pass_by_reference): Delete.
	(unknown_language_data): Delete la_pass_by_reference
	initializer.
	(auto_language_data): Likewise.
	* language.h (struct language_data): Delete la_pass_by_reference
	field.
	(language_defn::pass_by_reference_info): New member function.
	(default_pass_by_reference): Delete declaration.
	* m2-lang.c (m2_language_data): Delete la_pass_by_reference
	initializer.
	* objc-lang.c (objc_language_data): Likewise.
	* opencl-lang.c (opencl_language_data): Likewise.
	* p-lang.c (pascal_language_data): Likewise.
	* rust-lang.c (rust_language_data): Likewise.
2020-06-02 13:53:10 +01:00
Andrew Burgess 15e5fd3556 gdb: Convert language la_read_var_value field to a method
This commit changes the language_data::la_read_var_value function
pointer member variable into a member function of language_defn.

An interesting aspect of this change is that the implementation of
language_defn::read_var_value is actually in findvar.c.  This is
partly historical, the new language_defn::read_var_value is a rename
of default_read_var_value, which was already in that file, but also,
that is the file that contains the helper functions needed by the
read_var_value method, so it makes sens that the method implementation
should continue to live there (I think).

There should be no user visible changes after this commit.

gdb/ChangeLog:

	* ada-lang.c (ada_read_var_value): Delete function, move
	implementation to...
	(ada_language::read_var_value): ...here.
	(ada_language_data): Delete la_read_var_value initializer.
	* c-lang.c (c_language_data): Likewise.
	(cplus_language_data): Likewise.
	(minimal_language_data): Likewise.
	* d-lang.c (d_language_data): Likewise.
	* f-lang.c (f_language_data): Likewise.
	* findvar.c (default_read_var_value): Rename to...
	(language_defn::read_var_value): ...this.
	* findvar.c (read_var_value): Update header comment, and change to
	call member function instead of function pointer.
	* go-lang.c (go_language_data): Likewise.
	* language.c (unknown_language_data): Delete la_read_var_value
	initializer.
	(auto_language_data): Likewise.
	* language.h (struct language_data): Delete la_read_var_value
	field.
	(language_defn::read_var_value): New member function.
	(default_read_var_value): Delete declaration.
	* m2-lang.c (m2_language_data): Delete la_read_var_value
	initializer.
	* objc-lang.c (objc_language_data): Likewise.
	* opencl-lang.c (opencl_language_data): Likewise.
	* p-lang.c (pascal_language_data): Likewise.
	* rust-lang.c (rust_language_data): Likewise.
	* value.h (default_read_var_value): Delete declaration.
2020-06-02 13:53:10 +01:00
Andrew Burgess 5bd40f2a3f gdb: Convert language la_print_array_index field to a method
This commit changes the language_data::la_print_array_index function
pointer member variable into a member function of language_defn.

There should be no user visible changes after this commit.

gdb/ChangeLog:

	* ada-lang.c (ada_print_array_index): Delete function, move
	implementation to...
	(ada_language::print_array_index): ...here.
	(ada_language_data): Delete la_print_array_index initializer.
	* c-lang.c (c_language_data): Likewise.
	(cplus_language_data): Likewise.
	(minimal_language_data): Likewise.
	* d-lang.c (d_language_data): Likewise.
	* f-lang.c (f_language_data): Likewise.
	* go-lang.c (go_language_data): Likewise.
	* language.c (default_print_array_index): Delete function, move
	implementation to...
	(language_defn::print_array_index): ...here.
	(unknown_language_data): Delete la_print_array_index initializer.
	(auto_language_data): Likewise.
	* language.h (struct language_data): Delete la_print_array_index
	field.
	(language_defn::print_array_index): New member function.
	(LA_PRINT_ARRAY_INDEX): Update.
	(default_print_array_index): Delete declaration.
	* m2-lang.c (m2_language_data): Delete la_print_array_index
	initializer.
	* objc-lang.c (objc_language_data): Likewise.
	* opencl-lang.c (opencl_language_data): Likewise.
	* p-lang.c (pascal_language_data): Likewise.
	* rust-lang.c (rust_language_data): Likewise.
2020-06-02 13:53:10 +01:00
Andrew Burgess 0874fd075b gdb: Represent all languages as sub-classes of language_defn
This commit converts all languages to sub-classes of a language_defn
base class.

The motivation for this change is to make it easier to add new methods
onto languages without having to update all of the individual language
structures.  In the future it might be possible to move more things,
like expression parsing, into the language class(es) for better
encapsulation, however I have no plans to tackle this in the short
term.

This commit sets up a strategy for transitioning from the current
language system, where each language is an instance of the
language_defn structure, to the class hierarchy system.

The plan is to rename the existing language_defn into language_data,
and make this a base class for the new language_defn class, something
like this:

  struct language_data
  {
    ... old language_defn fields here ...
  };

  struct language_defn : public language_data
  {
    language_defn (const language_data d)
      : language_data (d)
    { .... }
  };

Then each existing language, for example ada_language_defn can be
converted into an instance of language_data, and passed into the
constructor of a new language class, something like this:

  language_data ada_language_data =
  {
    ... old ada_language_defn values here ...
  };

  struct ada_language : public language_defn
  {
    ada_language (ada_language_data)
    { .... }
  };

What this means is that immediately after the conversion nothing much
changes.  Every language is now its own class, but all the old
language fields still exist and can be accessed in the same way.

In later commits I will convert function pointers from the old
language_defn structure into real class methods on language_defn, with
overrides on sub-classes where needed.

At this point I imagine that those fields of the old language_defn
structure that contained only data will probably remain as data fields
within the new language_data base structure, it is only the methods
that I plan to change initially.

I tweaked how we manage the list of languages a bit, each language is
now registered as it is created, and this resulted in a small number
of changes in language.c.

Most of the changes in the *-lang.c files are identical.

There should be no user visible changes after this commit.

gdb/ChangeLog:

	* gdb/ada-lang.c (ada_language_defn): Convert to...
	(ada_language_data): ...this.
	(class ada_language): New class.
	(ada_language_defn): New static global.
	* gdb/c-lang.c (c_language_defn): Convert to...
	(c_language_data): ...this.
	(class c_language): New class.
	(c_language_defn): New static global.
	(cplus_language_defn): Convert to...
	(cplus_language_data): ...this.
	(class cplus_language): New class.
	(cplus_language_defn): New static global.
	(asm_language_defn): Convert to...
	(asm_language_data): ...this.
	(class asm_language): New class.
	(asm_language_defn): New static global.
	(minimal_language_defn): Convert to...
	(minimal_language_data): ...this.
	(class minimal_language): New class.
	(minimal_language_defn): New static global.
	* gdb/d-lang.c (d_language_defn): Convert to...
	(d_language_data): ...this.
	(class d_language): New class.
	(d_language_defn): New static global.
	* gdb/f-lang.c (f_language_defn): Convert to...
	(f_language_data): ...this.
	(class f_language): New class.
	(f_language_defn): New static global.
	* gdb/go-lang.c (go_language_defn): Convert to...
	(go_language_data): ...this.
	(class go_language): New class.
	(go_language_defn): New static global.
	* gdb/language.c (unknown_language_defn): Remove declaration.
	(current_language): Initialize to nullptr, real initialization is
	moved to _initialize_language.
	(languages): Delete global.
	(language_defn::languages): Define.
	(set_language_command): Use language_defn::languages.
	(set_language): Likewise.
	(range_error): Likewise.
	(language_enum): Likewise.
	(language_def): Likewise.
	(add_set_language_command): Use language_def::languages for the
	language list, and language_def to lookup language pointers.
	(skip_language_trampoline): Use language_defn::languages.
	(unknown_language_defn): Convert to...
	(unknown_language_data): ...this.
	(class unknown_language): New class.
	(unknown_language_defn): New static global.
	(auto_language_defn): Convert to...
	(auto_language_data): ...this.
	(class auto_language): New class.
	(auto_language_defn): New static global.
	(language_gdbarch_post_init): Use language_defn::languages.
	(_initialize_language): Initialize current_language.
	* gdb/language.h (struct language_defn): Rename to...
	(struct language_data): ...this.
	(struct language_defn): New.
	(auto_language_defn): Delete.
	(unknown_language_defn): Delete.
	(minimal_language_defn): Delete.
	(ada_language_defn): Delete.
	(asm_language_defn): Delete.
	(c_language_defn): Delete.
	(cplus_language_defn): Delete.
	(d_language_defn): Delete.
	(f_language_defn): Delete.
	(go_language_defn): Delete.
	(m2_language_defn): Delete.
	(objc_language_defn): Delete.
	(opencl_language_defn): Delete.
	(pascal_language_defn): Delete.
	(rust_language_defn): Delete.
	* gdb/m2-lang.c (m2_language_defn): Convert to...
	(m2_language_data): ...this.
	(class m2_language): New class.
	(m2_language_defn): New static global.
	* gdb/objc-lang.c (objc_language_defn): Convert to...
	(objc_language_data): ...this.
	(class objc_language): New class.
	(objc_language_defn): New static global.
	* gdb/opencl-lang.c (opencl_language_defn): Convert to...
	(opencl_language_data): ...this.
	(class opencl_language): New class.
	(opencl_language_defn): New static global.
	* gdb/p-lang.c (pascal_language_defn): Convert to...
	(pascal_language_data): ...this.
	(class pascal_language): New class.
	(pascal_language_defn): New static global.
	* gdb/rust-exp.y (rust_lex_tests): Use language_def to find
	language pointer, update comment format.
	* gdb/rust-lang.c (rust_language_defn): Convert to...
	(rust_language_data): ...this.
	(class rust_language): New class.
	(rust_language_defn): New static global.
2020-06-02 13:53:10 +01:00
Andrew Burgess 1313c56ef9 gdb: Preserve is-stmt lines when switch between files
After the is-stmt support commit:

  commit 8c95582da8
  Date:   Mon Dec 30 21:04:51 2019 +0000

      gdb: Add support for tracking the DWARF line table is-stmt field

A regression was observed where a breakpoint could no longer be placed
in some cases.

Consider a line table like this:

  File 1: test.c
  File 2: test.h

  | Addr | File | Line | Stmt |
  |------|------|------|------|
  | 1    | 1    | 16   | Y    |
  | 2    | 1    | 17   | Y    |
  | 3    | 2    | 21   | Y    |
  | 4    | 2    | 22   | Y    |
  | 4    | 1    | 18   | N    |
  | 5    | 2    | 23   | N    |
  | 6    | 1    | 24   | Y    |
  | 7    | 1    | END  | Y    |
  |------|------|------|------|

Before the is-stmt patch GDB would ignore any non-stmt lines, so GDB
built two line table structures:

  File 1                 File 2
  ------                 ------

  | Addr | Line |        | Addr | Line |
  |------|------|        |------|------|
  | 1    | 16   |        | 3    | 21   |
  | 2    | 17   |        | 4    | 22   |
  | 3    | END  |        | 6    | END  |
  | 6    | 24   |        |------|------|
  | 7    | END  |
  |------|------|

After the is-stmt patch GDB now records non-stmt lines, so the
generated line table structures look like this:

  File 1                   File 2
  ------                   ------

  | Addr | Line | Stmt |  | Addr | Line | Stmt |
  |------|------|------|  |------|------|------|
  | 1    | 16   | Y    |  | 3    | 21   | Y    |
  | 2    | 17   | Y    |  | 4    | 22   | Y    |
  | 3    | END  | Y    |  | 4    | END  | Y    |
  | 4    | 18   | N    |  | 5    | 23   | N    |
  | 5    | END  | Y    |  | 6    | END  | Y    |
  | 6    | 24   | Y    |  |------|------|------|
  | 7    | END  | Y    |
  |------|------|------|

The problem is that in 'File 2', end END marker at address 4 causes
the previous line table entry to be discarded, so we actually end up
with this:

  File 2
  ------

  | Addr | Line | Stmt |
  |------|------|------|
  | 3    | 21   | Y    |
  | 4    | END  | Y    |
  | 5    | 23   | N    |
  | 6    | END  | Y    |
  |------|------|------|

When a user tries to place a breakpoint in file 2 at line 22, this is
no longer possible.

The solution I propose here is that we ignore line table entries that
would trigger a change of file if:

  1. The new line being added is at the same address as the previous
  line, and

  2. We have previously seen an is-stmt line at the current address.

The result of this is that GDB switches file, and knows that some line
entry (or entries) are going to be discarded, prefer to keep is-stmt
lines and discard non-stmt lines.

After this commit the lines tables are now:

  File 1                   File 2
  ------                   ------

  | Addr | Line | Stmt |  | Addr | Line | Stmt |
  |------|------|------|  |------|------|------|
  | 1    | 16   | Y    |  | 3    | 21   | Y    |
  | 2    | 17   | Y    |  | 4    | 22   | Y    |
  | 3    | END  | Y    |  | 5    | 23   | N    |
  | 5    | END  | Y    |  | 6    | END  | Y    |
  | 6    | 24   | Y    |  |------|------|------|
  | 7    | END  | Y    |
  |------|------|------|

We've lost the non-stmt entry for file 1, line 18, but retained the
is-stmt entry for file 2, line 22.  The user can now place a
breakpoint at that location.

One problem that came from this commit was the test
gdb.cp/step-and-next-inline.exp, which broke in several places.  After
looking at this test again I think that in some cases this test was
only ever passing by pure luck.  The debug GCC is producing for this
test is pretty broken.  I raised this GCC bug:

  https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94474

for this and disabled one entire half of the test.  There are still
some cases in here that do pass, and if/when GCC is fixed it would be
great to enable this test again.

gdb/ChangeLog:

	* dwarf2/read.c (class lnp_state_machine) <m_last_address>: New
	member variable.
	<m_stmt_at_address>: New member variable.
	(lnp_state_machine::record_line): Don't record some lines, update
	tracking of is_stmt at the same address.
	(lnp_state_machine::lnp_state_machine): Initialise new member
	variables.

gdb/testsuite/ChangeLog:

	* gdb.cp/step-and-next-inline.exp (do_test): Skip all tests in the
	use_header case.
	* gdb.dwarf2/dw2-inline-header-1.exp: New file.
	* gdb.dwarf2/dw2-inline-header-2.exp: New file.
	* gdb.dwarf2/dw2-inline-header-3.exp: New file.
	* gdb.dwarf2/dw2-inline-header-lbls.c: New file.
	* gdb.dwarf2/dw2-inline-header.c: New file.
	* gdb.dwarf2/dw2-inline-header.h: New file.
2020-06-01 10:02:44 +01:00
Samuel Thibault b7ed9f3d46 hurd: Add shared mig declarations
We are using -Werror=missing-declarations, and the _S.h files generated
by mig do not currently include a declaration for the server routine.
gnu-nat.c used to have its own external declarations, but better just
share them between gnu-nat.c and the _S.c files.

Fixes

exc_request_S.c:177:24: error: no previous declaration for ‘exc_server’ [-Werror=missing-declarations]
  177 | mig_external boolean_t exc_server

gdb/ChangeLog:

	* config/i386/i386gnu.mn [%_S.o %_U.o] (COMPILE.post): Add
	"-include gnu-nat-mig.h".
	* gnu-nat-mig.h: New file.
	* gnu-nat.c: Include "gnu-nat-mig.h".
	(exc_server, msg_reply_server, notify_server,
	process_reply_server): Remove declarations.
2020-06-01 07:52:24 +00:00
Samuel Thibault 14a8ad62e6 gnu-nat: Move local functions inside gnu_nat_target class
This allows to have the process_stratum_target object at hand for future use in
the gdb API, and only use gnu_target from external calls.

gdb/Changelog:

	* gnu-nat.h (inf_validate_procs, inf_suspend, inf_set_traced,
	steal_exc_port, proc_get_state, inf_clear_wait, inf_cleanup,
	inf_startup, inf_update_suspends, inf_set_pid, inf_steal_exc_ports,
	inf_validate_procinfo, inf_validate_task_sc, inf_restore_exc_ports,
	inf_set_threads_resume_sc, inf_set_threads_resume_sc_for_signal_thread,
	inf_resume, inf_set_step_thread, inf_detach, inf_attach, inf_signal,
	inf_continue, make_proc, proc_abort, _proc_free, proc_update_sc,
	proc_get_exception_port, proc_set_exception_port, _proc_get_exc_port,
	proc_steal_exc_port, proc_restore_exc_port, proc_trace): Move functions
	to gnu_nat_target class.
	* gnu-nat.c: Likewise.
	(inf_update_procs, S_proc_wait_reply, set_task_pause_cmd,
	set_task_exc_port_cmd, set_signals_cmd, set_thread_pause_cmd,
	set_thread_exc_port_cmd): Call inf_validate_procs through gnu_target
	object.
	(gnu_nat_target::create_inferior, gnu_nat_target::detach): Pass `this'
	instead of `gnu_target'.
2020-05-31 07:42:10 +00:00
Samuel Thibault 0af5e1061d hurd: unwinding support over signal trampolines
This allows to get full backtrace from signal handlers, otherwise the
backtrace stops at the trampoline that calls the handler.

This needs special knowledge how the trampoline records register context
for the sigreturn call after signal handling.

gdb/ChangeLog:

	* i386-gnu-tdep.c: Include "gdbcore.h"
	(gnu_sigtramp_code, i386_gnu_sc_reg_offset): New arrays.
        (GNU_SIGTRAMP_LEN, GNU_SIGTRAMP_TAIL,
        I386_GNU_SIGCONTEXT_THREAD_STATE_OFFSET): New macros
        (i386_gnu_sigtramp_start, i386_gnu_sigtramp_p,
        i386_gnu_sigcontext_addr): New functions
        (i386gnu_init_abi): Register i386_gnu_sigtramp_p,
        i386_gnu_sigcontext_addr, and i386_gnu_sc_reg_offset in the gdbarch
        tdep.
2020-05-30 18:45:45 +00:00
Samuel Thibault 078f2fc9c1 hurd: fix pushing target on inferior creation
This fixes creating inferiors, which was broken since 5b6d1e4fa
('Multi-target support')

gdb/ChangeLog:

	* gnu-nat.c (gnu_nat_target::create_inferior): Move push_target call
	before fork_inferior call. Avoid calling it if target_is_pushed returns
	true.
2020-05-30 18:44:17 +00:00
Samuel Thibault 53dff92cb5 hurd: add gnu_target pointer to fix thread API calls
Fixes

../../gdb/gnu-nat.c:1110:28: error: cannot convert ‘ptid_t’ to ‘process_stratum_target*’
 1110 |        thread_change_ptid (inferior_ptid, ptid);

and others related to 5b6d1e4fa ("Multi-target support")

gdb/ChangeLog:

	* gnu-nat.h (gnu_target): New variable declaration.
	* i386-gnu-nat.c (_initialize_i386gnu_nat): Initialize
	gnu_target.
	* gnu-nat.c (gnu_target): New variable.
	(inf_validate_procs): Pass gnu_target to thread_change_ptid,
	add_thread_silent, and add_thread calls.
	(gnu_nat_target::create_inferior): Pass gnu_target to
	add_thread_silent, thread_change_ptid call.
	(gnu_nat_target::detach): Pass gnu_target to detach_inferior
	call.
2020-05-30 18:43:25 +00:00
Samuel Thibault 5a8b86270b hurd: remove unused variables
Fixes

../../gdb/gnu-nat.c:2554:7: error: unused variable ‘res’ [-Werror=unused-variable]
 2554 |   int res;
../../gdb/gnu-nat.c:2644:20: error: unused variable ‘old_address’ [-Werror=unused-variable]
 2644 |       vm_address_t old_address = region_address;

gdb/ChangeLog:

	* gnu-nat.c (gnu_xfer_auxv): Remove unused `res' variable.
	(gnu_nat_target::find_memory_regions): Remove unused
	`old_address' variable.
2020-05-30 18:42:17 +00:00
Samuel Thibault 366f550a59 hurd: add missing include
Fixes

../../gdb/gnu-nat.c:2522:14: error: ‘target_gdbarch’ was not declared in this scope; did you mean ‘target_detach’?
 2522 |    paddress (target_gdbarch (), memaddr), pulongest (len),

gdb/Changelog:

	* gnu-nat.c: Include "gdbarch.h".
2020-05-30 18:41:30 +00:00
Samuel Thibault f14871bfa4 hurd: make function cast stronger
Fixes

process_reply_S.c:104:23: error: function called through a non-compatible type [-Werror]
  104 |      OutP->RetCode = (*(kern_return_t (*)(mach_port_t, kern_return_t)) S_proc_setmsgport_reply) (In0P->Head.msgh_request_port, In0P-

As the existing comment says, it is in general not safe to drop some
parameters like this, but this is the error handling case, where the
called function does not actually read them, and mig is currently planned
to be used on i386 and x86_64 only, where this is not a problem. As the
existing comment says, fixing it properly would be far from trivial:
we can't just pass 0 for them, as they might not be scalar.

gdb/ChangeLog:

	* reply_mig_hack.awk (Error return): Cast function through
	void *, to bypass compiler function call check.
2020-05-30 18:40:34 +00:00
Samuel Thibault c6887cfb4f hurd: add missing awk script dependency
To make sure the *_reply_S.[ch] files get regenerated whenever we change
the awk script.

gdb/ChangeLog:

	* config/i386/i386gnu.mn (%_reply_S.c): Add dependency on
	$(srcdir)/reply_mig_hack.awk.
2020-05-30 18:40:29 +00:00
Samuel Thibault 6930bffe33 hurd: fix gnu_debug_flag type
Fixes

../../gdb/gnu-nat.c:96:6: error: conflicting declaration ‘bool gnu_debug_flag’
   96 | bool gnu_debug_flag = false;
../../gdb/gnu-nat.c: In function ‘void _initialize_gnu_nat()’:
../../gdb/gnu-nat.c:3511:7: error: cannot

gdb/ChangeLog:

	* gnu-nat.h (gnu_debug_flag): Set type to bool.
2020-05-30 18:40:25 +00:00
Jonny Grant 112c22ed1f gdb: change bug URL to https
gdb/ChangeLog:

	* configure.ac (ACX_BUGURL): change bug URL to https.

Signed-off-by: Jonny Grant <jg@jguk.org>
Change-Id: If8d939e50c830e3e452c3e8f7a7aee06d9c96645
2020-05-30 11:18:53 -04:00
Pedro Alves f68f85b52b replace_typedefs: handle templates in namespaces
GDB currently crashes with infinite recursion, if you set a breakpoint
on a function inside a namespace that includes a template on its fully
qualified name, and, the template's name is also used as typedef in
the global scope that expands to a name that includes the template
name in its qualified name.  For example, from the testcase added by
this commit:

 namespace NS1 { namespace NS2 {

 template<typename T> struct Templ1
 {
   T x;

   Templ1 (object_p) {}
 }} // namespace NS1::NS2

 using Templ1 = NS1::NS2::Templ1<unsigned>;

Setting a breakpoint like so:

(gdb) break NS1::NS2::Templ1<int>::Templ1(NS1::NS2::object*)

Results in infinite recursion, with this cycle (started by
cp_canonicalize_string_full) repeating until the stack is exhausted:

 ...
 #1709 0x000000000055533c in inspect_type (info=0x38ff720, ret_comp=0xd83be10, finder=0x0, data=0x0) at /home/pedro/gdb/mygit/src/gdb/cp-support.c:267
 #1710 0x0000000000555a6f in replace_typedefs (info=0x38ff720, ret_comp=0xd83be10, finder=0x0, data=0x0) at /home/pedro/gdb/mygit/src/gdb/cp-support.c:475
 #1711 0x0000000000555a36 in replace_typedefs (info=0x38ff720, ret_comp=0xd83be70, finder=0x0, data=0x0) at /home/pedro/gdb/mygit/src/gdb/cp-support.c:470
 #1712 0x0000000000555800 in replace_typedefs_qualified_name (info=0x38ff720, ret_comp=0xd839470, finder=0x0, data=0x0) at /home/pedro/gdb/mygit/src/gdb/cp-support.c:389
 #1713 0x0000000000555a8c in replace_typedefs (info=0x38ff720, ret_comp=0xd839470, finder=0x0, data=0x0) at /home/pedro/gdb/mygit/src/gdb/cp-support.c:479
 ...

The demangle component tree for that symbol name looks like this:

d_dump tree for NS1::NS2::Templ1<int>::Templ1(NS1::NS2::object*):
typed name
  qualified name
    name 'NS1'
    qualified name
      name 'NS2'
      qualified name
        template                  <<<<<<<<<<
          name 'Templ1'
          template argument list
            builtin type int
        name 'Templ1'
  function type
    argument list
      pointer
        qualified name
          name 'NS1'
          qualified name
            name 'NS2'
            name 'object'

The recursion starts at replace_typedefs_qualified_name, which doesn't
handle the "template" node, and thus doesn't realize that the template
name actually has the fully qualified name NS1::NS2::Templ1.
replace_typedefs_qualified_name calls into replace_typedefs on the
template node, and that ends up in inspect_type looking up for a
symbol named "Templ1", which finds the global namespace typedef, which
itself expands to NS1::NS2::Templ1.  GDB then tries replacing typedefs
in that newly expanded name, which ends up again in
replace_typedefs_qualified_name, trying to expand a fully qualified
name with "NS::NS2::Templ1<unsigned>" in its name, which results in
recursion whenever the template node is reached.

Fix this by teaching replace_typedefs_qualified_name how to handle
template nodes.  It needs handling in two places: the first spot
handles the symbol above; the second spot handles a symbol like this,
from the new test:

(gdb) b NS1::NS2::grab_it(NS1::NS2::Templ1<int>*)
d_dump tree for NS1::NS2::grab_it(NS1::NS2::Templ1<int>*):
typed name
  qualified name
    name 'NS1'
    qualified name
      name 'NS2'
      name 'grab_it'
  function type
    argument list
      pointer
        qualified name
          name 'NS1'
          qualified name
            name 'NS2'
            template             <<<<<<<<
              name 'Templ1'
              template argument list
                builtin type int

What's different in this case is that the template node appears on the
right child node of a qualified name, instead of on the left child.

The testcase includes a test that checks whether template aliases are
correctly replaced by GDB too.  That fails with GCC due to GCC PR
95437, which makes GDB not know about a typedef for
"NS1::NS2::AliasTempl<int>".  GCC emits a typedef named
"NS1::NS2::AliasTempl" instead, with no template parameter info.  The
test passes with Clang (5.0.2 at least).  See more details here:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95437

gdb/ChangeLog:
2020-05-30  Pedro Alves  <palves@redhat.com>

	* cp-support.c (replace_typedefs_template): New.
	(replace_typedefs_qualified_name): Handle
	DEMANGLE_COMPONENT_TEMPLATE.

gdb/testsuite/ChangeLog:
2020-05-30  Pedro Alves  <palves@redhat.com>

	* gdb.linespec/cp-replace-typedefs-ns-template.cc: New.
	* gdb.linespec/cp-replace-typedefs-ns-template.exp: New.
2020-05-30 14:20:10 +01:00
Simon Marchi 976ca31673 gdb: rename dwarf2_per_objfile variables/fields to per_objfile
While doing the psymtab-sharing patchset, I avoided renaming variables
unnecessarily to avoid adding noise to patches, but I'd like to do it
now.  Basically, we have these dwarf2 per-something structures:

  - dwarf2_per_objfile
  - dwarf2_per_bfd
  - dwarf2_per_cu_data

I named the instances of dwarf2_per_bfd `per_bfd` and most of instances
of dwarf2_per_cu_data are called `per_cu`.  Most pre-existing instances
of dwarf2_per_objfile are named `dwarf2_per_objfile`.  For consistency
with the other type, I'd like to rename them to just `per_objfile`.  The
`dwarf2_` prefix is superfluous, since it's already clear we are in
dwarf2 code.  It also helps reducing the line wrapping by saving 7
precious columns.

gdb/ChangeLog:

	* dwarf2/comp-unit.c, dwarf2/comp-unit.h, dwarf2/index-cache.c,
	dwarf2/index-cache.h, dwarf2/index-write.c,
	dwarf2/index-write.h, dwarf2/line-header.c,
	dwarf2/line-header.h, dwarf2/macro.c, dwarf2/macro.h,
	dwarf2/read.c, dwarf2/read.h: Rename struct dwarf2_per_objfile
	variables and fields from `dwarf2_per_objfile` to just
	`per_objfile` throughout.

Change-Id: I3c45cdcc561265e90df82cbd36b4b4ef2fa73aef
2020-05-29 15:15:10 -04:00
Simon Marchi 989ade0552 gdb: add comment in dwarf_evaluate_loc_desc::push_dwarf_reg_entry_value
Add a comment to clarify why we temporarily override some of the
context's fields, and especially the per_objfile field.  A longer
explanation can be found in this previous commit

    44486dcf19 ("gdb: use caller objfile in dwarf_evaluate_loc_desc::push_dwarf_reg_entry_value")

gdb/ChangeLog:

	* dwarf2/loc.c (class dwarf_evaluate_loc_desc)
	<push_dwarf_reg_entry_value>: Add comment.

Change-Id: I60c6e1062799f729b30a9db78bcb6448783324b4
2020-05-28 15:47:53 -04:00
Kevin Buettner c47bae859a Fix Python3.9 related runtime problems
Python3.9b1 is now available on Rawhide.  GDB w/ Python 3.9 support
can be built using the configure switch -with-python=/usr/bin/python3.9.

Attempting to run gdb/Python3.9 segfaults on startup:

    #0  0x00007ffff7b0582c in PyEval_ReleaseLock () from /lib64/libpython3.9.so.1.0
    #1  0x000000000069ccbf in do_start_initialization ()
	at worktree-test1/gdb/python/python.c:1789
    #2  _initialize_python ()
	at worktree-test1/gdb/python/python.c:1877
    #3  0x00000000007afb0a in initialize_all_files () at init.c:237
    ...

Consulting the the documentation...

https://docs.python.org/3/c-api/init.html

...we find that PyEval_ReleaseLock() has been deprecated since version
3.2.  It recommends using PyEval_SaveThread or PyEval_ReleaseThread()
instead.  In do_start_initialization, in gdb/python/python.c, we
can replace the calls to PyThreadState_Swap() and PyEval_ReleaseLock()
with a single call to PyEval_SaveThread.   (Thanks to Keith Seitz
for working this out.)

With that in place, GDB gets a little bit further.  It still dies
on startup, but the backtrace is different:

    #0  0x00007ffff7b04306 in PyOS_InterruptOccurred ()
       from /lib64/libpython3.9.so.1.0
    #1  0x0000000000576e86 in check_quit_flag ()
	at worktree-test1/gdb/extension.c:776
    #2  0x0000000000576f8a in set_active_ext_lang (now_active=now_active@entry=0x983c00 <extension_language_python>)
	at worktree-test1/gdb/extension.c:705
    #3  0x000000000069d399 in gdbpy_enter::gdbpy_enter (this=0x7fffffffd2d0,
	gdbarch=0x0, language=0x0)
	at worktree-test1/gdb/python/python.c:211
    #4  0x0000000000686e00 in python_new_inferior (inf=0xddeb10)
	at worktree-test1/gdb/python/py-inferior.c:251
    #5  0x00000000005d9fb9 in std::function<void (inferior*)>::operator()(inferior*) const (__args#0=<optimized out>, this=0xccad20)
	at /usr/include/c++/10/bits/std_function.h:617
    #6  gdb::observers::observable<inferior*>::notify (args#0=0xddeb10,
	this=<optimized out>)
	at worktree-test1/gdb/../gdbsupport/observable.h:106
    #7  add_inferior_silent (pid=0)
	at worktree-test1/gdb/inferior.c:113
    #8  0x00000000005dbcb8 in initialize_inferiors ()
	at worktree-test1/gdb/inferior.c:947
    ...

We checked with some Python Developers and were told that we should
acquire the GIL prior to calling any Python C API function.  We
definitely don't have the GIL for calls of PyOS_InterruptOccurred().

I moved class_gdbpy_gil earlier in the file and use it in
gdbpy_check_quit_flag() to acquire (and automatically release) the
GIL.

With those changes in place, I was able to run to a GDB prompt.  But,
when trying to quit, it segfaulted again due to due to some other
problems with gdbpy_check_quit_flag():

    Thread 1 "gdb" received signal SIGSEGV, Segmentation fault.
    0x00007ffff7bbab0c in new_threadstate () from /lib64/libpython3.9.so.1.0
    (top-gdb) bt 8
    #0  0x00007ffff7bbab0c in new_threadstate () from /lib64/libpython3.9.so.1.0
    #1  0x00007ffff7afa5ea in PyGILState_Ensure.cold ()
       from /lib64/libpython3.9.so.1.0
    #2  0x000000000069b58c in gdbpy_gil::gdbpy_gil (this=<synthetic pointer>)
	at worktree-test1/gdb/python/python.c:278
    #3  gdbpy_check_quit_flag (extlang=<optimized out>)
	at worktree-test1/gdb/python/python.c:278
    #4  0x0000000000576e96 in check_quit_flag ()
	at worktree-test1/gdb/extension.c:776
    #5  0x000000000057700c in restore_active_ext_lang (previous=0xe9c050)
	at worktree-test1/gdb/extension.c:729
    #6  0x000000000088913a in do_my_cleanups (
	pmy_chain=0xc31870 <final_cleanup_chain>,
	old_chain=0xae5720 <sentinel_cleanup>)
	at worktree-test1/gdbsupport/cleanups.cc:131
    #7  do_final_cleanups ()
	at worktree-test1/gdbsupport/cleanups.cc:143

In this case, we're trying to call a Python C API function after
Py_Finalize() has been called from finalize_python().  I made
finalize_python set gdb_python_initialized to false and then cause
check_quit_flag() to return early when it's false.

With these changes in place, GDB seems to be working again with
Python3.9b1.  I think it likely that there are other problems lurking.
I wouldn't be surprised to find that there are other calls into Python
where we don't first make sure that we have the GIL.  Further changes
may well be needed.

I see no regressions testing on Rawhide using a GDB built with the
default Python version (3.8.3) versus one built using Python 3.9b1.

I've also tested on Fedora 28, 29, 30, 31, and 32 (all x86_64) using
the default (though updated) system installed versions of Python on
those OSes.  This means that I've tested against Python versions
2.7.15, 2.7.17, 2.7.18, 3.7.7, 3.8.2, and 3.8.3.  In each case GDB
still builds without problem and shows no regressions after applying
this patch.

gdb/ChangeLog:

2020-MM-DD  Kevin Buettner  <kevinb@redhat.com>
	    Keith Seitz  <keiths@redhat.com>

	* python/python.c (do_start_initialization): For Python 3.9 and
	later, call PyEval_SaveThread instead of PyEval_ReleaseLock.
	(class gdbpy_gil): Move to earlier in file.
	(finalize_python): Set gdb_python_initialized.
	(gdbpy_check_quit_flag): Acquire GIL via gdbpy_gil.  Return early
	when not initialized.
2020-05-28 12:46:16 -07:00
Simon Marchi 44486dcf19 gdb: use caller objfile in dwarf_evaluate_loc_desc::push_dwarf_reg_entry_value
In commit

    89b07335fe ("Add dwarf2_per_objfile to dwarf_expr_context and dwarf2_frame_cache")

I replaced the offset property of dwarf_expr_context by a per_objfile
property (since we can get the text offset from the objfile).  The
previous code in dwarf_evaluate_loc_desc::push_dwarf_reg_entry_value
(dwarf_evaluate_loc_desc derives from dwarf_expr_context) did
temporarily override the offset property while evaluating a DWARF
sub-expression.  I speculated that this sub-expression always came from
the same objfile as the outer expression, so I didn't see the need to
temporarily override the per_objfile property in the new code.  A later
commit:

    9f47c70716 ("Remove dwarf2_per_cu_data::objfile ()")

added the following assertion to verify this:

    gdb_assert (this->per_objfile == caller_per_objfile);

It turns out that this is not true.  Call sites can refer to function in
another objfile, and therefore the caller's objfile can be different
from the callee's objfile.  This can happen when the call site DIE in the
DWARF represents a function call done through a function pointer.  The
DIE can't describe statically which function is being called, since it's
variable and not known at compile time.  Instead, it provides an
expression that evaluates to the address of the function being called.
In this case, the called function can very well be in a separate
objfile.

Fix this by overriding the per_objfile property while evaluating the
sub-expression.

This was exposed by the gdb.base/catch-load.exp test failing on openSUSE
Tumbleweed with the glibc debug info installed.  It was also reported to
fail on Fedora.

When I investigated the problem, the particular call site on which we
did hit the assert was coming from this DIE, in
/usr/lib/debug/lib64/libc-2.31.so-2.31-5.1.x86_64.debug on openSUSE
Tumbleweed:

    0x0091aa10:     DW_TAG_GNU_call_site
                      DW_AT_low_pc [DW_FORM_addr]   (0x00000000001398e0)
                      DW_AT_GNU_call_site_target [DW_FORM_exprloc]  (DW_OP_fbreg -272, DW_OP_deref)
                      DW_AT_sibling [DW_FORM_ref4]  (0x0091aa2b)

And for you curious out there, this call site is found in this function:

    0x0091a91d:   DW_TAG_subprogram
                    DW_AT_external [DW_FORM_flag_present]   (true)
                    DW_AT_name [DW_FORM_strp]       ("_dl_catch_exception")
                    DW_AT_decl_file [DW_FORM_data1] ("/usr/src/debug/glibc-2.31-5.1.x86_64/elf/dl-error-skeleton.c")
                    ...

Which is a function that indeed uses a function pointer.

gdb/ChangeLog:

	* dwarf2/loc.c (class dwarf_evaluate_loc_desc)
	<push_dwarf_reg_entry_value>: Remove assert.  Override
	per_objfile with caller_per_objfile.

Change-Id: Ib227d767ce525c10607ab6621a373aaae982c67a
2020-05-28 11:31:00 -04:00
Tom de Vries f030440daa [gdb/symtab] Make gold index workaround more precise
There's a PR gold/15646 - "gold-generated .gdb_index has duplicated
symbols that gdb-generated index doesn't", that causes gold to generate
duplicate symbols in the index.

F.i., a namespace N1 declared in a header file can be listed for two CUs that
include the header file:
...
[759] N1:
        2 [global type]
        3 [global type]
...

This causes a gdb performance problem: f.i. when attempting to set a
breakpoint on a non-existing function N1::misspelled, the symtab for both CUs
will be expanded.

Gdb contains a workaround for this, added in commit 8943b87476 "Work around
gold/15646", that skips duplicate global symbols in the index.

However, the workaround does not check for the symbol kind ("type" in the
example above).

Make the workaround more precise by limiting it to symbol kind "type".

Tested on x86_64-linux, with target boards cc-with-gdb-index and
gold-gdb-index.

gdb/ChangeLog:

2020-05-28  Tom de Vries  <tdevries@suse.de>

	* dwarf2/read.c	(dw2_symtab_iter_next, dw2_expand_marked_cus): Limit
	PR gold/15646 workaround to symbol kind "type".
2020-05-28 17:26:22 +02:00
Tom Tromey f0fbb768c2 Use add_partial_symbol in load_partial_dies
An earlier patch added the add_partial_symbol helper function to
dwarf2/read.c.  However, a couple of calls to add_psymbol_to_list were
left in place.  It turns out that these calls slow down partial symbol
reading, because they still go via the path that tries to needlessly
demangle already-demangled names.

This patch improves the performance of partial symbol reading by
changing this code to use add_partial_symbol instead.

The run previous to this had times of (see the first patch in the
series for an explanation):

gdb    1.64
libxul 1.99
Ada    2.47

This patch improves the times to:

gdb    1.47
libxul 1.89
Ada    2.39

gdb/ChangeLog
2020-05-27  Tom Tromey  <tromey@adacore.com>

	* dwarf2/read.c (load_partial_dies): Use add_partial_symbol.
2020-05-27 11:48:19 -06:00
Tom Tromey af0b2a3e85 Inline abbrev lookup
Profiling showed that calls to abbrev_table::lookup_abbrev were "too
visible".  As these are just forwarding calls to the hash table, this
patch inlines the lookup.  Also, htab_find_with_hash is used, avoiding
another call.

The run previous to this had times of (see the first patch in the
series for an explanation):

gdb    1.69
libxul 2.02
Ada    2.52

This patch improves the times to:

gdb    1.64
libxul 1.99
Ada    2.47

gdb/ChangeLog
2020-05-27  Tom Tromey  <tromey@adacore.com>

	* dwarf2/abbrev.h (struct abbrev_table) <lookup_abbrev>: Inline.
	Use htab_find_with_hash.
	<add_abbrev>: Remove "abbrev_number" parameter.
	* dwarf2/abbrev.c (abbrev_table::add_abbrev): Remove
	"abbrev_number" parameter.  Use htab_find_slot_with_hash.
	(hash_abbrev): Add comment.
	(abbrev_table::lookup_abbrev): Move to header file.
	(abbrev_table::read): Update.
2020-05-27 11:48:19 -06:00
Tom Tromey 7d00ffecd2 Lazily compute partial DIE name
Currently the name of a partial DIE is computed eagerly.  However, the
name is not always needed.  This patch changes partial DIEs to compute
their name lazily, improving performance by avoiding unnecessary name
computations.

The run previous to this had times of (see the first patch in the
series for an explanation):

gdb    1.88
libxul 2.11
Ada    2.60

This patch improves the times to:

gdb    1.69
libxul 2.02
Ada    2.52

gdb/ChangeLog
2020-05-27  Tom Tromey  <tromey@adacore.com>

	* dwarf2/read.c (struct partial_die_info) <name>: Declare new
	method.
	<canonical_name>: New member.
	<raw_name>: Rename from "name".
	(partial_die_info): Initialize canonical_name.
	(scan_partial_symbols): Check raw_name.
	(partial_die_parent_scope, partial_die_full_name)
	(add_partial_symbol, add_partial_subprogram)
	(add_partial_enumeration, load_partial_dies): Use "name" method.
	(partial_die_info::name): New method.
	(partial_die_info::read, guess_partial_die_structure_name)
	(partial_die_info::fixup): Update.
2020-05-27 11:48:19 -06:00
Tom Tromey 697bba1827 Attribute method inlining
This inlines a couple of methods on struct attribute, improving the
performance of DWARF partial symbol reading.  These methods were
discovered as hot spots using callgrind.

For this patch, and for all the patches in this series, I tested gdb's
performance on three programs:

1. gdb itself -- I built gdb and copied it to /tmp, ensuring that the
   same version was used in all tests.

2. The system libxul.so, the main library of Firefox.  I installed the
   separate debuginfo and ensured that gdb read it.

3. A large-ish Ada program that I happen to have.

I ran gdb 10 times like:

	  /bin/time -f %e \
		    ./gdb/gdb --data-directory ./gdb/data-directory -nx \
		    -iex 'set debug-file-directory /usr/lib/debug' \
		    -batch $X

... where $X was the test executable.  Then I computed the mean time.
This was all done with a standard (-g -O2) build of gdb.

The baseline times were

gdb    1.90
libxul 2.12
Ada    2.61

This patch brings the numbers down to

gdb    1.88
libxul 2.11
Ada    2.60

Not a huge change, but still visible in the results.

gdb/ChangeLog
2020-05-27  Tom Tromey  <tromey@adacore.com>

	* dwarf2/attribute.h (struct attribute) <form_is_ref>: Inline.
	<get_ref_die_offset>: Inline.
	<get_ref_die_offset_complaint>: New method.
	* dwarf2/attribute.c (attribute::form_is_ref): Move to header.
	(attribute::get_ref_die_offset_complaint): Rename from
	get_ref_die_offset.  Just issue complaint.
2020-05-27 11:48:18 -06:00
Hannes Domani c17ace4397 Move exit_status_set_internal_vars out of GLOBAL_CURDIR
Fixes these testsuite fails on Windows:
FAIL: gdb.base/shell.exp: shell success exitcode
FAIL: gdb.base/shell.exp: shell fail exitcode

The convenience variables $_shell_exitcode and $_shell_exitsignal don't
depend on the GLOBAL_CURDIR define.

gdb/ChangeLog:

2020-05-27  Hannes Domani  <ssbssa@yahoo.de>

	* cli/cli-cmds.c (shell_escape): Move exit_status_set_internal_vars.
2020-05-27 19:42:53 +02:00
Hannes Domani 96445f0b66 Use errno value of first openp failure
Fixes this testsuite fail on Windows:
FAIL: gdb.base/bad-file.exp: directory

If both tries to open the file fail (without and with ".exe"), use the
errno value of the first try.

gdb/ChangeLog:

2020-05-27  Hannes Domani  <ssbssa@yahoo.de>

	* exec.c (exec_file_attach): Use errno value of first openp failure.
2020-05-27 19:41:07 +02:00
Hannes Domani ac637ec30d Don't close thread handles provided by WaitForDebugEvent
I sometimes encountered a weird breakpoint failure when using start:

(gdb) start
Temporary breakpoint 2 at 0x40162d: file gdb-25911.c, line 4.
Starting program: C:\src\tests\gdb-25911.exe
Warning:
Cannot insert breakpoint 2.
Cannot access memory at address 0x401628

After trying a lot of combinations, I found a way to reproduce it:

(gdb) file gdb-25987.exe
Reading symbols from gdb-25987.exe...
(gdb) start
Temporary breakpoint 1 at 0x401638: file gdb-25987.cpp, line 13.
Starting program: C:\src\tests\gdb-25987.exe

Temporary breakpoint 1, main () at gdb-25987.cpp:13
13      int main() {
(gdb) c
Continuing.

Program received signal SIGSEGV, Segmentation fault.
MyClass::call (this=0x3d20d0) at gdb-25987.cpp:8
8           *(char*)(nullptr) = 1;
(gdb) kill
Kill the program being debugged? (y or n) y
[Inferior 1 (process 1140) killed]
(gdb) file gdb-25911.exe
Load new symbol table from "gdb-25911.exe"? (y or n) y
Reading symbols from gdb-25911.exe...
(gdb) start
Temporary breakpoint 2 at 0x40162d: file gdb-25911.c, line 4.
Starting program: C:\src\tests\gdb-25911.exe
Warning:
Cannot insert breakpoint 2.
Cannot access memory at address 0x401628

Command aborted.

The actual failure was that ReadProcessMemory used a process handle that
was no longer valid.
And the underlying reason was that the windows_thread_info destructor
closes a thread handle that was provided earlier by WaitForDebugEvent.

But since this is not allowed (and it was actually already closed at this
point, and the handle value reused), this closed another still-needed handle.

gdb/ChangeLog:

2020-05-27  Hannes Domani  <ssbssa@yahoo.de>

	* nat/windows-nat.c (windows_thread_info::~windows_thread_info):
	Don't close thread handle.
2020-05-27 19:15:53 +02:00
Tom Tromey 17ee85fc2a Share DWARF partial symtabs
This changes the DWARF reader to share partial symtabs (or indices if
they are available) across objfiles.  This has a few parts.

* If multiple objfiles backed by the same BFD can share partial symtabs
  (see below), a single dwarf2_per_bfd is created.  It is stored in the
  per-bfd `dwarf2_per_bfd_bfd_data_key` registry.  Multiple
  dwarf2_per_objfile objects will point to the same instance.  The
  lifetime of these dwarf2_per_bfd objects is naturally handled.  When
  all the objfiles using the BFD are destroyed, the BFD's refount drops
  to 0, which triggers the removal of the corresponding dwarf2_per_bfd
  object from the registry and its destruction.

* If multiple objfiles backed by the same BFD can't share partial
  symtabs (see below), one dwarf2_per_bfd object is created for each
  objfile.  Each dwarf2_per_objfile will point to their own instance of
  dwarf2_per_bfd.  These instances of dwarf2_per_bfd are kept in a
  per-objfile registry, meaning that when the objfile gets destroyed,
  the dwarf2_per_bfd instance gets destroyed as well.

* objfile::partial_symtabs is changed to be a shared_ptr again.  This
  lets us stash a second reference in dwarf2_per_bfd; if the DWARF
  data is being shared, we can simply copy this value to the new
  objfile.

* Two dwarf2_per_objfile objects backed by the same BFD may share a
  dwarf2_per_bfd instance if:

  * No other symbol reader has found symbols, and
  * No BFD section rqeuires relocation

gdb/ChangeLog:

YYYY-MM-DD  Tom Tromey  <tom@tromey.com>
YYYY-MM-DD  Simon Marchi  <simon.marchi@efficios.com>

	* objfiles.h (struct objfile) <partial_symtabs>: Now a
	shared_ptr.
	* dwarf2/read.h (struct dwarf2_per_objfile) <partial_symtabs>: New
	member.
	* dwarf2/read.c (dwarf2_per_bfd_bfd_data_key,
	dwarf2_per_bfd_objfile_data_key>: New globals.
	(dwarf2_has_info): Use shared dwarf2_per_bfd if possible.
	(dwarf2_get_section_info): Use get_dwarf2_per_objfile.
	(dwarf2_initialize_objfile): Consider cases where per_bfd can be
	shared.
	(dwarf2_build_psymtabs): Set objfile::partial_symtabs and
	short-circuit when sharing.
	(dwarf2_build_psymtabs): Set dwarf2_per_objfile::partial_symtabs.
	(dwarf2_psymtab::expand_psymtab): Use free_cached_comp_units.

Change-Id: I868c64448589102ab8cbb8f06c31a8de50a14004
2020-05-27 11:20:15 -04:00
Simon Marchi 39b16f87f7 Move line_header_hash to dwarf2_per_objfile
The `line_header_hash` field of `struct dwarf2_per_bfd` contains some
`struct line_header` objects.  A `struct line_header` objects contains
some `file_entry` objects.  A `file_entry` object contains a pointer to
the `symtab` object created from it.  The `line_header_hash` is
therefore ultimately objfile-dependent and can't be shared as-is between
objfiles.

Move it from `dwarf2_per_bfd` to `dwarf2_per_objfile`.

gdb/ChangeLog:

	* dwarf2/read.h (struct dwarf2_per_bfd) <line_header_hash>: Move
	to...
	(struct dwarf2_per_objfile) <line_header_hash>: ... here.
	* dwarf2/read.c (handle_DW_AT_stmt_list): Update.

Change-Id: I8d2ee04df4f4847c2db99061fc976c35af98ac71
2020-05-27 11:19:41 -04:00
Simon Marchi fcf23d5b65 Make mapped_debug_names independent of objfile
mapped_debug_names currently has a dwarf2_per_objfile field.  Since we
want it to become objfile-independent, this field must be removed.

This patch removes it, and then arranges for all methods that needed it
to accept a dwarf2_per_objfile parameter.  This trickles down at various
places, like the dw2_debug_names_iterator type.

Ultimately, the objfile only seems to be needed because we might need to
read a string from the string section.  For that, we might need to read
in the section, and if it's a relocatable section, the objfile is needed
in order to do the relocation.  This pattern happens often (that we to
pass an objfile only because a section might be read).  I think it's a
bit ugly, but I don't have a good alternative right now.

gdb/ChangeLog:

	* dwarf2/read.c (struct mapped_index_base) <symbol_name_at,
	build_name_components, find_name_components_bounds>:
	Add per_objfile parameter.
	(struct mapped_index) <symbol_name_at>: Likewise.
	(struct mapped_debug_names): Remove constructor.
	<dwarf2_per_objfile>: Remove field.
	<namei_to_name, symbol_name_at>: Add per_objfile parameter.
	(mapped_index_base::find_name_components_bounds,
	mapped_index_base::build_name_components,
	dw2_expand_symtabs_matching_symbol): Likewise.
	(class mock_mapped_index) <symbol_name_at>: Likewise.
	(check_match): Likewise.
	(check_find_bounds_finds): Likewise.
	(test_mapped_index_find_name_component_bounds): Update.
	(CHECK_MATCH): Update.
	(dw2_expand_symtabs_matching): Update.
	(class dw2_debug_names_iterator) <dw2_debug_names_iterator>: Add
	per_objfile parameter.
	<find_vec_in_debug_names>: Likewise.
	<m_per_objfile>: New field.
	(mapped_debug_names::namei_to_name): Add dwarf2_per_objfile
	parameter.
	(dw2_debug_names_iterator::find_vec_in_debug_names): Likewise.
	(dw2_debug_names_iterator::next): Update.
	(dw2_debug_names_lookup_symbol): Update.
	(dw2_debug_names_expand_symtabs_for_function): Update.
	(dw2_debug_names_map_matching_symbols): Update.
	(dw2_debug_names_expand_symtabs_matching): Update.
	(dwarf2_read_debug_names): Update.

Change-Id: I00ee0d939390d353442675c7d400a261307c57a1
2020-05-27 11:19:41 -04:00
Simon Marchi 7188ed02d2 Replace dwarf2_per_cu_data::cu backlink with per-objfile map
The dwarf2_per_cu_data type is going to become objfile-independent,
while the dwarf2_cu type will stay object-dependent.  This patch removes
the backlink from dwarf2_per_cu_data to dwarf2_cu, in favor of the
dwarf2_per_objfile::m_dwarf2_cus map.  It maps dwarf2_per_cu_data
objects to the corresponding dwarf2_cu objects for this objfile.  If a
CU has been read in in the context of this objfile, then an entry will
be present in the map.

The dwarf2_cu objects that are read in are currently kept in a linked
list rooted in the dwarf2_per_bfd.  Except that the dwarf2_cu objects
are not simply linked together, they are interleaved with their
corresponding dwarf2_per_cu_data objects.  So if we have CUs A and B
read in, the dwarf2_per_bfd::read_in_chain will point to a chain like
this (DPCD == dwarf2_per_cu_data, DC == dwarf2_cu):

 DPCD A -> DC A -> DPCD B -> DC B

Obviously, this can't stay as is, since a same CU can be read in for an
objfile but not read in for another objfile sharing the same BFD, and
the dwarf2_per_cu_data::cu link is removed.   This is all replaced by
the dwarf2_per_objfile::m_dwarf2_cus map.

gdb/ChangeLog:

	* dwarf2/read.h (struct dwarf2_cu): Forward-declare.
	(struct dwarf2_per_bfd) <free_cached_comp_units>: Remove,
	move to dwarf2_per_objfile.
	<read_in_chain>: Remove.
	(struct dwarf2_per_objfile) <get_cu, set_cu, remove_cu,
	remove_all_cus, age_comp_units>: New methods.
	<m_dwarf2_cus>: New member.
	(struct dwarf2_per_cu_data) <cu>: Remove.
	* dwarf2/read.c (struct dwarf2_cu) <read_in_chain>: Remove.
	(age_cached_comp_units, free_one_cached_comp_unit): Remove,
	moved to methods of dwarf2_per_objfile.
	(dwarf2_clear_marks): Remove.
	(dwarf2_queue_item::~dwarf2_queue_item): Update.
	(dwarf2_per_bfd::~dwarf2_per_bfd): Don't free dwarf2_cus.
	(dwarf2_per_bfd::free_cached_comp_units): Remove.
	(dwarf2_per_objfile::remove_all_cus): New.
	(class free_cached_comp_units) <~free_cached_comp_units>:
	Update.
	(load_cu): Update.
	(dw2_do_instantiate_symtab): Adjust.
	(fill_in_sig_entry_from_dwo_entry): Adjust.
	(cutu_reader::init_tu_and_read_dwo_dies): Update.
	(cutu_reader::cutu_reader): Likewise.
	(cutu_reader::keep): Use dwarf2_per_objfile::set_cu.
	(cutu_reader::cutu_reader): Use dwarf2_per_objfile::get_cu.
	(process_psymtab_comp_unit): Use dwarf2_per_objfile::remove_cu
	and dwarf2_per_objfile::age_comp_units.
	(load_partial_comp_unit): Update.
	(maybe_queue_comp_unit): Use dwarf2_per_objfile::get_cu.
	(process_queue): Likewise.
	(find_partial_die): Use dwarf2_per_objfile::get_cu instead of cu
	backlink.
	(dwarf2_read_addr_index): Likewise.
	(follow_die_offset): Likewise.
	(dwarf2_fetch_die_loc_sect_off): Likewise.
	(dwarf2_fetch_constant_bytes): Likewise.
	(dwarf2_fetch_die_type_sect_off): Likewise.
	(follow_die_sig_1): Likewise.
	(load_full_type_unit): Likewise.
	(read_signatured_type): Likewise.
	(dwarf2_cu::dwarf2_cu): Don't set cu field.
	(dwarf2_cu::~dwarf2_cu): Remove.
	(dwarf2_per_objfile::get_cu): New.
	(dwarf2_per_objfile::set_cu): New.
	(age_cached_comp_units): Rename to...
	(dwarf2_per_objfile::age_comp_units): ... this.  Adjust
	to std::unordered_map.
	(free_one_cached_comp_unit): Rename to...
	(dwarf2_per_objfile::remove_cu): ... this.  Adjust
	to std::unordered_map.
	(dwarf2_per_objfile::~dwarf2_per_objfile): New.
	(dwarf2_mark_helper): Use dwarf2_per_objfile::get_cu, expect
	a dwarf2_per_objfile in data.
	(dwarf2_mark): Pass dwarf2_per_objfile in data to htab_traverse.
	(dwarf2_clear_marks): Remove.

Change-Id: Ia33ac71c79b2de4710569008e22a6563a1505cde
2020-05-27 11:19:40 -04:00
Simon Marchi 2e6711003b Pass existing_cu object to cutu_reader
It is possible, seemingly for a special case described in
find_partial_die, for cutu_reader to re-use an existing dwarf2_cu
instead of creating a new one.  This happens when running this test, for
example:

    make check TESTS="gdb.dwarf2/fission-reread.exp"

Right now the, `use_existing_cu` flag tells cutu_reader to use the
dwarf2_cu object at dwarf2_per_cu_data::cu.  However, we'll remove that
field, so we need to find another solution.

This situation arises when some caller up the stack has already created
the dwarf2_cu to read a dwarf2_per_cu_data, but needs to re-read it with
some other parameters.  Therefore, it's possible to just have that
caller pass down the dwarf2_cu object to use as a `existing_cu`
parameter.  If `existing_cu` is NULL, it tells cutu_reader that it needs
to instantiate a new one.

gdb/ChangeLog:

	* dwarf2/read.c (class cutu_reader) <cutu_reader>: Replace
	`int use_existing_cu` parameter with `dwarf2_cu *existing_cu`.
	(init_tu_and_read_dwo_dies): Likewise.
	(cutu_reader::init_tu_and_read_dwo_dies): Likewise.
	(cutu_reader::cutu_reader): Likewise.
	(load_partial_comp_unit): Likewise.
	(process_psymtab_comp_unit): Update.
	(build_type_psymtabs_1): Update.
	(process_skeletonless_type_unit): Update.
	(load_full_comp_unit): Update.
	(find_partial_die): Update.
	(dwarf2_read_addr_index): Update.
	(read_signatured_type): Update.

Change-Id: Id03e3bc3de3cf99d9e4b4080ad83b029c93bf434
2020-05-27 11:19:40 -04:00
Simon Marchi 2e6a9f7959 Add comp_unit_head to dwarf2_per_cu_data
The per_cu_header_read_in function allows obtaining a filled
comp_unit_head object for a given dwarf2_per_cu_data object.  If a
dwarf2_cu object exists for this dwarf2_per_cu_data, then it just
returns a pointer to the comp_unit_head from that dwarf2_cu.  Otherwise,
it reads the header into a temporary buffer provided by the caller, and
returns a pointer to that.

Since the dwarf2_per_cu_data::cu link is going to be removed
(dwarf2_per_cu_data will become objfile-independent while dwarf2_cu
stays objfile-dependent), we cannot rely anymore on returning the header
from the dwarf2_cu object.

The not too complex solution implemented by this patch is to keep a copy
of the header in the dwarf2_per_cu_data object, independent from the
copy in dwarf2_cu.  The new copy is only used in the addr_size,
offset_size and ref_addr_size methods of dwarf2_per_cu_data.

There's nothing intrinsic to the comp_unit_head object that prevents it
to be shared between two dwarf2_cu objects (belonging to different
objfiles) representing the same CU.  In other words, I think we could
eventually get rid of the copy in dwarf2_cu to only keep the one in
dwarf2_per_cu_data.  It is not trivial, however, so I have decided not
to do it for the moment.

gdb/ChangeLog:

	* dwarf2/read.h (struct dwarf2_per_cu_data) <m_header,
	m_header_read_in>: New fields.
	<get_header>: New method.
	* dwarf2/read.c (per_cu_header_read_in): Remove.
	(dwarf2_per_cu_data::get_header): New.
	(dwarf2_per_cu_data::addr_size): Update.
	(dwarf2_per_cu_data::offset_size): Update.
	(dwarf2_per_cu_data::ref_addr_size): Update.

Change-Id: Id7541fca7562843eba110ece21c4df38d45fca23
2020-05-27 11:19:40 -04:00
Simon Marchi 1b555f1747 Make load_cu return the loaded dwarf2_cu
In a subsequent patch, the dwarf2_per_cu_data::cu link will be removed.
dwarf2_cu objects will instead need to be looked up from a per-objfile
map, using the dwarf2_per_cu_data object as the key.

To make it easier for some callers, this patch makes load_cu return the
dwarf2_cu it creates.  If the caller needs to use the created dwarf2_cu,
it will have it available right away, rather than having to do a map
lookup.

At the same time, this allows changing queue_and_load_all_dwo_tus to
take a dwarf2_cu instead of a dwarf2_per_cu_data.

gdb/ChangeLog:

	* dwarf2/read.c (load_cu): Return dwarf2_cu.
	(dw2_do_instantiate_symtab): Update.
	(queue_and_load_all_dwo_tus): Change parameter from
	dwarf2_per_cu_data to dwarf2_cu.
	(dwarf2_fetch_die_loc_sect_off): Update.
	(dwarf2_fetch_constant_bytes): Update.
	(dwarf2_fetch_die_type_sect_off): Update.

Change-Id: I8a04c5d1b8cc661b8203f97999258ba8e04e1765
2020-05-27 11:19:40 -04:00
Simon Marchi 8fc0b21da6 Pass dwarf2_cu to process_full_{comp,type}_unit
These two functions work on a dwarf2_cu.  It is currently obtained from
the per_cu->cu link, which we want to remove.  Make them accept the
dwarf2_cu directly as a parameter.  This moves the per_cu->cu references
one level up, but that one will be removed too in a subsequent patch.

gdb/ChangeLog:

	* dwarf2/read.c (process_full_comp_unit,
	process_full_type_unit): Remove per_cu, per_objfile paramters.
	Add dwarf2_cu parameter.
	(process_queue): Update.

Change-Id: I1027d36986073ac991e198e06f9d51341dc19c6e
2020-05-27 11:19:40 -04:00
Simon Marchi 168c9250f2 Pass dwarf2_per_bfd instead of dwarf2_per_objfile to some index-related functions
All these functions actually only need to receive a dwarf2_per_bfd, pass
that instead of dwarf2_per_objfile.

gdb/ChangeLog:

	* dwarf2/read.c (create_cu_from_index_list): Replace
	dwarf2_per_objfile parameter with dwarf2_per_bfd.
	(create_cus_from_index_list): Likewise.
	(create_cus_from_index): Likewise.
	(create_signatured_type_table_from_index): Likewise.
	(create_cus_from_debug_names_list): Likewise.
	(create_cus_from_debug_names): Likewise.
	(dwarf2_read_gdb_index): Update.
	(dwarf2_read_debug_names): Update.

Change-Id: I8cd7dc04bf815723a48745e7e9b283663dccc1ac
2020-05-27 11:19:40 -04:00
Tom Tromey e286671bf9 Move signatured_type::type to unshareable object
signatured_type has a link to the "struct type".  However, types are
inherently objfile-specific, so once sharing is implemented, this will
be incorrect.

This patch moves the type to a new map in the DWARF unshareable
object.

gdb/ChangeLog:

YYYY-MM-DD  Tom Tromey  <tom@tromey.com>
YYYY-MM-DD  Simon Marchi  <simon.marchi@efficios.com>

	* dwarf2/read.h (struct dwarf2_per_objfile)
	<get_type_for_signatured_type, set_type_for_signatured_type>:
	New methods.
	<m_type_map>: New member.
	(struct signatured_type) <type>: Remove.
	* dwarf2/read.c
	(dwarf2_per_objfile::get_type_for_signatured_type,
	dwarf2_per_objfile::set_type_for_signatured_type): New.
	(get_signatured_type): Use new methods.

Change-Id: I765ae3c43fae1064f51ced352167a57638609f02
2020-05-27 11:19:35 -04:00
Tom Tromey 8adb84872b Split type_unit_group
type_unit_group has links to the compunit_symtab and other symtabs.
However, once this object is shared across objfiles, this will no
longer be ok.

This patch introduces a new type_unit_group_unshareable and arranges to
store a map from type unit groups to type_unit_group_unshareable objects
in dwarf2_per_objfile.

gdb/ChangeLog:

YYYY-MM-DD  Tom Tromey  <tom@tromey.com>
YYYY-MM-DD  Simon Marchi  <simon.marchi@efficios.com>

	* dwarf2/read.h (struct type_unit_group_unshareable): New.
	(struct dwarf2_per_objfile) <type_units>: New member.
	<get_type_unit_group_unshareable>: New method.
	* dwarf2/read.c (struct type_unit_group) <compunit_symtab,
	num_symtabs, symtabs>: Remove; move to
	type_unit_group_unshareable.
	(dwarf2_per_objfile::get_type_unit_group_unshareable): New.
	(process_full_type_unit, dwarf2_cu::setup_type_unit_groups)
	(dwarf2_cu::setup_type_unit_groups): Use type_unit_group_unshareable.

Change-Id: I1fec2fab59e0ec40fee3614fc821172a469c0e41
2020-05-27 11:19:09 -04:00
Simon Marchi 127bbf4b50 Remove dwarf2_per_cu_data::dwarf2_per_objfile
Nothing references this field anymore, remove it.

gdb/ChangeLog:

	* dwarf2/read.h (struct dwarf2_per_cu_data):
	<dwarf2_per_objfile>: Remove.
	* dwarf2/read.c (create_cu_from_index_list): Don't assign
	dwarf2_per_objfile.
	(create_signatured_type_table_from_index): Likewise.
	(create_signatured_type_table_from_debug_names): Likewise.
	(create_debug_type_hash_table): Likewise.
	(fill_in_sig_entry_from_dwo_entry): Likewise.
	(create_type_unit_group): Likewise.
	(read_comp_units_from_section): Likewise.
	(create_cus_hash_table): Likewise.

Change-Id: Icf0b657a6beec953fe17cbe0fb2ae2c6e744d3ed
2020-05-27 11:15:58 -04:00
Simon Marchi f6e649ddaf Remove leftover references to dwarf2_per_cu_data::dwarf2_per_objfile
This patch removes the remaining references to that field in obvious
ways (the same object is already available some other way in these
contexts).

gdb/ChangeLog:

	* dwarf2/read.c (process_psymtab_comp_unit): Remove reference to
	dwarf2_per_cu_data::dwarf2_per_objfile.
	(compute_compunit_symtab_includes): Likewise.
	(dwarf2_cu::start_symtab): Likewise.

Change-Id: I965700fa793d8457711a2d6ae448aaefd779eb96
2020-05-27 11:15:58 -04:00
Simon Marchi aa66c37944 Add dwarf2_per_objfile parameter to get_die_type_at_offset
This allows removing some dwarf2_per_cu_data::dwarf2_per_objfile
references.

gdb/ChangeLog:

	* dwarf2/read.h (dwarf2_get_die_type): Add dwarf2_per_objfile
	parameter.
	* dwarf2/read.c (get_die_type_at_offset): Likewise.
	(read_namespace_alias): Update.
	(lookup_die_type): Update.
	(dwarf2_get_die_type): Add dwarf2_per_objfile parameter.
	* dwarf2/loc.c (class dwarf_evaluate_loc_desc) <get_base_type>:
	Update.
	(disassemble_dwarf_expression): Update.

Change-Id: Ibaf5b684cb0a8eb8f0b23e62bd0283c295410aa5
2020-05-27 11:15:58 -04:00
Simon Marchi 120ce1b5b2 Add dwarf2_per_objfile parameter to free_one_cached_comp_unit
This allows removing some references to
dwarf2_per_cu_data::dwarf2_per_objfile.

gdb/ChangeLog:

	* dwarf2/read.h (struct dwarf2_queue_item): Add
	dwarf2_per_objfile parameter, assign new parameter.
	<per_objfile>: New field.
	* dwarf2/read.c (free_one_cached_comp_unit): Add
	dwarf2_per_objfile parameter.
	(queue_comp_unit): Likewise.
	(dw2_do_instantiate_symtab): Update.
	(process_psymtab_comp_unit): Update.
	(maybe_queue_comp_unit): Add dwarf2_per_objfile parameter.
	(process_imported_unit_die): Update.
	(queue_and_load_dwo_tu): Update.
	(follow_die_offset): Update.
	(follow_die_sig_1): Update.

Change-Id: Ibb4a4ea28eeac5ebcbf73c0d2a13f9391e15c235
2020-05-27 11:15:57 -04:00
Simon Marchi 9f47c70716 Remove dwarf2_per_cu_data::objfile ()
Since dwarf2_per_cu_data objects are going to become
objfile-independent, the backlink from dwarf2_per_cu_data to one
particular objfile must be removed.  Instead, users of
dwarf2_per_cu_data that need an objfile must know from somewhere else in
the context of which objfile they are using this CU.

This also helps remove a dwarf2_per_cu_data::dwarf2_per_objfile
reference (from where the objfile was obtained).

Note that the dwarf2_per_cu_data::objfile method has a special case to
make sure to return the main objfile, if the objfile associated to the
dwarf2_per_cu_data is a separate debug objfile.  I don't really know if
this is necessary: I ignored that, and didn't see any regression when
testing with the various Dejagnu boards with separate debug info, so I
presume it wasn't needed.  If it turns out this was needed, then we can
have a helper method on the objfile type for that.

gdb/ChangeLog:

	* dwarf2/read.h (struct dwarf2_per_cu_data) <objfile>: Remove.
	* dwarf2/read.c (dwarf2_compute_name): Pass per_objfile down.
	(read_call_site_scope): Assign per_objfile.
	(dwarf2_per_cu_data::objfile): Remove.
	* gdbtypes.h (struct call_site) <per_objfile>: New member.
	* dwarf2/loc.h (dwarf2_evaluate_loc_desc): Add
	dwarf2_per_objfile parameter.
	* dwarf2/loc.c (dwarf2_evaluate_loc_desc_full): Add
	dwarf2_per_objfile parameter.
	(dwarf_expr_reg_to_entry_parameter): Add output
	dwarf2_per_objfile parameter.
	(locexpr_get_frame_base): Update.
	(class dwarf_evaluate_loc_desc) <get_tls_address>: Update.
	<push_dwarf_reg_entry_value>: Update.
	<call_site_to_target_addr>: Update.
	(dwarf_entry_parameter_to_value): Add dwarf2_per_objfile
	parameter.
	(value_of_dwarf_reg_entry): Update.
	(rw_pieced_value): Update.
	(indirect_synthetic_pointer): Update.
	(dwarf2_evaluate_property): Update.
	(dwarf2_loc_desc_get_symbol_read_needs): Add dwarf2_per_objfile
	parameter.
	(locexpr_read_variable): Update.
	(locexpr_get_symbol_read_needs): Update.
	(loclist_read_variable): Update.

Change-Id: Idb40d1a94995af305054d463967bb6ce11a08f25
2020-05-27 11:15:57 -04:00
Simon Marchi 14095eb326 Add dwarf2_per_objfile parameters to dwarf2_fetch_* functions
This allows removing dwarf2_per_cu_data references.

gdb/ChangeLog:

	* dwarf2/read.h (dwarf2_fetch_die_loc_sect_off,
	dwarf2_fetch_die_loc_cu_off, dwarf2_fetch_constant_bytes,
	dwarf2_fetch_die_type_sect_off): Add dwarf2_per_objfile
	parameter.
	* dwarf2/read.c (dwarf2_fetch_die_loc_sect_off,
	dwarf2_fetch_die_loc_cu_off, dwarf2_fetch_constant_bytes,
	dwarf2_fetch_die_type_sect_off): Add dwarf2_per_objfile
	parameter.
	* dwarf2/loc.c (indirect_synthetic_pointer, per_cu_dwarf_call,
	sect_variable_value): Add dwarf2_per_objfile parameter.
	(class dwarf_evaluate_loc_desc) <dwarf_call,
	dwarf_variable_value>: Update.
	(fetch_const_value_from_synthetic_pointer): Add
	dwarf2_per_objfile parameter.
	(fetch_const_value_from_synthetic_pointer): Update.
	(coerced_pieced_ref): Update.
	(class symbol_needs_eval_context) <dwarf_call,
	dwarf_variable_value>: Update.
	(dwarf2_compile_expr_to_ax): Update.

Change-Id: I07cf1806380633d0572304cea049a1fa5e9ea67f
2020-05-27 11:15:57 -04:00
Simon Marchi 3c3cd3d4d7 Add dwarf2_per_objfile parameter to allocate_piece_closure
This allows removing a dwarf2_per_cu_data::dwarf2_per_objfile reference.

gdb/ChangeLog:

	* dwarf2/loc.c (allocate_piece_closure): Add dwarf2_per_objfile
	parameter.
	(dwarf2_evaluate_loc_desc_full): Update.

Change-Id: Ic4a694a3fc763360a131ee4e3aaf5a5b4735c813
2020-05-27 11:15:57 -04:00
Simon Marchi 82ca3f5189 Add dwarf2_per_objfile parameter to dwarf2_read_addr_index
Pass it all the way from the symbol batons.  This allows removing a
dwarf2_per_cu_data::dwarf2_per_objfile reference.

gdb/ChangeLog:

	* dwarf2/read.h (dwarf2_read_addr_index): Add dwarf2_per_objfile
	parameter.
	* dwarf2/read.c (dwarf2_read_addr_index): Likewise.
	* dwarf2/loc.c (decode_debug_loclists_addresses): Add
	dwarf2_per_objfile parameter.
	(decode_debug_loc_dwo_addresses): Likewise.
	(dwarf2_find_location_expression): Update.
	(class dwarf_evaluate_loc_desc) <get_addr_index>: Update.
	(locexpr_describe_location_piece): Add dwarf2_per_objfile
	parameter.
	(disassemble_dwarf_expression): Add dwarf2_per_objfile
	parameter.
	(locexpr_describe_location_1): Likewise.
	(locexpr_describe_location): Update.

Change-Id: I8414755e41a87c92f96e408524cc7aaccf086cda
2020-05-27 11:15:57 -04:00
Simon Marchi 4b167ea1a0 Remove dwarf2_per_cu_data::text_offset
This method simply returns the text offset of the objfile associated to
the dwarf2_per_cu_data object.  Since dwarf2_per_cu_data objects are
going to become objfile-independent, we can't keep this method.  This
patch removes it.

Existing callers need to figure out the in the context of which objfile
this is being used, and call text_offset on it.  Typically, this comes
from a symbol baton, where we store the corresponding
dwarf2_per_objfile.

gdb/ChangeLog:

	* dwarf2/read.h (struct dwarf2_per_cu_data) <text_offset>:
	Remove.
	* dwarf2/read.c (dwarf2_per_cu_data::text_offset): Remove.
	* dwarf2/loc.c (dwarf2_find_location_expression): Update.
	(dwarf2_compile_property_to_c): Update.
	(dwarf2_compile_expr_to_ax): Add dwarf2_per_objfile parameter,
	use text offset from objfile.
	(locexpr_tracepoint_var_ref): Update.
	(locexpr_generate_c_location): Update.
	(loclist_describe_location): Update.
	(loclist_tracepoint_var_ref): Update.
	* dwarf2/compile.h (compile_dwarf_bounds_to_c): Add
	dwarf2_per_objfile parameter.
	* dwarf2/loc2c.c (do_compile_dwarf_expr_to_c): Likewise,
	use text offset from objfile.
	(compile_dwarf_expr_to_c): Add dwarf2_per_objfile parameter.

Change-Id: I56b01ba294733362a3562426a96d48ae051a776f
2020-05-27 11:15:57 -04:00
Simon Marchi 89b07335fe Add dwarf2_per_objfile to dwarf_expr_context and dwarf2_frame_cache
Evaluating DWARF expressions (such as location expressions) requires
knowing about the current objfile.  For example, it may call functions
like dwarf2_fetch_die_loc_sect_off, which currently obtain the
dwarf2_per_objfile object it needs from the dwarf2_per_cu_data object.
However, since we are going to remove this
dwarf2_per_cu_data::dwarf2_per_objfile link, these functions will need
to obtain the current dwarf2_per_objfile by parmeter.

If we go up the stack, we see that the DWARF expression contexts
(dwarf_expr_context and the classes that derive from it) need to store
the dwarf2_per_objfile, to be able to pass it to those functions that
will need it.

This patch adds a constructor to all these dwarf_expr_context variants,
accepting a dwarf2_per_objfile parameter.  This dwarf2_per_objfile
generally comes from a symbol baton created earlier.

For frame-related expressions, the dwarf2_per_objfile object must be
passed through the dwarf2_frame_cache object.  This lead to the
dwarf2_frame_find_fde function returning (by parameter) a
dwarf2_per_objfile object.  I then realized that this made the existing
"out_offset" parameter redundant.  This offset is
`objfile->text_section_offset ()`, so it can be recomputed from the
dwarf2_per_objfile object at any time.  I therefore opted to remove this
output parameter, as well as the offset field of dwarf2_frame_cache.

*Note*, there's one spot I'm particularly unsure about.  In
dwarf_evaluate_loc_desc::push_dwarf_reg_entry_value, we would save and
overwrite the offset value in the context, along with a bunch of other
state.  This is because we might be about to evaluate something in a
different CU that the current one.  If the two CUs are in the same
objfile, then the text_offset is the same, as it's a property of the
objfile.  However, if the two CUs are possibly in different objfiles,
then it means the text_offsets are different.  It would also mean we
would need to save and restore the dwarf2_per_objfile in the context.
Is that even possible?

gdb/ChangeLog:

	* dwarf2/expr.h (struct dwarf_expr_context)
	<dwarf_expr_context>: Add dwarf2_per_objfile parameter.
	<offset>: Remove.
	<per_objfile>: New member.
	* dwarf2/expr.c (dwarf_expr_context::dwarf_expr_context): Add
	dwarf2_per_objfile parameter.  Don't set offset, set
	per_objfile.
	(dwarf_expr_context::execute_stack_op): Use offset from objfile.
	* dwarf2/frame.c (dwarf2_frame_find_fde): Return (by parameter)
	a dwarf2_per_objfile object instead of an offset.
	(class dwarf_expr_executor) <dwarf_expr_executor>: Add
	constructor.
	(execute_stack_op): Add dwarf2_per_objfile parameter, pass it
	to dwarf2_expr_executor constructor.  Don't set offset.
	(dwarf2_fetch_cfa_info): Update.
	(struct dwarf2_frame_cache) <text_offset>: Remove.
	<per_objfile>: New field.
	(dwarf2_frame_cache): Update.
	(dwarf2_frame_prev_register): Update.
	* dwarf2/loc.c (class dwarf_evaluate_loc_desc)
	<dwarf_evaluate_loc_desc>: Add constructor.
	(dwarf2_evaluate_loc_desc_full): Update.
	(dwarf2_locexpr_baton_eval): Update.
	(class symbol_needs_eval_context) <symbol_needs_eval_context>:
	Add constructor.
	(dwarf2_loc_desc_get_symbol_read_needs): Update.

Change-Id: I14df060669cc36ad04759f1708c6d7b1fda77727
2020-05-27 11:15:56 -04:00
Simon Marchi 293e7e5114 Move int type methods out of dwarf2_per_cu_data
These methods rely on the current objfile to create types based on it.
Since dwarf2_per_cu_data is to become objfile-independent, these methods
need to mvoe.

int_type can be in dwarf2_per_objfile, as it only requires knowing about
the objfile.

addr_sized_int_type and addr_type also need to know about the DWARF
address type size, which is CU-specific.  The dwarf2_cu objects seems
like a good place for it, as it knows both about the current objfile and
the current CU.

gdb/ChangeLog:

	* dwarf2/read.h (struct dwarf2_per_cu_data) <addr_type,
	addr_sized_int_type>: Move to dwarf2_cu.
	<int_type>: Move to dwarf2_per_objfile.
	(struct dwarf2_per_objfile) <int_type>: Move here.
	* dwarf2/read.c (struct dwarf2_cu) <addr_type,
	addr_sized_int_type>: Move here.
	(read_func_scope): Update.
	(read_array_type): Update.
	(read_tag_string_type): Update.
	(attr_to_dynamic_prop): Update.
	(dwarf2_per_cu_data::int_type): Rename to...
	(dwarf2_per_objfile::int_type): ... this.
	(dwarf2_per_cu_data::addr_sized_int_type): Rename to...
	(dwarf2_cu::addr_sized_int_type): ... this.
	(read_subrange_type): Update.
	(dwarf2_per_cu_data::addr_type): Rename to...
	(dwarf2_cu::addr_type): ... this.
	(set_die_type): Update.

Change-Id: Ic4708ef99d43a8d99325ff91dee59b2eb706cb8f
2020-05-27 11:15:56 -04:00
Simon Marchi 64874a4030 Remove reference to dwarf2_per_cu_data::dwarf2_per_objfile in queue_and_load_all_dwo_tus
In this context, we know that per_cu->cu will be set, as there is this
assertion:

    gdb_assert (per_cu->cu != NULL)

So in order to remove the dwarf2_per_cu_data::dwarf2_per_objfile
reference in queue_and_load_all_dwo_tus, we can go through per_cu->cu.
This adds a reference to dwarf2_per_cu_data::cu, but it will get removed
eventually, in a subsequent patch.

gdb/ChangeLog:

	* dwarf2/read.c (queue_and_load_all_dwo_tus): Access per_objfile
	data through per_cu->cu.

Change-Id: Id4662828ac3c5bc93fe221df3c9bd9a36a8427ad
2020-05-27 11:15:56 -04:00
Simon Marchi 4ab09049d6 Pass dwarf2_cu objects to dwo-related functions, instead of dwarf2_per_cu_data
This allows removing references to the
dwarf2_per_cu_data::dwarf2_per_objfile field.

I am not too sure of the code flow here, but ultimately
open_and_init_dwo_file calls create_cus_hash_table, and passes it
per_cu->cu.  create_cus_hash_table requires a dwarf2_cu to pass to
cutu_reader, as the "parent_cu".

The dwarf2_per_cu_data::cu link is only set when in a certain context.
It's not easy to convince myself in which situations it's safe to use
it.  Instead, if a function is going to use a dwarf2_cu, I think it's
simpler if it takes that object directly.  If it needs access to the
corresponding dwarf2_per_cu_data object, then it can used the
dwarf2_cu::per_cu field, which we know is always set.

This patch adds some references to dwarf2_per_cu_data::cu in the
cutu_reader context.  In this context, we know this field will be set,
as it's cutu_reader that is responsible for instantiating the dwarf2_cu
and assigning the field.

gdb/ChangeLog:

	* dwarf2/read.c (lookup_dwo_comp_unit): Change
	dwarf2_per_cu_data parameter fo dwarf2_cu.
	(lookup_dwo_type_unit): Likewise.
	(read_cutu_die_from_dwo): Likewise.
	(lookup_dwo_unit): Likewise.
	(open_and_init_dwo_file): Likewise.
	(lookup_dwo_cutu): Likewise.
	(lookup_dwo_comp_unit): Likewise.
	(lookup_dwo_type_unit): Likewise.
	(cutu_reader::init_tu_and_read_dwo_dies): Update.
	(cutu_reader::cutu_reader): Update.

Change-Id: I0406a715b0797963bde2bd86237f159cbece5839
2020-05-27 11:15:56 -04:00
Simon Marchi 47b14e8676 Add dwarf2_per_objfile parameter to process_full_{comp,type}_unit
This allows removing the dwarf2_per_cu_data::dwarf2_per_objfile
references in them.

gdb/ChangeLog:

	* dwarf2/read.c (process_full_comp_unit): Add dwarf2_per_objfile
	parameter.
	(process_full_type_unit): Likewise.
	(process_queue): Update.

Change-Id: Ie68baa8cc4bf1f81cc67d4ad13a59881b4c3feb6
2020-05-27 11:15:56 -04:00
Simon Marchi 43182c09c6 Add dwarf2_per_objfile parameter to recursively_compute_inclusions
This allows removing dwarf2_per_cu_data::dwarf2_per_objfile references
in recursively_compute_inclusions and compute_compunit_symtab_includes.

gdb/ChangeLog:

	* dwarf2/read.c (recursively_compute_inclusions): Add
	dwarf2_per_objfile parameter.
	(compute_compunit_symtab_includes): Likewise.
	(process_cu_includes): Update.

Change-Id: I1ee7f8dfc07b39763985e6764e8ce04dcc943ec5
2020-05-27 11:15:55 -04:00
Simon Marchi 7aa104c423 Add dwarf2_per_objfile parameter to create_partial_symtab
This allows removing a dwarf2_per_cu_data::dwarf2_per_objfile reference.

gdb/ChangeLog:

	* dwarf2/read.c (create_partial_symtab): Add dwarf2_per_objfile
	parameter.
	(create_type_unit_group): Update.
	(process_psymtab_comp_unit_reader): Update.
	(build_type_psymtabs_reader): Update.

Change-Id: I72e3a8fce8022943ce6992fb623e05636cd0e3a5
2020-05-27 11:15:55 -04:00
Simon Marchi e3beb21d35 Remove dwarf2_per_cu_data::dwarf2_per_objfile reference in cutu_reader::keep
Here, it should be safe to use dwarf2_per_cu_data->cu->per_objfile, as
we know that dwarf2_per_cu_data->cu will be set at this point.

Note that this adds a reference to dwarf2_per_cu_data::cu, which we'll
want to remove later, but the current focus is to remove references to
dwarf2_per_cu_data::dwarf2_per_objfile.  We'll deal with that in a
subsequent patch.

gdb/ChangeLog:

	* dwarf2/read.c (cutu_reader::keep): Access dwarf2_per_objfile
	object through m_this_cu->cu.

Change-Id: I8dc26d4db021e0b9e9306eb033965b2704bba87c
2020-05-27 11:15:55 -04:00
Simon Marchi d460f6600a Make queue_and_load_dwo_tu receive a dwarf2_cu
queue_and_load_dwo_tu, used as a callback for htab_traverse_noresize,
currently receives a dwarf2_per_cu_data as its `info` user data.  It
accesses the current dwarf2_cu object through the dwarf2_per_cu_data::cu field.
This field will be removed, because the dwarf2_per_cu_data will become
objfile-independent, while dwarf_cu will remain objfile-dependent.

To remove references to this field, change queue_and_load_dwo_tu so
that it expects to receive a pointer to the dwarf2_cu as its info
parameter.

A reference to dwarf2_per_cu_data::cu needs to be added, but it will get
removed in a subsequent patch, when this function gets re-worked.

I kept this as a separate patch, because since there's no strong typing
here, it's easy to miss something.

gdb/ChangeLog:

	* dwarf2/read.c (queue_and_load_dwo_tu): Expect a dwarf2_cu as
	the info parameter.
	(queue_and_load_all_dwo_tus): Pass per_cu->cu.

Change-Id: I3db2a780f0e2157d52ce6939f478558ffe20abcf
2020-05-27 11:15:55 -04:00
Simon Marchi ab43249077 Add dwarf2_per_objfile parameter to cutu_reader's constructors
The cutu_reader type is used for reading the CU represented by the
passed dwarf2_per_cu_data object.  This reading is done in the context
of a given obfile, which is currently the one associated to the passed
dwarf2_per_cu_data object.  Since the dwarf2_per_cu_data type will
become objfile-independent, we will need to pass the objfile separately.

This patch therefore adds a dwarf2_per_objfile parameter to the
cutu_reader constructors, as well as to their callers, up until the
point where we can get the dwarf2_per_objfile object from somewhere
else.  In the end, this allows removing the reference to
dwarf2_per_cu_data::dwarf2_per_objfile in cutu_reader::cutu_reader.

A few dwarf2_per_cu_data::dwarf2_per_objfile references are added (e.g.
in dwarf2_fetch_die_type_sect_off).  This is temporary, this will be
removed once these functions will get re-worked in subsequent patches.

gdb/ChangeLog:

	* dwarf2/read.c (class cutu_reader) <cutu_reader>: Add
	per_objfile parameter.
	(load_full_type_unit): Add per_objfile parameter.
	(read_signatured_type): Likewise.
	(load_full_comp_unit): Likewise.
	(load_cu): Likewise.
	(dw2_do_instantiate_symtab): Likewise.
	(dw2_get_file_names): Likewise.
	(dw2_map_symtabs_matching_filename): Update.
	(dw_expand_symtabs_matching_file_matcher): Update.
	(dw2_map_symbol_filenames): Update.
	(process_psymtab_comp_unit): Add per_objfile parameter.
	(build_type_psymtabs_1): Update.
	(process_skeletonless_type_unit): Update.
	(dwarf2_build_psymtabs_hard): Update.
	(load_partial_comp_unit): Add per_objfile parameter.
	(scan_partial_symbols): Update.
	(load_full_comp_unit): Add per_objfile parameter.
	(process_imported_unit_die): Update.
	(create_cus_hash_table): Update.
	(find_partial_die): Update.
	(dwarf2_read_addr_index): Update.
	(follow_die_offset): Update.
	(dwarf2_fetch_die_loc_sect_off): Update.
	(dwarf2_fetch_constant_bytes): Update.
	(dwarf2_fetch_die_type_sect_off): Update.
	(follow_die_sig_1): Update.
	(load_full_type_unit): Add per_objfile parameter.
	(read_signatured_type): Likewise.

Change-Id: Ibd7bbc443df8b9b8b6f96ff18e93a60ee721b85f
2020-05-27 11:15:55 -04:00
Simon Marchi 313bad1bc6 Use bfd_get_filename instead of objfile_name in lookup_dwo_unit
There should be no functional difference, as objfile_name defers to
bfd_get_filename if objfile::obfd is non-NULL, which should be the case
here.  This allows to remove a reference to
dwarf2_per_cu_data::dwarf2_per_objfile.

gdb/ChangeLog:

	* dwarf2/read.c (lookup_dwo_unit): Use bfd_get_filename instead
	of objfile_name.

Change-Id: I1e1c1870820aec23701edc9c3994612da5781c23
2020-05-27 11:15:55 -04:00
Simon Marchi c3699833af Make dwarf2_get_dwz_file take a dwarf2_per_bfd
This allows removing a per_bfd->dwarf2_per_objfile reference in
get_abbrev_section_for_cu.

This requires saving the bfd in dwarf2_per_bfd.  The constructor of
dwarf2_per_bfd already accepts the bfd, so it's just a matter of saving
it in a field.

I replaced uses of objfile_name with bfd_get_filename, which should be
equivalent in this case.

gdb/ChangeLog:

	* dwarf2/read.h (struct dwarf2_per_bfd) <obfd>: New member.
	(dwarf2_get_dwz_file): Replace parameter with dwarf2_per_bfd.
	* dwarf2/read.c (dwarf2_per_bfd::dwarf2_per_bfd): Assign obfd
	field.
	(dwarf2_get_dwz_file): Replace parameter with dwarf2_per_bfd.
	(create_cus_from_index): Update.
	(dwarf2_read_gdb_index): Update.
	(create_cus_from_debug_names): Update.
	(dwarf2_read_debug_names): Update.
	(get_abbrev_section_for_cu): Update.
	(create_all_comp_units): Update.
	(read_attribute_value): Update.
	(get_debug_line_section): Update.
	* dwarf2/index-cache.c (index_cache::store): Update.
	* dwarf2/index-write.c (save_gdb_index_command): Update.
	* dwarf2/macro.c (dwarf_decode_macro_bytes): Update.

Change-Id: Ifb23f55dda93c499aae57b6a9aff9c6ff9d2f45f
2020-05-27 11:15:54 -04:00
Simon Marchi 1859c670e9 Add dwarf2_per_bfd field to dwarf2_per_cu_data
Some code using dwarf2_per_cu_data objects accesses the corresponding
dwarf2_per_bfd using the following pattern:

    per_cu->dwarf2_per_objfile->per_bfd

Since dwarf2_per_cu_data objects are going to become
objfile-independent, the dwarf2_per_objfile link must go.  To replace
it, add a dwarf2_per_cu_data->per_bfd link.  It makes sense to have it
there because the dwarf2_per_cu_data objects belong to the
dwarf2_per_bfd, so this is essentially just a backlink to their owner.

gdb/ChangeLog:

	* dwarf2/read.h (struct dwarf2_per_cu_data) <per_bfd>: New
	member.
	* dwarf2/read.c (dwarf2_per_bfd::allocate_per_cu): Initialize
	dwarf2_per_cu_data::per_bfd.
	(dwarf2_per_bfd::allocate_signatured_type): Likewise.
	(create_type_unit_group): Likewise.
	(queue_comp_unit): Remove reference to
	per_cu->dwarf2_per_objfile.
	(maybe_queue_comp_unit): Likewise.
	(fill_in_sig_entry_from_dwo_entry): Assign new field.
	(create_cus_hash_table): Assign new field.

Change-Id: I4ba0a393e64a14489ef061261a3dede1509d055b
2020-05-27 11:15:54 -04:00
Simon Marchi 5e22e966a0 Remove dwarf2_cu->per_cu->dwarf2_per_objfile references
Change spots that access the dwarf2_per_objfile object through this
pattern:

  dwarf2_cu->per_cu->dwarf2_per_objfile

to

  dwarf2_cu->per_objfile

This allows removing many references to
dwarf2_per_cu_data::dwarf2_per_objfile.

Again, I hope the following ChangeLog entry will be fine.  I'd rather not
list all the affected functions, as it would be time-consuming and a bit
pointless.

gdb/ChangeLog:

	* dwarf2/read.c: Replace
	dwarf2_cu->per_cu->dwarf2_per_objfile references with
	dwarf2_cu->per_objfile throughout.

Change-Id: I00f44e88295f70ae805a4b18e8144ca92154612e
2020-05-27 11:15:54 -04:00
Simon Marchi 97a1449a95 Remove reference to dwarf2_per_cu_data::dwarf2_per_objfile in dw2_do_instantiate_symtab
This patch begins by removing the per_cu->dwarf2_per_objfile reference
in dw2_do_instantiate_symtab, instead accepting a dwarf2_per_objfile
object as a parameter.  It then fixes the fallouts.  In this context,
the dwarf2_per_objfile is generally derived from an objfile passed to a
quick_symbol_functions callback.

gdb/ChangeLog:

	* dwarf2/read.c (dw2_do_instantiate_symtab): Add per_objfile
	parameter, don't use per_cu->dwarf2_per_objfile.
	(dw2_instantiate_symtab): Likewise.
	(dw2_find_last_source_symtab): Update.
	(dw2_map_expand_apply): Update.
	(dw2_lookup_symbol): Update.
	(dw2_expand_symtabs_for_function): Update.
	(dw2_expand_all_symtabs): Update.
	(dw2_expand_symtabs_with_fullname): Update.
	(dw2_expand_symtabs_matching_one): Add per_objfile parameter,
	don't use per_cu->dwarf2_per_objfile.
	(dw2_expand_marked_cus): Update.
	(dw2_find_pc_sect_compunit_symtab): Update.
	(dw2_debug_names_lookup_symbol): Update.
	(dw2_debug_names_expand_symtabs_for_function): Update.
	(dw2_debug_names_map_matching_symbols): Update.
	(dwarf2_psymtab::expand_psymtab): Update.

Change-Id: I248300822a09bae8470b65a7122d04fb9cb2b5bc
2020-05-27 11:15:54 -04:00
Simon Marchi 9e021579fa Add dwarf2_per_objfile field to dwarf2_cu
Subsequent patches will make dwarf2_per_cu_data objfile-independent.
This means that the dwarf2_per_cu_data::dwarf2_per_objfile field must
go.

The code using a dwarf2_cu structure currently accesses the current
dwarf2_per_objfile object through dwarf2_cu->per_cu->dwarf2_per_objfile.
Since it's ok for the dwarf2_cu to know about the current objfile (a
dwarf2_cu is always used in the context of a particular objfile), add a
dwarf2_per_objfile field to dwarf2_cu.  Upcoming patches will gradually
remove uses of dwarf2_per_cu_data::dwarf2_per_objfile in favor of
dwarf2_cu::dwarf2_per_objfile, until the former can be removed.

gdb/ChangeLog:

	* dwarf2/read.c (struct dwarf2_cu) <dwarf2_cu>: Add parameter.
	<per_objfile>: New member.
	(class cutu_reader) <init_tu_and_read_dwo_dies>: Add parameter.
	(cutu_reader::init_tu_and_read_dwo_dies): Add parameter, update
	call to dwarf2_cu.
	(cutu_reader::cutu_reader): Update.
	(dwarf2_cu::dwarf2_cu): Add parameter, initialize per_objfile.

Change-Id: I8fd0da7371f65baea1ea7787aad08e10453bc565
2020-05-27 11:15:54 -04:00
Simon Marchi ae090bdbf8 Move die_type_hash to dwarf2_per_objfile
The die_type_hash field can't be shared between multiple obfiles, as it
holds `struct type` objects, which are objfile-specific.  Move it from
dwarf2_per_bfd to dwarf2_per_objfile and update all references.

gdb/ChangeLog:

	* dwarf2/read.h (struct dwarf2_per_bfd) <die_type_hash>: Move to
	struct dwarf2_per_objfile.
	(struct dwarf2_per_objfile) <die_type_hash>: Move from struct
	dwarf2_per_bfd.
	* dwarf2/read.c (set_die_type): Update.
	(get_die_type_at_offset): Update.

Change-Id: I3589777ed3579bcabafd2ba859d27babe4502bfb
2020-05-27 11:15:54 -04:00
Simon Marchi af758d117e Remove symtab links from dwarf2_psymtab and dwarf2_per_cu_quick_data
The dwarf2_psymtab and dwarf2_per_cu_quick_data types contain a pointer
to a compunit_symtab, which is a pointer to the corresponding full
symtab.  The dwarf2_psymtab and dwarf2_per_cu_quick_data objects are
going to become objfile-independent, and possibly shared by multiple
objfiles, whereas compunit_symtab will stay objfile-dependent.  This
backlink to the compunit_symtab must therefore be removed.

This patch replaces them with a vector in the dwarf2_per_objfile type,
that serves as a mapping from dwarf2_per_cu_data objects to
compunit_symtab objects, for this particular objfile.  The vector is
indexed using the index assigned to the dwarf2_per_cu_data at its
creation.

I removed the get_compunit_symtab, as it appears to bring not much value
over calling dwarf2_per_objfile::get_symtab directly.

gdb/ChangeLog:

YYYY-MM-DD  Tom Tromey  <tom@tromey.com>
YYYY-MM-DD  Simon Marchi  <simon.marchi@efficios.com>

	* dwarf2/read.h (struct dwarf2_per_bfd) <num_psymtabs>: New
	method.
	(struct dwarf2_per_objfile) <resize_symtabs, symtab_set_p,
	get_symtab, set_symtab>: New methods.
	<m_symtabs>: New field.
	(struct dwarf2_psymtab): Derive from partial_symtab.
	<readin_p, get_compunit_symtab>: Declare methods.
	* dwarf2/read.c (dwarf2_per_objfile::symtab_set_p,
	dwarf2_per_objfile::get_symtab, dwarf2_per_objfile::set_symtab):
	New methods.
	(struct dwarf2_per_cu_quick_data) <compunit_symtab>: Remove.
	(dw2_do_instantiate_symtab, dw2_instantiate_symtab)
	(dw2_map_expand_apply, dw2_map_symtabs_matching_filename)
	(dw2_symtab_iter_next, dw2_print_stats)
	(dw2_expand_symtabs_with_fullname)
	(dw2_expand_symtabs_matching_one)
	(dw_expand_symtabs_matching_file_matcher)
	(dw2_find_pc_sect_compunit_symtab, dw2_map_symbol_filenames)
	(dw2_debug_names_iterator::next)
	(dw2_debug_names_map_matching_symbols)
	(fill_in_sig_entry_from_dwo_entry, dwarf2_psymtab::read_symtab)
	(process_queue, dwarf2_psymtab::expand_psymtab): Update.
	(dwarf2_psymtab::readin_p, dwarf2_psymtab::get_compunit_symtab):
	New methods.
	(get_compunit_symtab, process_full_comp_unit)
	(process_full_type_unit): Update.
	(dwarf2_build_psymtabs, dwarf2_initialize_objfile, add_type_unit): Call

Change-Id: Iec53d96e0b70a57d8b68408febdac3c6c3d4854b
2020-05-27 11:15:47 -04:00
Simon Marchi 5989a64ed5 Split dwarf2_per_objfile into dwarf2_per_objfile and dwarf2_per_bfd
This is the first step of splitting dwarf2_per_objfile in two, one
structure for objfile-independent data (dwarf2_per_bfd) and one for
objfile-dependent data (dwarf2_per_objfile).

The existing dwarf2_per_objfile is renamed dwarf2_per_bfd, and a new
dwarf2_per_objfile type is introduced, which sits "in between" the
objfile and dwarf2_per_bfd.

So where we had this before:

  objfile -> dwarf2_per_objfile (*)

we now have this:

  objfile -> dwarf2_per_objfile -> dwarf2_per_bfd (*)

(*) Note that the dwarf2_per_objfile in the former corresponds to
the dwarf2_per_bfd in the latter.

I've done the minimal amount of changes in this patch: following patches
will incrementally move things that are not actually shareable between
objfiles from dwarf2_per_bfd to dwarf2_per_objfile.

Most references to dwarf2_per_objfile objects are changed to
dwarf2_per_objfile->per_bfd.  To avoid many of these replacements, which
would have to be reverted later anyway, I've moved right away the
objfile backlink to the new dwarf2_per_objfile structure in this patch.
I've also moved the read_line_string method, since it references the
objfile backlink, and it's actually not difficult to move.

Once the moves are completed, multiple dwarf2_per_objfile sharing the
same BFD will point to the same single instance of dwarf2_per_bfd (as
long as they don't require relocation).

dwarf2_has_info, where we create these objects, is updated to the new
architecture.

I've had to change the get_gdb_index_contents_ftype typedef and related
functions.  The parameter type was changed from dwarf2_per_objfile to
dwarf2_per_bfd, otherwise the template wouldn't work.

Please excuse the terse ChangeLog entry, I have not listed all the
functions where dwarf2_per_objfile has been changed to
dwarf2_per_objfile->per_bfd.  It would take a considerable amount of
time and would not really be useful in the end.

gdb/ChangeLog:

	* dwarf2/read.h (dwarf2_per_objfile): Rename to dwarf2_per_bfd,
	then introduce a new dwarf2_per_objfile type.
	<read_line_string>: Move to the new dwarf2_per_objfile type.
	<objfile>: Likewise.
	(dwarf2_per_bfd): Rename dwarf2_per_objfile to this.
	* dwarf2/read.c: Replace references to dwarf2_per_objfile with
	dwarf2_per_objfile->per_bfd.
	(dwarf2_per_objfile::dwarf2_per_objfile): Rename to...
	(dwarf2_per_bfd::dwarf2_per_bfd): ... this.
	(dwarf2_per_objfile::free_cached_comp_units): Rename to...
	(dwarf2_per_bfd::free_cached_comp_units): ... this.
	(dwarf2_has_info): Allocate dwarf2_per_bfd.
	(dwarf2_per_objfile::locate_sections): Rename to...
	(dwarf2_per_bfd::locate_sections): ... this.
	(dwarf2_per_objfile::get_cutu): Rename to...
	(dwarf2_per_bfd::get_cutu): ... this.
	(dwarf2_per_objfile::get_cu): Rename to...
	(dwarf2_per_bfd::get_cu): ... this.
	(dwarf2_per_objfile::get_tu): Rename to...
	(dwarf2_per_bfd::get_tu): ... this.
	(dwarf2_per_objfile::allocate_per_cu): Rename to...
	(dwarf2_per_bfd::allocate_per_cu): ... this.
	(dwarf2_per_objfile::allocate_signatured_type): Rename to...
	(dwarf2_per_bfd::allocate_signatured_type): ... this.
	(get_gdb_index_contents_ftype): Change parameter from
	dwarf2_per_objfile to dwarf2_per_bfd.
	* dwarf2/macro.c, dwarf2/index-write.c: Replace references to
	dwarf2_per_objfile with dwarf2_per_objfile->per_bfd.

Change-Id: I7de7b5d1ce7494aa73bfcf15f719d3c5c46e138c
2020-05-27 11:13:50 -04:00
Tom Tromey a50264baf5 Add dwarf2_per_objfile member to DWARF batons
Various DWARF callbacks expect to be able to fetch the objfile and / or
dwarf2_per_objfile from the DWARF CU object.  However, this won't be
possible once sharing is implemented.

Because these objects are related to full symbols (e.g., they are used
to implement location expressions), they can simply store the
dwarf2_per_objfile they need.

This patch adds a per_objfile member to the various "baton" structures
and arranges to set this value when constructing the baton.

gdb/ChangeLog:

YYYY-MM-DD  Tom Tromey  <tom@tromey.com>
YYYY-MM-DD  Simon Marchi  <simon.marchi@efficios.com>

	* dwarf2/loc.c (struct piece_closure) <per_objfile>: New member.
	(allocate_piece_closure): Set "per_objfile" member.
	(dwarf2_find_location_expression, dwarf2_locexpr_baton_eval)
	(locexpr_describe_location, loclist_describe_location): Use new
	member.
	* dwarf2/read.c (read_call_site_scope)
	(mark_common_block_symbol_computed, attr_to_dynamic_prop)
	(dwarf2_const_value_attr, dwarf2_fetch_die_loc_sect_off)
	(fill_in_loclist_baton, dwarf2_symbol_mark_computed,
	handle_data_member_location): Set per_objfile member.
	* dwarf2/loc.h (struct dwarf2_locexpr_baton) <per_objfile>: New
	member.
	(struct dwarf2_loclist_baton) <per_objfile>: New member.

Change-Id: If3aaa6a0f544be86710157c3adb68fde24d80037
2020-05-27 11:13:50 -04:00
Tom Tromey d3473f0c4b Add dwarf2_per_cu_data::index
Currently, a dwarf2_per_cu_data can hold a link to the corresponding
expanded compunit_symtab.  However, the dwarf2_per_cu_data objects are
shared across objfiles, a simple pointer won't work: each objfile
sharing the dwarf2_per_cu_data instance will have a corresponding
compunit_symtab.

Instead, this link will be stored in the dwarf2_per_objfile object
(which will contain the objfile-specific data).  To enable this, we add
an index to each dwarf2_per_cu_data and signatured_type.  The data
structure in the dwarf2_per_objfile will use this new index to map a
dwarf2_per_cu_data to its corresponding compunit_symtab, for this
objfile.

gdb/ChangeLog:

	* dwarf2/read.h (struct dwarf2_per_objfile) <allocate_per_cu,
	allocate_signatured_type>: Declare new methods.
	<m_num_psymtabs>: New member.
	(struct dwarf2_per_cu_data) <index>: New member.
	* dwarf2/read.c (dwarf2_per_objfile::allocate_per_cu)
	(dwarf2_per_objfile::allocate_signatured_type): New methods.
	(create_cu_from_index_list): Use allocate_per_cu.
	(create_signatured_type_table_from_index)
	(create_signatured_type_table_from_debug_names)
	(create_debug_type_hash_table, add_type_unit)
	(read_comp_units_from_section): Use allocate_signatured_type.

Change-Id: I7733479a38ce82a5015cb184c8acce5f8bbf2e69
2020-05-27 11:13:49 -04:00
Tom Tromey 5717c425a6 Add "objfile" parameter to two partial_symtab methods
This series will cause partial symtabs to be shared across objfiles.
However, full symtabs and symbols will still be objfile-dependent, so
will be expanded separately for each objfile.  So, a debug info reader
will need to know which objfile to consult when expanding a partial
symtab.

This patch adds an objfile parameter to the two relevant methods of
partial_symtab.  Current implementations simply ignore them.

gdb/ChangeLog:

	* psymtab.c (partial_map_expand_apply)
	(psym_find_pc_sect_compunit_symtab, psym_lookup_symbol)
	(psym_lookup_global_symbol_language)
	(psymtab_to_symtab, psym_find_last_source_symtab, dump_psymtab)
	(psym_print_stats, psym_expand_symtabs_for_function)
	(psym_map_symbol_filenames, psym_map_matching_symbols)
	(psym_expand_symtabs_matching)
	(partial_symtab::read_dependencies, maintenance_info_psymtabs)
	(maintenance_check_psymtabs): Update.
	* psympriv.h (struct partial_symtab) <readin_p,
	get_compunit_symtab>: Add objfile parameter.
	(struct standard_psymtab) <readin_p, get_compunit_symtab>:
	Likewise.
	* dwarf2/read.c (struct dwarf2_include_psymtab) <readin_p,
	get_compunit_symtab>: Likewise.
	(dwarf2_psymtab::expand_psymtab): Pass objfile argument.

Change-Id: I3f0b26787c3e78f7fb78b9fc011d91fb8690f3a0
2020-05-27 11:13:48 -04:00
Tom Tromey 4594094926 Introduce dwarf2_per_objfile::obstack
Currently much of the DWARF-related data is stored on the objfile
obstack.  This prevents sharing this data across objfiles, so this patch
adds a new obstack to dwarf2_per_objfile.  Note that the
dwarf2_per_objfile type is going to become "dwarf2_per_bfd" in a
subsequent patch, which is indeed going to be shared between objfiles.

One way to check whether this is correct is to look at the remaining
uses of objfile_obstack in the DWARF code and note that they all
appear in the "full CU" code paths.

The converse -- storing per-objfile data on the shared obstack -- is
not good, but it is just a memory leak, not a potential
use-after-free.  Double-checking this would also be useful, though.

gdb/ChangeLog:

	* dwarf2/read.h (struct dwarf2_per_objfile) <obstack>: New
	member.
	* dwarf2/read.c (delete_file_name_entry): Fix comment.
	(create_cu_from_index_list)
	(create_signatured_type_table_from_index)
	(create_signatured_type_table_from_debug_names)
	(dw2_get_file_names_reader, dwarf2_initialize_objfile)
	(dwarf2_create_include_psymtab)
	(create_debug_type_hash_table, add_type_unit)
	(create_type_unit_group, read_comp_units_from_section)
	(dwarf2_compute_name, create_cus_hash_table)
	(create_dwp_hash_table, create_dwo_unit_in_dwp_v1)
	(create_dwo_unit_in_dwp_v2, open_and_init_dwp_file): Use new
	obstack.
	(dw2_get_real_path): Likewise.  Change argument to
	dwarf2_per_objfile.

Change-Id: Icdec7be7c4d9f33d1dce4f807284f3077f7d3f03
2020-05-27 11:13:48 -04:00
Luis Machado f8c6d1528c Fix PR 26000, logical bitwise error / prologue analyzer
This fixes an instruction mask typo. We should be matching only
ldrd (immediate) and not any other of its variants. As is, it never matches
anything.

With the patch, the instruction mask also allows matching of ldrd (literal),
but the check for SP discards this particular instruction pattern, as it has
a hardcoded PC register.

gdb/ChangeLog:

2020-05-27  Luis Machado  <luis.machado@linaro.org>

	PR tdep/26000
	* arm-tdep.c (thumb_analyze_prologue): Fix instruction matching
	for ldrd (immediate).
2020-05-27 09:41:53 -03:00
Philippe Waroquiers e98d2e6da4 Ensure class_tui is listed in the output of "help" giving the list of classes.
Before this change, "help" was not showing the TUI class.
With this change:
  (gdb) help
  ...
  support -- Support facilities.
  text-user-interface -- TUI is the GDB text based interface.
  tracepoints -- Tracing of program execution without stopping the program.
  ...
  (gdb) help text-user-interface
  TUI is the GDB text based interface.
  In TUI mode, GDB can display several text windows showing
  the source file, the processor registers, the program disassembly, ...

  List of commands:

  + -- Scroll window forward.
  ...

Note that we cannot use "tui" for the fake class command name, as "tui"
is a command.

gdb/ChangeLog

2020-05-26  Philippe Waroquiers  <philippe.waroquiers@skynet.be>

	* command.h: Add comment giving the name of class_tui.
	* cli/cli-cmds.c (_initialize_cli_cmds): If TUI defined,
	create the fake command for the help for class_tui.
2020-05-26 22:18:42 +02:00
Tom Tromey 53a47a3e49 Handle indexing Ada arrays with enum indices
In Ada, like C, an enum can assign values to the constants.  However,
unlike C (or any other language supported by gdb), the enum type can
also be used as the range of an array.

In this case, the user's code references the enum constants, but the
compiler translates these to the position of the constant in the enum.
So for example one might write:

   type Enum_With_Gaps is
     (
      LIT0,
      LIT1,
      LIT2,
      LIT3,
      LIT4
     );

   for Enum_With_Gaps use
     (
      LIT0 => 3,
      LIT1 => 5,
      LIT2 => 8,
      LIT3 => 13,
      LIT4 => 21
     );

Then index an array like "array(LIT3)" -- but this will be the 4th
element in an array of 5 elements, not the 13th element in an array of
19 (assuming I did the math right) elements.

gdb supports this to some degree, with the only missing piece being
indexing into such an array.  This patch implements this missing
feature, and also fixes an existing bug, which is that in some
situations I believe gdb would mis-compute the resulting array's
length.

The approach taken here is to try to integrate this feature into the
core of gdb.  My view is that much of the Ada support should be better
integrated with gdb, rather than being "on the side".  This, I think,
would help avoid code duplication at least.  So, I try to take steps
toward this goal when possible.

Because other languages generally don't allow the user to specify the
index type of an array, I simply made the core of gdb unconditionally
apply discrete_position when computing the range of such an array.
This is a no-op for ordinary types, but applies the enum
value-to-position transformation for TYPE_CODE_ENUM.

gdb/ChangeLog
2020-05-26  Tom Tromey  <tromey@adacore.com>

	* ada-lang.c (ada_print_array_index): Change type.  Call val_atr.
	(ada_value_ptr_subscript): Don't call pos_atr on the lower bound.
	(val_atr): New function.
	(value_val_atr): Use it.
	* ada-valprint.c (print_optional_low_bound): Change low bound
	handling for enums.
	(val_print_packed_array_elements): Don't call discrete_position.
	* gdbtypes.c (get_discrete_bounds) <TYPE_CODE_RANGE>: Call
	discrete_position for enum types.
	* language.c (default_print_array_index): Change type.
	* language.h (struct language_defn) <la_print_array_index>: Add
	index_type parameter, change type of index_value.
	(LA_PRINT_ARRAY_INDEX): Add index_type parameter.
	(default_print_array_index): Update.
	* valprint.c (maybe_print_array_index): Don't call
	value_from_longest.  Update.
	(value_print_array_elements): Don't call discrete_position.

gdb/testsuite/ChangeLog
2020-05-26  Tom Tromey  <tromey@adacore.com>

	* gdb.ada/arr_acc_idx_w_gap.exp: Add tests.
2020-05-26 14:11:08 -06:00
Tom Tromey 0bc2354b81 Fix bugs in 'val and 'pos with range types
In Ada, the 'val and 'pos attributes can be used to map from an
enumeration constant to its position in the enum and vice versa.
These operators did not work properly when the type in question was a
subrange of an enum type with "holes".

gdb/ChangeLog
2020-05-26  Tom Tromey  <tromey@adacore.com>

	* ada-lang.c (value_val_atr): Handle TYPE_CODE_RANGE.
	* gdbtypes.c (discrete_position): Handle TYPE_CODE_RANGE.

gdb/testsuite/ChangeLog
2020-05-26  Tom Tromey  <tromey@adacore.com>

	* gdb.ada/arr_acc_idx_w_gap.exp: Add enum subrange tests.
	* gdb.ada/arr_acc_idx_w_gap/enum_with_gap.ads (Enum_Subrange): New
	type.
	* gdb.ada/arr_acc_idx_w_gap/enum_with_gap_main.adb (V): New
	variable.
2020-05-26 14:11:08 -06:00
Cristiano De Alti 1218a4bf49 gdb: make avr_integer_to_address generate code or data address based on type
The AVR architecture is a Harvard one, meaning it has different memory
spaces for code and data.  In GDB, this is dealt with by having the data
(SRAM) addresses start at 0x00800000.  When interpreting an integer as
an address (converting to a CORE_ADDR), we currently always generate a
data address.  This doesn't work for some cases described below, where
the integer is meant to represent a code address.

This patch changes avr_integer_to_address so that it generates the
correct type of address (code or data) based on the passed type.

Using the simavr.exp board, I didn't see any regressions when running
the gdb.base/*.exp tests.  A few tests go from fail to pass, but none
from pass to fail.  There are a few new fails and unresolved, but it's
just because some tests manage to make more progress before failing in a
different way.

In practice, it fixes disassembling by address, as described in the PR:

    - (gdb) disassemble 0x12a,0x12b
    - Dump of assembler code from 0x12a to 0x12b:
    -    0x0000012a <main+0>: push    r28
    - End of assembler dump.

    + (gdb) disassemble 0x12a,0x12b
    + Dump of assembler code from 0x80012a to 0x80012b:
    +    0x0080012a:  nop
    + End of assembler dump.

And also, setting a breakpoint by address:

    - (gdb) p &main
    - $1 = (int (*)(void)) 0x12a <main>
    - (gdb) b *0x12a
    - Breakpoint 1 at 0x80012a

    + (gdb) p &main
    + $1 = (int (*)(void)) 0x12a <main>
    + (gdb) b *0x12a
    + Breakpoint 1 at 0x12a: file test-avr.c, line 3.
    + Note: automatically using hardware breakpoints for read-only addresses.

gdb/ChangeLog:

	PR gdb/13519
	* avr-tdep.c (avr_integer_to_address): Return data or code
	address accordingly to the second 'type' argument of the
	function.

Change-Id: Iaea1587d053e86f4ab8aebdcabec8d31a6d262cd
2020-05-25 11:56:14 -04:00
Michael Weghorn 92651b1d91 gdb: Move construct_inferior_arguments to gdbsupport
This moves the function construct_inferior_arguments from
gdb/inferior.h and gdb/infcmd.c to gdbsupport/common-inferior.{h,cc}.
While at it, also move the function's comment to the header file
to align with current standards.

The intention is to use it from gdbserver in a follow-up commit.

gdb/ChangeLog:

	* infcmd.c, inferior.h: (construct_inferior_arguments):
	Moved function from here to gdbsupport/common-inferior.{h,cc}

gdbsupport/ChangeLog:

	* common-inferior.h, common-inferior.cc: (construct_inferior_arguments):
	Move function here from gdb/infcmd.c, gdb/inferior.h

Change-Id: Ib9290464ce8c0872f605d8829f88352d064c30d6
2020-05-25 11:38:02 -04:00
Tom Tromey 0a4f5f8cae Revert "Add completion styling"
This reverts commit eca1f90cf4.  Several
changes were requested, and it seemed simplest to revert it.

gdb/ChangeLog
2020-05-23  Tom Tromey  <tom@tromey.com>

	Revert commit eca1f90c:
	* NEWS: Remove entry for completion styling.
	* completer.c (_rl_completion_prefix_display_length): Move
	declaration later.
	(gdb_fnprint): Revert.
	(gdb_display_match_list_1): Likewise.
	* cli/cli-style.c (completion_prefix_style)
	(completion_difference_style, completion_suffix_style): Remove.
	(_initialize_cli_style): Revert.
	* cli/cli-style.h (completion_prefix_style)
	(completion_difference_style, completion_suffix_style): Don't
	declare.

gdb/doc/ChangeLog
2020-05-23  Tom Tromey  <tom@tromey.com>

	* gdb.texinfo (Output Styling): Don't mention completion styling.
	(Editing): Don't mention readline completion styling.

gdb/testsuite/ChangeLog
2020-05-23  Tom Tromey  <tom@tromey.com>

	* gdb.base/style.exp: Remove completion styling test.
	* lib/gdb-utils.exp (style): Remove completion styles.
2020-05-24 20:27:48 -06:00
Pedro Alves e08bd6c508 Don't remove C++ aliases from completions if symbol doesn't match
completion_list_add_symbol currently tries to remove C++ function
aliases from the completions match list even if the symbol passed down
wasn't successfully added to the completion list because it didn't
match.  I.e., we call cp_canonicalize_string_no_typedefs for each and
every C++ function in the program, which is useful work.  This patch
skips that useless work.

gdb/ChangeLog:
2020-05-24  Pedro Alves  <palves@redhat.com>

	* symtab.c (completion_list_add_name): Return boolean indication
	of whether the symbol matched.
	(completion_list_add_symbol): Don't try to remove C++ aliases if
	the symbol didn't match in the first place.
	* symtab.h (completion_list_add_name): Return bool.
2020-05-24 13:32:25 +01:00
Simon Marchi ceacbf6edf gdb: remove TYPE_FIELD macro
Replace all uses of it by type::field.

Note that since type::field returns a reference to the field, some spots
are used to assign the whole field structure.  See ctfread.c, function
attach_fields_to_type, for example.  This is the same as was happening
with the macro, so I don't think it's a problem, but if anybody sees a
really nicer way to do this, now could be a good time to implement it.

gdb/ChangeLog:

	* gdbtypes.h (TYPE_FIELD): Remove.  Replace all uses with
	type::field.
2020-05-23 17:39:54 -04:00
Joel Brobecker 26f1625454 Document the GDB 9.2 release in gdb/ChangeLog
gdb/ChangeLog:

	GDB 9.2 released.
2020-05-23 14:19:06 -07:00
Tom Tromey eca1f90cf4 Add completion styling
Readline has a styling feature for completion -- if it is enabled, the
common prefix of completions will be displayed in a different style.
This doesn't work in gdb, because gdb implements its own completer.

This patch implements the feature.  However, it doesn't directly use
the Readline feature, because gdb can do a bit better: it can let the
user control the styling using the existing mechanisms.

This version incorporates an Emacs idea, via Eli: style the prefix,
the "difference character", and the suffix differently.

gdb/ChangeLog
2020-05-23  Tom Tromey  <tom@tromey.com>

	* NEWS: Add entry for completion styling.
	* completer.c (_rl_completion_prefix_display_length): Move
	declaration earlier.
	(gdb_fnprint): Use completion_style.
	(gdb_display_match_list_1): Likewise.
	* cli/cli-style.c (completion_prefix_style)
	(completion_difference_style, completion_suffix_style): New
	globals.
	(_initialize_cli_style): Register new globals.
	* cli/cli-style.h (completion_prefix_style)
	(completion_difference_style, completion_suffix_style): Declare.

gdb/doc/ChangeLog
2020-05-23  Tom Tromey  <tom@tromey.com>

	* gdb.texinfo (Output Styling): Mention completion styling.
	(Editing): Mention readline completion styling.

gdb/testsuite/ChangeLog
2020-05-23  Tom Tromey  <tom@tromey.com>

	* gdb.base/style.exp: Add completion styling test.
	* lib/gdb-utils.exp (style): Add completion styles.
2020-05-23 14:53:33 -06:00
Pedro Alves 51e2cfa2dc Use safe-ctype.h (ISSPACE etc.) in symbol parsing & comparison
This patch avoids depending on the current locale when parsing &
comparing symbol names, by using libiberty's safe-ctype.h uppercase
TOLOWER, ISXDIGIT, etc. macros instead of the standard ctype.h
tolower, isxdigit, etc. macros/functions.

This commit:

 commit b1b60145ae
 Author:     Pedro Alves <palves@redhat.com>
 AuthorDate: Tue May 22 17:35:38 2018 +0100

    Support UTF-8 identifiers in C/C++ expressions (PR gdb/22973)

did something similar, except in the expression parser.

This can improve GDB's symbol loading performance significantly.
Currently strcmp_iw_ordered can show up high on profiles (called from
sort_pst_symbols -> std::sort) because of the isspace and tolower
functions.  Hannes mentions seeing it as high as in ~24% of the
profiling samples on Windows
(https://sourceware.org/pipermail/gdb-patches/2020-May/168858.html).

I tested GDB's performance (built with "-g -O2") loading a "-g -O0"
build of gdb.

I ran GDB 10 times like:

	  /bin/time -f %e \
		    ./gdb/gdb --data-directory ./gdb/data-directory -nx \
		    -batch /tmp/gdb-g-O0

Then I computed the mean time.

The baseline mean time was

 gdb    2.515

This patch brings the number down to

 gdb    2.096

Which is an around 16% improvement.

gdb/ChangeLog:
2020-05-23  Pedro Alves  <palves@redhat.com>

	* utils.c: Include "gdbsupport/gdb-safe-ctype.h".
	(parse_escape): Use ISDIGIT instead of isdigit.
	(puts_debug): Use gdb_isprint instead of isprint.
	(fprintf_symbol_filtered): Use ISALNUM instead of isalnum.
	(cp_skip_operator_token, skip_ws, strncmp_iw_with_mode): Use
	ISSPACE instead of isspace.
	(strncmp_iw_with_mode): Use TOLOWER instead of tolower and ISSPACE
	instead of isspace.
	(strcmp_iw_ordered): Use ISSPACE instead of isspace.
	(string_to_core_addr): Use TOLOWER instead of tolower, ISXDIGIT
	instead of isxdigit and ISDIGIT instead of isdigit.

gdbsupport/ChangeLog:
2020-05-23  Pedro Alves  <palves@redhat.com>

	* gdb-safe-ctype.h: New.
2020-05-23 12:46:37 +01:00
Simon Marchi 80fc5e77f0 gdb: remove TYPE_FIELDS macro
Remove all uses of the `TYPE_FIELDS` macro.  Replace them with either:

1) type::fields, to obtain a pointer to the fields array (same as
   TYPE_FIELDS yields)
2) type::field, a new convenience method that obtains a reference to one
   of the type's field by index.  It is meant to replace

     TYPE_FIELDS (type)[idx]

   with

     type->field (idx)

gdb/ChangeLog:

	* gdbtypes.h (struct type) <field>: New method.
	(TYPE_FIELDS): Remove, replace all uses with either type::fields
	or type::field.

Change-Id: I49fba10114417deb502060c6156aa5f7fc62462f
2020-05-22 16:55:17 -04:00
Simon Marchi 3cabb6b069 gdb: add type::fields / type::set_fields
Add the `fields` and `set_fields` methods on `struct type`, in order to
remove the `TYPE_FIELDS` macro.  In this patch, the `TYPE_FIELDS` macro
is changed to the `type::fields`, so all the call sites that use it to
set the fields array are changed to use `type::set_fields`.  The next
patch will remove `TYPE_FIELDS` entirely.

gdb/ChangeLog:

	* gdbtypes.h (struct type) <fields, set_fields>: New methods.
	(TYPE_FIELDS): Use type::fields.  Change all call sites that
	modify the propery to use type::set_fields instead.

Change-Id: I05174ce68f2ce3fccdf5d8b469ff141f14886b33
2020-05-22 16:55:16 -04:00
Simon Marchi 1f704f761b gdb: remove TYPE_NFIELDS macro
Remove `TYPE_NFIELDS`, changing all the call sites to use
`type::num_fields` directly.  This is quite a big diff, but this was
mostly done using sed and coccinelle.  A few call sites were done by
hand.

gdb/ChangeLog:

	* gdbtypes.h (TYPE_NFIELDS): Remove.  Change all cal sites to use
	type::num_fields instead.

Change-Id: Ib73be4c36f9e770e0f729bac3b5257d7cb2f9591
2020-05-22 16:55:15 -04:00
Simon Marchi 5e33d5f4e1 gdb: add type::num_fields / type::set_num_fields
Add the `num_fields` and `set_num_fields` methods on `struct type`, in
order to remove the `TYPE_NFIELDS` macro.  In this patch, the
`TYPE_NFIELDS` macro is changed to use `type::num_fields`, so all the
call sites that are used to set the number of fields are changed to use
`type::set_num_fields`.  The next patch will remove `TYPE_NFIELDS`
completely.

I think that in the future, we should consider making the interface of
`struct type` better.  For example, right now it's possible for the
number of fields property and the actual number of fields set to be out
of sync.  However, I want to keep the existing behavior in this patch,
just translate from macros to methods.

gdb/ChangeLog:

	* gdbtypes.h (struct type) <num_fields, set_num_fields>: New
	methods.
	(TYPE_NFIELDS): Use type::num_fields.  Change all call sites
	that modify the number of fields to use type::set_num_fields
	instead.

Change-Id: I5ad9de5be4097feaf942d111077434bf91d13dc5
2020-05-22 16:55:14 -04:00
Tom Tromey 9392ebb3bb Remove obsolete declaration
Commit c9e0a7e333 ("Remove munmap_listp_free_cleanup") removed
munmap_listp_free, but missed a declaration.  This patch removes that
as well.

gdb/ChangeLog
2020-05-22  Tom Tromey  <tromey@adacore.com>

	* compile/compile-object-load.h (munmap_list_free): Don't
	declare.
2020-05-22 13:35:13 -06:00
Andrew Burgess 7c13f4e875 gdb: Restore old annotations behaviour when printing frame info
This undoes most of the changes from these commits:

  commit ec8e2b6d30
  Date:   Fri Jun 14 23:43:00 2019 +0100

      gdb: Don't allow annotations to influence what else GDB prints

  commit 0d3abd8cc9
  Date:   Wed Jun 12 22:34:26 2019 +0100

      gdb: Remove an update of current_source_line and current_source_symtab

as a result of the discussion here:

  https://sourceware.org/pipermail/gdb/2020-April/048468.html

Having taken time to reflect on the discussion, and reading the
documentation again I believe we should revert GDB's behaviour back to
how it used to be.

The original concern that triggered the initial patch was that when
annotations were on the current source and line were updated (inside
the annotation code), while when annotations are off this update would
not occur.  This was incorrect, as printing the source with the call
to print_source_lines does also update the current source and line.

Further, the documentation here:
  https://sourceware.org/gdb/current/onlinedocs/gdb/Source-Annotations.html#Source-Annotations

Clearly states:

  "The following annotation is used instead of displaying source code:

   ^Z^Zsource filename:line:character:middle:addr

   ..."

So it is documented that the 'source' annotation is a replacement for,
and not in addition to, actually printing the source lie.

There are still a few issues that I can see, these are:

  1. In source.c:info_line_command, when annotations are on we call
  annotate_source_line, however, if annotations are off then there is
  no corresponding call to print the source line.  This means that a
  if a user uses 'info line ...' with annotations on, and then does a
  'list', they will get different results than if they had done this
  with annotations off.

  2. It bothers me that the call to annotate_source_line returns a
  boolean, and that this controls a call to print_source_line (in
  stack.c:print_frame_info).

  The reason for this is that the source line annotation will only
  print something if the file is found, and the line number is in
  range for the file.

  It seems to me like an annotation should always be printed, either
  one that identifies the file and line, or one that identifies the
  file and line GDB would like to access, but couldn't.

  I considered changing this, but in the end decided not too, if I
  extend the existing 'source' annotation to print something in all
  cases then I risk breaking existing UIs that rely on the file and
  line always being valid.  If I add a new annotation then this might
  also break existing UIs that rely on GDB itself printing the error
  from within print_source_line.

Given that annotations is deprecated (as I understand it) mechanism
for UIs to interact with GDB (in favour of MI) I figure we should just
restore the old behaviour, and leave the mini-bugs in until someone
actually complains.

This isn't a straight revert of the two commits mentioned above.  I've
left annotate_source_line instead of going back to the original
identify_source_line, which lived in source.c, but was really
annotation related.  The API for setting the current source and line
has changed since the original patches, so I updated for that change
too.  Finally I wrote the code in stack.c so that we avoided an extra
level of indentation, which I felt made things easier to read.

gdb/ChangeLog:

	* annotate.c (annotate_source_line): Update return type, add call
	to update current symtab and line.
	* annotate.h (annotate_source_line): Update return type, and
	extend header comment.
	* source.c (info_line_command): Check annotation_level before
	calling annotate_source_line.
	* stack.c (print_frame_info): If calling annotate_source_line
	returns true, then don't print any other source line information.

gdb/testsuite/ChangeLog:

	* gdb.base/annota1.exp: Update expected results.
	* gdb.cp/annota2.exp: Update expected results, remove duplicate
	test name.
	* gdb.cp/annota3.exp: Update expected results.
2020-05-22 13:39:50 +01:00
Simon Marchi aa370940e2 gdb: fix -Wtautological-overlap-compare error in lm32-tdep.c
Building with clang 11, we get:

    /home/smarchi/src/binutils-gdb/gdb/lm32-tdep.c:84:44: error: overlapping comparisons always evaluate to false [-Werror,-Wtautological-overlap-compare]
        return ((regnum >= SIM_LM32_EA_REGNUM) && (regnum <= SIM_LM32_BA_REGNUM))
                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Indeed, this doesn't make sense, as EA_REGNUM is greater than BA_REGNUM.
I'll assume that it was just a mistake and that these two should be
swapped.

The regnums for BA and EA are contiguous, so ultimately this particular
part of the condition is only true if regnum is == EA or == BA.  These
registers are Exception Address and Breakpoint Address, so I guess it
makes sense for them to be in the system register group.

The relevant reference is here:

  https://www.latticesemi.com/-/media/LatticeSemi/Documents/UserManuals/JL/LatticeMico32ProcessorReferenceManual39.ashx?document_id=52077

gdb/ChangeLog:

	* lm32-tdep.c (lm32_register_reggroup_p): Fix condition.
2020-05-21 13:22:10 -04:00
Simon Marchi 84d53fa9d2 gdb: remove unnecessary NULL checks before xfree
I was inspired by a series of patches merged by Alan Modra in the other
projects, so I did the same in GDB with a bit of Coccinelle and grep.

This patch removes the unnecessary NULL checks before calls to xfree.
They are unnecessary because xfree already does a NULL check.  Since
free is supposed to handle NULL values correctly, the NULL check in
xfree itself is also questionable, but I've left it there for now.

gdb/ChangeLog:

	* coffread.c (patch_type): Remove NULL check before xfree.
	* corefile.c (set_gnutarget): Likewise.
	* cp-abi.c (set_cp_abi_as_auto_default): Likewise.
	* exec.c (build_section_table): Likewise.
	* remote.c (remote_target::pass_signals): Likewise.
	* utils.c (n_spaces): Likewise.
	* cli/cli-script.c (document_command): Likewise.
	* i386-windows-tdep.c (core_process_module_section): Likewise.
	* linux-fork.c (struct fork_info) <~fork_info>: Likewise.
2020-05-21 13:12:29 -04:00
Simon Marchi 9d428aae67 gdb: reset/recompute objfile section offsets in reread_symbols
This patch started as an investigation of this bug, where the program is
re-compiled between two "start" runs:

    $ ./gdb -nx --data-directory=data-directory -q a.out
    Reading symbols from a.out...
    (gdb) start
    Temporary breakpoint 1 at 0x1131: file test.c, line 1.
    Starting program: /home/smarchi/build/wt/test/gdb/a.out

    Temporary breakpoint 1, main () at test.c:1
    1       int main() { return 0; }

    *** re-compile a.out ***

    (gdb) start
    The program being debugged has been started already.
    Start it from the beginning? (y or n) y
    `/home/smarchi/build/wt/test/gdb/a.out' has changed; re-reading symbols.
    Temporary breakpoint 2 at 0x555555555129: file test.c, line 1.
    Starting program: /home/smarchi/build/wt/test/gdb/a.out
    warning: Probes-based dynamic linker interface failed.
    Reverting to original interface.

    Temporary breakpoint 2, main () at test.c:1
    1       int main() { return 0; }
    (gdb)

To reproduce the bug, a.out needs to be a position-independent
executable (PIE).

Here's what happens:

1) We first read the symbols of a.out.  The section offsets in the
   objfile are all 0, so the symbols are created unrelocated.
2) The breakpoint on main is created, as you can see the breakpoint
   address (derived from the `main` symbol with value 0x1129) is still
   unrelocated (0x1131).  Since the program is not yet started, we don't
   know at which base address the executable is going to end at.
   Everything good so far.
3) The execution starts, GDB finds out the executable's base address,
   fills the objfile's section_offsets vector with a bunch of offsets,
   and relocates the symbols with those offsets.  The latter modifies
   the symbol values (the `main` symbol is changed from 0x1129 to
   0x555555555129).
4) We `start` again, we detect that `a.out` has changed, the
   `reread_symbols` function kicks in.  It tries to reset everything
   in the `struct objfile` corresponding to `a.out`, except that it
   leaves the `section_offsets` vector there.
5) `reread_symbols` reads the debug info (calls `read_symbols`).  As the
   DWARF info is read, symbols are created using the old offsets still
   in `section_offsets`.  For example, the `main` symbol is created with
   the value 0x555555555129.  Even though at this point there is no
   process, so that address is bogus.  There's probably more that
   depends on section_offsets that is not done correctly.
6) Something in the SVR4 solib handling goes wrong, probably because
   of something that went wrong in (5).  I can't quite explain it (if
   somebody would like to provide a more complete analysis, please go
   ahead).  But this is where it takes a wrong turn:

    #0  elf_locate_base () at /home/smarchi/src/wt/test/gdb/solib-svr4.c:799
    #1  0x000055f0a5bee6d5 in locate_base (info=<optimized out>) at /home/smarchi/src/wt/test/gdb/solib-svr4.c:848
    #2  0x000055f0a5bf1771 in svr4_handle_solib_event () at /home/smarchi/src/wt/test/gdb/solib-svr4.c:1955
    #3  0x000055f0a5c0ff92 in handle_solib_event () at /home/smarchi/src/wt/test/gdb/solib.c:1258

   In the non-working case (without this patch), elf_locate_base returns
   0, whereas in the working case (with this patch) it returns a valid
   address, as we should expect.

This patch fixes this by making reread_symbols clear the
`section_offsets` vector, and re-create it by calling `sym_offsets`.
This is analogous to what syms_from_objfile_1 does.  I didn't seem
absolutely necessary, but I also made it clear the various
`sect_index_*` fields, since their values no longer make sense (they
describe the old executable, and are indices in the now cleared
sections/section_offsets arrays).

I don't really like the approach taken by reread_symbols, trying to
reset everything manually on the objfile object, instead of, for
example, creating a new one from scratch.  But I don't know enough yet
to propose a better solution.

One more reason I think this patch is needed is that the number of
sections of the new executable could be different from the number of
sections of the old executable.  So if we don't re-create the
section_offsets array, not only we'll have wrong offsets, but we could
make accesses past the array.

Something else that silently fails (but doesn't seem to have
consequences) is the prologue analysis when we try to create the
breakpoint on `main`.  Since the `main` symbol has the wrong value
0x555555555129, we try to access memory in that area, which fails.  This
can be observed by debugging gdb and using `catch throw`.  Before the
process is started, we need to access the memory at its unrelocated
address, 0x1129, which will read memory from the ELF file.  This is now
what happens, with this patch applied.

It silently fails, probably because commit 46a62268b, "Catch exceptions
thrown from gdbarch_skip_prologue", papered over the problem and added
an empty catch clause.  I'm quite sure that the root cause then was the
one fixed by this patch.

This fixes tests gdb.ada/exec_changed.exp and gdb.base/reread.exp for
me.

gdb/ChangeLog:

	* symfile.c (reread_symbols): Clear objfile's section_offsets
	vector and section indices, re-compute them by calling
	sym_offsets.
2020-05-20 15:45:03 -04:00
Tom Tromey 250106a76a Remove bound_name static from ada-lang.c
ada-lang.c has a "bound_name" static that is used when finding fields
in a structure in some cases.  This patch removes it in favor of
computing the desired field name at runtime; this avoids an artificial
limit.

I'm checking this in.  Tested on x86-64 Fedora 30, and also on the
internal AdaCore test suite.

gdb/ChangeLog
2020-05-20  Tom Tromey  <tromey@adacore.com>

	* ada-lang.c (bound_name, MAX_ADA_DIMENS): Remove.
        (desc_one_bound, desc_index_type): Compute field name.
2020-05-20 07:20:05 -06:00
Tom de Vries 9a0bacfb08 [gdb/symtab] Handle .gdb_index in ada language mode
When running test-case gdb.base/with.exp with target board cc-with-gdb-index,
we have:
...
(gdb) PASS: gdb.base/with.exp: basics: show language
with language ada -- print g_s^M
'g_s' has unknown type; cast it to its declared type^M
(gdb) FAIL: gdb.base/with.exp: basics: with language ada -- print g_s
...

This is due to this bit in dw2_map_matching_symbols:
...
  if (dwarf2_per_objfile->index_table != nullptr)
    {
      /* Ada currently doesn't support .gdb_index (see PR24713).  We can get
	 here though if the current language is Ada for a non-Ada objfile
	 using GNU index.  As Ada does not look for non-Ada symbols this
	 function should just return.  */
      return;
    }
...

While the reasoning in the comment may be sound from language perspective, it
does introduce an inconsistency in gdb behaviour between:
- having a .gdb_index section, and
- having a .gdb_names section, or a partial symtab, or -readnow.

Fix the inconsistency by completing implementation of
dw2_map_matching_symbols.

Tested on x86_64-linux, both with native and target board
cc-with-debug-index.

gdb/ChangeLog:

2020-05-20  Tom de Vries  <tdevries@suse.de>

	PR symtab/25833
	* dwarf2/read.c (dw2_map_matching_symbols): Handle .gdb_index.

gdb/testsuite/ChangeLog:

2020-05-20  Tom de Vries  <tdevries@suse.de>

	PR symtab/25833
	* gdb.base/with-mf-inc.c: New test.
	* gdb.base/with-mf-main.c: New test.
	* gdb.base/with-mf.exp: New file.
2020-05-20 11:48:39 +02:00
Alan Modra 7b958a48e1 PR25993, read of freed memory
ldmain.c:add_archive_element copies file name pointers from the bfd to
a lang_input_statement_type.
  input->filename = abfd->filename;
  input->local_sym_name = abfd->filename;
This results in stale pointers when twiddling the bfd filename in
places like the pe ld after_open.  So don't free the bfd filename,
and make copies using bfd_alloc memory that won't result in small
memory leaks that annoy memory checkers.

	PR 25993
bfd/
	* archive.c (_bfd_get_elt_at_filepos): Don't strdup filename,
	use bfd_set_filename.
	* elfcode.h (_bfd_elf_bfd_from_remote_memory): Likewise.
	* mach-o.c (bfd_mach_o_fat_member_init): Likewise.
	* opncls.c (bfd_fopen, bfd_openstreamr, bfd_openr_iovec, bfd_openw),
	(bfd_create): Likewise.
	(_bfd_delete_bfd): Don't free filename.
	(bfd_set_filename): Copy filename param to bfd_alloc'd memory,
	return pointer to the copy or NULL on alloc fail.
	* vms-lib.c (_bfd_vms_lib_get_module): Free newname and test
	result of bfd_set_filename.
	* bfd-in2.h: Regenerate.
gdb/
	* solib-darwin.c (darwin_bfd_open): Don't strdup pathname for
	bfd_set_filename.
	* solib-aix.c (solib_aix_bfd_open): Use std::string for name
	passed to bfd_set_filename.
	* symfile-mem.c (add_vsyscall_page): Likewise for string
	passed to symbol_file_add_from_memory.
	(symbol_file_add_from_memory): Make name param a const char* and
	don't strdup.
ld/
	* emultempl/pe.em (gld_${EMULATION_NAME}_after_open): Don't copy
	other_bfd_filename for bfd_set_filename, and test result of
	bfd_set_filename call.  Don't create a new is->filename, simply
	copy from bfd filename.  Free new_name after bfd_set_filename.
	* emultempl/pep.em (gld_${EMULATION_NAME}_after_open): Likewise.
2020-05-20 11:43:50 +09:30
Alan Modra c7e9767920 Use bfd_get_filename throughout gdb
This patch makes gdb use the inline accessor for all bfd->filename
read accesses.

	* coff-pe-read.c (read_pe_exported_syms): Use bfd_get_filename
	rather than accessing bfd->filename directly.
	* dtrace-probe.c (dtrace_static_probe_ops::get_probes): Likewise,
	and use bfd_section_name.
	* dwarf2/frame.c (decode_frame_entry): Likewise.
	* exec.c (exec_set_section_address): Likewise.
	* solib-aix.c (solib_aix_bfd_open): Likewise.
	* stap-probe.c (get_stap_base_address): Likewise.
	* symfile.c (reread_symbols): Likewise.
2020-05-20 07:59:00 +09:30
Tom Tromey 563c591bed Update call to target_fileio_open
An earlier patch changed target_fileio_open, but missed a caller.
This patch fixes it.

2020-05-19  Tom Tromey  <tromey@adacore.com>

	* sparc64-tdep.c (adi_tag_fd): Update call to target_fileio_open.
2020-05-19 12:35:07 -06:00
Simon Marchi f408d82c7a gdb: fix off-by-one error in quirk_rust_enum
Found by inspection, so I don't have a test for it (I don't think it
would be easy to have this bug cause a failure reliably).

We allocate space for N fields into `new_fields`, then memcpy N fields
at `new_fields + 1`.  This overflows the allocated buffer by one field.

Fix it by allocating `N + 1` fields.

gdb/ChangeLog:

	* dwarf2/read.c (quirk_rust_enum): Allocate enough fields.
2020-05-19 14:20:23 -04:00
Pedro Alves 98c59b527b Make exec-file-mismatch compare build IDs
The patch makes GDB do exec-file-mismatch validation by comparing
build IDs instead of the current method of comparing filenames.

Currently, the exec-file-mismatch feature simply compares filenames to
decide whether the exec file loaded in gdb and the exec file the
target reports is running match.  This causes false positives when
remote debugging, because it'll often be the case that the paths in
the host and the target won't match.  And of course misses the case of
the files having the same name but being actually different files
(e.g., different builds).

This also broke many testcases when running against gdbserver, causing
tests to be skipped like (here native-extended-gdbserver):

  (gdb) run
  Starting program: /home/pedro/gdb/binutils-gdb/build/gdb/testsuite/outputs/gdb.base/argv0-symlink/argv0-symlink-filelink
  warning: Mismatch between current exec-file /home/pedro/gdb/binutils-gdb/build/gdb/testsuite/outputs/gdb.base/argv0-symlink/argv0-symlink-filelink
  and automatically determined exec-file /home/pedro/gdb/binutils-gdb/build/gdb/testsuite/outputs/gdb.base/argv0-symlink/argv0-symlink
  exec-file-mismatch handling is currently "ask"
  Load new symbol table from "/home/pedro/gdb/binutils-gdb/build/gdb/testsuite/outputs/gdb.base/argv0-symlink/argv0-symlink"? (y or n) UNTESTED: gdb.base/argv0-symlink.exp: could not run to main

or to fail like (here native-gdbserver):

 (gdb) spawn /home/pedro/gdb/binutils-gdb/build/gdb/testsuite/../../gdbserver/gdbserver --once localhost:2346 /home/pedro/gdb/binutils-gdb/build/gdb/te
 stsuite/outputs/gdb.btrace/buffer-size/skip_btrace_tests-19968.x
 Process /home/pedro/gdb/binutils-gdb/build/gdb/testsuite/outputs/gdb.btrace/buffer-size/skip_btrace_tests-19968.x created; pid = 20040
 Listening on port 2346
 target remote localhost:2346
 Remote debugging using localhost:2346
 warning: Mismatch between current exec-file /home/pedro/gdb/binutils-gdb/build/gdb/testsuite/temp/19968/skip_btrace_tests-19968.x
 and automatically determined exec-file /home/pedro/gdb/binutils-gdb/build/gdb/testsuite/outputs/gdb.btrace/buffer-size/skip_btrace_tests-19968.x
 exec-file-mismatch handling is currently "ask"
 Load new symbol table from "/home/pedro/gdb/binutils-gdb/build/gdb/testsuite/outputs/gdb.btrace/buffer-size/skip_btrace_tests-19968.x"? (y or n) Quit
 (gdb) UNSUPPORTED: gdb.btrace/buffer-size.exp: target does not support record-btrace

The former case is about GDB not realizing the two files are the same,
because one of the them is a symlink to the other.  The latter case is
about GDB realizing that one file is a copy of the other.

Over the years, the toolchain has settled on build ID matching being
the canonical method to match core dumps to executables, and
executables with no debug info to their debug info.

This patch makes us use build IDs to match the running image of a
binary with its version loaded in gdb, which may or may not have debug
info.  This is very much like the core dump/executable matching.

The change to gdb_bfd_open is necessary to get rid of the "transfers
from remote targets can be slow" warning when we open the remote file
to read its build ID:

 (gdb) r
 Starting program: /home/pedro/gdb/binutils-gdb/build/gdb/testsuite/outputs/gdb.base/break/break
 Reading /home/pedro/gdb/binutils-gdb/build/gdb/testsuite/outputs/gdb.base/argv0-symlink/argv0-symlink from remote target...
 warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead.
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 warning: Mismatch between current exec-file /home/pedro/gdb/binutils-gdb/build/gdb/testsuite/outputs/gdb.base/break/break
 and automatically determined exec-file /home/pedro/gdb/binutils-gdb/build/gdb/testsuite/outputs/gdb.base/argv0-symlink/argv0-symlink
 exec-file-mismatch handling is currently "ask"
 Load new symbol table from "/home/pedro/gdb/binutils-gdb/build/gdb/testsuite/outputs/gdb.base/argv0-symlink/argv0-symlink"? (y or n)

While trying this out, I was worried that bfd would read a lot of
stuff from the binary in order to extract the build ID, making it
potentially slow, but turns out we don't read all that much.  Maybe a
couple hundred bytes, and most of it seemingly is the read-ahead
cache.  So I'm not worried about that.  Otherwise I'd consider whether
a new qXfer:buildid:read would be better.  But I'm happy that we
seemingly don't need to worry about it.

gdb/ChangeLog:
2020-05-19  Pedro Alves  <palves@redhat.com>

	* NEWS (set exec-file-mismatch): Adjust entry.
	* exec.c: Include "build-id.h".
	(validate_exec_file): Try to match build IDs instead of filenames.
	* gdb_bfd.c (struct gdb_bfd_open_closure): New.
	(gdb_bfd_iovec_fileio_open): Adjust to use gdb_bfd_open_closure
	and pass down 'warn_if_slow'.
	(gdb_bfd_open): Add 'warn_if_slow' parameter.  Use
	gdb_bfd_open_closure to pass it down.
	* gdb_bfd.h (gdb_bfd_open): Add 'warn_if_slow' parameter.

gdb/doc/ChangeLog:
2020-05-19  Pedro Alves  <palves@redhat.com>

	* gdb.texinfo (Attach): Update exec-file-mismatch description to
	mention build IDs.
	(Separate Debug Files): Add "build id" anchor.
2020-05-19 18:37:15 +01:00
Pedro Alves 4111f652f9 Eliminate target_fileio_open_warn_if_slow
This basically makes target_fileio_open_1 extern, renamed to
target_fileio_open, and eliminates the current
target_fileio_open_warn_if_slow and target_fileio_open.

A following parameter will want to change gdb_bfd_iovec_fileio_open,
the only caller of target_fileio_open_warn_if_slow, to pass down
"warn_if_slow" true/false from the caller, instead of hardcoding
"warn_if_slow" true.

gdb/ChangeLog:
2020-05-19  Pedro Alves  <palves@redhat.com>

	* gdb_bfd.c (gdb_bfd_iovec_fileio_open): Adjust.
	* target.c (target_fileio_open_1): Rename to target_fileio_open
	and make extern.  Use bool.
	(target_fileio_open, target_fileio_open_warn_if_slow): Delete.
	(target_fileio_read_alloc_1): Adjust.
	* target.h (target_fileio_open): Add 'warn_if_slow' parameter.
	(target_fileio_open_warn_if_slow): Delete declaration.
2020-05-19 18:36:51 +01:00
Pedro Alves ad80db5b9f Default gdb_bfd_open's fd parameter to -1
A following patch will add one more defaulted parameter.

gdb/ChangeLog:
2020-05-19  Pedro Alves  <palves@redhat.com>

	* gdb_bfd.h: (gdb_bfd_open): Default to 'fd' parameter to -1.
	Adjust all callers.
2020-05-19 18:36:24 +01:00
Yoshinori Sato 1d6ce4d312 gdb: fix -Wtautological-overlap-compare error in h8300-tdep.c
Compiling with clang 11 gives us:

      CXX    h8300-tdep.o
    /home/smarchi/src/binutils-gdb/gdb/h8300-tdep.c:225:21: error: overlapping comparisons always evaluate to false [-Werror,-Wtautological-overlap-compare]
                  if (disp < 0 && disp > 0xffffff)
                      ~~~~~~~~~^~~~~~~~~~~~~~~~~~
    /home/smarchi/src/binutils-gdb/gdb/h8300-tdep.c:203:17: error: overlapping comparisons always evaluate to false [-Werror,-Wtautological-overlap-compare]
              if (disp < 0 && disp > 0xffffff)
                  ~~~~~~~~~^~~~~~~~~~~~~~~~~~
    /home/smarchi/src/binutils-gdb/gdb/h8300-tdep.c:184:17: error: overlapping comparisons always evaluate to false [-Werror,-Wtautological-overlap-compare]
              if (disp < 0 && disp > 0xffffff)
                  ~~~~~~~~~^~~~~~~~~~~~~~~~~~

Indeed, disp (of type LONGEST) can't be less than 0 and greater than
0xffffff.

Fix it by changing the way we check if disp is negative.  Check the sign
bit of disp, which is a 24-bit number.

gdb/ChangeLog:

	* h8300-tdep.c (h8300_is_argument_spill): Change how we check
	whether disp is negative.
2020-05-19 13:33:52 -04:00
Simon Marchi 9005fbbb00 gdb: make symfile_segment_data::segment_info an std::vector
Change the symfile_segment_data::segment_info array to be an
std::vector.  No functional changes are expected.

gdb/ChangeLog:

	* symfile.h (struct symfile_segment_data)
	<~symfile_segment_data>: Remove.
	<segment_info>: Change to std::vector.
	* symfile.c (default_symfile_segments): Update.
	* elfread.c (elf_symfile_segments): Update.
2020-05-19 12:18:36 -04:00
Simon Marchi 68b888fff3 gdb: use std::vector to store segments in symfile_segment_data
Instead of maintaining two vectors, I added a small `segment` class
which holds both the base address and size of one segment and replaced
the two `segment_bases` and `segment_sizes` arrays with a single vector.

The rest of the changes are straightforward, no behavior changes are
expected.

gdb/ChangeLog:

	* symfile.h (struct symfile_segment_data) <struct segment>: New.
	<segments>: New.
	<segment_bases, segment_sizes>: Remove.
	* symfile.c (default_symfile_segments): Update.
	* elfread.c (elf_symfile_segments): Update.
	* remote.c (remote_target::get_offsets): Update.
	* solib-target.c (solib_target_relocate_section_addresses):
	Update.
2020-05-19 12:18:36 -04:00
Simon Marchi 62982abdee gdb: allocate symfile_segment_data with new
- Allocate this structure with new instead of XNEW, use a unique pointer
  to manage its lifetime.
- Change a few functions to return a unique   pointer instead of a
  plain pointer.
- Change free_symfile_segment_data to be symfile_segment_data's
  destructor.

gdb/ChangeLog:

	* symfile.h (struct symfile_segment_data): Initialize fields.
	<~symfile_segment_data>: Add.
	(symfile_segment_data_up): New.
	(struct sym_fns) <sym_segments>: Return a
	symfile_segment_data_up.
	(default_symfile_segments): Return a symfile_segment_data_up.
	(free_symfile_segment_data): Remove.
	(get_symfile_segment_data): Return a symfile_segment_data_up.
	* symfile.c (default_symfile_segments): Likewise.
	(get_symfile_segment_data): Likewise.
	(free_symfile_segment_data): Remove.
	(symfile_find_segment_sections): Update.
	* elfread.c (elf_symfile_segments): Return a
	symfile_segment_data_up.
	* remote.c (remote_target::get_offsets): Update.
	* solib-target.c (solib_target_relocate_section_addresses):
	Update.
	* symfile-debug.c (debug_sym_segments): Return a
	symfile_segment_data_up.
2020-05-19 12:18:36 -04:00
Rainer Orth e52a0f1bd9 Avoid short i386 register names on Solaris/x86 [PR25981]
This is the 32-bit companion to

	Remove unused ps_lgetLDT etc. on Solaris/x86 [PR25981]
        https://sourceware.org/pipermail/gdb-patches/2020-May/168713.html

A 32-bit-default gdb fails to compile with the updated <sys/regset.h>.
While it is also affected by the lack of a GS definition, which the
compantion patch above fixes, it also fails to compile i386-sol2-nat.c like
this

/vol/src/gnu/gdb/hg/master/git/gdb/i386-sol2-nat.c:181:3: error: 'EAX' was not declared in this scope
  181 |   EAX, ECX, EDX, EBX,
      |   ^~~

and several more.

While this could be fixed by either including <ucontext.h> here or
provding fallback definitions of the register macros, I chose to do what
the 64-bit-default code in the same file
(amd64_sol2_gregset32_reg_offset[]) does, namely just hardcode the
numeric values instead.  They are part of the ABI and thus guaranteed
not to change.

With this patch, a i386-pc-solaris2.11 configuration on master compiles
again, however, it doesn't work.  However, I could successfully test it
on the gdb-9 branch.

Compiling and testing proved to be messy, unfortunately:

* For one, Solaris <sys/procfs.h> and largefile support used to be
  mutually exclusive (fixed in Solaris 11.4 and Illumos), which was
  exacerbated by the fact that g++ predefines _FILE_OFFSET_BITS=64 since
  GCC 9.1.0.  For now I've worked around this by adding
  -U_FILE_OFFSET_BITS to CXXFLAGS and configuring with
  --disable-largefile.  I hope to clean this up in a future patch.

* gdb still defaults to startup-with-shell on.  However, /bin/bash is a
  64-bit executable which cannot be debugged by a 32-bit gdb.  I hacked
  around that part by pointing $SHELL at a 32-bit bash before running
  make check.

	PR build/25981
	* i386-sol2-nat.c [PR_MODEL_NATIVE != PR_MODEL_LP64] (regmap):
	Hardcode register numbers.
2020-05-18 17:59:43 +02:00
Rainer Orth 7f20433998 Remove unused ps_lgetLDT etc. on Solaris/x86 [PR25981]
As reported in PR build/25981, a future Solaris 11.4 update will soon
remove the short i386 register names like SS etc. from <sys/regset.h>.
They could leak into user code (e.g. via <signal.h> -> <sys/signal.h> ->
<sys/ucontext.h>) and pollute the user namespace.  Affected code would
have a hard time avoiding the issue: LLVM is one of those.

While the short names are required to be present by the i386 psABI, that
document only demands that they exist in <ucontext.h>, which is what the
upcoming update assures.

With this change, in a 64-bit-default configuration, procfs.c fails to
compile on Solaris/x86:

/vol/src/gnu/gdb/hg/master/git/gdb/procfs.c: In function 'ssd* procfs_find_LDT_entry(ptid_t)':
/vol/src/gnu/gdb/hg/master/git/gdb/procfs.c:1643:18: error: 'GS' was not declared in this scope
 1643 |   key = (*gregs)[GS] & 0xffff;
      |                  ^~
make[2]: *** [Makefile:1607: procfs.o] Error 1

Initially I meant to provide a definition using the planned replacement
macro, but closer inspection revealed a better way.  procfs_find_LDT_entry
and its helper proc_get_LDT_entry are only used to implement ps_lgetLDT,
one of the callback functions required by libthread_db.so.1
(cf. <proc_service.h>).  While that function is still documented as being
required even in Solaris 11.4, I found that calls to it had been removed
long ago in Solaris 9, so just removing the three functions above is the
easiest fix.

The following patch does just that.  It compiled successfully on
amd64-pc-solaris2.11, however, as reported in PR gdb/25939, master is
completely broken on Solaris since the multi-target patch.  The patch
applies cleanly to the gdb-9 branch and there I could test it
successfully.

	PR build/25981
	* procfs.c [(__i386__ || __x86_64__) && sun] (proc_get_LDT_entry,
	procfs_find_LDT_entry): Remove.
	* procfs.h [(__i386__ || __x86_64__) && sun] (struct ssd,
	procfs_find_LDT_entry): Remove.
	* sol-thread.c [(__i386__ || __x86_64__) && sun] (ps_lgetLDT):
	Remove.
2020-05-18 17:56:00 +02:00
Pedro Alves 7f32a4d5ae Stop considering hw and sw breakpoint locations duplicates (PR gdb/25741)
In the following conditions:

  - A target with hardware breakpoints available, and
  - A target that uses software single stepping,
  - An instruction at ADDRESS loops back to itself,

Now consider the following steps:

  1. The user places a hardware breakpoint at ADDRESS (an instruction
  that loops to itself),

  2. The inferior runs and hits the breakpoint at ADDRESS,

  3. The user tells GDB to 'continue'.

In #3 when the user tells GDB to continue, GDB first disables the
hardware breakpoint at ADDRESS, and then inserts a software
single-step breakpoint at ADDRESS.  The original user-created
breakpoint was a hardware breakpoint, while the single-step breakpoint
will be a software breakpoint.

GDB continues and immediately hits the software single-step
breakpoint.

GDB then deletes the software single-step breakpoint by calling
delete_single_step_breakpoints, which eventually calls
delete_breakpoint, which, once the breakpoint (and its locations) are
deleted, calls update_global_location_list.

During update_global_location_list GDB spots that we have an old
location (the software single step breakpoint location) that is
inserted, but being deleted, and a location (the original hardware
breakpoint) at the same address which we are keeping, but which is not
currently inserted, GDB then calls breakpoint_locations_match on these
two locations.

Currently the locations do match, and so GDB calls swap_insertion
which swaps the "inserted" state of the two locations.  The user
created hardware breakpoint is marked as inserted, while the GDB
internal software single step breakpoint is now marked as not
inserted.  After this GDB returns through the call stack and leaves
delete_single_step_breakpoints.

After this GDB continues with its normal "stopping" process, as part
of this stopping process GDB removes all the breakpoints from the
target.  Due to the swap it is now the user-created hardware
breakpoint that is marked as inserted, so it is this breakpoint GDB
tries to remove.

The problem is that GDB inserted the software single-step breakpoint
as a software breakpoint, but is now trying to remove the hardware
breakpoint.  The problem is removing a software breakpoint is very
different to removing a hardware breakpoint, this could result is some
undetected undefined behaviour, or as in the original bug report (PR
gdb/25741), could result in the target throwing an error.

With "set breakpoint always-inserted on", we can easily reproduce this
against GDBserver.  E.g.:

  (gdb) hbreak main
  Sending packet: $m400700,40#28...Packet received: 89e58b....
  Sending packet: $m400736,1#fe...Packet received: 48
  Hardware assisted breakpoint 1 at 0x400736: file threads.c, line 57.
  Sending packet: $Z1,400736,1#48...Packet received: OK
  Packet Z1 (hardware-breakpoint) is supported

  (gdb) b main
  Note: breakpoint 1 also set at pc 0x400736.
  Sending packet: $m400736,1#fe...Packet received: 48
  Breakpoint 2 at 0x400736: file threads.c, line 57.

  (gdb) del
  Delete all breakpoints? (y or n) y
  Sending packet: $z0,400736,1#67...Packet received: E01
  warning: Error removing breakpoint 2

This patch adds a testcase that does exactly that.

Trying to enhance GDB to handle this scenario while continuing to
avoid inserting redundant software and hardware breakpoints at the
same address turns out futile, because, given non-stop and breakpoints
always-inserted, if the user:

 #1 - inserts a hw breakpoint, then
 #2 - inserts a sw breakpoint at the same address, and then
 #3 - removes the original hw breakpoint,

GDB would have to make sure to insert the sw breakpoint before
removing the hw breakpoint, to avoid running threads missing the
breakpoint.  I.e., there's always going to be a window where a target
needs to be able to handle both sw and a hw breakpoints installed at
the same address.  You can see more detailed description of that issue
here:
https://sourceware.org/pipermail/gdb-patches/2020-April/167738.html

So the fix here is to just stop considering software breakpoints and
hw breakpoints duplicates, and let GDB insert sw and hw breakpoints at
the same address.

The central change is to make breakpoint_locations_match consider the
location's type too.  There are several other changes necessary to
actually make that that work correctly, however:

- We need to handle the duplicates detection better.  Take a look at
  the loop at the tail end of update_global_location_list.  Currently,
  because breakpoint locations aren't sorted by type, we can end up
  with, at the same address, a sw break, then a hw break, then a sw
  break, etc.  The result is that that second sw break won't be
  considered a duplicate of the first sw break.  Seems like we already
  handle that incorrectly for range breakpoints.

- The "set breakpoint auto-hw on" handling is moved out of
  insert_bp_location to update_global_location_list, before the
  duplicates determination.

  Moving "set breakpoint auto-hw off" handling as well and downgrading
  it to a warning+'disabling the location' was considered too, but in
  the end discarded, because we want to error out with internal and
  momentary breakpoints, like software single-step breakpoints.
  Disabling such locations at update_global_location_list time would
  make GDB lose control of the inferior.

- In update_breakpoint_locations, the logic of matching old locations
  with new locations, in the have_ambiguous_names case, is updated to
  still consider sw vs hw locations the same.

- Review all ALL_BP_LOCATIONS_AT_ADDR uses, and update those that
  might need to be updated, and update comments for those that don't.
  Note that that macro walks all locations at a given address, and
  doesn't call breakpoint_locations_match.

The result against GDBserver (with "set breakpoint
condition-evaluation host" to avoid seeing confusing reinsertions) is:

 (gdb) hbreak main
 Sending packet: $m400736,1#fe...Packet received: 48
 Hardware assisted breakpoint 1 at 0x400736: file main.c, line 57.
 Sending packet: $Z1,400736,1#48...Packet received: OK

 (gdb) b main
 Note: breakpoint 1 also set at pc 0x400736.
 Sending packet: $m400736,1#fe...Packet received: 48
 Breakpoint 4 at 0x400736: file main.c, line 57.
 Sending packet: $Z0,400736,1#47...Packet received: OK

 (gdb) del 3
 Sending packet: $z1,400736,1#68...Packet received: OK

gdb/ChangeLog:
2020-05-17  Pedro Alves  <palves@redhat.com>
	    Andrew Burgess  <andrew.burgess@embecosm.com>
	    Keno Fischer  <keno@juliacomputing.com>

	PR gdb/25741
	* breakpoint.c (build_target_condition_list): Update comments.
	(build_target_command_list): Update comments and skip matching
	locations.
	(insert_bp_location): Move "set breakpoint auto-hw on" handling to
	a separate function.  Simplify "set breakpoint auto-hw off"
	handling.
	(insert_breakpoints): Update comment.
	(tracepoint_locations_match): New parameter.  For breakpoints,
	compare location types too, if the caller wants to.
	(handle_automatic_hardware_breakpoints): New functions.
	(bp_location_is_less_than): Also sort by location type and
	hardware breakpoint length.
	(update_global_location_list): Handle "set breakpoint auto-hw on"
	here.
	(update_breakpoint_locations): Ask breakpoint_locations_match to
	ignore location types.

gdb/testsuite/ChangeLog:
2020-05-17  Pedro Alves  <palves@redhat.com>

	PR gdb/25741
	* gdb.base/hw-sw-break-same-address.exp: New file.
2020-05-17 19:17:56 +01:00
Simon Marchi 7d93a1e0b6 gdb: remove TYPE_NAME macro
Remove `TYPE_NAME`, changing all the call sites to use `type::name`
directly.  This is quite a big diff, but this was mostly done using sed
and coccinelle.  A few call sites were done by hand.

gdb/ChangeLog:

	* gdbtypes.h (TYPE_NAME): Remove.  Change all cal sites to use
	type::name instead.
2020-05-16 12:36:05 -04:00
Simon Marchi d0e39ea27c gdb: add type::name / type::set_name
Add the `name` and `set_name` methods on `struct type`, in order to
remove the `TYPE_NAME` macro.  In this patch, the `TYPE_NAME` macro is
changed to use `type::name`, so all the call sites that are used to set
the type name are changed to use `type::set_name`.  The next patch will
remove `TYPE_NAME` completely.

gdb/ChangeLog:

	* gdbtypes.h (struct type) <name, set_name>: New methods.
	(TYPE_CODE): Use type::name.  Change all call sites used to set
	the name to use type::set_name instead.
2020-05-16 12:36:05 -04:00
Tom Tromey 2dab0c7ba0 Remove ALL_UIS
Continuing my goal of removing the "ALL_*" iterator macros, this
removes ALL_UIS, replacing it with an iterator adaptor.

gdb/ChangeLog
2020-05-16  Tom Tromey  <tom@tromey.com>

	* top.c (quit_force): Update.
	* infrun.c (handle_no_resumed): Update.
	* top.h (all_uis): New function.
	(ALL_UIS): Remove.
2020-05-16 09:58:46 -06:00
Simon Marchi 59f7bd8d2b gdb: fix -Wtautological-overlap-compare warning in mips-linux-tdep.c
When building with clang 11, I get:

  CXX    mips-linux-tdep.o
/home/smarchi/src/binutils-gdb/gdb/mips-linux-tdep.c:643:30: error: overlapping comparisons always evaluate to true [-Werror,-Wtautological-overlap-compare]
      if (insn != 0x03e07821 || insn != 0x03e07825)
          ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~
/home/smarchi/src/binutils-gdb/gdb/mips-linux-tdep.c:636:30: error: overlapping comparisons always evaluate to true [-Werror,-Wtautological-overlap-compare]
      if (insn != 0x03e0782d || insn != 0x03e07825)
          ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~

Indeed, given two different values, `insn` will always be different to
one of them, and these conditions always be true.

This code is meant to return if `insn` isn't one of these two values, so
the `||` should be replaced with `&&`.

gdb/ChangeLog:

	* mips-linux-tdep.c (mips_linux_in_dynsym_stub): Fix condition.
2020-05-16 11:21:41 -04:00