Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9893a79e6eb6eac8165df6472e1efa401f1c4f14 (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
/*******************************************************************************
 * Copyright (c) 2000, 2016 QNX Software Systems 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:
 *     QNX Software Systems - Initial API and implementation
 *     QNX Software Systems - Refactored to use platform implementation
 *     Marc Khouzam (Ericsson) - Added support for Tracepoints (bug 376116)
 *     Marc Khouzam (Ericsson) - Added support for Dynamic-Printf (bug 400628)
 *     Jonah Graham - Set REQUESTED_* fields when creating from dialog (bug 46026)
 *     Jonah Graham (Kichwa Coders) - Create "Add Line Breakpoint (C/C++)" action (Bug 464917)
 *******************************************************************************/
package org.eclipse.cdt.debug.internal.ui.breakpoints;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.cdt.debug.core.CDIDebugModel;
import org.eclipse.cdt.debug.core.model.ICBreakpoint;
import org.eclipse.cdt.debug.core.model.ICBreakpoint2;
import org.eclipse.cdt.debug.core.model.ICDynamicPrintf;
import org.eclipse.cdt.debug.core.model.ICLineBreakpoint2;
import org.eclipse.cdt.debug.core.model.ICTracepoint;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.IWorkspaceRunnable;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.core.runtime.Path;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.jface.preference.IPersistentPreferenceStore;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;

/**
 * A preference store that presents the state of the properties of a C/C++ breakpoint,
 * tracepoint or dynamic-printf.
 */
public class CBreakpointPreferenceStore implements IPersistentPreferenceStore {

	// This map is the current properties/values being maintained/manipulated
	private HashMap<String, Object> fProperties = new HashMap<String, Object>();

	// Original set of values. So we can see what has really changed on the save and
	// perform appropriate change operations. We only really want to operate on changed
	// values, to avoid generating churn.
	private HashMap<String, Object> fOriginalValues = new HashMap<String, Object>();
	private boolean fIsDirty = false;
	private boolean fIsCanceled = false;
	private ListenerList<IPropertyChangeListener> fListeners;
	private final CBreakpointContext fContext;

	public CBreakpointPreferenceStore() {
		this(null, null);
	}

	public CBreakpointPreferenceStore(CBreakpointContext context, Map<String, Object> attributes) {
		fListeners = new ListenerList<>(org.eclipse.core.runtime.ListenerList.IDENTITY);
		fContext = context;

		fOriginalValues.clear();
		fProperties.clear();
		if (context != null) {
			IMarker marker = context.getBreakpoint().getMarker();
			if (marker != null) {
				Map<String, Object> bpAttrs = Collections.emptyMap();
				try {
					bpAttrs = marker.getAttributes();
					fOriginalValues.putAll(bpAttrs);
					fProperties.putAll(bpAttrs);
				} catch (CoreException e) {
					DebugPlugin.log(e);
				}
			}
		}
		if (attributes != null) {
			fProperties.putAll(attributes);
			fIsDirty = true;
		}
	}

	public Map<String, Object> getAttributes() {
		return fProperties;
	}

	public void setCanceled(boolean canceled) {
		fIsCanceled = canceled;
	}

	public void save() throws IOException {
		if (!fIsCanceled && fContext != null && fContext.getBreakpoint() != null) {
			ICBreakpoint bp = fContext.getBreakpoint();
			if (bp.getMarker() != null && fIsDirty) {
				saveToExistingMarker(bp, bp.getMarker());
			} else {
				IResource resolved = getResource(fContext.getResource());
				if (resolved != null) {
					saveToNewMarker(bp, resolved);
				} else {
					throw new IOException("Unable to create breakpoint: no resource specified."); //$NON-NLS-1$
				}
			}
		}

	}

	/**
	 * Get the resource to apply the marker against. This may not be the same
	 * resource the dialog was created for if the user has selected a different
	 * resource.
	 * <p>
	 * If the {@link ICBreakpoint#SOURCE_HANDLE} resolves to the same file on
	 * the filesystem as the preferred resource the preferred resource is used.
	 *
	 * @param preferred
	 *            resource to use if it matches the SOURCE_HANDLE
	 * @return Resource to install marker on, or <code>null</code> for not
	 *         available.
	 */
	private IResource getResource(IResource preferred) {
		IResource resolved = null;
		String source = getString(ICBreakpoint.SOURCE_HANDLE);
		if (!"".equals(source)) { //$NON-NLS-1$
			IPath rawLocation = preferred.getRawLocation();
			if (rawLocation != null) {
				File file = rawLocation.toFile();
				File sourceFile = new File(source);
				if (file.getAbsoluteFile().equals(sourceFile.getAbsoluteFile())) {
					resolved = preferred;
				}
			}

			if (resolved == null) {
				IPath path = Path.fromOSString(source);
				IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
				IFile file = root.getFileForLocation(path);
				if (file == null) {
					resolved = root;
				} else {
					resolved = file;
				}
			}
		}
		if (resolved == null) {
			resolved = preferred;
		}
		return resolved;
	}

	private void saveToExistingMarker(final ICBreakpoint breakpoint, final IMarker marker) throws IOException {
		final List<String> changedProperties = new ArrayList<String>(5);
		Set<String> valueNames = fProperties.keySet();
		for (String name : valueNames) {
			if (fProperties.containsKey(name)) {
				Object originalObject = fOriginalValues.get(name);
				Object currentObject = fProperties.get(name);
				if (originalObject == null) {
					changedProperties.add(name);
				} else if (!originalObject.equals(currentObject)) {
					changedProperties.add(name);
				}
			}
		}
		if (!changedProperties.isEmpty()) {
			IWorkspaceRunnable wr = new IWorkspaceRunnable() {
				public void run(IProgressMonitor monitor) throws CoreException {
					Iterator<String> changed = changedProperties.iterator();
					while (changed.hasNext()) {
						String property = changed.next();
						if (property.equals(ICBreakpoint.ENABLED)) {
							breakpoint.setEnabled(getBoolean(ICBreakpoint.ENABLED));
						} else if (property.equals(ICBreakpoint.IGNORE_COUNT)) {
							breakpoint.setIgnoreCount(getInt(ICBreakpoint.IGNORE_COUNT));
						} else if (breakpoint instanceof ICTracepoint && property.equals(ICTracepoint.PASS_COUNT)) {
							((ICTracepoint) breakpoint).setPassCount(getInt(ICTracepoint.PASS_COUNT));
						} else if (breakpoint instanceof ICDynamicPrintf
								&& property.equals(ICDynamicPrintf.PRINTF_STRING)) {
							((ICDynamicPrintf) breakpoint).setPrintfString(getString(ICDynamicPrintf.PRINTF_STRING));
						} else if (property.equals(ICBreakpoint.CONDITION)) {
							breakpoint.setCondition(getString(ICBreakpoint.CONDITION));
						} else if (property.equals(IMarker.LINE_NUMBER)) {
							if (breakpoint instanceof ICLineBreakpoint2) {
								// refresh message and line number
								// Note there are no API methods to set the line number of a Line Breakpoint, so we
								// replicate what is done in CDIDebugModel.setLineBreakpointAttributes()
								// to set the line number fields properly and then refresh the message if possible
								((ICLineBreakpoint2) breakpoint).setRequestedLine(getInt(IMarker.LINE_NUMBER));
								breakpoint.getMarker().setAttribute(IMarker.LINE_NUMBER, getInt(IMarker.LINE_NUMBER));
								((ICBreakpoint2) breakpoint).refreshMessage();
							} else {
								// already workspace runnable, setting markers are safe
								breakpoint.getMarker().setAttribute(IMarker.LINE_NUMBER, getInt(IMarker.LINE_NUMBER));
								breakpoint.getMarker().setAttribute(ICLineBreakpoint2.REQUESTED_LINE,
										getInt(IMarker.LINE_NUMBER));
							}
						} else {
							// this allow set attributes contributed by other plugins
							Object value = fProperties.get(property);
							if (value != null) {
								marker.setAttribute(property, value);
								if (breakpoint instanceof ICBreakpoint2) {
									// To be safe, refresh the breakpoint message as the property
									// change might affect it.
									((ICBreakpoint2) breakpoint).refreshMessage();
								}
							}
						}
					}
				}
			};
			try {
				ResourcesPlugin.getWorkspace().run(wr, null);
			} catch (CoreException ce) {
				throw new IOException("Cannot save properties to breakpoint.", ce); //$NON-NLS-1$
			}
		}
	}

	private void saveToNewMarker(final ICBreakpoint breakpoint, final IResource resource) throws IOException {
		try {
			// On initial creation of BP, make sure that requested values of breakpoint
			// match the current values (i.e. make sure it starts as a not-relocated breakpoint)
			// See CDIDebugModel.setLineBreakpointAttributes
			if (fProperties.containsKey(ICLineBreakpoint2.REQUESTED_SOURCE_HANDLE)) {
				fProperties.put(ICLineBreakpoint2.REQUESTED_SOURCE_HANDLE, fProperties.get(ICBreakpoint.SOURCE_HANDLE));
			}
			if (fProperties.containsKey(ICLineBreakpoint2.REQUESTED_LINE)) {
				fProperties.put(ICLineBreakpoint2.REQUESTED_LINE, fProperties.get(IMarker.LINE_NUMBER));
			}
			if (fProperties.containsKey(ICLineBreakpoint2.REQUESTED_CHAR_START)) {
				fProperties.put(ICLineBreakpoint2.REQUESTED_CHAR_START, fProperties.get(IMarker.CHAR_START));
			}
			if (fProperties.containsKey(ICLineBreakpoint2.REQUESTED_CHAR_END)) {
				fProperties.put(ICLineBreakpoint2.REQUESTED_CHAR_END, fProperties.get(IMarker.CHAR_END));
			}

			CDIDebugModel.createBreakpointMarker(breakpoint, resource, fProperties, true);
		} catch (CoreException ce) {
			throw new IOException("Cannot save properties to new breakpoint.", ce); //$NON-NLS-1$
		}
	}

	///////////////////////////////////////////////////////////////////////
	// IPreferenceStore

	public boolean needsSaving() {
		return fIsDirty && !fIsCanceled;
	}

	public boolean contains(String name) {
		return fProperties.containsKey(name);
	}

	public void addPropertyChangeListener(IPropertyChangeListener listener) {
		fListeners.add(listener);
	}

	public void removePropertyChangeListener(IPropertyChangeListener listener) {
		fListeners.remove(listener);
	}

	public void firePropertyChangeEvent(String name, Object oldValue, Object newValue) {
		Object[] listeners = fListeners.getListeners();
		// Do we need to fire an event.
		if (listeners.length > 0 && (oldValue == null || !oldValue.equals(newValue))) {
			PropertyChangeEvent pe = new PropertyChangeEvent(this, name, oldValue, newValue);
			for (int i = 0; i < listeners.length; ++i) {
				IPropertyChangeListener l = (IPropertyChangeListener) listeners[i];
				l.propertyChange(pe);
			}
		}
	}

	public boolean getBoolean(String name) {
		boolean retVal = false;
		Object o = fProperties.get(name);
		if (o instanceof Boolean) {
			retVal = ((Boolean) o).booleanValue();
		}
		return retVal;
	}

	public int getInt(String name) {
		int retVal = 0;
		Object o = fProperties.get(name);
		if (o instanceof Integer) {
			retVal = ((Integer) o).intValue();
		}
		return retVal;
	}

	public String getString(String name) {
		String retVal = ""; //$NON-NLS-1$
		Object o = fProperties.get(name);
		if (o instanceof String) {
			retVal = (String) o;
		}
		return retVal;
	}

	public double getDouble(String name) {
		return 0;
	}

	public float getFloat(String name) {
		return 0;
	}

	public long getLong(String name) {
		return 0;
	}

	public boolean isDefault(String name) {
		return false;
	}

	public boolean getDefaultBoolean(String name) {
		return false;
	}

	public double getDefaultDouble(String name) {
		return 0;
	}

	public float getDefaultFloat(String name) {
		return 0;
	}

	public int getDefaultInt(String name) {
		return 0;
	}

	public long getDefaultLong(String name) {
		return 0;
	}

	public String getDefaultString(String name) {
		return null;
	}

	public void putValue(String name, String value) {
		Object oldValue = fProperties.get(name);
		if (oldValue == null || !oldValue.equals(value)) {
			fProperties.put(name, value);
			setDirty(true);
		}
	}

	public void setDefault(String name, double value) {
	}

	public void setDefault(String name, float value) {
	}

	public void setDefault(String name, int value) {
	}

	public void setDefault(String name, long value) {
	}

	public void setDefault(String name, String defaultObject) {
	}

	public void setDefault(String name, boolean value) {
	}

	public void setToDefault(String name) {
	}

	public void setValue(String name, boolean value) {
		boolean oldValue = getBoolean(name);
		if (oldValue != value) {
			fProperties.put(name, Boolean.valueOf(value));
			setDirty(true);
			firePropertyChangeEvent(name, Boolean.valueOf(oldValue), Boolean.valueOf(value));
		}
	}

	public void setValue(String name, int value) {
		int oldValue = getInt(name);
		if (oldValue != value) {
			fProperties.put(name, Integer.valueOf(value));
			setDirty(true);
			firePropertyChangeEvent(name, Integer.valueOf(oldValue), Integer.valueOf(value));
		}
	}

	public void setValue(String name, String value) {
		Object oldValue = fProperties.get(name);
		if ((oldValue == null && value != null) || (oldValue != null && !oldValue.equals(value))) {
			fProperties.put(name, value);
			setDirty(true);
			firePropertyChangeEvent(name, oldValue, value);
		}
	}

	public void setValue(String name, float value) {
	}

	public void setValue(String name, double value) {
	}

	public void setValue(String name, long value) {
	}

	// IPreferenceStore
	///////////////////////////////////////////////////////////////////////

	private void setDirty(boolean isDirty) {
		fIsDirty = isDirty;
	}
}

Back to the top