0

When a method defined in C is called from Java (using JNI), what are the methods that are invoked inside the JVM? What does the execution flow look like?

  1. i.e. consider a method public native void display(). When this method is called from the java code as object.display(), what are the internal JVM methods that are called as execution proceeds towards the native code?
  2. Where can I find more documentation about this? Alternatively, which JVM function in the source can I look at and trace?
syntagma
  • 23,346
  • 16
  • 78
  • 134
  • To call a native method, HotSpot JVM enters a runtime stub (the fragment of machine code generated at VM startup). This runtime stub performs all the required JNI stuff - the procedure is described [here](https://stackoverflow.com/a/24747484/3448419). – apangin Jun 07 '17 at 22:08
  • The sources for calling native methods from interpreter are in [`InterpreterGenerator::generate_native_entry`](http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/4d9931ebf861/src/cpu/x86/vm/templateInterpreter_x86_64.cpp#l960), or [`SharedRuntime::generate_native_wrapper`](http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/file/4d9931ebf861/src/cpu/x86/vm/sharedRuntime_x86_64.cpp#l1723) for calling native methods from JIT-compiled code. – apangin Jun 07 '17 at 22:13
  • The above is true for HotSpot JVM. Other JVMs may do this differently. – apangin Jun 07 '17 at 22:14

1 Answers1

1

If you want to take a look at exact chain of execution I suggest to connect to JVM using gdb (or CLion) and take a look at backtrace.

Take a look here to see how to do it:

http://www.owsiak.org/?p=2095

You can also take a look here:

https://www.youtube.com/watch?v=8Cjeq4l5COU

Once you are attached to JVM, you can set breakpoint in your C code and check the backtrace from top to bottom.

Alternatively, you can create SIGSEGV and generate full backtrace of JVM (take a look here: http://jnicookbook.owsiak.org/recipe-No-015/)

Oo.oO
  • 12,464
  • 3
  • 23
  • 45