Search in sources :

Example 1 with RemoteConnection

use of org.eclipse.linuxtools.profiling.launch.RemoteConnection in project linuxtools by eclipse.

the class PerfLaunchConfigDelegate method launch.

@Override
public void launch(ILaunchConfiguration config, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
    try {
        ConfigUtils configUtils = new ConfigUtils(config);
        project = configUtils.getProject();
        // Set the current project that will be profiled
        PerfPlugin.getDefault().setProfiledProject(project);
        // check if Perf exists in $PATH
        if (!PerfCore.checkPerfInPath(project)) {
            // $NON-NLS-1$
            IStatus status = new Status(IStatus.ERROR, PerfPlugin.PLUGIN_ID, "Error: Perf was not found on PATH");
            throw new CoreException(status);
        }
        URI workingDirURI = new URI(config.getAttribute(RemoteProxyCMainTab.ATTR_REMOTE_WORKING_DIRECTORY_NAME, EMPTY_STRING));
        // Local project
        if (workingDirURI.toString().equals(EMPTY_STRING)) {
            workingDirURI = getWorkingDirectory(config).toURI();
            workingDirPath = Path.fromPortableString(workingDirURI.getPath());
            binPath = CDebugUtils.verifyProgramPath(config);
        } else {
            workingDirPath = Path.fromPortableString(workingDirURI.getPath() + IPath.SEPARATOR);
            URI binURI = new URI(configUtils.getExecutablePath());
            binPath = Path.fromPortableString(binURI.getPath().toString());
        }
        PerfPlugin.getDefault().setWorkingDir(workingDirPath);
        if (config.getAttribute(PerfPlugin.ATTR_ShowStat, PerfPlugin.ATTR_ShowStat_default)) {
            showStat(config, launch);
        } else {
            String perfPathString = RuntimeProcessFactory.getFactory().whichCommand(PerfPlugin.PERF_COMMAND, project);
            IFileStore workingDir;
            RemoteConnection workingDirRC = new RemoteConnection(workingDirURI);
            IRemoteFileProxy workingDirRFP = workingDirRC.getRmtFileProxy();
            workingDir = workingDirRFP.getResource(workingDirURI.getPath());
            // Build the commandline string to run perf recording the given project
            // Program args from launch config.
            String[] arguments = getProgramArgumentsArray(config);
            ArrayList<String> command = new ArrayList<>(4 + arguments.length);
            // Get the base commandline string (with flags/options based on config)
            command.addAll(Arrays.asList(PerfCore.getRecordString(config)));
            // Add the path to the executable
            command.add(binPath.toPortableString());
            command.set(0, perfPathString);
            command.add(2, OUTPUT_STR + PerfPlugin.PERF_DEFAULT_DATA);
            // Compile string
            command.addAll(Arrays.asList(arguments));
            // Spawn the process
            String[] commandArray = command.toArray(new String[command.size()]);
            Process pProxy = RuntimeProcessFactory.getFactory().exec(commandArray, getEnvironment(config), workingDir, project);
            // $NON-NLS-1$
            MessageConsole console = new MessageConsole("Perf Console", null);
            console.activate();
            ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[] { console });
            MessageConsoleStream stream = console.newMessageStream();
            if (pProxy != null) {
                try (BufferedReader error = new BufferedReader(new InputStreamReader(pProxy.getErrorStream()))) {
                    String err = error.readLine();
                    while (err != null) {
                        stream.println(err);
                        err = error.readLine();
                    }
                }
            }
            /* This commented part is the basic method to run perf record without integrating into eclipse.
                        String binCall = exePath.toOSString();
                        for(String arg : arguments) {
                            binCall.concat(" " + arg);
                        }
                        PerfCore.Run(binCall);*/
            pProxy.destroy();
            PrintStream print = null;
            if (config.getAttribute(IDebugUIConstants.ATTR_CAPTURE_IN_CONSOLE, true)) {
                // Get the console to output to.
                // This may not be the best way to accomplish this but it shall do for now.
                ConsolePlugin plugin = ConsolePlugin.getDefault();
                IConsoleManager conMan = plugin.getConsoleManager();
                IConsole[] existing = conMan.getConsoles();
                IOConsole binaryOutCons = null;
                // Find the console
                for (IConsole x : existing) {
                    if (x.getName().contains(renderProcessLabel(commandArray[0]))) {
                        binaryOutCons = (IOConsole) x;
                    }
                }
                if ((binaryOutCons == null) && (existing.length != 0)) {
                    // if can't be found get the most recent opened, this should probably never happen.
                    if (existing[existing.length - 1] instanceof IOConsole)
                        binaryOutCons = (IOConsole) existing[existing.length - 1];
                }
                // Get the printstream via the outputstream.
                // Get ouput stream
                OutputStream outputTo;
                if (binaryOutCons != null) {
                    outputTo = binaryOutCons.newOutputStream();
                    // Get the printstream for that console
                    print = new PrintStream(outputTo);
                }
                for (int i = 0; i < command.size(); i++) {
                    // $NON-NLS-1$
                    print.print(command.get(i) + " ");
                }
                // Print Message
                print.println();
                // $NON-NLS-1$
                print.println("Analysing recorded perf.data, please wait...");
            // Possibly should pass this (the console reference) on to PerfCore.Report if theres anything we ever want to spit out to user.
            }
            PerfCore.report(config, workingDirPath, monitor, null, print);
            URI perfDataURI = null;
            IRemoteFileProxy proxy = null;
            perfDataURI = new URI(workingDirURI.toString() + IPath.SEPARATOR + PerfPlugin.PERF_DEFAULT_DATA);
            proxy = RemoteProxyManager.getInstance().getFileProxy(perfDataURI);
            IFileStore perfDataFileStore = proxy.getResource(perfDataURI.getPath());
            IFileInfo info = perfDataFileStore.fetchInfo();
            info.setAttribute(EFS.ATTRIBUTE_READ_ONLY, true);
            perfDataFileStore.putInfo(info, EFS.SET_ATTRIBUTES, null);
            PerfCore.refreshView(renderProcessLabel(binPath.toPortableString()));
            if (config.getAttribute(PerfPlugin.ATTR_ShowSourceDisassembly, PerfPlugin.ATTR_ShowSourceDisassembly_default)) {
                showSourceDisassembly(Path.fromPortableString(workingDirURI.toString() + IPath.SEPARATOR));
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
        abort(e.getLocalizedMessage(), null, ICDTLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
    } catch (RemoteConnectionException e) {
        e.printStackTrace();
        abort(e.getLocalizedMessage(), null, ICDTLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
    } catch (URISyntaxException e) {
        e.printStackTrace();
        abort(e.getLocalizedMessage(), null, ICDTLaunchConfigurationConstants.ERR_INTERNAL_ERROR);
    }
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) OutputStream(java.io.OutputStream) ConfigUtils(org.eclipse.linuxtools.profiling.launch.ConfigUtils) ArrayList(java.util.ArrayList) MessageConsoleStream(org.eclipse.ui.console.MessageConsoleStream) ConsolePlugin(org.eclipse.ui.console.ConsolePlugin) 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) PrintStream(java.io.PrintStream) MessageConsole(org.eclipse.ui.console.MessageConsole) RemoteConnectionException(org.eclipse.linuxtools.profiling.launch.RemoteConnectionException) InputStreamReader(java.io.InputStreamReader) IConsole(org.eclipse.ui.console.IConsole) IOException(java.io.IOException) IConsoleManager(org.eclipse.ui.console.IConsoleManager) IFileInfo(org.eclipse.core.filesystem.IFileInfo) CoreException(org.eclipse.core.runtime.CoreException) BufferedReader(java.io.BufferedReader) IFileStore(org.eclipse.core.filesystem.IFileStore) RemoteConnection(org.eclipse.linuxtools.profiling.launch.RemoteConnection) IOConsole(org.eclipse.ui.console.IOConsole)

Example 2 with RemoteConnection

use of org.eclipse.linuxtools.profiling.launch.RemoteConnection 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)

Aggregations

BufferedReader (java.io.BufferedReader)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 IFileInfo (org.eclipse.core.filesystem.IFileInfo)2 IFileStore (org.eclipse.core.filesystem.IFileStore)2 CoreException (org.eclipse.core.runtime.CoreException)2 IStatus (org.eclipse.core.runtime.IStatus)2 Status (org.eclipse.core.runtime.Status)2 ConfigUtils (org.eclipse.linuxtools.profiling.launch.ConfigUtils)2 IRemoteFileProxy (org.eclipse.linuxtools.profiling.launch.IRemoteFileProxy)2 RemoteConnection (org.eclipse.linuxtools.profiling.launch.RemoteConnection)2 RemoteConnectionException (org.eclipse.linuxtools.profiling.launch.RemoteConnectionException)2 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)1 ArrayList (java.util.ArrayList)1 IProject (org.eclipse.core.resources.IProject)1 IPath (org.eclipse.core.runtime.IPath)1 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)1