gdb/gdb-fix-segfault-in-for_each_block-part-1.patch
Tom de Vries 8c12ed3178 - Rebase to 14.2 release (fedora rawhide @ 779f05e).
* Replace gdb-13.2.tar.bz2 with gdb-14.2.tar.bz2.
- Maintenance script import-fedora.sh:
  * Drop patch skips:
    * gdb-6.5-BEA-testsuite.patch
- Maintenance script qa-local.sh:
  * Drop SLE-11.  GDB 14.2 requires MPFR, and the SLE-11 version
    (2.3.2) is older than required (3.1.0+).
  * Add ALP.
  * Update version to 14.2.
- Maintenance script qa.sh:
  * Add PR30480, PR31440, PR31806 kfail.
  * Add PR31810, PR31809, PR31811 kfail.
  * Expand gdb.base/rtld-step.exp kfail.
  * Add gdb.threads/thread-specific-bp.exp kfail.
- Fedora patches updated:
  * gdb-6.3-gstack-20050411.patch
  * gdb-6.5-bz185337-resolve-tls-without-debuginfo-v2.patch
  * gdb-6.6-buildid-locate-rpm.patch
  * gdb-6.6-buildid-locate-solib-missing-ids.patch
  * gdb-6.6-buildid-locate.patch
  * gdb-6.6-testsuite-timeouts.patch
  * gdb-core-open-vdso-warning.patch
  * gdb-fedora-libncursesw.patch
  * gdb-linux_perf-bundle.patch
  * gdb-rhbz-2232086-cpp-ify-mapped-symtab.patch
  * gdb-rhbz-2232086-generate-dwarf-5-index-consistently.patch
  * gdb-rhbz-2232086-generate-gdb-index-consistently.patch
- Fedora patches removed:
  * gdb-binutils29988-read_indexed_address.patch
  * gdb-bz2196395-debuginfod-legacy-openssl-crash.patch
  * gdb-bz2237392-dwarf-obstack-allocation.patch
  * gdb-bz2237515-debuginfod-double-free.patch
  * gdb-rhbz1773651-gdb-index-internal-error.patch
  * gdb-rhbz2160211-excessive-core-file-warnings.patch
  * gdb-rhbz2192105-ftbs-dangling-pointer
  * gdb-rhbz2233961-CVE-2022-4806.patch
  * gdb-rhbz2233965-memory-leak.patch
- Fedora patches added:
  *  gdb-ftbs-swapped-calloc-args.patch
  *  gdb-rhbz2250652-avoid-PyOS_ReadlineTState.patch
  *  gdb-rhbz2250652-gdbpy_gil.patch
  *  gdb-rhbz2261580-intrusive_list-assertion-fix.patch
- Fedora fixup patches added:
  * fixup-skip-tests.patch
- Fedora fixup patches updated:  
  * fixup-gdb-linux_perf-bundle.patch
- Patches updated:
  * gdb-symtab-work-around-pr-gas-29517.patch
  * gdb-symtab-work-around-gas-pr28629.patch
  * gdb-testsuite-ada-pie.patch
  * gdb-python-finishbreakpoint-update.patch
  * gdb-testsuite-prevent-compilation-fails-with-unix-fpie-pie.patch
  * gdb-fix-segfault-in-for_each_block-part-1.patch
  * gdb-fix-segfault-in-for_each_block-part-2.patch
  * gdb-symtab-don-t-defer-backward-refs-inter-cu-intra-.patch
  * gdb-symtab-factor-out-m_deferred_entries-usage.patch
  * gdb-symtab-factor-out-m_die_range_map-usage.patch
  * gdb-symtab-fix-dw_tag_inlined_subroutine-entries-in-.patch
  * gdb-symtab-handle-nullptr-parent-in-parent_map-set_p.patch
  * gdb-symtab-keep-track-of-all-parents-for-cooked-inde.patch
  * gdb-symtab-keep-track-of-processed-dies-in-shard.patch
  * gdb-symtab-recurse-into-c-dw_tag_subprogram-dies-for.patch
  * gdb-symtab-refactor-condition-in-scan_attributes.patch
  * gdb-symtab-resolve-deferred-entries-inter-shard-case.patch
  * gdb-symtab-resolve-deferred-entries-intra-shard-case.patch
- Patches added:
  * gdb-symtab-work-around-pr-gas-29517-dwarf2-case.patch
  * fix-the-gdb.ada-inline-section-gc.exp-test.patch
  * gdb-testsuite-handle-pac-marker.patch
  * change-gdb.base-examine-backwards.exp-for-aix.patch
  * gdb-testsuite-fix-spurious-fails-with-examine-backwa.patch
  * gdb-testsuite-make-gdb.base-solib-search.exp-more-ro.patch
  * gdb-testsuite-fix-regexp-in-vgdb_start.patch
  * powerpc-and-aarch64-fix-reverse-stepping-failure.patch
  * gdb-tdep-fix-gdb.base-watch-bitfields.exp-on-aarch64.patch
  * gdb-tdep-fix-gdb.base-watchpoint-unaligned.exp-on-aa.patch
  * gdb-testsuite-add-pr-gdb-26967-kfail-in-two-more-tes.patch
  * gdb-testsuite-fix-gdb.base-eh_return.exp.patch
  * fixup-powerpc-and-aarch64-fix-reverse-stepping-failu.patch
  * gdb-exp-fix-printing-of-out-of-bounds-struct-members.patch
  * gdb-fix-heap-use-after-free-in-select_event_lwp.patch
  * fix-regression-on-aarch64-linux-gdbserver.patch
  * gdb-testsuite-factor-out-proc-get_portnum.patch
  * gdb-testsuite-make-portnum-a-persistent-global.patch
  * gdb-testsuite-factor-out-proc-with_lock.patch
  * gdb-testsuite-factor-out-proc-lock_dir.patch
  * gdb-testsuite-move-gpu-parallel.lock-to-cache-dir.patch
  * gdb-testsuite-use-unique-portnum-in-parallel-testing.patch
  * gdb-testsuite-use-unique-portnum-in-parallel-testing-check-slash-slash-case.patch
  * gdb-tdep-fix-reverse-execution-of-ldr-immediate-t4.patch
  * gdb-exp-fix-cast-handling-for-indirection.patch
  * gdb-remote-fix-abort-on-remote_close_error.patch
  * gdb-testsuite-use-find_gnatmake-instead-of-gdb_find_.patch
  * gdb-testsuite-simplify-gdb.server-server-kill-python.patch
  * gdb-testsuite-fix-gdbserver-pid-in-gdb.server-server.patch
  * gdb-testsuite-add-missing-include-in-gdb.base-ctf-pt.patch
  * gdb-testsuite-fix-gdb.ada-verylong.exp-on-32-bit-tar.patch
  * gdb-testsuite-add-missing-includes-in-gdb.trace-coll.patch
  * gdb-testsuite-fix-missing-return-type-in-gdb.linespe.patch
  * gdb-testsuite-fix-gdb.base-ending-run.exp-on-manjaro.patch
  * gdb-testsuite-fix-test-case-gdb.threads-attach-stopp.patch
  * gdb-testsuite-add-missing-include-in-gdb.base-rtld-s.patch
  * gdb-testsuite-fix-valgrind-tests-on-debian.patch
  * gdb-testsuite-fix-gdb.server-server-connect.exp-for-.patch
  * gdb-testsuite-handle-core-without-build-id-in-gdb.ba.patch
  * gdb-testsuite-fix-gdb.base-list-no-debug.exp-on-debi.patch
  * gdb-testsuite-reset-errcnt-and-warncnt-in-default_gd.patch
  * gdb-testsuite-fix-test-in-gdb.python-py-finish-break.patch
  * gdb-testsuite-further-handle-long-filenames-in-gdb.b.patch
  * gdb-testsuite-fix-license-text-in-gdb.reverse-map-to.patch
  * gdb-testsuite-call-ldd-version-in-gdb.testsuite-dump.patch
  * gdb-testsuite-fix-gdb.mi-mi-dprintf.exp-with-read1.patch
  * gdb-testsuite-fix-gdb.cp-namespace.exp-with-read1.patch
  * gdb-testsuite-fix-typo-in-gdb.base-catch-syscall.exp.patch
  * gdb-testsuite-use-more-progbits-for-arm.patch
  * gdb-testsuite-fix-gdb.dwarf2-dw2-gas-workaround.exp.patch
  * gdb-testsuite-add-gdb.dwarf2-backward-spec-inter-cu..patch
  * gdb-testsuite-add-gdb.dwarf2-forward-spec-inter-cu.e.patch
  * gdb-symtab-workaround-pr-gas-31115.patch
  * gdb-arm-remove-tpidruro-register-from-non-freebsd-ta.patch
  * gdb-tdep-fix-catching-syscall-execve-exit-for-arm.patch
  * gdb-arm-fix-epilogue-frame-id.patch
  * gdb-linux-delete-all-other-lwps-immediately-on-ptrac.patch
  * add-maint-info-linux-lwps-command.patch
  * fix-gdb.threads-threads-after-exec.exp-race.patch
  * rs6000-unwind-on-each-instruction-fix.patch
  * gdb-python-make-gdb.unwindinfo.add_saved_register-mo.patch
  * gdb-arm-remove-thumb-bit-in-arm_adjust_breakpoint_ad.patch
  * gdb-testsuite-fix-error-in-gdb.server-server-kill-py.patch
- Patches dropped:
  * remove-some-unnecessary-includes-from-exp.y.patch
  * gdb-testsuite-fix-gdb.gdb-python-helper.exp-with-o2-.patch
  * gdb-testsuite-simplify-gdb.base-unwind-on-each-insn..patch
  * gdb-testsuite-handle-output-after-prompt-in-gdb.thre.patch
  * gdb-testsuite-add-xfail-in-gdb.arch-i386-pkru.exp.patch
  * gdb-testsuite-factor-out-proc-linux_kernel_version.patch
  * gdb-testsuite-add-xfail-in-gdb.python-py-record-btra.patch
  * gdb-testsuite-fix-gdb.threads-schedlock.exp-on-fast-.patch
  * gdb-testsuite-simplify-gdb.arch-amd64-disp-step-avx..patch
  * gdb-testsuite-fix-gdb.threads-schedlock.exp-for-gcc-.patch
  * gdb-testsuite-add-xfail-case-in-gdb.python-py-record.patch
  * aarch64-avoid-initializers-for-vlas.patch
  * gdb-tdep-aarch64-fix-frame-address-of-last-insn.patch
  * fix-pr30369-regression-on-aarch64-arm-pr30506.patch
  * gdb-testsuite-fix-breakpoint-regexp-in-gdb.ada-out_o.patch
  * gdb-testsuite-relax-breakpoint-count-check-in-gdb.py.patch
  * gdb-testsuite-fix-buffer-overflow-in-gdb.base-signed.patch
  * gdb-testsuite-require-syscall-time-in-gdb.reverse-ti.patch
  * gdb-testsuite-handle-missing-gdc-in-gdb.dlang-dlang-.patch
  * gdb-testsuite-add-basic-lmap-for-tcl-8.6.patch
  * gdb-testsuite-fix-gdb.rust-watch.exp-on-ppc64le.patch
  * gdb-testsuite-fix-gdb.python-py-breakpoint.exp-timeo.patch
  * powerpc-fix-for-gdb.reverse-finish-precsave.exp-and-.patch
  * powerpc-regression-fix-for-reverse-finish-command.patch
  * gdb-testsuite-don-t-use-string-cat-in-gdb.dwarf2-dw2.patch
  * move-step_until-procedure.patch
  * gdb-testsuite-fix-gdb.arch-i386-signal.exp-on-x86_64.patch
  * gdb-testsuite-fix-regexps-in-gdb.base-step-over-sysc.patch
  * gdb-testsuite-add-kfail-for-pr-ada-30908.patch
  * gdb-testsuite-fix-gdb.ada-mi_task_arg.exp-with-newer.patch
  * gdb-testsuite-fix-gdb.cp-m-static.exp-regression-on-.patch
  * gdb-symtab-fix-line-number-of-static-const-class-mem.patch
  * gdb-symtab-handle-pu-in-iterate_over_some_symtabs.patch
  * gdb-testsuite-fix-gdb.dwarf2-nullptr_t.exp-with-cc-w.patch
  * gdb-symtab-fix-too-many-symbols-in-gdbpy_lookup_stat.patch
  * gdb-support-rseq-auxvs.patch
  * gdb-testsuite-add-xfail-for-gdb-29965-in-gdb.threads.patch
  * gdb-cli-handle-pending-c-after-rl_callback_read_char.patch
  * gdb-testsuite-add-have_host_locale.patch
  * gdb-symtab-find-main-language-without-symtab-expansi.patch
  * gdb-symtab-don-t-deduplicate-variables-in-gdb-index.patch
  * xcoffread.c-fix-werror-dangling-pointer-issue-with-m.patch
  * avoid-manual-memory-management-in-go-lang.c.patch
  * gdb-go-handle-v3-go_0-mangled-prefix.patch
  * gdb-symtab-handle-self-reference-die.patch
  * gdb-symtab-handle-self-reference-in-inherit_abstract.patch
  * gdb-symtab-add-optimized-out-static-var-to-cooked-in.patch
  * gdb-testsuite-fix-gdb.python-py-breakpoint.exp-with-.patch
  * gdb-tui-fix-segfault-in-tui_find_disassembly_address.patch
  * gdb-testsuite-add-wait_for_msg-arg-to-term-resize-fi.patch
  * gdb-testsuite-fix-gdb-server-ext-run-exp-for-obs.patch  
  * gdb-testsuite-work-around-skip_prologue-problems-in-gdb.threads-process-dies-while-detaching.exp.patch

OBS-URL: https://build.opensuse.org/package/show/devel:gcc/gdb?expand=0&rev=386
2024-05-30 21:00:48 +00:00

637 lines
22 KiB
Diff

From 1d02ba0f4adcba2595a67e88fb1ba6d35c7f8e5b Mon Sep 17 00:00:00 2001
From: Tom de Vries <tdevries@suse.de>
Date: Tue, 7 May 2024 11:33:57 +0200
Subject: [PATCH] [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
---
gdb/breakpoint.c | 29 ++++++++-------
gdb/inferior.c | 8 ++---
gdb/inferior.h | 2 +-
gdb/infrun.c | 18 +++++-----
gdb/linux-nat.c | 2 +-
gdb/process-stratum-target.c | 2 +-
gdb/progspace.c | 22 +++++-------
gdb/progspace.h | 64 +++++++++++++++++++++-------------
gdb/record-btrace.c | 2 +-
gdb/regcache.c | 2 +-
gdb/scoped-mock-context.h | 2 +-
gdb/target-dcache.c | 11 +++---
gdbsupport/refcounted-object.h | 17 +++++++++
13 files changed, 102 insertions(+), 79 deletions(-)
diff --git a/gdb/breakpoint.c b/gdb/breakpoint.c
index 01f187ca4fe..a22bdb091cd 100644
--- a/gdb/breakpoint.c
+++ b/gdb/breakpoint.c
@@ -1723,7 +1723,7 @@ one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
int bptoffset = 0;
if (!breakpoint_address_match (target_info->placed_address_space, 0,
- current_program_space->aspace, 0))
+ current_program_space->aspace.get (), 0))
{
/* The breakpoint is inserted in a different address space. */
return;
@@ -2409,7 +2409,7 @@ should_be_inserted (struct bp_location *bl)
a breakpoint. */
if ((bl->loc_type == bp_loc_software_breakpoint
|| bl->loc_type == bp_loc_hardware_breakpoint)
- && stepping_past_instruction_at (bl->pspace->aspace,
+ && stepping_past_instruction_at (bl->pspace->aspace.get (),
bl->address)
/* The single-step breakpoint may be inserted at the location
we're trying to step if the instruction branches to itself.
@@ -2847,7 +2847,7 @@ insert_bp_location (struct bp_location *bl,
read the breakpoint instead of returning the data saved in
the breakpoint location's shadow contents. */
bl->target_info.reqstd_address = bl->address;
- bl->target_info.placed_address_space = bl->pspace->aspace;
+ bl->target_info.placed_address_space = bl->pspace->aspace.get ();
bl->target_info.length = bl->length;
/* When working with target-side conditions, we must pass all the conditions
@@ -4429,7 +4429,7 @@ bp_location_inserted_here_p (const struct bp_location *bl,
const address_space *aspace, CORE_ADDR pc)
{
if (bl->inserted
- && breakpoint_address_match (bl->pspace->aspace, bl->address,
+ && breakpoint_address_match (bl->pspace->aspace.get (), bl->address,
aspace, pc))
{
/* An unmapped overlay can't be a match. */
@@ -4508,7 +4508,7 @@ hardware_watchpoint_inserted_in_range (const address_space *aspace,
continue;
for (bp_location &loc : bpt.locations ())
- if (loc.pspace->aspace == aspace && loc.inserted)
+ if (loc.pspace->aspace.get () == aspace && loc.inserted)
{
CORE_ADDR l, h;
@@ -7330,10 +7330,10 @@ breakpoint_location_address_match (struct bp_location *bl,
const address_space *aspace,
CORE_ADDR addr)
{
- return (breakpoint_address_match (bl->pspace->aspace, bl->address,
+ return (breakpoint_address_match (bl->pspace->aspace.get (), bl->address,
aspace, addr)
|| (bl->length
- && breakpoint_address_match_range (bl->pspace->aspace,
+ && breakpoint_address_match_range (bl->pspace->aspace.get (),
bl->address, bl->length,
aspace, addr)));
}
@@ -7350,7 +7350,7 @@ breakpoint_location_address_range_overlap (struct bp_location *bl,
CORE_ADDR addr, int len)
{
if (gdbarch_has_global_breakpoints (target_gdbarch ())
- || bl->pspace->aspace == aspace)
+ || bl->pspace->aspace.get () == aspace)
{
int bl_len = bl->length != 0 ? bl->length : 1;
@@ -7407,8 +7407,10 @@ breakpoint_locations_match (const struct bp_location *loc1,
/* We compare bp_location.length in order to cover ranged
breakpoints. Keep this in sync with
bp_location_is_less_than. */
- return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
- loc2->pspace->aspace, loc2->address)
+ return (breakpoint_address_match (loc1->pspace->aspace.get (),
+ loc1->address,
+ loc2->pspace->aspace.get (),
+ loc2->address)
&& (loc1->loc_type == loc2->loc_type || sw_hw_bps_match)
&& loc1->length == loc2->length);
}
@@ -9568,8 +9570,9 @@ ranged_breakpoint::breakpoint_hit (const struct bp_location *bl,
|| ws.sig () != GDB_SIGNAL_TRAP)
return 0;
- return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
- bl->length, aspace, bp_addr);
+ return breakpoint_address_match_range (bl->pspace->aspace.get (),
+ bl->address, bl->length, aspace,
+ bp_addr);
}
/* Implement the "resources_needed" method for ranged breakpoints. */
@@ -12018,7 +12021,7 @@ code_breakpoint::breakpoint_hit (const struct bp_location *bl,
|| ws.sig () != GDB_SIGNAL_TRAP)
return 0;
- if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
+ if (!breakpoint_address_match (bl->pspace->aspace.get (), bl->address,
aspace, bp_addr))
return 0;
diff --git a/gdb/inferior.c b/gdb/inferior.c
index 550bbd2827c..461f4fc076a 100644
--- a/gdb/inferior.c
+++ b/gdb/inferior.c
@@ -831,15 +831,13 @@ remove_inferior_command (const char *args, int from_tty)
struct inferior *
add_inferior_with_spaces (void)
{
- struct address_space *aspace;
struct program_space *pspace;
struct inferior *inf;
/* If all inferiors share an address space on this system, this
doesn't really return a new address space; otherwise, it
really does. */
- aspace = maybe_new_address_space ();
- pspace = new program_space (aspace);
+ pspace = new program_space (maybe_new_address_space ());
inf = add_inferior (0);
inf->pspace = pspace;
inf->aspace = pspace->aspace;
@@ -1002,15 +1000,13 @@ clone_inferior_command (const char *args, int from_tty)
for (i = 0; i < copies; ++i)
{
- struct address_space *aspace;
struct program_space *pspace;
struct inferior *inf;
/* If all inferiors share an address space on this system, this
doesn't really return a new address space; otherwise, it
really does. */
- aspace = maybe_new_address_space ();
- pspace = new program_space (aspace);
+ pspace = new program_space (maybe_new_address_space ());
inf = add_inferior (0);
inf->pspace = pspace;
inf->aspace = pspace->aspace;
diff --git a/gdb/inferior.h b/gdb/inferior.h
index 29c90d15efa..4139791740f 100644
--- a/gdb/inferior.h
+++ b/gdb/inferior.h
@@ -577,7 +577,7 @@ class inferior : public refcounted_object,
bool removable = false;
/* The address space bound to this inferior. */
- struct address_space *aspace = NULL;
+ address_space_ref_ptr aspace;
/* The program space bound to this inferior. */
struct program_space *pspace = NULL;
diff --git a/gdb/infrun.c b/gdb/infrun.c
index 7be98cfc252..3854c66bf6c 100644
--- a/gdb/infrun.c
+++ b/gdb/infrun.c
@@ -531,8 +531,8 @@ holding the child stopped. Try \"set detach-on-fork\" or \
}
else
{
- child_inf->aspace = new address_space ();
- child_inf->pspace = new program_space (child_inf->aspace);
+ child_inf->pspace = new program_space (new_address_space ());
+ child_inf->aspace = child_inf->pspace->aspace;
child_inf->removable = true;
clone_program_space (child_inf->pspace, parent_inf->pspace);
}
@@ -610,8 +610,8 @@ holding the child stopped. Try \"set detach-on-fork\" or \
child_inf->aspace = parent_inf->aspace;
child_inf->pspace = parent_inf->pspace;
- parent_inf->aspace = new address_space ();
- parent_inf->pspace = new program_space (parent_inf->aspace);
+ parent_inf->pspace = new program_space (new_address_space ());
+ parent_inf->aspace = parent_inf->pspace->aspace;
clone_program_space (parent_inf->pspace, child_inf->pspace);
/* The parent inferior is still the current one, so keep things
@@ -620,8 +620,8 @@ holding the child stopped. Try \"set detach-on-fork\" or \
}
else
{
- child_inf->aspace = new address_space ();
- child_inf->pspace = new program_space (child_inf->aspace);
+ child_inf->pspace = new program_space (new_address_space ());
+ child_inf->aspace = child_inf->pspace->aspace;
child_inf->removable = true;
child_inf->symfile_flags = SYMFILE_NO_READ;
clone_program_space (child_inf->pspace, parent_inf->pspace);
@@ -1031,7 +1031,6 @@ handle_vfork_child_exec_or_exit (int exec)
if (vfork_parent->pending_detach)
{
struct program_space *pspace;
- struct address_space *aspace;
/* follow-fork child, detach-on-fork on. */
@@ -1056,9 +1055,8 @@ handle_vfork_child_exec_or_exit (int exec)
of" a hack. */
pspace = inf->pspace;
- aspace = inf->aspace;
- inf->aspace = nullptr;
inf->pspace = nullptr;
+ address_space_ref_ptr aspace = std::move (inf->aspace);
if (print_inferior_events)
{
@@ -5906,7 +5904,7 @@ handle_inferior_event (struct execution_control_state *ecs)
= get_thread_arch_aspace_regcache (parent_inf,
ecs->ws.child_ptid (),
gdbarch,
- parent_inf->aspace);
+ parent_inf->aspace.get ());
/* Read PC value of parent process. */
parent_pc = regcache_read_pc (regcache);
diff --git a/gdb/linux-nat.c b/gdb/linux-nat.c
index ed445c5e5bb..c8991cc3da4 100644
--- a/gdb/linux-nat.c
+++ b/gdb/linux-nat.c
@@ -4365,7 +4365,7 @@ linux_nat_target::thread_address_space (ptid_t ptid)
inf = find_inferior_pid (this, pid);
gdb_assert (inf != NULL);
- return inf->aspace;
+ return inf->aspace.get ();
}
/* Return the cached value of the processor core for thread PTID. */
diff --git a/gdb/process-stratum-target.c b/gdb/process-stratum-target.c
index 5c031203e89..4bcca4f39c2 100644
--- a/gdb/process-stratum-target.c
+++ b/gdb/process-stratum-target.c
@@ -37,7 +37,7 @@ process_stratum_target::thread_address_space (ptid_t ptid)
"address space of thread %s\n"),
target_pid_to_str (ptid).c_str ());
- return inf->aspace;
+ return inf->aspace.get ();
}
struct gdbarch *
diff --git a/gdb/progspace.c b/gdb/progspace.c
index 1dbcd5875dd..b4d25ba6196 100644
--- a/gdb/progspace.c
+++ b/gdb/progspace.c
@@ -55,8 +55,8 @@ address_space::address_space ()
return a pointer to an existing address space, in case inferiors
share an address space on this target system. */
-struct address_space *
-maybe_new_address_space (void)
+address_space_ref_ptr
+maybe_new_address_space ()
{
int shared_aspace = gdbarch_has_shared_address_space (target_gdbarch ());
@@ -66,7 +66,7 @@ maybe_new_address_space (void)
return program_spaces[0]->aspace;
}
- return new address_space ();
+ return new_address_space ();
}
/* Start counting over from scratch. */
@@ -94,9 +94,9 @@ remove_program_space (program_space *pspace)
/* See progspace.h. */
-program_space::program_space (address_space *aspace_)
+program_space::program_space (address_space_ref_ptr aspace_)
: num (++last_program_space_num),
- aspace (aspace_)
+ aspace (std::move (aspace_))
{
program_spaces.push_back (this);
gdb::observers::new_program_space.notify (this);
@@ -121,8 +121,6 @@ program_space::~program_space ()
/* Defer breakpoint re-set because we don't want to create new
locations for this pspace which we're tearing down. */
clear_symtab_users (SYMFILE_DEFER_BP_RESET);
- if (!gdbarch_has_shared_address_space (target_gdbarch ()))
- delete this->aspace;
}
/* See progspace.h. */
@@ -408,18 +406,14 @@ update_address_spaces (void)
if (shared_aspace)
{
- struct address_space *aspace = new address_space ();
+ address_space_ref_ptr aspace = new_address_space ();
- delete current_program_space->aspace;
for (struct program_space *pspace : program_spaces)
pspace->aspace = aspace;
}
else
for (struct program_space *pspace : program_spaces)
- {
- delete pspace->aspace;
- pspace->aspace = new address_space ();
- }
+ pspace->aspace = new_address_space ();
for (inferior *inf : all_inferiors ())
if (gdbarch_has_global_solist (target_gdbarch ()))
@@ -456,5 +450,5 @@ initialize_progspace (void)
modules have done that. Do this before
initialize_current_architecture, because that accesses the ebfd
of current_program_space. */
- current_program_space = new program_space (new address_space ());
+ current_program_space = new program_space (new_address_space ());
}
diff --git a/gdb/progspace.h b/gdb/progspace.h
index ee12d89c173..12f113f6d9c 100644
--- a/gdb/progspace.h
+++ b/gdb/progspace.h
@@ -28,6 +28,8 @@
#include "solist.h"
#include "gdbsupport/next-iterator.h"
#include "gdbsupport/safe-iterator.h"
+#include "gdbsupport/refcounted-object.h"
+#include "gdbsupport/gdb_ref_ptr.h"
#include <list>
#include <vector>
@@ -42,6 +44,40 @@ struct so_list;
typedef std::list<std::unique_ptr<objfile>> objfile_list;
+/* An address space. It is used for comparing if
+ pspaces/inferior/threads see the same address space and for
+ associating caches to each address space. */
+struct address_space : public refcounted_object
+{
+ /* Create a new address space object, and add it to the list. */
+ address_space ();
+ DISABLE_COPY_AND_ASSIGN (address_space);
+
+ /* Returns the integer address space id of this address space. */
+ int num () const
+ {
+ return m_num;
+ }
+
+ /* Per aspace data-pointers required by other GDB modules. */
+ registry<address_space> registry_fields;
+
+private:
+ int m_num;
+};
+
+using address_space_ref_ptr
+ = gdb::ref_ptr<address_space,
+ refcounted_object_delete_ref_policy<address_space>>;
+
+/* Create a new address space. */
+
+static inline address_space_ref_ptr
+new_address_space ()
+{
+ return address_space_ref_ptr::new_reference (new address_space);
+}
+
/* An iterator that wraps an iterator over std::unique_ptr<objfile>,
and dereferences the returned object. This is useful for iterating
over a list of shared pointers and returning raw pointers -- which
@@ -191,7 +227,7 @@ struct program_space
{
/* Constructs a new empty program space, binds it to ASPACE, and
adds it to the program space list. */
- explicit program_space (address_space *aspace);
+ explicit program_space (address_space_ref_ptr aspace);
/* Releases a program space, and all its contents (shared libraries,
objfiles, and any other references to the program space in other
@@ -336,7 +372,7 @@ struct program_space
are global, then this field is ignored (we don't currently
support inferiors sharing a program space if the target doesn't
make breakpoints global). */
- struct address_space *aspace = NULL;
+ address_space_ref_ptr aspace;
/* True if this program space's section offsets don't yet represent
the final offsets of the "live" address space (that is, the
@@ -383,28 +419,6 @@ struct program_space
target_section_table m_target_sections;
};
-/* An address space. It is used for comparing if
- pspaces/inferior/threads see the same address space and for
- associating caches to each address space. */
-struct address_space
-{
- /* Create a new address space object, and add it to the list. */
- address_space ();
- DISABLE_COPY_AND_ASSIGN (address_space);
-
- /* Returns the integer address space id of this address space. */
- int num () const
- {
- return m_num;
- }
-
- /* Per aspace data-pointers required by other GDB modules. */
- registry<address_space> registry_fields;
-
-private:
- int m_num;
-};
-
/* The list of all program spaces. There's always at least one. */
extern std::vector<struct program_space *>program_spaces;
@@ -447,7 +461,7 @@ class scoped_restore_current_program_space
/* Maybe create a new address space object, and add it to the list, or
return a pointer to an existing address space, in case inferiors
share an address space. */
-extern struct address_space *maybe_new_address_space (void);
+extern address_space_ref_ptr maybe_new_address_space ();
/* Update all program spaces matching to address spaces. The user may
have created several program spaces, and loaded executables into
diff --git a/gdb/record-btrace.c b/gdb/record-btrace.c
index 97447d3e8f8..a4d6c1b5bf2 100644
--- a/gdb/record-btrace.c
+++ b/gdb/record-btrace.c
@@ -2321,7 +2321,7 @@ record_btrace_replay_at_breakpoint (struct thread_info *tp)
if (insn == NULL)
return 0;
- return record_check_stopped_by_breakpoint (tp->inf->aspace, insn->pc,
+ return record_check_stopped_by_breakpoint (tp->inf->aspace.get (), insn->pc,
&btinfo->stop_reason);
}
diff --git a/gdb/regcache.c b/gdb/regcache.c
index 91b20b7a2a2..a78fe0a80e7 100644
--- a/gdb/regcache.c
+++ b/gdb/regcache.c
@@ -1628,7 +1628,7 @@ get_thread_arch_aspace_regcache_and_check (inferior *inf_for_target_calls,
the current inferior's gdbarch. Also use the current inferior's address
space. */
gdbarch *arch = inf_for_target_calls->gdbarch;
- address_space *aspace = inf_for_target_calls->aspace;
+ address_space *aspace = inf_for_target_calls->aspace.get ();
regcache *regcache = get_thread_arch_aspace_regcache (inf_for_target_calls,
ptid, arch, aspace);
diff --git a/gdb/scoped-mock-context.h b/gdb/scoped-mock-context.h
index 9ad7ebf5f0c..5f25dc7ed6b 100644
--- a/gdb/scoped-mock-context.h
+++ b/gdb/scoped-mock-context.h
@@ -38,7 +38,7 @@ struct scoped_mock_context
Target mock_target;
ptid_t mock_ptid {1, 1};
- program_space mock_pspace {new address_space ()};
+ program_space mock_pspace {new_address_space ()};
inferior mock_inferior {mock_ptid.pid ()};
thread_info mock_thread {&mock_inferior, mock_ptid};
diff --git a/gdb/target-dcache.c b/gdb/target-dcache.c
index 13c2888e7ea..b1b772ab76e 100644
--- a/gdb/target-dcache.c
+++ b/gdb/target-dcache.c
@@ -33,7 +33,7 @@ int
target_dcache_init_p (void)
{
DCACHE *dcache
- = target_dcache_aspace_key.get (current_program_space->aspace);
+ = target_dcache_aspace_key.get (current_program_space->aspace.get ());
return (dcache != NULL);
}
@@ -44,7 +44,7 @@ void
target_dcache_invalidate (void)
{
DCACHE *dcache
- = target_dcache_aspace_key.get (current_program_space->aspace);
+ = target_dcache_aspace_key.get (current_program_space->aspace.get ());
if (dcache != NULL)
dcache_invalidate (dcache);
@@ -56,7 +56,7 @@ target_dcache_invalidate (void)
DCACHE *
target_dcache_get (void)
{
- return target_dcache_aspace_key.get (current_program_space->aspace);
+ return target_dcache_aspace_key.get (current_program_space->aspace.get ());
}
/* Return the target dcache. If it is not initialized yet, initialize
@@ -66,12 +66,13 @@ DCACHE *
target_dcache_get_or_init (void)
{
DCACHE *dcache
- = target_dcache_aspace_key.get (current_program_space->aspace);
+ = target_dcache_aspace_key.get (current_program_space->aspace.get ());
if (dcache == NULL)
{
dcache = dcache_init ();
- target_dcache_aspace_key.set (current_program_space->aspace, dcache);
+ target_dcache_aspace_key.set (current_program_space->aspace.get (),
+ dcache);
}
return dcache;
diff --git a/gdbsupport/refcounted-object.h b/gdbsupport/refcounted-object.h
index d8fdb950043..294fd873df1 100644
--- a/gdbsupport/refcounted-object.h
+++ b/gdbsupport/refcounted-object.h
@@ -67,4 +67,21 @@ struct refcounted_object_ref_policy
}
};
+/* A policy class to interface gdb::ref_ptr with a refcounted_object, that
+ deletes the object once the refcount reaches 0.. */
+
+template<typename T>
+struct refcounted_object_delete_ref_policy
+{
+ static void incref (T *obj)
+ { obj->incref (); }
+
+ static void decref (T *obj)
+ {
+ obj->decref ();
+ if (obj->refcount () == 0)
+ delete obj;
+ }
+};
+
#endif /* COMMON_REFCOUNTED_OBJECT_H */
base-commit: 0bb6f49bb9ad577667075550ca2ad4cb49931078
--
2.35.3