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