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
23 * questions.
24 */
25 package jdk.internal.vm;
26
27 import java.util.Arrays;
28 import java.util.stream.Stream;
29
30 /**
31 * Represents a snapshot of information about a Thread.
32 */
33 class ThreadSnapshot {
34 private static final StackTraceElement[] EMPTY_STACK = new StackTraceElement[0];
35 private static final ThreadLock[] EMPTY_LOCKS = new ThreadLock[0];
36
37 // filled by VM
38 private String name;
39 private int threadStatus;
40 private Thread carrierThread;
41 private StackTraceElement[] stackTrace;
42 // owned monitors
43 private ThreadLock[] locks;
44 // an object the thread is blocked/waiting on, converted to ThreadBlocker by ThreadSnapshot.of()
45 private int blockerTypeOrdinal;
46 private Object blockerObject;
47
48 // set by ThreadSnapshot.of()
49 private ThreadBlocker blocker;
50
51 private ThreadSnapshot() {}
52
53 /**
54 * Take a snapshot of a Thread to get all information about the thread.
55 * Return null if a ThreadSnapshot is not created, for example if the
56 * thread has terminated.
57 * @throws UnsupportedOperationException if not supported by VM
58 */
59 static ThreadSnapshot of(Thread thread) {
60 ThreadSnapshot snapshot = create(thread);
61 if (snapshot == null) {
62 return null; // thread terminated
63 }
64 if (snapshot.stackTrace == null) {
65 snapshot.stackTrace = EMPTY_STACK;
66 }
67 if (snapshot.locks != null) {
68 Arrays.stream(snapshot.locks).forEach(ThreadLock::finishInit);
69 } else {
70 snapshot.locks = EMPTY_LOCKS;
71 }
72 if (snapshot.blockerObject != null) {
73 snapshot.blocker = new ThreadBlocker(snapshot.blockerTypeOrdinal, snapshot.blockerObject);
74 snapshot.blockerObject = null; // release
75 }
76 return snapshot;
77 }
78
79 /**
80 * Returns the thread name.
81 */
82 String threadName() {
83 return name;
202 return type;
203 }
204
205 Object lockObject() {
206 if (type == OwnedLockType.ELIMINATED) {
207 // we have no lock object, lock contains lock class
208 return null;
209 }
210 return obj;
211 }
212 }
213
214 private record ThreadBlocker(BlockerLockType type, Object obj) {
215 private static final BlockerLockType[] lockTypeValues = BlockerLockType.values(); // cache
216
217 ThreadBlocker(int typeOrdinal, Object obj) {
218 this(lockTypeValues[typeOrdinal], obj);
219 }
220 }
221
222 private static native ThreadSnapshot create(Thread thread);
223 }
|
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
23 * questions.
24 */
25 package jdk.internal.vm;
26
27 import java.util.Arrays;
28 import java.util.stream.Stream;
29 import jdk.internal.access.JavaLangAccess;
30 import jdk.internal.access.SharedSecrets;
31
32 /**
33 * Represents a snapshot of information about a Thread.
34 */
35 class ThreadSnapshot {
36 private static final JavaLangAccess JLA = SharedSecrets.getJavaLangAccess();
37 private static final StackTraceElement[] EMPTY_STACK = new StackTraceElement[0];
38 private static final ThreadLock[] EMPTY_LOCKS = new ThreadLock[0];
39 private static final ThreadSnapshot NOT_ALIVE = new ThreadSnapshot();
40
41 // filled by VM
42 private String name;
43 private int threadStatus;
44 private Thread carrierThread;
45 private StackTraceElement[] stackTrace;
46 // owned monitors
47 private ThreadLock[] locks;
48 // an object the thread is blocked/waiting on, converted to ThreadBlocker by ThreadSnapshot.of()
49 private int blockerTypeOrdinal;
50 private Object blockerObject;
51
52 // set by ThreadSnapshot.of()
53 private ThreadBlocker blocker;
54
55 private ThreadSnapshot() {}
56
57 /**
58 * Take a snapshot of a Thread to get all information about the thread.
59 * Return null if the thread is not alive.
60 * @throws UnsupportedOperationException if not supported by VM
61 */
62 static ThreadSnapshot of(Thread thread) {
63 ThreadSnapshot snapshot;
64 if (thread.isVirtual()) {
65 do {
66 // assume unmounted
67 snapshot = JLA.supplyIfUnmounted(thread,
68 () -> create(thread, true), // unmounted and alive
69 () -> NOT_ALIVE); // not alive
70 if (snapshot == NOT_ALIVE) {
71 return null;
72 } else if (snapshot == null) {
73 // not unmounted, retry assuming mounted
74 snapshot = create(thread, false);
75 if (snapshot == null) {
76 // yield before retry
77 Thread.yield();
78 }
79 }
80 } while (snapshot == null);
81 } else {
82 // platform thread
83 snapshot = create(thread, false);
84 if (snapshot == null) {
85 return null; // not alive
86 }
87 }
88
89 if (snapshot.stackTrace == null) {
90 snapshot.stackTrace = EMPTY_STACK;
91 }
92 if (snapshot.locks != null) {
93 Arrays.stream(snapshot.locks).forEach(ThreadLock::finishInit);
94 } else {
95 snapshot.locks = EMPTY_LOCKS;
96 }
97 if (snapshot.blockerObject != null) {
98 snapshot.blocker = new ThreadBlocker(snapshot.blockerTypeOrdinal, snapshot.blockerObject);
99 snapshot.blockerObject = null; // release
100 }
101 return snapshot;
102 }
103
104 /**
105 * Returns the thread name.
106 */
107 String threadName() {
108 return name;
227 return type;
228 }
229
230 Object lockObject() {
231 if (type == OwnedLockType.ELIMINATED) {
232 // we have no lock object, lock contains lock class
233 return null;
234 }
235 return obj;
236 }
237 }
238
239 private record ThreadBlocker(BlockerLockType type, Object obj) {
240 private static final BlockerLockType[] lockTypeValues = BlockerLockType.values(); // cache
241
242 ThreadBlocker(int typeOrdinal, Object obj) {
243 this(lockTypeValues[typeOrdinal], obj);
244 }
245 }
246
247 /**
248 * Return the snapshot of the given thread if alive. If the thread is a virtual
249 * thread this method returns the snapshot if the virtual thread is mounted. If
250 * the virtual thread is unmounted then it must be suspended.
251 * @param thread the target thread
252 * @param suspendedByCaller true if a suspended unmounted virtual thread
253 * @return the snapshot, null if not alive, null if unmounted virtual and not suspended
254 */
255 private static native ThreadSnapshot create(Thread thread, boolean suspendedByCaller);
256 }
|