Skip to main content
summaryrefslogtreecommitdiffstats
blob: c03a5e11221e050c2701c5d762cf41b075732da8 (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
/*******************************************************************************
 * Copyright (c) 2008 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.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.IBinaryParser;
import org.eclipse.cdt.core.ISymbolReader;
import org.eclipse.cdt.core.IBinaryParser.IBinaryFile;
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.BinaryParserConfig;
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.CoreException;
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 {

	private IPath path;
	private IProject project;
	private String name;
	private IResource resource;
	private Map<ITranslationUnit, String> remappedPaths;
	private ArrayList<ITranslationUnit> sourceFiles;
	private boolean refreshSourceFiles;

	public IPath getPath() {
		return path;
	}

	public IProject getProject() {
		return project;
	}

	public Executable(IPath path, IProject project, IResource resource) {
		this.path = path;
		this.project = project;
		this.name = new File(path.toOSString()).getName();
		this.resource = resource;
		remappedPaths = new HashMap<ITranslationUnit, String>();
		sourceFiles = new ArrayList<ITranslationUnit>();
		refreshSourceFiles = true;
	}

	public IResource getResource() {
		return resource;
	}

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

	public String getName() {
		return name;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Object getAdapter(Class adapter) {
		if (adapter.equals(IResource.class))
			if (getResource() != null)
				return getResource();
			else
				return this.getProject();
		return super.getAdapter(adapter);
	}

	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;
	}

	public IBinaryFile createBinaryFile() {
		CModelManager factory = CModelManager.getDefault();

		if (resource != null && resource instanceof IFile)
			return factory.createBinaryFile((IFile) resource);

		BinaryParserConfig[] parsers = factory.getBinaryParser(getProject());
		if (parsers.length == 0) {
			return null;
		}

		if (!isExecutableFile(path))
			return null;

		File f = new File(path.toOSString());
		if (f.length() == 0) {
			return null;
		}

		int hints = 0;

		for (int i = 0; i < parsers.length; i++) {
			IBinaryParser parser = null;
			try {
				parser = parsers[i].getBinaryParser();
				if (parser.getHintBufferSize() > hints) {
					hints = parser.getHintBufferSize();
				}
			} catch (CoreException e) {
			}
		}
		byte[] bytes = new byte[hints];
		if (hints > 0) {
			InputStream is = null;
			try {
				is = new FileInputStream(path.toFile());
				int count = 0;
				// Make sure we read up to 'hints' bytes if we possibly can
				while (count < hints) {
					int bytesRead = is.read(bytes, count, hints - count);
					if (bytesRead < 0)
						break;
					count += bytesRead;
				}
				if (count > 0 && count < bytes.length) {
					byte[] array = new byte[count];
					System.arraycopy(bytes, 0, array, 0, count);
					bytes = array;
				}
			} catch (IOException e) {
				return null;
			} finally {
				if (is != null) {
					try {
						is.close();
					} catch (IOException e) {
						// ignore
					}
				}
			}
		}

		for (int i = 0; i < parsers.length; i++) {
			try {
				IBinaryParser parser = parsers[i].getBinaryParser();
				if (parser.isBinary(bytes, path)) {
					IBinaryFile binFile = parser.getBinary(bytes, path);
					if (binFile != null) {
						return binFile;
					}
				}
			} catch (IOException e) {
			} catch (CoreException e) {
			}
		}
		return null;
	}

	public TranslationUnit[] getSourceFiles(IProgressMonitor monitor) {
		
		if (!refreshSourceFiles)
			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();
		IBinaryFile bin = createBinaryFile();

		if (bin != null) {
			ICProject cproject = factory.create(project);

			ISymbolReader symbolreader = (ISymbolReader) bin.getAdapter(ISymbolReader.class);
			if (symbolreader != null) {
				String[] symReaderSources = symbolreader.getSourceFiles();
				if (symReaderSources != null && symReaderSources.length > 0) {
					for (int i = 0; i < symReaderSources.length; i++) {
						String filename = symReaderSources[i];
						String orgPath = filename;

						filename = ExecutablesManager.getExecutablesManager().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.

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

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

						IFile wkspFile = null;
						if (sourceFile.exists())
							wkspFile = sourceFile;
						else {
							IFile[] filesInWP = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocation(path);

							for (int j = 0; j < filesInWP.length; j++) {
								if (filesInWP[j].isAccessible()) {
									wkspFile = filesInWP[j];
									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
								tu = new ExternalTranslationUnit(cproject, URIUtil.toURI(path), id);

							sourceFiles.add(tu);

							if (!orgPath.equals(filename)) {
								remappedPaths.put(tu, orgPath);
							}
						}
					}
				}
			}

		}

		refreshSourceFiles = false;
		return sourceFiles.toArray(new TranslationUnit[sourceFiles.size()]) ;
	}

	public void setRefreshSourceFiles(boolean refreshSourceFiles) {
		this.refreshSourceFiles = refreshSourceFiles;
	}

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

}

Back to the top