Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b5c7c4606fc3ca3c3c25cef75a09f9d2898bd0bc (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
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
/*******************************************************************************
 * Copyright (c) 2012, 2016 Mentor Graphics and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * 		Mentor Graphics - Initial API and implementation
 * 		Salvatore Culcasi (ST) - Bug 407163 - GDB Console: breakpoint not added with MinGW and gdb
 *      Marc Khouzam (Ericsson) - Update breakpoint handling for GDB >= 7.4 (Bug 389945)
 *      Marc Khouzam (Ericsson) - Support for dynamic printf (Bug 400628)
 *      Jonah Graham (Kichwa Coders) - Bug 317173 - cleanup warnings
 *******************************************************************************/

package org.eclipse.cdt.dsf.mi.service;

import java.io.File;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.cdt.core.IAddress;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.debug.core.CDIDebugModel;
import org.eclipse.cdt.debug.core.breakpointactions.BreakpointActionManager;
import org.eclipse.cdt.debug.core.model.ICAddressBreakpoint;
import org.eclipse.cdt.debug.core.model.ICBreakpoint;
import org.eclipse.cdt.debug.core.model.ICBreakpointType;
import org.eclipse.cdt.debug.core.model.ICDynamicPrintf;
import org.eclipse.cdt.debug.core.model.ICFunctionBreakpoint;
import org.eclipse.cdt.debug.core.model.ICLineBreakpoint;
import org.eclipse.cdt.debug.core.model.ICTracepoint;
import org.eclipse.cdt.debug.core.model.ICWatchpoint;
import org.eclipse.cdt.dsf.concurrent.ConfinedToDsfExecutor;
import org.eclipse.cdt.dsf.concurrent.DataRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.ImmediateRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.RequestMonitor;
import org.eclipse.cdt.dsf.datamodel.DMContexts;
import org.eclipse.cdt.dsf.datamodel.IDMContext;
import org.eclipse.cdt.dsf.debug.service.IBreakpoints.IBreakpointDMData;
import org.eclipse.cdt.dsf.debug.service.IBreakpoints.IBreakpointsTargetDMContext;
import org.eclipse.cdt.dsf.debug.service.IDsfBreakpointExtension;
import org.eclipse.cdt.dsf.debug.service.IProcesses.IProcessDMContext;
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.IRunControl.IExitedDMEvent;
import org.eclipse.cdt.dsf.debug.service.ISourceLookup;
import org.eclipse.cdt.dsf.debug.service.ISourceLookup.ISourceLookupDMContext;
import org.eclipse.cdt.dsf.debug.service.command.ICommandControlService;
import org.eclipse.cdt.dsf.gdb.internal.GdbPlugin;
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.internal.tracepointactions.WhileSteppingAction;
import org.eclipse.cdt.dsf.mi.service.MIBreakpoints.MIBreakpointDMContext;
import org.eclipse.cdt.dsf.mi.service.MIBreakpointsManager.IMIBreakpointsTrackingListener;
import org.eclipse.cdt.dsf.mi.service.command.output.MIBreakpoint;
import org.eclipse.cdt.dsf.service.AbstractDsfService;
import org.eclipse.cdt.dsf.service.DsfServiceEventHandler;
import org.eclipse.cdt.dsf.service.DsfSession;
import org.eclipse.cdt.utils.Addr64;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.model.IBreakpoint;
import org.eclipse.debug.core.sourcelookup.containers.LocalFileStorage;
import org.osgi.framework.BundleContext;

/**
 * Provides synchronization between breakpoints set from outside of the Eclipse breakpoint 
 * framework (GDB console, trace files, etc.) and the Breakpoints view.
 * @since 4.2
 */
public class MIBreakpointsSynchronizer extends AbstractDsfService implements IMIBreakpointsTrackingListener {

	// Catchpoint expressions
    private static final String CE_EXCEPTION_CATCH = "exception catch"; //$NON-NLS-1$
    private static final String CE_EXCEPTION_THROW = "exception throw"; //$NON-NLS-1$
	
    // GDB tracepoint commands
    private static final String TC_COLLECT = "collect "; //$NON-NLS-1$
    private static final String TC_TEVAL = "teval "; //$NON-NLS-1$
    private static final String TC_WHILE_STEPPING = "while-stepping "; //$NON-NLS-1$
    private static final String TC_END = "end"; //$NON-NLS-1$
    
	private IMICommandControl fConnection;
	private MIBreakpoints fBreakpointsService;
	private MIBreakpointsManager fBreakpointsManager;

	/**
	 * Collection of the target contexts that are being tracked.
	 */
	private Set<IBreakpointsTargetDMContext> fTrackedTargets;

	/**
	 * Collection of breakpoints created from the GDB console or outside of Eclipse
	 */
	private Map<IBreakpointsTargetDMContext, Map<String, MIBreakpoint>> fCreatedTargetBreakpoints;

	/**
	 * Collection of breakpoints deleted from the GDB console or outside of Eclipse
	 */ 
	private Map<IBreakpointsTargetDMContext, Set<String>> fDeletedTargetBreakpoints;
	
	/**
	 * Collection of pending breakpoint modifications
	 */
	private Map<IBreakpointsTargetDMContext, Map<String, MIBreakpoint>> fPendingModifications;

	public MIBreakpointsSynchronizer(DsfSession session) {
		super(session);
		fTrackedTargets = new HashSet<IBreakpointsTargetDMContext>();
		fCreatedTargetBreakpoints = new HashMap<IBreakpointsTargetDMContext, Map<String, MIBreakpoint>>();
		fDeletedTargetBreakpoints = new HashMap<IBreakpointsTargetDMContext, Set<String>>();
		fPendingModifications = new HashMap<IBreakpointsTargetDMContext, Map<String, MIBreakpoint>>();
	}

	@Override
	protected BundleContext getBundleContext() {
        return GdbPlugin.getBundleContext();
	}

	@Override
	public void initialize(final RequestMonitor rm) {
		super.initialize(new ImmediateRequestMonitor(rm) {
			@Override
			protected void handleSuccess() {
				doInitialize(rm);
			}
		});
	}

	private void doInitialize(final RequestMonitor rm) {
		fConnection = getServicesTracker().getService(IMICommandControl.class);
		fBreakpointsService = getServicesTracker().getService(MIBreakpoints.class);
		fBreakpointsManager = getServicesTracker().getService(MIBreakpointsManager.class);
		if (fConnection == null || fBreakpointsService == null && fBreakpointsManager == null) {
			rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, "Service is not available")); //$NON-NLS-1$
			rm.done();
			return;
		}
		fBreakpointsManager.addBreakpointsTrackingListener(this);
		getSession().addServiceEventListener(this, null);

		// Register this service
		register(new String[] { 
					MIBreakpointsSynchronizer.class.getName() 
				 },
				 new Hashtable<String, String>());

		rm.done();
	}

	@Override
	public void shutdown(RequestMonitor rm) {
		fTrackedTargets.clear();
		fCreatedTargetBreakpoints.clear();
		fDeletedTargetBreakpoints.clear();
		fPendingModifications.clear();
		getSession().removeServiceEventListener(this);
		MIBreakpointsManager bm = getBreakpointsManager();
		if (bm != null) {
			bm.removeBreakpointsTrackingListener(this);
		}
        unregister();
		super.shutdown( rm );
	}

	@Override
	public void breakpointTrackingStarted(IBreakpointsTargetDMContext bpTargetDMC) {
		fTrackedTargets.add(bpTargetDMC);
	}

	@Override
	public void breakpointTrackingStopped(IBreakpointsTargetDMContext bpTargetDMC) {
		fTrackedTargets.remove(bpTargetDMC);
	}

	private IMICommandControl getCommandControl() {
		return fConnection;
	}

	private MIBreakpoints getBreakpointsService() {
		return fBreakpointsService;
	}

	private MIBreakpointsManager getBreakpointsManager() {
		return fBreakpointsManager;
	}

	public void targetBreakpointCreated(final MIBreakpoint miBpt) {
		if (isCatchpoint(miBpt)) {
			return;
		}
		ICommandControlService commandControl = getCommandControl();
		MIBreakpoints breakpointsService = getBreakpointsService();
		final MIBreakpointsManager bm = getBreakpointsManager();
		if (commandControl == null || breakpointsService == null || bm == null) {
			return;
		}

		final IBreakpointsTargetDMContext bpTargetDMC = getBreakpointsTargetContext(commandControl, miBpt);
		if (bpTargetDMC == null) {
			return;
		}

		// Store the target breakpoint data
		Map<String, MIBreakpointDMData> contextBreakpoints = breakpointsService.getBreakpointMap(bpTargetDMC);
		if (contextBreakpoints == null) {
			contextBreakpoints = breakpointsService.createNewBreakpointMap(bpTargetDMC);
		}
		contextBreakpoints.put(miBpt.getNumber(), new MIBreakpointDMData(miBpt));

		// Store the created target breakpoint to prevent setting it again on the target 
		// when addBreakpoint() is called.
		Map<String, MIBreakpoint> targetMap = fCreatedTargetBreakpoints.get(bpTargetDMC);
		if (targetMap == null) {
			targetMap = new HashMap<String, MIBreakpoint>();
			fCreatedTargetBreakpoints.put(bpTargetDMC, targetMap);
		}
		targetMap.put(miBpt.getNumber(), miBpt);

		// Convert the debug info file path into the file path in the local file system
    	String debuggerPath = getFileName(miBpt);
        getSource(
        	bpTargetDMC,
        	debuggerPath, 
        	new DataRequestMonitor<String>(getExecutor(), null) {
            	@Override
            	@ConfinedToDsfExecutor( "fExecutor" )
            	protected void handleSuccess() {
            		String fileName = getData();
            		if (fileName == null) {
            			fileName = getFileName(miBpt);
            		}
            		// Try to find matching platform breakpoint
        			ICBreakpoint plBpt = getPlatformBreakpoint(miBpt, fileName);
        			String threadId = miBpt.getThreadId();
        			boolean isThreadSpecific = threadId != null && !threadId.isEmpty() && !"0".equals(threadId); //$NON-NLS-1$
					try {
	        			if (plBpt == null) {
	        				// If matching platform breakpoint doesn't exist create a new one
							plBpt = createPlatformBreakpoint(fileName, miBpt);
		        			// If the target breakpoint is thread specific, update thread filters
							if (isThreadSpecific) {
								setThreadSpecificBreakpoint(plBpt, miBpt);
							}
	        			}
	        			else {
							// The corresponding platform breakpoint already exists. 
							// If the breakpoint tracking has already started we need
							// to notify MIBreakpointsManager which will increment its 
							// install count. 
							// Otherwise the breakpoint will be processed as an initial 
							// breakpoint when the breakpoint tracking starts.
							if (isBreakpointTargetTracked(bpTargetDMC)) {
			        			// If the target breakpoint is thread specific, update thread filters
								if (isThreadSpecific) {
									setThreadSpecificBreakpoint(plBpt, miBpt);
								}
								bm.breakpointAdded(plBpt);
							}
	        			}
						// Make sure the platform breakpoint's parameters are synchronized 
						// with the target breakpoint.
						Map<String, MIBreakpoint> map = fPendingModifications.get(bpTargetDMC);
						if (map != null) {
							MIBreakpoint mod = map.remove(miBpt.getNumber());
							if (mod != null) {
								targetBreakpointModified(bpTargetDMC, plBpt, mod);
							}
						}
						else {
							targetBreakpointModified(bpTargetDMC, plBpt, miBpt);
						}
					}
					catch(CoreException e) {
						GdbPlugin.log(getStatus());
					}
            		super.handleSuccess();
            	}
            });
	}

	/**
	 * @since 5.0
	 */
	public void targetBreakpointDeleted(final String id) {
		MIBreakpoints breakpointsService = getBreakpointsService();
		final MIBreakpointsManager bm = getBreakpointsManager();
		if (breakpointsService == null || bm == null) {
			return;
		}
		final IBreakpointsTargetDMContext bpTargetDMC = breakpointsService.getBreakpointTargetContext(id);
		if (bpTargetDMC != null){
			final MIBreakpointDMContext bpDMC = 
				new MIBreakpointDMContext(breakpointsService, new IDMContext[] { bpTargetDMC }, id);
			breakpointsService.getBreakpointDMData(
				bpDMC, 
				new DataRequestMonitor<IBreakpointDMData>(getExecutor(), null) {
					@Override
					@ConfinedToDsfExecutor( "fExecutor" )
					protected void handleSuccess() {
						if (!(getData() instanceof MIBreakpointDMData)) {
							return;
						}
						MIBreakpointDMData data = (MIBreakpointDMData)getData();
						if (MIBreakpoints.CATCHPOINT.equals(data.getBreakpointType())) {
							return;
						}
	
						IBreakpoint plBpt = bm.findPlatformBreakpoint(bpDMC);
						if (plBpt instanceof ICBreakpoint) {
							Set<String> set = fDeletedTargetBreakpoints.get(bpTargetDMC);
							if (set == null) {
								set = new HashSet<String>();
								fDeletedTargetBreakpoints.put(bpTargetDMC, set);
							}
							set.add(id);
	
							try {
								String threadId = data.getThreadId();
								if (!threadId.equals("0")) { //$NON-NLS-1$
									IDsfBreakpointExtension bpExtension = fBreakpointsManager.getFilterExtension((ICBreakpoint)plBpt);
									
									IMIProcesses processes = getServicesTracker().getService(IMIProcesses.class);
									if (processes == null) {
										return;
									}

									IContainerDMContext contDMC = processes.createContainerContextFromThreadId(getCommandControl().getContext(), data.getThreadId());
									if (contDMC == null) {
										return;
									}

									IExecutionDMContext[] execDMCs = bpExtension.getThreadFilters(contDMC);
									List<IExecutionDMContext> list = new ArrayList<IExecutionDMContext>(execDMCs.length);
									for (IExecutionDMContext c : execDMCs) {
										if (c instanceof IMIExecutionDMContext 
											&& !((IMIExecutionDMContext)c).getThreadId().equals(threadId)) {
											list.add(c);
										}
									}
									if (!list.isEmpty()) {
										bpExtension.setThreadFilters(list.toArray(new IExecutionDMContext[list.size()]));
									}
									else {
										bm.uninstallBreakpoint(bpTargetDMC, (ICBreakpoint)plBpt, new RequestMonitor(getExecutor(), null));
									}
								}
								else {
									bm.uninstallBreakpoint(bpTargetDMC, (ICBreakpoint)plBpt, new RequestMonitor(getExecutor(), null));
								}
							}
							catch(CoreException e) {
								GdbPlugin.log(e.getStatus());
							}
						}
					}
				});

		}
	}

	public void targetBreakpointModified(final MIBreakpoint miBpt) {
		if (isCatchpoint(miBpt)) {
			return;
		}
		ICommandControlService commandControl = getCommandControl();
		MIBreakpoints breakpointsService = getBreakpointsService();
		final MIBreakpointsManager bm = getBreakpointsManager();
		if (commandControl != null && breakpointsService != null && bm != null) {
			final IBreakpointsTargetDMContext bpTargetDMC = getBreakpointsTargetContext(commandControl, miBpt);
			if (bpTargetDMC == null) {
				return;
			}
			final Map<String, MIBreakpointDMData> contextBreakpoints = breakpointsService.getBreakpointMap(bpTargetDMC);
			if (contextBreakpoints == null) {
				return;
			}
			IBreakpoint b = bm.findPlatformBreakpoint(
				new MIBreakpointDMContext(breakpointsService, new IDMContext[] { bpTargetDMC }, miBpt.getNumber()));
			if (!(b instanceof ICBreakpoint)) {
				// Platform breakpoint hasn't been created yet. Store the latest 
				// modification data, it will be picked up later.
				Map<String, MIBreakpoint> map = fPendingModifications.get(bpTargetDMC);
				if (map == null) {
					map = new HashMap<String, MIBreakpoint>();
					fPendingModifications.put(bpTargetDMC, map);
				}
				map.put(miBpt.getNumber(), miBpt);
			}
			else {
				ICBreakpoint plBpt = (ICBreakpoint)b;
				targetBreakpointModified(bpTargetDMC, plBpt, miBpt);
			}
		}
	}

	private void targetBreakpointModified(
			IBreakpointsTargetDMContext bpTargetDMC,
			ICBreakpoint plBpt, 
			MIBreakpoint miBpt) {
		Map<String, MIBreakpointDMData> contextBreakpoints = getBreakpointsService().getBreakpointMap(bpTargetDMC);
		MIBreakpointDMData oldData = contextBreakpoints.get(miBpt.getNumber());
		contextBreakpoints.put(miBpt.getNumber(), new MIBreakpointDMData(miBpt));
		try {
			if (plBpt.isEnabled() != miBpt.isEnabled()) {
				plBpt.setEnabled(miBpt.isEnabled());
			}
			if (!plBpt.getCondition().equals(miBpt.getCondition())) {
				plBpt.setCondition(miBpt.getCondition());
			}
			if (plBpt.getIgnoreCount() != miBpt.getIgnoreCount()) {
				plBpt.setIgnoreCount(miBpt.getIgnoreCount());
			}
			if (oldData.isPending() != miBpt.isPending()) {
				if (miBpt.isPending()) {
					plBpt.decrementInstallCount();
				} else {
					plBpt.incrementInstallCount();
				}
			}
			if (plBpt instanceof ICTracepoint && miBpt.isTracepoint()) {
				ICTracepoint plTpt = (ICTracepoint)plBpt;
				if (plTpt.getPassCount() != miBpt.getPassCount()) {
					// GDB (up to 7.5) doesn't emit notification when the pass count is modified.
					plTpt.setPassCount(miBpt.getPassCount());
				}

				if (!miBpt.getCommands().equals(plBpt.getMarker().getAttribute(BreakpointActionManager.BREAKPOINT_ACTION_ATTRIBUTE))) {
					StringBuilder sb = new StringBuilder();
					boolean first = true;
					String[] commands = miBpt.getCommands().split(TracepointActionManager.TRACEPOINT_ACTION_DELIMITER);
					for (ITracepointAction action : getActionsFromCommands(commands)) {
						if (first) {
							first = false;
						} else {
							sb.append(TracepointActionManager.TRACEPOINT_ACTION_DELIMITER);
						}
						sb.append(action.getName());
					}
					// Target breakpoints and platform breakpoints use the same format 
					// to store trace commands. This format is different than the format 
					// used by GDB. We need to switch to the platform format to avoid unnecessary
					// modifications of target breakpoints.
					miBpt.setCommands(sb.toString());
					plBpt.getMarker().setAttribute(
						BreakpointActionManager.BREAKPOINT_ACTION_ATTRIBUTE, sb.toString());
				}
			} else if (plBpt instanceof ICDynamicPrintf && miBpt.isDynamicPrintf()) {
				// Cannot synchronize the string as there is a bug in GDB 7.7 that corrupts it.
				// https://sourceware.org/bugzilla/show_bug.cgi?id=15806
				// If we were to synchronize here, we would overwrite the string defined by
				// the user with the corrupted one!
				// Truth is that we don't need to synchronize the string anyway because there
				// is currently no way to change a dprintf string in GDB; instead a new
				// dprintf must be created.  That means that there will be no =breakpoint-modifed
				// event that indicates a real dprintf string change; only the other fields can
				// change and are handled as any other breakpoint.
				//
				// ICDynamicPrintf plDPrintf = (ICDynamicPrintf)plBpt;
				// if (!plDPrintf.getPrintfString().equals(miBpt.getPrintfString())) {
				// 	plDPrintf.setPrintfString(miBpt.getPrintfString());
				// }
			}
		}
		catch(CoreException e) {
			contextBreakpoints.put(miBpt.getNumber(), oldData);
			GdbPlugin.log(e.getStatus());
		}
	}

	private void setThreadSpecificBreakpoint(
			final ICBreakpoint plBpt, 
			MIBreakpoint miBpt) {

		try {
			IMIProcesses processes = getServicesTracker().getService(IMIProcesses.class);
			if (processes == null) {
				return;
			}
			String threadId = miBpt.getThreadId();
			IContainerDMContext contDMC = processes.createContainerContextFromThreadId(getCommandControl().getContext(), threadId);
			if (contDMC == null) {
				return;
			}
			IProcessDMContext procDmc =
					DMContexts.getAncestorOfType(contDMC, IProcessDMContext.class);
			if (procDmc == null) {
				return;
			}
			IDsfBreakpointExtension bpExtension = fBreakpointsManager.getFilterExtension(plBpt);

			IExecutionDMContext[] execDMCs = bpExtension.getThreadFilters(contDMC);
			if (execDMCs == null) {
				execDMCs = new IExecutionDMContext[0];
			}
			for (IExecutionDMContext execDMC : execDMCs) {
                String ctxThreadId = ((IMIExecutionDMContext)execDMC).getThreadId();
                if (execDMC instanceof IMIExecutionDMContext && ctxThreadId.equals(threadId)) {
                    // The platform breakpoint is already restricted to the given thread.
                    return;
                }
			}
			IExecutionDMContext[] newExecDMCs = new IExecutionDMContext[execDMCs.length + 1];
			System.arraycopy(execDMCs, 0, newExecDMCs, 0, execDMCs.length);
			newExecDMCs[execDMCs.length] = processes.createExecutionContext(
					contDMC,
					processes.createThreadContext(procDmc, threadId),
					threadId);
			bpExtension.setThreadFilters(newExecDMCs);
		}
		catch(CoreException e) {
			GdbPlugin.log(e);
		}
	}
	
	private ICBreakpoint getPlatformBreakpoint(MIBreakpoint miBpt, String fileName) {
    	for (IBreakpoint b : DebugPlugin.getDefault().getBreakpointManager().getBreakpoints()) {
    		if (b instanceof ICTracepoint 
    			&& miBpt.isTracepoint()
    			&& isPlatformTracepoint((ICTracepoint)b, miBpt, fileName)) {
    			 return (ICBreakpoint)b;
    		}
    		if (b instanceof ICDynamicPrintf 
        		&& miBpt.isDynamicPrintf()
        		&& isPlatformDynamicPrintf((ICDynamicPrintf)b, miBpt, fileName)) {
        		 return (ICBreakpoint)b;
        	}
    		if (b instanceof ICWatchpoint 
    			&& miBpt.isWatchpoint()
    	    	&& isPlatformWatchpoint((ICWatchpoint)b, miBpt)) {
    			return (ICBreakpoint)b;
    		}
    		if (b instanceof ICLineBreakpoint 
    			&& !miBpt.isWatchpoint()
    			&& !isCatchpoint(miBpt)
    			&& !miBpt.isTracepoint()
    			&& !miBpt.isDynamicPrintf()
    	    	&& isPlatformLineBreakpoint((ICLineBreakpoint)b, miBpt, fileName)) {
    			return (ICBreakpoint)b;
    		}
    	}
    	return null;
	}

	private ICBreakpoint createPlatformBreakpoint(String fileName, MIBreakpoint miBpt) throws CoreException {
		if (miBpt.isWatchpoint()) {
			return createPlatformWatchpoint(fileName, miBpt);
		}
		else if (miBpt.isTracepoint()) {
			return createPlatformTracepoint(fileName, miBpt);
		}
		else if (miBpt.isDynamicPrintf()) {
			return createPlatformDynamicPrintf(fileName, miBpt);
		}
		else {
			return createPlatformLocationBreakpoint(fileName, miBpt);
		}
	}

	private ICBreakpoint createPlatformLocationBreakpoint(String fileName, MIBreakpoint miBpt) throws CoreException {
		if (isAddressBreakpoint(miBpt)) {
			return createPlatformAddressBreakpoint(fileName, miBpt);
		}
		else if (isFunctionBreakpoint(miBpt)) {
			return createPlatformFunctionBreakpoint(fileName, miBpt);
		}
		else {
			return createPlatformLineBreakpoint(fileName, miBpt);
		}
	}

	private ICBreakpoint createPlatformAddressBreakpoint(String fileName, MIBreakpoint miBpt) throws CoreException {
		IResource resource = getResource(fileName);

		int type = 0;
		if (miBpt.isTemporary()) {
			type |= ICBreakpointType.TEMPORARY;
		}
		if (miBpt.isHardware()) {
			type |= ICBreakpointType.HARDWARE;
		}

		try {
			return CDIDebugModel.createAddressBreakpoint(
					null, 
					null, 
					resource, 
					type, 
					getPlatformAddress(miBpt.getAddress()), 
					miBpt.isEnabled(), 
					miBpt.getIgnoreCount(), 
					miBpt.getCondition(),
					true);
		}
		catch(NumberFormatException e) {
			throw new CoreException(new Status(IStatus.ERROR, GdbPlugin.getUniqueIdentifier(), 
			        String.format("Invalid breakpoint address: %s", miBpt.getAddress()))); //$NON-NLS-1$
		}
	}

	private ICBreakpoint createPlatformFunctionTracepoint(String fileName, MIBreakpoint miBpt) throws CoreException {
		IResource resource = getResource(fileName);

		int type = 0;
		if (miBpt.isTemporary()) {
			type |= ICBreakpointType.TEMPORARY;
		}
		if (miBpt.isHardware()) {
			type |= ICBreakpointType.HARDWARE;
		}
		
		return CDIDebugModel.createFunctionTracepoint(
				fileName, 
				resource, 
				type, 
				getFunctionName(miBpt), 
				-1, 
				-1, 
				getLineNumber(miBpt), 
				miBpt.isEnabled(), 
				miBpt.getIgnoreCount(), 
				miBpt.getCondition(),
				true);
	}

	private ICBreakpoint createPlatformLineTracepoint(String fileName, MIBreakpoint miBpt) throws CoreException {
		IResource resource = getResource(fileName);
		
		int type = 0;
		if (miBpt.isTemporary()) {
			type |= ICBreakpointType.TEMPORARY;
		}
		if (miBpt.isHardware()) {
			type |= ICBreakpointType.HARDWARE;
		}
		
		return CDIDebugModel.createLineTracepoint(
				fileName, 
				resource, 
				type, 
				getLineNumber(miBpt), 
				miBpt.isEnabled(), 
				miBpt.getIgnoreCount(), 
				miBpt.getCondition(), 
				true);
	}

	private ICBreakpoint createPlatformTracepoint(String fileName, MIBreakpoint miBpt) throws CoreException {
		if (isAddressBreakpoint(miBpt)) {
			return createPlatformAddressTracepoint(fileName, miBpt);
		}
		else if (isFunctionBreakpoint(miBpt)) {
			return createPlatformFunctionTracepoint(fileName, miBpt);
		}
		else {
			return createPlatformLineTracepoint(fileName, miBpt);
		}
	}

	private ICBreakpoint createPlatformAddressTracepoint(String fileName, MIBreakpoint miBpt) throws CoreException {
		IResource resource = getResource(fileName);

		int type = 0;
		if (miBpt.isTemporary()) {
			type |= ICBreakpointType.TEMPORARY;
		}
		if (miBpt.isHardware()) {
			type |= ICBreakpointType.HARDWARE;
		}

		try {
			return CDIDebugModel.createAddressTracepoint(
					null, 
					null, 
					resource, 
					type,
					getLineNumber(miBpt),
					getPlatformAddress(miBpt.getAddress()), 
					miBpt.isEnabled(), 
					miBpt.getIgnoreCount(), 
					miBpt.getCondition(),
					true);
		}
		catch(NumberFormatException e) {
			throw new CoreException(new Status(IStatus.ERROR, GdbPlugin.getUniqueIdentifier(), 
					String.format("Invalid breakpoint address: %s", miBpt.getAddress()))); //$NON-NLS-1$
		}
	}

	private ICBreakpoint createPlatformFunctionBreakpoint(String fileName, MIBreakpoint miBpt) throws CoreException {
		IResource resource = getResource(fileName);

		int type = 0;
		if (miBpt.isTemporary()) {
			type |= ICBreakpointType.TEMPORARY;
		}
		if (miBpt.isHardware()) {
			type |= ICBreakpointType.HARDWARE;
		}
		
		return CDIDebugModel.createFunctionBreakpoint(
				fileName, 
				resource, 
				type, 
				getFunctionName(miBpt), 
				-1, 
				-1, 
				getLineNumber(miBpt), 
				miBpt.isEnabled(), 
				miBpt.getIgnoreCount(), 
				miBpt.getCondition(),
				true);
	}

	private ICBreakpoint createPlatformLineBreakpoint(String fileName, MIBreakpoint miBpt) throws CoreException {
		IResource resource = getResource(fileName);

		int type = 0;
		if (miBpt.isTemporary()) {
			type |= ICBreakpointType.TEMPORARY;
		}
		if (miBpt.isHardware()) {
			type |= ICBreakpointType.HARDWARE;
		}
		
		return CDIDebugModel.createLineBreakpoint(
				fileName, 
				resource, 
				type, 
				getLineNumber(miBpt), 
				miBpt.isEnabled(), 
				miBpt.getIgnoreCount(), 
				miBpt.getCondition(), 
				true);
	}

	private ICBreakpoint createPlatformDynamicPrintf(String fileName, MIBreakpoint miBpt) throws CoreException {
		if (isAddressBreakpoint(miBpt)) {
			return createPlatformAddressDynamicPrintf(fileName, miBpt);
		}
		// TODO This is currently causing problems because we think a normal dprintf is a function one
		// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=400628#c16 which says:
		// "synchronization of function dprintf does not work"
//		else if (isFunctionBreakpoint(miBpt)) {
//			return createPlatformFunctionDynamicPrintf(fileName, miBpt);
//		}
		else {
			return createPlatformLineDynamicPrintf(fileName, miBpt);
		}
	}

	private ICBreakpoint createPlatformAddressDynamicPrintf(String fileName, MIBreakpoint miBpt) throws CoreException {
		IResource resource = getResource(fileName);

		int type = 0;
		if (miBpt.isTemporary()) {
			type |= ICBreakpointType.TEMPORARY;
		}
		if (miBpt.isHardware()) {
			type |= ICBreakpointType.HARDWARE;
		}

		try {
			return CDIDebugModel.createAddressDynamicPrintf(
					null, 
					null, 
					resource, 
					type,
					getLineNumber(miBpt),
					getPlatformAddress(miBpt.getAddress()), 
					miBpt.isEnabled(), 
					miBpt.getIgnoreCount(), 
					miBpt.getCondition(),
					miBpt.getPrintfString(),
					true);
		}
		catch(NumberFormatException e) {
			throw new CoreException(new Status(IStatus.ERROR, GdbPlugin.getUniqueIdentifier(), 
					String.format("Invalid breakpoint address: %s", miBpt.getAddress()))); //$NON-NLS-1$
		}
	}

	// Unused, see TODO in createPlatformDynamicPrintf and Bug 400628 Comment 16
	@SuppressWarnings("unused")
	private ICBreakpoint createPlatformFunctionDynamicPrintf(String fileName, MIBreakpoint miBpt) throws CoreException {
		IResource resource = getResource(fileName);

		int type = 0;
		if (miBpt.isTemporary()) {
			type |= ICBreakpointType.TEMPORARY;
		}
		if (miBpt.isHardware()) {
			type |= ICBreakpointType.HARDWARE;
		}
		
		return CDIDebugModel.createFunctionDynamicPrintf(
				fileName, 
				resource, 
				type, 
				getFunctionName(miBpt), 
				-1, 
				-1, 
				getLineNumber(miBpt), 
				miBpt.isEnabled(), 
				miBpt.getIgnoreCount(), 
				miBpt.getCondition(),
				miBpt.getPrintfString(),
				true);
	}

	private ICBreakpoint createPlatformLineDynamicPrintf(String fileName, MIBreakpoint miBpt) throws CoreException {
		IResource resource = getResource(fileName);
		
		int type = 0;
		if (miBpt.isTemporary()) {
			type |= ICBreakpointType.TEMPORARY;
		}
		if (miBpt.isHardware()) {
			type |= ICBreakpointType.HARDWARE;
		}
		
		return CDIDebugModel.createLineDynamicPrintf(
				fileName, 
				resource, 
				type, 
				getLineNumber(miBpt), 
				miBpt.isEnabled(), 
				miBpt.getIgnoreCount(), 
				miBpt.getCondition(), 
				miBpt.getPrintfString(),
				true);
	}

	private ICBreakpoint createPlatformWatchpoint(String fileName, MIBreakpoint miBpt) throws CoreException {
		IResource resource = getResource(fileName);
		
		int type = 0;
		if (miBpt.isTemporary()) {
			type |= ICBreakpointType.TEMPORARY;
		}
		if (miBpt.isHardware()) {
			type |= ICBreakpointType.HARDWARE;
		}

		return CDIDebugModel.createWatchpoint(
				fileName, 
				resource,
				type,
				miBpt.isAccessWatchpoint() || miBpt.isWriteWatchpoint(),
				miBpt.isAccessWatchpoint() || miBpt.isReadWatchpoint(),
				miBpt.getExpression(),
				miBpt.isEnabled(), 
				miBpt.getIgnoreCount(), 
				miBpt.getCondition(), 
				true);
	}

	private IBreakpointsTargetDMContext getBreakpointsTargetContext(ICommandControlService commandControl, MIBreakpoint miBpt) {
		IMIProcesses processes = getServicesTracker().getService(IMIProcesses.class);
		if (processes == null) {
			return null;
		}
		
        // For GDB  < 7.4, each process is its own breakpointTargetDMC so we need to find a the proper process
        // based on the threadId.  For GDB >= 7.4, this does not matter as we'll always end up with the global bpTargetDMC
		String threadId = (miBpt != null) ? miBpt.getThreadId() : null;
		IContainerDMContext contContext = processes.createContainerContextFromThreadId(commandControl.getContext(), threadId);
		if (contContext == null) {
			return null;
		}
		return DMContexts.getAncestorOfType(contContext, IBreakpointsTargetDMContext.class);
	}

	public void getTargetBreakpoint(
			IBreakpointsTargetDMContext context, 
			Map<String, Object> attributes,
			DataRequestMonitor<MIBreakpoint> rm) {
		Map<String, MIBreakpoint> map = fCreatedTargetBreakpoints.get(context);
		if (map == null) {
			rm.done();
			return;
		}
		String type = (String)attributes.get(MIBreakpoints.BREAKPOINT_TYPE);
		if (MIBreakpoints.BREAKPOINT.equals(type)) {
			rm.done(getTargetLineBreakpoint(
				map.values(),
				(String)attributes.get(MIBreakpoints.FILE_NAME), 
				(Integer)attributes.get(MIBreakpoints.LINE_NUMBER),
				(String)attributes.get(MIBreakpoints.FUNCTION),
				(String)attributes.get(MIBreakpoints.ADDRESS),
				(Boolean)attributes.get(MIBreakpointDMData.IS_HARDWARE),
				(Boolean)attributes.get(MIBreakpointDMData.IS_TEMPORARY)));
		}
		else if (MIBreakpoints.TRACEPOINT.equals(type)) {
			rm.done(getTargetTracepoint(
				map.values(),
				(String)attributes.get(MIBreakpoints.FILE_NAME), 
				(Integer)attributes.get(MIBreakpoints.LINE_NUMBER),
				(String)attributes.get(MIBreakpoints.FUNCTION),
				(String)attributes.get(MIBreakpoints.ADDRESS),
				(Boolean)attributes.get(MIBreakpointDMData.IS_HARDWARE),
				(Boolean)attributes.get(MIBreakpointDMData.IS_TEMPORARY)));
		}
		else if (MIBreakpoints.DYNAMICPRINTF.equals(type)) {
			rm.done(getTargetDPrintf(
				map.values(),
				(String)attributes.get(MIBreakpoints.FILE_NAME), 
				(Integer)attributes.get(MIBreakpoints.LINE_NUMBER),
				(String)attributes.get(MIBreakpoints.FUNCTION),
				(String)attributes.get(MIBreakpoints.ADDRESS),
				(Boolean)attributes.get(MIBreakpointDMData.IS_HARDWARE),
				(Boolean)attributes.get(MIBreakpointDMData.IS_TEMPORARY)));
		}
		else if (MIBreakpoints.WATCHPOINT.equals(type)) {
			rm.done(getTargetWatchpoint(
				map.values(),
				(String)attributes.get(MIBreakpoints.EXPRESSION), 
				(Boolean)attributes.get(MIBreakpoints.READ),
				(Boolean)attributes.get(MIBreakpoints.WRITE),
				(Boolean)attributes.get(MIBreakpointDMData.IS_HARDWARE),
				(Boolean)attributes.get(MIBreakpointDMData.IS_TEMPORARY)));
			
		} else {
			rm.done();
		}
	}
	
	private MIBreakpoint getTargetLineBreakpoint(
			Collection<MIBreakpoint> targetBreakpoints, 
			String fileName, 
			Integer lineNumber,
			String function,
			String address,
			Boolean isHardware, 
			Boolean isTemporary) {
		for (MIBreakpoint miBpt : targetBreakpoints) {
			if (!miBpt.isWatchpoint() && !isCatchpoint(miBpt) && !miBpt.isTracepoint() && !miBpt.isDynamicPrintf() 
				&& compareBreakpointAttributes(
					miBpt, fileName, lineNumber, function, address, isHardware, isTemporary)) {
				return miBpt;
			}
		}
		return null;
	}
	
	private MIBreakpoint getTargetTracepoint(
			Collection<MIBreakpoint> targetBreakpoints, 
			String fileName, 
			Integer lineNumber,
			String function,
			String address,
			Boolean isHardware, 
			Boolean isTemporary) {
		for (MIBreakpoint miBpt : targetBreakpoints) {
			if (miBpt.isTracepoint() 
				&& compareBreakpointAttributes(
					miBpt, fileName, lineNumber, function, address, isHardware, isTemporary)) {
				return miBpt;
			}
		}
		return null;
	}

	private MIBreakpoint getTargetDPrintf(
			Collection<MIBreakpoint> targetBreakpoints, 
			String fileName, 
			Integer lineNumber,
			String function,
			String address,
			Boolean isHardware, 
			Boolean isTemporary) {
		for (MIBreakpoint miBpt : targetBreakpoints) {
			if (miBpt.isDynamicPrintf() 
					&& compareBreakpointAttributes(
						miBpt, fileName, lineNumber, function, address, isHardware, isTemporary)) {
					return miBpt;
			}
		}
		return null;
	}
	
	private MIBreakpoint getTargetWatchpoint(
			Collection<MIBreakpoint> targetBreakpoints, 
			String expression,
			boolean readAccess,
			boolean writeAccess,
			Boolean isHardware, 
			Boolean isTemporary) {
		for (MIBreakpoint miBpt : targetBreakpoints) {
			if (!miBpt.isWatchpoint()) {
				continue;
			}
			if (expression == null || !expression.equals(miBpt.getExpression())) {
				continue;
			}
			if (readAccess && writeAccess && !miBpt.isAccessWatchpoint()) {
				continue;
			}
			if (readAccess && !writeAccess && !miBpt.isReadWatchpoint()) {
				continue;
			}
			if (!readAccess && writeAccess && !miBpt.isWriteWatchpoint()) {
				continue;
			}
			if (!compareBreakpointTypeAttributes(miBpt, isHardware, isTemporary)) {
				continue;
			}
			return miBpt;
		}
		return null;
	}

	private boolean compareBreakpointAttributes(
			MIBreakpoint miBpt,
			String fileName, 
			Integer lineNumber,
			String function,
			String address,
			Boolean isHardware, 
			Boolean isTemporary) {
		return compareBreakpointLocationAttributes(miBpt, fileName, lineNumber, function, address)
				&& compareBreakpointTypeAttributes(miBpt, isHardware, isTemporary);
	}

	private boolean compareBreakpointLocationAttributes(
			MIBreakpoint miBpt,
			String fileName, 
			Integer lineNumber,
			String function,
			String address) {
		if (isFunctionBreakpoint(miBpt) && (function == null || !function.equals(getFunctionName(miBpt)))) {
			return false;
		}
		if (isAddressBreakpoint(miBpt) 
			&& (address == null || !address.equals(getPlatformAddress(miBpt.getAddress()).toHexAddressString()))) { 
			return false;
		}
		if (isLineBreakpoint(miBpt)) {
			String miBptFileName = getFileName(miBpt);
			if (fileName == null || miBptFileName == null || !new File(fileName).equals(new File(miBptFileName))) {
				return false;
			}
			if (lineNumber == null || lineNumber.intValue() != getLineNumber(miBpt)) {
				return false;
			}
		}
		return true;
	}

	private boolean compareBreakpointTypeAttributes(MIBreakpoint miBpt, Boolean isHardware, Boolean isTemporary) {
		if ((isHardware == null && miBpt.isHardware()) 
			|| (isHardware != null && isHardware.booleanValue() != miBpt.isHardware())) {
			return false;
		}
		if ((isTemporary == null && miBpt.isTemporary()) 
			|| (isTemporary != null && isTemporary.booleanValue() != miBpt.isTemporary())) {
			return false;
		}
		return true;
	}

	public void removeCreatedTargetBreakpoint(IBreakpointsTargetDMContext context, MIBreakpoint miBpt) {
		Map<String, MIBreakpoint> map = fCreatedTargetBreakpoints.get(context);
		if (map != null) {
			map.remove(miBpt.getNumber());
		}
	}

    private boolean isPlatformLineBreakpoint(ICLineBreakpoint plBpt, MIBreakpoint miBpt, String fileName) {
    	if (plBpt instanceof ICAddressBreakpoint) {
    		return isAddressBreakpoint(miBpt) ? 
    			isPlatformAddressBreakpoint((ICAddressBreakpoint)plBpt, miBpt) : false;
    	}
    	if (plBpt instanceof ICFunctionBreakpoint) {
    		return isFunctionBreakpoint(miBpt) ? 
    			isPlatformFunctionBreakpoint((ICFunctionBreakpoint)plBpt, miBpt) : false;
    	}
    	try {
    		if (fileName == null || plBpt.getSourceHandle() == null 
    				|| !new File(fileName).equals(new File(plBpt.getSourceHandle()))) {
    			return false;
    		}
			if (plBpt.getLineNumber() != getLineNumber(miBpt)) {
				return false;
			}
			return true;
		}
		catch(CoreException e) {
			GdbPlugin.log(e.getStatus());
		}
    	return false;
    }

    private boolean isPlatformFunctionBreakpoint(ICFunctionBreakpoint plBpt, MIBreakpoint miBpt) {
		try {
			return (plBpt.getFunction() != null && plBpt.getFunction().equals(getFunctionName(miBpt)));
		}
		catch(CoreException e) {
			GdbPlugin.log(e.getStatus());
		}
		return false;
    }

    private boolean isPlatformAddressBreakpoint(ICAddressBreakpoint plBpt, MIBreakpoint miBpt) {
		try {
			return (plBpt.getAddress() != null 
					&& plBpt.getAddress().equals(getPlatformAddress(miBpt.getAddress()).toHexAddressString()));
		}
		catch(CoreException e) {
			GdbPlugin.log(e.getStatus());
		}
		return false;
    }

    private boolean isPlatformWatchpoint(ICWatchpoint plBpt, MIBreakpoint miBpt) {
    	try {
			if (plBpt.getExpression() != null && plBpt.getExpression().equals(miBpt.getExpression()) ) {
				if (miBpt.isAccessWatchpoint()) {
					return plBpt.isWriteType() && plBpt.isReadType();
				}
				else if (miBpt.isReadWatchpoint()) {
					return !plBpt.isWriteType() && plBpt.isReadType();
				}
				else if (miBpt.isWriteWatchpoint()) {
					return plBpt.isWriteType() && !plBpt.isReadType();
				}
			}
		}
		catch(CoreException e) {
			GdbPlugin.log(e.getStatus());
		}
    	return false;
    }
    
    private boolean isPlatformTracepoint(ICTracepoint plBpt, MIBreakpoint miBpt, String fileName) {
    	return isPlatformLineBreakpoint(plBpt, miBpt, fileName);
    }
    
    private boolean isPlatformDynamicPrintf(ICDynamicPrintf plBpt, MIBreakpoint miBpt, String fileName) {
    	return isPlatformLineBreakpoint(plBpt, miBpt, fileName);
    }

    /** @since 5.0 */
    public boolean isTargetBreakpointDeleted(IBreakpointsTargetDMContext context, String bpId, boolean remove) {
    	Set<String> set = fDeletedTargetBreakpoints.get(context);
    	if (set != null) {
    		return (remove) ? set.remove(bpId) : set.contains(bpId);
    	}
    	return false;
    }    

    /**
     * Returns the list of tracepoint actions generated from the given command string.
     * If the corresponding action for a command doesn't exist in TracepointActionManager 
     * the new action is created and added.
     *   
     * @param commands list of gdb tracepoint commands separated by TracepointActionManager.TRACEPOINT_ACTION_DELIMITER
     */
    private ITracepointAction[] getActionsFromCommands(String[] commands) {
    	List<ITracepointAction> list = new ArrayList<ITracepointAction>();
    	TracepointActionManager tam = TracepointActionManager.getInstance();
    	WhileSteppingAction whileStepping = null;
    	List<ITracepointAction> subActions = null;
    	for (String command : commands) {
    		// Check if an action for this command exists
        	boolean found = false;
        	for (ITracepointAction action :tam.getActions()) {
        		if (command.equals(action.getSummary())) {
        			if (whileStepping == null || subActions == null) {
        				list.add(action);
        			} else {
        				subActions.add(action);
        			}
        			found = true;
        			break;
        		}
        	}
        	if (!found) {
        		// Create a new action if an action for this command doesn't exists
        		ITracepointAction action = null;
            	if (command.startsWith(TC_COLLECT)) {
            		action = createCollectAction(command.substring(TC_COLLECT.length()));
            	} else if (command.startsWith(TC_TEVAL)) {
            		action = createEvaluateAction(command.substring(TC_TEVAL.length()));
            	} else if (command.startsWith(TC_WHILE_STEPPING)) {
            		whileStepping = createWhileSteppingAction(command.substring(TC_WHILE_STEPPING.length()));
            		if (whileStepping != null) {
            			subActions = new ArrayList<ITracepointAction>();
            		}
            	}
            	else if (command.equals(TC_END)) {
            		if (whileStepping == null || subActions == null) {
            			continue;
            		}
                	StringBuilder sb = new StringBuilder();
                	boolean first = true;
                	for (ITracepointAction a : subActions) {
                		if (first) {
                			first = false;
                		} else {
                			sb.append(',');
                		}
                		sb.append(a.getName());
                	}
                	whileStepping.setSubActionsNames(sb.toString());
                	whileStepping.setSubActionsContent(sb.toString());
                	action = whileStepping;
                	// Search for existing action for this 'while-stepping' command
                	for (ITracepointAction a :tam.getActions()) {
                		if (whileStepping.getSummary().equals(a.getSummary())) {
                			action = a;
                			found = true;
                			break;
                		}
                	}
                	whileStepping = null;
                	subActions.clear();
                	subActions = null;
            	}
        		if (action != null) {
        			if (!found) {
        				TracepointActionManager.getInstance().addAction(action);
        			}
        			if (whileStepping == null || subActions == null) {
	        			list.add(action);
        			}
        			else {
    					subActions.add(action);
        			}
        		}
        	}
        	TracepointActionManager.getInstance().saveActionData();
    	}
    	return list.toArray(new ITracepointAction[list.size()]);
    }

    private CollectAction createCollectAction(String collectStr) {
    	CollectAction action = new CollectAction();
    	action.setName(TracepointActionManager.getInstance().makeUniqueActionName(action.getDefaultName()));
    	action.setCollectString(collectStr);
    	return action;
    }

    private EvaluateAction createEvaluateAction(String evalStr) {
    	EvaluateAction action = new EvaluateAction();
    	action.setName(TracepointActionManager.getInstance().makeUniqueActionName(action.getDefaultName()));
    	action.setEvalString(evalStr);
    	return action;
    }

    private WhileSteppingAction createWhileSteppingAction(String str) {
    	WhileSteppingAction action = new WhileSteppingAction();
    	action.setName(TracepointActionManager.getInstance().makeUniqueActionName(action.getDefaultName()));
    	try {
			action.setStepCount(Integer.parseInt(str.trim()));
		}
		catch(NumberFormatException e) {
			return null;
		}
    	return action;
    }

    protected void getSource(
    	IBreakpointsTargetDMContext bpTargetDMC, 
    	final String debuggerPath, 
    	final DataRequestMonitor<String> rm) {
    	
		ISourceLookup sourceLookup = getServicesTracker().getService(ISourceLookup.class);
        if (sourceLookup == null) {
        	rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, "Source lookup service is not available")); //$NON-NLS-1$
        	rm.done();
        	return;
        }

        ISourceLookupDMContext srcDmc = DMContexts.getAncestorOfType(bpTargetDMC, ISourceLookupDMContext.class);
        if (srcDmc == null) {
        	rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, "No source lookup context")); //$NON-NLS-1$
        	rm.done();
        	return;
        }

        if (debuggerPath == null || debuggerPath.isEmpty()) {
        	rm.done();
        	return;
        }

        sourceLookup.getSource(
        	srcDmc, 
        	debuggerPath, 
        	new DataRequestMonitor<Object>(getExecutor(), rm) {        		
        		@Override
        		@ConfinedToDsfExecutor("fExecutor")
        		protected void handleCompleted() {
            		String fileName = null;
        			if (isSuccess()) {
                		if (getData() instanceof IFile) {
                			fileName = ((IFile)getData()).getLocation().toOSString();
                		}
                		else if (getData() instanceof File) {
                			fileName = ((File)getData()).getAbsolutePath();
                		}
                		else if (getData() instanceof ITranslationUnit) {
                			IPath location = ((ITranslationUnit)getData()).getLocation();
                			if (location != null) {
                				fileName = location.toOSString();
                			}
                		}
                		else if (getData() instanceof LocalFileStorage) {
                			fileName = ((LocalFileStorage)getData()).getFile().getAbsolutePath();
                		}
        			}
            		rm.setData((fileName != null && !fileName.isEmpty()) ? fileName : debuggerPath);
            		rm.done();
        		}
            });
    }

    private boolean isFunctionBreakpoint(MIBreakpoint miBpt) {
    	String origFunction = getFunctionFromOriginalLocation(miBpt.getOriginalLocation());
    	if (miBpt.getFunction().isEmpty()) {
    		return !origFunction.isEmpty();
    	}
    	String function = miBpt.getFunction();
    	// For C++ the function name for "break x" is reported as "x()".
    	// To compare it to the name retrieved from the original location
    	// we need to remove "()".
    	int index = function.indexOf('(');
    	if (index > 0 && origFunction.indexOf('(') == -1) {
    		return function.substring(0, index).equals(origFunction);
    	}
    	return function.equals(origFunction);
    }

    private boolean isAddressBreakpoint(MIBreakpoint miBpt) {
    	return miBpt.getOriginalLocation().startsWith("*"); //$NON-NLS-1$
    }

    private boolean isLineBreakpoint(MIBreakpoint miBpt) {
    	return !isFunctionBreakpoint(miBpt) && !isAddressBreakpoint(miBpt);
    }

    private IAddress getPlatformAddress(String miAddress) {
		int radix = 10;
		if (miAddress.startsWith("0x")) { //$NON-NLS-1$
			radix = 16;
			miAddress = miAddress.substring(2);
		}
		return new Addr64(new BigInteger(miAddress, radix));
    }

    private boolean isBreakpointTargetTracked(IBreakpointsTargetDMContext btTargetDMC) {
    	return fTrackedTargets.contains(btTargetDMC);
    }

    private String getFileName(MIBreakpoint miBpt) {
    	String fileName = (miBpt.getFullName() != null && !miBpt.getFullName().isEmpty()) ? 
    			miBpt.getFullName() : miBpt.getFile();
    	if (fileName != null && !fileName.isEmpty()) {
    		return fileName;
    	}
    	// When a breakpoint is set from the console on an invalid file both 
    	// 'file' and 'fullname' attributes are not available, we need to parse 
    	// the 'original-location' attribute to retrieve the file name.
    	String origLocation = miBpt.getOriginalLocation();
    	if (origLocation.isEmpty()) {
    		// Shouldn't happen
    		GdbPlugin.log(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, "Invalid 'original-location' attribute")); //$NON-NLS-1$
    		return ""; //$NON-NLS-1$
    	}
    	if (origLocation.startsWith("*")) { //$NON-NLS-1$
    		// Address breakpoint
    		return ""; //$NON-NLS-1$
    	}
		int index = origLocation.lastIndexOf(':');
		return (index > 0) ? origLocation.substring(0, index) : ""; //$NON-NLS-1$
    }

    private int getLineNumber(MIBreakpoint miBpt) {
    	int lineNumber = miBpt.getLine();
    	if (lineNumber != -1) {
    		return lineNumber;
    	}
    	// When a breakpoint is set from the console on an invalid file 
    	// the 'line' attributes is not available, we need to parse 
    	// the 'original-location' attribute to retrieve the line number.
    	String origLocation = miBpt.getOriginalLocation();
    	if (origLocation.isEmpty()) {
    		// Shouldn't happen
    		GdbPlugin.log(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, "Invalid 'original-location' attribute")); //$NON-NLS-1$
    		return -1;
    	}
    	if (origLocation.startsWith("*")) { //$NON-NLS-1$
    		// Address breakpoint
    		return -1;
    	}
		int index = origLocation.lastIndexOf(':');
		if (index > 0 && origLocation.length() > index + 1) {
			try {
				return Integer.valueOf(origLocation.substring(index + 1, origLocation.length())).intValue();
			}
			catch(NumberFormatException e) {
				// not a line breakpoint
			}
		}
		return -1;
    }

    private String getFunctionName(MIBreakpoint miBpt) {
    	if (miBpt.getFunction() != null && !miBpt.getFunction().isEmpty())
    		return miBpt.getFunction();
    	// When a function breakpoint is set from the console, the symbol associated with 
    	// the function may not be known to GDB. In this case the 'function' attribute is 
    	// not available, we need to parse the 'original-location' attribute to retrieve 
    	// the function name.
    	return getFunctionFromOriginalLocation(miBpt.getOriginalLocation());
    }

    private IResource getResource(String fileName) {
		IResource resource = null;
		if (fileName == null || fileName.isEmpty()) {
			resource = ResourcesPlugin.getWorkspace().getRoot();
		} else {
			IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(
				new File(fileName).toURI());
			if (files.length > 0) {
				resource = files[0];
			}
			else {
				resource = ResourcesPlugin.getWorkspace().getRoot();
			}
		}
		return resource;
    }

	@DsfServiceEventHandler
    public void eventDispatched(IExitedDMEvent e) {
     	if (e.getDMContext() instanceof IBreakpointsTargetDMContext) {
    		// Remove breakpoint entries when a breakpoint target is removed.
    		// This will happen for GDB < 7.4 where the container is the breakpoint target.
    		// For GDB >= 7.4, GDB is the breakpoint target and will not be removed.
    		IBreakpointsTargetDMContext bpTargetDMContext = (IBreakpointsTargetDMContext)e.getDMContext();
    			Map<String, MIBreakpoint> createdBreakpoints = fCreatedTargetBreakpoints.remove(bpTargetDMContext);
    			if (createdBreakpoints != null) {
    				createdBreakpoints.clear();
    			}
    			Map<String, MIBreakpoint> modifications = fPendingModifications.remove(bpTargetDMContext);
    			if (modifications != null) {
    				modifications.clear();
    			}
    			Set<String> deletedBreakpoints = fDeletedTargetBreakpoints.remove(bpTargetDMContext);
    			if (deletedBreakpoints != null) {
    				deletedBreakpoints.clear();
    			}
    	}
    }

	private String getFunctionFromOriginalLocation(String origLocation) {
    	if (origLocation.isEmpty()) {
    		// Shouldn't happen
    		GdbPlugin.log(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, "Invalid 'original-location' attribute")); //$NON-NLS-1$
    		return ""; //$NON-NLS-1$
    	}
    	if (origLocation.startsWith("*")) { //$NON-NLS-1$
    		// Address breakpoint
    		return ""; //$NON-NLS-1$
    	}
		int index = origLocation.lastIndexOf(':');
		String function = (index >= 0) ? origLocation.substring(index + 1) : origLocation;
    	try {
    		//TODO This does not work for dprintf since the output of the orginal location can look like this:
    		//original-location="/home/lmckhou/runtime-TestDSF/Producer/src/Producer.cpp:100,\\"Hit line %d of /home/lmckhou/runtime-TestDSF/Producer/src/Producer.cpp\\\\n\\",100"
			Integer.valueOf(function);
			// Line breakpoint
    		return ""; //$NON-NLS-1$
		}
		catch(NumberFormatException e) {
			// possible function breakpoint
		}
    	return function;
	}

	protected boolean isCatchpoint(MIBreakpoint miBpt) {
		// Since we are using the CLI 'catch' command to set catchpoints GDB will emit 
		// the 'breakpoint-created' notification even if the catchpoint is set from UI.
		// In case of 'catch' and 'throw' events the value of the 'type' attribute in 
		// the breakpoint notification's data is 'breakpoint' instead of 'catchpoint'.
		// In this cases to identify the correct type we need to check the content of 
		// the 'what' attribute.
		return (miBpt.isCatchpoint() ||
				(!miBpt.isWatchpoint() && 
				 (CE_EXCEPTION_CATCH.equals(miBpt.getExpression()) || 
				  CE_EXCEPTION_THROW.equals(miBpt.getExpression()))));
	}
}

Back to the top