Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 55c3642ac9aa9180aceb979ec8fd6d9abf28b4cf (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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
/*******************************************************************************
 * Copyright (c) 2010, 2014 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 API and implementation
 *   Dmitry Kozlov (Mentor Graphics) - Add support for IGDBTraceControl2 (Bug 390827)
 *******************************************************************************/
package org.eclipse.cdt.dsf.gdb.service;

import java.util.Hashtable;
import java.util.concurrent.TimeUnit;

import org.eclipse.cdt.dsf.concurrent.DataRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.ImmediateDataRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.ImmediateRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.Immutable;
import org.eclipse.cdt.dsf.concurrent.RequestMonitor;
import org.eclipse.cdt.dsf.datamodel.AbstractDMContext;
import org.eclipse.cdt.dsf.datamodel.AbstractDMEvent;
import org.eclipse.cdt.dsf.datamodel.DMContexts;
import org.eclipse.cdt.dsf.datamodel.IDMContext;
import org.eclipse.cdt.dsf.datamodel.IDMEvent;
import org.eclipse.cdt.dsf.debug.service.ICachingService;
import org.eclipse.cdt.dsf.debug.service.IRunControl.IContainerDMContext;
import org.eclipse.cdt.dsf.debug.service.IRunControl.IExecutionDMContext;
import org.eclipse.cdt.dsf.debug.service.command.CommandCache;
import org.eclipse.cdt.dsf.debug.service.command.ICommandControlService;
import org.eclipse.cdt.dsf.debug.service.command.ICommandControlService.ICommandControlDMContext;
import org.eclipse.cdt.dsf.gdb.internal.GdbPlugin;
import org.eclipse.cdt.dsf.gdb.internal.service.command.events.MITracepointSelectedEvent;
import org.eclipse.cdt.dsf.mi.service.IMICommandControl;
import org.eclipse.cdt.dsf.mi.service.IMIProcesses;
import org.eclipse.cdt.dsf.mi.service.command.CommandFactory;
import org.eclipse.cdt.dsf.mi.service.command.events.MIEvent;
import org.eclipse.cdt.dsf.mi.service.command.output.CLITraceDumpInfo;
import org.eclipse.cdt.dsf.mi.service.command.output.MIInfo;
import org.eclipse.cdt.dsf.mi.service.command.output.MIResultRecord;
import org.eclipse.cdt.dsf.mi.service.command.output.MITraceFindInfo;
import org.eclipse.cdt.dsf.mi.service.command.output.MITraceListVariablesInfo;
import org.eclipse.cdt.dsf.mi.service.command.output.MITraceListVariablesInfo.MITraceVariableInfo;
import org.eclipse.cdt.dsf.mi.service.command.output.MITraceStatusInfo;
import org.eclipse.cdt.dsf.mi.service.command.output.MITraceStopInfo;
import org.eclipse.cdt.dsf.service.AbstractDsfService;
import org.eclipse.cdt.dsf.service.DsfSession;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.osgi.framework.BundleContext;

/**
 * This class implements the IGDBTraceControl interface which gives access
 * to the debugger's tracing functionality.
 *
 * @since 3.0
 */
public class GDBTraceControl_7_2 extends AbstractDsfService implements IGDBTraceControl2, ICachingService {

	@Immutable
	protected static final class MITraceRecordDMContext extends AbstractDMContext implements ITraceRecordDMContext {

		// The trace record GDB reference
		private final String fReference;

		/**
		 * @param session    the DsfSession for this service
		 * @param parents    the parent contexts
		 * @param reference  the trace record reference
		 * @since 4.0
		 */
		public MITraceRecordDMContext(DsfSession session, ITraceTargetDMContext parent, String reference) {
			super(session.getId(), new IDMContext[] { parent });
			fReference = reference;
		}

		/** @since 4.0 */
		@Override
		public String getRecordId() {
			return fReference;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.cdt.dsf.datamodel.AbstractDMContext#equals(java.lang.Object)
		 */
		@Override
		public boolean equals(Object obj) {
			return baseEquals(obj) && (fReference == null ? ((MITraceRecordDMContext) obj).fReference == null
					: (fReference.equals(((MITraceRecordDMContext) obj).fReference)));
		}

		/* (non-Javadoc)
		 * @see org.eclipse.cdt.dsf.datamodel.AbstractDMContext#hashCode()
		 */
		@Override
		public int hashCode() {
			return baseHashCode() ^ (fReference == null ? 0 : fReference.hashCode());
		}

		/* (non-Javadoc)
		 * @see java.lang.Object#toString()
		 */
		@Override
		public String toString() {
			return baseToString() + ".reference(" + fReference + ")"; //$NON-NLS-1$//$NON-NLS-2$
		}
	}

	/**
	 * Trace record context used to indicate that there is no current trace record selected.
	 */
	@Immutable
	protected static final class InvalidTraceRecordDMContext extends AbstractDMContext
			implements ITraceRecordDMContext {

		/**
		 * @param session    the DsfSession for this service
		 * @param parents    the parent contexts
		 * @param reference  the trace record reference
		 */
		public InvalidTraceRecordDMContext(DsfSession session, ITraceTargetDMContext parent) {
			super(session.getId(), new IDMContext[] { parent });
		}

		/** @since 4.0 */
		@Override
		public String getRecordId() {
			return null;
		}

		/* (non-Javadoc)
		 * @see org.eclipse.cdt.dsf.datamodel.AbstractDMContext#equals(java.lang.Object)
		 */
		@Override
		public boolean equals(Object obj) {
			return baseEquals(obj);
		}

		/* (non-Javadoc)
		 * @see org.eclipse.cdt.dsf.datamodel.AbstractDMContext#hashCode()
		 */
		@Override
		public int hashCode() {
			return baseHashCode();
		}

		/* (non-Javadoc)
		 * @see java.lang.Object#toString()
		 */
		@Override
		public String toString() {
			return baseToString() + ".noTraceRecord"; //$NON-NLS-1$
		}
	}

	private class TraceVariableDMData implements ITraceVariableDMData {
		private String fName;
		private String fValue;
		private String fInitialValue;

		public TraceVariableDMData(String name, String initial, String value) {
			fName = name;
			fInitialValue = initial;
			fValue = value;
		}

		@Override
		public String getName() {
			return fName;
		}

		@Override
		public String getValue() {
			return fValue;
		}

		@Override
		public String getInitialValue() {
			return fInitialValue;
		}
	}

	private class TraceRecordDMData implements ITraceRecordDMData {
		private String fContent;
		private String fTracepointNum;
		private String fTimestamp;
		private String fFrameNumber;

		public TraceRecordDMData(String content, String tracepointNum, String frameNumber, String timestamp) {
			fContent = content;
			fTracepointNum = tracepointNum;
			fTimestamp = timestamp;
			fFrameNumber = frameNumber;
		}

		@Override
		public String getContent() {
			return fContent;
		}

		@Override
		public String getTracepointNumber() {
			return fTracepointNum;
		}

		@Override
		public String getRecordId() {
			return fFrameNumber;
		}

		@Override
		public String getTimestamp() {
			return fTimestamp;
		}
	}

	private class TraceStatusDMData implements ITraceStatusDMData2 {
		private MITraceStatusInfo fInfo;

		public TraceStatusDMData(MITraceStatusInfo info) {
			fInfo = info;
		}

		/**
		 * Create a status when tracing is not supported
		 */
		public TraceStatusDMData() {
			this(null);
		}

		@Override
		public int getFreeBufferSize() {
			if (fInfo == null)
				return 0;
			return fInfo.getFreeBufferSize();
		}

		@Override
		public int getNumberOfCreatedFrames() {
			if (fInfo == null)
				return 0;
			return fInfo.getNumberOfCreatedFrames();
		}

		@Override
		public int getNumberOfCollectedFrame() {
			if (fInfo == null)
				return 0;
			return fInfo.getNumberOfCollectedFrame();
		}

		@Override
		public int getTotalBufferSize() {
			if (fInfo == null)
				return 0;
			return fInfo.getTotalBufferSize();
		}

		@Override
		public boolean isTracingActive() {
			if (fInfo == null)
				return false;
			return fInfo.isTracingActive();
		}

		@Override
		public boolean isTracingSupported() {
			return fInfo != null;
		}

		@Override
		public boolean isCircularBuffer() {
			if (fInfo == null)
				return false;
			return fInfo.isCircularBuffer();
		}

		@Override
		public boolean isTracingFromFile() {
			if (fInfo == null)
				return false;
			return fInfo.isTracingFromFile();
		}

		@Override
		public boolean isDisconnectedTracingEnabled() {
			if (fInfo == null)
				return false;
			return fInfo.isDisconnectedTracingEnabled();
		}

		@Override
		public STOP_REASON_ENUM getStopReason() {
			if (fInfo == null)
				return null;
			return fInfo.getStopReason();
		}

		@Override
		public Integer getStoppingTracepoint() {
			if (fInfo.getStopReason() == null) {
				return null;
			}
			return fInfo.getStopTracepoint();
		}

		@Override
		public String getUserName() {
			if (fInfo == null)
				return ""; //$NON-NLS-1$
			return fInfo.getUserName() == null ? "" : fInfo.getUserName(); //$NON-NLS-1$
		}

		@Override
		public String getNotes() {
			if (fInfo == null)
				return ""; //$NON-NLS-1$
			return fInfo.getNotes() == null ? "" : fInfo.getNotes(); //$NON-NLS-1$
		}

		@Override
		public String getStartTime() {
			if (fInfo == null)
				return ""; //$NON-NLS-1$
			return fInfo.getStartTime() == null ? "" : fInfo.getStartTime(); //$NON-NLS-1$
		}

		@Override
		public String getStopTime() {
			if (fInfo == null)
				return ""; //$NON-NLS-1$
			return fInfo.getStopTime() == null ? "" : fInfo.getStopTime(); //$NON-NLS-1$
		}

		@Override
		public String getStopErrorDescription() {
			if (getStopReason() != STOP_REASON_ENUM.ERROR) {
				return null;
			}
			return fInfo.getStopErrorDescription();
		}

		@Override
		public String getTraceFile() {
			if (!isTracingFromFile()) {
				return null;
			}
			return fInfo.getTraceFile();
		}

		@Override
		public String getCurrentTraceFrameId() {
			// Not currently provided by -trace-status
			if (fCurrentRecordDmc instanceof MITraceRecordDMContext) {
				return ((MITraceRecordDMContext) fCurrentRecordDmc).getRecordId();
			}
			return null;
		}

		@Override
		public Integer getTracepointNumberForCurrentTraceFrame() {
			// Not currently provided by -trace-status
			if (getCurrentTraceFrameId() != null) {
				return fTracepointIndexForTraceRecord;
			}
			return null;
		}
	}

	private static class TracingSupportedChangeEvent extends AbstractDMEvent<ITraceTargetDMContext>
			implements ITracingSupportedChangeDMEvent {
		private final boolean fTracingSupported;

		public TracingSupportedChangeEvent(ITraceTargetDMContext context, boolean supported) {
			super(context);
			fTracingSupported = supported;
		}

		@Override
		public boolean isTracingSupported() {
			return fTracingSupported;
		}
	}

	private static class TracingStartedEvent extends AbstractDMEvent<ITraceTargetDMContext>
			implements ITracingStartedDMEvent {
		public TracingStartedEvent(ITraceTargetDMContext context) {
			super(context);
		}
	}

	private static class TracingStoppedEvent extends AbstractDMEvent<ITraceTargetDMContext>
			implements ITracingStoppedDMEvent {
		public TracingStoppedEvent(ITraceTargetDMContext context) {
			super(context);
		}
	}

	public static class TraceRecordSelectedChangedEvent extends AbstractDMEvent<ITraceRecordDMContext>
			implements ITraceRecordSelectedChangedDMEvent {
		final boolean fVisualModeEnabled;

		public TraceRecordSelectedChangedEvent(ITraceRecordDMContext context) {
			super(context);
			fVisualModeEnabled = !(context instanceof InvalidTraceRecordDMContext);
		}

		@Override
		public boolean isVisualizationModeEnabled() {
			return fVisualModeEnabled;
		}
	}

	private CommandCache fTraceStatusCache;
	private ICommandControlService fConnection;
	private CommandFactory fCommandFactory;
	private IGDBBackend fBackend;

	private ITraceRecordDMContext fCurrentRecordDmc;
	private int fTracepointIndexForTraceRecord;

	private boolean fIsTracingActive;
	private boolean fIsTracingCurrentlySupported;
	private boolean fIsTracingFeatureAvailable = true;
	private int fTraceRecordsStored;

	public GDBTraceControl_7_2(DsfSession session, ILaunchConfiguration config) {
		super(session);
	}

	/**
	 * This method initializes this service.
	 *
	 * @param requestMonitor
	 *            The request monitor indicating the operation is finished
	 */
	@Override
	public void initialize(final RequestMonitor requestMonitor) {
		super.initialize(new ImmediateRequestMonitor(requestMonitor) {
			@Override
			protected void handleSuccess() {
				doInitialize(requestMonitor);
			}
		});
	}

	/**
	 * This method initializes this service after our superclass's initialize()
	 * method succeeds.
	 *
	 * @param requestMonitor
	 *            The call-back object to notify when this service's
	 *            initialization is done.
	 */
	private void doInitialize(RequestMonitor requestMonitor) {
		// Register this service.
		register(new String[] { IGDBTraceControl.class.getName(), IGDBTraceControl2.class.getName() },
				new Hashtable<String, String>());

		fConnection = getServicesTracker().getService(ICommandControlService.class);
		fTraceStatusCache = new CommandCache(getSession(), fConnection);
		fTraceStatusCache.setContextAvailable(fConnection.getContext(), true);

		fBackend = getServicesTracker().getService(IGDBBackend.class);
		fCommandFactory = getServicesTracker().getService(IMICommandControl.class).getCommandFactory();

		requestMonitor.done();
	}

	/**
	 * This method shuts down this service. It unregisters the service, stops
	 * receiving service events, and calls the superclass shutdown() method to
	 * finish the shutdown process.
	 *
	 * @return void
	 */
	@Override
	public void shutdown(RequestMonitor requestMonitor) {
		unregister();
		super.shutdown(requestMonitor);
	}

	/**
	 * @return The bundle context of the plug-in to which this service belongs.
	 */
	@Override
	protected BundleContext getBundleContext() {
		return GdbPlugin.getBundleContext();
	}

	/** @since 4.4 */
	protected boolean isTracingCurrentlySupported() {
		return fIsTracingCurrentlySupported;
	}

	/** @since 4.4 */
	protected CommandCache getTraceStatusCache() {
		return fTraceStatusCache;
	}

	@Override
	public void canStartTracing(ITraceTargetDMContext context, final DataRequestMonitor<Boolean> rm) {
		if (context == null) {
			rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, INVALID_STATE, "Invalid context", null)); //$NON-NLS-1$
			rm.done();
			return;
		}

		if (fIsTracingCurrentlySupported == false) {
			rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, NOT_SUPPORTED, "Tracing not supported", null)); //$NON-NLS-1$
			rm.done();
			return;
		}

		if (fBackend.getSessionType() == SessionType.CORE) {
			rm.setData(false);
			rm.done();
			return;
		}

		if (fCurrentRecordDmc != null) {
			// We are visualizing data, no more tracing possible.
			rm.setData(false);
			rm.done();
			return;
		}

		rm.setData(true);
		rm.done();
	}

	@Override
	public void startTracing(final ITraceTargetDMContext context, final RequestMonitor rm) {
		if (context == null) {
			rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, INVALID_STATE, "Invalid context", null)); //$NON-NLS-1$
			rm.done();
			return;
		}

		canStartTracing(context, new DataRequestMonitor<Boolean>(getExecutor(), rm) {
			@Override
			protected void handleSuccess() {
				if (!getData()) {
					rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, INVALID_STATE, "Cannot start tracing", //$NON-NLS-1$
							null));
					rm.done();
					return;
				}

				fConnection.queueCommand(fCommandFactory.createMITraceStart(context),
						new DataRequestMonitor<MIInfo>(getExecutor(), rm) {
							@Override
							protected void handleSuccess() {
								fTraceStatusCache.reset(context);

								fIsTracingActive = true;
								getSession().dispatchEvent(new TracingStartedEvent(context), getProperties());
								rm.done();
							}

							@Override
							protected void handleError() {
								// Send an event to cause a refresh of the button states
								IDMEvent<ITraceTargetDMContext> event;
								if (fIsTracingActive) {
									event = new TracingStartedEvent(context);
								} else {
									event = new TracingStoppedEvent(context);
								}
								getSession().dispatchEvent(event, getProperties());
								rm.done();
							}
						});
			}
		});
	}

	@Override
	public void canStopTracing(ITraceTargetDMContext context, DataRequestMonitor<Boolean> rm) {
		if (context == null) {
			rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, INVALID_STATE, "Invalid context", null)); //$NON-NLS-1$
			rm.done();
			return;
		}

		if (fIsTracingCurrentlySupported == false) {
			rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, NOT_SUPPORTED, "Tracing not supported", null)); //$NON-NLS-1$
			rm.done();
			return;
		}

		if (fBackend.getSessionType() == SessionType.CORE) {
			rm.setData(false);
			rm.done();
			return;
		}

		if (fCurrentRecordDmc != null) {
			// We are visualizing data, no more tracing possible.
			rm.setData(false);
			rm.done();
			return;
		}

		isTracing(context, rm);
	}

	@Override
	public void stopTracing(final ITraceTargetDMContext context, final RequestMonitor rm) {
		if (context == null) {
			rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, INVALID_STATE, "Invalid context", null)); //$NON-NLS-1$
			rm.done();
			return;
		}

		canStopTracing(context, new DataRequestMonitor<Boolean>(getExecutor(), rm) {
			@Override
			protected void handleSuccess() {
				if (!getData()) {
					rm.setStatus(
							new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, INVALID_STATE, "Cannot stop tracing", null)); //$NON-NLS-1$
					rm.done();
					return;
				}

				fConnection.queueCommand(fCommandFactory.createMITraceStop(context),
						new DataRequestMonitor<MITraceStopInfo>(getExecutor(), rm) {
							@Override
							protected void handleSuccess() {
								fTraceStatusCache.reset(context);

								MITraceStopInfo info = getData();

								// Update the tracing state in case it was stopped by the backend
								if (fIsTracingActive != info.isTracingActive()) {
									fIsTracingActive = info.isTracingActive();
									if (!fIsTracingActive) {
										getSession().dispatchEvent(new TracingStoppedEvent(context), getProperties());
									}
								}

								fTraceRecordsStored = info.getNumberOfCollectedFrame();
								rm.done();
							}
						});
			}
		});
	}

	@Override
	public void isTracing(ITraceTargetDMContext context, final DataRequestMonitor<Boolean> rm) {
		if (context == null) {
			rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, INVALID_STATE, "Invalid context", null)); //$NON-NLS-1$
			rm.done();
			return;
		}

		if (fBackend.getSessionType() == SessionType.CORE) {
			rm.setData(false);
			rm.done();
			return;
		}

		// Although tracing can be automatically stopped on the target, we
		// don't go to the backend for this call, or we would make too many calls
		// Instead, we can use our buffered state; we simply won't know about an
		// automatic stop until a forced refresh.  (Note that the MI notification
		// about automatic stops, is not available until GDB 7.2 is released)
		rm.setData(fIsTracingActive);
		rm.done();
	}

	@Override
	public void canSaveTraceData(ITraceTargetDMContext context, DataRequestMonitor<Boolean> rm) {
		if (context == null) {
			rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, INVALID_STATE, "Invalid context", null)); //$NON-NLS-1$
			rm.done();
			return;
		}

		if (fIsTracingCurrentlySupported == false) {
			rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, NOT_SUPPORTED, "Tracing not supported", null)); //$NON-NLS-1$
			rm.done();
			return;
		}

		if (fBackend.getSessionType() == SessionType.CORE) {
			rm.setData(false);
			rm.done();
			return;
		}

		rm.setData(fTraceRecordsStored > 0);
		rm.done();
	}

	@Override
	public void saveTraceData(final ITraceTargetDMContext context, final String file, final boolean remoteSave,
			final RequestMonitor rm) {
		if (context == null) {
			rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, INVALID_STATE, "Invalid context", null)); //$NON-NLS-1$
			rm.done();
			return;
		}

		canSaveTraceData(context, new DataRequestMonitor<Boolean>(getExecutor(), rm) {
			@Override
			protected void handleSuccess() {
				if (!getData()) {
					rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, INVALID_STATE, "Cannot save trace data", //$NON-NLS-1$
							null));
					rm.done();
					return;
				}

				fConnection.queueCommand(fCommandFactory.createMITraceSave(context, file, remoteSave),
						new DataRequestMonitor<MIInfo>(getExecutor(), rm));
			}
		});
	}

	@Override
	public void canLoadTraceData(ITraceTargetDMContext context, DataRequestMonitor<Boolean> rm) {
		if (context == null) {
			rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, INVALID_STATE, "Invalid context", null)); //$NON-NLS-1$
			rm.done();
			return;
		}

		// If this service has been instantiated, it means we support loading trace data.
		// Unlike the other operations, loading trace data does not require any GDB special state
		// (like being connected to a target)
		rm.setData(true);
		rm.done();
	}

	@Override
	public void loadTraceData(final ITraceTargetDMContext context, final String file, final RequestMonitor rm) {
		if (context == null) {
			rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, INVALID_STATE, "Invalid context", null)); //$NON-NLS-1$
			rm.done();
			return;
		}

		canLoadTraceData(context, new DataRequestMonitor<Boolean>(getExecutor(), rm) {
			@Override
			protected void handleSuccess() {
				if (!getData()) {
					rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, INVALID_STATE, "Cannot load trace data", //$NON-NLS-1$
							null));
					rm.done();
					return;
				}

				fConnection.queueCommand(fCommandFactory.createMITargetSelectTFile(context, file),
						new DataRequestMonitor<MIInfo>(getExecutor(), rm) {
							@Override
							protected void handleSuccess() {
								fTraceStatusCache.reset(context);

								fIsTracingCurrentlySupported = true;
								// Workaround for GDB pre-release where we don't get the details
								// of the frame when we load a trace file.
								// To get around this, we can force a select of record 0
								final ITraceRecordDMContext initialRecord = createTraceRecordContext(context, "0"); //$NON-NLS-1$
								selectTraceRecord(initialRecord, new ImmediateRequestMonitor(rm) {
									@Override
									protected void handleSuccess() {
										// This event will indicate to the other services that we are visualizing trace data.
										getSession().dispatchEvent(new TraceRecordSelectedChangedEvent(initialRecord),
												getProperties());
										rm.done();
									}
								});
							}
						});
			}
		});
	}

	@Override
	public void getTraceStatus(final ITraceTargetDMContext context, final DataRequestMonitor<ITraceStatusDMData> rm) {
		if (context == null) {
			rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, INVALID_STATE, "Invalid context", null)); //$NON-NLS-1$
			rm.done();
			return;
		}

		if (fIsTracingFeatureAvailable == false) {
			rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, NOT_SUPPORTED, "Tracing not supported", null)); //$NON-NLS-1$
			rm.done();
			return;
		}

		// Start an automatic one-time flushing of the TraceStatusCache.
		// This avoids sending -trace-status multiples time in a very short
		// amount of time.  We still have to clear the cache very quickly
		// because -trace-status can change very fast as it reports
		// the number of frames collected.  Having a small interval of
		// stale data is currently not a big deal, and not user-visible.
		// Bug 353034
		getExecutor().schedule(new Runnable() {
			@Override
			public void run() {
				fTraceStatusCache.reset(context);
			}
		}, 300, TimeUnit.MILLISECONDS);

		fTraceStatusCache.execute(fCommandFactory.createMITraceStatus(context),
				new DataRequestMonitor<MITraceStatusInfo>(getExecutor(), rm) {
					@Override
					protected void handleError() {
						// The MI command
						fIsTracingFeatureAvailable = false;
						super.handleError();
					}

					@Override
					protected void handleSuccess() {
						MITraceStatusInfo info = getData();

						if (fIsTracingCurrentlySupported != info.isTracingSupported()) {
							fIsTracingCurrentlySupported = info.isTracingSupported();
							getSession().dispatchEvent(
									new TracingSupportedChangeEvent(context, fIsTracingCurrentlySupported),
									getProperties());
						}

						if (fIsTracingCurrentlySupported) {
							// Update the tracing state in case it was stopped by the backend
							if (fIsTracingActive != info.isTracingActive()) {
								fIsTracingActive = info.isTracingActive();
								if (fIsTracingActive) {
									getSession().dispatchEvent(new TracingStartedEvent(context), getProperties());
								} else {
									getSession().dispatchEvent(new TracingStoppedEvent(context), getProperties());
								}
							}

							fTraceRecordsStored = info.getNumberOfCollectedFrame();

							rm.setData(new TraceStatusDMData(info));
						} else {
							fTraceRecordsStored = 0;
							fIsTracingActive = false;
							rm.setData(new TraceStatusDMData());
						}
						rm.done();
					}
				});
	}

	@Override
	public void createTraceVariable(ITraceTargetDMContext context, String varName, String varValue, RequestMonitor rm) {
		if (context == null) {
			rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, INVALID_STATE, "Invalid context", null)); //$NON-NLS-1$
			rm.done();
			return;
		}

		if (fIsTracingCurrentlySupported == false) {
			rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, NOT_SUPPORTED, "Tracing not supported", null)); //$NON-NLS-1$
			rm.done();
			return;
		}

		if (varValue == null) {
			fConnection.queueCommand(fCommandFactory.createMITraceDefineVariable(context, varName),
					new DataRequestMonitor<MIInfo>(getExecutor(), rm));
		} else {
			fConnection.queueCommand(fCommandFactory.createMITraceDefineVariable(context, varName, varValue),
					new DataRequestMonitor<MIInfo>(getExecutor(), rm));
		}
	}

	@Override
	public void getTraceVariables(ITraceTargetDMContext context, final DataRequestMonitor<ITraceVariableDMData[]> rm) {
		if (context == null) {
			rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, INVALID_STATE, "Invalid context", null)); //$NON-NLS-1$
			rm.done();
			return;
		}

		if (fIsTracingCurrentlySupported == false) {
			rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, NOT_SUPPORTED, "Tracing not supported", null)); //$NON-NLS-1$
			rm.done();
			return;
		}

		// It may be possible to cache this call, if we can figure out that all the cases
		// where to data can change, to clear the cache in those cases
		fConnection.queueCommand(fCommandFactory.createMITraceListVariables(context),
				new DataRequestMonitor<MITraceListVariablesInfo>(getExecutor(), rm) {
					@Override
					protected void handleSuccess() {
						MITraceVariableInfo[] vars = getData().getTraceVariables();
						TraceVariableDMData[] varDataArray = new TraceVariableDMData[vars.length];
						for (int i = 0; i < vars.length; i++) {
							varDataArray[i] = new TraceVariableDMData(vars[i].getName(), vars[i].getInitialValue(),
									vars[i].getCurrentValue());
						}

						rm.setData(varDataArray);
						rm.done();
					}
				});
	}

	/**
	 * Create a trace record context
	 * @since 4.0
	 */
	@Override
	public ITraceRecordDMContext createTraceRecordContext(ITraceTargetDMContext ctx, String recordId) {
		return new MITraceRecordDMContext(getSession(), ctx, recordId);
	}

	@Override
	public ITraceRecordDMContext createNextRecordContext(ITraceRecordDMContext ctx) {
		ITraceTargetDMContext targetDmc = DMContexts.getAncestorOfType(ctx, ITraceTargetDMContext.class);
		if (ctx instanceof InvalidTraceRecordDMContext) {
			// No specified context, so we return the context for the first
			return createTraceRecordContext(targetDmc, "0"); //$NON-NLS-1$
		}
		if (ctx instanceof MITraceRecordDMContext) {
			String recordId = ((MITraceRecordDMContext) ctx).getRecordId();
			int recordIndex = Integer.parseInt(recordId);
			recordIndex++;
			if (recordIndex == fTraceRecordsStored) {
				// Loop back to the front
				recordIndex = 0;
			}
			return new MITraceRecordDMContext(getSession(), targetDmc, Integer.toString(recordIndex));
		}
		return null;
	}

	@Override
	public ITraceRecordDMContext createPrevRecordContext(ITraceRecordDMContext ctx) {
		if (ctx instanceof MITraceRecordDMContext) {
			ITraceTargetDMContext targetDmc = DMContexts.getAncestorOfType(ctx, ITraceTargetDMContext.class);
			String recordId = ((MITraceRecordDMContext) ctx).getRecordId();
			int recordIndex = Integer.parseInt(recordId);
			if (recordIndex == 0) {
				// Loop back to the end
				recordIndex = fTraceRecordsStored; // The last index of a trace record (zero-based)
			}
			recordIndex--;
			return new MITraceRecordDMContext(getSession(), targetDmc, Integer.toString(recordIndex));
		}
		return null;
	}

	@Override
	public void getCurrentTraceRecordContext(ITraceTargetDMContext context,
			DataRequestMonitor<ITraceRecordDMContext> drm) {
		if (fCurrentRecordDmc == null) {
			drm.setData(new InvalidTraceRecordDMContext(getSession(), context));
		} else {
			drm.setData(fCurrentRecordDmc);
		}
		drm.done();
	}

	@Override
	public void selectTraceRecord(final ITraceRecordDMContext context, final RequestMonitor rm) {
		if (fIsTracingCurrentlySupported == false) {
			rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, NOT_SUPPORTED, "Tracing not supported", null)); //$NON-NLS-1$
			rm.done();
			return;
		}

		if (context instanceof MITraceRecordDMContext) {
			ITraceTargetDMContext targetDmc = DMContexts.getAncestorOfType(context, ITraceTargetDMContext.class);
			String recordId = ((MITraceRecordDMContext) context).getRecordId();
			final int reference = Integer.parseInt(recordId);

			if (reference < 0) {
				// This was the old way to indicate that we want to exit visualization mode.
				// We continue supporting it for backward compatibility
				stopTraceVisualization(targetDmc, rm);
				return;
			}

			fConnection.queueCommand(fCommandFactory.createMITraceFindFrameNumber(targetDmc, reference),
					new DataRequestMonitor<MITraceFindInfo>(getExecutor(), rm) {
						@Override
						protected void handleSuccess() {
							if (getData().isFound() == false) {
								rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, INTERNAL_ERROR,
										"Could not find trace record", null)); //$NON-NLS-1$
								rm.done();
								return;
							}

							fCurrentRecordDmc = context;
							fTracepointIndexForTraceRecord = getData().getTraceRecord().getTracepointId();

							// We could rely on the TraceRecordSelectedChangedEvent to update all the views, but this
							// would require a lot of changes.
							// Notice that looking at a new trace record should behave in the same manner
							// as when the debugger suspends during normal execution; therefore we can simply
							// trigger an MIStoppedEvent, as if reported by GDB.  Note that we do this already for
							// cases where GDB is missing such a event (like older versions of GDB when using a CLI command)
							IMIProcesses procService = getServicesTracker().getService(IMIProcesses.class);
							if (procService == null) {
								rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, INTERNAL_ERROR,
										"Could not find necessary services", null)); //$NON-NLS-1$
								rm.done();
								return;
							}

							final MIResultRecord rr = getData().getMIOutput().getMIResultRecord();
							if (rr == null) {
								assert false;
								rm.done();
								return;
							}

							// First find the process we are using.
							ICommandControlDMContext controlDmc = DMContexts.getAncestorOfType(context,
									ICommandControlDMContext.class);
							procService.getProcessesBeingDebugged(controlDmc,
									new DataRequestMonitor<IDMContext[]>(getExecutor(), rm) {
										@Override
										protected void handleSuccess() {
											assert getData() != null;
											assert getData().length == 1;

											if (getData() == null || getData().length < 1) {
												rm.done();
												return;
											}

											// Choose the first process for now, until gdb can tell
											// us which process the trace record is associated with.
											// Or maybe GDB already tells us by only reporting a single
											// process?
											IContainerDMContext processContainerDmc = (IContainerDMContext) (getData()[0]);

											// Now find the proper thread.  We must do this here because in post-mortem debugging
											// we cannot rely on MIRunControl using 'thread', as it will fail
											IMIProcesses procService = getServicesTracker()
													.getService(IMIProcesses.class);
											if (procService == null) {
												rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID,
														INTERNAL_ERROR, "Could not find necessary services", null)); //$NON-NLS-1$
												rm.done();
												return;
											}

											procService.getProcessesBeingDebugged(processContainerDmc,
													new DataRequestMonitor<IDMContext[]>(getExecutor(), rm) {
														@Override
														protected void handleSuccess() {
															assert getData() != null;
															assert getData().length == 1;

															if (getData() == null || getData().length < 1) {
																rm.done();
																return;
															}

															IExecutionDMContext execDmc = (IExecutionDMContext) getData()[0];
															MIEvent<?> event = MITracepointSelectedEvent.parse(execDmc,
																	rr.getToken(), rr.getMIResults());
															getSession().dispatchEvent(event, getProperties());

															rm.done();
														}
													});
										}
									});
						}
					});
		} else {
			rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, INTERNAL_ERROR, "Invalid trace record context.", //$NON-NLS-1$
					null));
			rm.done();
		}
	}

	/** @since 4.4 */
	@Override
	public void stopTraceVisualization(final ITraceTargetDMContext context, final RequestMonitor rm) {
		if (fIsTracingCurrentlySupported == false) {
			rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, NOT_SUPPORTED, "Tracing not supported", null)); //$NON-NLS-1$
			rm.done();
			return;
		}

		if (fBackend.getSessionType() == SessionType.CORE) {
			rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, NOT_SUPPORTED,
					"Cannot stop visualizing for a post mortem session", null)); //$NON-NLS-1$
			rm.done();
			return;
		}

		fConnection.queueCommand(fCommandFactory.createMITraceFindNone(context),
				new DataRequestMonitor<MITraceFindInfo>(getExecutor(), rm) {
					@Override
					protected void handleSuccess() {
						assert getData().isFound() == false;
						fCurrentRecordDmc = null;
						// This event will indicate to the other services that we are no longer visualizing trace data.
						ITraceRecordDMContext invalidDmc = new InvalidTraceRecordDMContext(getSession(), context);
						getSession().dispatchEvent(new TraceRecordSelectedChangedEvent(invalidDmc), getProperties());

						rm.done();
						return;
					}
				});
	}

	@Override
	public void getTraceRecordData(final ITraceRecordDMContext context,
			final DataRequestMonitor<ITraceRecordDMData> rm) {
		if (context instanceof MITraceRecordDMContext) {

			RequestMonitor tdumpRm = new ImmediateRequestMonitor(rm) {
				@Override
				protected void handleSuccess() {
					fConnection.queueCommand(fCommandFactory.createCLITraceDump(context),
							new DataRequestMonitor<CLITraceDumpInfo>(getExecutor(), rm) {
								@Override
								protected void handleSuccess() {

									TraceRecordDMData data = new TraceRecordDMData(getData().getContent(),
											getData().getTracepointNumber(), getData().getFrameNumber(),
											getData().getTimestamp());
									rm.setData(data);
									rm.done();
								}
							});
				}
			};

			// If we are pointing to the right context, we can do the tdump right away,
			// if not, we should first select the record.
			// This is because 'tdump' does not take any parameters to specify
			// which record we want to dump.
			if (fCurrentRecordDmc.equals(context)) {
				tdumpRm.done();
			} else {
				selectTraceRecord(context, tdumpRm);
			}
		} else {
			rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, INTERNAL_ERROR, "Invalid trace record context.", //$NON-NLS-1$
					null));
			rm.done();
		}
	}

	/** @since 4.4 */
	@Override
	public void setCircularTraceBuffer(final ITraceTargetDMContext context, boolean useCircularBuffer,
			final RequestMonitor rm) {
		if (context == null) {
			rm.done(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, INVALID_STATE, "Invalid context", null)); //$NON-NLS-1$
			return;
		}

		if (fIsTracingCurrentlySupported == false) {
			rm.done(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, NOT_SUPPORTED, "Tracing not supported", null)); //$NON-NLS-1$
			return;
		}

		fConnection.queueCommand(fCommandFactory.createMIGDBSetCircularTraceBuffer(context, useCircularBuffer),
				new ImmediateDataRequestMonitor<MIInfo>(rm) {
					@Override
					protected void handleSuccess() {
						fTraceStatusCache.reset(context);
						rm.done();
					}
				});
	}

	/** @since 4.4 */
	@Override
	public void setDisconnectedTracing(final ITraceTargetDMContext context, boolean disconnectedTracing,
			final RequestMonitor rm) {
		if (context == null) {
			rm.done(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, INVALID_STATE, "Invalid context", null)); //$NON-NLS-1$
			return;
		}

		if (fIsTracingCurrentlySupported == false) {
			rm.done(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, NOT_SUPPORTED, "Tracing not supported", null)); //$NON-NLS-1$
			return;
		}

		fConnection.queueCommand(fCommandFactory.createMIGDBSetDisconnectedTracing(context, disconnectedTracing),
				new ImmediateDataRequestMonitor<MIInfo>(rm) {
					@Override
					protected void handleSuccess() {
						fTraceStatusCache.reset(context);
						rm.done();
					}
				});
	}

	/** @since 4.4 */
	@Override
	public void setTraceUser(ITraceTargetDMContext context, String userName, RequestMonitor rm) {
		// Only supported started with GDB 7.4
		rm.done(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, INVALID_STATE, Messages.ErrorNotSupported, null));
	}

	/** @since 4.4 */
	@Override
	public void setTraceNotes(ITraceTargetDMContext context, String note, RequestMonitor rm) {
		// Only supported started with GDB 7.4
		rm.done(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, INVALID_STATE, Messages.ErrorNotSupported, null));
	}

	@Override
	public void flushCache(IDMContext context) {
		fTraceStatusCache.reset(context);
	}
}

Back to the top