Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a60da90fa440ff05ef367d6f612ee9f1f4722280 (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
/*******************************************************************************
 * Copyright (c) 2008, 2009 Red Hat, Inc.
 * 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:
 *    Elliott Baron <ebaron@redhat.com> - initial API and implementation
 *    Patrick Hofer (Noser Engineering AG) - fix for Bug 275685
 *******************************************************************************/ 
package org.eclipse.linuxtools.internal.valgrind.launch;

import java.io.IOException;
import java.util.HashMap;
import java.util.Set;

import org.eclipse.cdt.launch.LaunchUtils;
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.IConfigurationElement;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.linuxtools.internal.valgrind.core.PluginConstants;
import org.eclipse.linuxtools.internal.valgrind.core.ValgrindCommand;
import org.eclipse.linuxtools.valgrind.launch.IValgrindLaunchDelegate;
import org.eclipse.linuxtools.valgrind.launch.IValgrindOutputDirectoryProvider;
import org.eclipse.linuxtools.valgrind.launch.IValgrindToolPage;
import org.eclipse.osgi.util.NLS;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Version;

public class ValgrindLaunchPlugin extends AbstractUIPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = PluginConstants.LAUNCH_PLUGIN_ID;
	public static final String LAUNCH_ID = PLUGIN_ID + ".valgrindLaunch"; //$NON-NLS-1$
	public static final String MARKER_TYPE = PLUGIN_ID + ".marker"; //$NON-NLS-1$

	// Extension point constants
	protected static final String EXT_ELEMENT_TOOL = "tool"; //$NON-NLS-1$
	protected static final String EXT_ATTR_NAME = "name"; //$NON-NLS-1$
	protected static final String EXT_ATTR_ID = "id"; //$NON-NLS-1$
	protected static final String EXT_ATTR_PAGE = "page"; //$NON-NLS-1$
	protected static final String EXT_ATTR_DELEGATE = "delegate"; //$NON-NLS-1$

	protected static final String EXT_ELEMENT_PROVIDER = "provider"; //$NON-NLS-1$
	protected static final String EXT_ATTR_CLASS = "class"; //$NON-NLS-1$
	
	public static final Version VER_3_3_0 = new Version(3, 3, 0);
	public static final Version VER_3_3_1 = new Version(3, 3, 1);
	public static final Version VER_3_4_0 = new Version(3, 4, 0);
	public static final Version VER_3_4_1 = new Version(3, 4, 1);
	public static final Version VER_3_5_0 = new Version(3, 5, 0);
	public static final Version VER_3_6_0 = new Version(3, 6, 0);
	
	private static final Version MIN_VER = VER_3_3_0;
	private static final String VERSION_PREFIX = "valgrind-"; //$NON-NLS-1$
	private static final char VERSION_DELIMITER = '-';
	
	protected HashMap<String, IConfigurationElement> toolMap;
	
	private ValgrindCommand valgrindCommand;
	private ILaunchConfiguration config;
	private ILaunch launch;

	// The shared instance
	private static ValgrindLaunchPlugin plugin;

	/**
	 * The constructor
	 */
	public ValgrindLaunchPlugin() {
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	@Override
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	@Override
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static ValgrindLaunchPlugin getDefault() {
		return plugin;
	}

	/**
	 * @since 0.10
	 */
	public Version getValgrindVersion(IProject project) throws CoreException {
		Version valgrindVersion;
		try {
			String verString = getValgrindCommand().whichVersion(project);
			verString = verString.replace(VERSION_PREFIX, ""); //$NON-NLS-1$
			if (verString.indexOf(VERSION_DELIMITER) > 0) {
				verString = verString.substring(0, verString.indexOf(VERSION_DELIMITER));
			} 
			if (verString.length() > 0) {
				valgrindVersion = Version.parseVersion(verString);
			}
			else {
				throw new CoreException(new Status(IStatus.ERROR, PLUGIN_ID, Messages.getString("ValgrindLaunchPlugin.Couldn't_determine_version"))); //$NON-NLS-1$
			}
		} catch (IOException e) {
			IStatus status = new Status(IStatus.ERROR, PLUGIN_ID, Messages.getString("ValgrindLaunchPlugin.Couldn't_determine_version"), e); //$NON-NLS-1$
			throw new CoreException(status);
		}

		// check for minimum supported version
		if (valgrindVersion.compareTo(MIN_VER) < 0) {
			throw new CoreException(new Status(IStatus.ERROR, PLUGIN_ID, NLS.bind(Messages.getString("ValgrindLaunchPlugin.Error_min_version"), valgrindVersion.toString(), MIN_VER.toString()))); //$NON-NLS-1$
		}
		return valgrindVersion;
	}
	
	public void setValgrindCommand(ValgrindCommand command) {
		valgrindCommand = command;
	}
	
	/**
	 * @since 0.10
	 */
	protected ValgrindCommand getValgrindCommand() {
		if (valgrindCommand == null) {
			valgrindCommand = new ValgrindCommand();
		}
		return valgrindCommand;
	}
	
	public String[] getRegisteredToolIDs() {
		Set<String> ids = getToolMap().keySet();
		return ids.toArray(new String[ids.size()]);
	}

	public String getToolName(String id) {
		String name = null;
		IConfigurationElement config = getToolMap().get(id);
		if (config != null) {
			name = config.getAttribute(EXT_ATTR_NAME);
		}
		return name;
	}

	public IValgrindToolPage getToolPage(String id) throws CoreException {
		IValgrindToolPage tab = null;
		IConfigurationElement config = getToolMap().get(id);
		if (config != null) {
			Object obj = config.createExecutableExtension(EXT_ATTR_PAGE);
			if (obj instanceof IValgrindToolPage) {
				tab = (IValgrindToolPage) obj;
			}
		}
		if (tab == null) {
			throw new CoreException(new Status(IStatus.ERROR, PLUGIN_ID, Messages.getString("ValgrindLaunchPlugin.Cannot_retrieve_page"))); //$NON-NLS-1$
		}
		return tab;
	}

	public IValgrindLaunchDelegate getToolDelegate(String id) throws CoreException {
		IValgrindLaunchDelegate delegate = null;
		IConfigurationElement config = getToolMap().get(id);
		if (config != null) {
			Object obj = config.createExecutableExtension(EXT_ATTR_DELEGATE);
			if (obj instanceof IValgrindLaunchDelegate) {
				delegate = (IValgrindLaunchDelegate) obj;
			}
		}
		if (delegate == null) {
			throw new CoreException(new Status(IStatus.ERROR, PLUGIN_ID, Messages.getString("ValgrindLaunchPlugin.Cannot_retrieve_delegate"))); //$NON-NLS-1$
		}
		return delegate;
	}

	public IValgrindOutputDirectoryProvider getOutputDirectoryProvider() throws CoreException {
		IValgrindOutputDirectoryProvider provider = null;
		IExtensionPoint extPoint = Platform.getExtensionRegistry().getExtensionPoint(PLUGIN_ID, PluginConstants.OUTPUT_DIR_EXT_ID);

		// if we find more than one provider just take the first one
		IConfigurationElement[] configs = extPoint.getConfigurationElements();
		for (int i = 0; i < configs.length && provider == null; i++) {
			IConfigurationElement config = configs[i];
			if (config.getName().equals(EXT_ELEMENT_PROVIDER)) {
				Object obj = config.createExecutableExtension(EXT_ATTR_CLASS);
				if (obj instanceof IValgrindOutputDirectoryProvider) {
					provider = (IValgrindOutputDirectoryProvider) obj;
				}
			}
		}
		
		// if no extender, use default
		if (provider == null) {
			provider = new ValgrindOutputDirectoryProvider();
		}

		return provider;
	}
	
	public void setCurrentLaunchConfiguration(ILaunchConfiguration config) {
		this.config = config;		
	}

	/**
	 * @return ILaunchConfiguration associated with Valgrind execution
	 * currently displayed in the Valgrind view.
	 */
	public ILaunchConfiguration getCurrentLaunchConfiguration() {
		return config;
	}

	public void setCurrentLaunch(ILaunch launch) {
		this.launch = launch;
	}
	
	/**
	 * @return ILaunch associated with Valgrind execution currently displayed
	 * in the Valgrind view.
	 */
	public ILaunch getCurrentLaunch() {
		return launch;
	}
	
	IPath parseWSPath(String strpath) throws CoreException {
		strpath = LaunchUtils.getStringVariableManager().performStringSubstitution(strpath, false);
		IPath path = new Path(strpath);
		if (!path.isAbsolute()) {
			IResource res = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
			if (res != null) {
				path = res.getLocation();
			}
		}		
		return path;
	}

	protected void initializeToolMap() {
		toolMap = new HashMap<String, IConfigurationElement>();
		IExtensionPoint extPoint = Platform.getExtensionRegistry().getExtensionPoint(PLUGIN_ID, PluginConstants.TOOL_EXT_ID);
		IConfigurationElement[] configs = extPoint.getConfigurationElements();
		for (IConfigurationElement config : configs) {
			if (config.getName().equals(EXT_ELEMENT_TOOL)) {
				String id = config.getAttribute(EXT_ATTR_ID);
				if (id != null && config.getAttribute(EXT_ATTR_NAME) != null
						&& config.getAttribute(EXT_ATTR_PAGE) != null
						&& config.getAttribute(EXT_ATTR_DELEGATE) != null) {
					toolMap.put(id, config);
				}
			}
		}
	}

	protected HashMap<String, IConfigurationElement> getToolMap() {
		if (toolMap == null) {
			initializeToolMap();
		}
		return toolMap;
	}
}

Back to the top