/
ThreadMonitorHelper.java
223 lines (203 loc) · 6.75 KB
/
ThreadMonitorHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package org.jboss.byteman.sample.helper;
import org.jboss.byteman.rule.Rule;
/**
* Helper class used by ThreadMonitorHelper script to trace thread operations
*/
public class ThreadMonitorHelper extends StackTraceHelper
{
protected ThreadMonitorHelper(Rule rule) {
super(rule);
}
/**
* trace creation of the supplied thread to System.out
*
* this should only be triggered from the constructor for class java.lang.Thread"
*
* @param thread the newly created thread
*/
public void traceCreate(Thread thread)
{
traceCreate(thread, "out");
}
/**
* trace creation of the supplied thread to the trace stream identified by key
*
* @param thread the newly created thread
* @param key an object identifying the trace stream to which output should be generated
*/
public void traceCreate(Thread thread, Object key)
{
StringBuffer buffer = new StringBuffer();
int l = stack.length;
int t = triggerIndex();
if (t < 0) {
return;
}
int i = matchIndex("java.lang.Thread.<init>", t, l);
if (i < 0) {
// illegal usage
traceStack("ThreadMonitorHelper.traceCreate : should only be triggered below Thread.<init>\n", key);
return;
}
buffer.append("*** Thread create ");
buffer.append(thread.getName());
buffer.append(" ");
buffer.append(thread.getClass().getCanonicalName());
buffer.append('\n');
// find bottommost constructor invocation
i++;
while (i < l && matchIndex(".*<init>", i, i) >= 0) {
i++;
}
if (i == l) {
// happens when a system thread is created by the runtime
buffer.append(" from VM runtime\n");
} else {
buffer.append(" from ");
printlnFrame(buffer, i);
}
trace(key, buffer.toString());
}
/**
* trace start of the supplied thread to System.out
*
* this should only be triggered from the call to java.lang.Thread.start"
*
* @param thread the newly starting thread
*/
public void traceStart(Thread thread)
{
traceStart(thread, "out");
}
/**
* trace start of the supplied thread to the trace stream identified by key
*
* this should only be triggered from the call to java.lang.Thread.start"
*
* @param thread the newly starting thread
* @param key an object identifying the trace stream to which output should be generated
*/
public void traceStart(Thread thread, Object key)
{
StringBuffer buffer = new StringBuffer();
int l = stack.length;
int t = triggerIndex();
if (t < 0) {
return;
}
int i = matchIndex("java.lang.Thread.start", t, l);
if (i < 0) {
// illegal usage
traceStack("ThreadMonitorHelper.traceStart : should only be triggered below Thread.start\n", key);
return;
}
buffer.append("*** Thread start ");
buffer.append(thread.getName());
buffer.append(" ");
buffer.append(thread.getClass().getCanonicalName());
buffer.append('\n');
StackTraceElement[] stack = Thread.currentThread().getStackTrace();
if (++i == l) {
// should not happen
traceStack("ThreadMonitorHelper.traceStart : failed to find frame for caller of start\n", key);
return;
}
buffer.append(" from ");
printlnFrame(buffer, i);
trace(key, buffer.toString());
}
/**
* trace exit of the supplied thread to System.out
*
* this should only be triggered from the call to java.lang.Thread.exit"
*
* @param thread the exiting thread
*/
public void traceExit(Thread thread)
{
traceExit(thread, "out");
}
/**
* trace exit of the supplied thread to the trace stream identified by key
*
* this should only be triggered from the call to java.lang.Thread.exit"
*
* @param thread the exiting thread
* @param key an object identifying the trace stream to which output should be generated
*/
public void traceExit(Thread thread, Object key)
{
StringBuffer buffer = new StringBuffer();
int l = stack.length;
int t = triggerIndex();
if (triggerIndex() < 0) {
return;
}
int i = matchIndex("java.lang.Thread.exit", t, l);
if (i < 0) {
// illegal usage
traceStack("ThreadMonitorHelper.traceExit : should only be triggered below Thread.exit\n", key);
return;
}
buffer.append("*** Thread exit ");
buffer.append(thread.getName());
buffer.append(" ");
buffer.append(thread.getClass().getCanonicalName());
buffer.append('\n');
trace(key, buffer.toString());
}
/**
* trace run of the supplied Runnable to System.out
*
* this should only be triggered from a call to an implementation of java.lang.Runnable.run"
*
* @param runnable the runnable being run
*/
public void traceRun(Runnable runnable)
{
traceRun(runnable, "out");
}
/**
* trace start of the supplied thread to the trace stream identified by key
*
* this should only be triggered from the call an implementation of java.lang.Runnable.run"
*
* @param runnable the runnable being run
* @param key an object identifying the trace stream to which output should be generated
*/
public void traceRun(Runnable runnable, Object key)
{
StringBuffer buffer = new StringBuffer();
int l = stack.length;
int t = triggerIndex();
if (t < 0) {
return;
}
int i = matchIndex(".*run", t, l);
if (i < 0) {
// illegal usage
traceStack("ThreadMonitorHelper.traceRun : should only be triggered below Runnable.run\n", key);
return;
}
if (runnable instanceof Thread) {
Thread thread = (Thread) runnable;
buffer.append("*** Thread run ");
buffer.append(thread.getName());
buffer.append(" ");
} else {
buffer.append("*** Runnable run ");
buffer.append(runnable.toString());
buffer.append(" ");
}
buffer.append(runnable.getClass().getCanonicalName());
buffer.append('\n');
StackTraceElement[] stack = Thread.currentThread().getStackTrace();
buffer.append(" from ");
if (i < l - 1) {
printlnFrame(buffer, i + 1);
} else {
buffer.append(" VM runtime\n");
}
trace(key, buffer.toString());
}
}