开发者

Visiting arrays access using ASM

I'd like to know if it's possible to trace access to an array using ASM API.

My goal is to determine which index of an array is accessed, and when (this part is easy - usin开发者_JS百科g System.NanoTime() ). I just couldn't find a way to determine which index is being accessed.

I have been trying to use those following without any success - visitFieldInsn (for static and non static vars of a class ), visitVarInsn ( for static and nonstatic local variables ), and visitMultiANewArrayInsn - which didn't really recognize any array.


The particular index is not part of the instruction. You have to peek at the value at top of the operand stack to find out which index the instruction refers to. See the JVM reference.

You don't want to havoc the operand stack however, so when you encounter an array-access instruction, perform a DUP do duplicate the top of the stack (duplicate the index the instruction refers to) and then print the value or do whatever you like with it and then continue by visiting the original instruction.

You should know however that there are multiple different instructions to access an array:

  • aaload, iaload, laload, saload, baload, caload and daload for reading, and
  • aastore, iastore, lastore, sastore, bastore, castore and dastore for writing


Its worth noting that nanoTime() takes about 100x long that the array access itself. This could significatly skew results.

Have you tried looking at your code with the ASMifier. This should show you what events are triggered by you code.

BTW you can replace the array lookups with method calls e.g.

public static int arrayGet(int[] int. int index)

This will allow you to put in Java whatever you want it to do when an int[] is accessed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜