I was trying to answer https://stackoverflow.com/questions/5751492/jvmti-agent-never-returns-the-thread-state-terminated which is related agentlib and jvmti, as a senior java engineer and have been working with java professionally since 2006, never tried jvmti.
So found a small example here https://github.com/sachin-handiekar/jvmti-examples which is not showing how to run it, so I manage to figure that out and note it here as blog, in case someone else would like to see how jvmti called in linux
- Compiling the cpp side we use cmake and make like below
oz-mint@ozmint-MACH-WX9:~/tmp/jvmti-examples/GetAllStackTraces/Native$ cmake CMakeLists.txt
-- The C compiler identification is GNU 11.4.0
-- The CXX compiler identification is GNU 11.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found JNI: /home/oz-mint/.sdkman/candidates/java/current/lib/libjawt.so
-- JNI_INCLUDE_DIRS=/home/oz-mint/.sdkman/candidates/java/current/include;/home/oz-mint/.sdkman/candidates/java/current/include/linux;/home/oz-mint/.sdkman/candidates/java/current/include
-- JNI_LIBRARIES=/home/oz-mint/.sdkman/candidates/java/current/lib/libjawt.so;/home/oz-mint/.sdkman/candidates/java/current/lib/server/libjvm.so
-- Configuring done
-- Generating done
-- Build files have been written to: /home/oz-mint/tmp/jvmti-examples/GetAllStackTraces/Native
oz-mint@ozmint-MACH-WX9:~/tmp/jvmti-examples/GetAllStackTraces/Native$ make
[ 50%] Building CXX object CMakeFiles/GetAllStackTraces.dir/library.cpp.o
[100%] Linking CXX shared library libGetAllStackTraces.so
[100%] Built target GetAllStackTraces
- For compiling the java class we need to use -g /jvmti-examples/GetAllStackTraces/Java/src$ javac -g GetAllStackTracesTest.java
- Running the java class with the agentlib so command and result below
oz-mint@ozmint-MACH-WX9:~/tmp/jvmti-examples/GetAllStackTraces/Java/src$ java -agentpath:../../Native/libGetAllStackTraces.so -agentlib:GetAllStackTraces GetAllStackTracesTest
Class Name : LGetAllStackTracesTest;
Got Exception in Method : exceptionMethod
Class Name : LGetAllStackTracesTest;
Got Exception in Method : methodStack5
Class Name : LGetAllStackTracesTest;
Got Exception in Method : methodStack4
Class Name : LGetAllStackTracesTest;
Got Exception in Method : methodStack3
Class Name : LGetAllStackTracesTest;
Got Exception in Method : methodStack2
Class Name : LGetAllStackTracesTest;
Got Exception in Method : exceptionMethod
Class Name : LGetAllStackTracesTest;
Got Exception in Method : methodStack5
Class Name : LGetAllStackTracesTest;
Got Exception in Method : methodStack4
Class Name : LGetAllStackTracesTest;
Got Exception in Method : methodStack3
Class Name : LGetAllStackTracesTest;
Exception thrown in Java code...
Got Exception in Method : methodStack2oz-mint@ozmint-MACH-WX9:~/tmp/jvmti-examples/GetAllStackTraces/Java/src$
Ref:https://www.oracle.com/technical-resources/articles/javase/jvmti.html
Top comments (0)