- java.lang.Object
-
- com.ibm.j9ddr.corereaders.tdump.zebedee.le.FunctionEmulator
-
public class FunctionEmulator extends Object
This class acts as a more convenient way of using theEmulator
class. Because it is in the LE package it adds the following benefits:- it knows the XPLINK calling conventions
- sets up the stack automatically
- can obtain a function address and environment from a function name
- provides tracing including function entry and exit
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
FunctionEmulator.CallbackFunction
This interface is similar to Emulator.CallbackFunction except that it adds the ability to set the return value from the function (which is XPLINK specific).static class
FunctionEmulator.CallOriginalException
This exception can be thrown by callback functions which want to call the overridden function.class
FunctionEmulator.Function
This class represents a called function.class
FunctionEmulator.TraceEntry
Describes a trace entry
-
Constructor Summary
Constructors Constructor Description FunctionEmulator(AddressSpace space, long functionEntryPoint, long functionEnv)
Create a new FunctionEmulator.FunctionEmulator(AddressSpace space, String functionName)
Create a new FunctionEmulator.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description long
createDummyReturnAddress()
Register a dummy return address.long
getArgument(int argNumber)
Gets the given function argument.FunctionEmulator.Function
getCallTreeRoot()
Returns the root of the call tree.FunctionEmulator.Function
getCurrentFunction()
Returns the current function.Emulator
getEmulator()
Returns theEmulator
object we use.MutableAddressSpace
getMutableAddressSpace()
Returns the MutableAddressSpace used by the emulator.long
getStackFloor()
Return the xplink stack floor.List
getTraceEntries()
Return a list of the instruction trace entriesvoid
overrideFunction(long functionEntryPoint, FunctionEmulator.CallbackFunction callback)
Allows the given function to be "overridden".void
overrideFunction(String functionName, long returnValue)
void
overrideFunction(String functionName, long retVal, boolean protect)
void
overrideFunction(String functionName, FunctionEmulator.CallbackFunction callback)
Allows the given function to be "overridden".void
recordAllCalledFunctions()
void
recordCalledFunctions()
Run the emulator and record any called functions as newDllFunction
s.long
registerCallbackFunction(FunctionEmulator.CallbackFunction callback)
Registers a callback function.long
run()
Now run the emulator.long
run(long functionPointer)
Run the emulator using the supplied xplink C function pointer.long
run(String functionName)
Run the emulator using the supplied xplink C function.void
runAllPaths()
Run all paths of the function.void
setArgument(int argNumber, int value)
Sets the given function argument.void
setArgument(int argNumber, long value)
Sets the given function argument.void
setBranchInstructionTracing(boolean branchInstructionTrace)
Turn branch instruction tracing on or off.void
setCallTreeCapture()
Turn call tree capturing on.void
setInstructionRecording(boolean instructionRecord)
Turn instruction recording on or off.void
setInstructionTracing(boolean instructionTrace)
Turn instruction tracing on or off.void
setPrintOffsets(boolean printOffsets)
Include instruction offsets from the start of the function when tracing.
-
-
-
Constructor Detail
-
FunctionEmulator
public FunctionEmulator(AddressSpace space, String functionName) throws IOException, NoSuchMethodException
Create a new FunctionEmulator.- Parameters:
space
- the AddressSpace containing the code and data we are to usefunctionName
- the name of the function to call- Throws:
NoSuchMethodException
- if the given function can't be foundIOException
-
FunctionEmulator
public FunctionEmulator(AddressSpace space, long functionEntryPoint, long functionEnv) throws IOException, NoSuchMethodException
Create a new FunctionEmulator. This is a raw form where the address of the function entry point and env are supplied. Note that the so-called "environment" pointer is not required by all functions. It is the value that is stored in R5. If you do need to know it you can sometimes find it by looking in the DSA of the function to find the saved value (assuming the dump has the function in one of the stacks!).- Parameters:
space
- the AddressSpace containing the code and data we are to usefunctionEntryPoint
- the address of the function entry pointfunctionEnv
- the address of the "environment" for the function (not all functions require this to be set)- Throws:
NoSuchMethodException
- if the given function can't be foundIOException
-
-
Method Detail
-
getStackFloor
public long getStackFloor()
Return the xplink stack floor. Be careful with this.
-
createDummyReturnAddress
public long createDummyReturnAddress() throws IOException
Register a dummy return address. When this is branched to we simply stop the emulator- Throws:
IOException
-
setPrintOffsets
public void setPrintOffsets(boolean printOffsets)
Include instruction offsets from the start of the function when tracing. XXX doesn't yet work for called functions
-
setInstructionTracing
public void setInstructionTracing(boolean instructionTrace)
Turn instruction tracing on or off. A trace of all instructions executed will be sent to System.out.
-
setInstructionRecording
public void setInstructionRecording(boolean instructionRecord)
Turn instruction recording on or off. A trace of all instructions executed will be saved.
-
getTraceEntries
public List getTraceEntries()
Return a list of the instruction trace entries
-
setBranchInstructionTracing
public void setBranchInstructionTracing(boolean branchInstructionTrace)
Turn branch instruction tracing on or off. A trace of all branch instructions executed will be sent to System.out.
-
setCallTreeCapture
public void setCallTreeCapture()
Turn call tree capturing on. The call tree may be retrieved withgetCallTreeRoot()
.
-
getCallTreeRoot
public FunctionEmulator.Function getCallTreeRoot()
Returns the root of the call tree.setCallTreeCapture()
must have been called prior to the emulator having run. These routines can be used to obtain the actual call tree during a run of the emulator. Note that the root function is a dummy one.
-
getCurrentFunction
public FunctionEmulator.Function getCurrentFunction()
Returns the current function. User callbacks can make use of this.
-
setArgument
public void setArgument(int argNumber, int value)
Sets the given function argument.- Parameters:
argNumber
- the number of the argument to be set, starting at one (XXX currently limited to a max of 3)value
- the value of the argument
-
setArgument
public void setArgument(int argNumber, long value)
Sets the given function argument.- Parameters:
argNumber
- the number of the argument to be set, starting at one (XXX currently limited to a max of 3)value
- the value of the argument
-
getArgument
public long getArgument(int argNumber)
Gets the given function argument. To be used from callback functions.- Parameters:
argNumber
- the number of the argument to be got, starting at one
-
getMutableAddressSpace
public MutableAddressSpace getMutableAddressSpace()
Returns the MutableAddressSpace used by the emulator. This can be used by users wishing to allocate space for input arguments for instance.
-
registerCallbackFunction
public long registerCallbackFunction(FunctionEmulator.CallbackFunction callback)
Registers a callback function.- Returns:
- the address of the linkage pointer. This is what you put in place of a C function pointer.
-
overrideFunction
public void overrideFunction(String functionName, FunctionEmulator.CallbackFunction callback) throws IOException, NoSuchMethodException
Allows the given function to be "overridden". The supplied callback function will be called rather than the real one.- Parameters:
functionName
- the name of the function to overridecallback
- the user function that will be called rather than the real one- Throws:
IOException
NoSuchMethodException
-
overrideFunction
public void overrideFunction(long functionEntryPoint, FunctionEmulator.CallbackFunction callback) throws IOException, NoSuchMethodException
Allows the given function to be "overridden". The supplied callback function will be called rather than the real one. This is the raw version of overrideFunction that takes the address of the function.- Parameters:
functionEntryPoint
- the address of the function entry pointcallback
- the user function that will be called rather than the real one- Throws:
IOException
NoSuchMethodException
-
overrideFunction
public void overrideFunction(String functionName, long retVal, boolean protect)
-
overrideFunction
public void overrideFunction(String functionName, long returnValue)
-
run
public long run() throws IOException
Now run the emulator. This method returns when the emulated function returns.- Returns:
- the return value from the function (if any)
- Throws:
IOException
-
run
public long run(long functionPointer) throws IOException
Run the emulator using the supplied xplink C function pointer. This method returns when the emulated function returns.- Returns:
- the return value from the function (if any)
- Throws:
IOException
-
run
public long run(String functionName) throws IOException, NoSuchMethodException
Run the emulator using the supplied xplink C function. This method returns when the emulated function returns.- Returns:
- the return value from the function (if any)
- Throws:
IOException
NoSuchMethodException
-
recordCalledFunctions
public void recordCalledFunctions() throws IOException
Run the emulator and record any called functions as newDllFunction
s. The purpose of this method is to obtain the entry points for functions which are not in the export list for a DLL. The emulator is run and any functions called are saved as fake DllFunction entries which may then be obtained usingDll.getFunction(java.lang.String)
.- Throws:
IOException
-
recordAllCalledFunctions
public void recordAllCalledFunctions() throws IOException
- Throws:
IOException
-
runAllPaths
public void runAllPaths() throws IOException
Run all paths of the function. This is used for disassembly.- Throws:
IOException
-
-