Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 124596bb58f4ff342f4d4a6587e37d3357161fbd (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
/*******************************************************************************
 * Copyright (c) 2008, 2010 Nokia 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:
 * Nokia - Initial API and implementation
 *******************************************************************************/

package org.eclipse.cdt.debug.core.executables;

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

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.internal.core.model.CModelManager;
import org.eclipse.cdt.internal.core.model.ExternalTranslationUnit;
import org.eclipse.cdt.internal.core.model.TranslationUnit;
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.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.PlatformObject;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.core.runtime.content.IContentTypeManager;

public class Executable extends PlatformObject {

	static public boolean isExecutableFile(IPath path) {
		// ignore directories
		if (path.toFile().isDirectory()) {
			return false;
		}
		// Only if file has no extension, has an extension that is an integer
		// or is a binary file content type
		String ext = path.getFileExtension();
		if (ext != null) {
			// shared libraries often have a version number
			boolean isNumber = true;
			for (int i = 0; i < ext.length(); ++i)
				if (!Character.isDigit(ext.charAt(i))) {
					isNumber = false;
					break;
				}
			if (!isNumber) {
				boolean isBinary = false;
				final IContentTypeManager ctm = Platform.getContentTypeManager();
				final IContentType ctbin = ctm.getContentType(CCorePlugin.CONTENT_TYPE_BINARYFILE);
				final IContentType[] cts = ctm.findContentTypesFor(path.toFile().getName());
				for (int i = 0; !isBinary && i < cts.length; i++) {
					isBinary = cts[i].isKindOf(ctbin);
				}
				if (!isBinary) {
					return false;
				}
			}
		}
		return true;
	}

	@Override
	public boolean equals(Object arg0) {
		if (arg0 instanceof Executable)
		{
			Executable exe = (Executable)arg0;
			return exe.getPath().equals(this.getPath());
		}
		return super.equals(arg0);
	}

	private final IPath executablePath;
	private final IProject project;
	private final String name;
	private final IResource resource;
	private final Map<ITranslationUnit, String> remappedPaths;
	private final ArrayList<ITranslationUnit> sourceFiles;
	/** see {@link #setRefreshSourceFiles(boolean)} */
	private boolean refreshSourceFiles;
	/** see {@link #setRemapSourceFiles(boolean) */
	private boolean remapSourceFiles;
	private ISourceFileRemapping[] remappers;
	/** The (unmapped) file specifications found in the executable */
	private String[] symReaderSources;

	public IPath getPath() {
		return executablePath;
	}

	public IProject getProject() {
		return project;
	}

	/**
	 * @since 7.0
	 */
	public Executable(IPath path, IProject project, IResource resource, ISourceFileRemapping[] sourceFileRemappings) {
		this.executablePath = path;
		this.project = project;
		this.name = new File(path.toOSString()).getName();
		this.resource = resource;
		this.remappers = sourceFileRemappings;		
		remappedPaths = new HashMap<ITranslationUnit, String>();
		sourceFiles = new ArrayList<ITranslationUnit>();
		refreshSourceFiles = true;
		remapSourceFiles = true;
	}

	public IResource getResource() {
		return resource;
	}

	@Override
	public String toString() {
		return executablePath.toString();
	}

	public String getName() {
		return name;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Object getAdapter(Class adapter) {
		if (adapter.equals(IResource.class))
			if (getResource() != null)
				return getResource();
			else
				return this.getProject();
		return super.getAdapter(adapter);
	}
	
	private String remapSourceFile(String filename) {
		for (ISourceFileRemapping remapper : remappers) {
			String remapped = remapper.remapSourceFile(this.getPath(), filename);
			if (!remapped.equals(filename)) {
				return remapped;
			}
		}
		return filename;
	}
	

	/**
	 * @noreference This method is not intended to be referenced by clients.
	 * @since 6.0
	 */
	public synchronized ITranslationUnit[] getSourceFiles(IProgressMonitor monitor) {
		
		if (!refreshSourceFiles && !remapSourceFiles)
			return sourceFiles.toArray(new TranslationUnit[sourceFiles.size()]) ;
		
		// Try to get the list of source files used to build the binary from the
		// symbol information.

		remappedPaths.clear();

		sourceFiles.clear();

		CModelManager factory = CModelManager.getDefault();

		ICProject cproject = factory.create(project);

		if (refreshSourceFiles)
		{
			symReaderSources = ExecutablesManager.getExecutablesManager().getSourceFiles(this, monitor);
		}
		if (symReaderSources != null && symReaderSources.length > 0) {
			for (String filename : symReaderSources) {
				String orgPath = filename;

				filename = remapSourceFile(filename);

				// Sometimes the path in the symbolics will have a different
				// case than the actual file system path. Even if the file
				// system is not case sensitive this will confuse the Path
				// class. So make sure the path is canonical, otherwise
				// breakpoints won't be resolved, etc.. Also check for relative
				// path names and attempt to resolve them relative to the
				// executable.

				boolean fileExists = false;

				try {
					File file = new File(filename);
					fileExists = file.exists();
					if (fileExists) {
						filename = file.getCanonicalPath();
					} else if (filename.startsWith(".")) { //$NON-NLS-1$
						file = new File(executablePath.removeLastSegments(1).toOSString(), filename);
						filename = file.getCanonicalPath();
					}
				} catch (IOException e) { // Do nothing.
				}

				IFile wkspFile = null;
				IFile sourceFile = getProject().getFile(filename);
				IPath sourcePath = new Path(filename);
				if (fileExists) {
					// See if this source file is already in the project.
					// We check this to determine if we should create a
					// TranslationUnit or ExternalTranslationUnit

					if (sourceFile.exists())
						wkspFile = sourceFile;
					else {
						IFile[] filesInWP = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(URIUtil.toURI(sourcePath));
						for (IFile fileInWP : filesInWP) {
							if (fileInWP.isAccessible()) {
								wkspFile = fileInWP;
								break;
							}
						}
					}
				}

				// Create a translation unit for this file and add it as
				// a child of the binary
				String id = CoreModel.getRegistedContentTypeId(sourceFile.getProject(), sourceFile.getName());

				if (id != null) { // Don't add files we can't get an
									// ID for.
					TranslationUnit tu;
					if (wkspFile != null)
						tu = new TranslationUnit(cproject, wkspFile, id);
					else {
						// Be careful not to convert a unix path like
						// "/src/home" to "c:\source\home" on Windows. See
						// bugzilla 297781
						URI uri = (sourcePath.toFile().exists()) ? URIUtil.toURI(sourcePath) : URIUtil.toURI(filename, true);						
						tu = new ExternalTranslationUnit(cproject, uri, id);
					}

					if (!sourceFiles.contains(tu)) {
						sourceFiles.add(tu);
					}

					if (!orgPath.equals(filename)) {
						remappedPaths.put(tu, orgPath);
					}
				}
			}
		}
		
		refreshSourceFiles = false;
		remapSourceFiles = false;
		return sourceFiles.toArray(new TranslationUnit[sourceFiles.size()]) ;
	}

	/**
	 * Call this to force a subsequent call to
	 * {@link #getSourceFiles(IProgressMonitor)} to re-fetch the list of
	 * source files referenced in the executable. Digging into the binary for
	 * that list can be expensive, so we cache the results. However, if the
	 * executable is rebuilt, the cache can no longer be trusted.
	 * 
	 * Note that calling this also invalidates any mappings we have cached, so
	 * there is no need to call both this method and
	 * {@link #setRemapSourceFiles(boolean)}. That latter is automatic.
	 * 
	 * @since 6.0
	 */
	public void setRefreshSourceFiles(boolean refreshSourceFiles) {
		this.refreshSourceFiles = refreshSourceFiles;
	}

	public synchronized String getOriginalLocation(ITranslationUnit tu) {
		String orgLocation = remappedPaths.get(tu);
		if (orgLocation == null)
			orgLocation = tu.getLocation().toOSString();
		return orgLocation;
	}

	/**
	 * Call this to force a subsequent call to
	 * {@link #getSourceFiles(IProgressMonitor)} to remap the source files
	 * referenced in the binary. Mapping a source file means running the file
	 * specification found in the executable through any applicable source
	 * locators. Source locators are used to convert a file specification found
	 * in an executable to a usable path on the local machine. E.g., a user
	 * debugging an executable built by someone else likely needs to configure
	 * source locator(s) to point Eclipse to his local copy of the sources.
	 * 
	 * <p>
	 * Remapping source paths is expensive, so we cache the results. However, if
	 * applicable source locators have been added, removed or changed, then the
	 * cache can no longer be trusted.
	 * 
	 * <p>
	 * Note that we separately cache the (unmapped) file specifications
	 * referenced in the executable, as that is also expensive to fetch. Calling
	 * this method does not invalidate that cache. However, that cache can be
	 * invalidated via {@link #setRefreshSourceFiles(boolean)}, which also ends
	 * up invalidating any mappings we have cached.
	 * 
	 * @since 7.0
	 */
	public void setRemapSourceFiles(boolean remapSourceFiles) {
		this.remapSourceFiles = remapSourceFiles;
	}

}

Back to the top