JNIEXPORT jint JNICALL Agent_OnLoad()

in src/agent.cpp [57:95]


JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
    logger::handleOptions(options);

    logger::debug("on agent load");
    jvmtiEnv *jvmti = nullptr;
    jint result = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_0);
    if (result != JNI_OK || jvmti == nullptr) {
        std::cerr << "ERROR: Unable to access JVMTI!" << std::endl;
        return result;
    }

    logger::debug("set capabilities");
    jvmtiCapabilities capabilities;
    setRequiredCapabilities(jvmti, capabilities);
    jvmtiError error = jvmti->AddCapabilities(&capabilities);
    if (error != JVMTI_ERROR_NONE) {
        handleError(jvmti, error, "Could not set capabilities");
        return JNI_ERR;
    }

    if (canSampleAllocations) {
        error = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_SAMPLED_OBJECT_ALLOC, NULL);
        if (error != JVMTI_ERROR_NONE) {
            handleError(jvmti, error, "Could not set allocation sampling notification mode");
            return JNI_ERR;
        }
    }

    logger::debug("set callbacks");
    jvmtiEventCallbacks callbacks;
    std::memset(&callbacks, 0, sizeof(jvmtiEventCallbacks));
    callbacks.SampledObjectAlloc = SampledObjectAlloc;
    jvmti->SetEventCallbacks(&callbacks, sizeof(jvmtiEventCallbacks));

    gdata = new GlobalAgentData();
    gdata->jvmti = jvmti;
    logger::debug("initializing done");
    return JNI_OK;
}