1 /*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
61 if (pid != 0) {
62 singlePid = String.valueOf(pid);
63 }
64 } catch (NumberFormatException nfe) {
65 matchClass = pidArg;
66 }
67 }
68
69 private static String getExcludeStringFrom(Class<?> excludeClass) {
70 if (excludeClass == null) {
71 return "";
72 }
73 Module m = excludeClass.getModule();
74 if (m.isNamed()) {
75 return m.getName() + "/" + excludeClass.getName();
76 }
77 return excludeClass.getName();
78 }
79
80 private static boolean check(VirtualMachineDescriptor vmd, String excludeClass, String partialMatch) {
81 String mainClass = null;
82 try {
83 VmIdentifier vmId = new VmIdentifier(vmd.id());
84 MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(vmId);
85 MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, -1);
86 mainClass = MonitoredVmUtil.mainClass(monitoredVm, true);
87 monitoredHost.detach(monitoredVm);
88 } catch (NullPointerException npe) {
89 // There is a potential race, where a running java app is being
90 // queried, unfortunately the java app has shutdown after this
91 // method is started but before getMonitoredVM is called.
92 // If this is the case, then the /tmp/hsperfdata_xxx/pid file
93 // will have disappeared and we will get a NullPointerException.
94 // Handle this gracefully....
95 return false;
96 } catch (MonitorException | URISyntaxException e) {
97 return false;
98 }
99
100 if (excludeClass != null && mainClass.equals(excludeClass)) {
101 return false;
102 }
103
104 if (partialMatch != null && mainClass.indexOf(partialMatch) == -1) {
105 return false;
106 }
107
108 return true;
109 }
110
111 private static Collection<VirtualMachineDescriptor> getSingleVMD(String pid) {
112 Collection<VirtualMachineDescriptor> vids = new ArrayList<>();
113 List<VirtualMachineDescriptor> vmds = VirtualMachine.list();
114 for (VirtualMachineDescriptor vmd : vmds) {
115 if (check(vmd, null, null)) {
116 if (pid.equals(vmd.id())) {
117 vids.add(vmd);
118 }
|
1 /*
2 * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
61 if (pid != 0) {
62 singlePid = String.valueOf(pid);
63 }
64 } catch (NumberFormatException nfe) {
65 matchClass = pidArg;
66 }
67 }
68
69 private static String getExcludeStringFrom(Class<?> excludeClass) {
70 if (excludeClass == null) {
71 return "";
72 }
73 Module m = excludeClass.getModule();
74 if (m.isNamed()) {
75 return m.getName() + "/" + excludeClass.getName();
76 }
77 return excludeClass.getName();
78 }
79
80 private static boolean check(VirtualMachineDescriptor vmd, String excludeClass, String partialMatch) {
81
82 String mainClass = null;
83
84 // Get the main class name using platform specific helper
85 ProcessHelper helper = ProcessHelper.platformProcessHelper();
86 if (helper != null) {
87 mainClass = helper.getMainClass(vmd.id());
88 }
89
90 // If the main class name is still unset then retrieve it with the attach mechanism
91 if (mainClass == null) {
92 try {
93 VmIdentifier vmId = new VmIdentifier(vmd.id());
94 MonitoredHost monitoredHost = MonitoredHost.getMonitoredHost(vmId);
95 MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmId, -1);
96 mainClass = MonitoredVmUtil.mainClass(monitoredVm, true);
97 monitoredHost.detach(monitoredVm);
98 } catch (NullPointerException npe) {
99 // There is a potential race, where a running java app is being
100 // queried, unfortunately the java app has shutdown after this
101 // method is started but before getMonitoredVM is called.
102 // If this is the case, then the /tmp/hsperfdata_xxx/pid file
103 // will have disappeared and we will get a NullPointerException.
104 // Handle this gracefully....
105 return false;
106 } catch (MonitorException | URISyntaxException e) {
107 return false;
108 }
109 }
110
111
112 if (excludeClass != null && mainClass.equals(excludeClass)) {
113 return false;
114 }
115
116 if (partialMatch != null && mainClass.indexOf(partialMatch) == -1) {
117 return false;
118 }
119
120 return true;
121 }
122
123 private static Collection<VirtualMachineDescriptor> getSingleVMD(String pid) {
124 Collection<VirtualMachineDescriptor> vids = new ArrayList<>();
125 List<VirtualMachineDescriptor> vmds = VirtualMachine.list();
126 for (VirtualMachineDescriptor vmd : vmds) {
127 if (check(vmd, null, null)) {
128 if (pid.equals(vmd.id())) {
129 vids.add(vmd);
130 }
|