public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: "Willgerodt, Felix" <felix.willgerodt@intel.com>
To: Simon Marchi <simark@simark.ca>,
	"Metzger, Markus T" <markus.t.metzger@intel.com>
Cc: "gdb-patches@sourceware.org" <gdb-patches@sourceware.org>
Subject: RE: [PATCH v8 05/10] python: Introduce gdb.RecordAuxiliary class.
Date: Tue, 4 Apr 2023 14:26:02 +0000	[thread overview]
Message-ID: <MN2PR11MB4566912F14188586D846F98B8E939@MN2PR11MB4566.namprd11.prod.outlook.com> (raw)
In-Reply-To: <27cb4730-4dc9-86fd-6da0-38230bb69158@simark.ca>

> -----Original Message-----
> From: Simon Marchi <simark@simark.ca>
> Sent: Dienstag, 4. April 2023 16:17
> To: Metzger, Markus T <markus.t.metzger@intel.com>; Willgerodt, Felix
> <felix.willgerodt@intel.com>
> Cc: gdb-patches@sourceware.org
> Subject: Re: [PATCH v8 05/10] python: Introduce gdb.RecordAuxiliary class.
> 
> On 4/4/23 02:57, Metzger, Markus T wrote:
> >
> >>>>> +/* Implementation of Auxiliary.data [str].  */
> >>>>> +
> >>>>> +static PyObject *
> >>>>> +recpy_aux_data (PyObject *self, void *closure)
> >>>>> +{
> >>>>> +  const recpy_aux_object * const obj = (const recpy_aux_object *)
> self;
> >>>>> +
> >>>>> +  return PyUnicode_FromString (obj->data);
> >>>>> +}
> >>>>
> >>>> Nothing new with this patch, since this is the same pattern used for
> >>>> other object, but just wondering: obj->data seems to be borrowed
> from
> >>>> the btrace backend.  Are there some lifetime issues if you do:
> >>>>
> >>>> 1. Create a gdb.RecordAuxiliary object "b"
> >>>> 2. Issue a gdb command to clear the btrace data
> >>>> 3. Access "b.data"
> >>>>
> >>>> ?  It seems to me like obj->data might point to freed data.
> >>>
> >>> About the pattern:
> >>> There is already "maint btrace clear" so I could test and debug this with
> >>> master. On master GDB prints an error before accessing any of these
> objects,
> >>> as we check if the trace is empty before accessing anything.
> >>
> >> Then, maybe if you record something after clearing the data?  Something
> >> like:
> >>
> >> 1. Create a gdb.RecordAuxiliary object "b"
> >> 2. Issue "maint btrace clear"
> >> 3. Do one step (to make the trace non-empty again)
> >> 4. Access "b.data"
> >
> > I added the 'maint btrace clear' command to force re-decoding of the trace
> > for debugging purposes.  Any CLI command that uses btrace will trigger a
> > trace fetch and decode.
> 
> Ack.
> 
> > For ptwrite filters, the use-case is to replace the filter and re-decode with
> > the new filter in place (and the old filter removed).
> 
> Yes, that makes sense.
> 
> > I'm not against chaining
> > filters and decorating their aux contribution with the filter name.  IIRC this
> > was considered more complicated and we couldn't really find a good use-
> case.
> 
> Just to be clear, I didn't suggest to add that.
> 
> > The filter needs to know about the source or binary instrumentation that
> > added those ptwrite instructions.  It's job is to aggregate the raw data and
> > the IP of the ptwrite instruction that emitted that data and turn it into
> > something more useful than the raw hex values.
> >
> > The use case for filter chaining would be independent instrumentations
> > active at the same time and interpreted by their respective ptwrite filter.
> 
> Ack.  Or some kind of top-level filter that delegates.
> 
> > To the topic at hand, I'd say that record instruction or aux objects should
> > behave like, say, frame objects.  You cannot clear the frames to force a
> > re-unwind AFAIK but you can step or finish to turn a frame invalid.
> 
> Yes, that is my worry.  It seems possible for the btrace Python objects
> to outlive the data they are wrapping, and I don't see any measures
> taken to avoid accessing the stale data.  Usually that can be done with
> an observer that will mark the Python objects invalid.
> 
> Another case: if you "continue" and a thread exits, is the btrace record
> data for that thread deleted?
> 
> Simon

I looked at it a bit more yesterday.

The gap objects get a string literal (const char *). Which have a guaranteed
lifetime over the full program afaik and therefore pointers are never dangling.
I didn't check if we wouldn't error out first, as I don't know how to reliably
record gaps.

The insn and func objects cannot be accessed after clear.
Any commands will show:

"gdb.error: No such function segment."
or
"gdb.error: No such instruction."

Even if you "save" it in a separate python variable, e.g. like "b" in Simon's list.

For auxiliaries in my patch however, Simon is right. We just pass a pointer
to an element in a vector of strings (aux_data) that is allocated in btrace.c.
We clear that vector with clear. I debugged GDB, in that case we do still point
to the same address and just read a string from it. No matter what is at that
address now:

>>> r = gdb.current_recording()
>>> i = r.instruction_history
>>> a = i[9]
>>> a.data

Thread 1 "gdb-up" hit Breakpoint 1, recpy_aux_data (
    self=<gdb.RecordAuxiliary at remote 0x7fffe8152710>, closure=0x0)
    at gdb/gdb/python/py-record.c:547
547       const recpy_aux_object * const obj = (const recpy_aux_object *) self;
(gdb) n
549       return PyUnicode_FromString (obj->data);
(gdb) p obj.data
$1 = 0x2d1ade0 "42"
(gdb) c
Continuing.
'42'
>>> r.clear()
>>> a.data

Thread 1 "gdb-up" hit Breakpoint 1, recpy_aux_data (
    self=<gdb.RecordAuxiliary at remote 0x7fffe8152710>, closure=0x0)
    at gdb/gdb/python/py-record.c:547
547       const recpy_aux_object * const obj = (const recpy_aux_object *) self;
(gdb) n
549       return PyUnicode_FromString (obj->data);
(gdb) p obj.data
$2 = 0x2d1ade0 "42"
(gdb) c
Continuing.
'42'

So I should really create a copy or error out if the trace
was cleared instead. Probably the latter to be consistent.

Thanks,
Felix


Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de <http://www.intel.de>
Managing Directors: Christin Eisenschmid, Sharon Heck, Tiffany Doon Silva  
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928

  reply	other threads:[~2023-04-04 14:26 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-21 15:46 [PATCH v8 00/10] Extensions for PTWRITE Felix Willgerodt
2023-03-21 15:46 ` [PATCH v8 01/10] btrace: Introduce auxiliary instructions Felix Willgerodt
2023-03-21 15:46 ` [PATCH v8 02/10] btrace: Enable auxiliary instructions in record instruction-history Felix Willgerodt
2023-03-21 15:46 ` [PATCH v8 03/10] btrace: Enable auxiliary instructions in record function-call-history Felix Willgerodt
2023-03-21 15:46 ` [PATCH v8 04/10] btrace: Handle stepping and goto for auxiliary instructions Felix Willgerodt
2023-03-24 14:09   ` Simon Marchi
2023-03-31 10:58     ` Willgerodt, Felix
2023-03-21 15:46 ` [PATCH v8 05/10] python: Introduce gdb.RecordAuxiliary class Felix Willgerodt
2023-03-24 14:27   ` Simon Marchi
2023-03-31 10:58     ` Willgerodt, Felix
2023-04-03 19:06       ` Simon Marchi
2023-04-04  6:57         ` Metzger, Markus T
2023-04-04 14:17           ` Simon Marchi
2023-04-04 14:26             ` Willgerodt, Felix [this message]
2023-03-21 15:46 ` [PATCH v8 06/10] python: Add clear() to gdb.Record Felix Willgerodt
2023-03-24 14:36   ` Simon Marchi
2023-03-31 10:58     ` Willgerodt, Felix
2023-03-21 15:46 ` [PATCH v8 07/10] btrace, gdbserver: Add ptwrite to btrace_config_pt Felix Willgerodt
2023-03-21 15:46 ` [PATCH v8 08/10] btrace, linux: Enable ptwrite packets Felix Willgerodt
2023-03-21 15:46 ` [PATCH v8 09/10] btrace, python: Enable ptwrite filter registration Felix Willgerodt
2023-03-24 15:23   ` Simon Marchi
2023-03-31 10:58     ` Willgerodt, Felix
2023-04-03 20:44       ` Simon Marchi
2023-04-04 14:42         ` Willgerodt, Felix
2023-04-04 15:06           ` Simon Marchi
2023-04-05 10:20             ` Willgerodt, Felix
2023-04-05 20:27               ` Simon Marchi
2023-04-06  9:44                 ` Willgerodt, Felix
2023-03-21 15:46 ` [PATCH v8 10/10] btrace: Extend ptwrite event decoding Felix Willgerodt
2023-03-24 15:40   ` Simon Marchi
2023-03-31 10:58     ` Willgerodt, Felix
2023-04-04 14:23       ` Simon Marchi
2023-03-24 13:56 ` [PATCH v8 00/10] Extensions for PTWRITE Simon Marchi
2023-03-24 18:23   ` Tom Tromey
2023-03-24 18:28     ` Simon Marchi
2023-03-24 22:29       ` Tom Tromey
2023-03-31 10:57   ` Willgerodt, Felix

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=MN2PR11MB4566912F14188586D846F98B8E939@MN2PR11MB4566.namprd11.prod.outlook.com \
    --to=felix.willgerodt@intel.com \
    --cc=gdb-patches@sourceware.org \
    --cc=markus.t.metzger@intel.com \
    --cc=simark@simark.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).