You are viewing a plain text version of this content. The canonical link for it is here.
Posted to bcel-user@jakarta.apache.org by Eric Sheridan <er...@aspectsecurity.com> on 2007/07/26 02:32:08 UTC

getLocalVariable help

Group,

I am a relatively new BCEL user and I am trying to invoke the getLocalVariable(int index, int pc) method on the LocalVariableTable object. I am unable to determine how to access the current pc, the second parameter required for the method. What is the best way to obtain this value?

Thanks in advance for everyone's help!

-Eric

RE: getLocalVariable help

Posted by "Nicholson, Jonathan O H" <jo...@essex.ac.uk>.
I am no expert in this stuff (far from it), although through using BCEL
I've learned a hell of a lot about the byte code etc. Therefore I assume
someone on this list will be able to correct me if I'm wrong, but I
understood that if the variable is "created" with the involvement of a
store instruction (as you seem to be dealing with?), the variable will
not be available until after that store instruction (+1).

Hope that gives some explanation.

Mac
 

-----Original Message-----
From: Eric Sheridan [mailto:eric.sheridan@aspectsecurity.com] 
Sent: 27 July 2007 02:43
To: BCEL Users List
Subject: RE: getLocalVariable help

Arrin and Mac,

Thanks for your help! Everything seems to be working well.

My code snippet is as follows:

StoreInstruction storeInstruction = (StoreInstruction)instruction;
int index = storeInstruction.getIndex();
int pc = ihs[i].getPosition() + 1;	
LocalVariable lv = lvt.getLocalVariable(index, pc);

For some reason, I had to add a '1' to getPosition() for it to work.
Printing out the LocalVariableTable revealed that getPosition() was
always one less than what was found in the LocalVariableTable. Not sure
why, just happy it works.

Thanks again!

-Eric


-----Original Message-----
From: Nicholson, Jonathan O H [mailto:johnic@essex.ac.uk]
Sent: Wed 7/25/2007 11:58 PM
To: BCEL Users List
Subject: RE: getLocalVariable help
 
Eric,

To add to Arrin's reply, when I do this I start from an
InstructionHandle. It has been a while since I have looked at my code
for this, but if I remember correctly from an InstructionHandle you can
retrieve both the instruction its self and the instructions pc (which I
believe is the method getPosition()). E.g. (roughly)

InstructionHandle ih = <however you get the handle, make it, etc>;
LocalVariable v =
<LocalVariableTable>.getLocalVariable(ih.getInstruction().getIndex(),
ih.getPosition());
<do whatever with v>

This may not be the "best" way, but it works how I have used it so far.
Note that if the pc is 0 (and I remember correctly) it is one of the
arguments of the method, which may or may not be useful information
depending on what you are doing with the variable.

Hope that helps
Mac

-----Original Message-----
From: Eric Sheridan [mailto:eric.sheridan@aspectsecurity.com] 
Sent: 26 July 2007 01:32
To: bcel-user@jakarta.apache.org
Subject: getLocalVariable help

Group,

I am a relatively new BCEL user and I am trying to invoke the
getLocalVariable(int index, int pc) method on the LocalVariableTable
object. I am unable to determine how to access the current pc, the
second parameter required for the method. What is the best way to obtain
this value?

Thanks in advance for everyone's help!

-Eric

---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org


RE: getLocalVariable help

Posted by Eric Sheridan <er...@aspectsecurity.com>.
Arrin and Mac,

Thanks for your help! Everything seems to be working well.

My code snippet is as follows:

StoreInstruction storeInstruction = (StoreInstruction)instruction;
int index = storeInstruction.getIndex();
int pc = ihs[i].getPosition() + 1;	
LocalVariable lv = lvt.getLocalVariable(index, pc);

For some reason, I had to add a '1' to getPosition() for it to work. Printing out the LocalVariableTable revealed that getPosition() was always one less than what was found in the LocalVariableTable. Not sure why, just happy it works.

Thanks again!

-Eric


-----Original Message-----
From: Nicholson, Jonathan O H [mailto:johnic@essex.ac.uk]
Sent: Wed 7/25/2007 11:58 PM
To: BCEL Users List
Subject: RE: getLocalVariable help
 
Eric,

To add to Arrin's reply, when I do this I start from an
InstructionHandle. It has been a while since I have looked at my code
for this, but if I remember correctly from an InstructionHandle you can
retrieve both the instruction its self and the instructions pc (which I
believe is the method getPosition()). E.g. (roughly)

InstructionHandle ih = <however you get the handle, make it, etc>;
LocalVariable v =
<LocalVariableTable>.getLocalVariable(ih.getInstruction().getIndex(),
ih.getPosition());
<do whatever with v>

This may not be the "best" way, but it works how I have used it so far.
Note that if the pc is 0 (and I remember correctly) it is one of the
arguments of the method, which may or may not be useful information
depending on what you are doing with the variable.

Hope that helps
Mac

-----Original Message-----
From: Eric Sheridan [mailto:eric.sheridan@aspectsecurity.com] 
Sent: 26 July 2007 01:32
To: bcel-user@jakarta.apache.org
Subject: getLocalVariable help

Group,

I am a relatively new BCEL user and I am trying to invoke the
getLocalVariable(int index, int pc) method on the LocalVariableTable
object. I am unable to determine how to access the current pc, the
second parameter required for the method. What is the best way to obtain
this value?

Thanks in advance for everyone's help!

-Eric

---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org




RE: getLocalVariable help

Posted by "Nicholson, Jonathan O H" <jo...@essex.ac.uk>.
Eric,

To add to Arrin's reply, when I do this I start from an
InstructionHandle. It has been a while since I have looked at my code
for this, but if I remember correctly from an InstructionHandle you can
retrieve both the instruction its self and the instructions pc (which I
believe is the method getPosition()). E.g. (roughly)

InstructionHandle ih = <however you get the handle, make it, etc>;
LocalVariable v =
<LocalVariableTable>.getLocalVariable(ih.getInstruction().getIndex(),
ih.getPosition());
<do whatever with v>

This may not be the "best" way, but it works how I have used it so far.
Note that if the pc is 0 (and I remember correctly) it is one of the
arguments of the method, which may or may not be useful information
depending on what you are doing with the variable.

Hope that helps
Mac

-----Original Message-----
From: Eric Sheridan [mailto:eric.sheridan@aspectsecurity.com] 
Sent: 26 July 2007 01:32
To: bcel-user@jakarta.apache.org
Subject: getLocalVariable help

Group,

I am a relatively new BCEL user and I am trying to invoke the
getLocalVariable(int index, int pc) method on the LocalVariableTable
object. I am unable to determine how to access the current pc, the
second parameter required for the method. What is the best way to obtain
this value?

Thanks in advance for everyone's help!

-Eric

---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org


Re: getLocalVariable help

Posted by Arrin Daley <ar...@anu.edu.au>.
Hi Eric

The pc refers to the count within the bytecode array, so the instruction 
your interested in should have a pc associated with it (if the 
InstructionList has been changed these counts will be invalid) 
IntructionList.setPositions() sets these counts, or I think it must be 
done automatically by MethodGen.getMethod(). If you are changing the 
InstructionList you can request a LocalVariableGen which uses 
InstructionHandles which maintain their targets even if the 
InstructionList is edited later (InstructionHandles are then converted 
back to pc).

The pc parameter to getLocalVariable(..) is required because a local 
variable index can be re-used by another local variable with a different 
type once the original local variable is no longer required so the 
method needs to know which section of code or instruction you are 
interested in.

Importantly in my experience LocalVariable tables are not to be trusted 
to be correct, unless you have created them yourself and know they are 
correct. It is possible to remove the local variable table completely or 
have the local variable table completely wrong and still have valid and 
working bytecode. LocalVariableTables like LineNumberTables are not 
compulsory and are only there as debugging structures.

The only way I know of to get local variable information you can trust 
is from the instructions themselves.

I hope this helps

Bye Arrin

Eric Sheridan wrote:

>Group,
>
>I am a relatively new BCEL user and I am trying to invoke the getLocalVariable(int index, int pc) method on the LocalVariableTable object. I am unable to determine how to access the current pc, the second parameter required for the method. What is the best way to obtain this value?
>
>Thanks in advance for everyone's help!
>
>-Eric
>
>  
>

-- 
Conventional wisdom says to know your limits. To know your limits you 
need to find them first. Finding you limits generally involves getting
in over your head and hoping you live long enough to benefit from the 
experience. That's the fun part.


---------------------------------------------------------------------
To unsubscribe, e-mail: bcel-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: bcel-user-help@jakarta.apache.org