Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 010236b5fcf25b72546d5692ea22d08302871318 (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
/*******************************************************************************
 * Copyright (c) 2004, 2008, 2009, 2012 Red Hat, Inc. 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:
 *    Kent Sebastian <ksebasti@redhat.com> - initial API and implementation
 *    Keith Seitz <keiths@redhat.com> - setup code in launch the method, initially
 *        written in the now-defunct OprofileSession class
 *    QNX Software Systems and others - the section of code marked in the launch
 *        method, and the exec method
 *    Red Hat Inc. - modification of OProfileLaunchConfigurationDelegate to here
 *******************************************************************************/
package org.eclipse.linuxtools.internal.gcov.launch;

import java.io.File;
import java.util.List;

import org.eclipse.cdt.debug.core.CDebugUtils;
import org.eclipse.core.resources.IProject;
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.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.ILaunchesListener2;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.linuxtools.internal.gcov.parser.CovManager;
import org.eclipse.linuxtools.internal.gcov.view.CovView;
import org.eclipse.linuxtools.internal.gcov.view.annotatedsource.GcovAnnotationModelTracker;
import org.eclipse.linuxtools.profiling.launch.IRemoteCommandLauncher;
import org.eclipse.linuxtools.profiling.launch.ProfileLaunchConfigurationDelegate;
import org.eclipse.linuxtools.profiling.launch.RemoteProxyManager;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.PlatformUI;

public class GcovLaunchConfigurationDelegate extends ProfileLaunchConfigurationDelegate {
	protected ILaunchConfiguration config;

	@Override
	public void launch(ILaunchConfiguration config, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
		this.config = config;
		IPath exePath = getExePath(config);

		/*
		 * this code written by QNX Software Systems and others and was
		 * originally in the CDT under LocalCDILaunchDelegate::RunLocalApplication
		 */
		//set up and launch the local c/c++ program
		IRemoteCommandLauncher launcher = RemoteProxyManager.getInstance().getLauncher(getProject());

		File workDir = getWorkingDirectory(config);
		if (workDir == null) {
			workDir = new File(System.getProperty("user.home", ".")); //$NON-NLS-1$ //$NON-NLS-2$
		}
		String arguments[] = getProgramArgumentsArray( config );

		//add a listener for termination of the launch
		ILaunchManager lmgr = DebugPlugin.getDefault().getLaunchManager();
		lmgr.addLaunchListener(new LaunchTerminationWatcher(launch, exePath));

		Process process = launcher.execute(exePath, arguments, getEnvironment(config), new Path(workDir.getAbsolutePath()), monitor);

		DebugPlugin.newProcess( launch, process, renderProcessLabel( exePath.toOSString() ) );

	}

	//A class used to listen for the termination of the current launch, and
	// run some functions when it is finished.
	class LaunchTerminationWatcher implements ILaunchesListener2 {
		private ILaunch launch;
		private IPath exePath;
		public LaunchTerminationWatcher(ILaunch il, IPath exePath) {
			launch = il;
			this.exePath = exePath;
		}
		@Override
		public void launchesTerminated(ILaunch[] launches) {

			for (ILaunch l : launches) {
				/**
				 * Dump samples from the daemon,
				 * shut down the daemon,
				 * activate the OProfile view (open it if it isn't already),
				 * refresh the view (which parses the data/ui model and displays it).
				 */
				if (l.equals(launch)) {
					//need to run this in the ui thread otherwise get SWT Exceptions
					// based on concurrency issues
					Display.getDefault().syncExec(new Runnable() {
						@Override
						public void run() {
							String s = exePath.toOSString();
							CovManager cvrgeMnger = new CovManager(s, getProject());

							try {
								List<String> gcdaPaths = cvrgeMnger.getGCDALocations();
								if (gcdaPaths.size() == 0) {
									String title = GcovLaunchMessages.GcovCompilerOptions_msg;
									String message = GcovLaunchMessages.GcovCompileAgain_msg;
									Shell parent = PlatformUI.getWorkbench().getDisplay().getActiveShell();
									MessageDialog.openWarning(parent, title, message);
								}
								CovView.displayCovResults(s, null);
								GcovAnnotationModelTracker.getInstance().addProject(getProject(), exePath);
								GcovAnnotationModelTracker.getInstance().annotateAllCEditors();
							} catch (InterruptedException e) {
								// Do nothing
							}
						}
					});
				}
			}

		}
		@Override
		public void launchesAdded(ILaunch[] launches) { /* dont care */}
		@Override
		public void launchesChanged(ILaunch[] launches) { /* dont care */ }
		@Override
		public void launchesRemoved(ILaunch[] launches) { /* dont care */ }

	}

	@Override
	protected String getPluginID() {
		return GcovLaunchPlugin.PLUGIN_ID;
	}

	/* all these functions exist to be overridden by the test class in order to allow launch testing */

	protected IProject getProject(){
		try{
			return CDebugUtils.verifyCProject(config).getProject();
		} catch (CoreException e) {
			e.printStackTrace();
		}
		return null;
	}
	 /**
	  *
	  * Return the exe path of the binary to be profiled.
	  * @param config
	  * @return the exe path of the binary stored in the configuration
	  * @throws CoreException
	  * @since 1.1
	  */
	protected IPath getExePath(ILaunchConfiguration config) throws CoreException{
		return CDebugUtils.verifyProgramPath( config );
	}

}

Back to the top