Patchwork [08/10] btrace, python: Enable ptwrite listener registration.

login
register
mail settings
Submitter felix.willgerodt@intel.com
Date May 29, 2019, 8:47 a.m.
Message ID <1559119673-30516-9-git-send-email-felix.willgerodt@intel.com>
Download mbox | patch
Permalink /patch/32890/
State New
Headers show

Comments

felix.willgerodt@intel.com - May 29, 2019, 8:47 a.m.
From: Felix Willgerodt <felix.willgerodt@intel.com>

With this patch a default ptwrite listener is registered upon start of GDB.
It prints the plain ptwrite payload as hex.  The default listener can be
overwritten by registering a custom listener in python or by registering
"None", for no output at all.  Registering a listener function creates per
thread copies to allow unique internal states per thread.

2019-05-29  Felix Willgerodt  <felix.willgerodt@intel.com>

gdb/ChangeLog:
	* btrace.c (ftrace_add_pt): Add call to
	apply_ext_lang_ptwrite_listener.
	* btrace.h (btrace_thread_info): New member ptw_listener.
	* data-directory/Makefile.in: Add ptwrite.py.
	* extension-priv.h (struct extension_language_ops): New member
	apply_ptwrite_listener.
	* extension.c (apply_ext_lang_ptwrite_listener): New function.
	* extension.h (apply_ext_lang_ptwrite_listener): New declaration.
	* guile/guile.c (guile_extension_ops): Add
	gdbscm_load_ptwrite_listener.
	* python/lib/gdb/ptwrite.py: New file.
	* python/py-record-btrace.c (recpy_initialize_listener,
	get_ptwrite_listener): New function.
	* python/py-record-btrace.h (recpy_initialize_listener): New
	declaration.
	* python/py-record.c (gdbpy_load_ptwrite_listener): New function.
	* python/python-internal.h (gdbpy_load_ptwrite_listener): New
	declaration.
	* python/python.c (python_extension_ops): New member
	gdbpy_load_ptwrite_listener.

---
 gdb/btrace.c                   |  3 ++
 gdb/btrace.h                   |  3 ++
 gdb/data-directory/Makefile.in |  1 +
 gdb/extension-priv.h           |  4 ++
 gdb/extension.c                | 24 ++++++++++
 gdb/extension.h                |  3 ++
 gdb/guile/guile.c              |  1 +
 gdb/python/lib/gdb/ptwrite.py  | 83 ++++++++++++++++++++++++++++++++++
 gdb/python/py-record-btrace.c  | 40 ++++++++++++++++
 gdb/python/py-record-btrace.h  |  3 ++
 gdb/python/py-record.c         | 29 ++++++++++++
 gdb/python/python-internal.h   |  3 ++
 gdb/python/python.c            |  2 +
 13 files changed, 199 insertions(+)
 create mode 100644 gdb/python/lib/gdb/ptwrite.py
Metzger, Markus T - June 4, 2019, 12:36 p.m.
Hello Felix,

> With this patch a default ptwrite listener is registered upon start of GDB.
> It prints the plain ptwrite payload as hex.  The default listener can be
> overwritten by registering a custom listener in python or by registering
> "None", for no output at all.  Registering a listener function creates per
> thread copies to allow unique internal states per thread.
> 
> 2019-05-29  Felix Willgerodt  <felix.willgerodt@intel.com>
> 
> gdb/ChangeLog:
> 	* btrace.c (ftrace_add_pt): Add call to
> 	apply_ext_lang_ptwrite_listener.
> 	* btrace.h (btrace_thread_info): New member ptw_listener.
> 	* data-directory/Makefile.in: Add ptwrite.py.
> 	* extension-priv.h (struct extension_language_ops): New member
> 	apply_ptwrite_listener.
> 	* extension.c (apply_ext_lang_ptwrite_listener): New function.
> 	* extension.h (apply_ext_lang_ptwrite_listener): New declaration.
> 	* guile/guile.c (guile_extension_ops): Add
> 	gdbscm_load_ptwrite_listener.
> 	* python/lib/gdb/ptwrite.py: New file.
> 	* python/py-record-btrace.c (recpy_initialize_listener,
> 	get_ptwrite_listener): New function.
> 	* python/py-record-btrace.h (recpy_initialize_listener): New
> 	declaration.
> 	* python/py-record.c (gdbpy_load_ptwrite_listener): New function.
> 	* python/python-internal.h (gdbpy_load_ptwrite_listener): New
> 	declaration.
> 	* python/python.c (python_extension_ops): New member
> 	gdbpy_load_ptwrite_listener.
> 
> ---
>  gdb/btrace.c                   |  3 ++
>  gdb/btrace.h                   |  3 ++
>  gdb/data-directory/Makefile.in |  1 +
>  gdb/extension-priv.h           |  4 ++
>  gdb/extension.c                | 24 ++++++++++
>  gdb/extension.h                |  3 ++
>  gdb/guile/guile.c              |  1 +
>  gdb/python/lib/gdb/ptwrite.py  | 83
> ++++++++++++++++++++++++++++++++++
>  gdb/python/py-record-btrace.c  | 40 ++++++++++++++++
>  gdb/python/py-record-btrace.h  |  3 ++
>  gdb/python/py-record.c         | 29 ++++++++++++
>  gdb/python/python-internal.h   |  3 ++
>  gdb/python/python.c            |  2 +
>  13 files changed, 199 insertions(+)
>  create mode 100644 gdb/python/lib/gdb/ptwrite.py

When registering a new PTWRITE listener, could GDB check the libipt version and give
an error if it does not support PTW?

> @@ -1308,6 +1309,8 @@ ftrace_add_pt (struct btrace_thread_info *btinfo,
>    uint64_t offset;
>    int status;
> 
> +  apply_ext_lang_ptwrite_listener (inferior_ptid);

We may decode the trace in chunks by calling ftrace_add_pt () multiple times.
This would need to preserve (the state of) the installed listener.

> @@ -0,0 +1,83 @@
> +# Ptwrite utilities.
> +# Copyright (C) 2018 Free Software Foundation, Inc.

It's 2019, meanwhile.

> +
> +# 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/>.
> +
> +"""Utilities for working with ptwrite listeners."""
> +
> +import gdb
> +from copy import deepcopy
> +
> +
> +def default_listener(payload, ip):

I somehow expected some PtwritePrinter class.

> +    """Default listener that is active upon starting GDB."""
> +    return "payload: {:#x}".format(payload)

Should we return the payload (in hex) without the prefix?

> @@ -735,3 +735,32 @@ gdbpy_stop_recording (PyObject *self, PyObject *args)
> 
>    Py_RETURN_NONE;
>  }
> +
> +/* Used for registering the default ptwrite listener to the current thread.  A
> +   pointer to this function is stored in the python extension interface.  */
> +
> +enum ext_lang_bt_status
> +gdbpy_load_ptwrite_listener (const struct extension_language_defn *extlang,
> +			     ptid_t inferior_ptid)

PTWRITE is very PT specific.  Any chance this could go into py-record-btrace.c?

> +{
> +  struct gdbarch *gdbarch = NULL;
> +
> +  if (!gdb_python_initialized)
> +    return EXT_LANG_BT_NO_FILTERS;
> +
> +  try
> +    {
> +      gdbarch = target_gdbarch ();
> +    }
> +  catch (const gdb_exception &except)
> +    {
> +      /* Let gdb try to print the stack trace.  */
> +      return EXT_LANG_BT_NO_FILTERS;

I fail to understand the comment.

> +    }
> +
> +  gdbpy_enter enter_py (gdbarch, current_language);
> +
> +  recpy_initialize_listener (inferior_ptid);
> +
> +  return EXT_LANG_BT_OK;
> +}

Thanks,
Markus.
Intel Deutschland GmbH
Registered Address: Am Campeon 10-12, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de
Managing Directors: Christin Eisenschmid, Gary Kershaw
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928

Patch

diff --git a/gdb/btrace.c b/gdb/btrace.c
index d3e2f46c678..5bdef1c1768 100644
--- a/gdb/btrace.c
+++ b/gdb/btrace.c
@@ -34,6 +34,7 @@ 
 #include "common/rsp-low.h"
 #include "gdbcmd.h"
 #include "cli/cli-utils.h"
+#include "extension.h"
 
 /* For maintenance commands.  */
 #include "record-btrace.h"
@@ -1308,6 +1309,8 @@  ftrace_add_pt (struct btrace_thread_info *btinfo,
   uint64_t offset;
   int status;
 
+  apply_ext_lang_ptwrite_listener (inferior_ptid);
+
   for (;;)
     {
       struct pt_insn insn;
diff --git a/gdb/btrace.h b/gdb/btrace.h
index d29a1633ecf..7a80f5a44e1 100644
--- a/gdb/btrace.h
+++ b/gdb/btrace.h
@@ -354,6 +354,9 @@  struct btrace_thread_info
      stepping through the execution history.  */
   std::vector<std::string> aux_data;
 
+  /* PyObject pointer to the ptwrite listener function.  */
+  void *ptw_listener = nullptr;
+
   /* The function level offset.  When added to each function's LEVEL,
      this normalizes the function levels such that the smallest level
      becomes zero.  */
diff --git a/gdb/data-directory/Makefile.in b/gdb/data-directory/Makefile.in
index 88147125ccd..39170e786e8 100644
--- a/gdb/data-directory/Makefile.in
+++ b/gdb/data-directory/Makefile.in
@@ -74,6 +74,7 @@  PYTHON_FILE_LIST = \
 	gdb/frames.py \
 	gdb/printing.py \
 	gdb/prompt.py \
+	gdb/ptwrite.py \
 	gdb/types.py \
 	gdb/unwinder.py \
 	gdb/xmethod.py \
diff --git a/gdb/extension-priv.h b/gdb/extension-priv.h
index 97594f853a2..ced4cd10390 100644
--- a/gdb/extension-priv.h
+++ b/gdb/extension-priv.h
@@ -187,6 +187,10 @@  struct extension_language_ops
      enum ext_lang_frame_args args_type,
      struct ui_out *out, int frame_low, int frame_high);
 
+  /* Used for registering the ptwrite listener to the current thread.  */
+  enum ext_lang_bt_status (*apply_ptwrite_listener)
+    (const struct extension_language_defn *, ptid_t inferior_ptid);
+
   /* Update values held by the extension language when OBJFILE is discarded.
      New global types must be created for every such value, which must then be
      updated to use the new types.
diff --git a/gdb/extension.c b/gdb/extension.c
index 8637bc53f2e..e463f0d9b87 100644
--- a/gdb/extension.c
+++ b/gdb/extension.c
@@ -570,6 +570,30 @@  apply_ext_lang_frame_filter (struct frame_info *frame,
   return EXT_LANG_BT_NO_FILTERS;
 }
 
+/* Used for registering the ptwrite listener to the current thread.  */
+
+enum ext_lang_bt_status
+apply_ext_lang_ptwrite_listener (ptid_t inferior_ptid)
+{
+  int i;
+  const struct extension_language_defn *extlang;
+
+  ALL_ENABLED_EXTENSION_LANGUAGES (i, extlang)
+    {
+      enum ext_lang_bt_status status;
+
+      if (extlang->ops->apply_ptwrite_listener == NULL)
+	continue;
+
+      status = extlang->ops->apply_ptwrite_listener (extlang, inferior_ptid);
+
+      if (status != EXT_LANG_BT_NO_FILTERS)
+	return status;
+    }
+
+  return EXT_LANG_BT_NO_FILTERS;
+}
+
 /* Update values held by the extension language when OBJFILE is discarded.
    New global types must be created for every such value, which must then be
    updated to use the new types.
diff --git a/gdb/extension.h b/gdb/extension.h
index 2f1b71851c6..cb5fe89a16d 100644
--- a/gdb/extension.h
+++ b/gdb/extension.h
@@ -291,6 +291,9 @@  extern enum ext_lang_bt_status apply_ext_lang_frame_filter
    enum ext_lang_frame_args args_type,
    struct ui_out *out, int frame_low, int frame_high);
 
+enum ext_lang_bt_status apply_ext_lang_ptwrite_listener
+  (ptid_t inferior_ptid);
+
 extern void preserve_ext_lang_values (struct objfile *, htab_t copied_types);
 
 extern const struct extension_language_defn *get_breakpoint_cond_ext_lang
diff --git a/gdb/guile/guile.c b/gdb/guile/guile.c
index 9247fd64122..9ba63da9fcd 100644
--- a/gdb/guile/guile.c
+++ b/gdb/guile/guile.c
@@ -150,6 +150,7 @@  const struct extension_language_ops guile_extension_ops =
   gdbscm_apply_val_pretty_printer,
 
   NULL, /* gdbscm_apply_frame_filter, */
+  NULL, /* gdbscm_load_ptwrite_listener, */
 
   gdbscm_preserve_values,
 
diff --git a/gdb/python/lib/gdb/ptwrite.py b/gdb/python/lib/gdb/ptwrite.py
new file mode 100644
index 00000000000..a485ce89ee9
--- /dev/null
+++ b/gdb/python/lib/gdb/ptwrite.py
@@ -0,0 +1,83 @@ 
+# Ptwrite utilities.
+# Copyright (C) 2018 Free Software Foundation, Inc.
+
+# 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/>.
+
+"""Utilities for working with ptwrite listeners."""
+
+import gdb
+from copy import deepcopy
+
+
+def default_listener(payload, ip):
+    """Default listener that is active upon starting GDB."""
+    return "payload: {:#x}".format(payload)
+
+# This dict contains the per thread copies of the listener function and the
+# global template listener, from which the copies are created.
+_ptwrite_listener = {"global" : default_listener}
+
+
+def _update_listener_dict(thread_list):
+    """Helper function to update the listener dict.
+
+    Discards listener copies of threads that already exited and registers
+    copies of the listener for new threads."""
+    # thread_list[x].ptid returns the tuple (pid, lwp, tid)
+    lwp_list = [i.ptid[1] for i in thread_list]
+
+    # clean-up old listeners
+    for key in _ptwrite_listener:
+        if key not in lwp_list and not "global":
+            _ptwrite_listener.pop(key)
+
+    # Register listener for new threads
+    for key in lwp_list:
+        if key not in _ptwrite_listener.keys():
+            _ptwrite_listener[key] = deepcopy(_ptwrite_listener["global"])
+
+
+def _clear_traces(thread_list):
+    """Helper function to clear the trace of all threads."""
+    current_thread = gdb.selected_thread()
+
+    for thread in thread_list:
+        thread.switch()
+        gdb.current_recording().clear_trace()
+
+    current_thread.switch()
+
+
+def register_listener(listener):
+    """Register the ptwrite listener function."""
+    global _ptwrite_listener
+
+    if listener is not None and not callable(listener):
+        raise TypeError("Listener must be callable!")
+
+    thread_list = gdb.Inferior.threads(gdb.selected_inferior())
+    _clear_traces(thread_list)
+
+    _ptwrite_listener.clear()
+    _ptwrite_listener["global"] = listener
+
+    _update_listener_dict(thread_list)
+
+
+def get_listener():
+    """Returns the listeners of the current thread."""
+    thread_list = gdb.Inferior.threads(gdb.selected_inferior())
+    _update_listener_dict(thread_list)
+
+    return _ptwrite_listener[gdb.selected_thread().ptid[1]]
diff --git a/gdb/python/py-record-btrace.c b/gdb/python/py-record-btrace.c
index 2a4af551146..c7ad47d64a0 100644
--- a/gdb/python/py-record-btrace.c
+++ b/gdb/python/py-record-btrace.c
@@ -772,6 +772,46 @@  recpy_bt_function_call_history (PyObject *self, void *closure)
   return btpy_list_new (tinfo, first, last, 1, &recpy_func_type);
 }
 
+/* Helper function returning the current ptwrite listener.  Returns nullptr
+   in case of errors.  */
+
+PyObject *
+get_ptwrite_listener ()
+{
+  PyObject *module = PyImport_ImportModule ("gdb.ptwrite");
+
+  if (PyErr_Occurred ())
+  {
+    gdbpy_print_stack ();
+    return nullptr;
+  }
+
+  PyObject *default_ptw_listener = PyObject_CallMethod (module,	"get_listener",
+							NULL);
+
+  gdb_Py_DECREF (module);
+
+  if (PyErr_Occurred ())
+    gdbpy_print_stack ();
+
+  return default_ptw_listener;
+}
+
+/* Helper function to initialize the ptwrite listener.  Returns nullptr in
+   case of errors.  */
+
+PyObject *
+recpy_initialize_listener (ptid_t inferior_ptid)
+{
+  struct thread_info * const tinfo = find_thread_ptid (inferior_ptid);
+
+  gdb_assert (tinfo != nullptr);
+
+  tinfo->btrace.ptw_listener = get_ptwrite_listener ();
+
+  return (PyObject *) tinfo->btrace.ptw_listener;
+}
+
 /* Implementation of BtraceRecord.goto (self, BtraceInstruction) -> None.  */
 
 PyObject *
diff --git a/gdb/python/py-record-btrace.h b/gdb/python/py-record-btrace.h
index 8056a5b75cb..8dff1917729 100644
--- a/gdb/python/py-record-btrace.h
+++ b/gdb/python/py-record-btrace.h
@@ -91,4 +91,7 @@  extern PyObject *recpy_bt_func_prev (PyObject *self, void *closure);
 /* Implementation of RecordFunctionSegment.next [RecordFunctionSegment].  */
 extern PyObject *recpy_bt_func_next (PyObject *self, void *closure);
 
+/* Helper function returning the ptwrite listener by thread id.  */
+extern PyObject *recpy_initialize_listener (ptid_t inferior_ptid);
+
 #endif /* PYTHON_PY_RECORD_BTRACE_H */
diff --git a/gdb/python/py-record.c b/gdb/python/py-record.c
index 855b4089d87..86dc0dfaa9a 100644
--- a/gdb/python/py-record.c
+++ b/gdb/python/py-record.c
@@ -735,3 +735,32 @@  gdbpy_stop_recording (PyObject *self, PyObject *args)
 
   Py_RETURN_NONE;
 }
+
+/* Used for registering the default ptwrite listener to the current thread.  A
+   pointer to this function is stored in the python extension interface.  */
+
+enum ext_lang_bt_status
+gdbpy_load_ptwrite_listener (const struct extension_language_defn *extlang,
+			     ptid_t inferior_ptid)
+{
+  struct gdbarch *gdbarch = NULL;
+
+  if (!gdb_python_initialized)
+    return EXT_LANG_BT_NO_FILTERS;
+
+  try
+    {
+      gdbarch = target_gdbarch ();
+    }
+  catch (const gdb_exception &except)
+    {
+      /* Let gdb try to print the stack trace.  */
+      return EXT_LANG_BT_NO_FILTERS;
+    }
+
+  gdbpy_enter enter_py (gdbarch, current_language);
+
+  recpy_initialize_listener (inferior_ptid);
+
+  return EXT_LANG_BT_OK;
+}
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 69ff1fe30de..552e2d8b76a 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -397,6 +397,9 @@  extern enum ext_lang_rc gdbpy_apply_val_pretty_printer
    struct value *val,
    const struct value_print_options *options,
    const struct language_defn *language);
+extern enum ext_lang_bt_status gdbpy_load_ptwrite_listener
+  (const struct extension_language_defn *extlang,
+   ptid_t inferior_ptid);
 extern enum ext_lang_bt_status gdbpy_apply_frame_filter
   (const struct extension_language_defn *,
    struct frame_info *frame, frame_filter_flags flags,
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 4dad8ec10d1..2f2fa4bb236 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -178,6 +178,8 @@  const struct extension_language_ops python_extension_ops =
 
   gdbpy_apply_frame_filter,
 
+  gdbpy_load_ptwrite_listener,
+
   gdbpy_preserve_values,
 
   gdbpy_breakpoint_has_cond,