Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
43 changes: 29 additions & 14 deletions test/jdk/jdk/crac/jdwp/JdwpTransportTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023, Azul Systems, Inc. All rights reserved.
* Copyright (c) 2023, 2025, Azul Systems, Inc. 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
Expand All @@ -22,6 +22,7 @@
*/

import jdk.crac.*;
import jdk.test.lib.crac.AsyncStreamReader;
import jdk.test.lib.crac.CracBuilder;
import jdk.test.lib.crac.CracEngine;
import jdk.test.lib.crac.CracProcess;
Expand All @@ -35,13 +36,10 @@
import com.sun.jdi.VirtualMachine;
import com.sun.jdi.VMDisconnectedException;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import static jdk.test.lib.Asserts.*;

Expand Down Expand Up @@ -88,14 +86,20 @@ private VirtualMachine attachDebugger() throws Exception {
return vm;
}

private void waitForString(BufferedReader reader, String str) throws IOException {
for (String line = reader.readLine(); true; line = reader.readLine()) {
private void waitForString(AsyncStreamReader reader, String str, long timeoutMillis) throws Exception {
while (true) {
String line = reader.readLine(timeoutMillis);
System.out.println(line);
if (line.contains(str))
break;
}
}

private void waitForString(AsyncStreamReader reader, String str) throws Exception {
final long timeoutMillis = 30000;
waitForString(reader, str, timeoutMillis);
}

@Override
public void test() throws Exception {
final String suspendArg = ",suspend=" + (suspendOnJdwpStart ? "y" : "n");
Expand All @@ -104,9 +108,9 @@ public void test() throws Exception {
builder.engine(CracEngine.SIMULATE);

CracProcess process = builder.captureOutput(true).startCheckpoint();
var errReader = new BufferedReader(new InputStreamReader(process.errOutput()));
var errReader = new AsyncStreamReader(process.errOutput());
var reader = new AsyncStreamReader(process.output());
try {
var reader = new BufferedReader(new InputStreamReader(process.output()));
if (!suspendOnJdwpStart) {
waitForString(reader, STARTED);
} else {
Expand Down Expand Up @@ -140,21 +144,32 @@ public void test() throws Exception {
process.input().flush();

process.waitForSuccess();
} catch (TimeoutException e) {
System.err.println("reader.isRunning()=" + reader.isRunning());
process.printThreadDump();
process.dumpProcess();
throw e;
} finally {
for (String line = errReader.readLine(); null != line; line = errReader.readLine()) {
System.err.println(line);
try {
final long timeoutMillis = 1000;
while (true) {
System.err.println("APP STDERR: " + errReader.readLine(timeoutMillis));
}
process.destroyForcibly();
} catch (TimeoutException e) {
// do nothing
}
reader.close();
errReader.close();
process.destroyForcibly();
}
}

@Override
public void exec() throws Exception {
System.out.println(STARTED);
System.out.println(STARTED + ", pid=" + ProcessHandle.current().pid());
Core.checkpointRestore();
System.out.println("APP: Restored");
System.in.read(); // Wait for debugger is attached and done
System.out.println("APP: Finished");
}
}

76 changes: 76 additions & 0 deletions test/lib/jdk/test/lib/crac/AsyncStreamReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2025, Azul Systems, Inc. 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.
*/

package jdk.test.lib.crac;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class AsyncStreamReader implements AutoCloseable {
private final LinkedBlockingQueue<String> lines = new LinkedBlockingQueue<>();
private volatile boolean isRunning = true;
private final InputStream inputStream;
private final Thread readerThread;

public AsyncStreamReader(InputStream stream) {
this.inputStream = stream;
this.readerThread = new Thread(() -> {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {
for (String line; (line = reader.readLine()) != null;) {
lines.put(line);
}
} catch (IOException | InterruptedException e) {
e.printStackTrace();
} finally {
isRunning = false;
}
});
this.readerThread.setDaemon(true);
this.readerThread.start();
}

public String readLine(long timeoutMillis) throws TimeoutException, InterruptedException {
String line = lines.poll(timeoutMillis, TimeUnit.MILLISECONDS);
if (line == null) {
throw new TimeoutException("Timeout " + timeoutMillis + " msecs while waiting for new line");
}
return line;
}

public boolean isRunning() {
return isRunning;
}

public void close() {
try {
this.inputStream.close();
} catch (IOException ignored) {
}
this.readerThread.interrupt();
}
}
71 changes: 71 additions & 0 deletions test/lib/jdk/test/lib/crac/CracProcess.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.process.StreamPumper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.nio.file.*;
import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -192,4 +194,73 @@ public void sendNewline() throws IOException {
public void destroyForcibly() {
process.destroyForcibly();
}

public void printThreadDump() throws IOException {
final long pid = this.pid();
boolean isAlive = ProcessHandle.of(pid).map(ProcessHandle::isAlive).orElse(false);
if (!isAlive) {
System.err.println("Process " + pid + " is not alive.");
} else {
System.err.println("Running: jcmd " + pid + " Thread.print");
Process jcmdProc = new ProcessBuilder(jdk.test.lib.Utils.TEST_JDK + "/bin/jcmd", String.valueOf(pid), "Thread.print")
.redirectErrorStream(true)
.start();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(jcmdProc.getInputStream()))) {
for (String line = reader.readLine(); null != line; line = reader.readLine()) {
System.err.println("JCMD: " + line);
}
}
}
}

private static boolean checkGcoreAvailable() {
ProcessBuilder builder = new ProcessBuilder("which", "gcore");
builder.redirectErrorStream(true);
try {
Process process = builder.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = reader.readLine();
int exitCode = process.waitFor();
if (exitCode == 0 && line != null && !line.trim().isEmpty()) {
System.out.println("gcore is available.");
return true;
} else {
System.out.println("gcore is NOT available.");
return false;
}
} catch (IOException | InterruptedException e) {
System.out.println("Could not run 'which gcore' or was interrupted");
return false;
}
}

public void dumpProcess() {
// For gcore, it's required 'sudo sysctl -w kernel.yama.ptrace_scope=0'
// For kill, it's required 'ulimit -c unlimited && echo core.%p | sudo tee /proc/sys/kernel/core_pattern'
final long pid = this.pid();
ProcessBuilder builder = checkGcoreAvailable() ? new ProcessBuilder("gcore", String.valueOf(pid))
: new ProcessBuilder("kill", "-ABRT", String.valueOf(pid));
builder.redirectErrorStream(true);
try {
Process process = builder.start();
var reader = new AsyncStreamReader(process.getInputStream());
int exitCode = process.waitFor();
try {
while (true) {
System.out.println("dumpProcess: " + reader.readLine(100));
}
} catch (Exception e) {
// do nothing
}
if (exitCode == 0) {
System.out.println("Core dump seems to be created successfully for pid=" + pid);
} else {
System.out.println("Something went wrong while dumping the app");
}
} catch (IOException | InterruptedException e) {
System.out.println("Exception thrown while dumping the app");
e.printStackTrace();
}
}

}