summaryrefslogtreecommitdiffstats
blob: e263593699e19be0bb84e6145b252c9d750f09e0 (plain)
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/*******************************************************************************
 * Copyright (c) 2005 - 2007 committers of openArchitectureWare 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:
 *     committers of openArchitectureWare - initial API and implementation
 *******************************************************************************/
package org.eclipse.emf.mwe.internal.core.debug.processing;

import static org.eclipse.emf.mwe.core.debug.processing.EventHandler.END_FRAME;
import static org.eclipse.emf.mwe.core.debug.processing.EventHandler.NORMAL_FRAME;
import static org.eclipse.emf.mwe.internal.core.debug.processing.ProcessHandler.INTERRUPT;
import static org.eclipse.emf.mwe.internal.core.debug.processing.ProcessHandler.POP;
import static org.eclipse.emf.mwe.internal.core.debug.processing.ProcessHandler.PUSH;
import static org.eclipse.emf.mwe.internal.core.debug.processing.ProcessHandler.SHALL_HANDLE;
import static org.eclipse.emf.mwe.internal.core.debug.processing.ProcessHandler.SUSPEND;

import java.io.IOException;
import java.net.ConnectException;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.emf.common.util.Diagnostic;
import org.eclipse.emf.mwe.core.debug.processing.ElementAdapter;
import org.eclipse.emf.mwe.core.debug.processing.EventHandler;
import org.eclipse.emf.mwe.core.monitor.ProgressMonitor;
import org.eclipse.emf.mwe.internal.core.debug.communication.Connection;

/**
 * The heart of the debug process on the runtime side. It has callback methods that are called by the syntax element
 * implementations before and after a process step.<br>
 * It works closely together with handlers and adapters that must be registered at startup.
 */
public class DebugMonitor implements ProgressMonitor {

	private static final int STARTED = 1;

	private static final int PRE_TASK = 2;

	private static final int POST_TASK = 3;

	private static final int SUSPENDED = 4;

	private static final int RESUMED = 5;

	private static final int TERMINATED = 6;

	private final Connection connection = new Connection();

	private CommandListener commandListener;

	private final Set<ProcessHandler> processHandlers = new HashSet<ProcessHandler>();

	private final Set<EventHandler> eventHandlers = new HashSet<EventHandler>();

	private final Set<ElementAdapter> elementAdapters = new HashSet<ElementAdapter>();

	private boolean missingAdapterReported;

	private Object context;

	// -------------------------------------------------------------------------

	/**
	 * open the connection to a debug server framework (e.g. eclipse) and instantiate and start the
	 * RuntimeHandlerManager listener.
	 * 
	 * @param args
	 *            arg[1] must be the port to be connected with
	 * @throws IOException
	 */
	public void init(final String[] args) throws IOException {
		// args[0] is the class name
		final int port = findPort(args);
		try {
			connection.connect(port);
		}
		catch (final ConnectException e) {
			throw new IOException("Couldn't establish connection to Debugger on port " + port);
		}
		try {
			final RuntimeHandlerManager handler = new RuntimeHandlerManager(this);
			handler.setConnection(connection);
			handler.startListener();
		}
		catch (final Exception e) {
			connection.close();
			if (e instanceof RuntimeException) {
				throw (RuntimeException) e;
			}
			throw (IOException) e;
		}
	}

	private int findPort(final String[] args) {
		for (final String string : args) {
			if (string.startsWith("port=")) {
				return Integer.parseInt(string.substring(5));
			}
		}
		return 0;
	}

	// -------------------------------------------------------------------------

	public void setCommandListener(final CommandListener commandListener) {
		this.commandListener = commandListener;
	}

	public void addProcessHandler(final ProcessHandler handler) {
		processHandlers.add(handler);
	}

	public void addEventHandler(final EventHandler handler) {
		eventHandlers.add(handler);
	}

	// -------------------------------------------------------------------------

	public void addAdapter(final ElementAdapter adapter) {
		elementAdapters.add(adapter);
	}

	public ElementAdapter getAdapter(final Object element) {
		for (final ElementAdapter adapter : elementAdapters) {
			if (adapter.canHandle(element)) {
				return adapter;
			}
		}
		if (!missingAdapterReported) {
			System.out.println("Warning: Element can't be debugged.\nDidn't find an adapter for element of type "
					+ element.getClass().getSimpleName());
			missingAdapterReported = true;
		}
		return null;
	}

	// ------------------------------------- methods called from the mwe process

	/**
	 * fire the STARTED event to the registered event handlers
	 * 
	 * @see org.eclipse.emf.mwe.core.monitor.ProgressMonitor#started(java.lang.Object, java.lang.Object)
	 */
	public void started(final Object element, final Object context) {
		fireEvent(STARTED);
	}

	/**
	 * the main method to manipulate the runtime process for debugging.<br>
	 * In case a suspension is requested it stops the process and waits for the next user command.
	 * 
	 * @see org.eclipse.emf.mwe.core.monitor.ProgressMonitor#preTask(java.lang.Object, java.lang.Object)
	 */
	public void preTask(final Object element, final Object context) {
		this.context = context;
		final ElementAdapter adapter = getAdapter(element);
		if (adapter == null) {
			return;
		}
		adapter.setContext(context);

		if (!ask(SHALL_HANDLE, element, PUSH)) {
			return;
		}

		fireEvent(PRE_TASK, element, NORMAL_FRAME);
		checkInterrupt();

		try {
			if (ask(SUSPEND, element, NORMAL_FRAME)) {
				fireEvent(SUSPENDED);
				commandListener.listenCommand();
				checkInterrupt();
				fireEvent(RESUMED);
			}
		}
		catch (final IOException e) {
			throw new DebuggerInterruptedException("User interrupt");
		}
	}

	/**
	 * inform the handlers about the finalization of a process step.<br>
	 * In case the process may suspend at the end of a syntax element it does so if requested.
	 * 
	 * @see org.eclipse.emf.mwe.core.monitor.ProgressMonitor#postTask(java.lang.Object, java.lang.Object)
	 */
	public void postTask(final Object element, final Object context) {
		this.context = context;
		final ElementAdapter adapter = getAdapter(element);
		if (adapter == null) {
			return;
		}
		adapter.setContext(context);

		if (!ask(SHALL_HANDLE, element, POP)) {
			return;
		}

		try {
			if (adapter.isSurroundingElement(element)) {
				if (ask(SUSPEND, element, END_FRAME)) {
					// if we are at the end of a "subroutine" and going to stop at the next lower level task
					// anyway
					// we add another "frame" here to stop again at the closing syntax element
					// this enables us to check variable values before closing the frame
					fireEvent(PRE_TASK, element, END_FRAME);
					fireEvent(SUSPENDED);
					commandListener.listenCommand();
					fireEvent(RESUMED);
					fireEvent(POST_TASK);
				}
			}
			fireEvent(POST_TASK);
		}
		catch (final IOException e) {
			throw new DebuggerInterruptedException(e);
		}
	}

	/**
	 * fire the finish events to the registered event handlers
	 * 
	 * @see org.eclipse.emf.mwe.core.monitor.ProgressMonitor#finished(java.lang.Object, java.lang.Object)
	 */
	public void finished(final Object element, final Object context) {
		getAdapter(element).setContext(context);
		fireEvent(PRE_TASK, element, END_FRAME);
		fireEvent(SUSPENDED);
		fireEvent(POST_TASK);
		fireEvent(TERMINATED);
	}

	// -------------------------------------------------------------------------

	private void checkInterrupt() {
		if (ask(INTERRUPT, null, 0)) {
			// we throw an exception since the preTask() method is called
			// iteratively and we want to come back to the most outer loop
			throw new DebuggerInterruptedException("User interrupt");
		}
	}

	// -------------------------------------------------------------------------

	private void fireEvent(final int event) {
		fireEvent(event, null, 0);
	}

	private void fireEvent(final int event, final Object element, final int state) {
		for (final EventHandler handler : eventHandlers) {
			try {
				if (event == PRE_TASK) {
					handler.preTask(element, context, state);
				}
				else if (event == POST_TASK) {
					handler.postTask(context);
				}
				else if (event == STARTED) {
					handler.started();
				}
				else if (event == SUSPENDED) {
					handler.suspended();
				}
				else if (event == RESUMED) {
					handler.resumed();
				}
				else if (event == TERMINATED) {
					handler.terminated();
				}
			}
			catch (final IOException e) {
				throw new DebuggerInterruptedException(e);
			}
		}
	}

	private boolean ask(final int event, final Object element, final int state) {
		boolean result = false;
		ProcessHandler lastHandler = null;

		for (final ProcessHandler handler : processHandlers) {
			if (handler.isLastCall()) {
				lastHandler = handler;
			}
			else {
				result = ask(event, element, state, result, handler);
			}
		}
		if (lastHandler != null) {
			result = ask(event, element, state, result, lastHandler);
		}
		return result;
	}

	private boolean ask(final int event, final Object element, final int state, final boolean lastState,
			final ProcessHandler handler) {
		boolean result = lastState;
		if (event == SHALL_HANDLE) {
			result = handler.shallHandle(result, element, state);
		}
		else if (event == SUSPEND) {
			result = handler.shallSuspend(result, element, state);
		}
		else if (event == INTERRUPT) {
			result = handler.shallInterrupt(result);
		}
		return result;
	}

	// ************************** unused Progress Monitor implementation methods

	public void beginTask(final String name, final int totalWork) {
		//
	}

	public void done() {
		//
	}

	public void internalWorked(final double work) {
		//
	}

	public boolean isCanceled() {
		return false;
	}

	public void setCanceled(final boolean value) {
		//
	}

	public void setTaskName(final String name) {
		//
	}

	public void subTask(final String name) {
		//
	}

	public void worked(final int work) {
		//
	}

	public void clearBlocked() {
		//
	}

	public void setBlocked(@SuppressWarnings("unused") final Diagnostic reason) {
		//
	}

}