kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
From: fweisbec@gmail.com (Frederic Weisbecker)
To: kernelnewbies@lists.kernelnewbies.org
Subject: question about macro __DO_TRACE
Date: Sat, 26 Feb 2011 05:37:59 +0100	[thread overview]
Message-ID: <20110226043757.GF1807@nowhere> (raw)
In-Reply-To: <AANLkTin7dR9DZo4m4c3QbH5y73rSa3t-kEypnFQqV+bJ@mail.gmail.com>

On Sat, Feb 26, 2011 at 10:40:32AM +0800, zhao bao wrote:
>  Hello,everybody. When I read tracepoint code, I find variable __data
> never used. Am I missing something?
> 
> #define __DO_TRACE(tp, proto, args, cond)                               \
>         do {                                                            \
>                 struct tracepoint_func *it_func_ptr;                    \
>                 void *it_func;                                          \
>                 void *__data;                                           \
>                                                                         \
>                 if (!(cond))                                            \
>                         return;                                         \
>                 rcu_read_lock_sched_notrace();                          \
>                 it_func_ptr = rcu_dereference_sched((tp)->funcs);       \
>                 if (it_func_ptr) {                                      \
>                         do {                                            \
>                                 it_func = (it_func_ptr)->func;          \
>                                 __data = (it_func_ptr)->data;           \
>                                 ((void(*)(proto))(it_func))(args);      \
>                         } while ((++it_func_ptr)->func);                \
>                 }                                                       \
>                 rcu_read_unlock_sched_notrace();                        \
>         } while (0)


Yeah that's a quite tricky part.

So what we want with tracepoints is to have them passing something
specific as a first parameter. Always the same thing for a given func.

So you register a probe with tracepoint_probe_register() and the third
argument of this func, data, is the first parameter that will always been
passed to your probe function.

This is that "__data".

We declare a tracepoint with DECLARE_TRACE():

DECLARE_TRACE(my_trace, int myarg, myarg).

The CPP engine will translate that too:

__DECLARE_TRACE(mytrace, (int myarg), (myarg), 1
			(void *__data, int myarg),
			(__data, myarg))

			^^
Look, that where is the trick. DECLARE_TRACE is cheating by
adding this __data argument.

Further, __DO_TRACE will be called with these arguments:

__DO_TRACE(&__tracepoint_mytrace, (void *__data, int myarg),
	(__data, myarg), (cond))

And then it makes the trick inside __DO_TRACE(), we end up having:

				void *__data;

				__data = (it_func_ptr)->data;
				((void(*)(proto))(it_func))(__data, myarg);

See? That's a kind of ghost argument we inject in our CPP macros
and in the end we cheat in order to pass that constant tracepoint data
as a first argument of the probe.

In practice we use that to pass the ftrace event (struct ftrace_event_call *)
structure as a first argument of the probe.

It's a shame we needed to get something so unreadable and quick to generate
headaches but IIRC we didn't have the choice in order to pass that specific data
argument.

  reply	other threads:[~2011-02-26  4:37 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-02-26  2:40 question about macro __DO_TRACE zhao bao
2011-02-26  4:37 ` Frederic Weisbecker [this message]
2011-02-26  4:54   ` Mulyadi Santosa

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=20110226043757.GF1807@nowhere \
    --to=fweisbec@gmail.com \
    --cc=kernelnewbies@lists.kernelnewbies.org \
    /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 NNTP newsgroup(s).