Commit Graph

106 Commits

Author SHA1 Message Date
Simon Marchi
0fb43ab598 gdb/progspace: make program_space::objfiles_list private
This field is only accessed within the program_space class, make it
private.

Change-Id: I0b53d78d3d11adf0dfadfb3ecace33d2996dd87b
2024-11-11 11:28:24 -05:00
Simon Marchi
fa15972b68 gdb/progspace: link objfiles using owning_intrusive_list
This simplifies things a little bit, removing some `find_if` when
inserting or removing objfiles, and the whole
unwrapping_objfile_iterator thing.

Change-Id: Idd1851d36c7834820c9c1639a6a252de643eafba
2024-11-11 11:28:24 -05:00
Simon Marchi
4113c737bd gdb: make program_space::free_all_objfiles use this
Use `this` instead of `current_program_space`.  Presumably, the method
wants to check the solibs of "this" program space, not the current
global program space (although they are likely always the same at the
moment).

Change-Id: Iaf0534f36bfd47c04c53ed0657da332bdb8fb906
Approved-By: Tom Tromey <tom@tromey.com>
Reviewed-By: Thiago Jung Bauermann <thiago.bauermann@linaro.org>
2024-07-15 14:34:12 -04:00
Simon Marchi
b8c9d0de90 gdb: pass program space to no_shared_libraries
Make the current program space reference bubble up one level.  Pass
`current_program_space` everywhere, except in some cases where we can
get the pspace another way, and it's relatively obvious that it's the
same as the current program space.

Change-Id: Id86b79f1e44f92a398f49d137d57457174dfa96d
Approved-By: Tom Tromey <tom@tromey.com>
Reviewed-By: Thiago Jung Bauermann <thiago.bauermann@linaro.org>
2024-07-15 14:34:12 -04:00
Simon Marchi
89dc60d957 gdb: split no_shared_libraries, command vs implementation
The `no_shared_libraries` function is currently used to implement the
`nosharedlibrary` command, but it also used internally by other
functions.  This does not make a very good internal API.

Add the `no_shared_libraries_command` function to implement the CLI
command.  Remove the unused parameters from `no_shared_libraries`.

Remove the `from_tty` parameter of `target_pre_inferior`, since it's now
unused.

Change-Id: I4fcba5ee1e0f7d250aab1a7b62b9ea16265fe962
Approved-By: Tom Tromey <tom@tromey.com>
Reviewed-By: Thiago Jung Bauermann <thiago.bauermann@linaro.org>
2024-07-15 14:34:12 -04:00
Simon Marchi
9ad8c5832d gdb: make progspace::exec_filename private, add getter / setter
Just like the title says... I think this makes things a bit clearer, for
instance where the exec filename is set.  It also makes the read call
sites a bit nicer, avoiding the `.get ()`.

Change-Id: If8b58ae8f6270c8a34b868f6ca06128c6671ea3c
Approved-By: Tom Tromey <tom@tromey.com>
2024-06-07 23:09:03 -04:00
Simon Marchi
f6bbac3f2e gdb: remove unused includes from progspace.{c,h}
Remove some include files reported as unused by clangd.

Change-Id: I39f9d40b9d5bbf040250b41ef258fb8f32dd5c0a
2024-05-16 12:36:52 -04:00
Simon Marchi
5b9707eb87 gdb: remove gdbcmd.h
Most files including gdbcmd.h currently rely on it to access things
actually declared in cli/cli-cmds.h (setlist, showlist, etc).  To make
things easy, replace all includes of gdbcmd.h with includes of
cli/cli-cmds.h.  This might lead to some unused includes of
cli/cli-cmds.h, but it's harmless, and much faster than going through
the 170 or so files by hand.

Change-Id: I11f884d4d616c12c05f395c98bbc2892950fb00f
Approved-By: Tom Tromey <tom@tromey.com>
2024-04-25 12:59:02 -04:00
Simon Marchi
7b21ae941e gdb: move two declarations out of defs.h
Move declarations of initialize_progspace and initialize_inferiors to
progspace.h and inferior.h, respectively.

Change-Id: I62292ffda429861b9f27d8c836a56d161dfa548d
Approved-By: John Baldwin <jhb@FreeBSD.org>
2024-04-22 21:34:19 -04:00
Simon Marchi
18d2988e5d gdb, gdbserver, gdbsupport: remove includes of early headers
Now that defs.h, server.h and common-defs.h are included via the
`-include` option, it is no longer necessary for source files to include
them.  Remove all the inclusions of these files I could find.  Update
the generation scripts where relevant.

Change-Id: Ia026cff269c1b7ae7386dd3619bc9bb6a5332837
Approved-By: Pedro Alves <pedro@palves.net>
2024-03-26 21:13:22 -04:00
Simon Marchi
7b323785ef gdb: rename struct shobj -> struct solib
`struct so_list` was recently renamed to `struct shobj` (in 3fe0dfd160
("gdb: rename struct so_list to shobj")).  In hindsight, `solib` would
have been a better name.  We have solib.c, the implementations in
solib-*.c, many functions with solib in their name, the solib_loaded /
solib_unloaded observables, etc.

Rename shobj to solib.

Change-Id: I0af1c7a9b29bdda027e9af633f6d37e1cfcacd5d
Approved-By: Tom Tromey <tom@tromey.com>
2024-02-05 16:10:15 -05:00
Andrew Burgess
1d506c26d9 Update copyright year range in header of all files managed by GDB
This commit is the result of the following actions:

  - Running gdb/copyright.py to update all of the copyright headers to
    include 2024,

  - Manually updating a few files the copyright.py script told me to
    update, these files had copyright headers embedded within the
    file,

  - Regenerating gdbsupport/Makefile.in to refresh it's copyright
    date,

  - Using grep to find other files that still mentioned 2023.  If
    these files were updated last year from 2022 to 2023 then I've
    updated them this year to 2024.

I'm sure I've probably missed some dates.  Feel free to fix them up as
you spot them.
2024-01-12 15:49:57 +00:00
Tom de Vries
f9582a22db [gdb] Fix segfault in for_each_block, part 1
When running test-case gdb.base/vfork-follow-parent.exp on powerpc64 (likewise
on s390x), I run into:
...
(gdb) PASS: gdb.base/vfork-follow-parent.exp: \
  exec_file=vfork-follow-parent-exit: target-non-stop=on: non-stop=off: \
  resolution_method=schedule-multiple: print unblock_parent = 1
continue^M
Continuing.^M
Reading symbols from vfork-follow-parent-exit...^M
^M
^M
Fatal signal: Segmentation fault^M
----- Backtrace -----^M
0x1027d3e7 gdb_internal_backtrace_1^M
        src/gdb/bt-utils.c:122^M
0x1027d54f _Z22gdb_internal_backtracev^M
        src/gdb/bt-utils.c:168^M
0x1057643f handle_fatal_signal^M
        src/gdb/event-top.c:889^M
0x10576677 handle_sigsegv^M
        src/gdb/event-top.c:962^M
0x3fffa7610477 ???^M
0x103f2144 for_each_block^M
        src/gdb/dcache.c:199^M
0x103f235b _Z17dcache_invalidateP13dcache_struct^M
        src/gdb/dcache.c:251^M
0x10bde8c7 _Z24target_dcache_invalidatev^M
        src/gdb/target-dcache.c:50^M
...
or similar.

The root cause for the segmentation fault is that linux_is_uclinux gives an
incorrect result: it should always return false, given that we're running on a
regular linux system, but instead it returns first true, then false.

In more detail, the segmentation fault happens as follows:
- a program space with an address space is created
- a second program space is about to be created. maybe_new_address_space
  is called, and because linux_is_uclinux returns true, maybe_new_address_space
  returns false, and no new address space is created
- a second program space with the same address space is created
- a program space is deleted. Because linux_is_uclinux now returns false,
  gdbarch_has_shared_address_space (current_inferior ()->arch ()) returns
  false, and the address space is deleted
- when gdb uses the address space of the remaining program space, we run into
  the segfault, because the address space is deleted.

Hardcoding linux_is_uclinux to false makes the test-case pass.

We leave addressing the root cause for the following commit in this series.

For now, prevent the segmentation fault by making the address space a refcounted
object.

This was already suggested here [1]:
...
A better solution might be to have the address spaces be reference counted
...

Tested on top of trunk on x86_64-linux and ppc64le-linux.
Tested on top of gdb-14-branch on ppc64-linux.

Co-Authored-By: Simon Marchi <simon.marchi@polymtl.ca>

PR gdb/30547
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30547

[1] https://sourceware.org/pipermail/gdb-patches/2023-October/202928.html
2023-11-28 10:31:25 +01:00
Simon Marchi
0e17d3fc08 gdb: fix owner passed to remove_target_sections in clear_solib
Commit 8971d2788e ("gdb: link so_list using intrusive_list") introduced
a bug in clear_solib.  Instead of passing an `so_list *` to
remove_target_sections, it passed an `so_list **`.  This was not caught
by the compiler, because remove_target_sections takes a `void *` as the
"owner", so you can pass it any pointer and it won't complain.

This happened because I previously had a patch to change the type of the
disposer parameter to be a reference rather than a pointer, so had to
change `so` to `&so`.  When dropping that patch, I forgot to revert this
bit and / or it got re-introduced when handling subsequent merge
conflicts.  And I didn't properly retest.

Fix that, but try to make things less error prone.  Add a union to
represent the possible owner kinds for a target_section.  Trying to pass
a pointer to another type than those will not compile.

Change-Id: I600cab5ea0408ccc5638467b760768161ca3036c
2023-10-20 22:00:23 -04:00
Simon Marchi
3fe0dfd160 gdb: rename struct so_list to shobj
Now that so_list lists are implemented using intrusive_list, it doesn't
really make sense for the element type to be named "_list".  Rename to
just `struct shobj` (`struct so` was deemed to be not greppable enough).

Change-Id: I1063061901298bb40fee73bf0cce44cd12154c0e
Approved-By: Pedro Alves <pedro@palves.net>
Reviewed-By: Reviewed-By: Lancelot Six <lancelot.six@amd.com>
2023-10-19 11:14:26 -04:00
Simon Marchi
8971d2788e gdb: link so_list using intrusive_list
Replace the hand-made linked list implementation with intrusive_list,
simplying management of list items.

Change-Id: I7f55fd88325bb197cc655c9be5a2ec966d8cc48d
Approved-By: Pedro Alves <pedro@palves.net>
Reviewed-By: Reviewed-By: Lancelot Six <lancelot.six@amd.com>
2023-10-19 10:57:51 -04:00
Simon Marchi
99d9c3b92c gdb: remove target_gdbarch
This function is just a wrapper around the current inferior's gdbarch.
I find that having that wrapper just obscures where the arch is coming
from, and that it's often used as "I don't know which arch to use so
I'll use this magical target_gdbarch function that gets me an arch" when
the arch should in fact come from something in the context (a thread,
objfile, symbol, etc).  I think that removing it and inlining
`current_inferior ()->arch ()` everywhere will make it a bit clearer
where that arch comes from and will trigger people into reflecting
whether this is the right place to get the arch or not.

Change-Id: I79f14b4e4934c88f91ca3a3155f5fc3ea2fadf6b
Reviewed-By: John Baldwin <jhb@FreeBSD.org>
Approved-By: Andrew Burgess <aburgess@redhat.com>
2023-10-10 10:44:35 -04:00
Andrew Burgess
59912fb2d2 gdb: add Python events for program space addition and removal
Initially I just wanted a Python event for when GDB removes a program
space, I'm writing a Python extension that caches information for each
program space, and need to know when I should discard entries for a
particular program space.

But, it seemed easy enough to also add an event for when GDB adds a
new program space, so I went ahead and added both new events.

Of course, we don't currently have an observable for program space
addition or removal, so I first needed to add these.  After that it's
pretty simple to add two new Python events and have these trigger.

The two new event registries are:

  events.new_progspace
  events.free_progspace

These emit NewProgspaceEvent and FreeProgspaceEvent objects
respectively, each of these new event types has a 'progspace'
attribute that contains the relevant gdb.Progspace object.

There's a couple of things to be mindful of.

First, it is not possible to catch the NewProgspaceEvent for the very
first program space, the one that is created when GDB first starts, as
this program space is created before any Python scripts are sourced.

In order to allow this event to be caught we would need to defer
creating the first program space, and as a consequence the first
inferior, until some later time.  But, existing scripts could easily
depend on there being an initial inferior, so I really don't think we
should change that -- and so, we end up with the consequence that we
can't catch the event for the first program space.

The second, I think minor, issue, is that GDB doesn't clean up its
program spaces upon exit -- or at least, they are not cleaned up
before Python is shut down.  As a result, any program spaces in use at
the time GDB exits don't generate a FreeProgspaceEvent.  I'm not
particularly worried about this for my use case, I'm using the event
to ensure that a cache doesn't hold stale entries within a single GDB
session.  It's also easy enough to add a Python at-exit callback which
can do any final cleanup if needed.

Finally, when testing, I did hit a slightly weird issue with some of
the remote boards (e.g. remote-stdio-gdbserver).  As a consequence of
this issue I see some output like this in the gdb.log:

  (gdb) PASS: gdb.python/py-progspace-events.exp: inferior 1
  step
  FreeProgspaceEvent: <gdb.Progspace object at 0x7fb7e1d19c10>
  warning: cannot close "target:/lib64/libm.so.6": Cannot execute this command while the target is running.
  Use the "interrupt" command to stop the target
  and then try again.
  warning: cannot close "target:/lib64/libc.so.6": Cannot execute this command while the target is running.
  Use the "interrupt" command to stop the target
  and then try again.
  warning: cannot close "target:/lib64/ld-linux-x86-64.so.2": Cannot execute this command while the target is running.
  Use the "interrupt" command to stop the target
  and then try again.
  do_parent_stuff () at py-progspace-events.c:41
  41        ++global_var;
  (gdb) PASS: gdb.python/py-progspace-events.exp: step

The 'FreeProgspaceEvent ...' line is expected, that's my test Python
extension logging the event.  What isn't expected are all the blocks
like:

  warning: cannot close "target:/lib64/libm.so.6": Cannot execute this command while the target is running.
  Use the "interrupt" command to stop the target
  and then try again.

It turns out that this has nothing to do with my changes, this is just
a consequence of reading files over the remote protocol.  The test
forks a child process which GDB stays attached too.  When the child
exits, GDB cleans up by calling prune_inferiors, which in turn can
result in GDB trying to close some files that are open because of the
inferior being deleted.

If the prune_inferiors call occurs when the remote target is
running (and in non-async mode) then GDB will try to send a fileio
packet while the remote target is waiting for a stop reply, and the
remote target will throw an error, see remote_target::putpkt_binary in
remote.c for details.

I'm going to look at fixing this, but, as I said, this is nothing to
do with this change, I just mention it because I ended up needing to
account for these warning messages in one of my tests, and it all
looks a bit weird.

Approved-By: Tom Tromey <tom@tromey.com>
Reviewed-By: Eli Zaretskii <eliz@gnu.org>
2023-10-02 17:06:40 +01:00
Tom Tromey
27b2eff1b8 Add Progspace.objfile_for_address
This adds a new objfile_for_address method to gdb.Progspace.  This
makes it easy to find the objfile for a given address.

There's a related PR; and while this change would have been sufficient
for my original need, it's not clear to me whether I should close the
bug.  Nevertheless I think it makes sense to at least mention it here.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=19288
Reviewed-By: Eli Zaretskii <eliz@gnu.org>
2023-07-21 12:05:30 -06:00
Joel Brobecker
213516ef31 Update copyright year range in header of all files managed by GDB
This commit is the result of running the gdb/copyright.py script,
which automated the update of the copyright year range for all
source files managed by the GDB project to be updated to include
year 2023.
2023-01-01 17:01:16 +04:00
Andrew Burgess
5d80df4a10 gdb/maint: add core file name to 'maint info program-spaces' output
Each program space can have an associated core file.  Include this
information in the output of 'maint info program-spaces'.
2022-12-14 13:57:22 +00:00
Tom Tromey
08b8a139c9 Rewrite registry.h
This rewrites registry.h, removing all the macros and replacing it
with relatively ordinary template classes.  The result is less code
than the previous setup.  It replaces large macros with a relatively
straightforward C++ class, and now manages its own cleanup.

The existing type-safe "key" class is replaced with the equivalent
template class.  This approach ended up requiring relatively few
changes to the users of the registry code in gdb -- code using the key
system just required a small change to the key's declaration.

All existing users of the old C-like API are now converted to use the
type-safe API.  This mostly involved changing explicit deletion
functions to be an operator() in a deleter class.

The old "save/free" two-phase process is removed, and replaced with a
single "free" phase.  No existing code used both phases.

The old "free" callbacks took a parameter for the enclosing container
object.  However, this wasn't truly needed and is removed here as
well.
2022-07-28 14:16:50 -06:00
Tom Tromey
b382c16682 Change address_space to use new and delete
This changes address_space to use new and delete, and makes some other
small C++-ification changes as well, like changing address_space_num
to be a method.

This patch was needed for the subsequent patch to rewrite the registry
system.
2022-07-28 14:16:50 -06:00
Tom Tromey
e2904e1ff0 Use unique_ptr for objfiles
A while back, I changed objfiles to be held via a shared_ptr.  The
idea at the time was that this was a step toward writing to the index
cache in the background, and this would let gdb keep a reference alive
to do so.  However, since then we've rewritten the DWARF reader, and
the new index can do this without requiring a shared pointer -- in
fact there are patches pending to implement this.

This patch switches objfile management to unique_ptr, which makes more
sense now.

Regression tested on x86-64 Fedora 34.
2022-05-31 14:46:11 -06:00
Tom Tromey
6cb06a8cda Unify gdb printf functions
Now that filtered and unfiltered output can be treated identically, we
can unify the printf family of functions.  This is done under the name
"gdb_printf".  Most of this patch was written by script.
2022-03-29 12:46:24 -06:00
Tom Tromey
972f7a4b97 Add more filename styling
I found a few spots where filename styling ought to be applied, but is
not.
2022-02-28 11:33:30 -07:00
Joel Brobecker
4a94e36819 Automatic Copyright Year update after running gdb/copyright.py
This commit brings all the changes made by running gdb/copyright.py
as per GDB's Start of New Year Procedure.

For the avoidance of doubt, all changes in this commits were
performed by the script.
2022-01-01 19:13:23 +04:00
Pedro Alves
08bdefb58b gdb: make inferior_list use intrusive_list
Change inferior_list, the global list of inferiors, to use
intrusive_list.  I think most other changes are somewhat obvious
fallouts from this change.

There is a small change in behavior in scoped_mock_context.  Before this
patch, constructing a scoped_mock_context would replace the whole
inferior list with only the new mock inferior.  Tests using two
scoped_mock_contexts therefore needed to manually link the two inferiors
together, as the second scoped_mock_context would bump the first mock
inferior from the thread list.  With this patch, a scoped_mock_context
adds its mock inferior to the inferior list on construction, and removes
it on destruction.  This means that tests run with mock inferiors in the
inferior list in addition to any pre-existing inferiors (there is always
at least one).  There is no possible pid clash problem, since each
scoped mock inferior uses its own process target, and pids are per
process target.

Co-Authored-By: Simon Marchi <simon.marchi@efficios.com>
Change-Id: I7eb6a8f867d4dcf8b8cd2dcffd118f7270756018
2021-07-12 20:46:52 -04:00
Simon Marchi
9be259865c gdb: introduce iterator_range, remove next_adapter
I was always a bit confused by next_adapter, because it kind of mixes
the element type and the iterator type.  In reality, it is not much more
than a class that wraps two iterators (begin and end).  However, it
assumes that:

 - you can construct the begin iterator by passing a pointer to the
   first element of the iterable
 - you can default-construct iterator to make the end iterator

I think that by generalizing it a little bit, we can re-use it at more
places.

Rename it to "iterator_range".  I think it describes a bit better: it's
a range made by wrapping a begin and end iterator.  Move it to its own
file, since it's not related to next_iterator anymore.

iterator_range has two constructors.  The variadic one, where arguments
are forwarded to construct the underlying begin iterator.  The end
iterator is constructed through default construction.  This is a
generalization of what we have today.

There is another constructor which receives already constructed begin
and end iterators, useful if the end iterator can't be obtained by
default-construction.  Or, if you wanted to make a range that does not
end at the end of the container, you could pass any iterator as the
"end".

This generalization allows removing some "range" classes, like
all_inferiors_range.  These classes existed only to pass some arguments
when constructing the begin iterator.  With iterator_range, those same
arguments are passed to the iterator_range constructed and then
forwarded to the constructed begin iterator.

There is a small functional difference in how iterator_range works
compared to next_adapter.  next_adapter stored the pointer it received
as argument and constructeur an iterator in the `begin` method.
iterator_range constructs the begin iterator and stores it as a member.
Its `begin` method returns a copy of that iterator.

With just iterator_range, uses of next_adapter<foo> would be replaced
with:

  using foo_iterator = next_iterator<foo>;
  using foo_range = iterator_range<foo_iterator>;

However, I added a `next_range` wrapper as a direct replacement for
next_adapter<foo>.  IMO, next_range is a slightly better name than
next_adapter.

The rest of the changes are applications of this new class.

gdbsupport/ChangeLog:

	* next-iterator.h (class next_adapter): Remove.
	* iterator-range.h: New.

gdb/ChangeLog:

	* breakpoint.h (bp_locations_range): Remove.
	(bp_location_range): New.
	(struct breakpoint) <locations>: Adjust type.
	(breakpoint_range): Use iterator_range.
	(tracepoint_range): Use iterator_range.
	* breakpoint.c (breakpoint::locations): Adjust return type.
	* gdb_bfd.h (gdb_bfd_section_range): Use iterator_range.
	* gdbthread.h (all_threads_safe): Pass argument to
	all_threads_safe_range.
	* inferior-iter.h (all_inferiors_range): Use iterator_range.
	(all_inferiors_safe_range): Use iterator_range.
	(all_non_exited_inferiors_range): Use iterator_range.
	* inferior.h (all_inferiors, all_non_exited_inferiors): Pass
	inferior_list as argument.
	* objfiles.h (struct objfile) <compunits_range>: Remove.
	<compunits>: Return compunit_symtab_range.
	* progspace.h (unwrapping_objfile_iterator)
	<unwrapping_objfile_iterator>: Take parameter by value.
	(unwrapping_objfile_range): Use iterator_range.
	(struct program_space) <objfiles_range>: Define with "using".
	<objfiles>: Adjust.
	<objfiles_safe_range>: Define with "using".
	<objfiles_safe>: Adjust.
	<solibs>: Return so_list_range, define here.
	* progspace.c (program_space::solibs): Remove.
	* psymtab.h (class psymtab_storage) <partial_symtab_iterator>:
	New.
	<partial_symtab_range>: Use iterator_range.
	* solist.h (so_list_range): New.
	* symtab.h (compunit_symtab_range):
	New.
	(symtab_range): New.
	(compunit_filetabs): Change to a function.
	* thread-iter.h (inf_threads_range,
	inf_non_exited_threads_range, safe_inf_threads_range,
	all_threads_safe_range): Use iterator_range.
	* top.h (ui_range): New.
	(all_uis): Use ui_range.

Change-Id: Ib7a9d2a3547f45f01aa1c6b24536ba159db9b854
2021-07-06 15:02:05 -04:00
Simon Marchi
6bd434d6ca gdb: make some variables static
I'm trying to enable clang's -Wmissing-variable-declarations warning.
This patch fixes all the obvious spots where we can simply add "static"
(at least, found when building on x86-64 Linux).

gdb/ChangeLog:

	* aarch64-linux-tdep.c (aarch64_linux_record_tdep): Make static.
	* aarch64-tdep.c (tdesc_aarch64_list, aarch64_prologue_unwind,
	aarch64_stub_unwind, aarch64_normal_base, ): Make static.
	* arm-linux-tdep.c (arm_prologue_unwind): Make static.
	* arm-tdep.c (struct frame_unwind): Make static.
	* auto-load.c (auto_load_safe_path_vec): Make static.
	* csky-tdep.c (csky_stub_unwind): Make static.
	* gdbarch.c (gdbarch_data_registry): Make static.
	* gnu-v2-abi.c (gnu_v2_abi_ops): Make static.
	* i386-netbsd-tdep.c (i386nbsd_mc_reg_offset): Make static.
	* i386-tdep.c (i386_frame_setup_skip_insns,
	i386_tramp_chain_in_reg_insns, i386_tramp_chain_on_stack_insns):
	Make static.
	* infrun.c (observer_mode): Make static.
	* linux-nat.c (sigchld_action): Make static.
	* linux-thread-db.c (thread_db_list): Make static.
	* maint-test-options.c (maintenance_test_options_list):
	* mep-tdep.c (mep_csr_registers): Make static.
	* mi/mi-cmds.c (struct mi_cmd_stats): Remove struct type name.
	(stats): Make static.
	* nat/linux-osdata.c (struct osdata_type): Make static.
	* ppc-netbsd-tdep.c (ppcnbsd_reg_offsets): Make static.
	* progspace.c (last_program_space_num): Make static.
	* python/py-param.c (struct parm_constant): Remove struct type
	name.
	(parm_constants): Make static.
	* python/py-record-btrace.c (btpy_list_methods): Make static.
	* python/py-record.c (recpy_gap_type): Make static.
	* record.c (record_goto_cmdlist): Make static.
	* regcache.c (regcache_descr_handle): Make static.
	* registry.h (DEFINE_REGISTRY): Make definition static.
	* symmisc.c (std_in, std_out, std_err): Make static.
	* top.c (previous_saved_command_line): Make static.
	* tracepoint.c (trace_user, trace_notes, trace_stop_notes): Make
	static.
	* unittests/command-def-selftests.c (nr_duplicates,
	nr_invalid_prefixcmd, lists): Make static.
	* unittests/observable-selftests.c (test_notification): Make
	static.
	* unittests/optional/assignment/1.cc (counter): Make static.
	* unittests/optional/assignment/2.cc (counter): Make static.
	* unittests/optional/assignment/3.cc (counter): Make static.
	* unittests/optional/assignment/4.cc (counter): Make static.
	* unittests/optional/assignment/5.cc (counter): Make static.
	* unittests/optional/assignment/6.cc (counter): Make static.

gdbserver/ChangeLog:

	* ax.cc (bytecode_address_table): Make static.
	* debug.cc (debug_file): Make static.
	* linux-low.cc (stopping_threads): Make static.
	(step_over_bkpt): Make static.
	* linux-x86-low.cc (amd64_emit_ops, i386_emit_ops): Make static.
	* tracepoint.cc (stop_tracing_bkpt, flush_trace_buffer_bkpt,
	alloced_trace_state_variables, trace_buffer_ctrl,
	tracing_start_time, tracing_stop_time, tracing_user_name,
	tracing_notes, tracing_stop_note): Make static.

Change-Id: Ic1d8034723b7802502bda23770893be2338ab020
2021-01-20 20:55:05 -05:00
Joel Brobecker
3666a04883 Update copyright year range in all GDB files
This commits the result of running gdb/copyright.py as per our Start
of New Year procedure...

gdb/ChangeLog

        Update copyright year range in copyright header of all GDB files.
2021-01-01 12:12:21 +04:00
Tom Tromey
8407f91bd3 Remove call to exec_close
There's no need to call exec_close from ~progspace, because that
method just does some cleanup that's already going to be done during
destruction.  This patch removes the call.

gdb/ChangeLog
2020-10-29  Tom Tromey  <tom@tromey.com>

	* progspace.c (program_space::~program_space): Don't call
	exec_close.
2020-10-29 15:04:41 -06:00
Tom Tromey
004eecfdc7 Change program_space_empty_p to method on program_space
This changes program_space_empty_p to be a method on program_space.
It also changes it to return bool.  I removed the "_p" suffix because
"empty" is a "well-known" C++ method name.

gdb/ChangeLog
2020-10-29  Tom Tromey  <tom@tromey.com>

	* inferior.c (delete_inferior): Update.
	* progspace.c (program_space::empty): Rename from
	program_space_empty_p.  Return bool.
	* progspace.h (struct program_space) <empty>: New method.
	(program_space_empty_p): Don't declare.
2020-10-29 15:04:38 -06:00
Tom Tromey
e39fb97114 Change clear_program_space_solib_cache to method on program_space
This changes clear_program_space_solib_cache to be a method on
program_space.  Also, it removes a call to this function from the
program_space destructor, as that is not necessary.

gdb/ChangeLog
2020-10-29  Tom Tromey  <tom@tromey.com>

	* progspace.c (program_space::~program_space): Don't call
	clear_program_space_solib_cache.
	(program_space::clear_solib_cache): Rename from
	clear_solib_cache.
	* solib.c (handle_solib_event): Update.
	* progspace.h (struct program_space) <clear_solib_cache>: New
	method.
	(clear_program_space_solib_cache): Don't declare.
2020-10-29 15:04:38 -06:00
Tom Tromey
19f6550ebb Change program_space::ebfd to a gdb_bfd_ref_ptr
This changes program_space::ebfd to a gdb_bfd_ref_ptr, removing some
manual management.

gdb/ChangeLog
2020-10-29  Tom Tromey  <tom@tromey.com>

	* exec.c (exec_file_attach): Update.
	* progspace.c (program_space::exec_close): Update.
	* progspace.h (struct program_space) <ebfd>: Now a
	gdb_bfd_ref_ptr.
	<set_exec_bfd>: Change argument type.
	<exec_bfd>: Update.
2020-10-29 15:04:37 -06:00
Tom Tromey
7e10abd146 Remove the exec_bfd macro
This removes the exec_bfd macro, in favor of new accessors on
program_space.  In one spot the accessor can't be used; but this is
still a big improvement over the macro, IMO.

gdb/ChangeLog
2020-10-29  Tom Tromey  <tom@tromey.com>

	* windows-tdep.c (windows_solib_create_inferior_hook): Update.
	* symfile.c (reread_symbols): Update.
	* symfile-mem.c (add_symbol_file_from_memory_command)
	(add_vsyscall_page): Update.
	* source-cache.c (source_cache::get_plain_source_lines): Update.
	* solib-svr4.c (find_program_interpreter, elf_locate_base)
	(svr4_current_sos_direct, svr4_exec_displacement)
	(svr4_relocate_main_executable): Update.
	(svr4_iterate_over_objfiles_in_search_order): Update.
	* solib-frv.c (enable_break2, enable_break): Update.
	* solib-dsbt.c (lm_base, enable_break): Update.
	* solib-darwin.c (find_program_interpreter)
	(darwin_solib_create_inferior_hook): Update.
	* sol-thread.c (rw_common, ps_pdmodel): Update.
	* rs6000-nat.c (rs6000_nat_target::create_inferior): Update.
	* remote.c (compare_sections_command)
	(remote_target::trace_set_readonly_regions): Update.
	* remote-sim.c (get_sim_inferior_data)
	(gdbsim_target::create_inferior, gdbsim_target::create_inferior): Update.
	(gdbsim_target_open, gdbsim_target::files_info): Update.
	* exec.h (exec_bfd): Remove macro.
	* progspace.c (initialize_progspace): Update.
	* proc-service.c (ps_addr_to_core_addr, core_addr_to_ps_addr):
	Update.
	* nto-procfs.c (nto_procfs_target::post_attach)
	(nto_procfs_target::create_inferior): Update.
	* maint.c (maintenance_info_sections): Update.
	* linux-thread-db.c (thread_db_target::get_thread_local_address):
	Update.
	* infcmd.c (post_create_inferior): Update.
	* gcore.c (default_gcore_arch, default_gcore_target): Update.
	(objfile_find_memory_regions): Update.
	* exec.c (validate_exec_file, exec_file_attach)
	(exec_read_partial_read_only, print_section_info): Update.
	* corelow.c (core_target_open): Update.
	* corefile.c (reopen_exec_file, validate_files): Update.
	* arm-tdep.c (gdb_print_insn_arm): Update.
	* arch-utils.c (gdbarch_update_p, default_print_insn): Update.
	* progspace.h (struct program_space) <exec_bfd, set_exec_bfd>: New
	methods.
2020-10-29 15:04:36 -06:00
Tom Tromey
8a4f140205 Change exec_close to be a method on program_space
exec_close uses the current program space, so it seemed cleaner to
change it to be a method on program_space.  This patch makes this
change.

gdb/ChangeLog
2020-10-29  Tom Tromey  <tom@tromey.com>

	* progspace.c (program_space::exec_close): New method, from
	exec_close in exec.c.
	* exec.c (exec_close): Move to progspace.c.
	(exec_target::close, exec_file_attach): Update.
	* progspace.h (struct program_space) <exec_close>: Declare
	method.
2020-10-29 15:04:34 -06:00
Tom Tromey
c20cb6860c Remove exec_filename macro
This removes the exec_filename macro, replacing it with uses of the
member of current_program_space.  This also renames that member, and
changes it to be a unique pointer.

gdb/ChangeLog
2020-10-29  Tom Tromey  <tom@tromey.com>

	* progspace.h (struct program_space) <exec_filename>: Rename from
	pspace_exec_filename.  Now a unique_xmalloc_ptr.
	* inferior.c (print_selected_inferior): Update.
	(print_inferior): Update.
	* mi/mi-main.c (print_one_inferior): Update.
	* exec.h (exec_filename): Remove macro.
	* corefile.c (get_exec_file): Update.
	* exec.c (exec_close): Update.
	(exec_file_attach): Update.
	* progspace.c (clone_program_space): Update.
	(print_program_space): Update.
2020-10-29 15:04:34 -06:00
Tom Tromey
eda214cec1 Remove clear_section_table
The call to clear_section_table in ~program_space is now clearly not
needed -- the section table will clear itself.  This patch removes
this call and then inlines the one remaining call to
clear_section_table.

gdb/ChangeLog
2020-10-12  Tom Tromey  <tom@tromey.com>

	* progspace.c (program_space::~program_space): Don't call
	clear_section_table.
	* exec.h (clear_section_table): Don't declare.
	* exec.c (exec_target::close): Update.
	(clear_section_table): Remove.
2020-10-12 20:18:48 -06:00
Pedro Alves
f7c7700d32 Adjust "maint info program-spaces" to per-inferior target stack
By inspection, I noticed that print_program_space is calling
target_pid_to_str on the wrong target stack.  Most targets print a
process pid the same way, so it isn't actually visible.

gdb/ChangeLog:

	* progspace.c (print_program_space): Use all_inferiors.  Switch to
	the inferior before calling target_pid_to_str.
2020-08-29 00:55:58 +01:00
Pedro Alves
27c7b875bd gdb.base/corefile.exp regression for unix/-m32 on x86_64 (PR 26336)
gdb.base/corefile.exp is showing an unexpected failure and an
unresolved testcase when testing against unix/-m32:

 (gdb) PASS: gdb.base/corefile.exp: attach: sanity check we see the core file
 attach 15741
 gdb/dwarf2-frame.c:1009: internal-error: dwarf2_frame_cache* dwarf2_frame_cache(frame_info*, void**): Assertion `fde != NULL' failed.
 A problem internal to GDB has been detected,
 further debugging may prove unreliable.
 Quit this debugging session? (y or n) FAIL: gdb.base/corefile.exp: attach: with core (GDB internal error)
 Resyncing due to internal error.

This regressed with:

 From 5b6d1e4fa4 Mon Sep 17 00:00:00 2001
 From: Pedro Alves <palves@redhat.com>
 Date: Fri, 10 Jan 2020 20:06:08 +0000
 Subject: [PATCH] Multi-target support

The assertion is here:

 #0  internal_error (file=0xbffffccb0 <error: Cannot access memory at address 0xbffffccb0>, line=0, fmt=0x555556327320 "en_US.UTF-8") at sr
 c/gdbsupport/errors.cc:51
 #1  0x00005555557d4e45 in dwarf2_frame_cache (this_frame=0x55555672f950, this_cache=0x55555672f968) at src/gdb/dwarf2/frame.c:1013
 #2  0x00005555557d5886 in dwarf2_frame_this_id (this_frame=0x55555672f950, this_cache=0x55555672f968, this_id=0x55555672f9b0) at src/gdb/d
 warf2/frame.c:1226
 #3  0x00005555558b184e in compute_frame_id (fi=0x55555672f950) at src/gdb/frame.c:558
 #4  0x00005555558b19b2 in get_frame_id (fi=0x55555672f950) at src/gdb/frame.c:588
 #5  0x0000555555bda338 in scoped_restore_current_thread::scoped_restore_current_thread (this=0x7fffffffd0d8) at src/gdb/thread.c:1458
 #6  0x00005555556ce41f in scoped_restore_current_pspace_and_thread::scoped_restore_current_pspace_and_thread (During symbol reading: .debug_line address at offset 0x1db2d3
 is 0 [in module /home/pedro/gdb/cascais-builds/binutils-gdb/gdb/gdb]
 this=0x7fffffffd0d0) at src/gdb/progspace-and-thread.h:29
 #7  0x0000555555898ea6 in remove_target_sections (owner=0x555556935550) at src/gdb/exec.c:798
 #8  0x0000555555b700b6 in symfile_free_objfile (objfile=0x555556935550) at src/gdb/symfile.c:3742
 #9  0x000055555565050e in std::_Function_handler<void (objfile*), void (*)(objfile*)>::_M_invoke(std::_Any_data const&, objfile*&&) (__functor=..., __args#0=@0x7fffffffd190
 : 0x555556935550) at /usr/include/c++/9/bits/std_function.h:300
 #10 0x0000555555a3053d in std::function<void (objfile*)>::operator()(objfile*) const (this=0x555556752a20, __args#0=0x555556935550) at /usr/include/c++/9/bits/std_function.
 h:688
 #11 0x0000555555a2ff01 in gdb::observers::observable<objfile*>::notify (this=0x5555562eaa80 <gdb::observers::free_objfile>, args#0=0x555556935550) at /net/cascais.nfs/gdb/b
 inutils-gdb/src/gdb/../gdbsupport/observable.h:106
 #12 0x0000555555a2c56a in objfile::~objfile (this=0x555556935550, __in_chrg=<optimized out>) at src/gdb/objfiles.c:521
 #13 0x0000555555a31d46 in std::_Sp_counted_ptr<objfile*, (__gnu_cxx::_Lock_policy)2>::_M_dispose (this=0x555556c1f6f0) at /usr/include/c++/9/bits/shared_ptr_base.h:377
 #14 0x00005555556d3444 in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release (this=0x555556c1f6f0) at /usr/include/c++/9/bits/shared_ptr_base.h:155
 #15 0x00005555556cec77 in std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count (this=0x555556b99ee8, __in_chrg=<optimized out>) at /usr/include/c++/9/bits/shared_ptr_base.h:730
 #16 0x0000555555a2f8da in std::__shared_ptr<objfile, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr (this=0x555556b99ee0, __in_chrg=<optimized out>) at /usr/include/c++/9/bits/shared_ptr_base.h:1169
 #17 0x0000555555a2f8fa in std::shared_ptr<objfile>::~shared_ptr (this=0x555556b99ee0, __in_chrg=<optimized out>) at /usr/include/c++/9/bits/shared_ptr.h:103
 #18 0x0000555555a63fba in __gnu_cxx::new_allocator<std::_List_node<std::shared_ptr<objfile> > >::destroy<std::shared_ptr<objfile> > (this=0x55555679f0c0, __p=0x555556b99ee0) at /usr/include/c++/9/ext/new_allocator.h:153
 #19 0x0000555555a638fb in std::allocator_traits<std::allocator<std::_List_node<std::shared_ptr<objfile> > > >::destroy<std::shared_ptr<objfile> > (__a=..., __p=0x555556b99ee0) at /usr/include/c++/9/bits/alloc_traits.h:497
 #20 0x0000555555a6351c in std::__cxx11::list<std::shared_ptr<objfile>, std::allocator<std::shared_ptr<objfile> > >::_M_erase (this=0x55555679f0c0, __position=std::shared_ptr<objfile> (expired, weak count 1) = {get() = 0x555556935550}) at /usr/include/c++/9/bits/stl_list.h:1921
 #21 0x0000555555a62dab in std::__cxx11::list<std::shared_ptr<objfile>, std::allocator<std::shared_ptr<objfile> > >::erase (this=0x55555679f0c0, __position=std::shared_ptr<objfile> (expired, weak count 1) = {get() = 0x555556935550}) at /usr/include/c++/9/bits/list.tcc:158
 #22 0x0000555555a614dd in program_space::remove_objfile (this=0x55555679f080, objfile=0x555556935550) at src/gdb/progspace.c:207
 #23 0x0000555555a2c4dc in objfile::unlink (this=0x555556935550) at src/gdb/objfiles.c:497
 #24 0x0000555555a2da65 in objfile_purge_solibs () at src/gdb/objfiles.c:904
 #25 0x0000555555b3af74 in no_shared_libraries (ignored=0x0, from_tty=1) at src/gdb/solib.c:1236
 #26 0x0000555555bbafc7 in target_pre_inferior (from_tty=1) at src/gdb/target.c:1900
 #27 0x0000555555940afb in attach_command (args=0x5555563277c7 "15741", from_tty=1) at src/gdb/infcmd.c:2582
 ...


The problem is that the multi-target commit added a
scoped_restore_current_thread to remove_target_sections (frame #7
above).  scoped_restore_current_thread's ctor fetches the selected
frame's frame id.  If the frame had not had its frame id computed yet,
it is computed then (frame #4 above).  Because it has been determined
earlier that the frame's unwinder is the DWARF unwinder, we end up
here:

 static struct dwarf2_frame_cache *
 dwarf2_frame_cache (struct frame_info *this_frame, void **this_cache)
 {
 ...
   /* Find the correct FDE.  */
   fde = dwarf2_frame_find_fde (&pc1, &cache->per_objfile);
   gdb_assert (fde != NULL);

And, that assertion fails.  The assertion is reasonable, because the
DWARF unwinder only claims the frame if it managed to find the FDE
earlier (in dwarf2_frame_sniffer).

(unix/-m32 is thus really a red herring here -- it's just that on
x86_64 -m64, the frame is not claimed by the DWARF unwinder.)

The reason the assertion is failing, is because the objfile that
contains the FDE has been removed from the objfiles list already when
we get here (frame #22 above).  This suggests that the fix should be
to invalidate DWARF frames when their objfile is removed.  Or to keep
it simple and safe, invalidate the frame cache when an objfile is
removed.  That is what this commit does.

OOC, I checked why is it that when you unload a file with plain "(gdb)
file", we don't hit the assertion.  It must be because we're already
flushing the frame cache somewhere else in that case.  And indeed, we
flush the frame cache here:

 (gdb) bt
 #0  reinit_frame_cache () at src/gdb/frame.c:1857
 #1  0x0000555555ad1ad6 in registers_changed_ptid (target=0x0, ptid=...) at src/gdb/regcache.c:470
 #2  0x0000555555ad1b58 in registers_changed () at src/gdb/regcache.c:485
 #3  0x00005555558d095e in set_target_gdbarch (new_gdbarch=0x555556d5f5b0) at src/gdb/gdbarch.c:5528
 #4  0x0000555555677175 in set_gdbarch_from_file (abfd=0x0) at src/gdb/arch-utils.c:601
 #5  0x0000555555897c6b in exec_file_attach (filename=0x0, from_tty=1) at src/gdb/exec.c:409
 #6  0x000055555589852d in exec_file_command (args=0x0, from_tty=1) at src/gdb/exec.c:571
 #7  0x00005555558985a1 in file_command (arg=0x0, from_tty=1) at src/gdb/exec.c:583
 #8  0x000055555572b55f in do_const_cfunc (c=0x55555672e200, args=0x0, from_tty=1) at src/gdb/cli/cli-decode.c:95
 #9  0x000055555572f3d3 in cmd_func (cmd=0x55555672e200, args=0x0, from_tty=1) at src/gdb/cli/cli-decode.c:2181
 #10 0x0000555555be1ecc in execute_command (p=0x555556327804 "", from_tty=1) at src/gdb/top.c:668
 #11 0x0000555555895427 in command_handler (command=0x555556327800 "file") at src/gdb/event-top.c:588
 #12 0x00005555558958af in command_line_handler (rl=...) at src/gdb/event-top.c:773
 #13 0x0000555555894b3e in gdb_rl_callback_handler (rl=0x55555a09e240 "file") at src/gdb/event-top.c:219
 #14 0x0000555555ccfeec in rl_callback_read_char () at src/readline/readline/callback.c:281
 #15 0x000055555589495a in gdb_rl_callback_read_char_wrapper_noexcept () at src/gdb/event-top.c:177
 #16 0x0000555555894a08 in gdb_rl_callback_read_char_wrapper (client_data=0x555556327520) at src/gdb/event-top.c:194
 #17 0x00005555558952a5 in stdin_event_handler (error=0, client_data=0x555556327520) at src/gdb/event-top.c:516
 #18 0x0000555555e027d6 in handle_file_event (file_ptr=0x555558d20840, ready_mask=1) at src/gdbsupport/event-loop.cc:548
 #19 0x0000555555e02d88 in gdb_wait_for_event (block=1) at src/gdbsupport/event-loop.cc:673
 #20 0x0000555555e01c42 in gdb_do_one_event () at src/gdbsupport/event-loop.cc:215
 #21 0x00005555559c47c2 in start_event_loop () at src/gdb/main.c:356
 #22 0x00005555559c490d in captured_command_loop () at src/gdb/main.c:416
 #23 0x00005555559c6217 in captured_main (data=0x7fffffffdc00) at src/gdb/main.c:1253
 #24 0x00005555559c6289 in gdb_main (args=0x7fffffffdc00) at src/gdb/main.c:1268
 #25 0x0000555555621756 in main (argc=3, argv=0x7fffffffdd18) at src/gdb/gdb.c:32

gdb/ChangeLog:

	PR gdb/26336
	* progspace.c (program_space::remove_objfile): Invalidate the
	frame cache.
2020-08-12 19:31:19 +01:00
Tom Tromey
94c93c35b5 Remove ALL_PSPACES
This removes the ALL_PSPACES macro.  In this case it seemed cleanest
to change how program spaces are stored -- instead of using a linked
list, they are now stored in a std::vector.

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

	* symtab.c (set_symbol_cache_size)
	(maintenance_print_symbol_cache, maintenance_flush_symbol_cache)
	(maintenance_print_symbol_cache_statistics): Update.
	* symmisc.c (print_symbol_bcache_statistics)
	(print_objfile_statistics, maintenance_print_objfiles)
	(maintenance_info_symtabs, maintenance_check_symtabs)
	(maintenance_expand_symtabs, maintenance_info_line_tables):
	Update.
	* symfile-debug.c (set_debug_symfile): Update.
	* source.c (forget_cached_source_info): Update.
	* python/python.c (gdbpy_progspaces): Update.
	* psymtab.c (maintenance_info_psymtabs): Update.
	* probe.c (parse_probes): Update.
	* linespec.c (iterate_over_all_matching_symtabs)
	(collect_symtabs_from_filename, search_minsyms_for_name): Update.
	* guile/scm-progspace.c (gdbscm_progspaces): Update.
	* exec.c (exec_target::close): Update.
	* ada-tasks.c (ada_tasks_new_objfile_observer): Update.
	* breakpoint.c (print_one_breakpoint_location)
	(create_longjmp_master_breakpoint)
	(create_std_terminate_master_breakpoint): Update.
	* progspace.c (program_spaces): Now a std::vector.
	(maybe_new_address_space): Update.
	(add_program_space): Remove.
	(program_space::program_space): Update.
	(remove_program_space): Update.
	(number_of_program_spaces): Remove.
	(print_program_space, update_address_spaces): Update.
	* progspace.h (program_spaces): Change type.
	(ALL_PSPACES): Remove.
	(number_of_program_spaces): Don't declare.
	(struct program_space) <next>: Remove.
2020-05-08 14:21:22 -06:00
Tom Tromey
a1fd1ac9de Remove ALL_SO_LIBS and so_list_head
This patch started as an attempt to replace ALL_SO_LIBS with an
ordinary C++ iterator.  However, then I tripped over the so_list_head
define again, and decided to remove it as well.

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

	* mi/mi-cmd-file.c (mi_cmd_file_list_shared_libraries): Update.
	* solib-svr4.c (svr4_fetch_objfile_link_map): Update.
	(enable_break): Update.
	* solib-frv.c (frv_fdpic_find_global_pointer): Update.
	(frv_fdpic_find_canonical_descriptor): Update.
	(frv_fetch_objfile_link_map): Update.
	* progspace.c (program_space::free_all_objfiles): Update.
	(program_space::solibs): New method.
	* progspace.h (struct program_space) <solibs>: New method.
	* solist.h (master_so_list): Don't declare.
	(ALL_SO_LIBS): Remove.
	* solib.h (so_list_head): Remove.
	(update_solib_list): Update comment.
	* solib.c (master_so_list): Remove.
	(solib_used, update_solib_list, solib_add)
	(info_sharedlibrary_command, clear_solib)
	(reload_shared_libraries_1, remove_user_added_objfile): Update.
2020-05-08 14:21:22 -06:00
Pedro Alves
381ce63f2f Refactor delete_program_space as a destructor
Currently, while the program_space's ctor adds the new pspace to the
pspaces list, the destructor doesn't remove the pspace from the pspace
list.  Instead, you're supposed to use delete_program_space, to both
remove the pspace from the list, and deleting the pspace.

This patch eliminates delete_program_space, and makes the pspace dtor
remove the deleted pspace from the pspace list itself, i.e., makes the
dtor do the mirror opposite of the ctor.

I found this helps with a following patch that will allocate a mock
program_space on the stack.  It's easier to just let the regular dtor
remove the mock pspace from the pspace list than arrange to call
delete_program_space instead of the pspace dtor in that situation.

While at it, move the ctor/dtor intro comments to the header file, and
make the ctor explicit.

gdb/ChangeLog:
2020-04-16  Pedro Alves  <palves@redhat.com>

	* inferior.c (delete_inferior): Use delete operator directly
	instead of delete_program_space.
	* progspace.c (add_program_space): New, factored out from
	program_space::program_space.
	(remove_program_space): New, factored out from
	delete_program_space.
	(program_space::program_space): Remove intro comment.  Rewrite.
	(program_space::~program_space): Remove intro comment.  Call
	remove_program_space.
	(delete_program_space): Delete.
	* progspace.h (program_space::program_space): Make explicit.  Move
	intro comment here, adjusted.
	(program_space::~program_space): Move intro comment here,
	adjusted.
	(delete_program_space): Remove.
2020-04-16 14:50:07 +01:00
Pedro Alves
f3c469b95b Switch the inferior too in switch_to_program_space_and_thread
With multi-target, each inferior now has its own target connection.
The problem in switch_to_program_space_and_thread is that in the
current state GDB switches to "no thread" and also sets the program
space but because the inferior is not switched, potentially an
incorrect target remains selected.

Here is a sample scenario that exploits this flow:

On terminal 1, start a gdbserver on a program named foo:

 $ gdbserver :1234 ./foo

On terminal 2, start gdb on a program named bar.  Suppose foo and bar
are compiled from foo.c and bar.c.  They are completely separate.  So,
bar.c:2 has no meaning for foo.

 $ gdb -q ./bar
 Reading symbols from ./bar...
 (gdb) add-inferior
 [New inferior 2]
 Added inferior 2
 (gdb) inferior 2
 [Switching to inferior 2 [<null>] (<noexec>)]
 (gdb) target remote :1234
 ...
 (gdb) set debug remote 2
 (gdb) break bar.c:2
 Sending packet: $Hgp0.0#ad...Packet received: OK
 Sending packet: $m5fa,12#f8...Packet received: E01
 Sending packet: $m5fa,1#c6...Packet received: E01
 Sending packet: $m5fb,3#c9...Packet received: E01
 Sending packet: $m5fe,1#ca...Packet received: E01
 Breakpoint 1 at 0x5fe: file bar.c, line 2.
 (gdb)

Here we have an unnecessary sending of the packets to the gdbserver.

With this fix in progspace-and-thread.c, we'll get this:

 (gdb) break bar.c:2
 Breakpoint 1 at 0x5fe: file bar.c, line 2.
 (gdb)

Now there is no sending of the packets to gdbserver.

The changes around clear_symtab_users calls are necessary because
otherwise we regress gdb.base/step-over-exit.exp, hitting the new
assertion in switch_to_program_space_and_thread.  The problem is, a
forked child terminates, and when GDB decides to auto-purge that
inferior, GDB tries to switch to the pspace of that no-longer-existing
inferior.

The root of the problem is within the program_space destructor:

program_space::~program_space ()
{
...
  set_current_program_space (this);        # (1)
...
  breakpoint_program_space_exit (this);    # (2)
...
  free_all_objfiles ();                    # (3)
...
}

We get here from delete_inferior -> delete_program_space.

So we're deleting an inferior, and the inferior to be
deleted is no longer in the inferior list.

At (2), we've deleted all the breakpoints and locations for the
program space being deleted.

The crash happens while doing a breakpoint re-set, called by
clear_symtab_users at the tail end of (3).  That is, while recreating
breakpoints for the current program space, which is the program space
we're tearing down.  During breakpoint re-set, we try to switch to the
new location's pspace (the current pspace set in (1), so the pspace
we're tearing down) with switch_to_program_space_and_thread, and that
hits the failed assertion.  It's the fact that we recreate breakpoints
in the program_space destructor that is the latent bug here.  Just
don't do that, and we don't end up in the crash situation.

My first approach to fix this added a symfile_add_flags parameter to
program_space::free_all_objfiles, and then passed that down to
clear_symtab_users.  The program_space dtor would then pass down
SYMFILE_DEFER_BP_RESET to free_all_objfiles.  I couldn't help feeling
that adding that parameter to free_all_objfiles looked a little
awkward, so I settled on something a little different -- hoist the
clear_symtab_users call to the callers.  There are only two callers.
I felt that that didn't look as odd, particularly since
remove_symbol_file_command also does:

  objf->unlink ();
  clear_symtab_users (0);

I.e., objfile deletion is already separate from calling
clear_symtab_users in some places.

gdb/ChangeLog:
2020-01-10  Aleksandar Paunovic  <aleksandar.paunovic@intel.com>
	    Pedro Alves  <palves@redhat.com>

	* progspace-and-thread.c (switch_to_program_space_and_thread):
	Assert there's an inferior for PSPACE.  Use
	switch_to_inferior_no_thread to switch the inferior too.
	* progspace.c (program_space::~program_space): Call
	clear_symtab_users here, with SYMFILE_DEFER_BP_RESET.
	(program_space::free_all_objfiles): Don't call clear_symtab_users
	here.
	* symfile.c (symbol_file_clear): Call clear_symtab_users here.

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

	* gdb.server/bkpt-other-inferior.exp: New file.
2020-01-10 20:06:16 +00:00
Joel Brobecker
b811d2c292 Update copyright year range in all GDB files.
gdb/ChangeLog:

        Update copyright year range in all GDB files.
2020-01-01 10:20:53 +04:00
Tom Tromey
7d7167ce1b Manage objfiles with shared_ptr
This changes objfiles to be managed using a shared_ptr.  shared_ptr is
chosen because it enables the use of objfiles in background threads.

The simplest way to do this was to introduce a new iterator that will
return the underlying objfile, rather than a shared_ptr.  (I also
tried changing the rest of gdb to use shared_ptr, but this was quite
large; and to using intrusive reference counting, but this also was
tricky.)

gdb/ChangeLog
2019-12-12  Tom Tromey  <tom@tromey.com>

	* progspace.h (objfile_list): New typedef.
	(class unwrapping_objfile_iterator)
	(struct unwrapping_objfile_range): Newl
	(struct program_space) <objfiles_range>: Change type.
	<objfiles>: Change return type.
	<add_objfile>: Change type of "objfile" parameter.
	<objfiles_list>: Now a list of shared_ptr.
	* progspace.c (program_space::add_objfile): Change type of
	"objfile".  Update.
	(program_space::remove_objfile): Update.
	* objfiles.h (struct objfile) <~objfile>: Make public.
	* objfiles.c (objfile::make): Update.
	(objfile::unlink): Don't call delete.

Change-Id: I6fb7fbf06efb7cb7474c525908365863eae27eb3
2019-12-12 15:50:56 -07:00
Tom Tromey
343cc95202 Move free_all_objfiles to program_space
This changes free_all_objfiles to be a method on program_space, in
line with the other changes to treat program_space as a container for
objfiles.

gdb/ChangeLog
2019-12-12  Tom Tromey  <tom@tromey.com>

	* symfile.c (symbol_file_clear): Update.
	* progspace.h (struct program_space) <free_all_objfiles>: Declare
	method.
	* progspace.c (program_space::free_all_objfiles): New method.
	* objfiles.h (free_all_objfiles): Don't declare.
	* objfiles.c (free_all_objfiles): Move to program_space.

Change-Id: I908b549d2981b6005f7ca181fc0e6d24fc8b7b6f
2019-12-12 15:50:56 -07:00
Tom Tromey
d0801dd8f2 Store objfiles on a std::list
This removes objfile::next and changes objfiles to be stored in a
std::list.

gdb/ChangeLog
2019-12-12  Tom Tromey  <tom@tromey.com>

	* progspace.c (program_space::add_objfile)
	(program_space::remove_objfile): Update.
	(program_space::multi_objfile_p): Remove.
	* objfiles.h (struct objfile) <next>: Remove.
	* objfiles.c (objfile::objfile): Update.
	(put_objfile_before): Update.
	(unlink_objfile): Update.
	* progspace.h (object_files): Remove.
	(struct program_space) <objfiles_head>: Remove.
	<objfiles_list>: New member.
	<objfiles_range, objfiles_safe_range>: Change type.
	(objfiles): Change return type.
	(objfiles_safe): Update.
	(multi_objfile_p): Rewrite and inline.
	(object_files): Remove macro.

Change-Id: Ib4430e3db6f9a390399924379a5c10426c514853
2019-12-12 15:50:55 -07:00
Tom Tromey
deeafabb67 Remove MULTI_OBJFILE_P
This removes the MULTI_OBJFILE_P macro in favor of a method on the
program space.

gdb/ChangeLog
2019-12-12  Tom Tromey  <tom@tromey.com>

	* progspace.c (program_space::multi_objfile_p): New method.
	* printcmd.c (info_symbol_command): Update.
	* maint.c (maintenance_translate_address): Update.
	* objfiles.h (MULTI_OBJFILE_P): Remove.
	* progspace.h (struct program_space) <multi_objfile_p>: New
	method.

Change-Id: I2779e26ea8909078d63fea8f13bce94cab73948c
2019-12-12 15:50:53 -07:00