summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gdb/corelow.c27
-rw-r--r--gdb/testsuite/gdb.base/corefile.exp12
2 files changed, 33 insertions, 6 deletions
diff --git a/gdb/corelow.c b/gdb/corelow.c
index 001c4f147f..bdd7ddc59c 100644
--- a/gdb/corelow.c
+++ b/gdb/corelow.c
@@ -120,6 +120,9 @@ public:
private: /* per-core data */
+ /* Get rid of the core inferior. */
+ void clear_core ();
+
/* The core's section table. Note that these target sections are
*not* mapped in the current address spaces' set of target
sections --- those should come only from pure executable or
@@ -290,10 +293,8 @@ core_target::build_file_mappings ()
/* An arbitrary identifier for the core inferior. */
#define CORELOW_PID 1
-/* Close the core target. */
-
void
-core_target::close ()
+core_target::clear_core ()
{
if (core_bfd)
{
@@ -307,6 +308,14 @@ core_target::close ()
current_program_space->cbfd.reset (nullptr);
}
+}
+
+/* Close the core target. */
+
+void
+core_target::close ()
+{
+ clear_core ();
/* Core targets are heap-allocated (see core_target_open), so here
we delete ourselves. */
@@ -592,9 +601,15 @@ core_target_open (const char *arg, int from_tty)
void
core_target::detach (inferior *inf, int from_tty)
{
- /* Note that 'this' is dangling after this call. unpush_target
- closes the target, and our close implementation deletes
- 'this'. */
+ /* Get rid of the core. Don't rely on core_target::close doing it,
+ because target_detach may be called with core_target's refcount > 1,
+ meaning core_target::close may not be called yet by the
+ unpush_target call below. */
+ clear_core ();
+
+ /* Note that 'this' may be dangling after this call. unpush_target
+ closes the target if the refcount reaches 0, and our close
+ implementation deletes 'this'. */
inf->unpush_target (this);
/* Clear the register cache and the frame cache. */
diff --git a/gdb/testsuite/gdb.base/corefile.exp b/gdb/testsuite/gdb.base/corefile.exp
index 4ed92a0295..7f3d2efe3a 100644
--- a/gdb/testsuite/gdb.base/corefile.exp
+++ b/gdb/testsuite/gdb.base/corefile.exp
@@ -207,6 +207,16 @@ gdb_test "up" "#\[0-9\]* *\[0-9xa-fH'\]* in .* \\(.*\\).*" "up in corefile.exp (
gdb_test "core" "No core file now."
+# Test that we can unload the core with the "detach" command.
+
+proc_with_prefix corefile_detach {} {
+ clean_restart $::binfile
+
+ gdb_test "core-file $::corefile" "Core was generated by .*" "load core"
+ gdb_test "detach" "No core file now\\." "detach core"
+}
+
+corefile_detach
# Test a run (start) command will clear any loaded core file.
@@ -222,6 +232,8 @@ proc corefile_test_run {} {
return
}
+ clean_restart $::binfile
+
gdb_test "core-file $corefile" "Core was generated by .*" "run: load core again"
gdb_test "info files" "\r\nLocal core dump file:\r\n.*" "run: sanity check we see the core file"