| author | Rafael Medeiros Teixeira | 2012-05-10 12:26:43 (EDT) |
|---|---|---|
| committer | Otavio Pontes | 2012-06-19 07:58:12 (EDT) |
| commit | 23a84d389e2475fd33c57784d4ec3b0f707aa952 (patch) (side-by-side diff) | |
| tree | e5d40e032086c1a8782de8b955e13661b4df81be | |
| parent | af97000f8b630e4225ee69d0da7a54395bfeafcd (diff) | |
| download | org.eclipse.linuxtools-23a84d389e2475fd33c57784d4ec3b0f707aa952.zip org.eclipse.linuxtools-23a84d389e2475fd33c57784d4ec3b0f707aa952.tar.gz org.eclipse.linuxtools-23a84d389e2475fd33c57784d4ec3b0f707aa952.tar.bz2 | |
Oprofile: Fix NPE after failed profile
8 files changed, 129 insertions, 36 deletions
diff --git a/oprofile/org.eclipse.linuxtools.oprofile.core/src/org/eclipse/linuxtools/internal/oprofile/core/Oprofile.java b/oprofile/org.eclipse.linuxtools.oprofile.core/src/org/eclipse/linuxtools/internal/oprofile/core/Oprofile.java index cc0182a..8bdae00 100644 --- a/oprofile/org.eclipse.linuxtools.oprofile.core/src/org/eclipse/linuxtools/internal/oprofile/core/Oprofile.java +++ b/oprofile/org.eclipse.linuxtools.oprofile.core/src/org/eclipse/linuxtools/internal/oprofile/core/Oprofile.java @@ -23,7 +23,6 @@ import org.eclipse.linuxtools.internal.oprofile.core.daemon.OpInfo; import org.eclipse.linuxtools.internal.oprofile.core.model.OpModelEvent; import org.eclipse.linuxtools.internal.oprofile.core.model.OpModelImage; import org.eclipse.linuxtools.internal.oprofile.core.opxml.checkevent.CheckEventsProcessor; -import org.eclipse.linuxtools.tools.launch.core.properties.LinuxtoolsPathProperty; /** @@ -40,9 +39,6 @@ public class Oprofile // Oprofile information private static OpInfo info; - // Project that will be profiled. - public static IProject currentProject; - // Make sure that oprofile is ready to go static { initializeOprofileModule(); @@ -228,31 +224,32 @@ public class Oprofile return image; } - // Reloads oprofile modules by calling 'opcontrol --deinit' and 'opcontrol --init' - public static void setCurrentProject(IProject project){ - - if(currentProject == null){ - currentProject = project; - initializeOprofileModule(); - } else { - - String currentPath = LinuxtoolsPathProperty.getInstance().getLinuxtoolsPath(currentProject); - String newPath = LinuxtoolsPathProperty.getInstance().getLinuxtoolsPath(project); - - if(!currentPath.equals(newPath)){ - try { - OprofileCorePlugin.getDefault().getOpcontrolProvider().deinitModule(); - currentProject = project; - OprofileCorePlugin.getDefault().getOpcontrolProvider().initModule(); - } catch (OpcontrolException e) { - OprofileCorePlugin.showErrorDialog("opcontrolProvider", e); //$NON-NLS-1$ - } - currentProject = project; + public static void updateInfo(){ + try { + if (!OprofileCorePlugin.getDefault().getOpcontrolProvider().status()){ + OprofileCorePlugin.getDefault().getOpcontrolProvider().initModule(); } - } + info = OpInfo.getInfo(); + } catch (OpcontrolException e) { + e.printStackTrace(); + } + } - public static IProject getCurrentProject(){ - return currentProject; + // Oprofile class has a static initializer and the code inside it needs to know which project + // is being profiled in order to get the path for the Linux Tools' binaries set for that project. + // For this reason the project property has to be set outside the Oprofile class + public static class OprofileProject { + private static IProject project; + + // Reloads oprofile modules by calling 'opcontrol --deinit' and + // 'opcontrol --init' + public static void setProject(IProject project) { + OprofileProject.project = project; + + } + public static IProject getProject() { + return project; + } } } diff --git a/oprofile/org.eclipse.linuxtools.oprofile.core/src/org/eclipse/linuxtools/internal/oprofile/core/linux/LinuxOpcontrolProvider.java b/oprofile/org.eclipse.linuxtools.oprofile.core/src/org/eclipse/linuxtools/internal/oprofile/core/linux/LinuxOpcontrolProvider.java index 8556d7b..7a6d9c5 100644 --- a/oprofile/org.eclipse.linuxtools.oprofile.core/src/org/eclipse/linuxtools/internal/oprofile/core/linux/LinuxOpcontrolProvider.java +++ b/oprofile/org.eclipse.linuxtools.oprofile.core/src/org/eclipse/linuxtools/internal/oprofile/core/linux/LinuxOpcontrolProvider.java @@ -252,7 +252,7 @@ public class LinuxOpcontrolProvider implements IOpcontrolProvider { * entered the password */ private boolean runOpcontrol(ArrayList<String> args) throws OpcontrolException { - IProject project = Oprofile.getCurrentProject(); + IProject project = Oprofile.OprofileProject.getProject(); // If no linuxtools' toolchain is defined for this project, use the path for the @@ -342,7 +342,7 @@ public class LinuxOpcontrolProvider implements IOpcontrolProvider { } private static String findOpcontrol() throws OpcontrolException { - IProject project = Oprofile.getCurrentProject(); + IProject project = Oprofile.OprofileProject.getProject(); URL url = FileLocator.find(Platform.getBundle(OprofileCorePlugin .getId()), new Path(OPCONTROL_REL_PATH), null); diff --git a/oprofile/org.eclipse.linuxtools.oprofile.core/src/org/eclipse/linuxtools/internal/oprofile/core/linux/OpxmlRunner.java b/oprofile/org.eclipse.linuxtools.oprofile.core/src/org/eclipse/linuxtools/internal/oprofile/core/linux/OpxmlRunner.java index 5ceb412..680bb9a 100644 --- a/oprofile/org.eclipse.linuxtools.oprofile.core/src/org/eclipse/linuxtools/internal/oprofile/core/linux/OpxmlRunner.java +++ b/oprofile/org.eclipse.linuxtools.oprofile.core/src/org/eclipse/linuxtools/internal/oprofile/core/linux/OpxmlRunner.java @@ -192,7 +192,7 @@ public class OpxmlRunner { cmd.add("event:" + args[1]); //$NON-NLS-1$ } String [] a = {}; - p = RuntimeProcessFactory.getFactory().exec(cmd.toArray(a), Oprofile.getCurrentProject()); + p = RuntimeProcessFactory.getFactory().exec(cmd.toArray(a), Oprofile.OprofileProject.getProject()); StringBuilder output = new StringBuilder(); StringBuilder errorOutput = new StringBuilder(); @@ -277,7 +277,7 @@ public class OpxmlRunner { try { String cmd[] = {"opreport", "-X", "-d"}; - Process p = RuntimeProcessFactory.getFactory().exec(cmd, Oprofile.getCurrentProject()); + Process p = RuntimeProcessFactory.getFactory().exec(cmd, Oprofile.OprofileProject.getProject()); StringBuilder output = new StringBuilder(); StringBuilder errorOutput = new StringBuilder(); String s = null; diff --git a/oprofile/org.eclipse.linuxtools.oprofile.core/src/org/eclipse/linuxtools/internal/oprofile/core/opxml/EventIdCache.java b/oprofile/org.eclipse.linuxtools.oprofile.core/src/org/eclipse/linuxtools/internal/oprofile/core/opxml/EventIdCache.java index 91b0248..768340a 100644 --- a/oprofile/org.eclipse.linuxtools.oprofile.core/src/org/eclipse/linuxtools/internal/oprofile/core/opxml/EventIdCache.java +++ b/oprofile/org.eclipse.linuxtools.oprofile.core/src/org/eclipse/linuxtools/internal/oprofile/core/opxml/EventIdCache.java @@ -91,7 +91,8 @@ public class EventIdCache { */ private void readXML() { try { - Process p = RuntimeProcessFactory.getFactory().exec(OPHELP + " " + "-X", Oprofile.getCurrentProject()); + Process p = RuntimeProcessFactory.getFactory().exec(OPHELP + " " + "-X", Oprofile.OprofileProject.getProject()); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder; try { diff --git a/oprofile/org.eclipse.linuxtools.oprofile.core/src/org/eclipse/linuxtools/internal/oprofile/core/opxml/info/InfoAdapter.java b/oprofile/org.eclipse.linuxtools.oprofile.core/src/org/eclipse/linuxtools/internal/oprofile/core/opxml/info/InfoAdapter.java index 5dc37b7..65425b8 100644 --- a/oprofile/org.eclipse.linuxtools.oprofile.core/src/org/eclipse/linuxtools/internal/oprofile/core/opxml/info/InfoAdapter.java +++ b/oprofile/org.eclipse.linuxtools.oprofile.core/src/org/eclipse/linuxtools/internal/oprofile/core/opxml/info/InfoAdapter.java @@ -94,7 +94,7 @@ public class InfoAdapter extends AbstractDataAdapter{ // In timer mode, we have no relevant XML generated by ophelp createDOM(null); }else{ - Process p = RuntimeProcessFactory.getFactory().exec("ophelp -X", Oprofile.getCurrentProject()); + Process p = RuntimeProcessFactory.getFactory().exec("ophelp -X", Oprofile.OprofileProject.getProject()); InputStream is = p.getInputStream(); createDOM(is); } diff --git a/oprofile/org.eclipse.linuxtools.oprofile.launch/META-INF/MANIFEST.MF b/oprofile/org.eclipse.linuxtools.oprofile.launch/META-INF/MANIFEST.MF index 5f858bf..20d4ced 100644 --- a/oprofile/org.eclipse.linuxtools.oprofile.launch/META-INF/MANIFEST.MF +++ b/oprofile/org.eclipse.linuxtools.oprofile.launch/META-INF/MANIFEST.MF @@ -6,7 +6,13 @@ Bundle-Version: 1.0.0.qualifier Bundle-Vendor: %bundleProvider Bundle-RequiredExecutionEnvironment: J2SE-1.5 Require-Bundle: org.eclipse.core.runtime;bundle-version="3.4.0", - org.eclipse.ui;bundle-version="3.4.0" + org.eclipse.linuxtools.tools.launch.core, + org.eclipse.linuxtools.profiling.launch;bundle-version="0.9.0", + org.eclipse.cdt.core;bundle-version="5.3.0", + org.eclipse.core.filesystem;bundle-version="1.3.100", + org.eclipse.jface.text;bundle-version="3.7.0", + org.eclipse.ui;bundle-version="3.4.0", + org.eclipse.debug.ui;bundle-version="3.7.0" Bundle-Activator: org.eclipse.linuxtools.internal.oprofile.launch.OprofileLaunchPlugin Bundle-ActivationPolicy: lazy Bundle-Localization: plugin diff --git a/oprofile/org.eclipse.linuxtools.oprofile.launch/src/org/eclipse/linuxtools/internal/oprofile/launch/launching/OprofileLaunchConfigurationDelegate.java b/oprofile/org.eclipse.linuxtools.oprofile.launch/src/org/eclipse/linuxtools/internal/oprofile/launch/launching/OprofileLaunchConfigurationDelegate.java index d845be7..65831e8 100644 --- a/oprofile/org.eclipse.linuxtools.oprofile.launch/src/org/eclipse/linuxtools/internal/oprofile/launch/launching/OprofileLaunchConfigurationDelegate.java +++ b/oprofile/org.eclipse.linuxtools.oprofile.launch/src/org/eclipse/linuxtools/internal/oprofile/launch/launching/OprofileLaunchConfigurationDelegate.java @@ -10,6 +10,11 @@ *******************************************************************************/ package org.eclipse.linuxtools.internal.oprofile.launch.launching; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +import org.eclipse.core.resources.IProject; import org.eclipse.debug.core.DebugPlugin; import org.eclipse.debug.core.ILaunch; import org.eclipse.debug.core.ILaunchConfiguration; @@ -20,17 +25,31 @@ import org.eclipse.linuxtools.internal.oprofile.core.Oprofile; import org.eclipse.linuxtools.internal.oprofile.core.OprofileCorePlugin; import org.eclipse.linuxtools.internal.oprofile.core.daemon.OprofileDaemonEvent; import org.eclipse.linuxtools.internal.oprofile.launch.configuration.LaunchOptions; +import org.eclipse.linuxtools.tools.launch.core.factory.RuntimeProcessFactory; +import org.eclipse.linuxtools.tools.launch.core.properties.LinuxtoolsPathProperty; import org.eclipse.swt.widgets.Display; public class OprofileLaunchConfigurationDelegate extends AbstractOprofileLaunchConfigurationDelegate { + private static final int SUDO_TIMEOUT = 5000; + private static final String OPCONTROL_EXECUTABLE = "opcontrol"; + @Override protected boolean preExec(LaunchOptions options, OprofileDaemonEvent[] daemonEvents) { //set up and launch the oprofile daemon try { + IProject project = getProject(); + + //check if user has NOPASSWD sudo permission for opcontrol + //if the Linux Tools Path property was changed + if(project != null && !LinuxtoolsPathProperty.getInstance().getLinuxtoolsPath(project).equals("")){ + if(!hasPermissions(project)){ + throw new OpcontrolException(OprofileCorePlugin.createErrorStatus("opcontrolSudo", null)); + } + } // Set current project to allow using the oprofile path that // was chosen for the project - Oprofile.setCurrentProject(getProject()); + Oprofile.OprofileProject.setProject(project); if (!oprofileStatus()) return false; @@ -57,6 +76,76 @@ public class OprofileLaunchConfigurationDelegate extends AbstractOprofileLaunchC return true; } + + /** + * Checks if the user has permissions to execute opcontrol as root without providing password + * and if opcontrol exists in the indicated path + * @param project + * @return + */ + public static boolean hasPermissions(IProject project) { + String linuxtoolsPath = LinuxtoolsPathProperty.getInstance().getLinuxtoolsPath(project); + + try { + String opcontrolPath = null; + if(linuxtoolsPath.equals("")){ + opcontrolPath = RuntimeProcessFactory.getFactory().whichCommand(OPCONTROL_EXECUTABLE, project); + } else if(linuxtoolsPath.endsWith("/")){ + opcontrolPath = linuxtoolsPath + "opcontrol"; + } else { + opcontrolPath = linuxtoolsPath + "/opcontrol"; + } + + if(opcontrolPath.equals("")){ + return false; + } + + // Check if user has sudo permissions without password by running sudo -l. + final Process p = RuntimeProcessFactory.getFactory().exec("sudo -l", project); + final StringBuffer buffer = new StringBuffer(); + + if(p == null){ + return false; + } + + Thread t = new Thread() { + @Override + public void run() { + try { + BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); + String s = null; + while ((s = input.readLine()) != null) { + buffer.append(s); + buffer.append('\n'); + } + p.waitFor(); + p.destroy(); + } catch (InterruptedException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + }; + + t.start(); + t.join(SUDO_TIMEOUT); + + String[] sudoLines = buffer.toString().split("\n"); + for (String s : sudoLines) { + if(s.contains(opcontrolPath) && s.contains("NOPASSWD")){ + return true; + } + } + } catch (IOException e) { + e.printStackTrace(); + return false; + } catch (InterruptedException e) { + e.printStackTrace(); + } + return false; + } + @Override protected void postExec(LaunchOptions options, OprofileDaemonEvent[] daemonEvents, ILaunch launch, Process process) { //add a listener for termination of the launch diff --git a/oprofile/org.eclipse.linuxtools.oprofile.launch/src/org/eclipse/linuxtools/internal/oprofile/launch/launching/OprofileManualLaunchConfigurationDelegate.java b/oprofile/org.eclipse.linuxtools.oprofile.launch/src/org/eclipse/linuxtools/internal/oprofile/launch/launching/OprofileManualLaunchConfigurationDelegate.java index 2166ec4..cca054b 100644 --- a/oprofile/org.eclipse.linuxtools.oprofile.launch/src/org/eclipse/linuxtools/internal/oprofile/launch/launching/OprofileManualLaunchConfigurationDelegate.java +++ b/oprofile/org.eclipse.linuxtools.oprofile.launch/src/org/eclipse/linuxtools/internal/oprofile/launch/launching/OprofileManualLaunchConfigurationDelegate.java @@ -48,7 +48,7 @@ public class OprofileManualLaunchConfigurationDelegate extends AbstractOprofileL protected boolean preExec(LaunchOptions options, OprofileDaemonEvent[] daemonEvents) { // Set current project to allow using the oprofile path that // was chosen for the project - Oprofile.setCurrentProject(getProject()); + Oprofile.OprofileProject.setProject(getProject()); // //set up the oprofile daemon // try { // //kill the daemon (it shouldn't be running already, but to be safe) |

