Re: Learning ASM
Paul Bell <[email protected]> Wed, 3 Oct 2018 06:58:09 -0400
| Newsgroups | gmane.comp.java.objectweb.asm |
|---|---|
| Message-ID | <CA+ttMd_ufDZhW3sD5rEL=mgM6KKeLbxYJp+9ROFSXv9JMgt=mA@mail.gmail.com> |
This is a multi-part message in MIME format...
------------=_1538564306-4637-7
Content-Type: multipart/alternative; boundary="000000000000a527cd057750ea7d"
--000000000000a527cd057750ea7d
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
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 JVMTI
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 called
> 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
>
>
--000000000000a527cd057750ea7d
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<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 "...if you put a <i>breakpoint </i>in one of the MethodVisi=
tor.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 u=
sing only the -javaagent from the command line, i.e., no Eclipse, no JVMTI =
agent. It would be great if a breakpoint "event" (to use the JVMT=
I terminology) could be caught by ASM, but I don't think that'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 "equals" on some object and the resulting bytecode:</span></div=
><div><b style=3D"color:rgb(0,0,0);font-family:"Times New Roman";=
font-size:medium"><pre><font size=3D"-1"> Object x;
...
x.equals("hello")</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:"Times New Roman""><font size=3D"-1"> alo=
ad_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</font></pre><pre style=3D"font=
-weight:bold;font-size:medium;font-family:"Times New Roman""><fon=
t size=3D"-1"><br></font></pre><pre style=3D""><font face=3D"arial, helveti=
ca, sans-serif" style=3D"">How can my Java agent code obtain the 'x'=
; variable (at stack location 1), the 'hello' argument (at stack lo=
cation 0), and the fully-qualified method name (java/lang/Object/equals)?</=
font></pre><pre style=3D""><font face=3D"arial, helvetica, sans-serif" styl=
e=3D"">Thanks again!</font></pre><pre style=3D""><font face=3D"arial, helve=
tica, sans-serif" style=3D"">-Paul</font></pre><pre style=3D""></pre><pre s=
tyle=3D""><font face=3D"arial, helvetica, sans-serif" style=3D""><br></font=
></pre><pre style=3D""><font face=3D"arial, helvetica, sans-serif" style=3D=
""><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 Remi Forax <<a href=3D"mailto=
:[email protected]">[email protected]</a>> wrote:<br></div><blockquote c=
lass=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;=
padding-left:1ex"><div><div style=3D"font-family:arial,helvetica,sans-serif=
;font-size:12pt;color:#000000"><div>Hi Paul,<br></div><div><br></div><hr id=
=3D"m_-8970503987493700044zwchr"><div><blockquote style=3D"border-left:2px =
solid #1010ff;margin-left:5px;padding-left:5px;color:#000;font-weight:norma=
l;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-s=
erif;font-size:12pt"><b>De: </b>"Paul Bell" <<a href=3D"mailto=
:[email protected]" target=3D"_blank">[email protected]</a>><br><b>=C3=
=80: </b>"asm" <<a href=3D"mailto:[email protected]" target=3D"_blan=
k">[email protected]</a>><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=
style=3D"border-left:2px solid #1010ff;margin-left:5px;padding-left:5px;co=
lor:#000;font-weight:normal;font-style:normal;text-decoration:none;font-fam=
ily: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 fi=
rst post.</div><br><div>I've cobbled together a working Java agent that=
uses ASM to modify methods in the code I want to trace/debug/monitor. Spec=
ifically, I modify static methods, instance methods, and constructors. Thes=
e modifications, inserted at methods' entry, call into my tracing progr=
am.</div><br><div>There's an awful lot I don't know about ASM and I=
would be grateful for help with a few things (for now):</div><div><ul><li>=
How can ASM be used to "single step" through program execution? B=
y "single step" I mean similar to the Eclipse debug breakpoint fa=
cility where pressing F5 allows you to step into the next instruction.</li>=
</ul></div></div></blockquote><div><br></div><div>ASM do bytecode transform=
ation 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 called for each bytecode managed by this method.=C2=A0=
<br></div><div><br></div><blockquote style=3D"border-left:2px solid #1010f=
f;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style=
:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-si=
ze:12pt"><div dir=3D"ltr"><div><ul><li>How can I "hook" INVOKEVIR=
TUAL calls? I would like to know when a method calls another method.</li></=
ul></div></div></blockquote><div><br></div><div>override MethodVisitor.visi=
tMethodInsn et check if the opcode is Opcodes.INVOKEVIRTUAL<br></div><div><=
br></div><blockquote style=3D"border-left:2px solid #1010ff;margin-left:5px=
;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-deco=
ration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt"><div dir=
=3D"ltr"><div><ul><li>What are "labels" and how are they used?</l=
i></ul></div></div></blockquote><div><br></div><div>labels are target of go=
tos, catch blocks, etc.<br></div><div>You can create one with new Label(), =
insert 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 g=
uide that explain how to use ASM and how ASM works under the cover,<br></di=
v><div><a href=3D"https://asm.ow2.io/asm4-guide.pdf" target=3D"_blank">http=
s://asm.ow2.io/asm4-guide.pdf</a><br></div><div><br></div><div><br></div><b=
lockquote style=3D"border-left:2px solid #1010ff;margin-left:5px;padding-le=
ft: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"><d=
iv>Thanks 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>
--000000000000a527cd057750ea7d--
------------=_1538564306-4637-7
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
------------=_1538564306-4637-7--