Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ca1b5e13df116def14732690f76d8a7ede4c8f36 (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
/*******************************************************************************
 * Copyright (c) 2007, 2016 Wind River 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:
 *     Wind River Systems - initial API and implementation
 *     Ericsson           - Added tracepoint support (284286)
 *     Marc Khouzam (Ericsson) - Added dynamic printf support (400628)
 *******************************************************************************/
package org.eclipse.cdt.debug.internal.ui.breakpoints;

import java.util.Map;

import org.eclipse.cdt.debug.core.CDIDebugModel;
import org.eclipse.cdt.debug.core.model.ICAddressBreakpoint;
import org.eclipse.cdt.debug.core.model.ICBreakpoint;
import org.eclipse.cdt.debug.core.model.ICDynamicPrintf;
import org.eclipse.cdt.debug.core.model.ICEventBreakpoint;
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.debug.ui.breakpoints.ICBreakpointContext;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.debug.core.model.IBreakpoint;
import org.eclipse.debug.core.model.IDebugElement;
import org.eclipse.debug.core.model.IDebugModelProvider;
import org.eclipse.debug.ui.contexts.IDebugContextListener;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IActionFilter;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.model.IWorkbenchAdapter;

/**
 * Input for breakpoint properties dialog.  It captures both the
 * selected breakpoint object as well as the selected debug context.
 * This combined context can then be used by breakpoint property
 * pages to access model and target specific breakpoint settings.
 */
public class CBreakpointContext extends PlatformObject implements ICBreakpointContext {

	// Register an adapter factory for the class when it is first loaded.
	static {
		Platform.getAdapterManager().registerAdapters(new CBreakpointContextAdapterFactory(), CBreakpointContext.class);
	}

	/**
	 * Breakpoint object held by this context.
	 */
	private final ICBreakpoint fBreakpoint;

	/**
	 * The resource that the breakpoint is to be created for.
	 */
	private final IResource fResource;

	/**
	 * The active debug context held by this context.
	 */
	private final ISelection fDebugContext;

	/**
	 * Associated preference store.
	 */
	private final CBreakpointPreferenceStore fPreferenceStore;

	/**
	 * Creates a new breakpoint context with given breakpoint and debug
	 * context selection.
	 */
	public CBreakpointContext(ICBreakpoint breakpoint, ISelection debugContext) {
		this(breakpoint, debugContext, null, null);
	}

	public CBreakpointContext(ICBreakpoint breakpoint, ISelection debugContext, IResource resource,
			Map<String, Object> attributes) {
		fBreakpoint = breakpoint;
		fResource = resource;
		fDebugContext = debugContext;
		fPreferenceStore = new CBreakpointPreferenceStore(this, attributes);
	}

	@Override
	public ICBreakpoint getBreakpoint() {
		return fBreakpoint;
	}

	@Override
	public IResource getResource() {
		return fResource;
	}

	@Override
	public IPreferenceStore getPreferenceStore() {
		return fPreferenceStore;
	}

	/**
	 * Returns the debug context.
	 */
	public ISelection getDebugContext() {
		return fDebugContext;
	}

	/**
	 * (non-Javadoc)
	 * @see org.eclipse.debug.ui.contexts.IDebugContextProvider implementation
	 */
	@Override
	public IWorkbenchPart getPart() {
		return null;
	}

	@Override
	public void addDebugContextListener(IDebugContextListener listener) {
	}

	@Override
	public void removeDebugContextListener(IDebugContextListener listener) {
	}

	@Override
	public ISelection getActiveContext() {
		return fDebugContext;
	}
}

/**
 * Action filter for the breakpoint context, which allows property
 * pages to filter their activation based on the debug model ID of
 * the active debug context.
 */
class CBreakpointContextActionFilter implements IActionFilter {

	private static String[] EMPTY_IDENTIFIERS_ARRAY = new String[0];

	@Override
	public boolean testAttribute(Object target, String name, String value) {
		if (target instanceof CBreakpointContext) {
			if ("debugModelId".equals(name)) { //$NON-NLS-1$
				String[] targetModelIds = getDebugModelIds((CBreakpointContext) target);
				for (int i = 0; i < targetModelIds.length; i++) {
					if (targetModelIds[i].equals(value)) {
						return true;
					}
				}
			}
		}
		return false;
	}

	private String[] getDebugModelIds(CBreakpointContext bpContext) {
		ISelection debugContext = bpContext.getDebugContext();
		if (debugContext instanceof IStructuredSelection) {
			Object debugElement = ((IStructuredSelection) debugContext).getFirstElement();
			if (debugElement instanceof IAdaptable) {
				IDebugModelProvider debugModelProvider = ((IAdaptable) debugElement)
						.getAdapter(IDebugModelProvider.class);
				if (debugModelProvider != null) {
					return debugModelProvider.getModelIdentifiers();
				} else if (debugElement instanceof IDebugElement) {
					return new String[] { ((IDebugElement) debugElement).getModelIdentifier() };
				}
			}
		}
		return EMPTY_IDENTIFIERS_ARRAY;
	}
}

class CBreakpointContextWorkbenchAdapter implements IWorkbenchAdapter {
	@Override
	public String getLabel(Object o) {
		if (o instanceof ICBreakpointContext) {
			ICBreakpointContext context = (ICBreakpointContext) o;
			return getBreakpointMainLabel(context.getBreakpoint(), context);
		}
		return ""; //$NON-NLS-1$
	}

	@Override
	public Object[] getChildren(Object o) {
		return null;
	}

	@Override
	public ImageDescriptor getImageDescriptor(Object object) {
		return null;
	}

	@Override
	public Object getParent(Object o) {
		return null;
	}

	private String getBreakpointMainLabel(ICBreakpoint breakpoint, ICBreakpointContext o) {
		if (breakpoint instanceof ICFunctionBreakpoint) {
			if (breakpoint instanceof ICTracepoint) {
				return BreakpointsMessages.getString("TracepointPropertyPage.tracepointType_function_label"); //$NON-NLS-1$
			} else if (breakpoint instanceof ICDynamicPrintf) {
				return BreakpointsMessages.getString("DPrintfPropertyPage.dprintfType_function_label"); //$NON-NLS-1$
			} else {
				return BreakpointsMessages.getString("CBreakpointPropertyPage.breakpointType_function_label"); //$NON-NLS-1$
			}
		} else if (breakpoint instanceof ICAddressBreakpoint) {
			if (breakpoint instanceof ICTracepoint) {
				return BreakpointsMessages.getString("TracepointPropertyPage.tracepointType_address_label"); //$NON-NLS-1$
			} else if (breakpoint instanceof ICDynamicPrintf) {
				return BreakpointsMessages.getString("DPrintfPropertyPage.dprintfType_address_label"); //$NON-NLS-1$
			} else {
				return BreakpointsMessages.getString("CBreakpointPropertyPage.breakpointType_address_label"); //$NON-NLS-1$
			}
		} else if (breakpoint instanceof ICLineBreakpoint) {
			if (breakpoint instanceof ICTracepoint) {
				return BreakpointsMessages.getString("TracepointPropertyPage.tracepointType_line_label"); //$NON-NLS-1$
			} else if (breakpoint instanceof ICDynamicPrintf) {
				return BreakpointsMessages.getString("DPrintfPropertyPage.dprintfType_line_label"); //$NON-NLS-1$
			} else {
				return BreakpointsMessages.getString("CBreakpointPropertyPage.breakpointType_line_label"); //$NON-NLS-1$
			}
		} else if (breakpoint instanceof ICEventBreakpoint) {
			return BreakpointsMessages.getString("CBreakpointPropertyPage.breakpointType_event_label"); //$NON-NLS-1$
		} else if (breakpoint instanceof ICWatchpoint) {
			if (breakpoint.getMarker() != null) {
				// For an existing breakpoint, calculate watchpoint label based
				// on read/write type.
				boolean isReadType = o.getPreferenceStore().getBoolean(ICWatchpoint.READ);
				boolean isWriteType = o.getPreferenceStore().getBoolean(ICWatchpoint.WRITE);
				if (isReadType && !isWriteType) {
					return BreakpointsMessages
							.getString("CBreakpointPropertyPage.breakpointType_watchpoint_read_label"); //$NON-NLS-1$
				} else if (!isReadType && isWriteType) {
					return BreakpointsMessages.getString("CBreakpointPropertyPage.breakpointType_watchpoint_label"); //$NON-NLS-1$
				} else {
					return BreakpointsMessages
							.getString("CBreakpointPropertyPage.breakpointType_watchpoint_access_label"); //$NON-NLS-1$
				}
			}
			return BreakpointsMessages.getString("CBreakpointPropertyPage.breakpointType_watchpoint_label"); //$NON-NLS-1$
		}
		// default main label is the label of marker type for the breakpoint
		return CDIDebugModel.calculateMarkerType(breakpoint);
	}

}

/**
 * Adapter factory which returns the breakpoint object and the action
 * filter for the CBreakpointContext type.
 */
class CBreakpointContextAdapterFactory implements IAdapterFactory {

	private static final Class<?>[] fgAdapterList = new Class[] { IBreakpoint.class, ICBreakpoint.class,
			ICTracepoint.class, ICDynamicPrintf.class, IActionFilter.class, IPreferenceStore.class,
			IWorkbenchAdapter.class, };

	private static final IActionFilter fgActionFilter = new CBreakpointContextActionFilter();
	private static final IWorkbenchAdapter fgWorkbenchAdapter = new CBreakpointContextWorkbenchAdapter();

	@SuppressWarnings("unchecked")
	@Override
	public <T> T getAdapter(Object obj, Class<T> adapterType) {
		// Note: only return the breakpoint object as an adapter if it has
		// an associated marker.  Otherwise the property pages will throw multiple
		// exceptions.
		if (adapterType.isInstance(((CBreakpointContext) obj).getBreakpoint())
				&& ((CBreakpointContext) obj).getBreakpoint().getMarker() != null) {
			return (T) ((CBreakpointContext) obj).getBreakpoint();
		}

		if (IPreferenceStore.class.equals(adapterType)) {
			return (T) ((CBreakpointContext) obj).getPreferenceStore();
		}

		if (IActionFilter.class.equals(adapterType)) {
			return (T) fgActionFilter;
		}

		if (IWorkbenchAdapter.class.equals(adapterType)) {
			return (T) fgWorkbenchAdapter;
		}

		return null;
	}

	@Override
	public Class<?>[] getAdapterList() {
		return fgAdapterList;
	}
}

Back to the top