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
|
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.ddmuilib;
import com.android.ddmlib.AndroidDebugBridge.IClientChangeListener;
import com.android.ddmlib.Client;
import com.android.ddmlib.ThreadInfo;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Sash;
import org.eclipse.swt.widgets.Table;
import java.util.Date;
/**
* Base class for our information panels.
*/
public class ThreadPanel extends TablePanel {
private final static String PREFS_THREAD_COL_ID = "threadPanel.Col0"; //$NON-NLS-1$
private final static String PREFS_THREAD_COL_TID = "threadPanel.Col1"; //$NON-NLS-1$
private final static String PREFS_THREAD_COL_STATUS = "threadPanel.Col2"; //$NON-NLS-1$
private final static String PREFS_THREAD_COL_UTIME = "threadPanel.Col3"; //$NON-NLS-1$
private final static String PREFS_THREAD_COL_STIME = "threadPanel.Col4"; //$NON-NLS-1$
private final static String PREFS_THREAD_COL_NAME = "threadPanel.Col5"; //$NON-NLS-1$
private final static String PREFS_THREAD_SASH = "threadPanel.sash"; //$NON-NLS-1$
private static final String PREFS_STACK_COL_CLASS = "threadPanel.stack.col0"; //$NON-NLS-1$
private static final String PREFS_STACK_COL_METHOD = "threadPanel.stack.col1"; //$NON-NLS-1$
private static final String PREFS_STACK_COL_FILE = "threadPanel.stack.col2"; //$NON-NLS-1$
private static final String PREFS_STACK_COL_LINE = "threadPanel.stack.col3"; //$NON-NLS-1$
private static final String PREFS_STACK_COL_NATIVE = "threadPanel.stack.col4"; //$NON-NLS-1$
private Display mDisplay;
private Composite mBase;
private Label mNotEnabled;
private Label mNotSelected;
private Composite mThreadBase;
private Table mThreadTable;
private TableViewer mThreadViewer;
private Composite mStackTraceBase;
private Button mRefreshStackTraceButton;
private Label mStackTraceTimeLabel;
private StackTracePanel mStackTracePanel;
private Table mStackTraceTable;
/** Indicates if a timer-based Runnable is current requesting thread updates regularly. */
private boolean mMustStopRecurringThreadUpdate = false;
/** Flag to tell the recurring thread update to stop running */
private boolean mRecurringThreadUpdateRunning = false;
private Object mLock = new Object();
private static final String[] THREAD_STATUS = {
"zombie", "running", "timed-wait", "monitor",
"wait", "init", "start", "native", "vmwait",
"suspended"
};
/**
* Content Provider to display the threads of a client.
* Expected input is a {@link Client} object.
*/
private static class ThreadContentProvider implements IStructuredContentProvider {
@Override
public Object[] getElements(Object inputElement) {
if (inputElement instanceof Client) {
return ((Client)inputElement).getClientData().getThreads();
}
return new Object[0];
}
@Override
public void dispose() {
// pass
}
@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
// pass
}
}
/**
* A Label Provider to use with {@link ThreadContentProvider}. It expects the elements to be
* of type {@link ThreadInfo}.
*/
private static class ThreadLabelProvider implements ITableLabelProvider {
@Override
public Image getColumnImage(Object element, int columnIndex) {
return null;
}
@Override
public String getColumnText(Object element, int columnIndex) {
if (element instanceof ThreadInfo) {
ThreadInfo thread = (ThreadInfo)element;
switch (columnIndex) {
case 0:
return (thread.isDaemon() ? "*" : "") + //$NON-NLS-1$ //$NON-NLS-2$
String.valueOf(thread.getThreadId());
case 1:
return String.valueOf(thread.getTid());
case 2:
if (thread.getStatus() >= 0 && thread.getStatus() < THREAD_STATUS.length)
return THREAD_STATUS[thread.getStatus()];
return "unknown";
case 3:
return String.valueOf(thread.getUtime());
case 4:
return String.valueOf(thread.getStime());
case 5:
return thread.getThreadName();
}
}
return null;
}
@Override
public void addListener(ILabelProviderListener listener) {
// pass
}
@Override
public void dispose() {
// pass
}
@Override
public boolean isLabelProperty(Object element, String property) {
// pass
return false;
}
@Override
public void removeListener(ILabelProviderListener listener) {
// pass
}
}
/**
* Create our control(s).
*/
@Override
protected Control createControl(Composite parent) {
mDisplay = parent.getDisplay();
final IPreferenceStore store = DdmUiPreferences.getStore();
mBase = new Composite(parent, SWT.NONE);
mBase.setLayout(new StackLayout());
// UI for thread not enabled
mNotEnabled = new Label(mBase, SWT.CENTER | SWT.WRAP);
mNotEnabled.setText("Thread updates not enabled for selected client\n"
+ "(use toolbar button to enable)");
// UI for not client selected
mNotSelected = new Label(mBase, SWT.CENTER | SWT.WRAP);
mNotSelected.setText("no client is selected");
// base composite for selected client with enabled thread update.
mThreadBase = new Composite(mBase, SWT.NONE);
mThreadBase.setLayout(new FormLayout());
// table above the sash
mThreadTable = new Table(mThreadBase, SWT.MULTI | SWT.FULL_SELECTION);
mThreadTable.setHeaderVisible(true);
mThreadTable.setLinesVisible(true);
TableHelper.createTableColumn(
mThreadTable,
"ID",
SWT.RIGHT,
"888", //$NON-NLS-1$
PREFS_THREAD_COL_ID, store);
TableHelper.createTableColumn(
mThreadTable,
"Tid",
SWT.RIGHT,
"88888", //$NON-NLS-1$
PREFS_THREAD_COL_TID, store);
TableHelper.createTableColumn(
mThreadTable,
"Status",
SWT.LEFT,
"timed-wait", //$NON-NLS-1$
PREFS_THREAD_COL_STATUS, store);
TableHelper.createTableColumn(
mThreadTable,
"utime",
SWT.RIGHT,
"utime", //$NON-NLS-1$
PREFS_THREAD_COL_UTIME, store);
TableHelper.createTableColumn(
mThreadTable,
"stime",
SWT.RIGHT,
"utime", //$NON-NLS-1$
PREFS_THREAD_COL_STIME, store);
TableHelper.createTableColumn(
mThreadTable,
"Name",
SWT.LEFT,
"android.class.ReallyLongClassName.MethodName", //$NON-NLS-1$
PREFS_THREAD_COL_NAME, store);
mThreadViewer = new TableViewer(mThreadTable);
mThreadViewer.setContentProvider(new ThreadContentProvider());
mThreadViewer.setLabelProvider(new ThreadLabelProvider());
mThreadViewer.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
ThreadInfo selectedThread = getThreadSelection(event.getSelection());
updateThreadStackTrace(selectedThread);
}
});
mThreadViewer.addDoubleClickListener(new IDoubleClickListener() {
@Override
public void doubleClick(DoubleClickEvent event) {
ThreadInfo selectedThread = getThreadSelection(event.getSelection());
if (selectedThread != null) {
Client client = (Client)mThreadViewer.getInput();
if (client != null) {
client.requestThreadStackTrace(selectedThread.getThreadId());
}
}
}
});
// the separating sash
final Sash sash = new Sash(mThreadBase, SWT.HORIZONTAL);
Color darkGray = parent.getDisplay().getSystemColor(SWT.COLOR_DARK_GRAY);
sash.setBackground(darkGray);
// the UI below the sash
mStackTraceBase = new Composite(mThreadBase, SWT.NONE);
mStackTraceBase.setLayout(new GridLayout(2, false));
mRefreshStackTraceButton = new Button(mStackTraceBase, SWT.PUSH);
mRefreshStackTraceButton.setText("Refresh");
mRefreshStackTraceButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
ThreadInfo selectedThread = getThreadSelection(null);
if (selectedThread != null) {
Client currentClient = getCurrentClient();
if (currentClient != null) {
currentClient.requestThreadStackTrace(selectedThread.getThreadId());
}
}
}
});
mStackTraceTimeLabel = new Label(mStackTraceBase, SWT.NONE);
mStackTraceTimeLabel.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
mStackTracePanel = new StackTracePanel();
mStackTraceTable = mStackTracePanel.createPanel(mStackTraceBase,
PREFS_STACK_COL_CLASS,
PREFS_STACK_COL_METHOD,
PREFS_STACK_COL_FILE,
PREFS_STACK_COL_LINE,
PREFS_STACK_COL_NATIVE,
store);
GridData gd;
mStackTraceTable.setLayoutData(gd = new GridData(GridData.FILL_BOTH));
gd.horizontalSpan = 2;
// now setup the sash.
// form layout data
FormData data = new FormData();
data.top = new FormAttachment(0, 0);
data.bottom = new FormAttachment(sash, 0);
data.left = new FormAttachment(0, 0);
data.right = new FormAttachment(100, 0);
mThreadTable.setLayoutData(data);
final FormData sashData = new FormData();
if (store != null && store.contains(PREFS_THREAD_SASH)) {
sashData.top = new FormAttachment(0, store.getInt(PREFS_THREAD_SASH));
} else {
sashData.top = new FormAttachment(50,0); // 50% across
}
sashData.left = new FormAttachment(0, 0);
sashData.right = new FormAttachment(100, 0);
sash.setLayoutData(sashData);
data = new FormData();
data.top = new FormAttachment(sash, 0);
data.bottom = new FormAttachment(100, 0);
data.left = new FormAttachment(0, 0);
data.right = new FormAttachment(100, 0);
mStackTraceBase.setLayoutData(data);
// allow resizes, but cap at minPanelWidth
sash.addListener(SWT.Selection, new Listener() {
@Override
public void handleEvent(Event e) {
Rectangle sashRect = sash.getBounds();
Rectangle panelRect = mThreadBase.getClientArea();
int bottom = panelRect.height - sashRect.height - 100;
e.y = Math.max(Math.min(e.y, bottom), 100);
if (e.y != sashRect.y) {
sashData.top = new FormAttachment(0, e.y);
store.setValue(PREFS_THREAD_SASH, e.y);
mThreadBase.layout();
}
}
});
((StackLayout)mBase.getLayout()).topControl = mNotSelected;
return mBase;
}
/**
* Sets the focus to the proper control inside the panel.
*/
@Override
public void setFocus() {
mThreadTable.setFocus();
}
/**
* Sent when an existing client information changed.
* <p/>
* This is sent from a non UI thread.
* @param client the updated client.
* @param changeMask the bit mask describing the changed properties. It can contain
* any of the following values: {@link Client#CHANGE_INFO}, {@link Client#CHANGE_NAME}
* {@link Client#CHANGE_DEBUGGER_STATUS}, {@link Client#CHANGE_THREAD_MODE},
* {@link Client#CHANGE_THREAD_DATA}, {@link Client#CHANGE_HEAP_MODE},
* {@link Client#CHANGE_HEAP_DATA}, {@link Client#CHANGE_NATIVE_HEAP_DATA}
*
* @see IClientChangeListener#clientChanged(Client, int)
*/
@Override
public void clientChanged(final Client client, int changeMask) {
if (client == getCurrentClient()) {
if ((changeMask & Client.CHANGE_THREAD_MODE) != 0 ||
(changeMask & Client.CHANGE_THREAD_DATA) != 0) {
try {
mThreadTable.getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
clientSelected();
}
});
} catch (SWTException e) {
// widget is disposed, we do nothing
}
} else if ((changeMask & Client.CHANGE_THREAD_STACKTRACE) != 0) {
try {
mThreadTable.getDisplay().asyncExec(new Runnable() {
@Override
public void run() {
updateThreadStackCall();
}
});
} catch (SWTException e) {
// widget is disposed, we do nothing
}
}
}
}
/**
* Sent when a new device is selected. The new device can be accessed
* with {@link #getCurrentDevice()}.
*/
@Override
public void deviceSelected() {
// pass
}
/**
* Sent when a new client is selected. The new client can be accessed
* with {@link #getCurrentClient()}.
*/
@Override
public void clientSelected() {
if (mThreadTable.isDisposed()) {
return;
}
Client client = getCurrentClient();
mStackTracePanel.setCurrentClient(client);
if (client != null) {
if (!client.isThreadUpdateEnabled()) {
((StackLayout)mBase.getLayout()).topControl = mNotEnabled;
mThreadViewer.setInput(null);
// if we are currently updating the thread, stop doing it.
mMustStopRecurringThreadUpdate = true;
} else {
((StackLayout)mBase.getLayout()).topControl = mThreadBase;
mThreadViewer.setInput(client);
synchronized (mLock) {
// if we're not updating we start the process
if (mRecurringThreadUpdateRunning == false) {
startRecurringThreadUpdate();
} else if (mMustStopRecurringThreadUpdate) {
// else if there's a runnable that's still going to get called, lets
// simply cancel the stop, and keep going
mMustStopRecurringThreadUpdate = false;
}
}
}
} else {
((StackLayout)mBase.getLayout()).topControl = mNotSelected;
mThreadViewer.setInput(null);
}
mBase.layout();
}
/**
* Updates the stack call of the currently selected thread.
* <p/>
* This <b>must</b> be called from the UI thread.
*/
private void updateThreadStackCall() {
Client client = getCurrentClient();
if (client != null) {
// get the current selection in the ThreadTable
ThreadInfo selectedThread = getThreadSelection(null);
if (selectedThread != null) {
updateThreadStackTrace(selectedThread);
} else {
updateThreadStackTrace(null);
}
}
}
/**
* updates the stackcall of the specified thread. If <code>null</code> the UI is emptied
* of current data.
* @param thread
*/
private void updateThreadStackTrace(ThreadInfo thread) {
mStackTracePanel.setViewerInput(thread);
if (thread != null) {
mRefreshStackTraceButton.setEnabled(true);
long stackcallTime = thread.getStackCallTime();
if (stackcallTime != 0) {
String label = new Date(stackcallTime).toString();
mStackTraceTimeLabel.setText(label);
} else {
mStackTraceTimeLabel.setText(""); //$NON-NLS-1$
}
} else {
mRefreshStackTraceButton.setEnabled(true);
mStackTraceTimeLabel.setText(""); //$NON-NLS-1$
}
}
@Override
protected void setTableFocusListener() {
addTableToFocusListener(mThreadTable);
addTableToFocusListener(mStackTraceTable);
}
/**
* Initiate recurring events. We use a shorter "initialWait" so we do the
* first execution sooner. We don't do it immediately because we want to
* give the clients a chance to get set up.
*/
private void startRecurringThreadUpdate() {
mRecurringThreadUpdateRunning = true;
int initialWait = 1000;
mDisplay.timerExec(initialWait, new Runnable() {
@Override
public void run() {
synchronized (mLock) {
// lets check we still want updates.
if (mMustStopRecurringThreadUpdate == false) {
Client client = getCurrentClient();
if (client != null) {
client.requestThreadUpdate();
mDisplay.timerExec(
DdmUiPreferences.getThreadRefreshInterval() * 1000, this);
} else {
// we don't have a Client, which means the runnable is not
// going to be called through the timer. We reset the running flag.
mRecurringThreadUpdateRunning = false;
}
} else {
// else actually stops (don't call the timerExec) and reset the flags.
mRecurringThreadUpdateRunning = false;
mMustStopRecurringThreadUpdate = false;
}
}
}
});
}
/**
* Returns the current thread selection or <code>null</code> if none is found.
* If a {@link ISelection} object is specified, the first {@link ThreadInfo} from this selection
* is returned, otherwise, the <code>ISelection</code> returned by
* {@link TableViewer#getSelection()} is used.
* @param selection the {@link ISelection} to use, or <code>null</code>
*/
private ThreadInfo getThreadSelection(ISelection selection) {
if (selection == null) {
selection = mThreadViewer.getSelection();
}
if (selection instanceof IStructuredSelection) {
IStructuredSelection structuredSelection = (IStructuredSelection)selection;
Object object = structuredSelection.getFirstElement();
if (object instanceof ThreadInfo) {
return (ThreadInfo)object;
}
}
return null;
}
}
|