public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 3/3] btrace: pretend we're not replaying when generating a core file
  2014-05-22 12:19 [PATCH 1/3] make_corefile_notes: have caller free returned memory Markus Metzger
@ 2014-05-22 12:19 ` Markus Metzger
  2014-06-04 17:03   ` Pedro Alves
  2014-05-22 12:19 ` [PATCH 2/3] observer, gcore: add corefile notes generation observers Markus Metzger
  2014-06-04 16:42 ` [PATCH 1/3] make_corefile_notes: have caller free returned memory Pedro Alves
  2 siblings, 1 reply; 5+ messages in thread
From: Markus Metzger @ 2014-05-22 12:19 UTC (permalink / raw)
  To: palves; +Cc: gdb-patches

When generating a core file using the "generate-core-file" command while
replaying with the btrace record target, we won't be able to access all
registers and all memory.  This leads to an assertion.

Use the gcore_start and gcore_end observers to pretend that we are not
replaying while generating a core file.  This will forward fetch and store
registers as well as xfer memory calls to the target beneath.

2014-05-22  Markus Metzger  <markus.t.metzger@intel.com>

	* record-btrace.c (before_corefile_observer)
	(after_corefile_observer, record_btrace_generating_corefile)
	(do_before_corefile, do_after_corefile): New.
	(record_btrace_xfer_partial, record_btrace_fetch_registers)
	(record_btrace_store_registers, record_btrace_prepare_to_store):
	Forward request when generating a core file.
	(_initialize_record_btrace): Attach before_corefile_observer and
	after_corefile_observer.

testsuite/
	* gdb.btrace/gcore.exp: New.
---
 gdb/record-btrace.c                | 35 ++++++++++++++++++++++++++++----
 gdb/testsuite/gdb.btrace/gcore.exp | 41 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 72 insertions(+), 4 deletions(-)
 create mode 100644 gdb/testsuite/gdb.btrace/gcore.exp

diff --git a/gdb/record-btrace.c b/gdb/record-btrace.c
index d768225..1265154 100644
--- a/gdb/record-btrace.c
+++ b/gdb/record-btrace.c
@@ -42,6 +42,13 @@ static struct target_ops record_btrace_ops;
 /* A new thread observer enabling branch tracing for the new thread.  */
 static struct observer *record_btrace_thread_observer;
 
+/* A pair of make_corefile_notes observers.  */
+static struct observer *before_corefile_observer;
+static struct observer *after_corefile_observer;
+
+/* A flag indicating that we are currently generating a core file.  */
+static int record_btrace_generating_corefile;
+
 /* Temporarily allow memory accesses.  */
 static int record_btrace_allow_memory_access;
 
@@ -815,7 +822,8 @@ record_btrace_xfer_partial (struct target_ops *ops, enum target_object object,
   struct target_ops *t;
 
   /* Filter out requests that don't make sense during replay.  */
-  if (!record_btrace_allow_memory_access && record_btrace_is_replaying (ops))
+  if (!record_btrace_allow_memory_access && !record_btrace_generating_corefile
+      && record_btrace_is_replaying (ops))
     {
       switch (object)
 	{
@@ -928,7 +936,7 @@ record_btrace_fetch_registers (struct target_ops *ops,
   gdb_assert (tp != NULL);
 
   replay = tp->btrace.replay;
-  if (replay != NULL)
+  if (replay != NULL && !record_btrace_generating_corefile)
     {
       const struct btrace_insn *insn;
       struct gdbarch *gdbarch;
@@ -969,7 +977,7 @@ record_btrace_store_registers (struct target_ops *ops,
 {
   struct target_ops *t;
 
-  if (record_btrace_is_replaying (ops))
+  if (!record_btrace_generating_corefile && record_btrace_is_replaying (ops))
     error (_("This record target does not allow writing registers."));
 
   gdb_assert (may_write_registers != 0);
@@ -992,7 +1000,7 @@ record_btrace_prepare_to_store (struct target_ops *ops,
 {
   struct target_ops *t;
 
-  if (record_btrace_is_replaying (ops))
+  if (!record_btrace_generating_corefile && record_btrace_is_replaying (ops))
     return;
 
   for (t = ops->beneath; t != NULL; t = t->beneath)
@@ -1938,6 +1946,22 @@ cmd_record_btrace_start (char *args, int from_tty)
   execute_command ("target record-btrace", from_tty);
 }
 
+/* The before_corefile_observer function.  */
+
+static void
+do_before_corefile (void)
+{
+  record_btrace_generating_corefile = 1;
+}
+
+/* The after_corefile_observer function.  */
+
+static void
+do_after_corefile (void)
+{
+  record_btrace_generating_corefile = 0;
+}
+
 void _initialize_record_btrace (void);
 
 /* Initialize btrace commands.  */
@@ -1945,6 +1969,9 @@ void _initialize_record_btrace (void);
 void
 _initialize_record_btrace (void)
 {
+  before_corefile_observer = observer_attach_gcore_start (do_before_corefile);
+  after_corefile_observer = observer_attach_gcore_end (do_after_corefile);
+
   add_cmd ("btrace", class_obscure, cmd_record_btrace_start,
 	   _("Start branch trace recording."),
 	   &record_cmdlist);
diff --git a/gdb/testsuite/gdb.btrace/gcore.exp b/gdb/testsuite/gdb.btrace/gcore.exp
new file mode 100644
index 0000000..1ff4f27
--- /dev/null
+++ b/gdb/testsuite/gdb.btrace/gcore.exp
@@ -0,0 +1,41 @@
+# This testcase is part of GDB, the GNU debugger.
+#
+# Copyright 2014 Free Software Foundation, Inc.
+#
+# Contributed by Intel Corp. <markus.t.metzger@intel.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# check for btrace support
+if { [skip_btrace_tests] } { return -1 }
+
+# start inferior
+standard_testfile x86-record_goto.S
+if [prepare_for_testing gcore.exp $testfile $srcfile] {
+    return -1
+}
+
+if ![runto_main] {
+    return -1
+}
+
+# trace the call to the test function
+gdb_test_no_output "record btrace"
+gdb_test "next" ".*main\.3.*"
+
+# start replaying
+gdb_test "record goto begin" ".*main\.2.*"
+
+# generate a core file - this used to assert
+gdb_test "generate-core-file core" "Saved corefile core"
-- 
1.8.3.1

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/3] observer, gcore: add corefile notes generation observers
  2014-05-22 12:19 [PATCH 1/3] make_corefile_notes: have caller free returned memory Markus Metzger
  2014-05-22 12:19 ` [PATCH 3/3] btrace: pretend we're not replaying when generating a core file Markus Metzger
@ 2014-05-22 12:19 ` Markus Metzger
  2014-06-04 16:42 ` [PATCH 1/3] make_corefile_notes: have caller free returned memory Pedro Alves
  2 siblings, 0 replies; 5+ messages in thread
From: Markus Metzger @ 2014-05-22 12:19 UTC (permalink / raw)
  To: palves; +Cc: gdb-patches

Add two observers around corefile generation:

  - gcore_start
  - gcore_end

2014-05-22  Markus Metzger  <markus.t.metzger@intel.com>

	* gcore.c: Include "observer.h".
	(write_gcore_file): Rename to ...
	(write_gcore_file_1): ...this.
	(write_gcore_file): Call gcore notifiers.

doc/
	* observer.texi (GDB Observers): Add gcore_start and gcore_end.
---
 gdb/doc/observer.texi |  7 +++++++
 gdb/gcore.c           | 28 +++++++++++++++++++++++-----
 2 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/gdb/doc/observer.texi b/gdb/doc/observer.texi
index 61acbb2..d1056bd 100644
--- a/gdb/doc/observer.texi
+++ b/gdb/doc/observer.texi
@@ -258,3 +258,10 @@ This observer is used for internal testing.  Do not use.
 See testsuite/gdb.gdb/observer.exp.
 @end deftypefun
 
+@deftypefun void gcore_start (void)
+Called before generating a core file.
+@end deftypefun
+
+@deftypefun void gcore_end (void)
+Called after generating a core file.
+@end deftypefun
diff --git a/gdb/gcore.c b/gdb/gcore.c
index 7a4ded7..b4a5ff1 100644
--- a/gdb/gcore.c
+++ b/gdb/gcore.c
@@ -35,6 +35,7 @@
 #include "regset.h"
 #include "gdb_bfd.h"
 #include "readline/tilde.h"
+#include "observer.h"
 
 /* The largest amount of memory to read from the target at once.  We
    must throttle it to limit the amount of memory used by GDB during
@@ -61,12 +62,10 @@ create_gcore_bfd (const char *filename)
   return obfd;
 }
 
-/* write_gcore_file -- helper for gcore_command (exported).
-   Compose and write the corefile data to the core file.  */
-
+/* write_gcore_file_1 -- do the actual work of write_gcore_file.  */
 
-void
-write_gcore_file (bfd *obfd)
+static void
+write_gcore_file_1 (bfd *obfd)
 {
   struct cleanup *cleanup;
   void *note_data = NULL;
@@ -111,6 +110,25 @@ write_gcore_file (bfd *obfd)
   do_cleanups (cleanup);
 }
 
+/* write_gcore_file -- helper for gcore_command (exported).
+   Compose and write the corefile data to the core file.  */
+
+void
+write_gcore_file (bfd *obfd)
+{
+  volatile struct gdb_exception except;
+
+  observer_notify_gcore_start ();
+
+  TRY_CATCH (except, RETURN_MASK_ALL)
+    write_gcore_file_1 (obfd);
+
+  observer_notify_gcore_end ();
+
+  if (except.reason < 0)
+    throw_exception (except);
+}
+
 static void
 do_bfd_delete_cleanup (void *arg)
 {
-- 
1.8.3.1

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/3] make_corefile_notes: have caller free returned memory
@ 2014-05-22 12:19 Markus Metzger
  2014-05-22 12:19 ` [PATCH 3/3] btrace: pretend we're not replaying when generating a core file Markus Metzger
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Markus Metzger @ 2014-05-22 12:19 UTC (permalink / raw)
  To: palves; +Cc: gdb-patches

The various make_corefile_notes implementations for gdbarch as well as target
currently make an xfree cleanup on the data they return.  This causes problems
when trying to put a TRY_CATCH around the make_corefile_notes call.

Omit the make_cleanup and have the caller free the memory.

2014-05-22  Markus Metzger  <markus.t.metzger@intel.com>

	* fbsd-nat.c (fbsd_make_corefile_notes): Remove make_cleanup call.
	* gcore.c (write_gcore_file): Free memory returned from
	make_corefile_notes.
	* linux-tdep.c (linux_make_corefile_notes): Remove make_cleanup call.
	* procfs.c (procfs_make_note_section): Remove make_cleanup call.
---
 gdb/fbsd-nat.c   | 1 -
 gdb/gcore.c      | 5 +++++
 gdb/linux-tdep.c | 1 -
 gdb/procfs.c     | 1 -
 4 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/gdb/fbsd-nat.c b/gdb/fbsd-nat.c
index 9f30edf..4e115b2 100644
--- a/gdb/fbsd-nat.c
+++ b/gdb/fbsd-nat.c
@@ -214,6 +214,5 @@ fbsd_make_corefile_notes (struct target_ops *self, bfd *obfd, int *note_size)
 					  fname, psargs);
     }
 
-  make_cleanup (xfree, note_data);
   return note_data;
 }
diff --git a/gdb/gcore.c b/gdb/gcore.c
index e225080..7a4ded7 100644
--- a/gdb/gcore.c
+++ b/gdb/gcore.c
@@ -68,6 +68,7 @@ create_gcore_bfd (const char *filename)
 void
 write_gcore_file (bfd *obfd)
 {
+  struct cleanup *cleanup;
   void *note_data = NULL;
   int note_size = 0;
   asection *note_sec = NULL;
@@ -84,6 +85,8 @@ write_gcore_file (bfd *obfd)
   if (note_data == NULL || note_size == 0)
     error (_("Target does not support core file generation."));
 
+  cleanup = make_cleanup (xfree, note_data);
+
   /* Create the note section.  */
   note_sec = bfd_make_section_anyway_with_flags (obfd, "note0",
 						 SEC_HAS_CONTENTS
@@ -104,6 +107,8 @@ write_gcore_file (bfd *obfd)
   /* Write out the contents of the note section.  */
   if (!bfd_set_section_contents (obfd, note_sec, note_data, 0, note_size))
     warning (_("writing note section (%s)"), bfd_errmsg (bfd_get_error ()));
+
+  do_cleanups (cleanup);
 }
 
 static void
diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index c10b8ee..a8d52de 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -1497,7 +1497,6 @@ linux_make_corefile_notes (struct gdbarch *gdbarch, bfd *obfd, int *note_size,
   note_data = linux_make_mappings_corefile_notes (gdbarch, obfd,
 						  note_data, note_size);
 
-  make_cleanup (xfree, note_data);
   return note_data;
 }
 
diff --git a/gdb/procfs.c b/gdb/procfs.c
index 80b0a6a..7e1bed6 100644
--- a/gdb/procfs.c
+++ b/gdb/procfs.c
@@ -5521,7 +5521,6 @@ procfs_make_note_section (struct target_ops *self, bfd *obfd, int *note_size)
       xfree (auxv);
     }
 
-  make_cleanup (xfree, note_data);
   return note_data;
 }
 #else /* !Solaris */
-- 
1.8.3.1

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/3] make_corefile_notes: have caller free returned memory
  2014-05-22 12:19 [PATCH 1/3] make_corefile_notes: have caller free returned memory Markus Metzger
  2014-05-22 12:19 ` [PATCH 3/3] btrace: pretend we're not replaying when generating a core file Markus Metzger
  2014-05-22 12:19 ` [PATCH 2/3] observer, gcore: add corefile notes generation observers Markus Metzger
@ 2014-06-04 16:42 ` Pedro Alves
  2 siblings, 0 replies; 5+ messages in thread
From: Pedro Alves @ 2014-06-04 16:42 UTC (permalink / raw)
  To: Markus Metzger; +Cc: gdb-patches

On 05/22/2014 01:19 PM, Markus Metzger wrote:
> The various make_corefile_notes implementations for gdbarch as well as target
> currently make an xfree cleanup on the data they return.  This causes problems
> when trying to put a TRY_CATCH around the make_corefile_notes call.

Please clarify in the commit log / intro what these problems are.
I happen to know because you told me off list, but if you hadn't,
I'd have to ask.  :-)

I suggest adding:

 "Specifically, we get a stale cleanup error in restore_my_cleanups."

> Omit the make_cleanup and have the caller free the memory.
> 
> 2014-05-22  Markus Metzger  <markus.t.metzger@intel.com>
> 
> 	* fbsd-nat.c (fbsd_make_corefile_notes): Remove make_cleanup call.
> 	* gcore.c (write_gcore_file): Free memory returned from
> 	make_corefile_notes.
> 	* linux-tdep.c (linux_make_corefile_notes): Remove make_cleanup call.
> 	* procfs.c (procfs_make_note_section): Remove make_cleanup call.

OK.

Thanks,
-- 
Pedro Alves

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 3/3] btrace: pretend we're not replaying when generating a core file
  2014-05-22 12:19 ` [PATCH 3/3] btrace: pretend we're not replaying when generating a core file Markus Metzger
@ 2014-06-04 17:03   ` Pedro Alves
  0 siblings, 0 replies; 5+ messages in thread
From: Pedro Alves @ 2014-06-04 17:03 UTC (permalink / raw)
  To: Markus Metzger; +Cc: gdb-patches

On 05/22/2014 01:19 PM, Markus Metzger wrote:

> Use the gcore_start and gcore_end observers to pretend that we are not
> replaying while generating a core file.  This will forward fetch and store
> registers as well as xfer memory calls to the target beneath.

Using observers for this doesn't sound ideal to me -- e.g., mid-term as
we go multi-target, and make more things async (and potentially parallel,
even) we'd have to make the observer know which target is the current, and
whether it's record-btrace anyway.  As this is a target-side detail, I think
we should instead have a pair of target_prepare_to_generate_core /
target_done_generating_core target hooks.  Not much unlike target_prepare_to_store.
Could you try that please?

-- 
Pedro Alves

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2014-06-04 17:03 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-05-22 12:19 [PATCH 1/3] make_corefile_notes: have caller free returned memory Markus Metzger
2014-05-22 12:19 ` [PATCH 3/3] btrace: pretend we're not replaying when generating a core file Markus Metzger
2014-06-04 17:03   ` Pedro Alves
2014-05-22 12:19 ` [PATCH 2/3] observer, gcore: add corefile notes generation observers Markus Metzger
2014-06-04 16:42 ` [PATCH 1/3] make_corefile_notes: have caller free returned memory Pedro Alves

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).