Search in sources :

Example 11 with IValgrindMessage

use of org.eclipse.linuxtools.valgrind.core.IValgrindMessage in project linuxtools by eclipse.

the class ValgrindLaunchConfigurationDelegate method launch.

@Override
public void launch(ILaunchConfiguration config, String mode, ILaunch launch, IProgressMonitor m) throws CoreException {
    if (m == null) {
        m = new NullProgressMonitor();
    }
    // $NON-NLS-1$
    SubMonitor monitor = SubMonitor.convert(m, Messages.getString("ValgrindLaunchConfigurationDelegate.Profiling_Local_CCPP_Application"), 10);
    // check for cancellation
    if (monitor.isCanceled()) {
        return;
    }
    this.config = config;
    this.launch = launch;
    try {
        IProject project = CDebugUtils.verifyCProject(config).getProject();
        ValgrindUIPlugin.getDefault().setProfiledProject(project);
        command = getValgrindCommand();
        // remove any output from previous run
        ValgrindUIPlugin.getDefault().resetView();
        // reset stored launch data
        getPlugin().setCurrentLaunchConfiguration(null);
        getPlugin().setCurrentLaunch(null);
        String valgrindCommand = getValgrindCommand().getValgrindCommand();
        // also ensure Valgrind version is usable
        try {
            valgrindVersion = getPlugin().getValgrindVersion(project);
        } catch (CoreException e) {
            // if versioning failed, issue an error dialog and return
            errorDialog(// $NON-NLS-1$
            Messages.getString("ValgrindLaunchConfigurationDelegate.Valgrind_version_failed_msg"), e.getMessage());
            return;
        }
        monitor.worked(1);
        IPath exePath = CDebugUtils.verifyProgramPath(config);
        String[] arguments = getProgramArgumentsArray(config);
        File workDir = getWorkingDirectory(config);
        if (workDir == null) {
            // $NON-NLS-1$ //$NON-NLS-2$
            workDir = new File(System.getProperty("user.home", "."));
        }
        // set output directory in config
        IValgrindOutputDirectoryProvider provider = getPlugin().getOutputDirectoryProvider();
        setOutputPath(config, provider.getOutputPath());
        outputPath = verifyOutputPath(config);
        // create/empty output directory
        createDirectory(outputPath);
        // tool that was launched
        toolID = getTool(config);
        // ask tool extension for arguments
        dynamicDelegate = getDynamicDelegate(toolID);
        String[] opts = getValgrindArgumentsArray(config);
        // set the default source locator if required
        setDefaultSourceLocator(launch, config);
        ArrayList<String> cmdLine = new ArrayList<>(1 + arguments.length);
        cmdLine.add(valgrindCommand);
        cmdLine.addAll(Arrays.asList(opts));
        cmdLine.add(exePath.toPortableString());
        cmdLine.addAll(Arrays.asList(arguments));
        String[] commandArray = cmdLine.toArray(new String[cmdLine.size()]);
        boolean usePty = config.getAttribute(ICDTLaunchConfigurationConstants.ATTR_USE_TERMINAL, ICDTLaunchConfigurationConstants.USE_TERMINAL_DEFAULT);
        monitor.worked(1);
        // check for cancellation
        if (monitor.isCanceled()) {
            return;
        }
        // call Valgrind
        command.execute(commandArray, getEnvironment(config), workDir, usePty, project);
        monitor.worked(3);
        process = createNewProcess(launch, command.getProcess(), commandArray[0]);
        // set the command line used
        process.setAttribute(IProcess.ATTR_CMDLINE, command.getCommandLine());
        while (!process.isTerminated()) {
            Thread.sleep(100);
        }
        // store these for use by other classes
        getPlugin().setCurrentLaunchConfiguration(config);
        getPlugin().setCurrentLaunch(launch);
        // parse Valgrind logs
        IValgrindMessage[] messages = parseLogs(outputPath);
        // create launch summary string to distinguish this launch
        launchStr = createLaunchStr();
        // create view
        ValgrindUIPlugin.getDefault().createView(launchStr, toolID);
        // set log messages
        ValgrindViewPart view = ValgrindUIPlugin.getDefault().getView();
        view.setMessages(messages);
        monitor.worked(1);
        // pass off control to extender
        dynamicDelegate.handleLaunch(config, launch, outputPath, monitor.newChild(2));
        // initialize tool-specific part of view
        dynamicDelegate.initializeView(view.getDynamicView(), launchStr, monitor.newChild(1));
        // refresh view
        ValgrindUIPlugin.getDefault().refreshView();
        // show view
        ValgrindUIPlugin.getDefault().showView();
        // set up resource listener for post-build events.
        ResourcesPlugin.getWorkspace().addResourceChangeListener(new ProjectBuildListener(project), IResourceChangeEvent.POST_BUILD);
        monitor.worked(1);
    } catch (IOException e) {
        // $NON-NLS-1$
        abort(Messages.getString("ValgrindLaunchConfigurationDelegate.Error_starting_process"), e, ICDTLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        m.done();
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IPath(org.eclipse.core.runtime.IPath) IValgrindMessage(org.eclipse.linuxtools.valgrind.core.IValgrindMessage) SubMonitor(org.eclipse.core.runtime.SubMonitor) ArrayList(java.util.ArrayList) IOException(java.io.IOException) IProject(org.eclipse.core.resources.IProject) CoreException(org.eclipse.core.runtime.CoreException) IValgrindOutputDirectoryProvider(org.eclipse.linuxtools.valgrind.launch.IValgrindOutputDirectoryProvider) ValgrindViewPart(org.eclipse.linuxtools.internal.valgrind.ui.ValgrindViewPart) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 12 with IValgrindMessage

use of org.eclipse.linuxtools.valgrind.core.IValgrindMessage in project linuxtools by eclipse.

the class ValgrindLaunchConfigurationDelegate method createMarkers.

private void createMarkers(IValgrindMessage[] messages) throws CoreException {
    // find the topmost stack frame within the workspace to annotate with marker
    // traverse nested errors as well
    Stack<IValgrindMessage> messageStack = new Stack<>();
    messageStack.addAll(Arrays.asList(messages));
    while (!messageStack.isEmpty()) {
        IValgrindMessage message = messageStack.pop();
        IMarker marker = null;
        IValgrindMessage[] children = message.getChildren();
        for (int i = 0; i < children.length; i++) {
            // if we've found our marker we don't care about any further frames in this stack
            if (children[i] instanceof ValgrindStackFrame && marker == null) {
                ValgrindStackFrame frame = (ValgrindStackFrame) children[i];
                if (frame.getLine() > 0) {
                    ISourceLocator locator = frame.getSourceLocator();
                    ISourceLookupResult result = DebugUITools.lookupSource(frame.getFile(), locator);
                    Object sourceElement = result.getSourceElement();
                    if (sourceElement != null) {
                        // Resolve IResource in case we get a LocalFileStorage object
                        if (sourceElement instanceof LocalFileStorage) {
                            IPath filePath = ((LocalFileStorage) sourceElement).getFullPath();
                            URI fileURI = URIUtil.toURI(filePath);
                            IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
                            IFile[] files = root.findFilesForLocationURI(fileURI);
                            if (files.length > 0) {
                                // Take the first match
                                sourceElement = files[0];
                            }
                        }
                        if (sourceElement instanceof IResource) {
                            IResource resource = (IResource) sourceElement;
                            marker = resource.createMarker(ValgrindLaunchPlugin.MARKER_TYPE);
                            marker.setAttribute(IMarker.MESSAGE, message.getText());
                            marker.setAttribute(IMarker.SEVERITY, IMarker.SEVERITY_ERROR);
                            marker.setAttribute(IMarker.LINE_NUMBER, frame.getLine());
                        }
                    }
                }
            } else if (children[i] instanceof ValgrindError) {
                // nested error
                messageStack.push(children[i]);
            }
        }
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IValgrindMessage(org.eclipse.linuxtools.valgrind.core.IValgrindMessage) IPath(org.eclipse.core.runtime.IPath) LocalFileStorage(org.eclipse.debug.core.sourcelookup.containers.LocalFileStorage) URI(java.net.URI) Stack(java.util.Stack) ValgrindError(org.eclipse.linuxtools.internal.valgrind.core.ValgrindError) IWorkspaceRoot(org.eclipse.core.resources.IWorkspaceRoot) ISourceLookupResult(org.eclipse.debug.ui.sourcelookup.ISourceLookupResult) ValgrindStackFrame(org.eclipse.linuxtools.internal.valgrind.core.ValgrindStackFrame) IMarker(org.eclipse.core.resources.IMarker) ISourceLocator(org.eclipse.debug.core.model.ISourceLocator) IResource(org.eclipse.core.resources.IResource)

Example 13 with IValgrindMessage

use of org.eclipse.linuxtools.valgrind.core.IValgrindMessage in project linuxtools by eclipse.

the class ValgrindRemoteProxyLaunchDelegate method launch.

@Override
public void launch(final ILaunchConfiguration config, String mode, final ILaunch launch, IProgressMonitor m) throws CoreException {
    if (m == null) {
        m = new NullProgressMonitor();
    }
    // Clear process as we wait on it to be instantiated
    process = null;
    SubMonitor monitor = SubMonitor.convert(m, Messages.getString("ValgrindRemoteLaunchDelegate.task_name"), // $NON-NLS-1$
    10);
    // check for cancellation
    if (monitor.isCanceled()) {
        return;
    }
    this.config = config;
    this.launch = launch;
    try {
        // remove any output from previous run
        ValgrindUIPlugin.getDefault().resetView();
        // reset stored launch data
        getPlugin().setCurrentLaunchConfiguration(null);
        getPlugin().setCurrentLaunch(null);
        this.configUtils = new ConfigUtils(config);
        IProject project = configUtils.getProject();
        ValgrindUIPlugin.getDefault().setProfiledProject(project);
        URI exeURI = new URI(configUtils.getExecutablePath());
        RemoteConnection exeRC = new RemoteConnection(exeURI);
        monitor.worked(1);
        String valgrindPathString = RuntimeProcessFactory.getFactory().whichCommand(VALGRIND_CMD, project);
        IPath valgrindFullPath = Path.fromOSString(valgrindPathString);
        boolean copyExecutable = configUtils.getCopyExecutable();
        if (copyExecutable) {
            URI copyExeURI = new URI(configUtils.getCopyFromExecutablePath());
            RemoteConnection copyExeRC = new RemoteConnection(copyExeURI);
            IRemoteFileProxy copyExeRFP = copyExeRC.getRmtFileProxy();
            IFileStore copyExeFS = copyExeRFP.getResource(copyExeURI.getPath());
            IRemoteFileProxy exeRFP = exeRC.getRmtFileProxy();
            IFileStore exeFS = exeRFP.getResource(exeURI.getPath());
            IFileInfo exeFI = exeFS.fetchInfo();
            if (exeFI.isDirectory()) {
                // Assume the user wants to copy the file to the given directory, using
                // the same filename as the "copy from" executable.
                IPath copyExePath = Path.fromOSString(copyExeURI.getPath());
                IPath newExePath = Path.fromOSString(exeURI.getPath()).append(copyExePath.lastSegment());
                // update the exeURI with the new path.
                exeURI = new URI(exeURI.getScheme(), exeURI.getAuthority(), newExePath.toString(), exeURI.getQuery(), exeURI.getFragment());
                exeFS = exeRFP.getResource(exeURI.getPath());
            }
            copyExeFS.copy(exeFS, EFS.OVERWRITE | EFS.SHALLOW, SubMonitor.convert(monitor, 1));
        // Note: assume that we don't need to create a new exeRC since the
        // scheme and authority remain the same between the original exeURI and the new one.
        }
        valgrindVersion = getValgrindVersion(project);
        IPath remoteBinFile = Path.fromOSString(exeURI.getPath());
        String configWorkingDir = configUtils.getWorkingDirectory();
        IFileStore workingDir;
        if (configWorkingDir == null) {
            // If no working directory was provided, use the directory containing the
            // the executable as the working directory.
            IPath workingDirPath = remoteBinFile.removeLastSegments(1);
            IRemoteFileProxy workingDirRFP = exeRC.getRmtFileProxy();
            workingDir = workingDirRFP.getResource(workingDirPath.toOSString());
        } else {
            URI workingDirURI = new URI(configUtils.getWorkingDirectory());
            RemoteConnection workingDirRC = new RemoteConnection(workingDirURI);
            IRemoteFileProxy workingDirRFP = workingDirRC.getRmtFileProxy();
            workingDir = workingDirRFP.getResource(workingDirURI.getPath());
        }
        // $NON-NLS-1$
        IPath remoteLogDir = Path.fromOSString("/tmp/");
        // $NON-NLS-1$
        outputPath = remoteLogDir.append("eclipse-valgrind-" + System.currentTimeMillis());
        exeRC.createFolder(outputPath, SubMonitor.convert(monitor, 1));
        // create/empty local output directory
        IValgrindOutputDirectoryProvider provider = getPlugin().getOutputDirectoryProvider();
        setOutputPath(config, provider.getOutputPath());
        IPath localOutputDir = null;
        try {
            localOutputDir = provider.getOutputPath();
            createDirectory(localOutputDir);
        } catch (IOException e2) {
            throw new CoreException(new Status(IStatus.ERROR, ValgrindLaunchPlugin.PLUGIN_ID, IStatus.OK, "", // $NON-NLS-1$
            e2));
        }
        // tool that was launched
        toolID = getTool(config);
        // ask tool extension for arguments
        dynamicDelegate = getDynamicDelegate(toolID);
        String[] valgrindArgs = getValgrindArgumentsArray(config);
        String[] executableArgs = getProgramArgumentsArray(config);
        String[] allArgs = new String[executableArgs.length + valgrindArgs.length + 2];
        int idx = 0;
        allArgs[idx++] = VALGRIND_CMD;
        for (String valgrindArg : valgrindArgs) {
            allArgs[idx++] = valgrindArg;
        }
        allArgs[idx++] = remoteBinFile.toOSString();
        for (String executableArg : executableArgs) {
            allArgs[idx++] = executableArg;
        }
        Process p = RuntimeProcessFactory.getFactory().exec(allArgs, new String[0], workingDir, project);
        int state = p.waitFor();
        if (state != IRemoteCommandLauncher.OK) {
            abort(// $NON-NLS-1$ //$NON-NLS-2$
            Messages.getString("ValgrindLaunchConfigurationDelegate.Launch_exited_status") + " " + state + ". " + // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            NLS.bind(Messages.getString("ValgrindRemoteProxyLaunchDelegate.see_reference"), "IRemoteCommandLauncher") + // $NON-NLS-1$
            "\n", null, ICDTLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
        }
        if (p.exitValue() != 0) {
            String line = null;
            StringBuilder valgrindOutSB = new StringBuilder();
            BufferedReader valgrindOut = new BufferedReader(new InputStreamReader(p.getInputStream()));
            while ((line = valgrindOut.readLine()) != null) {
                valgrindOutSB.append(line);
            }
            StringBuilder valgrindErrSB = new StringBuilder();
            BufferedReader valgrindErr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
            while ((line = valgrindErr.readLine()) != null) {
                valgrindErrSB.append(line);
            }
            abort(// $NON-NLS-1$
            NLS.bind("ValgrindRemoteProxyLaunchDelegate.Stdout", valgrindOutSB.toString()) + "\n" + // $NON-NLS-1$ //$NON-NLS-2$
            NLS.bind("ValgrindRemoteProxyLaunchDelegate.Stderr", valgrindErrSB.toString()), null, ICDTLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
        }
        // move remote log files to local directory
        exeRC.download(outputPath, localOutputDir, SubMonitor.convert(monitor, 1));
        // remove remote log dir and all files under it
        exeRC.delete(outputPath, SubMonitor.convert(monitor, 1));
        // store these for use by other classes
        getPlugin().setCurrentLaunchConfiguration(config);
        getPlugin().setCurrentLaunch(launch);
        // parse Valgrind logs
        IValgrindMessage[] messages = parseLogs(localOutputDir);
        // create launch summary string to distinguish this launch
        launchStr = createLaunchStr(valgrindFullPath);
        // create view
        ValgrindUIPlugin.getDefault().createView(launchStr, toolID);
        // set log messages
        ValgrindViewPart view = ValgrindUIPlugin.getDefault().getView();
        view.setMessages(messages);
        monitor.worked(1);
        // pass off control to extender
        dynamicDelegate.handleLaunch(config, launch, localOutputDir, monitor.newChild(2));
        // initialize tool-specific part of view
        dynamicDelegate.initializeView(view.getDynamicView(), launchStr, monitor.newChild(1));
        // refresh view
        ValgrindUIPlugin.getDefault().refreshView();
        // show view
        ValgrindUIPlugin.getDefault().showView();
        monitor.worked(1);
    } catch (URISyntaxException | IOException | RemoteConnectionException | InterruptedException e) {
        abort(e.getLocalizedMessage(), null, ICDTLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
    } finally {
        monitor.done();
        m.done();
    }
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ConfigUtils(org.eclipse.linuxtools.profiling.launch.ConfigUtils) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) IRemoteFileProxy(org.eclipse.linuxtools.profiling.launch.IRemoteFileProxy) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) RemoteConnectionException(org.eclipse.linuxtools.profiling.launch.RemoteConnectionException) IPath(org.eclipse.core.runtime.IPath) InputStreamReader(java.io.InputStreamReader) IValgrindMessage(org.eclipse.linuxtools.valgrind.core.IValgrindMessage) SubMonitor(org.eclipse.core.runtime.SubMonitor) IOException(java.io.IOException) IProject(org.eclipse.core.resources.IProject) IFileInfo(org.eclipse.core.filesystem.IFileInfo) IValgrindOutputDirectoryProvider(org.eclipse.linuxtools.valgrind.launch.IValgrindOutputDirectoryProvider) CoreException(org.eclipse.core.runtime.CoreException) ValgrindViewPart(org.eclipse.linuxtools.internal.valgrind.ui.ValgrindViewPart) BufferedReader(java.io.BufferedReader) IFileStore(org.eclipse.core.filesystem.IFileStore) RemoteConnection(org.eclipse.linuxtools.profiling.launch.RemoteConnection)

Example 14 with IValgrindMessage

use of org.eclipse.linuxtools.valgrind.core.IValgrindMessage in project linuxtools by eclipse.

the class AbstractMemcheckTest method checkTestMessages.

/**
 * Check messages appear as expected for the specified test.
 *
 * @param messages IValgrindMessage messages
 * @param testName test name
 */
public void checkTestMessages(IValgrindMessage[] messages, String testName) {
    assertTrue(messages.length > 0);
    // $NON-NLS-1$
    String lostBytesMsg = "10 bytes in 1 blocks are definitely lost in loss record 1 of 1";
    // $NON-NLS-1$
    String invalidReadMsg = "Invalid read of size 1";
    // $NON-NLS-1$
    String invalidWriteMsg = "Invalid write of size 1";
    for (IValgrindMessage message : messages) {
        for (IValgrindMessage child : message.getChildren()) {
            if (child instanceof ValgrindStackFrame) {
                ValgrindStackFrame stackFrameMsg = (ValgrindStackFrame) child;
                // check expected error messages exist for basicTest (child process in multiProcTest)
                if (// $NON-NLS-1$ //$NON-NLS-2$
                ("testNumErrors".equals(testName) || "testExec".equals(testName)) && "test.c".equals(stackFrameMsg.getFile())) {
                    // $NON-NLS-1$
                    assertTrue(stackFrameMsg.getLine() >= 15);
                    switch(stackFrameMsg.getLine()) {
                        case 15:
                            assertTrue(message.getText().contains(lostBytesMsg));
                            break;
                        case 16:
                            assertTrue(message.getText().contains(invalidReadMsg));
                            break;
                        case 17:
                            assertTrue(message.getText().contains(invalidWriteMsg));
                            break;
                        default:
                            break;
                    }
                }
                // check expected error messages exist for parent process in multiProcTest
                if (// $NON-NLS-1$ //$NON-NLS-2$
                ("testNoExec".equals(testName) || "testExec".equals(testName)) && "parent.c".equals(stackFrameMsg.getFile())) {
                    // $NON-NLS-1$
                    assertEquals(8, stackFrameMsg.getLine());
                    assertTrue(child.getParent().getText().contains(lostBytesMsg));
                }
            }
        }
    }
}
Also used : IValgrindMessage(org.eclipse.linuxtools.valgrind.core.IValgrindMessage) ValgrindStackFrame(org.eclipse.linuxtools.internal.valgrind.core.ValgrindStackFrame)

Example 15 with IValgrindMessage

use of org.eclipse.linuxtools.valgrind.core.IValgrindMessage in project linuxtools by eclipse.

the class DoubleClickTest method doDoubleClick.

private void doDoubleClick() {
    ValgrindViewPart view = ValgrindUIPlugin.getDefault().getView();
    CoreMessagesViewer viewer = view.getMessagesViewer();
    // get first leaf
    IValgrindMessage[] elements = (IValgrindMessage[]) viewer.getTreeViewer().getInput();
    IValgrindMessage element = elements[0];
    TreePath path = new TreePath(new Object[] { element });
    frame = null;
    while (element.getChildren().length > 0) {
        element = element.getChildren()[0];
        path = path.createChildPath(element);
        if (element instanceof ValgrindStackFrame) {
            frame = (ValgrindStackFrame) element;
        }
    }
    assertNotNull(frame);
    viewer.getTreeViewer().expandToLevel(frame, AbstractTreeViewer.ALL_LEVELS);
    TreeSelection selection = new TreeSelection(path);
    // do double click
    IDoubleClickListener listener = viewer.getDoubleClickListener();
    listener.doubleClick(new DoubleClickEvent(viewer.getTreeViewer(), selection));
}
Also used : ValgrindViewPart(org.eclipse.linuxtools.internal.valgrind.ui.ValgrindViewPart) IValgrindMessage(org.eclipse.linuxtools.valgrind.core.IValgrindMessage) TreePath(org.eclipse.jface.viewers.TreePath) TreeSelection(org.eclipse.jface.viewers.TreeSelection) ValgrindStackFrame(org.eclipse.linuxtools.internal.valgrind.core.ValgrindStackFrame) IDoubleClickListener(org.eclipse.jface.viewers.IDoubleClickListener) DoubleClickEvent(org.eclipse.jface.viewers.DoubleClickEvent) CoreMessagesViewer(org.eclipse.linuxtools.internal.valgrind.ui.CoreMessagesViewer)

Aggregations

IValgrindMessage (org.eclipse.linuxtools.valgrind.core.IValgrindMessage)24 ValgrindStackFrame (org.eclipse.linuxtools.internal.valgrind.core.ValgrindStackFrame)10 ValgrindViewPart (org.eclipse.linuxtools.internal.valgrind.ui.ValgrindViewPart)10 Test (org.junit.Test)9 ILaunchConfiguration (org.eclipse.debug.core.ILaunchConfiguration)6 ArrayList (java.util.ArrayList)5 IMarker (org.eclipse.core.resources.IMarker)5 TreePath (org.eclipse.jface.viewers.TreePath)4 TreeSelection (org.eclipse.jface.viewers.TreeSelection)4 ValgrindError (org.eclipse.linuxtools.internal.valgrind.core.ValgrindError)4 IFile (org.eclipse.core.resources.IFile)3 CoreException (org.eclipse.core.runtime.CoreException)3 IPath (org.eclipse.core.runtime.IPath)3 File (java.io.File)2 IOException (java.io.IOException)2 URI (java.net.URI)2 IProject (org.eclipse.core.resources.IProject)2 IStatus (org.eclipse.core.runtime.IStatus)2 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)2 Status (org.eclipse.core.runtime.Status)2