Skip to content
This repository was archived by the owner on Aug 27, 2022. It is now read-only.
/ lanai Public archive

Commit 9c35471

Browse files
committedFeb 25, 2020
8219904: ClassCastException when calling FlightRecorderMXBean#getRecordings()
Reviewed-by: egahlin, mseledtsov
1 parent 52d7a61 commit 9c35471

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed
 

‎src/jdk.management.jfr/share/classes/jdk/management/jfr/RecordingInfo.java

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -51,7 +51,7 @@ public final class RecordingInfo {
5151
private final String state;
5252
private final boolean dumpOnExit;
5353
private final long size;
54-
private final boolean disk;
54+
private final boolean toDisk;
5555
private final long maxAge;
5656
private final long maxSize;
5757
private final long startTime;
@@ -67,7 +67,7 @@ public final class RecordingInfo {
6767
state = recording.getState().toString();
6868
dumpOnExit = recording.getDumpOnExit();
6969
size = recording.getSize();
70-
disk = recording.isToDisk();
70+
toDisk = recording.isToDisk();
7171

7272
Duration d = recording.getMaxAge();
7373
if (d == null) {
@@ -87,12 +87,17 @@ public final class RecordingInfo {
8787
}
8888

8989
private RecordingInfo(CompositeData cd) {
90-
id = (int) cd.get("id");
90+
id = (long) cd.get("id");
9191
name = (String) cd.get("name");
9292
state = (String) cd.get("state");
9393
dumpOnExit = (boolean) cd.get("dumpOnExit");
9494
size = (long) cd.get("size");
95-
disk = (boolean) cd.get("disk");
95+
if(cd.containsKey("toDisk")){
96+
toDisk = (boolean) cd.get("toDisk");
97+
} else {
98+
// Before JDK-8219904 was fixed, the element name was disk, so for compatibility
99+
toDisk = (boolean) cd.get("disk");
100+
}
96101
maxAge = (Long) cd.get("maxAge");
97102
maxSize = (Long) cd.get("maxSize");
98103
startTime = (Long) cd.get("startTime");
@@ -290,7 +295,7 @@ public long getSize() {
290295
* @return {@code true} if recording is to disk, {@code false} otherwise
291296
*/
292297
public boolean isToDisk() {
293-
return disk;
298+
return toDisk;
294299
}
295300

296301
/**
@@ -342,7 +347,7 @@ public long getDuration() {
342347
* <td>{@code Long}</td>
343348
* </tr>
344349
* <tr>
345-
* <th scope="row">disk</th>
350+
* <th scope="row">toDisk</th>
346351
* <td>{@code Boolean}</td>
347352
* </tr>
348353
* <tr>

‎test/jdk/jdk/jfr/jmx/JmxHelper.java

+23
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.List;
3737
import java.util.Map;
3838

39+
import com.sun.tools.attach.VirtualMachine;
3940
import jdk.jfr.EventType;
4041
import jdk.jfr.FlightRecorder;
4142
import jdk.jfr.Recording;
@@ -52,7 +53,15 @@
5253
import jdk.test.lib.jfr.CommonHelper;
5354
import jdk.test.lib.jfr.Events;
5455

56+
import javax.management.JMX;
57+
import javax.management.MBeanServerConnection;
58+
import javax.management.ObjectName;
59+
import javax.management.remote.JMXConnector;
60+
import javax.management.remote.JMXConnectorFactory;
61+
import javax.management.remote.JMXServiceURL;
62+
5563
public class JmxHelper {
64+
private static final String LOCAL_CONNECTION_ADDRESS = "com.sun.management.jmxremote.localConnectorAddress";
5665

5766
public static RecordingInfo getJmxRecording(long recId) {
5867
for (RecordingInfo r : getFlighteRecorderMXBean().getRecordings()) {
@@ -279,4 +288,18 @@ public static FlightRecorderMXBean getFlighteRecorderMXBean() {
279288
return ManagementFactory.getPlatformMXBean(FlightRecorderMXBean.class);
280289
}
281290

291+
public static long getPID(){
292+
return ManagementFactory.getRuntimeMXBean().getPid();
293+
}
294+
295+
public static FlightRecorderMXBean getFlighteRecorderMXBean(long pid) throws Exception {
296+
VirtualMachine targetVM = VirtualMachine.attach("" + pid);
297+
String jmxServiceUrl = targetVM.getAgentProperties().getProperty(LOCAL_CONNECTION_ADDRESS);
298+
JMXServiceURL jmxURL = new JMXServiceURL(jmxServiceUrl);
299+
JMXConnector connector = JMXConnectorFactory.connect(jmxURL);
300+
MBeanServerConnection connection = connector.getMBeanServerConnection();
301+
302+
ObjectName objectName = new ObjectName("jdk.management.jfr:type=FlightRecorder");
303+
return JMX.newMXBeanProxy(connection, objectName, FlightRecorderMXBean.class);
304+
}
282305
}

‎test/jdk/jdk/jfr/jmx/TestGetRecordings.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -35,7 +35,7 @@
3535
* @key jfr
3636
* @requires vm.hasJFR
3737
* @library /test/lib /test/jdk
38-
* @run main/othervm jdk.jfr.jmx.TestGetRecordings
38+
* @run main/othervm -Djdk.attach.allowAttachSelf=true -Dcom.sun.management.jmxremote jdk.jfr.jmx.TestGetRecordings
3939
*/
4040
public class TestGetRecordings {
4141
public static void main(String[] args) throws Throwable {
@@ -46,5 +46,11 @@ public static void main(String[] args) throws Throwable {
4646
JmxHelper.verifyNotExists(recId, preCreateRecordings);
4747
bean.closeRecording(recId);
4848
JmxHelper.verifyNotExists(recId, bean.getRecordings());
49+
50+
long selfPID = JmxHelper.getPID();
51+
FlightRecorderMXBean remoteBean = JmxHelper.getFlighteRecorderMXBean(selfPID);
52+
long remoteRecId = remoteBean.newRecording();
53+
remoteBean.getRecordings();
54+
remoteBean.closeRecording(remoteRecId);
4955
}
5056
}

0 commit comments

Comments
 (0)
This repository has been archived.