[cfe-dev] source-to-source transformation to insert instrumentation calls

Manuel Klimek klimek at google.com
Mon Sep 24 15:11:11 PDT 2012


On Mon, Sep 24, 2012 at 10:07 PM, Jan Smets
<jan.smets at alcatel-lucent.com> wrote:
>
> On 24/09/2012 18:57, Ronan Keryell wrote:
>>>>>>>
>>>>>>> On Mon, 24 Sep 2012 18:50:36 +0300, Dmitri Gribenko
>>>>>>> <gribozavr at gmail.com> said:
>>
>>      Dmitri> On Mon, Sep 24, 2012 at 6:43 PM, Jan Smets
>> <jan.smets at alcatel-lucent.com> wrote:
>>      >> That's OK. I'm aware that I won't be 100% accurate.
>>
>>      Dmitri> OK, good to know.
>>
>>      >> Now I just need to figure out a good location to insert the call!
>>      >> :)
>>
>>      Dmitri> I think that there are cases where inserting instrumentation
>>      Dmitri> calls will require non-trivial rewriting.  For example:
>>
>>      Dmitri> int f(int, int);
>>      Dmitri> int g(int *);
>>      Dmitri> int test(int *a) {
>>      Dmitri>    f(*a, g(a));
>>      Dmitri> }
>>
>>      Dmitri> Function argument evaluation order is unspecified, so we can
>>      Dmitri> not simply insert an instrumentation call for *a before f()
>>      Dmitri> because *a can be evaluated after g(a), which can change *a.
>>
>>      Dmitri> So we need to make an arbitrary decision ourselves and
>>      Dmitri> rewrite this to something like:
>>
>>      Dmitri> int test(int *a) { int tmp1 = *a; int tmp2 = g(a); f(tmp1,
>>      Dmitri> tmp2); }
>>
>>      Dmitri> I didn't give this much thought, but I suspect there are
>>      Dmitri> much more cases like this.
>>
>> Yes, for example with side effect operators like ++ or -=, etc.
>>
>> To deal with this kind of stuff in our own source-to-source compiler we
>> use the "," sequence operator to capture some values right from inside
>> the battle field.
>>
>> For example you could compile this to:
>> int f(int, int);
>> int g(int *);
>> int test(int *a) {
>>     f((instrument_read(*a, 4), *a), (instrument_read(a, 4), g(a)));
>> }
>>
>> That should work if the function formal argument evaluation order is
>> consistent for any call.
>>
>> But, well, I guess we have enough work to deal with well-written
>> programs before dealing with all this wicked stuff... :-)
>
> I realize that it won't be easy to handle these exceptional things, my basic
> aim is at the trivial assignments, compares, things inside if/while/for etc.
> The basic stuff.
>
> So I would like to know how I can do just that.
> As I said, I think I need to walk up the AST tree and find the intersection
> point with the CompountStmt.  How can I do that? How do I get the parent?

The AST doesn't have parent - you can either wait for me to implement
hasParent in the matchers, or build up the parent map yourself
thorough a RecursiveASTVisitor.

Cheers,
/Manuel



More information about the cfe-dev mailing list