Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9b15215b9b65bae385473f11d270edc7a34c60a9 (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
package org.eclipse.linuxtools.internal.systemtap.ui.ide.launcher;

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.Launch;
import org.eclipse.linuxtools.systemtap.ui.consolelog.structures.ScriptConsole;
import org.eclipse.linuxtools.systemtap.ui.consolelog.structures.ScriptConsole.ScriptConsoleObserver;

public class SystemTapScriptLaunch extends Launch
    implements ScriptConsoleObserver {

    private ScriptConsole console = null;
    private boolean runStarted = false;
    private boolean runStopped = false;

    public SystemTapScriptLaunch(ILaunchConfiguration launchConfiguration, String mode) {
        super(launchConfiguration, mode, null);
    }

    public void setConsole(ScriptConsole console) {
        if (this.console == console) {
            return;
        }
        // If another launch is using the same console, remove that launch since
        // ScriptConsole prevents two identical stap scripts from being be run at once.
        this.console = console;
        ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
        for (ILaunch launch : manager.getLaunches()) {
            if (launch.equals(this)) {
                continue;
            }
            if (launch instanceof SystemTapScriptLaunch) {
                SystemTapScriptLaunch olaunch = (SystemTapScriptLaunch) launch;
                if (olaunch.console == null || olaunch.console.equals(console)) {
                    olaunch.forceRemove();
                }
            }
        }
        console.addScriptConsoleObserver(this);
    }

    public ScriptConsole getConsole() {
        return console;
    }

    @Override
    public void runningStateChanged(boolean started, boolean stopped) {
        if (!runStarted && started) {
            runStarted = started;
            if (console.getCommand().getProcess() != null) {
                DebugPlugin.newProcess(this, console.getCommand().getProcess(), console.getName());
            }
            if (stopped) {
                runStopped = true;
            }
            console.removeScriptConsoleObserver(this);
        }
    }

    @Override
    public boolean canTerminate() {
        return !isTerminated();
    }

    @Override
    public boolean isTerminated() {
        if (!runStopped) {
            if (super.isTerminated() || (console != null && !console.isRunning())) {
                runStopped = true;
            }
        }
        return runStopped;
    }

    @Override
    public void terminate() {
        if (console != null) {
            console.stop();
        }
    }

    @Override
    public void launchRemoved(ILaunch launch) {
        super.launchRemoved(launch);
        if (launch.equals(this)) {
            removeConsole();
        }
    }

    private void removeConsole() {
        if (console != null) {
            console.removeScriptConsoleObserver(this);
            console = null;
        }
    }

    public void forceRemove() {
        removeConsole();
        runStopped = true;
        ILaunchManager manager = DebugPlugin.getDefault().getLaunchManager();
        manager.removeLaunch(this);
    }
}

Back to the top