~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to gprof/org.eclipse.linuxtools.gprof.launch/src/org/eclipse/linuxtools/internal/gprof/launch/GprofLaunchConfigurationDelegate.java

  • Committer: Package Import Robot
  • Author(s): tony mancill
  • Date: 2013-05-13 21:43:22 UTC
  • mfrom: (1.2.1) (2.1.2 experimental)
  • Revision ID: package-import@ubuntu.com-20130513214322-6frgd9du1n0w2uo7
Tags: 1.2.1-1
* Team upload.
* New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 * Copyright (c) 2004, 2008, 2009, 2012 Red Hat, Inc. and others
 
3
 * All rights reserved. This program and the accompanying materials
 
4
 * are made available under the terms of the Eclipse Public License v1.0
 
5
 * which accompanies this distribution, and is available at
 
6
 * http://www.eclipse.org/legal/epl-v10.html
 
7
 *
 
8
 * Contributors:
 
9
 *    Kent Sebastian <ksebasti@redhat.com> - initial API and implementation
 
10
 *    Keith Seitz <keiths@redhat.com> - setup code in launch the method, initially 
 
11
 *        written in the now-defunct OprofileSession class
 
12
 *    QNX Software Systems and others - the section of code marked in the launch 
 
13
 *        method, and the exec method
 
14
 *    Red Hat Inc. - modification of OProfileLaunchConfigurationDelegate to here
 
15
 *******************************************************************************/ 
 
16
package org.eclipse.linuxtools.internal.gprof.launch;
 
17
 
 
18
import java.net.URI;
 
19
 
 
20
import org.eclipse.cdt.debug.core.CDebugUtils;
 
21
import org.eclipse.core.filesystem.IFileStore;
 
22
import org.eclipse.core.resources.IProject;
 
23
import org.eclipse.core.runtime.CoreException;
 
24
import org.eclipse.core.runtime.IPath;
 
25
import org.eclipse.core.runtime.IProgressMonitor;
 
26
import org.eclipse.core.runtime.Path;
 
27
import org.eclipse.debug.core.DebugPlugin;
 
28
import org.eclipse.debug.core.ILaunch;
 
29
import org.eclipse.debug.core.ILaunchConfiguration;
 
30
import org.eclipse.debug.core.ILaunchManager;
 
31
import org.eclipse.debug.core.ILaunchesListener2;
 
32
import org.eclipse.jface.dialogs.MessageDialog;
 
33
import org.eclipse.linuxtools.gprof.launch.GprofLaunchPlugin;
 
34
import org.eclipse.linuxtools.internal.gprof.view.GmonView;
 
35
import org.eclipse.linuxtools.profiling.launch.IRemoteCommandLauncher;
 
36
import org.eclipse.linuxtools.profiling.launch.IRemoteFileProxy;
 
37
import org.eclipse.linuxtools.profiling.launch.ProfileLaunchConfigurationDelegate;
 
38
import org.eclipse.linuxtools.profiling.launch.RemoteProxyManager;
 
39
import org.eclipse.swt.widgets.Display;
 
40
import org.eclipse.swt.widgets.Shell;
 
41
import org.eclipse.ui.PlatformUI;
 
42
 
 
43
public class GprofLaunchConfigurationDelegate extends ProfileLaunchConfigurationDelegate {
 
44
        protected ILaunchConfiguration config;
 
45
        
 
46
        @Override
 
47
        public void launch(ILaunchConfiguration config, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
 
48
                this.config = config;
 
49
                IPath exePath = getExePath(config);
 
50
 
 
51
                /* 
 
52
                 * this code written by QNX Software Systems and others and was 
 
53
                 * originally in the CDT under LocalCDILaunchDelegate::RunLocalApplication
 
54
                 */
 
55
                //set up and launch the local c/c++ program
 
56
                IRemoteCommandLauncher launcher = RemoteProxyManager.getInstance().getLauncher(getProject());
 
57
 
 
58
                URI workingDirURI = getProject().getLocationURI();
 
59
                IPath workingDirPath = new Path(workingDirURI.getPath());
 
60
                String arguments[] = getProgramArgumentsArray( config );
 
61
                
 
62
                //add a listener for termination of the launch
 
63
                ILaunchManager lmgr = DebugPlugin.getDefault().getLaunchManager();
 
64
                lmgr.addLaunchListener(new LaunchTerminationWatcher(launch, exePath));
 
65
                
 
66
                Process process = launcher.execute(exePath, arguments, getEnvironment(config), workingDirPath, monitor);
 
67
 
 
68
                DebugPlugin.newProcess( launch, process, renderProcessLabel( exePath.toOSString() ) );
 
69
 
 
70
        }
 
71
        
 
72
        //A class used to listen for the termination of the current launch, and 
 
73
        // run some functions when it is finished. 
 
74
        class LaunchTerminationWatcher implements ILaunchesListener2 {
 
75
                private ILaunch launch;
 
76
                private IPath exePath;
 
77
                
 
78
                class LaunchTerminationWatcherRunnable implements Runnable {
 
79
 
 
80
                        private String exePath;
 
81
                        private String gmonPath;
 
82
                        
 
83
                        public LaunchTerminationWatcherRunnable(String exePath, String gmonPath) {
 
84
                                this.exePath = exePath;
 
85
                                this.gmonPath = gmonPath;
 
86
                        }
 
87
                        
 
88
                        @Override
 
89
                        public void run() {
 
90
                                GmonView.displayGprofView(exePath, gmonPath, getProject());
 
91
                        }
 
92
                }
 
93
                
 
94
                public LaunchTerminationWatcher(ILaunch il, IPath exePath) {
 
95
                        launch = il;
 
96
                        this.exePath = exePath;
 
97
                }
 
98
                public void launchesTerminated(ILaunch[] launches) {
 
99
 
 
100
                        for (ILaunch l : launches) {
 
101
                                /**
 
102
                                 * Dump samples from the daemon,
 
103
                                 * shut down the daemon,
 
104
                                 * activate the OProfile view (open it if it isn't already),
 
105
                                 * refresh the view (which parses the data/ui model and displays it).
 
106
                                 */
 
107
                                if (l.equals(launch)) {
 
108
                                        //need to run this in the ui thread otherwise get SWT Exceptions
 
109
                                        // based on concurrency issues
 
110
                                        Display.getDefault().syncExec(new Runnable() {
 
111
                                                public void run() {
 
112
                                                        try {
 
113
                                                                String s = exePath.toOSString();                        
 
114
                                                                URI workingDirURI = getProject().getLocationURI();
 
115
                                                                RemoteProxyManager rpmgr = RemoteProxyManager.getInstance();
 
116
                                                                IRemoteFileProxy proxy = rpmgr.getFileProxy(getProject());
 
117
                                                                String workingDirPath = proxy.toPath(workingDirURI);
 
118
                                                                // Because we set the working directory on execution to the top-level
 
119
                                                                // project directory, the gmon.out file should be found there
 
120
                                                                String gmonExpected = workingDirPath + "/gmon.out"; //$NON-NLS-1$
 
121
                                                                IFileStore f = proxy.getResource(gmonExpected);
 
122
                                                                if (!f.fetchInfo().exists()) {
 
123
                                                                        String title = GprofLaunchMessages.GprofCompilerOptions_msg;
 
124
                                                                        String message = GprofLaunchMessages.GprofCompileAgain_msg;
 
125
                                                                        Shell parent = PlatformUI.getWorkbench().getDisplay().getActiveShell();
 
126
                                                                        MessageDialog.openWarning(parent, title, message);
 
127
                                                                } else {
 
128
                                                                        // We found gmon.out.  Make sure it was generated after the executable was formed.
 
129
                                                                        IFileStore exe = proxy.getResource(exePath.toString());
 
130
                                                                        if (exe.fetchInfo().getLastModified() > f.fetchInfo().getLastModified()) {
 
131
                                                                                String title = GprofLaunchMessages.GprofGmonStale_msg;
 
132
                                                                                String message = GprofLaunchMessages.GprofGmonStaleExplanation_msg;
 
133
                                                                                Shell parent = PlatformUI.getWorkbench().getDisplay().getActiveShell();
 
134
                                                                                MessageDialog.openWarning(parent, title, message);
 
135
                                                                        }
 
136
                                                                }
 
137
                                                                Display.getDefault().asyncExec(new LaunchTerminationWatcherRunnable(s, gmonExpected));
 
138
 
 
139
                                                        } catch (NullPointerException e) {
 
140
                                                                // Do nothing
 
141
                                                        } catch (CoreException e) {
 
142
                                                                // Do nothing
 
143
                                                        }
 
144
                                                }
 
145
                                        });
 
146
                                }
 
147
                        }
 
148
                }
 
149
 
 
150
                public void launchesAdded(ILaunch[] launches) { /* dont care */}
 
151
                public void launchesChanged(ILaunch[] launches) { /* dont care */ }
 
152
                public void launchesRemoved(ILaunch[] launches) { /* dont care */ }
 
153
 
 
154
        }
 
155
        
 
156
        @Override
 
157
        protected String getPluginID() {
 
158
                return GprofLaunchPlugin.getUniqueIdentifier();
 
159
        }
 
160
        
 
161
        /* all these functions exist to be overridden by the test class in order to allow launch testing */
 
162
        
 
163
        protected IProject getProject(){
 
164
                try{
 
165
                        IProject project = CDebugUtils.verifyCProject(config).getProject();
 
166
                        return project;
 
167
                } catch (CoreException e) {
 
168
                        e.printStackTrace();
 
169
                }
 
170
                return null;
 
171
        }
 
172
         /**
 
173
          *
 
174
          * Return the exe path of the binary to be profiled.
 
175
          * @param config
 
176
          * @return the exe path of the binary stored in the configuration
 
177
          * @throws CoreException
 
178
          * @since 1.1
 
179
          */
 
180
        protected IPath getExePath(ILaunchConfiguration config) throws CoreException{
 
181
                IPath exePath = CDebugUtils.verifyProgramPath( config );
 
182
 
 
183
                return exePath;
 
184
        }
 
185
 
 
186
        @Override
 
187
        public String generateCommand(ILaunchConfiguration config) {
 
188
                return "";
 
189
        }
 
190
}