Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: dd7a71656dc4c8898b0f586bf152698d56b74c83 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/*******************************************************************************
 * Copyright (c) 2009, 2013 Ericsson 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:
 *     Ericsson - initial API and implementation
 *     Marc Khouzam (Ericsson) - Listen for IReverseModeChangedDMEvent (Bug 399163)
 *******************************************************************************/
package org.eclipse.cdt.dsf.gdb.internal.ui.commands;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.RejectedExecutionException;

import org.eclipse.cdt.debug.core.model.IReverseToggleHandler;
import org.eclipse.cdt.dsf.concurrent.DataRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.DsfExecutor;
import org.eclipse.cdt.dsf.concurrent.DsfRunnable;
import org.eclipse.cdt.dsf.concurrent.Query;
import org.eclipse.cdt.dsf.datamodel.DMContexts;
import org.eclipse.cdt.dsf.datamodel.IDMContext;
import org.eclipse.cdt.dsf.debug.service.IRunControl;
import org.eclipse.cdt.dsf.debug.service.IRunControl.IExecutionDMContext;
import org.eclipse.cdt.dsf.debug.service.command.ICommandControlService.ICommandControlDMContext;
import org.eclipse.cdt.dsf.gdb.internal.ui.GdbUIPlugin;
import org.eclipse.cdt.dsf.gdb.service.IReverseRunControl;
import org.eclipse.cdt.dsf.gdb.service.IReverseRunControl.IReverseModeChangedDMEvent;
import org.eclipse.cdt.dsf.service.DsfServiceEventHandler;
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.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.IRequest;
import org.eclipse.debug.core.commands.AbstractDebugCommand;
import org.eclipse.debug.core.commands.IDebugCommandRequest;
import org.eclipse.debug.core.commands.IEnabledStateRequest;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.progress.WorkbenchJob;
import org.eclipse.ui.services.IEvaluationService;

/**
 * Command that toggles the Reverse Debugging feature
 * 
 * @since 2.1
 */
public class GdbReverseToggleCommand extends AbstractDebugCommand implements IReverseToggleHandler {
    private final DsfExecutor fExecutor;
    private final DsfServicesTracker fTracker;
    private final DsfSession fSession;
    
    public GdbReverseToggleCommand(DsfSession session) {
        fExecutor = session.getExecutor();
        fTracker = new DsfServicesTracker(GdbUIPlugin.getBundleContext(), session.getId());
        fSession = session;
        try {
            fExecutor.execute(new DsfRunnable() {
                @Override
                public void run() {
                	fSession.addServiceEventListener(GdbReverseToggleCommand.this, null);
                }
            });
        } catch(RejectedExecutionException e) {}
    }    

    public void dispose() {
        try {
            fExecutor.execute(new DsfRunnable() {
                @Override
                public void run() {
                	fSession.removeServiceEventListener(GdbReverseToggleCommand.this);
                }
            });
        } catch (RejectedExecutionException e) {
            // Session already gone.
        }
        fTracker.dispose();
    }

    @Override
	protected void doExecute(Object[] targets, IProgressMonitor monitor, IRequest request) throws CoreException {
    	if (targets.length != 1) {
    		return;
    	}

    	IDMContext dmc = ((IDMVMContext)targets[0]).getDMContext();
        final ICommandControlDMContext controlDmc = DMContexts.getAncestorOfType(dmc, ICommandControlDMContext.class);  
        if (controlDmc == null) {
        	return;
        }

      	Query<Object> setReverseMode = new Query<Object>() {
            @Override
            public void execute(final DataRequestMonitor<Object> rm) {
       			final IReverseRunControl runControl = fTracker.getService(IReverseRunControl.class);

       			if (runControl != null) {
       				runControl.isReverseModeEnabled(controlDmc, 
       				                                new DataRequestMonitor<Boolean>(fExecutor, rm) {
       					@Override
       					public void handleSuccess() {
       	       				runControl.enableReverseMode(controlDmc, !getData(), rm);
       					}
       				});
       			} else {
       				rm.done();
       			}
       		}
       	};
    	try {
    		fExecutor.execute(setReverseMode);
    		setReverseMode.get();
		} catch (InterruptedException e) {
		} catch (ExecutionException e) {
        } catch (RejectedExecutionException e) {
        	// Can be thrown if the session is shutdown
        }
    }

    @Override
	protected boolean isExecutable(Object[] targets, IProgressMonitor monitor, IEnabledStateRequest request)
        throws CoreException 
    {
    	if (targets.length != 1) {
    		return false;
    	}
    	
    	IDMContext dmc = ((IDMVMContext)targets[0]).getDMContext();
        final ICommandControlDMContext controlDmc = DMContexts.getAncestorOfType(dmc, ICommandControlDMContext.class);
        final IExecutionDMContext execDmc = DMContexts.getAncestorOfType(dmc, IExecutionDMContext.class);
        if (controlDmc == null && execDmc == null) {
        	return false;
        }

        Query<Boolean> canSetReverseMode = new Query<Boolean>() {
        	@Override
        	public void execute(DataRequestMonitor<Boolean> rm) {
        		IReverseRunControl runControl = fTracker.getService(IReverseRunControl.class);

        		// Only allow to toggle reverse if the program is suspended.
        		// When the program is running, GDB will not answer our command
        		// in toggleReverse() and since it is blocking, it will hang the entire UI! 
        		if (runControl != null && 
        			runControl instanceof IRunControl && ((IRunControl)runControl).isSuspended(execDmc)) {
        			runControl.canEnableReverseMode(controlDmc, rm);
        		} else {
        			rm.setData(false);
        			rm.done();
        		}
        	}
        };
        try {
        	fExecutor.execute(canSetReverseMode);
        	return canSetReverseMode.get();
        } catch (InterruptedException e) {
        } catch (ExecutionException e) {
        } catch (RejectedExecutionException e) {
        	// Can be thrown if the session is shutdown
        }

		return false;
    }
    
    @Override
	protected Object getTarget(Object element) {
    	if (element instanceof IDMVMContext) {
    		return element;
    	}
        return null;
    }

    @Override
	protected boolean isRemainEnabled(IDebugCommandRequest request) {
		return true;
	}
    
    @Override
    public boolean toggleNeedsUpdating() {
    	return true;
    }

    @Override
    public boolean isReverseToggled(Object context) {
    	IDMContext dmc;

    	if (context instanceof IDMContext) {
    		dmc = (IDMContext)context;
    	} else if (context instanceof IDMVMContext) {
    		dmc = ((IDMVMContext)context).getDMContext();
    	} else {
    		return false;
    	}

    	final ICommandControlDMContext controlDmc = DMContexts.getAncestorOfType(dmc, ICommandControlDMContext.class);
    	if (controlDmc == null) {
    		return false;
    	}

    	Query<Boolean> isToggledQuery = new Query<Boolean>() {
    		@Override
    		public void execute(final DataRequestMonitor<Boolean> rm) {
    			final IReverseRunControl runControl = fTracker.getService(IReverseRunControl.class);

    			if (runControl != null) {
    				runControl.isReverseModeEnabled(controlDmc, rm);
    			} else {
    				rm.setData(false);
    				rm.done();
    			}
    		}
    	};
    	try {
    		fExecutor.execute(isToggledQuery);
    		return isToggledQuery.get();
    	} catch (InterruptedException e) {
    	} catch (ExecutionException e) {
    	} catch (RejectedExecutionException e) {
    		// Can be thrown if the session is shutdown
    	}

    	return false;
    }
    
    /**
     * @noreference This method is not intended to be referenced by clients.
     */
    @DsfServiceEventHandler 
    public void eventDispatched(IReverseModeChangedDMEvent e) {
    	new WorkbenchJob("") { //$NON-NLS-1$
			@Override
			public IStatus runInUIThread(IProgressMonitor monitor) {
		        // Request re-evaluation of property "org.eclipse.cdt.debug.ui.isReverseDebuggingEnabled" to update 
			    // visibility of reverse stepping commands.
			    IEvaluationService exprService = PlatformUI.getWorkbench().getService(IEvaluationService.class);
			    if (exprService != null) {
			        exprService.requestEvaluation("org.eclipse.cdt.debug.ui.isReverseDebuggingEnabled"); //$NON-NLS-1$
			    }
			    // Refresh reverse toggle commands with the new state of reverse enabled. 
			    // This is in order to keep multiple toggle actions in UI in sync.
			    ICommandService commandService = PlatformUI.getWorkbench().getService(ICommandService.class);
		        if (commandService != null) {
		           commandService.refreshElements("org.eclipse.cdt.debug.ui.command.reverseToggle", null); //$NON-NLS-1$
		        }
		        
				return Status.OK_STATUS;
			}
		}.schedule();
    }
}

Back to the top