Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: acf45f16b1d7dd54576f65b56e8d9be0fd4ccb8b (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
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
/*******************************************************************************
 * Copyright (c) 2011, 2016 Ericsson 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:
 *     Ericsson			  - Initial Implementation
 *     Simon Marchi (Ericsson) - Remove a catch that just fails a test.
 *     Simon Marchi (Ericsson) - Disable tests for gdb < 7.2.
 *******************************************************************************/
package org.eclipse.cdt.tests.dsf.gdb.tests;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.eclipse.cdt.debug.core.CDIDebugModel;
import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants;
import org.eclipse.cdt.debug.core.model.ICBreakpointType;
import org.eclipse.cdt.dsf.concurrent.DataRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.ImmediateDataRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.Query;
import org.eclipse.cdt.dsf.datamodel.DMContexts;
import org.eclipse.cdt.dsf.debug.service.IBreakpoints.IBreakpointsTargetDMContext;
import org.eclipse.cdt.dsf.debug.service.IExpressions;
import org.eclipse.cdt.dsf.debug.service.IExpressions.IExpressionDMContext;
import org.eclipse.cdt.dsf.debug.service.IFormattedValues.FormattedValueDMData;
import org.eclipse.cdt.dsf.debug.service.IRunControl.IExecutionDMContext;
import org.eclipse.cdt.dsf.debug.service.IRunControl.StepType;
import org.eclipse.cdt.dsf.debug.service.IStack.IFrameDMData;
import org.eclipse.cdt.dsf.debug.service.command.ICommandControlService.ICommandControlShutdownDMEvent;
import org.eclipse.cdt.dsf.gdb.IGDBLaunchConfigurationConstants;
import org.eclipse.cdt.dsf.gdb.IGdbDebugConstants;
import org.eclipse.cdt.dsf.gdb.launching.InferiorRuntimeProcess;
import org.eclipse.cdt.dsf.gdb.service.command.IGDBControl;
import org.eclipse.cdt.dsf.mi.service.MIExpressions;
import org.eclipse.cdt.dsf.mi.service.command.events.MIStoppedEvent;
import org.eclipse.cdt.dsf.mi.service.command.output.CLITraceInfo;
import org.eclipse.cdt.dsf.mi.service.command.output.MIBreakListInfo;
import org.eclipse.cdt.dsf.mi.service.command.output.MIBreakpoint;
import org.eclipse.cdt.dsf.mi.service.command.output.MIInfo;
import org.eclipse.cdt.dsf.service.DsfServicesTracker;
import org.eclipse.cdt.dsf.service.DsfSession;
import org.eclipse.cdt.tests.dsf.gdb.framework.BaseParametrizedTestCase;
import org.eclipse.cdt.tests.dsf.gdb.framework.Intermittent;
import org.eclipse.cdt.tests.dsf.gdb.framework.IntermittentRule;
import org.eclipse.cdt.tests.dsf.gdb.framework.ServiceEventWaitor;
import org.eclipse.cdt.tests.dsf.gdb.framework.SyncUtil;
import org.eclipse.cdt.tests.dsf.gdb.launching.TestsPlugin;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.model.IProcess;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

@RunWith(Parameterized.class)
@Intermittent(repetition = 3)
public class LaunchConfigurationAndRestartTest extends BaseParametrizedTestCase {
	public @Rule IntermittentRule intermittentRule = new IntermittentRule();
	protected static final String EXEC_NAME = "LaunchConfigurationAndRestartTestApp.exe";
	protected static final String SOURCE_NAME = "LaunchConfigurationAndRestartTestApp.cc";

	protected static final String[] LINE_TAGS = new String[] { "FIRST_LINE_IN_MAIN", "LAST_LINE_IN_MAIN", };

	protected int FIRST_LINE_IN_MAIN;
	protected int LAST_LINE_IN_MAIN;

	// The exit code returned by the test program
	private static final int TEST_EXIT_CODE = 36;

	protected DsfSession fSession;
	protected DsfServicesTracker fServicesTracker;
	protected IExpressions fExpService;
	protected IGDBControl fGdbControl;

	// Indicates if a restart operation should be done
	// This allows us to re-use tests for restarts tests
	protected boolean fRestart;

	@Override
	public void doBeforeTest() throws Exception {
		assumeLocalSession();
		removeTeminatedLaunchesBeforeTest();
		setLaunchAttributes();
		// Can't run the launch right away because each test needs to first set some
		// parameters.  The individual tests will be responsible for starting the launch.

		// Looks up line tags in source file(s).
		clearLineTags();
		resolveLineTagLocations(SOURCE_NAME, LINE_TAGS);

		FIRST_LINE_IN_MAIN = getLineForTag("FIRST_LINE_IN_MAIN");
		LAST_LINE_IN_MAIN = getLineForTag("LAST_LINE_IN_MAIN");
	}

	@Override
	protected void setLaunchAttributes() {
		super.setLaunchAttributes();

		// Set the binary
		setLaunchAttribute(ICDTLaunchConfigurationConstants.ATTR_PROGRAM_NAME, EXEC_PATH + EXEC_NAME);
	}

	// This method cannot be tagged as @Before, because the launch is not
	// running yet.  We have to call this manually after all the proper
	// parameters have been set for the launch
	@Override
	protected void doLaunch() throws Exception {
		// perform the launch
		super.doLaunch();

		fSession = getGDBLaunch().getSession();
		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				fServicesTracker = new DsfServicesTracker(TestsPlugin.getBundleContext(), fSession.getId());

				fExpService = fServicesTracker.getService(IExpressions.class);
				fGdbControl = fServicesTracker.getService(IGDBControl.class);
			}
		};
		fSession.getExecutor().submit(runnable).get();

		// Restart the program if we are testing such a case
		if (fRestart) {
			synchronized (this) {
				wait(1000); // XXX: horrible hack, what are we waiting for?
			}
			fRestart = false;
			SyncUtil.restart(getGDBLaunch());
		}
	}

	@Override
	public void doAfterTest() throws Exception {
		super.doAfterTest();

		if (fServicesTracker != null)
			fServicesTracker.dispose();
	}

	// *********************************************************************
	// Below are the tests for the launch configuration.
	// *********************************************************************

	/**
	 * This test will tell the launch to set the working directory to data/launch/bin/
	 * and will verify that we can find the file LaunchConfigurationAndRestartTestApp.cpp.
	 * This will confirm that GDB has been properly configured with the working dir.
	 */
	@Test
	public void testSettingWorkingDirectory() throws Throwable {
		String dir = new File(EXEC_PATH).getAbsolutePath();
		setLaunchAttribute(ICDTLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, dir);
		setLaunchAttribute(ICDTLaunchConfigurationConstants.ATTR_PROGRAM_NAME, dir + "/" + EXEC_NAME);

		doLaunch();

		Query<MIInfo> query = new Query<MIInfo>() {
			@Override
			protected void execute(DataRequestMonitor<MIInfo> rm) {
				fGdbControl.queueCommand(
						fGdbControl.getCommandFactory().createMIFileExecFile(fGdbControl.getContext(), EXEC_NAME), rm);
			}
		};
		{
			fExpService.getExecutor().execute(query);
			query.get(TestsPlugin.massageTimeout(500), TimeUnit.MILLISECONDS);
		}
	}

	/**
	 * This test will verify that a launch will fail if the gdbinit file
	 * does not exist and is not called ".gdbinit".
	 */
	@Test
	public void testSourceInvalidGdbInit() throws Throwable {
		setLaunchAttribute(IGDBLaunchConfigurationConstants.ATTR_GDB_INIT, "gdbinitThatDoesNotExist");
		try {
			doLaunch();
		} catch (CoreException e) {
			// Success of the test
			return;
		}

		fail("Launch seems to have succeeded even though the gdbinit file did not exist");
	}

	/**
	 * This test will verify that a launch does not fail if the gdbinit file
	 * is called ".gdbinit" and does not exist
	 */
	@Test
	public void testSourceDefaultGdbInit() throws Throwable {
		setLaunchAttribute(IGDBLaunchConfigurationConstants.ATTR_GDB_INIT, ".gdbinit");
		try {
			doLaunch();
		} catch (CoreException e) {
			throw new AssertionError("Launch has failed even though the gdbinit file has the default name of .gdbinit",
					e);
		}
	}

	/**
	 * This test will tell the launch to use data/launch/src/launchConfigTestGdbinit
	 * as the gdbinit file.  We then verify the that the content was properly read.
	 * launchConfigTestGdbinit will simply set some arguments for the program to read;
	 * the arguments are "1 2 3 4 5 6".
	 *
	 * This test is disabled for gdb.7.1 because gdb inserts an extraneous \n that messes up
	 * the launch sequence (more particularly, the byte length detection):
	 *
	 *     17-interpreter-exec console "p/x (char)-1"
	 *     ~"\n"
	 *     ~"$1 = 0xff\n"
	 *     17^done
	 */
	@Test

	public void testSourceGdbInit() throws Throwable {
		assumeGdbVersionAtLeast(ITestConstants.SUFFIX_GDB_7_2);
		setLaunchAttribute(IGDBLaunchConfigurationConstants.ATTR_GDB_INIT, "data/launch/src/launchConfigTestGdbinit");
		doLaunch();

		MIStoppedEvent stoppedEvent = getInitialStoppedEvent();

		// Check that argc is correct
		final IExpressionDMContext argcDmc = SyncUtil.createExpression(stoppedEvent.getDMContext(), "argc");
		Query<FormattedValueDMData> query = new Query<FormattedValueDMData>() {
			@Override
			protected void execute(DataRequestMonitor<FormattedValueDMData> rm) {
				fExpService.getFormattedExpressionValue(
						fExpService.getFormattedValueContext(argcDmc, MIExpressions.DETAILS_FORMAT), rm);
			}
		};
		{
			fExpService.getExecutor().execute(query);
			FormattedValueDMData value = query.get(TestsPlugin.massageTimeout(500), TimeUnit.MILLISECONDS);

			// Argc should be 7: the program name and the six arguments
			assertTrue("Expected 7 but got " + value.getFormattedValue(), value.getFormattedValue().trim().equals("7"));
		}

		// Check that argv is also correct.  For simplicity we only check the last argument
		final IExpressionDMContext argvDmc = SyncUtil.createExpression(stoppedEvent.getDMContext(), "argv[argc-1]");
		Query<FormattedValueDMData> query2 = new Query<FormattedValueDMData>() {
			@Override
			protected void execute(DataRequestMonitor<FormattedValueDMData> rm) {
				fExpService.getFormattedExpressionValue(
						fExpService.getFormattedValueContext(argvDmc, MIExpressions.DETAILS_FORMAT), rm);
			}
		};
		{
			fExpService.getExecutor().execute(query2);
			FormattedValueDMData value = query2.get(TestsPlugin.massageTimeout(500), TimeUnit.MILLISECONDS);
			assertTrue("Expected \"6\" but got " + value.getFormattedValue(),
					value.getFormattedValue().trim().endsWith("\"6\""));
		}
	}

	/**
	 * Repeat the test testSourceGdbInit, but after a restart.
	 */
	@Test
	public void testSourceGdbInitRestart() throws Throwable {
		assumeGdbVersionAtLeast(ITestConstants.SUFFIX_GDB_7_2);
		fRestart = true;
		testSourceGdbInit();
	}

	/**
	 * This test will tell the launch to clear the environment variables.  We will
	 * then check that the variable $HOME cannot be found by the program.
	 */
	@Test
	public void testClearingEnvironment() throws Throwable {
		setLaunchAttribute(ILaunchManager.ATTR_APPEND_ENVIRONMENT_VARIABLES, false);
		doLaunch();

		SyncUtil.runToLocation("envTest");
		MIStoppedEvent stoppedEvent = SyncUtil.step(2, StepType.STEP_OVER);

		// The program has stored the content of $HOME into a variable called 'home'.
		// Let's verify this variable is 0x0 which means $HOME does not exist.
		final IExpressionDMContext exprDmc = SyncUtil.createExpression(stoppedEvent.getDMContext(), "home");
		Query<FormattedValueDMData> query = new Query<FormattedValueDMData>() {
			@Override
			protected void execute(DataRequestMonitor<FormattedValueDMData> rm) {
				fExpService.getFormattedExpressionValue(
						fExpService.getFormattedValueContext(exprDmc, MIExpressions.DETAILS_FORMAT), rm);
			}
		};
		{
			fExpService.getExecutor().execute(query);
			FormattedValueDMData value = query.get(TestsPlugin.massageTimeout(500), TimeUnit.MILLISECONDS);
			assertTrue("Expected 0x0 but got " + value.getFormattedValue(), value.getFormattedValue().equals("0x0"));
		}
	}

	/**
	 * Repeat the test testClearingEnvironment, but after a restart.
	 */
	@Test
	public void testClearingEnvironmentRestart() throws Throwable {
		fRestart = true;
		testClearingEnvironment();
	}

	/**
	 * This test will tell the launch to set a new environment variable LAUNCHTEST.
	 * We will then check that this new variable can be read by the program.
	 */
	@Test
	public void testSettingEnvironment() throws Throwable {
		setLaunchAttribute(ILaunchManager.ATTR_APPEND_ENVIRONMENT_VARIABLES, true);

		Map<String, String> map = new HashMap<>(1);
		map.put("LAUNCHTEST", "IS SET");
		setLaunchAttribute(ILaunchManager.ATTR_ENVIRONMENT_VARIABLES, map);
		doLaunch();

		SyncUtil.runToLocation("envTest");
		MIStoppedEvent stoppedEvent = SyncUtil.step(2, StepType.STEP_OVER);

		// The program has stored the content of $LAUNCHTEST into a variable called 'launchTest'.
		// Let's verify this variable is set to "IS SET".
		final IExpressionDMContext exprDmc = SyncUtil.createExpression(stoppedEvent.getDMContext(), "launchTest");
		Query<FormattedValueDMData> query = new Query<FormattedValueDMData>() {
			@Override
			protected void execute(DataRequestMonitor<FormattedValueDMData> rm) {
				fExpService.getFormattedExpressionValue(
						fExpService.getFormattedValueContext(exprDmc, MIExpressions.DETAILS_FORMAT), rm);
			}
		};
		{
			fExpService.getExecutor().execute(query);
			FormattedValueDMData value = query.get(TestsPlugin.massageTimeout(500), TimeUnit.MILLISECONDS);
			assertTrue("Expected a string ending with \"IS SET\" but got " + value.getFormattedValue(),
					value.getFormattedValue().trim().endsWith("\"IS SET\""));
		}

		// Check that the normal environment is there by checking that $HOME (which is stored in 'home" exists.
		final IExpressionDMContext exprDmc2 = SyncUtil.createExpression(stoppedEvent.getDMContext(), "home");
		Query<FormattedValueDMData> query2 = new Query<FormattedValueDMData>() {
			@Override
			protected void execute(DataRequestMonitor<FormattedValueDMData> rm) {
				fExpService.getFormattedExpressionValue(
						fExpService.getFormattedValueContext(exprDmc2, MIExpressions.DETAILS_FORMAT), rm);
			}
		};
		{
			fExpService.getExecutor().execute(query2);
			FormattedValueDMData value = query2.get(TestsPlugin.massageTimeout(500), TimeUnit.MILLISECONDS);
			assertFalse("Expected something else than 0x0", value.getFormattedValue().equals("0x0"));
		}

	}

	/**
	 * Repeat the test testSettingEnvironment, but after a restart.
	 */
	@Test
	public void testSettingEnvironmentRestart() throws Throwable {
		fRestart = true;
		testSettingEnvironment();
	}

	/**
	 * This test will tell the launch to clear the environment variables and then
	 * set a new environment variable LAUNCHTEST.  We will then check that the variable
	 * $HOME cannot be found by the program and that the new variable LAUNCHTEST can be
	 * read by the program.
	 */
	@Test
	public void testClearingAndSettingEnvironment() throws Throwable {
		setLaunchAttribute(ILaunchManager.ATTR_APPEND_ENVIRONMENT_VARIABLES, false);

		Map<String, String> map = new HashMap<>(1);
		map.put("LAUNCHTEST", "IS SET");
		setLaunchAttribute(ILaunchManager.ATTR_ENVIRONMENT_VARIABLES, map);
		doLaunch();

		SyncUtil.runToLocation("envTest");
		MIStoppedEvent stoppedEvent = SyncUtil.step(2, StepType.STEP_OVER);

		// The program has stored the content of $LAUNCHTEST into a variable called 'launchTest'.
		// Let's verify this variable is set to "IS SET".
		final IExpressionDMContext exprDmc = SyncUtil.createExpression(stoppedEvent.getDMContext(), "launchTest");
		Query<FormattedValueDMData> query = new Query<FormattedValueDMData>() {
			@Override
			protected void execute(DataRequestMonitor<FormattedValueDMData> rm) {
				fExpService.getFormattedExpressionValue(
						fExpService.getFormattedValueContext(exprDmc, MIExpressions.DETAILS_FORMAT), rm);
			}
		};
		{
			fExpService.getExecutor().execute(query);
			FormattedValueDMData value = query.get(TestsPlugin.massageTimeout(500), TimeUnit.MILLISECONDS);
			assertTrue("Expected a string ending with \"IS SET\" but got " + value.getFormattedValue(),
					value.getFormattedValue().trim().endsWith("\"IS SET\""));
		}

		// The program has stored the content of $HOME into a variable called 'home'.
		// Let's verify this variable is 0x0 which means it does not exist.
		final IExpressionDMContext exprDmc2 = SyncUtil.createExpression(stoppedEvent.getDMContext(), "home");
		Query<FormattedValueDMData> query2 = new Query<FormattedValueDMData>() {
			@Override
			protected void execute(DataRequestMonitor<FormattedValueDMData> rm) {
				fExpService.getFormattedExpressionValue(
						fExpService.getFormattedValueContext(exprDmc2, MIExpressions.DETAILS_FORMAT), rm);
			}
		};
		{
			fExpService.getExecutor().execute(query2);
			FormattedValueDMData value = query2.get(TestsPlugin.massageTimeout(500), TimeUnit.MILLISECONDS);
			assertTrue("Expected 0x0 but got " + value.getFormattedValue(), value.getFormattedValue().equals("0x0"));
		}
	}

	/**
	 * Repeat the test testClearingAndSettingEnvironment, but after a restart.
	 */
	@Test
	public void testClearingAndSettingEnvironmentRestart() throws Throwable {
		fRestart = true;
		testClearingAndSettingEnvironment();
	}

	/**
	 * This test will tell the launch to set some arguments for the program.  We will
	 * then check that the program has the same arguments.
	 *
	 * NOTE: The main setting arguments tests are in {@link CommandLineArgsTest}, this
	 * test remains here to test interaction of command line arguments are restarting.
	 * See {@link #testSettingArgumentsRestart()}
	 */
	@Test
	public void testSettingArguments() throws Throwable {
		setLaunchAttribute(ICDTLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, "1 2 3\n4 5 6");
		doLaunch();

		MIStoppedEvent stoppedEvent = getInitialStoppedEvent();

		// Check that argc is correct
		final IExpressionDMContext argcDmc = SyncUtil.createExpression(stoppedEvent.getDMContext(), "argc");
		Query<FormattedValueDMData> query = new Query<FormattedValueDMData>() {
			@Override
			protected void execute(DataRequestMonitor<FormattedValueDMData> rm) {
				fExpService.getFormattedExpressionValue(
						fExpService.getFormattedValueContext(argcDmc, MIExpressions.DETAILS_FORMAT), rm);
			}
		};
		{
			fExpService.getExecutor().execute(query);
			FormattedValueDMData value = query.get(TestsPlugin.massageTimeout(500), TimeUnit.MILLISECONDS);

			// Argc should be 7: the program name and the six arguments
			assertTrue("Expected 7 but got " + value.getFormattedValue(), value.getFormattedValue().trim().equals("7"));
		}

		// Check that argv is also correct.  For simplicity we only check the last argument
		final IExpressionDMContext argvDmc = SyncUtil.createExpression(stoppedEvent.getDMContext(), "argv[argc-1]");
		Query<FormattedValueDMData> query2 = new Query<FormattedValueDMData>() {
			@Override
			protected void execute(DataRequestMonitor<FormattedValueDMData> rm) {
				fExpService.getFormattedExpressionValue(
						fExpService.getFormattedValueContext(argvDmc, MIExpressions.DETAILS_FORMAT), rm);
			}
		};
		{
			fExpService.getExecutor().execute(query2);
			FormattedValueDMData value = query2.get(TestsPlugin.massageTimeout(500), TimeUnit.MILLISECONDS);
			assertTrue("Expected \"6\" but got " + value.getFormattedValue(),
					value.getFormattedValue().trim().endsWith("\"6\""));
		}
	}

	/**
	 * Repeat the test testSettingArguments, but after a restart.
	 */
	@Test
	public void testSettingArgumentsRestart() throws Throwable {
		fRestart = true;
		testSettingArguments();
	}

	/**
	 * This test will tell the launch to "stop on main" at method main(), which we will verify.
	 */
	@Test
	public void testStopAtMain() throws Throwable {
		setLaunchAttribute(ICDTLaunchConfigurationConstants.ATTR_DEBUGGER_STOP_AT_MAIN, true);
		setLaunchAttribute(ICDTLaunchConfigurationConstants.ATTR_DEBUGGER_STOP_AT_MAIN_SYMBOL, "main");
		doLaunch();

		MIStoppedEvent stoppedEvent = getInitialStoppedEvent();
		assertTrue(
				"Expected to stop at main:27 but got " + stoppedEvent.getFrame().getFunction() + ":"
						+ Integer.toString(stoppedEvent.getFrame().getLine()),
				stoppedEvent.getFrame().getFunction().equals("main") && stoppedEvent.getFrame().getLine() == 27);
	}

	/**
	 * Repeat the test testStopAtMain, but after a restart.
	 */
	@Test
	public void testStopAtMainRestart() throws Throwable {
		fRestart = true;
		testStopAtMain();
	}

	/**
	 * This test will tell the launch to "stop on main" at method stopAtOther(),
	 * which we will then verify.
	 */
	@Test
	public void testStopAtOther() throws Throwable {
		setLaunchAttribute(ICDTLaunchConfigurationConstants.ATTR_DEBUGGER_STOP_AT_MAIN, true);
		setLaunchAttribute(ICDTLaunchConfigurationConstants.ATTR_DEBUGGER_STOP_AT_MAIN_SYMBOL, "stopAtOther");
		doLaunch();

		MIStoppedEvent stoppedEvent = getInitialStoppedEvent();
		assertTrue("Expected to stop at stopAtOther but got " + stoppedEvent.getFrame().getFunction() + ":",
				stoppedEvent.getFrame().getFunction().equals("stopAtOther"));
	}

	/**
	 * Repeat the test testStopAtOther, but after a restart.
	 */
	@Test
	public void testStopAtOtherRestart() throws Throwable {
		fRestart = true;
		testStopAtOther();
	}

	/**
	 * This test will set a breakpoint at some place in the program and will tell
	 * the launch to NOT "stop on main".  We will verify that the first stop is
	 * at the breakpoint that we set.
	 */
	@Ignore
	@Test
	public void testNoStopAtMain() throws Throwable {
		setLaunchAttribute(ICDTLaunchConfigurationConstants.ATTR_DEBUGGER_STOP_AT_MAIN, false);
		// Set this one as well to make sure it gets ignored
		setLaunchAttribute(ICDTLaunchConfigurationConstants.ATTR_DEBUGGER_STOP_AT_MAIN_SYMBOL, "main");

		// We need to set the breakpoint before the launch is started, but the only way to do that is
		// to set it in the platorm.  Ok, but how do I get an IResource that points to my binary?
		// The current workspace is the JUnit runtime workspace instead of the workspace containing
		// the JUnit tests.

		IFile fakeFile = null;
		CDIDebugModel.createLineBreakpoint(EXEC_PATH + EXEC_NAME, fakeFile, ICBreakpointType.REGULAR,
				LAST_LINE_IN_MAIN + 1, true, 0, "", true); //$NON-NLS-1$
		doLaunch();

		MIStoppedEvent stoppedEvent = getInitialStoppedEvent();
		assertTrue("Expected to stop at envTest but got " + stoppedEvent.getFrame().getFunction() + ":",
				stoppedEvent.getFrame().getFunction().equals("envTest"));
	}

	/**
	 * Repeat the test testNoStopAtMain, but after a restart.
	 */
	@Ignore
	@Test
	public void testNoStopAtMainRestart() throws Throwable {
		fRestart = true;
		testNoStopAtMain();
	}

	/**
	 * Test that the exit code is available after the inferior as run to
	 * completion so that the console can use it.
	 */
	@Test
	public void testExitCodeSet() throws Throwable {
		assumeGdbVersionAtLeast(ITestConstants.SUFFIX_GDB_7_3);
		doLaunch();

		ServiceEventWaitor<ICommandControlShutdownDMEvent> shutdownEventWaitor = new ServiceEventWaitor<>(
				getGDBLaunch().getSession(), ICommandControlShutdownDMEvent.class);

		// The target is currently stopped.  We resume to get it running
		// and wait for a shutdown event to say execution has completed
		SyncUtil.resume();

		shutdownEventWaitor.waitForEvent(TestsPlugin.massageTimeout(1000));

		IProcess[] launchProcesses = getGDBLaunch().getProcesses();
		;
		for (IProcess proc : launchProcesses) {
			if (proc instanceof InferiorRuntimeProcess) {
				assertThat(proc.getAttribute(IGdbDebugConstants.INFERIOR_EXITED_ATTR), is(notNullValue()));

				// Wait for the process to terminate so we can obtain its exit code
				int count = 0;
				while (count++ < 100 && !proc.isTerminated()) {
					try {
						synchronized (proc) {
							proc.wait(10);
						}
					} catch (InterruptedException ie) {
					}
				}

				int exitValue = proc.getExitValue();
				assertThat(exitValue, is(TEST_EXIT_CODE));
				return;
			}
		}
		assert false;
	}

	/**
	 * This test will confirm that we have turned on "pending breakpoints"
	 * The pending breakpoint setting only affects CLI commands so we have
	 * to test with one.  We don't have classes to set breakpoints using CLI,
	 * but we do for tracepoints, which is the same for this test.
	 *
	 * The pending breakpoint feature only works with tracepoints starting
	 * with GDB 7.0.
	 *
	 * We could run this test before 7.0 but we would have to use a breakpoint
	 * set using CLI commands.
	 */
	@Test
	public void testPendingBreakpointSetting() throws Throwable {
		assumeGdbVersionAtLeast(ITestConstants.SUFFIX_GDB_7_0);
		doLaunch();
		MIStoppedEvent stoppedEvent = getInitialStoppedEvent();

		final IBreakpointsTargetDMContext bpTargetDmc = DMContexts.getAncestorOfType(stoppedEvent.getDMContext(),
				IBreakpointsTargetDMContext.class);
		Query<MIBreakListInfo> query = new Query<MIBreakListInfo>() {
			@Override
			protected void execute(final DataRequestMonitor<MIBreakListInfo> rm) {
				fGdbControl.queueCommand(fGdbControl.getCommandFactory().createCLITrace(bpTargetDmc, "invalid", ""),
						new ImmediateDataRequestMonitor<CLITraceInfo>(rm) {
							@Override
							protected void handleSuccess() {
								fGdbControl.queueCommand(fGdbControl.getCommandFactory().createMIBreakList(bpTargetDmc),
										new ImmediateDataRequestMonitor<MIBreakListInfo>(rm) {
											@Override
											protected void handleSuccess() {
												rm.setData(getData());
												rm.done();
											}
										});
							}
						});
			}
		};
		{
			fExpService.getExecutor().execute(query);
			MIBreakListInfo value = query.get(TestsPlugin.massageTimeout(500), TimeUnit.MILLISECONDS);
			MIBreakpoint[] bps = value.getMIBreakpoints();
			assertTrue("Expected 1 breakpoint but got " + bps.length, bps.length == 1);
			assertTrue("Expending a <PENDING> breakpoint but got one at " + bps[0].getAddress(),
					bps[0].getAddress().equals("<PENDING>"));
		}
	}

	/**
	 * This test will tell the launch to "stop on main" at method main() with reverse
	 * debugging enabled.  We will verify that the launch stops at main() and that
	 * reverse debugging is enabled.
	 *
	 * In this test, the execution crosses getenv() while recording is enabled. gdb 7.0
	 * and 7.1 have trouble with that. We disable the test for those, and enable it for
	 * 7.2 and upwards.
	 */
	@Test
	public void testStopAtMainWithReverse() throws Throwable {
		assumeGdbVersionAtLeast(ITestConstants.SUFFIX_GDB_7_2);
		setLaunchAttribute(ICDTLaunchConfigurationConstants.ATTR_DEBUGGER_STOP_AT_MAIN, true);
		setLaunchAttribute(ICDTLaunchConfigurationConstants.ATTR_DEBUGGER_STOP_AT_MAIN_SYMBOL, "main");
		setLaunchAttribute(IGDBLaunchConfigurationConstants.ATTR_DEBUGGER_REVERSE, true);
		doLaunch();

		MIStoppedEvent stoppedEvent = getInitialStoppedEvent();
		// Make sure we stopped at the first line of main
		assertTrue(
				"Expected to stop at main:" + FIRST_LINE_IN_MAIN + " but got " + stoppedEvent.getFrame().getFunction()
						+ ":" + Integer.toString(stoppedEvent.getFrame().getLine()),
				stoppedEvent.getFrame().getFunction().equals("main")
						&& stoppedEvent.getFrame().getLine() == FIRST_LINE_IN_MAIN);

		// Step a couple of times and check where we are
		final int NUM_STEPS = 3;
		stoppedEvent = SyncUtil.step(NUM_STEPS, StepType.STEP_OVER);
		assertTrue("Expected to stop at main:" + (FIRST_LINE_IN_MAIN + NUM_STEPS) + " but got "
				+ stoppedEvent.getFrame().getFunction() + ":" + Integer.toString(stoppedEvent.getFrame().getLine()),
				stoppedEvent.getFrame().getFunction().equals("main")
						&& stoppedEvent.getFrame().getLine() == FIRST_LINE_IN_MAIN + NUM_STEPS);

		// Now step backwards to make sure reverse was enabled

		final ServiceEventWaitor<MIStoppedEvent> eventWaitor = new ServiceEventWaitor<>(fSession,
				MIStoppedEvent.class);

		final int REVERSE_NUM_STEPS = 2;
		final IExecutionDMContext execDmc = stoppedEvent.getDMContext();
		Query<MIInfo> query = new Query<MIInfo>() {
			@Override
			protected void execute(DataRequestMonitor<MIInfo> rm) {
				fGdbControl.queueCommand(
						fGdbControl.getCommandFactory().createMIExecReverseNext(execDmc, REVERSE_NUM_STEPS), rm);
			}
		};
		{
			fGdbControl.getExecutor().execute(query);
			query.get(TestsPlugin.massageTimeout(500), TimeUnit.MILLISECONDS);
		}

		stoppedEvent = eventWaitor.waitForEvent(TestsPlugin.massageTimeout(1000));

		assertTrue("Expected to stop at main:" + (FIRST_LINE_IN_MAIN + NUM_STEPS - REVERSE_NUM_STEPS) + " but got "
				+ stoppedEvent.getFrame().getFunction() + ":" + Integer.toString(stoppedEvent.getFrame().getLine()),
				stoppedEvent.getFrame().getFunction().equals("main")
						&& stoppedEvent.getFrame().getLine() == FIRST_LINE_IN_MAIN + NUM_STEPS - REVERSE_NUM_STEPS);
	}

	/**
	 * Repeat the test testStopAtMainWithReverse, but after a restart.
	 */
	@Test
	public void testStopAtMainWithReverseRestart() throws Throwable {
		assumeGdbVersionAtLeast(ITestConstants.SUFFIX_GDB_7_2);
		fRestart = true;
		testStopAtMainWithReverse();
	}

	/**
	 * This test will tell the launch to "stop on main" at method stopAtOther(),
	 * with reverse debugging enabled.  We will then verify that the launch is properly
	 * stopped at stopAtOther() and that it can go backwards until main() (this will
	 * confirm that reverse debugging was enabled at the very start).
	 *
	 * In this test, the execution crosses getenv() while recording is enabled. gdb 7.0
	 * and 7.1 have trouble with that. We disable the test for those, and enable it for
	 * 7.2 and upwards.
	 */
	@Test
	public void testStopAtOtherWithReverse() throws Throwable {
		assumeGdbVersionAtLeast(ITestConstants.SUFFIX_GDB_7_2);
		setLaunchAttribute(ICDTLaunchConfigurationConstants.ATTR_DEBUGGER_STOP_AT_MAIN, true);
		setLaunchAttribute(ICDTLaunchConfigurationConstants.ATTR_DEBUGGER_STOP_AT_MAIN_SYMBOL, "stopAtOther");
		setLaunchAttribute(IGDBLaunchConfigurationConstants.ATTR_DEBUGGER_REVERSE, true);
		doLaunch();

		MIStoppedEvent stoppedEvent = getInitialStoppedEvent();

		// The initial stopped event is not the last stopped event.
		// With reverse we have to stop the program, turn on reverse and start it again.
		// Let's get the frame where we really are stopped right now.
		final IExecutionDMContext execDmc = stoppedEvent.getDMContext();
		IFrameDMData frame = SyncUtil.getFrameData(execDmc, 0);

		// Make sure we stopped at the first line of main
		assertTrue("Expected to stop at stopAtOther but got " + frame.getFunction(),
				frame.getFunction().equals("stopAtOther"));

		// Now step backwards all the way to the start to make sure reverse was enabled from the very start
		final ServiceEventWaitor<MIStoppedEvent> eventWaitor = new ServiceEventWaitor<>(fSession,
				MIStoppedEvent.class);

		final int REVERSE_NUM_STEPS = 3;
		Query<MIInfo> query2 = new Query<MIInfo>() {
			@Override
			protected void execute(DataRequestMonitor<MIInfo> rm) {
				fGdbControl.queueCommand(
						fGdbControl.getCommandFactory().createMIExecReverseNext(execDmc, REVERSE_NUM_STEPS), rm);
			}
		};
		{
			fGdbControl.getExecutor().execute(query2);
			query2.get(TestsPlugin.massageTimeout(500), TimeUnit.MILLISECONDS);
		}

		stoppedEvent = eventWaitor.waitForEvent(TestsPlugin.massageTimeout(1000));

		assertTrue(
				"Expected to stop at main:" + (FIRST_LINE_IN_MAIN) + " but got " + stoppedEvent.getFrame().getFunction()
						+ ":" + Integer.toString(stoppedEvent.getFrame().getLine()),
				stoppedEvent.getFrame().getFunction().equals("main")
						&& stoppedEvent.getFrame().getLine() == FIRST_LINE_IN_MAIN);
	}

	/**
	 * Repeat the test testStopAtOtherWithReverse, but after a restart.
	 */
	@Test
	@Ignore("Fails. Investigate what it needs to wait for.")
	public void testStopAtOtherWithReverseRestart() throws Throwable {
		assumeGdbVersionAtLeast(ITestConstants.SUFFIX_GDB_7_2);
		fRestart = true;
		testStopAtOtherWithReverse();
	}

	/**
	 * This test will set a breakpoint at the last line of the program and will tell
	 * the launch to NOT "stop on main", with reverse debugging enabled.  We will
	 * verify that the first stop is at the last line of the program but that the program
	 * can run backwards until main() (this will confirm that reverse debugging was
	 * enabled at the very start).
	 */
	@Test
	@Ignore("TODO: this is not working because it does not insert the breakpoint propertly")
	public void testNoStopAtMainWithReverse() throws Throwable {
		assumeGdbVersionAtLeast(ITestConstants.SUFFIX_GDB_7_2);
		setLaunchAttribute(ICDTLaunchConfigurationConstants.ATTR_DEBUGGER_STOP_AT_MAIN, false);
		// Set this one as well to make sure it gets ignored
		setLaunchAttribute(ICDTLaunchConfigurationConstants.ATTR_DEBUGGER_STOP_AT_MAIN_SYMBOL, "main");
		setLaunchAttribute(IGDBLaunchConfigurationConstants.ATTR_DEBUGGER_REVERSE, true);

		// MUST SET BREAKPOINT AT LAST LINE BUT BEFORE LAUNCH IS STARTED
		// MUST SET BREAKPOINT AT LAST LINE BUT BEFORE LAUNCH IS STARTED
		// MUST SET BREAKPOINT AT LAST LINE BUT BEFORE LAUNCH IS STARTED
		// see testNoStopAtMain()

		doLaunch();

		MIStoppedEvent stoppedEvent = getInitialStoppedEvent();

		// The initial stopped event is not the last stopped event.
		// With reverse we have to stop the program, turn on reverse and start it again.
		// Let's get the frame where we really are stopped right now.
		final IExecutionDMContext execDmc = stoppedEvent.getDMContext();
		IFrameDMData frame = SyncUtil.getFrameData(execDmc, 0);

		// Make sure we stopped at the first line of main
		assertTrue(
				"Expected to stop at main:" + LAST_LINE_IN_MAIN + " but got " + frame.getFunction() + ":"
						+ Integer.toString(frame.getLine()),
				frame.getFunction().equals("main") && frame.getLine() == LAST_LINE_IN_MAIN);

		// Now step backwards all the way to the start to make sure reverse was enabled from the very start
		final ServiceEventWaitor<MIStoppedEvent> eventWaitor = new ServiceEventWaitor<>(fSession,
				MIStoppedEvent.class);

		final int REVERSE_NUM_STEPS = 3;
		Query<MIInfo> query2 = new Query<MIInfo>() {
			@Override
			protected void execute(DataRequestMonitor<MIInfo> rm) {
				fGdbControl.queueCommand(
						fGdbControl.getCommandFactory().createMIExecReverseNext(execDmc, REVERSE_NUM_STEPS), rm);
			}
		};
		{
			fGdbControl.getExecutor().execute(query2);
			query2.get(TestsPlugin.massageTimeout(500), TimeUnit.MILLISECONDS);
		}

		stoppedEvent = eventWaitor.waitForEvent(TestsPlugin.massageTimeout(1000));

		assertTrue(
				"Expected to stop at main:" + (FIRST_LINE_IN_MAIN) + " but got " + stoppedEvent.getFrame().getFunction()
						+ ":" + Integer.toString(stoppedEvent.getFrame().getLine()),
				stoppedEvent.getFrame().getFunction().equals("main")
						&& stoppedEvent.getFrame().getLine() == FIRST_LINE_IN_MAIN);
	}

	/**
	 * Repeat the test testNoStopAtMainWithReverse, but after a restart.
	 * TODO: remove ignore when parent test is fixed
	 */
	@Test
	@Ignore
	public void testNoStopAtMainWithReverseRestart() throws Throwable {
		assumeGdbVersionAtLeast(ITestConstants.SUFFIX_GDB_7_2);
		fRestart = true;
		testNoStopAtMainWithReverse();
	}
}

Back to the top