Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
b7166c7
fix and test
fandreuz Oct 13, 2025
29f1a25
nl
fandreuz Oct 13, 2025
b821ba3
cc
fandreuz Oct 13, 2025
001e2f6
macro
fandreuz Oct 13, 2025
96e2340
fix check
fandreuz Oct 13, 2025
cdd5a22
mv
fandreuz Oct 14, 2025
e3df527
nn
fandreuz Oct 14, 2025
941f9a6
mv
fandreuz Oct 14, 2025
49722e1
summary
fandreuz Oct 14, 2025
84f168b
Update src/hotspot/share/prims/jvmtiAgentList.cpp
fandreuz Oct 16, 2025
2e21bc3
nn
fandreuz Oct 16, 2025
e9646e6
errno
fandreuz Oct 16, 2025
cbbbf8a
ops
fandreuz Oct 16, 2025
b53c2da
debug
fandreuz Oct 16, 2025
2a7ab98
replace
fandreuz Oct 16, 2025
7870ca7
revert
fandreuz Oct 16, 2025
1363424
check jcmd exist
fandreuz Oct 16, 2025
bc998af
wip
fandreuz Oct 17, 2025
e0b879b
revert
fandreuz Oct 17, 2025
4269dee
quote
fandreuz Oct 17, 2025
8bbedbc
backslash
fandreuz Oct 17, 2025
84fa5ac
debug
fandreuz Oct 17, 2025
5b98640
revert
fandreuz Oct 17, 2025
4dddb5a
mv to attach
fandreuz Oct 17, 2025
4fa7d72
simplfiy
fandreuz Oct 17, 2025
4fd6e07
cc
fandreuz Oct 17, 2025
1af09f8
c++
fandreuz Oct 17, 2025
93667b8
msg
fandreuz Oct 17, 2025
2e628c6
nullptr
fandreuz Oct 17, 2025
30b2cf9
fix tool call
fandreuz Oct 17, 2025
bd4b98a
PidJcmdExecutor. unused import. cc
fandreuz Oct 21, 2025
9d51f08
rephrase
fandreuz Oct 21, 2025
206fd08
close
fandreuz Oct 21, 2025
a401f0a
indent
fandreuz Oct 21, 2025
925f9fc
jvmti errors
fandreuz Oct 21, 2025
60d6fdf
empty stderr
fandreuz Oct 21, 2025
ba3dc02
cc
fandreuz Oct 21, 2025
2059679
return value
fandreuz Oct 22, 2025
462f982
Merge branch 'master' into JDK-8359472
fandreuz Oct 22, 2025
1dafec3
rename
fandreuz Oct 22, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/hotspot/share/prims/jvmtiAgentList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ void JvmtiAgentList::load_xrun_agents() {
// Invokes Agent_OnAttach for agents loaded dynamically during runtime.
void JvmtiAgentList::load_agent(const char* agent_name, bool is_absolute_path,
const char* options, outputStream* st) {
if (JvmtiEnvBase::get_phase() != JVMTI_PHASE_LIVE) {
st->print_cr("Dynamic agent loading is only permitted in the live phase");
return;
}

JvmtiAgent* const agent = new JvmtiAgent(agent_name, options, is_absolute_path, /* dynamic agent */ true);
if (agent->load(st)) {
add(agent);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

import com.sun.tools.attach.VirtualMachine;
import com.sun.tools.attach.AgentLoadException;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.AfterAll;

import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.concurrent.TimeUnit;
import jdk.test.lib.dcmd.PidJcmdExecutor;
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.process.ProcessTools;
import jdk.test.lib.Utils;

/*
* @test EarlyDynamicLoad
* @summary Test that dynamic attach fails gracefully when the JVM is not in live phase.
* @requires vm.jvmti
* @library /test/lib
* @run junit EarlyDynamicLoad
*/
public class EarlyDynamicLoad {
private static final String EXPECTED_MESSAGE = "Dynamic agent loading is only permitted in the live phase";

private static Process child;

@BeforeAll
static void startAndWaitChild() throws Exception {
child = ProcessTools.createTestJavaProcessBuilder(
"-XX:+StartAttachListener",
"-agentpath:" + Utils.TEST_NATIVE_PATH + File.separator + System.mapLibraryName("EarlyDynamicLoad"),
"--version").start();

// Wait until the process enters VMStartCallback
try (InputStream is = child.getInputStream()) {
is.read();
}
}

@AfterAll
static void stopChild() throws Exception {
try (OutputStream os = child.getOutputStream()) {
os.write(0);
}

if (!child.waitFor(5, TimeUnit.SECONDS)) {
child.destroyForcibly();
throw new AssertionError("Timed out while waiting child process to complete");
}

OutputAnalyzer analyzer = new OutputAnalyzer(child);
analyzer.shouldHaveExitValue(0);
analyzer.stderrShouldBeEmpty();
}

@Test
public void virtualMachine() throws Exception {
try {
VirtualMachine vm = VirtualMachine.attach(String.valueOf(child.pid()));
vm.loadAgent("some.jar");
vm.detach();
throw new AssertionError("Should have failed with AgentLoadException");
} catch(AgentLoadException exception) {
if (!exception.getMessage().contains(EXPECTED_MESSAGE)) {
throw new AssertionError("Unexpected error message", exception);
}
}
}

@Test
public void jcmd() throws Exception {
PidJcmdExecutor executor = new PidJcmdExecutor(String.valueOf(child.pid()));
OutputAnalyzer out = executor.execute("JVMTI.agent_load some.jar");

out.shouldHaveExitValue(0);
out.stdoutShouldContain(EXPECTED_MESSAGE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

#include <jvmti.h>
#include <cstdio>
#include <cstring>

extern "C" {

static void JNICALL VMStartCallback(jvmtiEnv* jvmti, JNIEnv* env) {
putchar('1');
fflush(stdout);
getchar();
}

JNIEXPORT int Agent_OnLoad(JavaVM* vm, char* options, void* reserved) {
jvmtiEnv* jvmti;
if (vm->GetEnv((void**) &jvmti, JVMTI_VERSION_1_0) != JVMTI_ERROR_NONE) {
fprintf(stderr, "JVMTI error occurred during GetEnv\n");
return JNI_ERR;
}

jvmtiEventCallbacks callbacks;
memset(&callbacks, 0, sizeof(callbacks));
callbacks.VMStart = VMStartCallback;

if (jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks)) != JVMTI_ERROR_NONE) {
fprintf(stderr, "JVMTI error occurred during SetEventCallbacks\n");
return JNI_ERR;
}
if (jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_START, nullptr) != JVMTI_ERROR_NONE) {
fprintf(stderr, "JVMTI error occurred during SetEventNotificationMode\n");
return JNI_ERR;
}

return JNI_OK;
}

}