-
Notifications
You must be signed in to change notification settings - Fork 3
/
ThreadOverviewQuery.java
949 lines (899 loc) · 39.2 KB
/
ThreadOverviewQuery.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
/*******************************************************************************
* Copyright (c) 2008, 2023 SAP AG, IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* SAP AG - initial API and implementation
* IBM Corporation - optional columns, cancellation
*******************************************************************************/
package org.eclipse.mat.inspections.threads;
import java.io.Serializable;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.logging.Logger;
import org.eclipse.mat.SnapshotException;
import org.eclipse.mat.collect.ArrayInt;
import org.eclipse.mat.collect.HashMapIntObject;
import org.eclipse.mat.collect.SetInt;
import org.eclipse.mat.inspections.InspectionAssert;
import org.eclipse.mat.internal.Messages;
import org.eclipse.mat.query.Bytes;
import org.eclipse.mat.query.Column;
import org.eclipse.mat.query.DetailResultProvider;
import org.eclipse.mat.query.IContextObject;
import org.eclipse.mat.query.IContextObjectSet;
import org.eclipse.mat.query.IDecorator;
import org.eclipse.mat.query.IIconProvider;
import org.eclipse.mat.query.IQuery;
import org.eclipse.mat.query.IResult;
import org.eclipse.mat.query.IResultTree;
import org.eclipse.mat.query.ResultMetaData;
import org.eclipse.mat.query.annotations.Argument;
import org.eclipse.mat.query.annotations.CommandName;
import org.eclipse.mat.query.annotations.HelpUrl;
import org.eclipse.mat.query.annotations.Icon;
import org.eclipse.mat.snapshot.ISnapshot;
import org.eclipse.mat.snapshot.OQL;
import org.eclipse.mat.snapshot.extension.Subject;
import org.eclipse.mat.snapshot.model.GCRootInfo;
import org.eclipse.mat.snapshot.model.GCRootInfo.Type;
import org.eclipse.mat.snapshot.model.IClass;
import org.eclipse.mat.snapshot.model.IObject;
import org.eclipse.mat.snapshot.model.IStackFrame;
import org.eclipse.mat.snapshot.model.IThreadStack;
import org.eclipse.mat.snapshot.model.NamedReference;
import org.eclipse.mat.snapshot.model.ThreadToLocalReference;
import org.eclipse.mat.snapshot.query.IHeapObjectArgument;
import org.eclipse.mat.snapshot.query.Icons;
import org.eclipse.mat.snapshot.query.ObjectListResult;
import org.eclipse.mat.snapshot.query.SnapshotQuery;
import org.eclipse.mat.util.IProgressListener;
import org.eclipse.mat.util.MessageUtil;
@CommandName("thread_overview")
@Icon("/META-INF/icons/threads.gif")
@HelpUrl("/org.eclipse.mat.ui.help/tasks/analyzingthreads.html")
@Subject("java.lang.Thread")
public class ThreadOverviewQuery implements IQuery
{
@Argument
public ISnapshot snapshot;
@Argument(isMandatory = false, flag = Argument.UNFLAGGED)
public IHeapObjectArgument objects;
private static URL THREAD_ICON_URL = Icons.getURL("thread.gif"); //$NON-NLS-1$
private static URL STACK_FRAME_ICON_URL = Icons.getURL("stack_frame.gif"); //$NON-NLS-1$
public static final String CLASS_THREAD = "java.lang.Thread"; //$NON-NLS-1$
public IResult execute(IProgressListener listener) throws Exception
{
listener.subTask(Messages.ThreadOverviewQuery_SearchingThreads);
List<ThreadOverviewNode> result = new ArrayList<ThreadOverviewNode>();
if (objects != null)
{
for (int[] objectIds : objects)
{
for (int objectId : objectIds)
{
if (listener.isCanceled())
break;
if (isThread(snapshot, objectId)) {
result.add(buildThreadOverviewNode(objectId, listener));
}
}
}
}
else
{
Collection<IClass> classes = snapshot.getClassesByName(CLASS_THREAD, true);
if (classes != null)
{
for (IClass clasz : classes)
{
for (int id : clasz.getObjectIds())
{
if (listener.isCanceled())
break;
result.add(buildThreadOverviewNode(id, listener));
}
}
}
// Add objects marked as a GCRoot THREAD_OBJ
for (int objectId : snapshot.getGCRoots())
{
GCRootInfo gcs[] = snapshot.getGCRootInfo(objectId);
if (gcs != null)
{
for (GCRootInfo gc : gcs)
{
// Sometime PHD files have threads marked as objects
if (gc.getType() == Type.THREAD_OBJ)
{
if (isThread(snapshot, objectId)) {
result.add(buildThreadOverviewNode(objectId, listener));
}
}
}
}
if (listener.isCanceled())
break;
}
}
Collections.sort(result, new Comparator<ThreadOverviewNode>()
{
public int compare(ThreadOverviewNode o1, ThreadOverviewNode o2)
{
return o1.threadInfo.getRetainedHeap().getValue() > o2.threadInfo.getRetainedHeap().getValue() ? -1
: o1.threadInfo.getRetainedHeap().getValue() < o2.threadInfo.getRetainedHeap().getValue() ? 1 :
Long.compare(o1.threadInfo.getThreadObject().getObjectAddress(), o2.threadInfo.getThreadObject().getObjectAddress());
}
});
// Remove duplicates - easy as already sorted
ThreadOverviewNode prev = null;
for (Iterator<ThreadOverviewNode>it = result.iterator(); it.hasNext();)
{
ThreadOverviewNode node = it.next();
if (prev != null && node.threadInfo.getThreadId() == prev.threadInfo.getThreadId())
{
it.remove();
}
else
{
prev = node;
}
}
if (result.isEmpty())
return null;
return new ThreadInfoList(snapshot, result);
}
public static boolean isThread(ISnapshot snapshot, int objectId) throws SnapshotException
{
IObject obj = snapshot.getObject(objectId);
IClass cls = obj.getClazz();
if (cls != null)
{
String className = cls.getName();
if (CLASS_THREAD.equals(className))
{
return true;
}
while (cls.hasSuperClass())
{
cls = cls.getSuperClass();
className = cls.getName();
if (CLASS_THREAD.equals(className))
{
return true;
}
}
GCRootInfo gcs[] = snapshot.getGCRootInfo(objectId);
if (gcs != null)
{
// Reset class name
className = obj.getClazz().getName();
for (GCRootInfo gc : gcs)
{
// Sometime PHD files have threads marked as objects
if (gc.getType() == Type.THREAD_OBJ && "java.lang.Object".equals(className)) //$NON-NLS-1$
return true;
}
}
}
return false;
}
private ThreadOverviewNode buildThreadOverviewNode(int objectId, IProgressListener listener) throws SnapshotException
{
ThreadOverviewNode result = new ThreadOverviewNode();
result.threadInfo = ThreadInfoImpl.build(snapshot.getObject(objectId), false, listener);
result.stack = snapshot.getThreadStack(objectId);
// add all GC roots referenced from this thread stack
if (result.stack != null)
{
IStackFrame[] frames = result.stack.getStackFrames();
if (frames != null)
{
SetInt roots = new SetInt();
for (IStackFrame IStackFrame : frames)
{
int[] objects = IStackFrame.getLocalObjectsIds();
if (objects != null)
{
for (int i : objects)
{
roots.add(i);
}
}
}
result.stackRoots = roots.toArray();
}
}
return result;
}
private static class ThreadOverviewNode
{
private ThreadInfoImpl threadInfo;
private IThreadStack stack;
private int[] stackRoots;
}
private static class ThreadStackFrameNode
{
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + depth;
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ThreadStackFrameNode other = (ThreadStackFrameNode) obj;
if (depth != other.depth)
return false;
return true;
}
private ThreadOverviewNode threadOverviewNode;
private IStackFrame stackFrame;
private int depth;
private boolean firstNonNativeFrame;
private int objectId;
private boolean extraColumnsCalculated;
private Bytes maxLocalRetainedHeapSize;
}
private static class ThreadStackFrameLocalNode
{
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + objectId;
result = prime * result + ((threadStackFrameNode == null) ? 0 : threadStackFrameNode.hashCode());
return result;
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ThreadStackFrameLocalNode other = (ThreadStackFrameLocalNode) obj;
if (objectId != other.objectId)
return false;
if (threadStackFrameNode == null)
{
if (other.threadStackFrameNode != null)
return false;
}
else if (!threadStackFrameNode.equals(other.threadStackFrameNode))
return false;
return true;
}
private ThreadStackFrameNode threadStackFrameNode;
private int objectId;
}
private static class ThreadInfoList implements IResultTree, IIconProvider, IDecorator
{
ISnapshot snapshot;
List<ThreadOverviewNode> infos;
Column[] columns;
HashMapIntObject<Object> root2element;
ObjectListResult.Outbound objectList;
int colMap[];
public ThreadInfoList(ISnapshot snapshot, List<ThreadOverviewNode> infos)
{
this.snapshot = snapshot;
this.infos = infos;
List<ThreadInfoImpl> threadInfos = new ArrayList<ThreadInfoImpl>(infos.size());
ArrayInt roots = new ArrayInt();
for (ThreadOverviewNode node : infos)
{
threadInfos.add(node.threadInfo);
if (node.stackRoots != null) {
roots.addAll(node.stackRoots);
}
}
this.columns = ThreadInfoImpl.getUsedColumns(threadInfos).toArray(new Column[0]);
this.columns[0].decorator(this).comparing(new NoCompareComparator());
root2element = new HashMapIntObject<Object>();
objectList = new ObjectListResult.Outbound(snapshot, roots.toArray());
for (Object o : objectList.getElements())
root2element.put(objectList.getContext(o).getObjectId(), o);
// Find matching columns in the object list
colMap = new int[columns.length];
Column[] objColumns = objectList.getColumns();
for (int i = 0; i < columns.length; ++i)
{
colMap[i] = i == 0 ? 0 : -1;
for (int j = 0; j < objColumns.length; ++j)
{
Column threadCol = columns[i];
Column listCol = objColumns[j];
// Don't worry about matching totals or sort direction.
if (threadCol.getLabel().equals(listCol.getLabel())
&& Objects.equals(threadCol.getFormatter(), listCol.getFormatter())
&& threadCol.getType().equals(listCol.getType()))
{
colMap[i] = j;
break;
}
}
}
}
public ResultMetaData getResultMetaData()
{
return new ResultMetaData.Builder() //
.addDetailResult(new DetailResultProvider(Messages.ThreadOverviewQuery_ThreadDetails)
{
@Override
public boolean hasResult(Object row)
{
if (!(row instanceof ThreadOverviewNode))
return false;
try
{
InspectionAssert.heapFormatIsNot(snapshot, "phd"); //$NON-NLS-1$
return true;
}
catch (UnsupportedOperationException e)
{
return false;
}
}
@Override
public URL getIcon()
{
try
{
return SnapshotQuery
.lookup("thread_details", snapshot).getDescriptor().getIcon(); //$NON-NLS-1$
}
catch (SnapshotException e)
{
return null;
}
}
@Override
public IResult getResult(Object row, IProgressListener listener)
throws SnapshotException
{
int threadId = ((ThreadOverviewNode) row).threadInfo.getThreadId();
return SnapshotQuery.lookup("thread_details", snapshot) //$NON-NLS-1$
.setArgument("threadIds", threadId) //$NON-NLS-1$
.execute(listener);
}
}) //
.addDetailResult(new DetailResultProvider(Messages.ThreadOverviewQuery_ThreadStack)
{
@Override
public boolean hasResult(Object row)
{
if (!(row instanceof ThreadOverviewNode))
return false;
try
{
InspectionAssert.heapFormatIsNot(snapshot, "phd"); //$NON-NLS-1$
return true;
}
catch (UnsupportedOperationException e)
{
return false;
}
}
@Override
public URL getIcon()
{
try
{
return SnapshotQuery
.lookup("thread_stack", snapshot).getDescriptor().getIcon(); //$NON-NLS-1$
}
catch (SnapshotException e)
{
return null;
}
}
@Override
public IResult getResult(Object row, IProgressListener listener)
throws SnapshotException
{
int threadId = ((ThreadOverviewNode) row).threadInfo.getThreadId();
return SnapshotQuery.lookup("thread_stack", snapshot) //$NON-NLS-1$
.setArgument("threadIds", threadId) //$NON-NLS-1$
.execute(listener);
}
}) //
.build();
}
public Column[] getColumns()
{
return columns;
}
public Object getColumnValue(Object row, int columnIndex)
{
if (row instanceof ThreadOverviewNode) {
ThreadOverviewNode info = (ThreadOverviewNode) row;
return info.threadInfo.getValue(columns[columnIndex]);
} else {
if (row instanceof ThreadStackFrameNode)
{
ThreadStackFrameNode info = (ThreadStackFrameNode) row;
Column column = columns[columnIndex];
try
{
if (ThreadInfoImpl.COL_INSTANCE.equals(column))
{
IStackFrame frame = info.stackFrame;
return frame.getText();
}
else if (ThreadInfoImpl.COL_MAXLOCALRETAINED.equals(column))
{
calculateStackFrameNodeColumnsIfNeeded(info);
return info.maxLocalRetainedHeapSize;
}
}
catch (SnapshotException se)
{
Logger.getLogger(getClass().getCanonicalName()).warning(MessageUtil.format(
Messages.ThreadOverviewQuery_StackFrameLocalIssue, se.getLocalizedMessage()));
}
}
else
{
int newColumnIndex = colMap[columnIndex];
if (newColumnIndex >= 0)
{
if (row instanceof ThreadStackFrameLocalNode)
{
row = root2element.get(((ThreadStackFrameLocalNode)row).objectId);
}
return objectList.getColumnValue(row, newColumnIndex);
}
}
return null;
}
}
private void calculateStackFrameNodeColumnsIfNeeded(ThreadStackFrameNode info) throws SnapshotException
{
if (!info.extraColumnsCalculated)
{
try
{
@SuppressWarnings("unchecked")
List<ThreadStackFrameLocalNode> locals = (List<ThreadStackFrameLocalNode>) getChildren(info);
if (locals != null && locals.size() > 0)
{
long maxLocalRetainedHeapSize = 0, localRetainedHeapSize;
for (ThreadStackFrameLocalNode local : locals)
{
localRetainedHeapSize = snapshot.getRetainedHeapSize(local.objectId);
if (localRetainedHeapSize > maxLocalRetainedHeapSize)
{
maxLocalRetainedHeapSize = localRetainedHeapSize;
}
}
info.maxLocalRetainedHeapSize = new Bytes(maxLocalRetainedHeapSize);
}
}
finally
{
info.extraColumnsCalculated = true;
}
}
}
public IContextObject getContext(final Object row)
{
if (row instanceof ThreadOverviewNode) {
return new IContextObject()
{
public int getObjectId()
{
return ((ThreadOverviewNode) row).threadInfo.getThreadId();
}
};
}
if (row instanceof ThreadStackFrameNode)
{
if (((ThreadStackFrameNode)row).objectId != -1)
{
return new IContextObjectSet()
{
@Override
public int getObjectId()
{
return ((ThreadStackFrameNode) row).objectId;
}
@Override
public int[] getObjectIds()
{
IStackFrame frame = ((ThreadStackFrameNode) row).stackFrame;
int[] objectIds = frame.getLocalObjectsIds();
SetInt si = new SetInt();
for (int i : objectIds)
{
// Ignore stack frame psuedo-objects
if (!(i == getObjectId() && !snapshot.isClass(i)))
si.add(i);
}
return si.toArray();
}
@Override
public String getOQL()
{
return OQL.forObjectIds(getObjectIds());
}
};
}
return null;
}
if (row instanceof ThreadStackFrameLocalNode)
{
return objectList.getContext(root2element.get(((ThreadStackFrameLocalNode)row).objectId));
}
else
{
return objectList.getContext(row);
}
}
public URL getIcon(Object row)
{
if (row instanceof ThreadOverviewNode)
{
return THREAD_ICON_URL;
}
else if (row instanceof ThreadStackFrameNode)
{
return STACK_FRAME_ICON_URL;
}
else
{
if (row instanceof ThreadStackFrameLocalNode)
{
row = root2element.get(((ThreadStackFrameLocalNode)row).objectId);
}
return objectList.getIcon(row);
}
}
public String prefix(Object row)
{
if (row instanceof ThreadOverviewNode)
return null;
else if (row instanceof ThreadStackFrameNode)
return null;
else
{
ThreadStackFrameLocalNode tsfmln = null;
if (row instanceof ThreadStackFrameLocalNode)
{
tsfmln = (ThreadStackFrameLocalNode) row;
row = root2element.get(tsfmln.objectId);
}
String prefix = objectList.prefix(row);
if (prefix == null)
{
// If this is the top stack frame and this object is a GC
// Root, then check if it's of the type BUSY_MONITOR with
// the context ID of this thread, and if so, note that this
// is the object the thread is blocked on
if (tsfmln != null && tsfmln.threadStackFrameNode != null)
{
boolean topFrame = tsfmln.threadStackFrameNode.firstNonNativeFrame || tsfmln.threadStackFrameNode.depth == 0;
ThreadInfoImpl threadInfo = tsfmln.threadStackFrameNode.threadOverviewNode.threadInfo;
int objectId = objectList.getContext(row).getObjectId();
try
{
GCRootInfo[] gcRootInfos = snapshot.getGCRootInfo(objectId);
if (gcRootInfos != null)
{
for (GCRootInfo gcRootInfo : gcRootInfos)
{
if (gcRootInfo.getType() == GCRootInfo.Type.BUSY_MONITOR
&& gcRootInfo.getContextAddress() != 0
&& gcRootInfo.getContextId() == threadInfo.getThreadId())
{
return topFrame ? Messages.ThreadStackQuery_Label_Local_Probably_Blocked_On
: Messages.ThreadStackQuery_Label_Local_Busy_Monitor;
}
if (gcRootInfo.getType() == GCRootInfo.Type.BUSY_MONITOR
&& gcRootInfo.getContextAddress() == 0)
{
// HPROF doesn't have a context address
return topFrame ? Messages.ThreadStackQuery_Label_Local_Possibly_Blocked_On
: Messages.ThreadStackQuery_Label_Local_Possible_Busy_Monitor;
}
}
}
/*
* Handle references from the thread
* or the stack frame pseudo-object.
*/
int refererId = tsfmln.threadStackFrameNode.objectId;
IObject refererObj;
if (refererId != -1 && !snapshot.isClass(refererId))
// Actual stack frame pseudo object
refererObj = snapshot.getObject(refererId);
else
refererObj = threadInfo.getThreadObject();
List<NamedReference> refs = refererObj.getOutboundReferences();
for (NamedReference ref : refs)
{
if (ref.getObjectId() != objectId)
continue;
if (ref instanceof ThreadToLocalReference)
{
ThreadToLocalReference tlr = (ThreadToLocalReference)ref;
for (GCRootInfo gcRootInfo : tlr.getGcRootInfo())
{
if (gcRootInfo.getType() == GCRootInfo.Type.BUSY_MONITOR)
{
return topFrame ? Messages.ThreadStackQuery_Label_Local_Probably_Blocked_On
: Messages.ThreadStackQuery_Label_Local_Busy_Monitor;
}
}
return tlr.getName();
}
}
}
catch (SnapshotException e)
{
// Something is wrong with the GC root information,
// so just skip this
}
}
return Messages.ThreadStackQuery_Label_Local;
}
else
{
return prefix;
}
}
}
public String suffix(Object row)
{
if (row instanceof ThreadOverviewNode)
return null;
else if (row instanceof ThreadStackFrameNode)
return null;
else
{
if (row instanceof ThreadStackFrameLocalNode)
{
row = root2element.get(((ThreadStackFrameLocalNode)row).objectId);
}
return objectList.suffix(row);
}
}
public List<?> getElements()
{
return infos;
}
public boolean hasChildren(Object element)
{
if (element instanceof ThreadOverviewNode)
{
IThreadStack stack = ((ThreadOverviewNode) element).stack;
if (stack == null) return false;
IStackFrame[] frames = stack.getStackFrames();
return frames != null && frames.length > 0;
}
else if (element instanceof ThreadStackFrameNode)
{
IStackFrame frame = ((ThreadStackFrameNode) element).stackFrame;
int[] objectIds = frame.getLocalObjectsIds();
return objectIds != null && objectIds.length > 0;
}
else
{
if (element instanceof ThreadStackFrameLocalNode)
{
element = root2element.get(((ThreadStackFrameLocalNode)element).objectId);
}
return objectList.hasChildren(element);
}
}
public List<?> getChildren(Object parent)
{
if (parent instanceof ThreadOverviewNode)
{
ThreadOverviewNode ton = (ThreadOverviewNode) parent;
if (ton.stack == null)
return Collections.emptyList();
IStackFrame[] frames = ton.stack.getStackFrames();
final int numFrames = frames.length;
List<ThreadStackFrameNode> stackFrameNodes = new ArrayList<ThreadStackFrameNode>(numFrames);
boolean foundNonNativeFrame = false;
int frameIds[] = frameIds(ton);
for (int i = 0; i < numFrames; i++)
{
IStackFrame frame = frames[i];
ThreadStackFrameNode tsfn = new ThreadStackFrameNode();
tsfn.stackFrame = frame;
tsfn.depth = i;
tsfn.firstNonNativeFrame = false;
if (!foundNonNativeFrame)
{
String frameText = frame.getText();
if (frameText != null && !frameText.contains("Native Method")) //$NON-NLS-1$
{
tsfn.firstNonNativeFrame = true;
foundNonNativeFrame = true;
}
}
tsfn.threadOverviewNode = ton;
tsfn.objectId = frameIds[tsfn.depth];
if (tsfn.objectId == -1)
{
// Add the class from the method name or class name of the stack frame
tsfn.objectId = frameId(frame);
}
stackFrameNodes.add(tsfn);
}
return stackFrameNodes;
}
else if (parent instanceof ThreadStackFrameNode)
{
ThreadStackFrameNode tsfn = (ThreadStackFrameNode) parent;
IStackFrame frame = tsfn.stackFrame;
int[] localIds = frame.getLocalObjectsIds();
if (localIds != null)
{
List<ThreadStackFrameLocalNode> stackFrameLocals = new ArrayList<ThreadStackFrameLocalNode>(localIds.length);
for (int localId : localIds)
{
// Is the local actually just the stack frame object (and not the class)?
if (tsfn.objectId == localId && !snapshot.isClass(localId))
continue;
ThreadStackFrameLocalNode tsfln = new ThreadStackFrameLocalNode();
tsfln.objectId = localId;
tsfln.threadStackFrameNode = tsfn;
stackFrameLocals.add(tsfln);
}
return stackFrameLocals;
}
else
{
return null;
}
}
else
{
if (parent instanceof ThreadStackFrameLocalNode)
{
parent = root2element.get(((ThreadStackFrameLocalNode)parent).objectId);
}
return objectList.getChildren(parent);
}
}
private int[] frameIds(ThreadOverviewNode ton)
{
final int numFrames = ton.stack.getStackFrames().length;
int frameIds[] = new int[numFrames];
Arrays.fill(frameIds, -1);
IObject to = ton.threadInfo.getThreadObject();
for (NamedReference ref : to.getOutboundReferences())
{
if (ref instanceof ThreadToLocalReference)
{
ThreadToLocalReference tlr = (ThreadToLocalReference)ref;
for (GCRootInfo gcRootInfo : tlr.getGcRootInfo())
{
if (gcRootInfo.getType() == GCRootInfo.Type.JAVA_STACK_FRAME)
{
try
{
Object fn = tlr.getObject().resolveValue("frameNumber"); //$NON-NLS-1$
if (fn instanceof Integer)
{
int f = (Integer)fn;
if (f >= 0 && f < numFrames)
{
frameIds[f] = tlr.getObjectId();
break;
}
}
/*
* See if a reference from the thread is to
* the pseudo-object stack frame
*/
for (int f = 0; f < ton.stack.getStackFrames().length; ++f)
{
IStackFrame fm = ton.stack.getStackFrames()[f];
for (int o : fm.getLocalObjectsIds())
{
if (o == tlr.getObjectId())
{
frameIds[f] = o;
break;
}
}
}
}
catch (SnapshotException e)
{
// Ignore
}
}
}
}
}
return frameIds;
}
/**
* Return the id of the class associated with this stack frame.
* @param frm the stack frame
* @return the id, or -1 if not found
*/
private int frameId(IStackFrame frm)
{
String fm = frm.getText();
if (fm != null)
{
// Extract the full method name.
// Example text:
// at package.name.Classname.method(Arguments)LReturnCls; Classname.java(13)
String fm1 = fm.replaceFirst("\\s*at\\s+([^ ]+).*", "$1"); //$NON-NLS-1$//$NON-NLS-2$
try
{
Collection<IClass>clss = snapshot.getClassesByName(fm1, false);
// If unambiguous which class
if (clss != null && clss.size() == 1)
{
for (IClass cls : clss)
{
return cls.getObjectId();
}
}
}
catch (SnapshotException e)
{
}
// Extract the class name from the full method name
// at package.name.Classname.method(Arguments)LReturnCls; Classname.java(13)
fm1 = fm.replaceFirst("\\s*at\\s+([^(]+)\\.[^.(]+.*", "$1"); //$NON-NLS-1$//$NON-NLS-2$
try
{
Collection<IClass>clss = snapshot.getClassesByName(fm1, false);
// If unambiguous which class
if (clss != null && clss.size() == 1)
{
for (IClass cls : clss)
{
return cls.getObjectId();
}
}
}
catch (SnapshotException e)
{
}
}
return -1;
}
static class NoCompareComparator implements Comparator<Object>, Serializable
{
private static final long serialVersionUID = 1L;
public int compare(Object o1, Object o2)
{
return 0;
}
}
}
}