This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
Re: [PATCH v3 6/8] python: Implement btrace Python bindings for record history.
- From: Doug Evans <dje at google dot com>
- To: Tim Wiederhake <tim dot wiederhake at intel dot com>
- Cc: gdb-patches at sourceware dot org, palves at redhat dot com, markus dot t dot metzger at intel dot com
- Date: Thu, 01 Dec 2016 00:46:38 +0000
- Subject: Re: [PATCH v3 6/8] python: Implement btrace Python bindings for record history.
- Authentication-results: sourceware.org; auth=none
Tim Wiederhake writes:
> This patch implements the gdb.Record Python object methods and fields for
> record target btrace. Additionally, add a stub for record target full.
>
> 2016-11-21 Tim Wiederhake <tim.wiederhake@intel.com>
>
> gdb/ChangeLog:
>
> * Makefile.in (SUBDIR_PYTHON_OBS): Add py-btrace.o.
> (SUBDIR_PYTHON_SRCS): Add py-btrace.c.
> * python/py-btrace.c: New file.
> * python/py-btrace.h: New file.
> * python/python-internal.h (gdbpy_initialize_btrace): New export.
> * python/python.c (_initialize_python): Add gdbpy_initialize_btrace.
> * record-btrace.c: Add conditional includes for python/py-record.h
> and python/py-btrace.h
> (record_btrace_python_interface): New function.
> (init_record_btrace_ops): Add record_btrace_python_interface.
> * record-full.c: Add conditional include for python/py-record.h.
> (record_full_python_interface): New function.
> (init_record_full_ops): Add record_full_python_interface.
>
>
> ---
> gdb/Makefile.in | 2 +
> gdb/python/py-btrace.c | 994
+++++++++++++++++++++++++++++++++++++++++++
> gdb/python/py-btrace.h | 32 ++
> gdb/python/python-internal.h | 2 +
> gdb/python/python.c | 1 +
> gdb/record-btrace.c | 38 ++
> gdb/record-full.c | 20 +
> 7 files changed, 1089 insertions(+)
> create mode 100644 gdb/python/py-btrace.c
> create mode 100644 gdb/python/py-btrace.h
>
> diff --git a/gdb/Makefile.in b/gdb/Makefile.in
> index 3340b5e..8bc0bf8 100644
> --- a/gdb/Makefile.in
> +++ b/gdb/Makefile.in
> @@ -381,6 +381,7 @@ SUBDIR_PYTHON_OBS = \
> py-block.o \
> py-bpevent.o \
> py-breakpoint.o \
> + py-btrace.o \
> py-cmd.o \
> py-continueevent.o \
> py-xmethods.o \
> @@ -422,6 +423,7 @@ SUBDIR_PYTHON_SRCS = \
> python/py-block.c \
> python/py-bpevent.c \
> python/py-breakpoint.c \
> + python/py-btrace.c \
> python/py-cmd.c \
> python/py-continueevent.c \
> python/py-xmethods.c \
> diff --git a/gdb/python/py-btrace.c b/gdb/python/py-btrace.c
> new file mode 100644
> index 0000000..0420ff6
> --- /dev/null
> +++ b/gdb/python/py-btrace.c
> @@ -0,0 +1,994 @@
> +/* Python interface to btrace instruction history.
> +
> + Copyright 2016 Free Software Foundation, Inc.
> +
> + Contributed by Intel Corp. <tim.wiederhake@intel.com>
> +
> + This file is part of GDB.
> +
> + 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/>. */
> +
> +#include "defs.h"
> +#include "py-btrace.h"
> +#include "py-record.h"
> +#include "gdbcore.h"
> +#include "gdbcmd.h"
> +#include "gdbthread.h"
> +#include "btrace.h"
> +#include "disasm.h"
> +
> +#if defined (IS_PY3K)
> +
> +#define BTPY_PYSLICE(x) (x)
> +#define PyInt_FromSsize_t PyLong_FromSsize_t
> +#define PyInt_FromSize_t PyLong_FromSize_t
> +#define PyInt_AsSsize_t PyLong_AsSsize_t
The PyInt_* macros should probably go to python-internal.h.
grep for other "#define Py*" macros there.
> +
> +#else
> +
> +#define BTPY_PYSLICE(x) ((PySliceObject *) x)
> +
> +#endif
> +
> +#define BTPY_REQUIRE_VALID_INSN(obj, iter) \
> + do { \
> + struct thread_info *tinfo = find_thread_ptid (obj->ptid); \
> + if (tinfo == NULL || btrace_is_empty (tinfo)) \
> + return PyErr_Format (gdbpy_gdb_error, _("Empty branch trace."));\
> + if (0 == btrace_find_insn_by_number (&iter, &tinfo->btrace, \
> + obj->number)) \
I don't remember having a convention for conditions, but it is rare in gdb
to have "if (constant == expression)" and everytime I see it I have to read
it backwards.
I'm guessing it's written that way because of the line wrap.
This is ok, I just don't want to give tacit approval for
writing conditions this way.
> + return PyErr_Format (gdbpy_gdb_error, _("No such instruction."));\
> + } while (0)
> +
> +#define BTPY_REQUIRE_VALID_CALL(obj, iter) \
> + do { \
> + struct thread_info *tinfo = find_thread_ptid (obj->ptid); \
> + if (tinfo == NULL || btrace_is_empty (tinfo)) \
> + return PyErr_Format (gdbpy_gdb_error, _("Empty branch trace."));\
> + if (0 == btrace_find_call_by_number (&iter, &tinfo->btrace, \
> + obj->number)) \
Ditto.
> + return PyErr_Format (gdbpy_gdb_error, _("No such call segment."));\
> + } while (0)
> +
> +/* Python object for btrace records. This can either be an instruction
or a
> + function call segment, depending on the chosen type. */
> +
> +typedef struct {
> + PyObject_HEAD
> +
> + /* The thread this object belongs to. */
> + ptid_t ptid;
> +
> + /* Instruction number or function call segment number, depending on
the type
> + of this object. */
> + Py_ssize_t number;
> +} btpy_object;
> +
> +/* Python object for btrace record lists. */
> +
> +typedef struct {
> + PyObject_HEAD
> +
> + /* The thread this list belongs to. */
> + ptid_t ptid;
> +
> + /* The first index being part of this list. */
> + Py_ssize_t first;
I'm guessing these members are signed because they can
be negative (-1?). Is that correct?
> +
> + /* The last index begin part of this list. */
> + Py_ssize_t last;
> +
> + /* Stride size. */
> + Py_ssize_t step;
> +
> + /* Either &BTPY_CALL_TYPE or &BTPY_INSN_TYPE. */
> + PyTypeObject* element_type;
> +} btpy_list_object;
> +
> +/* Python type for btrace instructions. */
> +
> +static PyTypeObject btpy_insn_type = {
> + PyVarObject_HEAD_INIT (NULL, 0)
> +};
> +
> +/* Python type for btrace function-calls. */
> +
> +static PyTypeObject btpy_call_type = {
> + PyVarObject_HEAD_INIT (NULL, 0)
> +};
> +
> +/* Python type for btrace lists. */
> +
> +static PyTypeObject btpy_list_type = {
> + PyVarObject_HEAD_INIT (NULL, 0)
> +};
> +
> +/* Create a new gdb.BtraceInstruction or gdb.BtraceFunctionCall object,
> + depending on TYPE. */
> +
> +static PyObject *
> +btpy_new (ptid_t ptid, Py_ssize_t number, PyTypeObject* type)
> +{
> + btpy_object * const obj = PyObject_New (btpy_object, type);
> +
> + if (obj == NULL)
> + return NULL;
> +
> + obj->ptid = ptid;
> + obj->number = number;
> +
> + return (PyObject *) obj;
> +}
> +
> +/* Create a new gdb.BtraceInstruction object. */
> +
> +static PyObject *
> +btpy_insn_new (ptid_t ptid, Py_ssize_t number)
> +{
> + return btpy_new (ptid, number, &btpy_insn_type);
> +}
> +
> +/* Create a new gdb.BtraceFunctionCall object. */
> +
> +static PyObject *
> +btpy_call_new (ptid_t ptid, Py_ssize_t number)
> +{
> + return btpy_new (ptid, number, &btpy_call_type);
> +}
> +
> +/* Create a new gdb.BtraceList object. */
> +
> +static PyObject *
> +btpy_list_new (ptid_t ptid, Py_ssize_t first, Py_ssize_t last,
Py_ssize_t step,
> + PyTypeObject *element_type)
> +{
> + btpy_list_object * const obj = PyObject_New (btpy_list_object,
> + &btpy_list_type);
> +
> + if (obj == NULL)
> + return NULL;
> +
> + obj->ptid = ptid;
> + obj->first = first;
> + obj->last = last;
> + obj->step = step;
> + obj->element_type = element_type;
> +
> + return (PyObject *) obj;
> +}
> +
> +/* Implementation of BtraceInstruction.number [int] and
> + BtraceFunctionCall.number [int]. */
> +
> +static PyObject *
> +btpy_number (PyObject *self, void *closure)
> +{
> + const btpy_object * const obj = (btpy_object *) self;
> +
> + return PyInt_FromSsize_t (obj->number);
> +}
> +
> +/* Implementation of BtraceInstruction.__hash__ () -> int and
> + BtraceFunctionCall.__hash__ () -> int. */
> +
> +static Py_hash_t
> +btpy_hash (PyObject *self)
> +{
> + const btpy_object * const obj = (btpy_object *) self;
> +
> + return obj->number;
> +}
> +
> +/* Implementation of BtraceInstruction.error [int]. Returns the
> + error code for gaps. */
> +
> +static PyObject *
> +btpy_insn_error (PyObject *self, void *closure)
> +{
> + const btpy_object * const obj = (btpy_object *) self;
> + struct btrace_insn_iterator iter;
> + int error;
> +
> + BTPY_REQUIRE_VALID_INSN (obj, iter);
> +
> + error = btrace_insn_get_error (&iter);
> +
> + if (error == 0)
> + Py_RETURN_NONE;
> +
> + return PyInt_FromLong (error);
> +}
> +
> +/* Implementation of BtraceInstruction.symbol [gdb.Symtab_and_line].
> + Return the symbol associated with this instruction. */
> +
> +static PyObject *
> +btpy_insn_symbol (PyObject *self, void *closure)
The name of this function is confusing.
The result is not a symbol, it's a SAL.
E.g. BtraceFunctionCall.symbol returns a symbol.
Any reason to not name this BtraceInstruction.sal
[and btpy_insn_sal] ?
(or some such)
> +{
> + const btpy_object * const obj = (btpy_object *) self;
> + const struct btrace_insn *insn;
> + struct btrace_insn_iterator iter;
> + PyObject *result = NULL;
> +
> + BTPY_REQUIRE_VALID_INSN (obj, iter);
> +
> + insn = btrace_insn_get (&iter);
> + if (insn == NULL)
> + Py_RETURN_NONE;
> +
> + TRY
> + {
> + result = symtab_and_line_to_sal_object (find_pc_line (insn->pc,
0));
> + }
> + CATCH (except, RETURN_MASK_ALL)
> + {
> + GDB_PY_HANDLE_EXCEPTION (except);
> + }
> + END_CATCH
> +
> + return result;
> +}
> +
> +/* Implementation of BtraceInstruction.pc [int]. Returns
> + the instruction address. */
> +
> +static PyObject *
> +btpy_insn_pc (PyObject *self, void *closure)
> +{
> + const btpy_object * const obj = (btpy_object *) self;
> + const struct btrace_insn *insn;
> + struct btrace_insn_iterator iter;
> +
> + BTPY_REQUIRE_VALID_INSN (obj, iter);
> +
> + insn = btrace_insn_get (&iter);
> + if (insn == NULL)
> + Py_RETURN_NONE;
> +
> + return PyInt_FromSize_t (insn->pc);
I think(!) this should use gdb_py_long_from_ulongest.
> +}
> +
> +/* Implementation of BtraceInstruction.size [int]. Returns
> + the instruction size. */
> +
> +static PyObject *
> +btpy_insn_size (PyObject *self, void *closure)
> +{
> + const btpy_object * const obj = (btpy_object *) self;
> + const struct btrace_insn *insn;
> + struct btrace_insn_iterator iter;
> +
> + BTPY_REQUIRE_VALID_INSN (obj, iter);
> +
> + insn = btrace_insn_get (&iter);
> + if (insn == NULL)
> + Py_RETURN_NONE;
> +
> + return PyInt_FromLong (insn->size);
> +}
> +
> +/* Implementation of BtraceInstruction.is_speculative [bool].
> + Returns if this instruction was executed speculatively. */
> +
> +static PyObject *
> +btpy_insn_is_speculative (PyObject *self, void *closure)
> +{
> + const btpy_object * const obj = (btpy_object *) self;
> + const struct btrace_insn *insn;
> + struct btrace_insn_iterator iter;
> +
> + BTPY_REQUIRE_VALID_INSN (obj, iter);
> +
> + insn = btrace_insn_get (&iter);
> + if (insn == NULL)
> + Py_RETURN_NONE;
> +
> + if (insn->flags & BTRACE_INSN_FLAG_SPECULATIVE)
> + Py_RETURN_TRUE;
> + else
> + Py_RETURN_FALSE;
> +}
> +
> +/* Implementation of BtraceInstruction.data [buffer].
> + Returns raw instruction data. */
> +
> +static PyObject *
> +btpy_insn_data (PyObject *self, void *closure)
> +{
> + const btpy_object * const obj = (btpy_object *) self;
> + const struct btrace_insn *insn;
> + struct btrace_insn_iterator iter;
> + gdb_byte *buffer = NULL;
> + PyObject *object;
> +
> + BTPY_REQUIRE_VALID_INSN (obj, iter);
> +
> + insn = btrace_insn_get (&iter);
> + if (insn == NULL)
> + Py_RETURN_NONE;
> +
> + TRY
> + {
> + buffer = (gdb_byte *) xmalloc (insn->size);
> + read_memory (insn->pc, buffer, insn->size);
> + }
> + CATCH (except, RETURN_MASK_ALL)
> + {
> + xfree (buffer);
> + GDB_PY_HANDLE_EXCEPTION (except);
> + }
> + END_CATCH
> +
> + object = PyBytes_FromStringAndSize ((const char*) buffer, insn->size);
> + xfree (buffer);
> +
> + if (object == NULL)
> + return NULL;
> +
> + return PyMemoryView_FromObject (object);
> +}
> +
> +/* Implementation of BtraceInstruction.decode [str]. Returns
> + the instruction as human readable string. */
> +
> +static PyObject *
> +btpy_insn_decode (PyObject *self, void *closure)
A disassembler is great, but it's a generally useful facility.
I'd rather see a general purpose disassemble function
than BtraceInstruction.decode.
There is already gdb.Architecture.disassemble (since the argument
is still a pc and not a set of bytes).
Will that not work here?
[Do we need BtraceInstruction.decode?]
> +{
> + const btpy_object * const obj = (btpy_object *) self;
> + const struct btrace_insn *insn;
> + struct btrace_insn_iterator iter;
> + struct cleanup *cleanup;
> + struct ui_file *strfile;
> + char* buffer;
> + PyObject *object;
> +
> + BTPY_REQUIRE_VALID_INSN (obj, iter);
> +
> + insn = btrace_insn_get (&iter);
> + if (insn == NULL)
> + {
> + int error_code = btrace_insn_get_error (&iter);
> + const struct btrace_config *config;
> +
> + config = btrace_conf (&find_thread_ptid (obj->ptid)->btrace);
> + return PyBytes_FromString (btrace_decode_error (config->format,
> + error_code));
> + }
> +
> + strfile = mem_fileopen ();
> + cleanup = make_cleanup_ui_file_delete (strfile);
> +
> + gdb_print_insn (target_gdbarch (), insn->pc, strfile, NULL);
> +
> + buffer = ui_file_xstrdup (strfile, NULL);
> + make_cleanup (xfree, buffer);
> +
> + object = PyBytes_FromString (buffer);
> +
> + do_cleanups (cleanup);
> + return object;
> +}
> +
> +/* Implementation of BtraceFunctionCall.level [int]. Returns the
> + call level. */
> +
> +static PyObject *
> +btpy_call_level (PyObject *self, void *closure)
> +{
> + const btpy_object * const obj = (btpy_object *) self;
> + const struct btrace_function *func;
> + struct btrace_call_iterator iter;
> +
> + BTPY_REQUIRE_VALID_CALL (obj, iter);
> +
> + func = btrace_call_get (&iter);
> + if (func == NULL)
> + Py_RETURN_NONE;
> +
> + return PyInt_FromLong (iter.btinfo->level + func->level);
> +}
> +
> +/* Implementation of BtraceFunctionCall.symbol [gdb.Symbol]. Returns
> + the symbol associated with this function call. */
> +
> +static PyObject *
> +btpy_call_symbol (PyObject *self, void *closure)
> +{
> + const btpy_object * const obj = (btpy_object *) self;
> + const struct btrace_function *func;
> + struct btrace_call_iterator iter;
> +
> + BTPY_REQUIRE_VALID_CALL (obj, iter);
> +
> + func = btrace_call_get (&iter);
> + if (func == NULL)
> + Py_RETURN_NONE;
> +
> + if (func->sym == NULL)
> + Py_RETURN_NONE;
> +
> + return symbol_to_symbol_object (func->sym);
> +}
> +
> +/* Implementation of BtraceFunctionCall.instructions [list].
> + Return the list of instructions that belong to this function call.
*/
> +
> +static PyObject *
> +btpy_call_instructions (PyObject *self, void *closure)
> +{
> + const btpy_object * const obj = (btpy_object *) self;
> + const struct btrace_function *func;
> + struct btrace_call_iterator iter;
> + unsigned int len;
> +
> + BTPY_REQUIRE_VALID_CALL (obj, iter);
> +
> + func = btrace_call_get (&iter);
> + if (func == NULL)
> + Py_RETURN_NONE;
> +
> + len = VEC_length (btrace_insn_s, func->insn);
> +
> + /* Gaps count as one instruction. */
> + if (len == 0)
> + len = 1;
> +
> + return btpy_list_new (obj->ptid, func->insn_offset, func->insn_offset
+ len,
> + 1, &btpy_insn_type);
> +}
> +
> +/* Implementation of BtraceFunctionCall.up [gdb.BtraceRecordCall].
> + Return the caller / returnee of this function. */
> +
> +static PyObject *
> +btpy_call_up (PyObject *self, void *closure)
> +{
> + const btpy_object * const obj = (btpy_object *) self;
> + const struct btrace_function *func;
> + struct btrace_call_iterator iter;
> +
> + BTPY_REQUIRE_VALID_CALL (obj, iter);
> +
> + func = btrace_call_get (&iter);
> + if (func == NULL)
> + Py_RETURN_NONE;
> +
> + if (func->up == NULL)
> + Py_RETURN_NONE;
> +
> + return btpy_call_new (obj->ptid, func->up->number);
> +}
> +
> +/* Implementation of BtraceFunctionCall.prev_sibling
[BtraceFunctionCall].
> + Return a previous segment of this function. */
> +
> +static PyObject *
> +btpy_call_prev_sibling (PyObject *self, void *closure)
> +{
> + const btpy_object * const obj = (btpy_object *) self;
> + const struct btrace_function *func;
> + struct btrace_call_iterator iter;
> +
> + BTPY_REQUIRE_VALID_CALL (obj, iter);
> +
> + func = btrace_call_get (&iter);
> + if (func == NULL)
> + Py_RETURN_NONE;
> +
> + if (func->segment.prev == NULL)
> + Py_RETURN_NONE;
> +
> + return btpy_call_new (obj->ptid, func->segment.prev->number);
> +}
> +
> +/* Implementation of BtraceFunctionCall.next_sibling
[BtraceFunctionCall].
> + Return a following segment of this function. */
> +
> +static PyObject *
> +btpy_call_next_sibling (PyObject *self, void *closure)
> +{
> + const btpy_object * const obj = (btpy_object *) self;
> + const struct btrace_function *func;
> + struct btrace_call_iterator iter;
> +
> + BTPY_REQUIRE_VALID_CALL (obj, iter);
> +
> + func = btrace_call_get (&iter);
> + if (func == NULL)
> + Py_RETURN_NONE;
> +
> + if (func->segment.next == NULL)
> + Py_RETURN_NONE;
> +
> + return btpy_call_new (obj->ptid, func->segment.next->number);
> +}
> +
> +/* Python rich compare function to allow for equality and inequality
checks
> + in Python. */
> +
> +static PyObject *
> +btpy_richcompare (PyObject *self, PyObject *other, int op)
> +{
> + const btpy_object * const obj1 = (btpy_object *) self;
> + const btpy_object * const obj2 = (btpy_object *) other;
> +
> + if (Py_TYPE (self) != Py_TYPE (other))
> + {
> + Py_INCREF (Py_NotImplemented);
> + return Py_NotImplemented;
> + }
> +
> + switch (op)
> + {
> + case Py_EQ:
> + if (ptid_equal (obj1->ptid, obj2->ptid) && obj1->number ==
obj2->number)
> + Py_RETURN_TRUE;
> + else
> + Py_RETURN_FALSE;
> +
> + case Py_NE:
> + if (!ptid_equal (obj1->ptid, obj2->ptid) || obj1->number !=
obj2->number)
> + Py_RETURN_TRUE;
> + else
> + Py_RETURN_FALSE;
> +
> + default:
> + break;
> + }
> +
> + Py_INCREF (Py_NotImplemented);
> + return Py_NotImplemented;
> +}
> +
> +/* Implementation of BtraceList.__len__ (self) -> int. */
> +
> +static Py_ssize_t
> +btpy_list_length (PyObject *self)
> +{
> + const btpy_list_object * const obj = (btpy_list_object *) self;
> + const Py_ssize_t distance = obj->last - obj->first;
> + const Py_ssize_t result = distance / obj->step;
> +
> + if ((distance % obj->step) == 0)
> + return result;
> +
> + return result + 1;
> +}
> +
> +/* Implementation of
> + BtraceList.__getitem__ (self, key) -> BtraceInstruction and
> + BtraceList.__getitem__ (self, key) -> BtraceFunctionCall. */
> +
> +static PyObject *
> +btpy_list_item (PyObject *self, Py_ssize_t index)
> +{
> + const btpy_list_object * const obj = (btpy_list_object *) self;
> + struct thread_info * const tinfo = find_thread_ptid (obj->ptid);
> +
> + if (index < 0 || index >= btpy_list_length (self))
> + return PyErr_Format (PyExc_IndexError, _("Index out of
range: %zd."),
> + index);
> +
> + return btpy_new (obj->ptid, obj->first + (obj->step * index),
> + obj->element_type);
> +}
> +
> +/* Implementation of BtraceList.__getitem__ (self, slice) ->
BtraceList. */
> +
> +static PyObject *
> +btpy_list_slice (PyObject *self, PyObject *value)
> +{
> + const btpy_list_object * const obj = (btpy_list_object *) self;
> + const Py_ssize_t length = btpy_list_length (self);
> + Py_ssize_t start, stop, step, slicelength;
> +
> + if (PyInt_Check (value))
> + {
> + Py_ssize_t index = PyInt_AsSsize_t (value);
> +
> + /* Emulate Python behavior for negative indices. */
> + if (index < 0)
> + index += length;
> +
> + return btpy_list_item (self, index);
> + }
> +
> + if (!PySlice_Check (value))
> + return PyErr_Format (PyExc_TypeError, _("Index must be int or
slice."));
> +
> + if (0 != PySlice_GetIndicesEx (BTPY_PYSLICE (value), length, &start,
&stop,
> + &step, &slicelength))
> + return NULL;
> +
> + return btpy_list_new (obj->ptid, obj->first + obj->step * start,
> + obj->first + obj->step * stop, obj->step * step,
> + obj->element_type);
> +}
> +
> +/* Helper function that returns the position of an element in a
BtraceList
> + or -1 if the element is not in the list. */
> +
> +static LONGEST
> +btpy_list_position (PyObject *self, PyObject *value)
> +{
> + const btpy_list_object * const list_obj = (btpy_list_object *) self;
> + const btpy_object * const obj = (btpy_object *) value;
> + Py_ssize_t index = obj->number;
> +
> + if (list_obj->element_type != Py_TYPE (value))
> + return -1;
> +
> + if (!ptid_equal (list_obj->ptid, obj->ptid))
> + return -1;
> +
> + if (index < list_obj->first || index > list_obj->last)
> + return -1;
> +
> + index -= list_obj->first;
> +
> + if (index % list_obj->step != 0)
> + return -1;
> +
> + return index / list_obj->step;
> +}
> +
> +/* Implementation of "in" operator for BtraceLists. */
> +
> +static int
> +btpy_list_contains (PyObject *self, PyObject *value)
> +{
> + if (btpy_list_position (self, value) < 0)
> + return 0;
> +
> + return 1;
> +}
> +
> +/* Implementation of BtraceLists.index (self, value) -> int. */
> +
> +static PyObject *
> +btpy_list_index (PyObject *self, PyObject *value)
> +{
> + const LONGEST index = btpy_list_position (self, value);
> +
> + if (index < 0)
> + return PyErr_Format (PyExc_ValueError, _("Not in list."));
> +
> + return PyInt_FromSize_t (index);
> +}
> +
> +/* Implementation of BtraceList.count (self, value) -> int. */
> +
> +static PyObject *
> +btpy_list_count (PyObject *self, PyObject *value)
> +{
> + const LONGEST index = btpy_list_position (self, value);
> +
> + if (index < 0)
> + return PyInt_FromLong (0);
> + else
> + return PyInt_FromLong (1);
> +}
> +
> +/* Python rich compare function to allow for equality and inequality
checks
> + in Python. */
> +
> +static PyObject *
> +btpy_list_richcompare (PyObject *self, PyObject *other, int op)
> +{
> + const btpy_list_object * const obj1 = (btpy_list_object *) self;
> + const btpy_list_object * const obj2 = (btpy_list_object *) other;
> +
> + if (Py_TYPE (self) != Py_TYPE (other))
> + {
> + Py_INCREF (Py_NotImplemented);
> + return Py_NotImplemented;
> + }
> +
> + switch (op)
> + {
> + case Py_EQ:
> + if (ptid_equal (obj1->ptid, obj2->ptid)
> + && obj1->element_type == obj2->element_type
> + && obj1->first == obj2->first
> + && obj1->last == obj2->last
> + && obj1->step == obj2->step)
> + Py_RETURN_TRUE;
> + else
> + Py_RETURN_FALSE;
> +
> + case Py_NE:
> + if (!ptid_equal (obj1->ptid, obj2->ptid)
> + || obj1->element_type != obj2->element_type
> + || obj1->first != obj2->first
> + || obj1->last != obj2->last
> + || obj1->step != obj2->step)
> + Py_RETURN_TRUE;
> + else
> + Py_RETURN_FALSE;
> +
> + default:
> + break;
> + }
> +
> + Py_INCREF (Py_NotImplemented);
> + return Py_NotImplemented;
> +}
> +
> +/* Implementation of
> + BtraceRecord.replay_position [BtraceInstruction]. */
> +
> +static PyObject *
> +recpy_bt_replay_position (PyObject* self, void* closure)
> +{
> + const struct thread_info * const tinfo = find_thread_ptid
(inferior_ptid);
> +
> + if (tinfo == NULL)
> + Py_RETURN_NONE;
> +
> + if (tinfo->btrace.replay == NULL)
> + Py_RETURN_NONE;
> +
> + return btpy_insn_new (inferior_ptid,
> + btrace_insn_number (tinfo->btrace.replay));
> +}
> +
> +/* Implementation of
> + BtraceRecord.begin [BtraceInstruction]. */
> +
> +static PyObject *
> +recpy_bt_begin (PyObject* self, void* closure)
> +{
> + struct thread_info * const tinfo = find_thread_ptid (inferior_ptid);
> + struct btrace_insn_iterator iterator;
> +
> + if (tinfo == NULL)
> + Py_RETURN_NONE;
> +
> + btrace_fetch (tinfo);
> +
> + if (btrace_is_empty (tinfo))
> + Py_RETURN_NONE;
> +
> + btrace_insn_begin (&iterator, &tinfo->btrace);
> + return btpy_insn_new (inferior_ptid, btrace_insn_number (&iterator));
> +}
> +
> +/* Implementation of
> + BtraceRecord.end [BtraceInstruction]. */
> +
> +static PyObject *
> +recpy_bt_end (PyObject* self, void* closure)
> +{
> + struct thread_info * const tinfo = find_thread_ptid (inferior_ptid);
> + struct btrace_insn_iterator iterator;
> +
> + if (tinfo == NULL)
> + Py_RETURN_NONE;
> +
> + btrace_fetch (tinfo);
> +
> + if (btrace_is_empty (tinfo))
> + Py_RETURN_NONE;
> +
> + btrace_insn_end (&iterator, &tinfo->btrace);
> + return btpy_insn_new (inferior_ptid, btrace_insn_number (&iterator));
> +}
> +
> +/* Implementation of
> + BtraceRecord.instruction_history [list]. */
> +
> +static PyObject *
> +recpy_bt_instruction_history (PyObject* self, void* closure)
> +{
> + struct thread_info * const tinfo = find_thread_ptid (inferior_ptid);
> + struct btrace_insn_iterator iterator;
> + unsigned long first = 0;
> + unsigned long last = 0;
> +
> + if (tinfo == NULL)
> + Py_RETURN_NONE;
> +
> + btrace_fetch (tinfo);
> +
> + if (btrace_is_empty (tinfo))
> + Py_RETURN_NONE;
> +
> + btrace_insn_begin (&iterator, &tinfo->btrace);
> + first = btrace_insn_number (&iterator);
> +
> + btrace_insn_end (&iterator, &tinfo->btrace);
> + last = btrace_insn_number (&iterator);
> +
> + return btpy_list_new (inferior_ptid, first, last, 1,
&btpy_insn_type);
> +}
> +
> +/* Implementation of
> + BtraceRecord.function_call_history [list]. */
> +
> +static PyObject *
> +recpy_bt_function_call_history (PyObject* self, void* closure)
> +{
> + struct thread_info * const tinfo = find_thread_ptid (inferior_ptid);
> + struct btrace_call_iterator iterator;
> + unsigned long first = 0;
> + unsigned long last = 0;
> +
> + if (tinfo == NULL)
> + Py_RETURN_NONE;
> +
> + btrace_fetch (tinfo);
> +
> + if (btrace_is_empty (tinfo))
> + Py_RETURN_NONE;
> +
> + btrace_call_begin (&iterator, &tinfo->btrace);
> + first = btrace_call_number (&iterator);
> +
> + btrace_call_end (&iterator, &tinfo->btrace);
> + last = btrace_call_number (&iterator);
> +
> + return btpy_list_new (inferior_ptid, first, last, 1,
&btpy_call_type);
> +}
> +
> +/* Implementation of BtraceRecord.goto (self, BtraceInstruction) ->
None. */
> +
> +static PyObject *
> +recpy_bt_goto_position (PyObject* self, PyObject* args)
> +{
> + struct thread_info * const tinfo = find_thread_ptid (inferior_ptid);
> + const btpy_object *obj;
> +
> + if (tinfo == NULL || btrace_is_empty (tinfo))
> + return PyErr_Format (gdbpy_gdb_error, _("Empty branch trace."));
> +
> + if (!PyArg_ParseTuple (args, "O", &obj))
> + return NULL;
> +
> + if (Py_TYPE (obj) != &btpy_insn_type)
> + return PyErr_Format (PyExc_TypeError, _("Argument must be
instruction."));
> +
> + TRY
> + {
> + struct btrace_insn_iterator iter;
> + char buffer[256] = { '\0' };
> +
> + btrace_insn_end (&iter, &tinfo->btrace);
> +
> + if (btrace_insn_number (&iter) == obj->number)
> + execute_command_to_string ("record goto end", 0);
> + else
> + {
> + snprintf (buffer, sizeof (buffer), "record goto %zi", obj->number);
> + execute_command_to_string (buffer, 0);
> + }
> + }
> + CATCH (except, RETURN_MASK_ALL)
> + {
> + GDB_PY_HANDLE_EXCEPTION (except);
> + }
> + END_CATCH
> +
> + Py_RETURN_NONE;
> +}
> +
> +/* See py-btrace.h. */
> +
> +void
> +recpy_btrace_python_interface (struct record_python_interface *iface)
> +{
> + iface->begin = recpy_bt_begin;
> + iface->end = recpy_bt_end;
> + iface->function_call_history = recpy_bt_function_call_history;
> + iface->instruction_history = recpy_bt_instruction_history;
> + iface->goto_position = recpy_bt_goto_position;
> + iface->replay_position = recpy_bt_replay_position;
> +}
> +
> +/* BtraceInstruction members. */
> +
> +struct PyGetSetDef btpy_insn_getset[] = {
> + { "number", btpy_number, NULL, "instruction number", NULL},
> + { "error", btpy_insn_error, NULL, "error number for gaps", NULL},
> + { "symbol", btpy_insn_symbol, NULL, "associated symbol", NULL},
> + { "pc", btpy_insn_pc, NULL, "instruction address", NULL},
> + { "data", btpy_insn_data, NULL, "raw instruction data", NULL},
> + { "decoded", btpy_insn_decode, NULL, "decoded instruction", NULL},
> + { "size", btpy_insn_size, NULL, "instruction size in byte", NULL},
> + { "is_speculative", btpy_insn_is_speculative, NULL, "if the
instruction was \
> +executed speculatively", NULL},
> + {NULL}
> +};
> +
> +/* BtraceFunctionCall members. */
> +
> +static PyGetSetDef btpy_call_getset[] = {
> + { "number", btpy_number, NULL, "function call number", NULL},
> + { "level", btpy_call_level, NULL, "call stack level", NULL},
> + { "symbol", btpy_call_symbol, NULL, "associated line and symbol",
NULL},
> + { "instructions", btpy_call_instructions, NULL, "list of instructions
in \
> +this function call", NULL},
> + { "up", btpy_call_up, NULL, "caller or returned-to function segment",
NULL},
> + { "prev_sibling", btpy_call_prev_sibling, NULL, "previous segment of
this \
> +function", NULL},
> + { "next_sibling", btpy_call_next_sibling, NULL, "next segment of this
\
> +function", NULL},
> + {NULL}
> +};
> +
> +/* BtraceList methods. */
> +
> +struct PyMethodDef btpy_list_methods[] = {
> + { "count", btpy_list_count, METH_O, "count number of occurences"},
> + { "index", btpy_list_index, METH_O, "index of entry"},
> + {NULL}
> +};
> +
> +/* BtraceList sequence methods. */
> +
> +static PySequenceMethods btpy_list_sequence_methods = {
> + NULL
> +};
> +
> +/* BtraceList mapping methods. Necessary for slicing. */
> +
> +static PyMappingMethods btpy_list_mapping_methods = {
> + NULL
> +};
> +
> +/* Sets up the btrace record API. */
> +
> +int
> +gdbpy_initialize_btrace (void)
> +{
> + btpy_insn_type.tp_new = PyType_GenericNew;
> + btpy_insn_type.tp_flags = Py_TPFLAGS_DEFAULT;
> + btpy_insn_type.tp_basicsize = sizeof (btpy_object);
> + btpy_insn_type.tp_name = "gdb.BtraceInstruction";
> + btpy_insn_type.tp_doc = "GDB btrace instruction object";
> + btpy_insn_type.tp_getset = btpy_insn_getset;
> + btpy_insn_type.tp_richcompare = btpy_richcompare;
> + btpy_insn_type.tp_hash = btpy_hash;
> +
> + btpy_call_type.tp_new = PyType_GenericNew;
> + btpy_call_type.tp_flags = Py_TPFLAGS_DEFAULT;
> + btpy_call_type.tp_basicsize = sizeof (btpy_object);
> + btpy_call_type.tp_name = "gdb.BtraceFunctionCall";
> + btpy_call_type.tp_doc = "GDB btrace call object";
> + btpy_call_type.tp_getset = btpy_call_getset;
> + btpy_call_type.tp_richcompare = btpy_richcompare;
> + btpy_call_type.tp_hash = btpy_hash;
> +
> + btpy_list_type.tp_new = PyType_GenericNew;
> + btpy_list_type.tp_flags = Py_TPFLAGS_DEFAULT;
> + btpy_list_type.tp_basicsize = sizeof (btpy_list_object);
> + btpy_list_type.tp_name = "gdb.BtraceInstructionList";
> + btpy_list_type.tp_doc = "GDB instruction list object";
> + btpy_list_type.tp_methods = btpy_list_methods;
> + btpy_list_type.tp_as_sequence = &btpy_list_sequence_methods;
> + btpy_list_type.tp_as_mapping = &btpy_list_mapping_methods;
> + btpy_list_type.tp_richcompare = btpy_list_richcompare;
> +
> + btpy_list_sequence_methods.sq_item = btpy_list_item;
> + btpy_list_sequence_methods.sq_length = btpy_list_length;
> + btpy_list_sequence_methods.sq_contains = btpy_list_contains;
> +
> + btpy_list_mapping_methods.mp_subscript = btpy_list_slice;
> +
> + if (PyType_Ready (&btpy_insn_type) < 0
> + || PyType_Ready (&btpy_call_type) < 0
> + || PyType_Ready (&btpy_list_type) < 0)
> + return -1;
> + else
> + return 0;
> +}
> diff --git a/gdb/python/py-btrace.h b/gdb/python/py-btrace.h
> new file mode 100644
> index 0000000..70e30a2
> --- /dev/null
> +++ b/gdb/python/py-btrace.h
> @@ -0,0 +1,32 @@
> +/* Python interface to btrace record targets.
> +
> + Copyright 2016 Free Software Foundation, Inc.
> +
> + Contributed by Intel Corp. <tim.wiederhake@intel.com>
> +
> + This file is part of GDB.
> +
> + 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/>. */
> +
> +#ifndef GDB_PY_BTRACE_H
> +#define GDB_PY_BTRACE_H
> +
> +struct record_python_interface;
> +
> +/* Fill in the record btrace python interface object and return
non-zero.
> + Return zero on failure or if no recording is active. */
> +
> +extern void recpy_btrace_python_interface (struct
record_python_interface *);
> +
> +#endif /* GDB_PY_BTRACE_H */
> diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
> index e2bb9a8..8dc8608 100644
> --- a/gdb/python/python-internal.h
> +++ b/gdb/python/python-internal.h
> @@ -432,6 +432,8 @@ int gdbpy_initialize_values (void)
> CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
> int gdbpy_initialize_frames (void)
> CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
> +int gdbpy_initialize_btrace (void)
> + CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
> int gdbpy_initialize_record (void)
> CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
> int gdbpy_initialize_symtabs (void)
> diff --git a/gdb/python/python.c b/gdb/python/python.c
> index 7f031ae..53cebd0 100644
> --- a/gdb/python/python.c
> +++ b/gdb/python/python.c
> @@ -1787,6 +1787,7 @@ message == an error message without a stack will
be printed."),
> || gdbpy_initialize_frames () < 0
> || gdbpy_initialize_commands () < 0
> || gdbpy_initialize_record () < 0
> + || gdbpy_initialize_btrace () < 0
> || gdbpy_initialize_symbols () < 0
> || gdbpy_initialize_symtabs () < 0
> || gdbpy_initialize_blocks () < 0
> diff --git a/gdb/record-btrace.c b/gdb/record-btrace.c
> index 0576123..129a2ff 100644
> --- a/gdb/record-btrace.c
> +++ b/gdb/record-btrace.c
> @@ -41,6 +41,11 @@
> #include "vec.h"
> #include <algorithm>
>
> +#if HAVE_PYTHON
> +#include "python/py-record.h"
> +#include "python/py-btrace.h"
> +#endif
> +
Again, #if HAVE_PYTHON in gdb core is a bug.
[The fact that varobj.c still has it is a TODO.]
Use the extension language interface.
> /* The target_ops of record-btrace. */
> static struct target_ops record_btrace_ops;
>
> @@ -2783,6 +2788,38 @@ record_btrace_stop_replaying_all (struct
target_ops *self)
> record_btrace_stop_replaying (tp);
> }
>
> +/* The to_record_python_interface method of target record-btrace. */
> +
> +static int
> +record_btrace_python_interface (struct target_ops *self,
> + struct record_python_interface *iface)
> +{
> +#if HAVE_PYTHON
> + struct thread_info *tp;
> + const struct btrace_config *conf;
> + int err;
> +
> + gdb_assert (iface != NULL);
> +
> + tp = find_thread_ptid (inferior_ptid);
> + if (tp == NULL)
> + return 0;
> +
> + btrace_fetch (tp);
> +
> + conf = btrace_conf (&tp->btrace);
> +
> + recpy_btrace_python_interface (iface);
> +
> + iface->format = btrace_format_short_string (conf->format);
> + iface->method = "btrace";
> +
> + return 1;
> +#else
> + return 0;
> +#endif
> +}
> +
> /* The to_execution_direction target method. */
>
> static enum exec_direction_kind
> @@ -2836,6 +2873,7 @@ init_record_btrace_ops (void)
> ops->to_record_is_replaying = record_btrace_is_replaying;
> ops->to_record_will_replay = record_btrace_will_replay;
> ops->to_record_stop_replaying = record_btrace_stop_replaying_all;
> + ops->to_record_python_interface = record_btrace_python_interface;
> ops->to_xfer_partial = record_btrace_xfer_partial;
> ops->to_remove_breakpoint = record_btrace_remove_breakpoint;
> ops->to_insert_breakpoint = record_btrace_insert_breakpoint;
> diff --git a/gdb/record-full.c b/gdb/record-full.c
> index 803c5d4..d627774 100644
> --- a/gdb/record-full.c
> +++ b/gdb/record-full.c
> @@ -38,6 +38,10 @@
>
> #include <signal.h>
>
> +#if HAVE_PYTHON
> +#include "python/py-record.h"
> +#endif
> +
> /* This module implements "target record-full", also known as "process
> record and replay". This target sits on top of a "normal" target
> (a target that "has execution"), and provides a record and replay
> @@ -1954,6 +1958,21 @@ record_full_stop_replaying (struct target_ops
*self)
> record_full_goto_end (self);
> }
>
> +/* The "to_record_python_interface" target method. */
> +
> +static int
> +record_full_python_interface (struct target_ops *self,
> + struct record_python_interface *iface)
> +{
> +#if HAVE_PYTHON
> + iface->method = "full";
> + iface->format = "full";
> + return 1;
> +#else
> + return 0;
> +#endif
> +}
> +
> static void
> init_record_full_ops (void)
> {
> @@ -1997,6 +2016,7 @@ init_record_full_ops (void)
> record_full_ops.to_record_is_replaying = record_full_is_replaying;
> record_full_ops.to_record_will_replay = record_full_will_replay;
> record_full_ops.to_record_stop_replaying = record_full_stop_replaying;
> + record_full_ops.to_record_python_interface =
record_full_python_interface;
> record_full_ops.to_goto_record_begin = record_full_goto_begin;
> record_full_ops.to_goto_record_end = record_full_goto_end;
> record_full_ops.to_goto_record = record_full_goto;
> --
> 2.7.4
>
--
/dje