Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4b17bfbe7ba3ca1227833e3592556c39ddef835c (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
/*******************************************************************************
 * Copyright (c) 2012, 2016 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:
 * Mentor Graphics - Initial API and implementation
 * Marc Khouzam (Ericsson) - Run tests in alphabetical order since they are dependent on each other.
 *******************************************************************************/

package org.eclipse.cdt.tests.dsf.gdb.tests;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants;
import org.eclipse.cdt.debug.core.breakpointactions.BreakpointActionManager;
import org.eclipse.cdt.debug.core.model.ICTracepoint;
import org.eclipse.cdt.dsf.concurrent.ConfinedToDsfExecutor;
import org.eclipse.cdt.dsf.concurrent.DataRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.RequestMonitor;
import org.eclipse.cdt.dsf.datamodel.DMContexts;
import org.eclipse.cdt.dsf.debug.service.IBreakpoints;
import org.eclipse.cdt.dsf.debug.service.IBreakpoints.IBreakpointDMContext;
import org.eclipse.cdt.dsf.debug.service.IBreakpoints.IBreakpointsTargetDMContext;
import org.eclipse.cdt.dsf.debug.service.IRunControl.IContainerDMContext;
import org.eclipse.cdt.dsf.gdb.IGDBLaunchConfigurationConstants;
import org.eclipse.cdt.dsf.gdb.internal.tracepointactions.CollectAction;
import org.eclipse.cdt.dsf.gdb.internal.tracepointactions.EvaluateAction;
import org.eclipse.cdt.dsf.gdb.internal.tracepointactions.ITracepointAction;
import org.eclipse.cdt.dsf.gdb.internal.tracepointactions.TracepointActionManager;
import org.eclipse.cdt.dsf.gdb.service.IGDBTraceControl;
import org.eclipse.cdt.dsf.gdb.service.IGDBTraceControl.ITraceStatusDMData;
import org.eclipse.cdt.dsf.gdb.service.IGDBTraceControl.ITraceTargetDMContext;
import org.eclipse.cdt.dsf.mi.service.MIBreakpoints;
import org.eclipse.cdt.dsf.mi.service.MIBreakpoints.MIBreakpointDMContext;
import org.eclipse.cdt.dsf.mi.service.command.events.MIBreakpointHitEvent;
import org.eclipse.cdt.dsf.mi.service.command.events.MIStoppedEvent;
import org.eclipse.cdt.dsf.service.DsfServicesTracker;
import org.eclipse.cdt.dsf.service.DsfSession;
import org.eclipse.cdt.tests.dsf.gdb.framework.AsyncCompletionWaitor;
import org.eclipse.cdt.tests.dsf.gdb.framework.BaseParametrizedTestCase;
import org.eclipse.cdt.tests.dsf.gdb.framework.SyncUtil;
import org.eclipse.cdt.tests.dsf.gdb.launching.TestsPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.Path;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.IBreakpointManager;
import org.eclipse.debug.core.model.IBreakpoint;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assume;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith(Parameterized.class)
public class TraceFileTest extends BaseParametrizedTestCase {

	private final static String SOURCE_NAME = "TracepointTestApp.cc";
	private final static String EXEC_NAME = "TracepointTestApp.exe";
	private final static String TRACE_NAME = "trace";
	private final static String TRACE_FILE_PATH = EXEC_PATH + TRACE_NAME;
	// Breakpoint tags in TracepointTestApp.cc
	public static final String[] LINE_TAGS = new String[] { "IF_X_NE_A", "INCR_X", };
	private final static String END_FUNCTION = "lastCall";
	private final static String TEVAL_STRING = "a";
	private final static String COLLECT_STRING1 = "x";
	private final static String COLLECT_STRING2 = "$regs";

	private DsfSession fSession;
	private DsfServicesTracker fServicesTracker;
	private IBreakpoints fBreakpointService;
	private IGDBTraceControl fTraceService;
	private IBreakpointsTargetDMContext fBreakpointsDmc;
	private ITraceTargetDMContext fTraceTargetDmc;
	private boolean suppressRemoveAllPlatformBreakpoints;

	@Override
	public void doBeforeTest() throws Exception {
		// GDB tracepoints are only supported on a remote target (e.g., using gdbserver)
		assumeRemoteSession();
		resolveLineTagLocations(SOURCE_NAME, LINE_TAGS);
		assumeGdbVersionAtLeast(ITestConstants.SUFFIX_GDB_7_4);
		removeTeminatedLaunchesBeforeTest();
		// Suppress settings of the launch attributes and launching.
		// Each test sets its own launch attributes
	}

	@AfterClass
	public static void doAfterClassTraceFileTest_7_4() {
		try {
			// Make sure we don't have any tracepoint actions
			// or any kind of breakpoints in the workspace
			// so that tests run after this class are not affected
			deleteActionsAndBreakpoints();
		} catch (Throwable e) {
			System.out.println("ERROR: Failed to delete all breakpoints");
		}
	}

	/**
	 * Some tests call doBefore/After in the middle of their test and rely on
	 * platform breakpoints to survive that step. So override with the ability
	 * to disable.
	 */
	@Override
	public void removeAllPlatformBreakpoints() throws CoreException {
		if (!suppressRemoveAllPlatformBreakpoints) {
			super.removeAllPlatformBreakpoints();
		}
	}

	@Override
	@After
	public void doAfterTest() throws Exception {
		super.doAfterTest();
		fBreakpointService = null;
		fTraceService = null;
		fBreakpointsDmc = null;
		fTraceTargetDmc = null;
		if (fServicesTracker != null) {
			fServicesTracker.dispose();
			fServicesTracker = null;
		}
	}

	/**
	 * This method implements the following steps.
	 * 1. Starts a remote session
	 * 2. Sets two tracepoints in data/launch/src/TracepointTestApp.cc
	 *    The first tracepoint's command is "teval a".
	 *    The second tracepoint's commands are "collect counter" and "collect $regs".
	 * 3. Sets a regular breakpoint at the end of the source file.
	 * 4. Starts tracing
	 * 5. Resumes and runs until the breakpoint is hit
	 * 6. Stops tracing
	 * 7. Saves the trace data into a file (data/launch/bin/trace).
	 */
	protected void createTraceFile() throws Throwable {
		// Make sure that there are no tracepoint actions and no platform breakpoints in the workspace.
		deleteActionsAndBreakpoints();
		deleteOldTraceFile();

		startRemoteSession();
		setTracepoints();
		MIBreakpointDMContext bptDMC = setBreakpointAtEndLine();
		startTracing();
		MIStoppedEvent stoppedEvent = SyncUtil.resumeUntilStopped();
		assertTrue(stoppedEvent instanceof MIBreakpointHitEvent
				&& ((MIBreakpointHitEvent) stoppedEvent).getNumber().equals(bptDMC.getReference()));
		stopTracing();
		saveTraceData();
	}

	/**
	 * This test verifies that creating a trace file works as expected.
	 * @see #createTraceFile
	 */
	@Test
	public void testCreateTraceFile() throws Throwable {
		// This tests forces a remote session by calling startRemoteSession()
		// The problem is that if gdbserver is not available, the test will fail
		// If the tests being run are only triggering local tests, then this failure
		// should not be happening.
		// Therefore, we only run this tests if we are specifically running remote tests.
		Assume.assumeTrue("Skipping non-remote", remote);

		createTraceFile();
	}

	/**
	 * This test sets up by first creating a trace file
	 * by calling {@link #createTraceFile}
	 *
	 * It then removes all existing tracepoint actions and tracepoints
	 * and verifies that corresponding platform tracepoints with the proper
	 * actions are created.
	 */
	@Test
	public void testTraceFile() throws Throwable {
		// This test will force a local post-mortem session, so only run it in local mode
		Assume.assumeTrue("Skipping remote", !remote);

		try {
			createTraceFile();
			suppressRemoveAllPlatformBreakpoints = true;
			try {
				// Cleanup the interim launch that we just caused
				doAfterTest();
				// Setup for the upcoming launch
				doBeforeTest();
			} finally {
				suppressRemoveAllPlatformBreakpoints = false;
			}
		} catch (Throwable t) {
			// If we cannot create the trace file, ignore the test using the
			// assume check below.  The reason for the failure could be a missing
			// gdbserver, and we don't want to fail a local test due to that
		}

		Assume.assumeTrue("Cannot find trace file: " + TRACE_FILE_PATH, new File(TRACE_FILE_PATH).exists());

		// Make sure that there are no tracepoint actions and no platform breakpoints in the workspace.
		deleteActionsAndBreakpoints();

		startTraceFileSession();
		// Verify that required tracepoints and new tracepoint actions are created.
		checkActionsAndTracepoints();
	}

	/**
	 * This test sets up by first creating a trace file and importing it back
	 * by calling {@link #testTraceFile} which also calls {@link #createTraceFile}

	 * It then verifies that the tracepoint actions and platform tracepoints
	 * created by {@link #testTraceFile()} are associated with the corresponding target
	 * tracepoints and are not created a second time.
	 */
	@Test
	public void testTraceFileWithExistingTracepoints() throws Throwable {
		// This test will force a local post-mortem session, so only run it in local mode
		Assume.assumeTrue("Skipping remote", !remote);

		// This test requires the presence of tracepoints created by another test.
		// To allow our current test to be independent, we explicitly call
		// the required test ourselves.
		testTraceFile();
		suppressRemoveAllPlatformBreakpoints = true;
		try {
			// Cleanup the interim launch that we just caused
			doAfterTest();
			// Setup for the upcoming launch
			clearLineTags();
			doBeforeTest();
		} finally {
			suppressRemoveAllPlatformBreakpoints = false;
		}

		// Verify that actions and tracepoints required for this test are in place.
		checkActionsAndTracepoints();
		startTraceFileSession();
		// Verify that no new platform tracepoints or new tracepoint actions are created.
		checkActionsAndTracepoints();
	}

	private void startTraceFileSession() throws Throwable {
		// Set launch attributes
		super.setLaunchAttributes();
		// Set a working directory for GDB that is different than eclipse's directory.
		// This allows us to make sure we properly handle finding the core file,
		// especially in the case of a relative path
		setLaunchAttribute(ICDTLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, "${workspace_loc}");
		// Because we just set a different working directory, we must use an absolute path for the program
		String absoluteProgram = new Path(EXEC_PATH + EXEC_NAME).toFile().getAbsolutePath();
		setLaunchAttribute(ICDTLaunchConfigurationConstants.ATTR_PROGRAM_NAME, absoluteProgram);

		// Set post-mortem launch
		setLaunchAttribute(ICDTLaunchConfigurationConstants.ATTR_DEBUGGER_START_MODE,
				ICDTLaunchConfigurationConstants.DEBUGGER_MODE_CORE);
		// Set post-mortem type to trace file
		setLaunchAttribute(IGDBLaunchConfigurationConstants.ATTR_DEBUGGER_POST_MORTEM_TYPE,
				IGDBLaunchConfigurationConstants.DEBUGGER_POST_MORTEM_TRACE_FILE);
		// Set core file path
		setLaunchAttribute(ICDTLaunchConfigurationConstants.ATTR_COREFILE_PATH, TRACE_FILE_PATH);

		doLaunch();

		// Allow time to create tracepoint actions
		Thread.sleep(1000);
	}

	/**
	 * Deletes all tracepoint actions and all existing platform breakpoints.
	 */
	private static void deleteActionsAndBreakpoints() throws Throwable {
		TracepointActionManager tam = TracepointActionManager.getInstance();
		IBreakpointManager bm = DebugPlugin.getDefault().getBreakpointManager();

		// Delete all existing actions
		@SuppressWarnings("unchecked")
		ArrayList<ITracepointAction> actions = (ArrayList<ITracepointAction>) tam.getActions().clone();
		for (ITracepointAction a : actions) {
			tam.deleteAction(a);
		}

		IBreakpoint[] bpts = bm.getBreakpoints();
		for (IBreakpoint b : bpts) {
			bm.removeBreakpoint(b, true);
		}
	}

	/**
	 * Checks whether there are only two platform tracepoints and three tracepoint actions.
	 */
	private void checkActionsAndTracepoints() throws Throwable {
		TracepointActionManager tam = TracepointActionManager.getInstance();
		IBreakpointManager bm = DebugPlugin.getDefault().getBreakpointManager();
		ArrayList<ITracepointAction> actions = tam.getActions();
		IBreakpoint[] bpts = bm.getBreakpoints();
		actions = tam.getActions();
		assertTrue(String.format("Unexpected count of tracepoint actions: %d", actions.size()), actions.size() == 3);
		bpts = bm.getBreakpoints();
		assertTrue(String.format("Unexpected count of breakpoints: %d", bpts.length), bpts.length == 2);
		for (IBreakpoint b : bpts) {
			assertTrue(b instanceof ICTracepoint);
			checkTracepoint((ICTracepoint) b);
		}
	}

	private void checkTracepoint(ICTracepoint tracepoint) throws Throwable {
		TracepointActionManager tam = TracepointActionManager.getInstance();
		assertTrue(SOURCE_NAME.equals(new Path(tracepoint.getFileName()).lastSegment()));
		assertTrue(getLineForTag("IF_X_NE_A") == tracepoint.getLineNumber()
				|| getLineForTag("INCR_X") == tracepoint.getLineNumber());
		String[] actionNames = ((String) tracepoint.getMarker()
				.getAttribute(BreakpointActionManager.BREAKPOINT_ACTION_ATTRIBUTE))
						.split(TracepointActionManager.TRACEPOINT_ACTION_DELIMITER);
		for (String name : actionNames) {
			ITracepointAction a = tam.findAction(name);
			assertNotNull(a);
			if (a instanceof EvaluateAction) {
				assertTrue(TEVAL_STRING.equals(((EvaluateAction) a).getEvalString()));
			}
			if (a instanceof CollectAction) {
				assertTrue(COLLECT_STRING1.equals(((CollectAction) a).getCollectString())
						|| COLLECT_STRING2.equals(((CollectAction) a).getCollectString()));
			}
		}
	}

	private void startRemoteSession() throws Throwable {
		// Set launch attributes
		super.setLaunchAttributes();
		setLaunchAttribute(ICDTLaunchConfigurationConstants.ATTR_PROGRAM_NAME, EXEC_PATH + EXEC_NAME);
		setLaunchAttribute(IGDBLaunchConfigurationConstants.ATTR_DEBUGGER_TRACEPOINT_MODE,
				IGDBLaunchConfigurationConstants.DEBUGGER_TRACEPOINT_MODE_DEFAULT);

		// Start the session
		doLaunch();

		// Initialize
		fSession = getGDBLaunch().getSession();
		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				fServicesTracker = new DsfServicesTracker(TestsPlugin.getBundleContext(), fSession.getId());
				fBreakpointService = fServicesTracker.getService(IBreakpoints.class);
				fTraceService = fServicesTracker.getService(IGDBTraceControl.class);
			}
		};
		fSession.getExecutor().submit(runnable).get();

		IContainerDMContext containerDmc = SyncUtil.getContainerContext();
		fBreakpointsDmc = DMContexts.getAncestorOfType(containerDmc, IBreakpointsTargetDMContext.class);
		assertNotNull(fBreakpointsDmc);
		fTraceTargetDmc = DMContexts.getAncestorOfType(containerDmc, ITraceTargetDMContext.class);
		assertNotNull(fTraceTargetDmc);
	}

	private void startTracing() throws Throwable {
		final AsyncCompletionWaitor wait = new AsyncCompletionWaitor();
		fSession.getExecutor().submit(new Runnable() {
			@Override
			public void run() {
				fTraceService.getTraceStatus(fTraceTargetDmc,
						new DataRequestMonitor<ITraceStatusDMData>(fSession.getExecutor(), null) {
							@Override
							@ConfinedToDsfExecutor("fExecutor")
							protected void handleCompleted() {
								if (isSuccess() && getData().isTracingSupported()) {
									fTraceService.startTracing(fTraceTargetDmc,
											new RequestMonitor(fSession.getExecutor(), null) {
												@Override
												@ConfinedToDsfExecutor("fExecutor")
												protected void handleCompleted() {
													wait.waitFinished(getStatus());
												}
											});
								} else {
									wait.waitFinished(getStatus());
								}
							}
						});
			}
		});
		wait.waitUntilDone(AsyncCompletionWaitor.WAIT_FOREVER);
		assertTrue(wait.getMessage(), wait.isOK());
	}

	private void stopTracing() throws Throwable {
		final AsyncCompletionWaitor wait = new AsyncCompletionWaitor();
		fSession.getExecutor().submit(new Runnable() {
			@Override
			public void run() {
				fTraceService.stopTracing(fTraceTargetDmc, new RequestMonitor(fSession.getExecutor(), null) {
					@Override
					@ConfinedToDsfExecutor("fExecutor")
					protected void handleCompleted() {
						wait.waitFinished(getStatus());
					}
				});
			}
		});
		wait.waitUntilDone(AsyncCompletionWaitor.WAIT_FOREVER);
		assertTrue(wait.getMessage(), wait.isOK());
	}

	private MIBreakpointDMContext setBreakpointAtEndLine() throws Throwable {
		Map<String, Object> attributes = new HashMap<String, Object>();
		attributes.put(MIBreakpoints.BREAKPOINT_TYPE, MIBreakpoints.BREAKPOINT);
		attributes.put(MIBreakpoints.FILE_NAME, SOURCE_NAME);
		attributes.put(MIBreakpoints.FUNCTION, END_FUNCTION);
		IBreakpointDMContext bptDMC = insertBreakpoint(fBreakpointsDmc, attributes);
		assertTrue(bptDMC instanceof MIBreakpointDMContext);
		return (MIBreakpointDMContext) bptDMC;
	}

	private void setTracepoints() throws Throwable {
		TracepointActionManager tam = TracepointActionManager.getInstance();

		CollectAction collectAction1 = new CollectAction();
		collectAction1.setCollectString(COLLECT_STRING1);
		collectAction1.setName(String.format("Collect %s", COLLECT_STRING1));
		tam.addAction(collectAction1);

		CollectAction collectAction2 = new CollectAction();
		collectAction2.setCollectString(COLLECT_STRING2);
		collectAction2.setName(String.format("Collect %s", COLLECT_STRING2));
		tam.addAction(collectAction2);

		EvaluateAction evalAction = new EvaluateAction();
		evalAction.setEvalString(TEVAL_STRING);
		evalAction.setName(String.format("Evaluate %s", TEVAL_STRING));
		tam.addAction(evalAction);

		Map<String, Object> attributes = new HashMap<String, Object>();
		attributes.put(MIBreakpoints.BREAKPOINT_TYPE, MIBreakpoints.TRACEPOINT);
		attributes.put(MIBreakpoints.FILE_NAME, SOURCE_NAME);
		attributes.put(MIBreakpoints.LINE_NUMBER, getLineForTag("IF_X_NE_A"));
		attributes.put(MIBreakpoints.COMMANDS, evalAction.getName());
		insertBreakpoint(fBreakpointsDmc, attributes);

		attributes.put(MIBreakpoints.LINE_NUMBER, getLineForTag("INCR_X"));
		attributes.put(MIBreakpoints.COMMANDS, String.format("%s%s%s", collectAction1.getName(),
				TracepointActionManager.TRACEPOINT_ACTION_DELIMITER, collectAction2.getName()));
		insertBreakpoint(fBreakpointsDmc, attributes);
	}

	private IBreakpointDMContext insertBreakpoint(final IBreakpointsTargetDMContext context,
			final Map<String, Object> attributes) throws InterruptedException {
		final AsyncCompletionWaitor wait = new AsyncCompletionWaitor();

		fSession.getExecutor().submit(new Runnable() {
			@Override
			public void run() {
				fBreakpointService.insertBreakpoint(context, attributes,
						new DataRequestMonitor<IBreakpointDMContext>(fBreakpointService.getExecutor(), null) {
							@Override
							protected void handleCompleted() {
								wait.setReturnInfo(getData());
								wait.waitFinished(getStatus());
							}
						});
			}
		});

		// Wait for the result and return the breakpoint context
		wait.waitUntilDone(AsyncCompletionWaitor.WAIT_FOREVER);
		assertTrue(wait.getMessage(), wait.isOK());

		return (IBreakpointDMContext) wait.getReturnInfo();
	}

	private void saveTraceData() throws Throwable {
		final File traceFile = new Path(TRACE_FILE_PATH).toFile();
		final AsyncCompletionWaitor wait = new AsyncCompletionWaitor();

		fSession.getExecutor().submit(new Runnable() {
			@Override
			public void run() {
				fTraceService.saveTraceData(fTraceTargetDmc, traceFile.getAbsolutePath(), false,
						new RequestMonitor(fSession.getExecutor(), null) {
							@Override
							protected void handleCompleted() {
								wait.waitFinished(getStatus());
							}
						});
			}
		});

		// Wait for the result and verify the trace file is created
		wait.waitUntilDone(AsyncCompletionWaitor.WAIT_FOREVER);
		assertTrue(wait.getMessage(), wait.isOK());
		assertTrue(traceFile.exists());
	}

	private void deleteOldTraceFile() throws Throwable {
		File traceFile = new Path(TRACE_FILE_PATH).toFile();
		traceFile.delete();
		assertFalse(traceFile.exists());
	}
}

Back to the top