Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7ffff8904a651439edaa46fbed7039df6d56a4f5 (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
/*******************************************************************************
 * Copyright (c) 2010, 2012 Freescale Semiconductor 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:
 * Freescale Semiconductor - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.debug.internal.core.srcfinder;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.cdt.core.ISourceFinder;
import org.eclipse.cdt.core.model.IBinary;
import org.eclipse.cdt.debug.core.CDebugCorePlugin;
import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants;
import org.eclipse.cdt.debug.core.sourcelookup.ProgramRelativePathSourceContainer;
import org.eclipse.cdt.debug.internal.core.sourcelookup.CSourceLookupDirector;
import org.eclipse.cdt.internal.core.model.ExternalTranslationUnit;
import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.variables.VariablesPlugin;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationListener;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.ILaunchesListener;
import org.eclipse.debug.core.model.IPersistableSourceLocator;
import org.eclipse.debug.core.model.ISourceLocator;
import org.eclipse.debug.core.sourcelookup.IPersistableSourceLocator2;
import org.eclipse.debug.core.sourcelookup.ISourceLookupDirector;
import org.eclipse.debug.core.sourcelookup.containers.LocalFileStorage;

public class CSourceFinder implements ISourceFinder, ILaunchConfigurationListener, ILaunchesListener {

	/**
	 * The binary we are searching files for. We need this reference to find a
	 * matching ILaunch or ILaunchConfiguration if the caller needs to search
	 * for files when it doesn't have a debug context.
	 */
	private IBinary fBinary;

	/**
	 * The locator tied to an ILaunch or an ILaunchConfiguration that is
	 * associated with the binary. Used for searching when the caller has no
	 * debug context. See {@link ISourceFinder#toLocalPath(String)} for
	 * performance considerations that dictate how the locator is chosen. Access
	 * this only from synchronized blocks as the field is subject to be changed
	 * by listener invocations.
	 */
	private ISourceLookupDirector fLaunchLocator;

	/**
	 * A launch configuration doesn't have a source locator instance tied to it.
	 * Instead, one is created on the fly as needed from attributes in the
	 * launch config. This is a heavy operation. As an optimization, we cache
	 * the locators we create and discard when the launch config changes or is
	 * disposed. Collection is subject to be changed by listener invocations.
	 * Map key is the launch configuration name.
	 * 
	 * @see CSourceFinder#getLocator(ILaunchConfiguration)
	 */
	private Map<String, ISourceLocator> fConfigLocators = Collections.synchronizedMap(new HashMap<String, ISourceLocator>());

	/**
	 * We use this when we don't have an ILaunch or ILaunchConfiguration
	 * locator. A program relative container instance is automatically added to
	 * every CDT launch configuration. So when we lack a configuration context,
	 * we rely on this container to help us resolve relative paths.
	 */
	private ProgramRelativePathSourceContainer fRelativePathContainer;

	/**
	 * Constructor.
	 * 
	 * @param binary
	 *            the executable whose source files we will be asked to find
	 *            locally
	 */
	public CSourceFinder(IBinary binary) {
		assert(binary != null);
		fBinary = binary;
		
		fRelativePathContainer = new ProgramRelativePathSourceContainer(binary); 
		
		ILaunchManager lmgr = DebugPlugin.getDefault().getLaunchManager();
		lmgr.addLaunchConfigurationListener(this);
		lmgr.addLaunchListener(this);
		
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.ISourceFinder#toLocalPath(java.lang.String)
	 */
	@Override
	synchronized public String toLocalPath(String compilationPath) {
		try {
			Object foundElement = null;
			
			// Find a suitable launch/config locator if we haven't found one yet
			if (fLaunchLocator == null) {
				ILaunchManager lmgr = DebugPlugin.getDefault().getLaunchManager();
			
				// See if there are any active debug sessions (running, or
				// terminated but still in the Debug view) that are targeting
				// our executable. If there are then use the first one to
				// provide a locator
				ILaunch[] launches = lmgr.getLaunches();
				for (ILaunch launch : launches) {
					ILaunchConfiguration config = launch.getLaunchConfiguration();
					if (config != null && isMatch(config)) {
						ISourceLocator launchLocator = launch.getSourceLocator();
						// in practice, a launch locator is always an ISourceLookupDirector
						if (launchLocator instanceof ISourceLookupDirector) {
							fLaunchLocator = (ISourceLookupDirector)launchLocator;
							break;
						}
					}
				}
				
				// If there were no matching launches or none of them
				// provided a locator, search the launch configurations
				if (fLaunchLocator == null) {
					for (ILaunchConfiguration config : lmgr.getLaunchConfigurations()) {
						if (isMatch(config)) {
							String configName = config.getName();
							
							// Search our cache of locators that we
							// instantiate for configurations. Create one if
							// not found
							ISourceLocator configLocator = fConfigLocators.get(configName);
							if (configLocator == null) {
								configLocator = getLocator(config);	// heavy operation
								fConfigLocators.put(configName, configLocator);	// cache to avoid next time
							}
							// In practice, a config's locator is always an ISourceLookupDirector
							if (configLocator instanceof ISourceLookupDirector) {
								fLaunchLocator = (ISourceLookupDirector)configLocator;
								break;
							}
						}
					}
				}
			}
			
			// Search for the file using the launch/config locator
			if (fLaunchLocator != null) {
				foundElement = fLaunchLocator.getSourceElement(compilationPath);
			}
			else {
				// If there isn't a launch/config locator, we need to explicitly
				// try to resolve relative paths...relative to the binary
				// location.
				Object[] elements = fRelativePathContainer.findSourceElements(compilationPath);
				if (elements.length > 0) {
					assert elements.length == 1; // relative path container should return at most one element
					foundElement = elements[0];
				}
			}

			// If we didn't search using the locator of an ILaunch or
			// ILaunchConfiguration, look in the global (common) locator. The
			// common locator is automatically included in every
			// launch/configuration locator, which is why we skip looking
			// through the common one again here.
			if ((fLaunchLocator == null) && (foundElement == null)) {
				CSourceLookupDirector locator = CDebugCorePlugin.getDefault().getCommonSourceLookupDirector();
				foundElement = locator.getSourceElement(compilationPath);
			}

			return foundElementToPath(foundElement);
		}
		catch (CoreException exc) {
			CDebugCorePlugin.log(exc);			
		}
		return null;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.ISourceFinder#toLocalPath(org.eclipse.core.runtime.IAdaptable, java.lang.String)
	 */
	@Override
	public String toLocalPath(IAdaptable _launch, String compilationPath) {
		Object foundElement = null;
		
		ILaunch launch = _launch.getAdapter(ILaunch.class);
		if (launch != null) {
			ISourceLocator locator = launch.getSourceLocator();
			// in practice, a launch locator is always an ISourceLookupDirector
			if (locator instanceof ISourceLookupDirector) {
				foundElement = ((ISourceLookupDirector)locator).getSourceElement(compilationPath);
			}
		}

		// If not found, look in the global (common) locator
		if (foundElement == null) {
			CSourceLookupDirector locator = CDebugCorePlugin.getDefault().getCommonSourceLookupDirector();
			foundElement = locator.getSourceElement(compilationPath);
		}
		
		return foundElementToPath(foundElement);
	}

	/**
	 * Utility method to convert the element found by the source locators to a
	 * canonical file path
	 * 
	 * @param foundElement
	 *            the element found by the source locator, or null if not found
	 * @return the canonical file path of the element
	 */
	private static String foundElementToPath(Object foundElement) {
		if (foundElement != null) {
			try {
				if (foundElement instanceof IFile) {
					IPath path = ((IFile)foundElement).getLocation();
					if (path != null) {
						File file = path.toFile();
						if (file != null) {
								return file.getCanonicalPath();
						}
					}
					
				}
				else if (foundElement instanceof LocalFileStorage) {
					File file = ((LocalFileStorage)foundElement).getFile();
					if (file != null) {
						return file.getCanonicalPath();
					}
				}
				else if (foundElement instanceof ExternalTranslationUnit) {
					URI uri = ((ExternalTranslationUnit)foundElement).getLocationURI();
					if (uri != null) {
						IPath path = URIUtil.toPath(uri);
						if (path != null) {
							File file = path.toFile();
							if (file != null) {
								return file.getCanonicalPath();
							}
						}
					}
				}
			} catch (IOException e) {
				CDebugCorePlugin.log(e);
			}
		}
		
		return null;
	}
	
	/**
	 * Utility method to determine if the given launch configuration targets the Binary we are associated with
	 * @param config
	 * @return true if the launch config targets our binary, false otherwise
	 */
	private boolean isMatch(ILaunchConfiguration config) {
		IResource resource = fBinary.getAdapter(IResource.class);
		if (resource != null) {
			String binaryPath = resource.getFullPath().toString();
			try {
				String projectNameConfig = config.getAttribute(ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME, ""); //$NON-NLS-1$
				String programNameConfig = config.getAttribute(ICDTLaunchConfigurationConstants.ATTR_PROGRAM_NAME, ""); //$NON-NLS-1$
				IProject project = resource.getProject();
				if (project != null && project.getName().equals(projectNameConfig)) {
					programNameConfig = VariablesPlugin.getDefault().getStringVariableManager().performStringSubstitution(programNameConfig);
					Path path = new Path(programNameConfig);
					if (!path.isEmpty()) {
						IFile file = project.getFile(path);
						if (file != null) {
							String fullPath = file.getFullPath().toString();
							return fullPath.equals(binaryPath);
						}
					}
				}
			} catch (CoreException e) {
				// Problem getting attribute from launch config? Not expecting that.
				CDebugCorePlugin.log(e);
			}
		}
		
		return false;
	}

	/**
	 * Utility method to instantiate a source locator for a launch
	 * configuration. A launch configuration doesn't have a source locator
	 * instance tied to it. Transient instances are created as needed. from
	 * attributes in the launch config. This is a heavy operation.
	 * 
	 * @param config
	 *            the launch configuration to create the locator for
	 * @return the source locator
	 * @throws CoreException
	 */
	static private ISourceLocator getLocator(ILaunchConfiguration config) throws CoreException {
		String type = config.getAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_ID, (String)null);
		if (type == null) {
			type = config.getType().getSourceLocatorId();
		}
		if (type != null) {
			IPersistableSourceLocator locator = DebugPlugin.getDefault().getLaunchManager().newSourceLocator(type);
			String memento = config.getAttribute(ILaunchConfiguration.ATTR_SOURCE_LOCATOR_MEMENTO, (String)null);
			if (memento == null) {
				locator.initializeDefaults(config);
			} else {
				if(locator instanceof IPersistableSourceLocator2)
					((IPersistableSourceLocator2)locator).initializeFromMemento(memento, config);
				else
					locator.initializeFromMemento(memento);
			}
			return locator;
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.ILaunchConfigurationListener#launchConfigurationAdded(org.eclipse.debug.core.ILaunchConfiguration)
	 */
	@Override
	public void launchConfigurationAdded(ILaunchConfiguration config) {
		// Don't care.  
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.ILaunchConfigurationListener#launchConfigurationChanged(org.eclipse.debug.core.ILaunchConfiguration)
	 */
	@Override
	synchronized public void launchConfigurationChanged(ILaunchConfiguration config) {
		// We don't care if it's a working copy.
		if (config.isWorkingCopy()) {
			return;
		}
		
		// the source locator attribute may have changed
		fConfigLocators.remove(config.getName());
		if ((fLaunchLocator != null) && (fLaunchLocator.getLaunchConfiguration().getName() == config.getName())) {
			fLaunchLocator = null;
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.ILaunchConfigurationListener#launchConfigurationRemoved(org.eclipse.debug.core.ILaunchConfiguration)
	 */
	@Override
	synchronized public void launchConfigurationRemoved(ILaunchConfiguration config) {
		// We don't care if it's a working copy.
		if (config.isWorkingCopy()) {
			return;
		}
		
		fConfigLocators.remove(config.getName());
		if ((fLaunchLocator != null) && (fLaunchLocator.getLaunchConfiguration().getName() == config.getName())) {
			fLaunchLocator = null;
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.ILaunchesListener#launchesRemoved(org.eclipse.debug.core.ILaunch[])
	 */
	@Override
	synchronized public void launchesRemoved(ILaunch[] launches) {
		for (ILaunch launch : launches) {
			if (launch.getSourceLocator() == fLaunchLocator) {
				fLaunchLocator = null;
				return;
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.ILaunchesListener#launchesAdded(org.eclipse.debug.core.ILaunch[])
	 */
	@Override
	public void launchesAdded(ILaunch[] launches) {
		// If there's a new launch in town, we need to take it into
		// consideration. E.g., if it targets our binary, and we're currently
		// searching using an inactive launch configuration's locator, then the
		// new launch's locator should take precedence
		for (ILaunch launch : launches) {
			ILaunchConfiguration config = launch.getLaunchConfiguration();
			if (config != null && isMatch(config)) {
				synchronized(this) {
					fLaunchLocator = null;
				}
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.ILaunchesListener#launchesChanged(org.eclipse.debug.core.ILaunch[])
	 */
	@Override
	public void launchesChanged(ILaunch[] launches) {
		// don't care. I don't think setting a new locator in a launch would result in us getting notified
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.ISourceFinder#dispose()
	 */
	@Override
	public void dispose() {
		ILaunchManager lmgr = DebugPlugin.getDefault().getLaunchManager();
		lmgr.removeLaunchConfigurationListener(this);
		lmgr.removeLaunchListener(this);
	}
}

Back to the top