public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Yao Qi <qiyaoltc@gmail.com>
To: "Wiederhake\, Tim" <tim.wiederhake@intel.com>
Cc: "gdb-patches\@sourceware.org" <gdb-patches@sourceware.org>,
	 "Metzger\, Markus T" <markus.t.metzger@intel.com>,
	 "palves\@redhat.com" <palves@redhat.com>,
	 "xdje42\@gmail.com" <xdje42@gmail.com>
Subject: Re: [PATCH v6 9/9] Add documentation for new record Python bindings.
Date: Fri, 17 Mar 2017 16:50:00 -0000	[thread overview]
Message-ID: <86tw6rluf0.fsf@gmail.com> (raw)
In-Reply-To: <9676A094AF46E14E8265E7A3F4CCE9AF942A2E@irsmsx105.ger.corp.intel.com>	(Tim Wiederhake's message of "Tue, 7 Mar 2017 17:18:09 +0000")

"Wiederhake, Tim" <tim.wiederhake@intel.com> writes:

> For the sake of the argument, let's assume that "number", "pc" and "sal" are not
> unique to btrace and that support for "full" is implemented in the way that I
> suggested: "FullInstruction" has some functions / data members that happen to
> have the same names as their "BtraceInstruction" counterparts but these python
> classes are not related in any way in terms of class inheritance. The user now
> wants to record with method "full" and "instruction_history" returns not a list
> of BtraceInstruction objects but a list of FullInstruction objects; the input
> looks like this:

That is not the use case of OOP.  I, as a user, write a python script to
get instructions (which have attributes "number", "pc", and "sal"), I
don't have to know which record method is used.  Just start record,
want gdb gives me a list of instructions, and iterate them to parse
these attributes of each instruction.

>
> (gdb) record full
> [RECORD SOME PROGRAM FLOW]
> (gdb) python-interactive
>>>> recorded_instructions = gdb.current_recording().instruction_history
>>>> for instruction in recorded_instructions:
> ...   print(i.number, hex(i.pc), i.sal)
> ...
> [OUTPUT]
>
> It is exactly the same. Now if we were to have some Python instruction base
> class, let's call it "RecordInstruction", and two Python sub classes,
> "BtraceInstruction" and "FullInstruction", the example would still look exactly
> like this:
>
> (gdb) record [full or btrace]
> [RECORD SOME PROGRAM FLOW]
> (gdb) python-interactive
>>>> recorded_instructions = gdb.current_recording().instruction_history
>>>> for instruction in recorded_instructions:
> ...   print(i.number, hex(i.pc), i.sal)
> ...
> [OUTPUT]
>
> The user has no benefit from the existance of a python base class. They never
> receive an instance of this base class to see what the common interface is. The
> pythonic way -- as far as I understand it -- is to rely on the existance of
> functions / data members of the objects, not on the actual type of the
> object.

The benefit is that we can easily enforce the instruction attributes for
new added record methods.  If I want to add "FullInstruction",
"BarInstruction", or "FooInstruction", how can we make sure these
classes have these mandatory or basic attributes "number", "pc", and
"sal" so that the following python script can still be used with new
record format?

recorded_instructions = gdb.current_recording().instruction_history
for instruction in recorded_instructions:
   print(i.number, hex(i.pc), i.sal)

Secondly, the document of these interfaces becomes simpler with class
inheritance.  We only need to document these mandatory attributes in
base class once.  In your current approach, we need to document these
attributes separately in each class.

> That is what I meant with "duck typing". Both BtraceInstruction and
> FullInstruction behave in the same way for a couple of functions / data members,
> they "quack like a duck" and therefore "are a duck", no matter that they are
> instances of two unrelated classes.
>

This is from consumer's point of view.  I am looking at this from the
producer's or interface provider's point of view.

> What exactly forms this common interface has yet to defined and codified in the
> documentation. But this is in my opinion the beauty of this approach: All
> functions / data members of BtraceInstruction that are not part of this common
> interface automatically are "just there" and "additional functionality".
>
> The approach of having a common base class /would/ have some benefit, if all
> three of "RecordInstruction", "BtraceInstruction" and "FullInstruction" were
> native Python classes: Less code in BtraceInstruction and FullInstruction. But
> BtraceInstruction and eventually FullInstruction are "Built-In"s from Python's
> perspective. That is, not real Python classes but some C code that interfaces
> with the Python interpreter.
>
> Let's assume that "number", "pc" and "sal" were to be the common interface and
> therefore data members of "RecordInstruction". The C code for this Python class
> now needs to know which recording is currently active, since there is no
>
> current_target.to_get_pc_for_recorded_instruction_by_number(struct target_ops*,
> 							unsigned int);
>

We can change GDB C code, like target_ops, whenever we want, however, we
can't change external python interface.  I am focusing on the external
python interface now.

>
> From my point of view, there is no benefit for the user from the common base
> class approach. Both cases exhibit the same functionality but the one with the
> common base class adds lots of complexity and added code to maintain.
>
> So, no "if (method = ...)" in python code; the manual tells you how write
> (python) code that works regardless of the recording method in a yet to be
> written and to be decided upon paragraph about the "common interface"; and the
> python API itself is actually not at all btrace specific, as one could argue
> that all implemented functions and data members are currently just "added
> functionality for the btrace case".

I'll do some experimental hacking to help me fully understanding whether
my suggestion is a good idea or bad idea.

-- 
Yao (齐尧)

  reply	other threads:[~2017-03-17 16:50 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-13 12:38 [PATCH v6 0/9] Python bindings for btrace recordings Tim Wiederhake
2017-02-13 12:38 ` [PATCH v6 2/9] btrace: Export btrace_decode_error function Tim Wiederhake
2017-02-13 12:38 ` [PATCH v6 1/9] btrace: Count gaps as one instruction explicitly Tim Wiederhake
2017-02-13 12:38 ` [PATCH v6 5/9] Add method to query current recording method to target_ops Tim Wiederhake
2017-02-13 12:38 ` [PATCH v6 3/9] btrace: Use binary search to find instruction Tim Wiederhake
2017-02-13 12:38 ` [PATCH v6 9/9] Add documentation for new record Python bindings Tim Wiederhake
2017-02-13 14:48   ` Eli Zaretskii
2017-03-03 11:10   ` Yao Qi
2017-03-06  8:56     ` Wiederhake, Tim
2017-03-07 11:53       ` Yao Qi
2017-03-07 17:23         ` Wiederhake, Tim
2017-03-17 16:50           ` Yao Qi [this message]
2017-02-13 12:38 ` [PATCH v6 6/9] python: Create Python bindings for record history Tim Wiederhake
2017-02-13 12:38 ` [PATCH v6 4/9] Add record_start and record_stop functions Tim Wiederhake
     [not found] ` <1486989450-11313-8-git-send-email-tim.wiederhake@intel.com>
2017-02-13 17:03   ` [PATCH v6 7/9] python: Implement btrace Python bindings for record history Doug Evans
2017-02-13 17:12   ` Doug Evans
     [not found] ` <1486989450-11313-9-git-send-email-tim.wiederhake@intel.com>
2017-02-13 17:17   ` [PATCH v6 8/9] python: Add tests for record Python bindings Doug Evans
2017-02-13 17:18 ` [PATCH v6 0/9] Python bindings for btrace recordings Doug Evans
2017-02-14 10:20   ` Wiederhake, Tim
2017-02-14 16:22     ` Doug Evans
2017-02-15  7:35       ` Wiederhake, Tim

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=86tw6rluf0.fsf@gmail.com \
    --to=qiyaoltc@gmail.com \
    --cc=gdb-patches@sourceware.org \
    --cc=markus.t.metzger@intel.com \
    --cc=palves@redhat.com \
    --cc=tim.wiederhake@intel.com \
    --cc=xdje42@gmail.com \
    /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).