Re: Learning ASM

Paul Bell <[email protected]> Fri, 12 Oct 2018 09:06:50 -0400
Newsgroups gmane.comp.java.objectweb.asm
Message-ID <CA+ttMd9_9CjSnRf-dUFnPU5BtBuBSofzVjxVrc4WOrt99t1-GQ@mail.gmail.com>
This is a multi-part message in MIME format...

------------=_1539349628-18757-4
Content-Type: multipart/alternative; boundary="0000000000006a7b0d057807c38e"

--0000000000006a7b0d057807c38e
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

Also, would you be so kind as to provide an example of how to obtain member
variables that are defined inside a method?

In reading Eric's document, I am uncertain as to the distinction between
local variables and constants. My sense (probably wrong) is that local
variables represent the arguments passed to a method, but I don't know how
to obtain "fields" defined inside a method definition.

The use case is here is runtime inspection of a class's members. I handle
most of this via reflection. But reflection cannot provide insight into
members defined within a method.

Thank you for your help.

On Wed, Oct 3, 2018 at 6:58 AM Paul Bell <[email protected]> wrote:

> Hi  R=C3=A9mi,
>
> Thank you for getting back to me about this and apologies for my tardy
> reply.
>
> When you say "...if you put a *breakpoint *in one of the
> MethodVisitor.visit*Insn", are you talking about a breakpoint instruction
> (0xCA, I think)? If so, it's my understanding that, absent a JVMTI agent,
> or the Eclipse debug perspective, this won't work. Please note that I am
> using only the -javaagent from the command line, i.e., no Eclipse, no JVM=
TI
> agent. It would be great if a breakpoint "event" (to use the JVMTI
> terminology) could be caught by ASM, but I don't think that's possible
> (again, I know very little about ASM).
>
> With respect to intercepting INVOKEVIRTUAL calls, how can my Java agent
> code obtain the name of the method being called by INVOKEVIRTUAL? This
> example, taken from one of the jasmin pages, shows Java code that calls
> method "equals" on some object and the resulting bytecode:
>
> * Object x; ... x.equals("hello")*
>
>     aload_1       ; push local variable 1 (i.e. 'x') onto stack
>     ldc "hello"   ; push the string "hello" onto stack
>
>     ; invoke the equals method
>     invokevirtual java/lang/Object/equals(Ljava/lang/Object;)Z
>     ; the boolean result is now on the stack
>
>
> How can my Java agent code obtain the 'x' variable (at stack location 1),=
 the 'hello' argument (at stack location 0), and the fully-qualified method=
 name (java/lang/Object/equals)?
>
> Thanks again!
>
> -Paul
>
>
>
>
> On Tue, Oct 2, 2018 at 9:16 AM Remi Forax <[email protected]> wrote:
>
>> Hi Paul,
>>
>> ------------------------------
>>
>> *De: *"Paul Bell" <[email protected]>
>> *=C3=80: *"asm" <[email protected]>
>> *Envoy=C3=A9: *Mardi 2 Octobre 2018 14:40:18
>> *Objet: *[asm] Learning ASM
>>
>> Hello,
>>
>> I am just now learning ASM and this is my first post.
>>
>> I've cobbled together a working Java agent that uses ASM to modify
>> methods in the code I want to trace/debug/monitor. Specifically, I modify
>> static methods, instance methods, and constructors. These modifications,
>> inserted at methods' entry, call into my tracing program.
>>
>> There's an awful lot I don't know about ASM and I would be grateful for
>> help with a few things (for now):
>>
>>    - How can ASM be used to "single step" through program execution? By
>>    "single step" I mean similar to the Eclipse debug breakpoint facility=
 where
>>    pressing F5 allows you to step into the next instruction.
>>
>>
>> ASM do bytecode transformation by transforming the bytecode in a series
>> of visitor calls, if you put a breakpoint in one of the
>> MethodVisitor.visit*Insn you override, you will see the method been call=
ed
>> for each bytecode managed by this method.
>>
>>
>>    - How can I "hook" INVOKEVIRTUAL calls? I would like to know when a
>>    method calls another method.
>>
>>
>> override MethodVisitor.visitMethodInsn et check if the opcode is
>> Opcodes.INVOKEVIRTUAL
>>
>>
>>    - What are "labels" and how are they used?
>>
>>
>> labels are target of gotos, catch blocks, etc.
>> You can create one with new Label(), insert it in the bytecode flow with
>> visitLabel() and use it as parameter of any visit* methods.
>>
>> Eric has written a nice guide that explain how to use ASM and how ASM
>> works under the cover,
>> https://asm.ow2.io/asm4-guide.pdf
>>
>>
>> Thanks very much.
>>
>> -Paul
>>
>>
>> R=C3=A9mi
>>
>>

--0000000000006a7b0d057807c38e
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Also, would you be so kind as to provide an example of how=
 to obtain member variables that are defined inside a method?<div><br></div=
><div>In reading Eric&#39;s document, I am uncertain as to the distinction =
between local variables and constants. My sense (probably wrong) is that lo=
cal variables represent the arguments passed to a method, but I don&#39;t k=
now how to obtain &quot;fields&quot; defined inside a method definition.=C2=
=A0</div><div><br></div><div>The use case is here is runtime inspection of =
a class&#39;s members. I handle most of this via reflection. But reflection=
 cannot provide insight into members defined within a method.</div><div><br=
></div><div>Thank you for your help.</div></div><br><div class=3D"gmail_quo=
te"><div dir=3D"ltr">On Wed, Oct 3, 2018 at 6:58 AM Paul Bell &lt;<a href=
=3D"mailto:[email protected]">[email protected]</a>&gt; wrote:<br></div><=
blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px=
 #ccc solid;padding-left:1ex"><div dir=3D"ltr">Hi=C2=A0

<span style=3D"color:rgb(0,0,0);font-family:arial,helvetica,sans-serif">R=
=C3=A9mi,</span><div><span style=3D"color:rgb(0,0,0);font-family:arial,helv=
etica,sans-serif"><br></span></div><div><span style=3D"color:rgb(0,0,0);fon=
t-family:arial,helvetica,sans-serif">Thank you for getting back to me about=
 this and apologies for my tardy reply.</span></div><div><span style=3D"col=
or:rgb(0,0,0);font-family:arial,helvetica,sans-serif"><br></span></div><div=
><span style=3D"color:rgb(0,0,0);font-family:arial,helvetica,sans-serif">Wh=
en you say &quot;...if you put a <i>breakpoint </i>in one of the MethodVisi=
tor.visit*Insn&quot;, are you talking about a breakpoint instruction (0xCA,=
 I think)? If so, it&#39;s my understanding that, absent a JVMTI agent, or =
the Eclipse debug perspective, this won&#39;t work. Please note that I am u=
sing only the -javaagent from the command line, i.e., no Eclipse, no JVMTI =
agent. It would be great if a breakpoint &quot;event&quot; (to use the JVMT=
I terminology) could be caught by ASM, but I don&#39;t think that&#39;s pos=
sible (again, I know very little about ASM).</span></div><div><span style=
=3D"color:rgb(0,0,0);font-family:arial,helvetica,sans-serif"><br></span></d=
iv><div><span style=3D"color:rgb(0,0,0);font-family:arial,helvetica,sans-se=
rif">With respect to intercepting INVOKEVIRTUAL calls, how can my Java agen=
t code obtain the name of the method being called by INVOKEVIRTUAL? This ex=
ample, taken from one of the jasmin pages, shows Java code that calls metho=
d &quot;equals&quot; on some object and the resulting bytecode:</span></div=
><div><b style=3D"color:rgb(0,0,0);font-family:&quot;Times New Roman&quot;;=
font-size:medium"><pre><font size=3D"-1">    Object x;
    ...
    x.equals(&quot;hello&quot;)</font></pre></b></div><div><span style=3D"c=
olor:rgb(0,0,0);font-family:arial,helvetica,sans-serif"><br></span></div><d=
iv><span style=3D"color:rgb(0,0,0)"><pre style=3D"font-weight:bold;font-siz=
e:medium;font-family:&quot;Times New Roman&quot;"><font size=3D"-1">    alo=
ad_1       ; push local variable 1 (i.e. &#39;x&#39;) onto stack
    ldc &quot;hello&quot;   ; push the string &quot;hello&quot; onto stack

    ; invoke the equals method
    invokevirtual java/lang/Object/equals(Ljava/lang/Object;)Z
    ; the boolean result is now on the stack</font></pre><pre style=3D"font=
-weight:bold;font-size:medium;font-family:&quot;Times New Roman&quot;"><fon=
t size=3D"-1"><br></font></pre><pre><font face=3D"arial, helvetica, sans-se=
rif">How can my Java agent code obtain the &#39;x&#39; variable (at stack l=
ocation 1), the &#39;hello&#39; argument (at stack location 0), and the ful=
ly-qualified method name (java/lang/Object/equals)?</font></pre><pre><font =
face=3D"arial, helvetica, sans-serif">Thanks again!</font></pre><pre><font =
face=3D"arial, helvetica, sans-serif">-Paul</font></pre><pre></pre><pre><fo=
nt face=3D"arial, helvetica, sans-serif"><br></font></pre><pre><font face=
=3D"arial, helvetica, sans-serif"><br></font></pre></span></div></div><br><=
div class=3D"gmail_quote"><div dir=3D"ltr">On Tue, Oct 2, 2018 at 9:16 AM R=
emi Forax &lt;<a href=3D"mailto:[email protected]" target=3D"_blank">forax@=
univ-mlv.fr</a>&gt; wrote:<br></div><blockquote class=3D"gmail_quote" style=
=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><di=
v style=3D"font-family:arial,helvetica,sans-serif;font-size:12pt;color:#000=
000"><div>Hi Paul,<br></div><div><br></div><hr id=3D"m_-8682450087244901233=
m_-8970503987493700044zwchr"><div><blockquote style=3D"border-left:2px soli=
d #1010ff;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;fo=
nt-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif=
;font-size:12pt"><b>De: </b>&quot;Paul Bell&quot; &lt;<a href=3D"mailto:ara=
[email protected]" target=3D"_blank">[email protected]</a>&gt;<br><b>=C3=80:=
 </b>&quot;asm&quot; &lt;<a href=3D"mailto:[email protected]" target=3D"_blank">a=
[email protected]</a>&gt;<br><b>Envoy=C3=A9: </b>Mardi 2 Octobre 2018 14:40:18<br>=
<b>Objet: </b>[asm] Learning ASM<br></blockquote></div><div><blockquote sty=
le=3D"border-left:2px solid #1010ff;margin-left:5px;padding-left:5px;color:=
#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:=
Helvetica,Arial,sans-serif;font-size:12pt"><div dir=3D"ltr"><div dir=3D"ltr=
">Hello,<br></div><br><div>I am just now learning ASM and this is my first =
post.</div><br><div>I&#39;ve cobbled together a working Java agent that use=
s ASM to modify methods in the code I want to trace/debug/monitor. Specific=
ally, I modify static methods, instance methods, and constructors. These mo=
difications, inserted at methods&#39; entry, call into my tracing program.<=
/div><br><div>There&#39;s an awful lot I don&#39;t know about ASM and I wou=
ld be grateful for help with a few things (for now):</div><div><ul><li>How =
can ASM be used to &quot;single step&quot; through program execution? By &q=
uot;single step&quot; I mean similar to the Eclipse debug breakpoint facili=
ty where pressing F5 allows you to step into the next instruction.</li></ul=
></div></div></blockquote><div><br></div><div>ASM do bytecode transformatio=
n by transforming the bytecode in a series of visitor calls, if you put a b=
reakpoint in one of the MethodVisitor.visit*Insn you override, you will see=
 the method been called for each bytecode managed by this method.=C2=A0 <br=
></div><div><br></div><blockquote style=3D"border-left:2px solid #1010ff;ma=
rgin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:nor=
mal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:1=
2pt"><div dir=3D"ltr"><div><ul><li>How can I &quot;hook&quot; INVOKEVIRTUAL=
 calls? I would like to know when a method calls another method.</li></ul><=
/div></div></blockquote><div><br></div><div>override MethodVisitor.visitMet=
hodInsn et check if the opcode is Opcodes.INVOKEVIRTUAL<br></div><div><br><=
/div><blockquote style=3D"border-left:2px solid #1010ff;margin-left:5px;pad=
ding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decorati=
on:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt"><div dir=3D"=
ltr"><div><ul><li>What are &quot;labels&quot; and how are they used?</li></=
ul></div></div></blockquote><div><br></div><div>labels are target of gotos,=
 catch blocks, etc.<br></div><div>You can create one with new Label(), inse=
rt it in the bytecode flow with visitLabel() and use it as parameter of any=
 visit* methods.<br></div><div><br></div><div>Eric has written a nice guide=
 that explain how to use ASM and how ASM works under the cover,<br></div><d=
iv><a href=3D"https://asm.ow2.io/asm4-guide.pdf" target=3D"_blank">https://=
asm.ow2.io/asm4-guide.pdf</a><br></div><div><br></div><div><br></div><block=
quote style=3D"border-left:2px solid #1010ff;margin-left:5px;padding-left:5=
px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;fon=
t-family:Helvetica,Arial,sans-serif;font-size:12pt"><div dir=3D"ltr"><div>T=
hanks very much.</div><br><div>-Paul</div></div></blockquote><div><br></div=
><div>R=C3=A9mi<br></div><div><br></div>
</div></div></div></blockquote></div>
</blockquote></div>

--0000000000006a7b0d057807c38e--

------------=_1539349628-18757-4
Content-Type: text/plain; charset="UTF-8"
Content-Disposition: inline
Content-Transfer-Encoding: 8bit


-- 
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

------------=_1539349628-18757-4--