1

I want to view arguments for method calls. So if I call foo:

x = 4;

y = 5;

...

foo(x, y, 20, 25);

I want to print the arguments(4,5,20,25) I understand these arguments are pushed onto the stack before the method is invoked. How do I get the value(if initialized or a constant) from the method's local variable array?

visitVarInsn() and VarInsnNode do not have a way to lookup the actual value from the array.

Do I need to use an Analyzer and Interpreter to do this, or is there an easier way?

EDIT: Figured out how to do this. I modified BasicValue and BasicInterpreter to account for bytecode instruction arguments. So Values representing instructions like BIPUSH contain information about the value being pushed, instead of only type information. Frames are examined the same way with an Analyzer

3
  • What problem are you trying to solve? Commented Jul 23, 2013 at 3:02
  • Do you want to find out the arguments statically at compile time (without running the program) or at runtime? (The second beeing far easier) Commented Jul 23, 2013 at 19:31
  • I need to find them statically. I only need to know whether they are initialized or not, as well as the initial value Commented Jul 24, 2013 at 0:19

2 Answers 2

1

Constant numeric values passed directly to the method call (20 and 25) are easy to retrieve statically - they will result in push instructions that you can read in visitIntInsn. Smaller values will result in const instructions you can catch with visitInsn, large values can be caught with visitLdcInsn.

I don't believe it is generally possible to determine the values bound to variables at the point of the method call statically. You will need to do a dataflow analysis (using Analyzer and Interpreter as you suggest) which should be able to provide the range of possible values for each variable. This won't give you definite values in the general case, but will in the specific cases of variables that are only assigned once or assigned multiple times, but unconditionally.

Sign up to request clarification or add additional context in comments.

Comments

0

it's not related to asm and bytecode manipulation, but just in case -

if method foo belongs to a class with interface method foo you may use Proxy to wrap interface implementation and intercept method names.

Also, you may found this answer useful for ASM bytecode modifications.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.