Re: Instrument at marked locations
Eliot Moss <[email protected]> Thu, 30 Nov 2017 09:33:05 -0500
| Newsgroups | gmane.comp.java.objectweb.asm |
|---|---|
| Message-ID | <[email protected]> |
On 11/30/2017 7:20 AM, Jochen Theodorou wrote:
>
>
> Am 30.11.2017 um 00:03 schrieb ThanhVu (Vu) Nguyen:
>> Hi all,
>> I want to use ASM to instrument program at certain marked locations.
>> For example, in my original program, I might have
>> ...
>> somecode
>> //@trace: int x, int y, int z
>> some more code
>> ...
>>
>> I want to use ASM to instrument code at the trace locations,
>> ...
>> somecode
>> System.out.printf("trace: %d %d %d\n, x,y,z);
>> some more code
>> ...
>>
>> But I am not sure how I would "mark" the locations so that ASM can see them.
>
> What I did in the past was to push a String constant and then pop it again. I used this mainly to
> mark certain parts in the bytecode to know what part of my compiler produced them. But I am not sure
> if ASM will eliminate this kind of code now.
That's fine if you're at the bytecode level. I am not sure how to accomplish that
at the Java source level.
Putting this together with my previous suggestion, you could try:
Tracer.trace("string describing things to trace");
That keeps the source code level interface simple. But at ASM level, you would
have to deal with finding variables, etc., by name. This is possible, if you
compile so that that debugging information in in the class file, but it is not
the most convenient thing. The suggestions I made yesterday have the virtue
that the pushing bytecodes used to mention the variables are exactly the bytecodes
you need to use for pushing them for the logging call.
Anyway, I don't think there is a way yet to put annotations on arbitrary code,
which is that the OP was asking about. I think there's a *proposal* for this
for future Java. It *is* possible to place annotations on declarations and
types, but that is not necessarily helpful for logging. You might think that
you could annotate a local variable, but local variable annotations are not
retained in class files, regardless of the retention you specify. You could
try putting in your own annotation processor, which would mean you're doing
this with ASM, or else just using the annotation processor to turn the
annotation into the kinds of calls we've been talking about.
Hope this helps ... 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