Skip to main content
summaryrefslogtreecommitdiffstats
blob: f40fa3b7bb67f06e2b9251dd3829698a3acad5ea (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
/*******************************************************************************
 * Copyright (c) 2009, 2012 Wind River Systems and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *     Ericsson                    - Added support for IRunToAddress for DSF DisassemblyView (302324)
 *******************************************************************************/
package org.eclipse.cdt.dsf.debug.internal.ui.actions;

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

import org.eclipse.cdt.core.IAddress;
import org.eclipse.cdt.debug.core.model.IRunToAddress;
import org.eclipse.cdt.debug.core.model.IRunToLine;
import org.eclipse.cdt.dsf.concurrent.DataRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.IDsfStatusConstants;
import org.eclipse.cdt.dsf.concurrent.Query;
import org.eclipse.cdt.dsf.debug.service.IRunControl.IExecutionDMContext;
import org.eclipse.cdt.dsf.debug.service.IRunControl2;
import org.eclipse.cdt.dsf.internal.ui.DsfUIPlugin;
import org.eclipse.cdt.dsf.service.DsfServicesTracker;
import org.eclipse.cdt.dsf.service.DsfSession;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.ui.actions.IRunToLineTarget;

/**
 * Implements the CDT's run to line interface.  This interface is called by CDT's
 * {@link IRunToLineTarget} implementation.
 *
 * @since 2.1
 */
public class RunToLine implements IRunToLine, IRunToAddress {

	private final IExecutionDMContext fContext;

	public RunToLine(IExecutionDMContext context) {
		fContext = context;
	}

	@Override
	public boolean canRunToLine(final IFile file, final int lineNumber) {
		return canRunToLine(file.getLocation().makeAbsolute().toOSString(), lineNumber);
	}

	@Override
	public boolean canRunToLine(final String fileName, final int lineNumber) {
		DsfSession session = DsfSession.getSession(fContext.getSessionId());
		if (session != null && session.isActive()) {
			try {
				Query<Boolean> query = new Query<Boolean>() {
					@Override
					protected void execute(DataRequestMonitor<Boolean> rm) {
						DsfServicesTracker tracker = new DsfServicesTracker(DsfUIPlugin.getBundleContext(),
								fContext.getSessionId());

						IRunControl2 runControl = tracker.getService(IRunControl2.class);
						if (runControl != null) {
							runControl.canRunToLine(fContext, fileName, lineNumber, rm);
						} else {
							rm.setData(false);
							rm.done();
						}
						tracker.dispose();
					}
				};
				session.getExecutor().execute(query);
				return query.get(IDsfActionsConstants.ACTION_ADAPTERS_TIMEOUT_MS, TimeUnit.MILLISECONDS);
			} catch (RejectedExecutionException e) {
			} catch (InterruptedException e) {
			} catch (ExecutionException e) {
			} catch (TimeoutException e) {
			}
		}
		return false;
	}

	@Override
	public void runToLine(IFile file, int lineNumber, boolean skipBreakpoints) throws DebugException {
		runToLine(file.getLocation().makeAbsolute().toOSString(), lineNumber, skipBreakpoints);
	}

	@Override
	public void runToLine(final String fileName, final int lineNumber, final boolean skipBreakpoints)
			throws DebugException {
		DsfSession session = DsfSession.getSession(fContext.getSessionId());
		if (session != null && session.isActive()) {
			Throwable exception = null;
			try {
				Query<Object> query = new Query<Object>() {
					@Override
					protected void execute(final DataRequestMonitor<Object> rm) {
						DsfServicesTracker tracker = new DsfServicesTracker(DsfUIPlugin.getBundleContext(),
								fContext.getSessionId());

						IRunControl2 runControl = tracker.getService(IRunControl2.class);
						if (runControl != null) {
							runControl.runToLine(fContext, fileName, lineNumber, skipBreakpoints, rm);
						} else {
							rm.setStatus(new Status(IStatus.ERROR, DsfUIPlugin.PLUGIN_ID,
									IDsfStatusConstants.NOT_SUPPORTED, "IRunControl2 service not available", null)); //$NON-NLS-1$
							rm.done();
						}
						tracker.dispose();
					}
				};
				session.getExecutor().execute(query);
				query.get();
			} catch (RejectedExecutionException e) {
				exception = e;
			} catch (InterruptedException e) {
				exception = e;
			} catch (ExecutionException e) {
				exception = e;
			}
			if (exception != null) {
				throw new DebugException(new Status(IStatus.ERROR, DsfUIPlugin.PLUGIN_ID, DebugException.REQUEST_FAILED,
						"Failed executing run to line", exception)); //$NON-NLS-1$
			}
		} else {
			throw new DebugException(new Status(IStatus.ERROR, DsfUIPlugin.PLUGIN_ID, DebugException.REQUEST_FAILED,
					"Debug session is not active", null)); //$NON-NLS-1$
		}
	}

	@Override
	public boolean canRunToAddress(final IAddress address) {
		DsfSession session = DsfSession.getSession(fContext.getSessionId());
		if (session != null && session.isActive()) {
			try {
				Query<Boolean> query = new Query<Boolean>() {
					@Override
					protected void execute(DataRequestMonitor<Boolean> rm) {
						DsfServicesTracker tracker = new DsfServicesTracker(DsfUIPlugin.getBundleContext(),
								fContext.getSessionId());

						IRunControl2 runControl = tracker.getService(IRunControl2.class);
						if (runControl != null) {
							runControl.canRunToAddress(fContext, address, rm);
						} else {
							rm.setData(false);
							rm.done();
						}
						tracker.dispose();
					}
				};
				session.getExecutor().execute(query);
				return query.get(IDsfActionsConstants.ACTION_ADAPTERS_TIMEOUT_MS, TimeUnit.MILLISECONDS);
			} catch (RejectedExecutionException e) {
			} catch (InterruptedException e) {
			} catch (ExecutionException e) {
			} catch (TimeoutException e) {
			}
		}
		return false;
	}

	@Override
	public void runToAddress(final IAddress address, final boolean skipBreakpoints) throws DebugException {
		DsfSession session = DsfSession.getSession(fContext.getSessionId());
		if (session != null && session.isActive()) {
			Throwable exception = null;
			try {
				Query<Object> query = new Query<Object>() {
					@Override
					protected void execute(final DataRequestMonitor<Object> rm) {
						DsfServicesTracker tracker = new DsfServicesTracker(DsfUIPlugin.getBundleContext(),
								fContext.getSessionId());

						IRunControl2 runControl = tracker.getService(IRunControl2.class);
						if (runControl != null) {
							runControl.runToAddress(fContext, address, skipBreakpoints, rm);
						} else {
							rm.setStatus(new Status(IStatus.ERROR, DsfUIPlugin.PLUGIN_ID,
									IDsfStatusConstants.NOT_SUPPORTED, "IRunControl2 service not available", null)); //$NON-NLS-1$
							rm.done();
						}
						tracker.dispose();
					}
				};
				session.getExecutor().execute(query);
				query.get();
			} catch (RejectedExecutionException e) {
				exception = e;
			} catch (InterruptedException e) {
				exception = e;
			} catch (ExecutionException e) {
				exception = e;
			}
			if (exception != null) {
				throw new DebugException(new Status(IStatus.ERROR, DsfUIPlugin.PLUGIN_ID, DebugException.REQUEST_FAILED,
						"Failed executing run to line", exception)); //$NON-NLS-1$
			}
		} else {
			throw new DebugException(new Status(IStatus.ERROR, DsfUIPlugin.PLUGIN_ID, DebugException.REQUEST_FAILED,
					"Debug session is not active", null)); //$NON-NLS-1$
		}
	}
}

Back to the top