[4/4] gdbsupport: add observer_debug_printf, OBSERVER_SCOPED_DEBUG_ENTER_EXIT

Message ID 20210424021006.22779-5-simon.marchi@polymtl.ca
State New
Headers show
Series
  • Improvements to observer debug output
Related show

Commit Message

Mike Frysinger via Gdb-patches April 24, 2021, 2:10 a.m.
Switch observer to use the "new" debug printf mechanism and sprinkle a
few debug prints.  Here's a small example of the output with "infrun"
and "observer" debug output enabled:

    [infrun] proceed: enter
      [observer] notify: start: observable target_resumed notify() called
        [observer] notify: start: calling observer mi-interp of observable target_resumed
        [observer] notify: end: calling observer mi-interp of observable target_resumed
        [observer] notify: start: calling observer py-inferior of observable target_resumed
        [observer] notify: end: calling observer py-inferior of observable target_resumed
      [observer] notify: end: observable target_resumed notify() called
      ...

gdbsupport/ChangeLog:

	* observable.h (observer_debug_printf,
	OBSERVER_SCOPED_DEBUG_START_END): New.
	(class observable) <notify, attach>: Use them.

Change-Id: If3ae4b6b65450ca3b7cae56698a87fc526688b86
---
 gdbsupport/observable.h | 30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)

-- 
2.30.1

Patch

diff --git a/gdbsupport/observable.h b/gdbsupport/observable.h
index bf85a74ec980..623bdbf0ca4c 100644
--- a/gdbsupport/observable.h
+++ b/gdbsupport/observable.h
@@ -24,6 +24,16 @@ 
 #include <functional>
 #include <vector>
 
+/* Print an "observer" debug statement.  */
+
+#define observer_debug_printf(fmt, ...) \
+  debug_prefixed_printf_cond (observer_debug, "observer", fmt, ##__VA_ARGS__)
+
+/* Print "observer" start/end debug statements.  */
+
+#define OBSERVER_SCOPED_DEBUG_START_END(fmt, ...) \
+  scoped_debug_start_end (observer_debug, "observer", fmt, ##__VA_ARGS__)
+
 namespace gdb
 {
 
@@ -82,6 +92,9 @@  class observable
      detached.  */
   void attach (const func_type &f, const char *name)
   {
+    observer_debug_printf ("Attaching observable %s to observer %s",
+			   name, m_name);
+
     m_observers.emplace_back (nullptr, f, name);
   }
 
@@ -89,6 +102,9 @@  class observable
      a token that can be used to later remove F.  */
   void attach (const func_type &f, const token &t, const char *name)
   {
+    observer_debug_printf ("Attaching observable %s to observer %s",
+			   name, m_name);
+
     m_observers.emplace_back (&t, f, name);
   }
 
@@ -104,17 +120,23 @@  class observable
 				  return o.token == &t;
 				});
 
+    observer_debug_printf ("Detaching observable %s from observer %s",
+			   iter->name, m_name);
+
     m_observers.erase (iter, m_observers.end ());
   }
 
   /* Notify all observers that are attached to this observable.  */
   void notify (T... args) const
   {
-    if (observer_debug)
-      fprintf_unfiltered (gdb_stdlog, "observable %s notify() called\n",
-			  m_name);
+    OBSERVER_SCOPED_DEBUG_START_END ("observable %s notify() called", m_name);
+
     for (auto &&e : m_observers)
-      e.func (args...);
+      {
+	OBSERVER_SCOPED_DEBUG_START_END ("calling observer %s of observable %s",
+					 e.name, m_name);
+	e.func (args...);
+      }
   }
 
 private: