Re: Instrument at marked locations

Eliot Moss <[email protected]> Fri, 1 Dec 2017 11:30:00 -0500
Newsgroups gmane.comp.java.objectweb.asm
Message-ID <[email protected]>
I wanted to post one more time to give more details of
how you can actually do this.

Suppose the protocol is:

   Tracer.startTrace();
   ... trace calls, e.g., Tracer.trace(var), Tracer.trace(expr), ...
   Tracer.endTrace();

First, you have to wrap your ahead around the fact that you
are using ASM as a tool to *remove* code, not one to *inject*
code.  So you would have your tracing code in the source,
but have it presented in a way that makes it possible (even
easy) for ASM to cut it out.

The purpose of the startTrace call is to make it easy to find
the bytecodes related to tracing, which necessarily start *before*
the Tracer.trace calls, in order to push arguments.

So, the code exciser would look for calls of Trace.start/endTrace.
Your method processor would include a boolean flag; let's call
it "keeping".  When it sees startTrace, it would set keeping to
false.  All the bytecode handling methods would do:

   if (keeping) {
     pass this bytecode through
   } /* otherwise nothing */

When endTrace is seen, you turn keeping back on (but after
considering the call for processing).

This is pretty straight forward, but requires that you override
a whole bunch of methods (but in a simple way).

A REFINEMENT:

If you want your tool to operate in a moe general and selective
way, you could add a version of startTrace that takes a string:

   Tracer.startTrace("level 2 logging");

I assume the string is a constant.  By adding a bit of slightly
tedious code, you can track whether the last thing pushed was a
string constant, and if so, what its value is.  When you get to
the call of startTrace(Ljava/lang/String;) you check that and
use it to determine whether to turn keeping off or not.  You
should complain it is if not a constant.

If you want NOT to keep the succeeding code, you replace the
startTrace call with a POP (sorry, you have a PUSH/POP pair
left -- but when code goes through a JIT compiler, this will
disappear, so the ultimate overhead is not a big issue IMO).

Hope this helps!  Regards - Eliot Moss


-- 
You receive this message as a subscriber of the [email protected] mailing list.
To unsubscribe: mailto:[email protected]
For general help: mailto:[email protected]?subject=help
OW2 mailing lists service home page: http://www.ow2.org/wws