Re: Instrument at marked locations

"ThanhVu (Vu) Nguyen" <[email protected]> Fri, 01 Dec 2017 16:39:26 +0000
Newsgroups gmane.comp.java.objectweb.asm
Message-ID <CA+OL+MjW-31m998Z3jxTY3yar7eOamNRq+P-E-S16cvjKWs9EA@mail.gmail.com>
Hi Eliot,

Thanks! .  I'll try what you suggest.

Back to your prev question about what my goal is.  I am developing a tool
that detects properties at selected program location.  So the user can put
something in the source code (e.g., //%trace x y z), and compile it with
debugging enable (e.g., javac -g),  then give resulting class file to the
tool.  The tool will process the class file, do instrumentation, etc ...
and infer relationships among the x y z variables.

My goal is to reduce the user's work as much as possible, e.g., they just
have to put in a comment like //%trace x y z.  But clearly comments are not
carried over to bytecote so I try to find a different way.



On Fri, Dec 1, 2017 at 10:30 AM Eliot Moss <[email protected]> wrote:

> 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