Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b6d3b87aeefa48f08b7668c1906970bbf93c817b (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2013, 2014 Mentor Graphics and others.
 *
 * 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:
 *     Dmitry Kozlov (Mentor Graphics) - Trace control view enhancements (Bug 390827)
 *******************************************************************************/
package org.eclipse.cdt.dsf.gdb.internal.ui.tracepoints;

import java.util.Hashtable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.eclipse.cdt.dsf.concurrent.DataRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.DsfRunnable;
import org.eclipse.cdt.dsf.concurrent.IDsfStatusConstants;
import org.eclipse.cdt.dsf.concurrent.ImmediateDataRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.ImmediateRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.Query;
import org.eclipse.cdt.dsf.concurrent.RequestMonitor;
import org.eclipse.cdt.dsf.datamodel.DMContexts;
import org.eclipse.cdt.dsf.datamodel.IDMContext;
import org.eclipse.cdt.dsf.debug.service.IRunControl.ISuspendedDMEvent;
import org.eclipse.cdt.dsf.gdb.internal.ui.GdbUIPlugin;
import org.eclipse.cdt.dsf.gdb.internal.ui.tracepoints.TraceControlView.FailedTraceVariableCreationException;
import org.eclipse.cdt.dsf.gdb.service.GDBTraceControl_7_2.TraceRecordSelectedChangedEvent;
import org.eclipse.cdt.dsf.gdb.service.IGDBTraceControl;
import org.eclipse.cdt.dsf.gdb.service.IGDBTraceControl.ITraceRecordDMContext;
import org.eclipse.cdt.dsf.gdb.service.IGDBTraceControl.ITraceRecordSelectedChangedDMEvent;
import org.eclipse.cdt.dsf.gdb.service.IGDBTraceControl.ITraceStatusDMData;
import org.eclipse.cdt.dsf.gdb.service.IGDBTraceControl.ITraceStatusDMData2;
import org.eclipse.cdt.dsf.gdb.service.IGDBTraceControl.ITraceTargetDMContext;
import org.eclipse.cdt.dsf.gdb.service.IGDBTraceControl.ITraceVariableDMData;
import org.eclipse.cdt.dsf.gdb.service.IGDBTraceControl.ITracingStartedDMEvent;
import org.eclipse.cdt.dsf.gdb.service.IGDBTraceControl.ITracingStoppedDMEvent;
import org.eclipse.cdt.dsf.gdb.service.IGDBTraceControl.ITracingSupportedChangeDMEvent;
import org.eclipse.cdt.dsf.gdb.service.IGDBTraceControl2;
import org.eclipse.cdt.dsf.service.DsfServiceEventHandler;
import org.eclipse.cdt.dsf.service.DsfServicesTracker;
import org.eclipse.cdt.dsf.service.DsfSession;
import org.eclipse.cdt.dsf.ui.viewmodel.datamodel.IDMVMContext;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.ui.DebugUITools;
import org.eclipse.debug.ui.contexts.DebugContextEvent;
import org.eclipse.debug.ui.contexts.IDebugContextListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchWindow;

/**
 * This class is a bridge between the TraceControl view and the TraceControl service.
 * It performs the necessary requests to the service on behalf of the view.
 * Those request must be done on the DSF Executor thread.
 * Note that this class will have a single instance which will deal with
 * all DSF debug sessions at the same time.
 */
public class TraceControlModel {

	private String fDebugSessionId;
	private DsfServicesTracker fServicesTracker;
	private volatile IGDBTraceControl fGDBTraceControl;
	private volatile ITraceTargetDMContext fTargetContext;
	private TraceControlView fTraceControlView;

	private IDebugContextListener fDebugContextListener = new IDebugContextListener() {
		@Override
		public void debugContextChanged(DebugContextEvent event) {
			if ((event.getFlags() & DebugContextEvent.ACTIVATED) != 0) {
				updateDebugContext();
			}
		}
	};

	TraceControlModel(TraceControlView view) {
		fTraceControlView = view;

		IWorkbenchWindow window = fTraceControlView.getSite().getWorkbenchWindow();
		DebugUITools.getDebugContextManager().getContextService(window).addDebugContextListener(fDebugContextListener);
		updateDebugContext();
	}

	protected void updateContent() {
		if (getSession() == null) {
			notifyUI(TracepointsMessages.TraceControlView_trace_status_no_debug_session);
			return;
		}

		if (fTargetContext == null || fGDBTraceControl == null) {
			notifyUI(TracepointsMessages.TraceControlView_trace_status_not_supported);
			return;
		}

		getSession().getExecutor().execute(new DsfRunnable() {
			@Override
			public void run() {
				if (fTargetContext != null && fGDBTraceControl != null) {
					fGDBTraceControl.getTraceStatus(fTargetContext,
							new DataRequestMonitor<ITraceStatusDMData>(getSession().getExecutor(), null) {
								@Override
								protected void handleCompleted() {
									if (isSuccess() && getData() != null) {
										notifyUI((ITraceStatusDMData2) getData());
									} else {
										notifyUI((ITraceStatusDMData2) null);
									}
								}
							});
				} else {
					notifyUI((ITraceStatusDMData2) null);
				}
			}
		});
	}

	public void init() {
		if (fDebugSessionId != null) {
			debugSessionChanged();
		} else {
			updateDebugContext();
		}
	}

	public void dispose() {
		IWorkbenchWindow window = fTraceControlView.getSite().getWorkbenchWindow();
		DebugUITools.getDebugContextManager().getContextService(window)
				.removeDebugContextListener(fDebugContextListener);
		setDebugContext(null);
	}

	protected void updateDebugContext() {
		IAdaptable debugContext = DebugUITools.getDebugContext();
		if (debugContext instanceof IDMVMContext) {
			setDebugContext((IDMVMContext) debugContext);
		} else {
			setDebugContext(null);
		}
	}

	protected void setDebugContext(IDMVMContext vmContext) {
		if (vmContext != null) {
			IDMContext dmContext = vmContext.getDMContext();
			String sessionId = dmContext.getSessionId();
			fTargetContext = DMContexts.getAncestorOfType(dmContext, ITraceTargetDMContext.class);
			if (!sessionId.equals(fDebugSessionId)) {
				if (getSession() != null) {
					try {
						final DsfSession session = getSession();
						session.getExecutor().execute(new DsfRunnable() {
							@Override
							public void run() {
								session.removeServiceEventListener(TraceControlModel.this);
							}
						});
					} catch (RejectedExecutionException e) {
						// Session is shut down.
					}
				}
				fDebugSessionId = sessionId;
				if (fServicesTracker != null) {
					fServicesTracker.dispose();
				}
				fServicesTracker = new DsfServicesTracker(GdbUIPlugin.getBundleContext(), sessionId);
				getGDBTraceControl();
				debugSessionChanged();
			}
		} else if (fDebugSessionId != null) {
			if (getSession() != null) {
				try {
					final DsfSession session = getSession();
					session.getExecutor().execute(new DsfRunnable() {
						@Override
						public void run() {
							session.removeServiceEventListener(TraceControlModel.this);
						}
					});
				} catch (RejectedExecutionException e) {
					// Session is shut down.
				}
			}
			fDebugSessionId = null;
			fTargetContext = null;
			if (fServicesTracker != null) {
				fServicesTracker.dispose();
				fServicesTracker = null;
			}
			debugSessionChanged();
		}
	}

	private void debugSessionChanged() {
		if (getSession() != null) {
			try {
				final DsfSession session = getSession();
				session.getExecutor().execute(new DsfRunnable() {
					@Override
					public void run() {
						session.addServiceEventListener(TraceControlModel.this, null);
					}
				});
			} catch (RejectedExecutionException e) {
				// Session is shut down.
			}
		}

		updateContent();
	}

	public void exitVisualizationMode() {
		if (getSession() == null) {
			return;
		}

		getSession().getExecutor().execute(new DsfRunnable() {
			@Override
			public void run() {
				if (fTargetContext != null && fGDBTraceControl != null) {
					if (fGDBTraceControl instanceof IGDBTraceControl2) {
						((IGDBTraceControl2) fGDBTraceControl).stopTraceVisualization(fTargetContext,
								new ImmediateRequestMonitor());
					} else {
						// Legacy way of stopping visualization of trace data
						ITraceRecordDMContext emptyDmc = fGDBTraceControl.createTraceRecordContext(fTargetContext,
								"-1"); //$NON-NLS-1$
						fGDBTraceControl.selectTraceRecord(emptyDmc, new ImmediateRequestMonitor());
					}
				}
			}
		});
	}

	/**
	 * Get the list of trace variables from the backend.
	 *
	 * @return null when the list cannot be obtained.
	 */
	public ITraceVariableDMData[] getTraceVarList() {
		if (getSession() == null) {
			return null;
		}

		Query<ITraceVariableDMData[]> query = new Query<ITraceVariableDMData[]>() {
			@Override
			protected void execute(final DataRequestMonitor<ITraceVariableDMData[]> rm) {

				if (fTargetContext != null && fGDBTraceControl != null) {
					fGDBTraceControl.getTraceVariables(fTargetContext,
							new DataRequestMonitor<ITraceVariableDMData[]>(getSession().getExecutor(), rm) {
								@Override
								protected void handleCompleted() {
									if (isSuccess()) {
										rm.setData(getData());
									} else {
										rm.setData(null);
									}
									rm.done();
								}

							});
				} else {
					rm.setData(null);
					rm.done();
				}
			}
		};
		try {
			getSession().getExecutor().execute(query);
			return query.get(1, TimeUnit.SECONDS);
		} catch (InterruptedException exc) {
		} catch (ExecutionException exc) {
		} catch (TimeoutException e) {
		}

		return null;
	}

	/**
	 * Create a new trace variable in the backend.
	 *
	 * @throws FailedTraceVariableCreationException when the creation fails.  The exception
	 *         will contain the error message to display to the user.
	 */
	public void createVariable(final String name, final String value) throws FailedTraceVariableCreationException {
		if (getSession() == null) {
			throw new TraceControlView.FailedTraceVariableCreationException(
					TracepointsMessages.TraceControlView_create_variable_error);
		}

		Query<String> query = new Query<String>() {
			@Override
			protected void execute(final DataRequestMonitor<String> rm) {

				if (fTargetContext != null && fGDBTraceControl != null) {
					fGDBTraceControl.createTraceVariable(fTargetContext, name, value,
							new RequestMonitor(getSession().getExecutor(), rm) {
								@Override
								protected void handleFailure() {
									String message = TracepointsMessages.TraceControlView_create_variable_error;
									Throwable t = getStatus().getException();
									if (t != null) {
										message = t.getMessage();
									}
									FailedTraceVariableCreationException e = new FailedTraceVariableCreationException(
											message);
									rm.setStatus(new Status(IStatus.ERROR, GdbUIPlugin.PLUGIN_ID,
											IDsfStatusConstants.INVALID_STATE, "Backend error", e)); //$NON-NLS-1$
									rm.done();
								}
							});
				} else {
					FailedTraceVariableCreationException e = new FailedTraceVariableCreationException(
							TracepointsMessages.TraceControlView_trace_variable_tracing_unavailable);
					rm.setStatus(new Status(IStatus.ERROR, GdbUIPlugin.PLUGIN_ID, IDsfStatusConstants.INVALID_STATE,
							"Tracing unavailable", e)); //$NON-NLS-1$
					rm.done();
				}
			}
		};
		try {
			getSession().getExecutor().execute(query);
			query.get();
		} catch (InterruptedException e) {
			// Session terminated
		} catch (ExecutionException e) {
			Throwable t = e.getCause();
			if (t instanceof CoreException) {
				t = ((CoreException) t).getStatus().getException();
				if (t instanceof FailedTraceVariableCreationException) {
					throw (FailedTraceVariableCreationException) t;
				}
			}
			throw new FailedTraceVariableCreationException(TracepointsMessages.TraceControlView_create_variable_error);
		}
	}

	public void setCurrentTraceRecord(final String traceRecordId) {
		if (getSession() == null) {
			return;
		}

		getSession().getExecutor().execute(new DsfRunnable() {
			@Override
			public void run() {
				if (fTargetContext != null && fGDBTraceControl != null) {
					fGDBTraceControl.getCurrentTraceRecordContext(fTargetContext,
							new ImmediateDataRequestMonitor<ITraceRecordDMContext>() {
								@Override
								protected void handleSuccess() {
									final ITraceRecordDMContext previousDmc = getData();
									ITraceRecordDMContext nextRecord = fGDBTraceControl
											.createTraceRecordContext(fTargetContext, traceRecordId);

									// Must send the event right away to tell the services we are starting visualization
									// If we don't, the services won't behave accordingly soon enough
									// Bug 347514
									getSession().dispatchEvent(new TraceRecordSelectedChangedEvent(nextRecord),
											new Hashtable<String, String>());

									fGDBTraceControl.selectTraceRecord(nextRecord, new ImmediateRequestMonitor() {
										@Override
										protected void handleError() {
											// If we weren't able to select the next record, we must notify that we are still on the previous one
											// since we have already sent a TraceRecordSelectedChangedEvent early, but it didn't happen.
											getSession().dispatchEvent(new TraceRecordSelectedChangedEvent(previousDmc),
													new Hashtable<String, String>());
										}
									});
								}
							});

				}
			}
		});
	}

	public void setCircularBuffer(final boolean useCircularBuffer) {
		if (getSession() == null) {
			return;
		}

		getSession().getExecutor().execute(new DsfRunnable() {
			@Override
			public void run() {
				if (fTargetContext != null && fGDBTraceControl != null
						&& fGDBTraceControl instanceof IGDBTraceControl2) {
					((IGDBTraceControl2) fGDBTraceControl).setCircularTraceBuffer(fTargetContext, useCircularBuffer,
							new ImmediateRequestMonitor());
				}
			}
		});
	}

	public void setDisconnectedTracing(final boolean disconnected) {
		if (getSession() == null) {
			return;
		}

		getSession().getExecutor().execute(new DsfRunnable() {
			@Override
			public void run() {
				if (fTargetContext != null && fGDBTraceControl != null
						&& fGDBTraceControl instanceof IGDBTraceControl2) {
					((IGDBTraceControl2) fGDBTraceControl).setDisconnectedTracing(fTargetContext, disconnected,
							new ImmediateRequestMonitor());
				}
			}
		});
	}

	public void setTraceNotes(final String notes) {
		if (getSession() == null) {
			return;
		}

		getSession().getExecutor().execute(new DsfRunnable() {
			@Override
			public void run() {
				if (fTargetContext != null && fGDBTraceControl != null
						&& fGDBTraceControl instanceof IGDBTraceControl2) {
					((IGDBTraceControl2) fGDBTraceControl).setTraceNotes(fTargetContext, notes,
							new ImmediateRequestMonitor());
				}
			}
		});
	}

	private void getGDBTraceControl() {
		if (getSession() == null) {
			fGDBTraceControl = null;
			return;
		}

		getSession().getExecutor().execute(new DsfRunnable() {
			@Override
			public void run() {
				fGDBTraceControl = getService(IGDBTraceControl.class);
			}
		});
	}

	private DsfSession getSession() {
		return DsfSession.getSession(fDebugSessionId);
	}

	private <V> V getService(Class<V> serviceClass) {
		if (fServicesTracker != null) {
			return fServicesTracker.getService(serviceClass);
		}
		return null;
	}

	private void notifyUI(final ITraceStatusDMData2 data) {
		final TraceControlView v = fTraceControlView;
		if (v != null) {
			Display.getDefault().asyncExec(new Runnable() {
				@Override
				public void run() {
					if (v != null) {
						v.fLastRefreshTime = System.currentTimeMillis();
						v.updateUI(data);
					}
				}
			});
		}
	}

	private void notifyUI(final String message) {
		final TraceControlView v = fTraceControlView;
		if (v != null) {
			Display.getDefault().asyncExec(new Runnable() {
				@Override
				public void run() {
					if (v != null) {
						v.updateUI(message);
					}
				}
			});
		}
	}

	/*
	 * When tracing starts, we know the status has changed
	 */
	@DsfServiceEventHandler
	public void handleEvent(ITracingStartedDMEvent event) {
		updateContent();
	}

	/*
	 * When tracing stops, we know the status has changed
	 */
	@DsfServiceEventHandler
	public void handleEvent(ITracingStoppedDMEvent event) {
		updateContent();
	}

	@DsfServiceEventHandler
	public void handleEvent(ITraceRecordSelectedChangedDMEvent event) {
		updateContent();
	}

	/*
	 * Since something suspended, might as well refresh our status
	 * to show the latest.
	 */
	@DsfServiceEventHandler
	public void handleEvent(ISuspendedDMEvent event) {
		updateContent();
	}

	/*
	 * Tracing support has changed, update view
	 */
	@DsfServiceEventHandler
	public void handleEvent(ITracingSupportedChangeDMEvent event) {
		updateContent();
	}
}

Back to the top