JVMTI – Get System Properties

Introduction: This article delves into leveraging JVMTI GetSystemProperties to extract a comprehensive list of system properties within Java Virtual Machine (JVM) environments. By utilizing this technique, software engineers can attain valuable insights into system configurations, aiding in performance optimization and troubleshooting.

Code Implementation:

cppCopy code#include "jvmti.h"
#include <iostream>

JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
    std::cout << "JVMTIGetSystemProperties :: Agent Initiated!!";
    jvmtiEnv *jvmti;
    jvmtiError error;
    jint result;
    jint count = 0;
    char **props;

    result = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_2);

    if (result != JNI_OK) {
        std::cerr << "Unable to access JVMTI!!!";
    }

    error = jvmti->GetSystemProperties(&count, &props);
    if (error != JVMTI_ERROR_NONE) return JNI_FALSE;

    std::cout << "JVMTI Agent (System Property) \n";
    for (int i = 0; i < count; i++) {
        char *value;
        error = jvmti->GetSystemProperty(props[i], &value);
        if (error != JVMTI_ERROR_NONE) return JNI_FALSE;
        std::cout << props[i] << " = " << value << "\n";
    }
    return JNI_OK;
}

CMake Configuration:

cmakeCopy codecmake_minimum_required(VERSION 3.13)
project(JVMTIGetSystemProperties)

set(CMAKE_CXX_STANDARD 11)

# Compiler and Linker flags
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_COLOR_MAKEFILE ON)

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
    # using Clang
    message("Clang")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
    # using GCC
    message("GCC")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Intel")
    # using Intel C++
    message("IntelC++")
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
    # using Visual Studio C++
    message("Visual Studio C++")
endif ()

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-long-long -pedantic")
set(CMAKE_CXX_FLAGS_VALGRIND "-fno-inline")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${CMAKE_CXX_FLAGS_VALGRIND} -Werror -Wno-missing-field-initializers -Wmissing-braces")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O4 -DNDEBUG")

find_package(JNI)

if (JNI_FOUND)
    message(STATUS "JNI_INCLUDE_DIRS=${JNI_INCLUDE_DIRS}")
    message(STATUS "JNI_LIBRARIES=${JNI_LIBRARIES}")
endif ()

set(HEADER_BASE "${CMAKE_CURRENT_SOURCE_DIR}/include")
include_directories(${JNI_INCLUDE_DIRS})
include_directories(${HEADER_BASE})

set(SOURCE_FILES library.cpp ${HEADER_BASE})

add_library(JVMTIGetSystemProps SHARED ${SOURCE_FILES})

Usage: Build the provided source code and integrate the resultant library into a running JVM environment using the agentlib configuration parameter.

For further details and access to the complete source code, please visit the GitHub repository: https://github.com/sachin-handiekar/jvmti-examples

Conclusion: By employing JVMTI GetSystemProperties, software engineers gain a powerful tool for obtaining detailed system property information within JVM contexts. This facilitates enhanced system analysis, aiding in the optimization and maintenance of Java applications.

Did you find this article valuable?

Support Sachin Handiekar by becoming a sponsor. Any amount is appreciated!