Question: Get parameters from JavaThread* before a call takes place

I am currently working on a c++ project that interacts with the JVM, more specifically I have a block of code that will run before some Java function ( lets call it funcABC ) runs, within this block of code I am able to read/write the registers and stack of the JVM.
I am able to get a JavaThread* ptr out of a register, and so far I have successfully been able to get important data from that JavaThread instance such as the JNIEnv and the thread state.
This is done by reconstructing the JavaThread structure in ReClass.NET, allowing me to access the variables stored within the structure.
I would like to also get the parameters that are being passed to funcABC, I am told that they are stored somewhere within the JavaThread structure, so far I have not been able to find them, I dont see anything within the jdk sources that would suggest where they might be.
Does anyone know how and where they are stored in the JavaThread?
As an alternative I tried getting the parameters directly from the stack, only to find that there is no consistent layout, the parameters will be in a random order, sometimes on the stack and sometimes within registers like rdi, r9, and r8, or both, their positions/order also change during runtime.
By printing all the registers and a large chunk of the stack I was able to find them, they are all there, but using them in this current state is beyond impractical unless there is some sort of field somewhere that specifics where everything is located in that particular call, but I cant find anything like that.


Answer:
In the JVM, the method parameters are typically passed on the stack or stored in registers, depending on the platform's calling convention. However, the exact layout and location of these parameters can be complex and implementation-dependent. As you have observed, the layout may change during runtime, making it challenging to reliably extract the parameters directly from the stack or registers.
To access the parameters passed to funcABC, you may need to dig deeper into the JVM implementation and examine the bytecode interpretation and method invocation process. The JVM uses a bytecode instruction set, and the parameters are usually pushed onto the stack before the method is invoked. The JVM then retrieves the parameters from the stack and places them in appropriate locations, such as registers or stack frames, for the method to access.
Since you are already working with the JavaThread structure and have access to the JNIEnv and thread state, you might consider examining the thread's stack frames. The stack frames store local variables, including method parameters, and their layout can be platform-specific. You may need to navigate through the stack frames to locate the specific frame corresponding to funcABC and extract the parameters from there. However, keep in mind that accessing stack frames directly can be challenging and error-prone.
Another option is to leverage JVM debugging interfaces, such as the Java Debug Interface (JDI), which provides a higher-level abstraction for inspecting and manipulating JVM internals. The JDI allows you to programmatically interact with the JVM, inspect variables, and retrieve method arguments using a more convenient API.
I would recommend consulting JVM-specific documentation, such as the OpenJDK source code or documentation for the particular JVM implementation you are working with. Additionally, exploring JVM debugging and profiling tools might provide insights into how to access method parameters at runtime.
Keep in mind that manipulating JVM internals at such a low level can be fragile and might lead to unpredictable behavior. Exercise caution and thoroughly test your code to ensure it works reliably across different JVM versions and configurations.