|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ThreadSnapshot.java | 38.9% | 56.5% | 12.5% | 43.8% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK* | |
| 2 | ||
| 3 | PLT Utilities BSD License | |
| 4 | ||
| 5 | Copyright (c) 2007-2010 JavaPLT group at Rice University | |
| 6 | All rights reserved. | |
| 7 | ||
| 8 | Developed by: Java Programming Languages Team | |
| 9 | Rice University | |
| 10 | http://www.cs.rice.edu/~javaplt/ | |
| 11 | ||
| 12 | Redistribution and use in source and binary forms, with or without modification, are permitted | |
| 13 | provided that the following conditions are met: | |
| 14 | ||
| 15 | - Redistributions of source code must retain the above copyright notice, this list of conditions | |
| 16 | and the following disclaimer. | |
| 17 | - Redistributions in binary form must reproduce the above copyright notice, this list of | |
| 18 | conditions and the following disclaimer in the documentation and/or other materials provided | |
| 19 | with the distribution. | |
| 20 | - Neither the name of the JavaPLT group, Rice University, nor the names of the library's | |
| 21 | contributors may be used to endorse or promote products derived from this software without | |
| 22 | specific prior written permission. | |
| 23 | ||
| 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR | |
| 25 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 26 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND | |
| 27 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 28 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 29 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER | |
| 30 | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
| 31 | OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 32 | ||
| 33 | *END_COPYRIGHT_BLOCK*/ | |
| 34 | ||
| 35 | package edu.rice.cs.plt.debug; | |
| 36 | ||
| 37 | import java.io.Serializable; | |
| 38 | import java.util.Date; | |
| 39 | import edu.rice.cs.plt.iter.IterUtil; | |
| 40 | import edu.rice.cs.plt.iter.SizedIterable; | |
| 41 | ||
| 42 | /** A serializable and immutable view of a Thread at a particular time. */ | |
| 43 | public class ThreadSnapshot implements Serializable { | |
| 44 | ||
| 45 | /** The number of stack frames used by the implementation of Thread.getStackTrace() */ | |
| 46 | private static final int GET_STACK_TRACE_DEPTH; | |
| 47 | static { | |
| 48 | 4 | StackTraceElement[] s = Thread.currentThread().getStackTrace(); |
| 49 | 4 | int depth = 0; |
| 50 | 4 | String name = ThreadSnapshot.class.getName(); |
| 51 | 8 | while (depth < s.length) { |
| 52 | 4 | if (name.equals(s[depth].getClassName())) { break; } |
| 53 | 4 | depth++; |
| 54 | } | |
| 55 | 4 | GET_STACK_TRACE_DEPTH = depth; |
| 56 | } | |
| 57 | ||
| 58 | private final String _name; | |
| 59 | private final long _id; | |
| 60 | private final boolean _daemon; | |
| 61 | private final int _priority; | |
| 62 | private final String _group; | |
| 63 | ||
| 64 | private final Date _time; | |
| 65 | ||
| 66 | private final SizedIterable<StackTraceElement> _stack; | |
| 67 | private final StackTraceElement _running; | |
| 68 | private final StackTraceElement _calling; | |
| 69 | ||
| 70 | private final Thread.State _state; | |
| 71 | private final boolean _alive; | |
| 72 | private final boolean _interrupted; | |
| 73 | ||
| 74 | 18 | public ThreadSnapshot() { |
| 75 | 18 | this(Thread.currentThread(), true); |
| 76 | } | |
| 77 | ||
| 78 | 0 | public ThreadSnapshot(Thread t) { |
| 79 | 0 | this(t, t == Thread.currentThread()); |
| 80 | } | |
| 81 | ||
| 82 | /** | |
| 83 | * If {@code filterStack}, the top two stack locations are ignored: they correspond | |
| 84 | * to this constructor and the calling public constructor. | |
| 85 | */ | |
| 86 | 18 | private ThreadSnapshot(Thread t, boolean filterStack) { |
| 87 | 18 | _name = t.getName(); |
| 88 | 18 | _id = t.getId(); |
| 89 | 18 | _daemon = t.isDaemon(); |
| 90 | 18 | _priority = t.getPriority(); |
| 91 | 18 | ThreadGroup g = t.getThreadGroup(); |
| 92 | 18 | _group = (g == null) ? null : g.getName(); |
| 93 | ||
| 94 | 18 | _time = new Date(); |
| 95 | ||
| 96 | 18 | StackTraceElement[] s = t.getStackTrace(); |
| 97 | 18 | if (filterStack) { |
| 98 | 18 | int offset = GET_STACK_TRACE_DEPTH + 2; // two extra for ThreadSnapshot constructors |
| 99 | 18 | if (s.length > offset) { // at least one useful StackTraceElement is available |
| 100 | 18 | _stack = IterUtil.arraySegment(s, offset); |
| 101 | 18 | _running = s[offset]; |
| 102 | 18 | _calling = (s.length > offset+1) ? s[offset+1] : null; |
| 103 | } | |
| 104 | else { | |
| 105 | 0 | _stack = IterUtil.empty(); |
| 106 | 0 | _running = null; |
| 107 | 0 | _calling = null; |
| 108 | } | |
| 109 | } | |
| 110 | else { | |
| 111 | 0 | _stack = IterUtil.asIterable(s); |
| 112 | 0 | _running = (s.length >= 1) ? s[0] : null; |
| 113 | 0 | _calling = (s.length >= 2) ? s[1] : null; |
| 114 | } | |
| 115 | ||
| 116 | 18 | _state = t.getState(); |
| 117 | 18 | _alive = t.isAlive(); |
| 118 | 18 | _interrupted = t.isInterrupted(); |
| 119 | } | |
| 120 | ||
| 121 | /** The result of {@link Thread#getName()} at the snapshot time. */ | |
| 122 | 0 | public String getName() { return _name; } |
| 123 | /** The result of {@link Thread#getId()} at the snapshot time. */ | |
| 124 | 0 | public long getId() { return _id; } |
| 125 | /** The result of {@link Thread#isDaemon()} at the snapshot time. */ | |
| 126 | 0 | public boolean isDaemon() { return _daemon; } |
| 127 | /** The result of {@link Thread#getPriority()} at the snapshot time. */ | |
| 128 | 0 | public int getPriority() { return _priority; } |
| 129 | /** | |
| 130 | * The name of {@code t}'s ThreadGroup at the snapshot time, as returned by {@link Thread#getThreadGroup()}, | |
| 131 | * or {@code null} if it did not have one. | |
| 132 | */ | |
| 133 | 0 | public String getThreadGroup() { return _group; } |
| 134 | ||
| 135 | /** The time at which the snapshot was taken. */ | |
| 136 | 0 | public Date snapshotTime() { return _time; } |
| 137 | ||
| 138 | /** | |
| 139 | * The stack trace at creation time, as returned by {@link Thread#getStackTrace()}. If the thread was | |
| 140 | * used to take a snapshot of itself, the relevant ThreadSnapshot invocations are hidden in the result. | |
| 141 | * Note that {@code Thread.getStackTrace()} does not guarantee complete results, and the trace may even | |
| 142 | * be empty. | |
| 143 | */ | |
| 144 | 0 | public SizedIterable<StackTraceElement> getStackTrace() { return _stack; } |
| 145 | /** | |
| 146 | * The top of the stack at snapshot time, or {@code null} if unavailable. If the thread was | |
| 147 | * used to take a snapshot of itself, the relevant ThreadSnapshot invocations are hidden in the result. | |
| 148 | */ | |
| 149 | 0 | public StackTraceElement runningLocation() { return _running; } |
| 150 | /** | |
| 151 | * The second stack element at snapshot time, or {@code null} if unavailable. If the thread was | |
| 152 | * used to take a snapshot of itself, the relevant ThreadSnapshot invocations are hidden in the result. | |
| 153 | */ | |
| 154 | 0 | public StackTraceElement callingLocation() { return _calling; } |
| 155 | ||
| 156 | /** The result of {@link Thread#getState()} at the snapshot time. */ | |
| 157 | 0 | public Thread.State getState() { return _state; } |
| 158 | /** The result of {@link Thread#isAlive()} at the snapshot time. */ | |
| 159 | 0 | public boolean isAlive() { return _alive; } |
| 160 | /** The result of {@link Thread#isInterrupted()} at the snapshot time. */ | |
| 161 | 0 | public boolean isInterrupted() { return _interrupted; } |
| 162 | ||
| 163 | /** | |
| 164 | * Produce a string representation of the thread, as would be returned by {@link Thread#toString} at | |
| 165 | * snapshot time. | |
| 166 | */ | |
| 167 | 0 | public String toString() { |
| 168 | 0 | return "Thread[" + _name + "," + _priority + "," + (_group == null ? "" : _group) + "]"; |
| 169 | } | |
| 170 | ||
| 171 | } |
|
||||||||||