Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 4850e0781d11931ece2498b6cbf0ccc902dc2ee6 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*******************************************************************************
 * Copyright (c) 2006, 2010 Wind River Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *     Nokia - create and use backend service. 
 *******************************************************************************/
package org.eclipse.cdt.dsf.gdb.internal.ui.actions;

import java.util.concurrent.RejectedExecutionException;

import org.eclipse.cdt.dsf.concurrent.DsfExecutor;
import org.eclipse.cdt.dsf.concurrent.DsfRunnable;
import org.eclipse.cdt.dsf.concurrent.ImmediateDataRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.ImmediateRequestMonitor;
import org.eclipse.cdt.dsf.datamodel.DMContexts;
import org.eclipse.cdt.dsf.debug.service.IProcesses;
import org.eclipse.cdt.dsf.debug.service.IProcesses.IProcessDMContext;
import org.eclipse.cdt.dsf.gdb.internal.ui.GdbUIPlugin;
import org.eclipse.cdt.dsf.service.DsfServicesTracker;
import org.eclipse.cdt.dsf.service.DsfSession;
import org.eclipse.cdt.dsf.ui.viewmodel.datamodel.IDMVMContext;
import org.eclipse.debug.core.commands.IDebugCommandRequest;
import org.eclipse.debug.core.commands.IEnabledStateRequest;
import org.eclipse.debug.core.commands.ITerminateHandler;

public class DsfTerminateCommand implements ITerminateHandler {
    private final DsfExecutor fExecutor;
    private final DsfServicesTracker fTracker;
    
    public DsfTerminateCommand(DsfSession session) {
        fExecutor = session.getExecutor();
        fTracker = new DsfServicesTracker(GdbUIPlugin.getBundleContext(), session.getId());
    }    

    public void dispose() {
        fTracker.dispose();
    }

    // Run control may not be avilable after a connection is terminated and shut down.
    @Override
    public void canExecute(final IEnabledStateRequest request) {
        if (request.getElements().length != 1 || 
            !(request.getElements()[0] instanceof IDMVMContext) ) 
        {
            request.setEnabled(false);
            request.done();
            return;
        }

        IDMVMContext vmc = (IDMVMContext)request.getElements()[0];
        
        // First check if there is an ancestor process to terminate.  This is the smallest entity we can terminate
        final IProcessDMContext processDmc = DMContexts.getAncestorOfType(vmc.getDMContext(), IProcessDMContext.class);
        if (processDmc == null) {
            request.setEnabled(false);
            request.done();
            return;
        }
        
        try {
            fExecutor.execute(
                new DsfRunnable() { 
                    @Override
                    public void run() {
                        // Get the processes service and the exec context.
                    	IProcesses procService = fTracker.getService(IProcesses.class);
                        if (procService == null) {
                            // Service already invalid.
                            request.setEnabled(false);
                            request.done();
                        } else {
                        	procService.canTerminate(processDmc, new ImmediateDataRequestMonitor<Boolean>() {
                        		@Override
                        		protected void handleCompleted() {
                        			request.setEnabled(isSuccess() && getData());
                        			request.done();
                        		}
                        	});
                        }
                    }
                });
        } catch (RejectedExecutionException e) {
            request.setEnabled(false);
            request.done();
        }
    }

    @Override
    public boolean execute(final IDebugCommandRequest request) {
        if (request.getElements().length != 1 || 
        	!(request.getElements()[0] instanceof IDMVMContext)) {
        	request.done();
        	return false;
        }

        IDMVMContext vmc = (IDMVMContext)request.getElements()[0];

        // First check if there is an ancestor process to terminate.  This is the smallest entity we can terminate
        final IProcessDMContext processDmc = DMContexts.getAncestorOfType(vmc.getDMContext(), IProcessDMContext.class);
        if (processDmc == null) {
        	request.done();
        	return false;
        }

        try {
            fExecutor.execute(new DsfRunnable() { 
                @Override
                public void run() {
                	IProcesses procService = fTracker.getService(IProcesses.class);
                    if (procService != null) {
                    	procService.terminate(processDmc, new ImmediateRequestMonitor() {
                            @Override
                            protected void handleCompleted() {
                                request.setStatus(getStatus());
                                request.done();
                            };
                        });
                    } else {
                    	request.done();
                    }
                 }
            });
        } catch (RejectedExecutionException e) {
            request.done();
        }
        return false;
    }
    
}

Back to the top