Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 087f4d4bc9c612c30b9d3e4626bd640ea790a211 (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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*******************************************************************************
 * Copyright (c) 2011, 2016 Mentor Graphics 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:
 * Mentor Graphics - Initial API and implementation
 * Jason Litton (Sage Electronic Engineering, LLC) - Use Dynamic Tracing option (Bug 379169)
 *******************************************************************************/

package org.eclipse.cdt.dsf.gdb.service.command;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.eclipse.cdt.dsf.debug.service.command.ICommand;
import org.eclipse.cdt.dsf.debug.service.command.ICommandControl;
import org.eclipse.cdt.dsf.debug.service.command.ICommandListener;
import org.eclipse.cdt.dsf.debug.service.command.ICommandResult;
import org.eclipse.cdt.dsf.debug.service.command.ICommandToken;
import org.eclipse.cdt.dsf.gdb.IGdbDebugPreferenceConstants;
import org.eclipse.cdt.dsf.gdb.internal.GdbDebugOptions;
import org.eclipse.cdt.dsf.gdb.internal.GdbPlugin;
import org.eclipse.cdt.dsf.mi.service.command.AbstractMIControl;
import org.eclipse.cdt.dsf.mi.service.command.commands.MICommand;
import org.eclipse.cdt.dsf.mi.service.command.output.MIInfo;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.ListenerList;
import org.eclipse.core.runtime.Platform;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
import org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener;
import org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent;
import org.eclipse.core.runtime.preferences.InstanceScope;

/**
 * The command timeout manager registers itself as a command listener and monitors
 * the command execution time. The goal of this implementation is to gracefully
 * handle disruptions in the communication between Eclipse and GDB.
 *
 * The algorithm used by this class is based on the assumption that the command
 * execution in GDB is sequential even though DSF can send up to 3 commands at
 * a time to GDB (see {@link AbstractMIControl}).
 *
 * @since 4.1
 */
public class GdbCommandTimeoutManager implements ICommandListener, IPreferenceChangeListener {

	public interface ICommandTimeoutListener {

		void commandTimedOut(ICommandToken token);
	}

	/**
	 * @deprecated The DEBUG flag is replaced with the GdbDebugOptions.DEBUG_COMMAND_TIMEOUTS
	 */
	@Deprecated
	public final static boolean DEBUG = Boolean
			.parseBoolean(Platform.getDebugOption("org.eclipse.cdt.dsf.gdb/debug/timeouts")); //$NON-NLS-1$

	private class QueueEntry {
		private long fTimestamp;
		private ICommandToken fCommandToken;

		private QueueEntry(long timestamp, ICommandToken commandToken) {
			super();
			fTimestamp = timestamp;
			fCommandToken = commandToken;
		}

		/* (non-Javadoc)
		 * @see java.lang.Object#equals(java.lang.Object)
		 */
		@Override
		public boolean equals(Object obj) {
			if (obj instanceof QueueEntry) {
				return fCommandToken.equals(((QueueEntry) obj).fCommandToken);
			}
			return false;
		}
	}

	private enum TimerThreadState {
		INITIALIZING, RUNNING, HALTED, SHUTDOWN
	}

	private class TimerThread extends Thread {

		private BlockingQueue<QueueEntry> fQueue;
		private int fWaitTimeout = IGdbDebugPreferenceConstants.COMMAND_TIMEOUT_VALUE_DEFAULT;
		private TimerThreadState fState = TimerThreadState.INITIALIZING;

		TimerThread(BlockingQueue<QueueEntry> queue, int timeout) {
			super();
			setName("GDB Command Timer Thread"); //$NON-NLS-1$
			fQueue = queue;
			setWaitTimout(timeout);
		}

		/* (non-Javadoc)
		 * @see java.lang.Thread#run()
		 */
		@Override
		public void run() {
			setTimerThreadState((getWaitTimeout() > 0) ? TimerThreadState.RUNNING : TimerThreadState.HALTED);
			doRun();
		}

		private void doRun() {
			while (getTimerThreadState() != TimerThreadState.SHUTDOWN) {
				if (getTimerThreadState() == TimerThreadState.HALTED) {
					halted();
				} else {
					running();
				}
			}
		}

		private void halted() {
			fQueue.clear();
			try {
				synchronized (TimerThread.this) {
					wait();
				}
			} catch (InterruptedException e) {
			}
		}

		private void running() {
			try {
				while (getTimerThreadState() == TimerThreadState.RUNNING) {
					// Use the minimum of all timeout values > 0 as the wait timeout.
					long timeout = getWaitTimeout();
					QueueEntry entry = fQueue.peek();
					if (entry != null) {
						// Calculate the time elapsed since the execution of this command started
						// and compare it with the command's timeout value.
						// If the elapsed time is greater or equal than the timeout value the command
						// is marked as timed out. Otherwise, schedule the next check when the timeout
						// expires.
						long commandTimeout = getTimeoutForCommand(entry.fCommandToken.getCommand());

						if (GdbDebugOptions.DEBUG_COMMAND_TIMEOUTS) {
							String commandText = entry.fCommandToken.getCommand().toString();
							if (commandText.endsWith("\n")) //$NON-NLS-1$
								commandText = commandText.substring(0, commandText.length() - 1);

							printDebugMessage(String.format("Processing command '%s', command timeout is %d", //$NON-NLS-1$
									commandText, Long.valueOf(commandTimeout)));

						}

						long currentTime = System.currentTimeMillis();
						long elapsedTime = currentTime - entry.fTimestamp;
						if (commandTimeout <= elapsedTime) {
							processTimedOutCommand(entry.fCommandToken);
							fQueue.remove(entry);
							// Reset the timestamp of the next command in the queue because
							// regardless how long the command has been in the queue GDB will
							// start executing it only when the execution of the previous command
							// is completed.
							QueueEntry nextEntry = fQueue.peek();
							if (nextEntry != null) {
								setTimeStamp(currentTime, nextEntry);
							}
						} else {
							// Adjust the wait timeout because the time remaining for
							// the current command to expire may be less than the current wait timeout.
							timeout = Math.min(timeout, commandTimeout - elapsedTime);

							if (GdbDebugOptions.DEBUG_COMMAND_TIMEOUTS) {
								String commandText = entry.fCommandToken.getCommand().toString();
								if (commandText.endsWith("\n")) //$NON-NLS-1$
									commandText = commandText.substring(0, commandText.length() - 1);

								printDebugMessage(String.format("Setting timeout %d for command '%s'", //$NON-NLS-1$
										Long.valueOf(timeout), commandText));

							}
						}
					}
					synchronized (TimerThread.this) {
						wait(timeout);
					}
				}
			} catch (InterruptedException e) {
			}
		}

		private void shutdown() {
			setTimerThreadState(TimerThreadState.SHUTDOWN);
		}

		private synchronized void setWaitTimout(int waitTimeout) {
			fWaitTimeout = waitTimeout;
			if (GdbDebugOptions.DEBUG_COMMAND_TIMEOUTS)
				printDebugMessage(String.format("Wait timeout is set to %d", Integer.valueOf(fWaitTimeout))); //$NON-NLS-1$
		}

		private synchronized int getWaitTimeout() {
			return fWaitTimeout;
		}

		private synchronized void setTimerThreadState(TimerThreadState state) {
			fState = state;
			interrupt();
		}

		private synchronized TimerThreadState getTimerThreadState() {
			return fState;
		}
	}

	private static final String TIMEOUT_TRACE_IDENTIFIER = "[TMO]"; //$NON-NLS-1$

	private ICommandControl fCommandControl;
	private boolean fTimeoutEnabled = false;
	private int fTimeout = 0;
	private TimerThread fTimerThread;
	private BlockingQueue<QueueEntry> fCommandQueue = new LinkedBlockingQueue<>();
	private CustomTimeoutsMap fCustomTimeouts = new CustomTimeoutsMap();

	private ListenerList fListeners;

	public GdbCommandTimeoutManager(ICommandControl commandControl) {
		fCommandControl = commandControl;
		fListeners = new ListenerList();
	}

	public void initialize() {
		IEclipsePreferences node = InstanceScope.INSTANCE.getNode(GdbPlugin.PLUGIN_ID);

		fTimeoutEnabled = Platform.getPreferencesService().getBoolean(GdbPlugin.PLUGIN_ID,
				IGdbDebugPreferenceConstants.PREF_COMMAND_TIMEOUT, false, null);

		fTimeout = Platform.getPreferencesService().getInt(GdbPlugin.PLUGIN_ID,
				IGdbDebugPreferenceConstants.PREF_COMMAND_TIMEOUT_VALUE,
				IGdbDebugPreferenceConstants.COMMAND_TIMEOUT_VALUE_DEFAULT, null);

		fCustomTimeouts.initializeFromMemento(Platform.getPreferencesService().getString(GdbPlugin.PLUGIN_ID,
				IGdbDebugPreferenceConstants.PREF_COMMAND_CUSTOM_TIMEOUTS, "", //$NON-NLS-1$
				null));

		node.addPreferenceChangeListener(this);

		fCommandControl.addCommandListener(this);

		fTimerThread = new TimerThread(fCommandQueue, calculateWaitTimeout());
		fTimerThread.start();
	}

	public void dispose() {
		fTimerThread.shutdown();
		fListeners.clear();
		InstanceScope.INSTANCE.getNode(GdbPlugin.PLUGIN_ID).removePreferenceChangeListener(this);
		fCommandControl.removeCommandListener(this);
		fCommandQueue.clear();
		fCustomTimeouts.clear();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.dsf.debug.service.command.ICommandListener#commandQueued(org.eclipse.cdt.dsf.debug.service.command.ICommandToken)
	 */
	@Override
	public void commandQueued(ICommandToken token) {
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.dsf.debug.service.command.ICommandListener#commandSent(org.eclipse.cdt.dsf.debug.service.command.ICommandToken)
	 */
	@Override
	public void commandSent(ICommandToken token) {
		if (!isTimeoutEnabled())
			return;
		int commandTimeout = getTimeoutForCommand(token.getCommand());
		if (GdbDebugOptions.DEBUG_COMMAND_TIMEOUTS) {
			String commandText = token.getCommand().toString();
			if (commandText.endsWith("\n")) //$NON-NLS-1$
				commandText = commandText.substring(0, commandText.length() - 1);
			printDebugMessage(
					String.format("Command '%s' sent, timeout = %d", commandText, Integer.valueOf(commandTimeout))); //$NON-NLS-1$
		}
		if (commandTimeout == 0)
			// Skip commands with no timeout
			return;
		try {
			fCommandQueue.put(new QueueEntry(System.currentTimeMillis(), token));
		} catch (InterruptedException e) {
			// ignore
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.dsf.debug.service.command.ICommandListener#commandRemoved(org.eclipse.cdt.dsf.debug.service.command.ICommandToken)
	 */
	@Override
	public void commandRemoved(ICommandToken token) {
	}

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.dsf.debug.service.command.ICommandListener#commandDone(org.eclipse.cdt.dsf.debug.service.command.ICommandToken, org.eclipse.cdt.dsf.debug.service.command.ICommandResult)
	 */
	@Override
	public void commandDone(ICommandToken token, ICommandResult result) {
		if (!isTimeoutEnabled())
			return;
		fCommandQueue.remove(new QueueEntry(0, token));
		if (GdbDebugOptions.DEBUG_COMMAND_TIMEOUTS) {
			String commandText = token.getCommand().toString();
			if (commandText.endsWith("\n")) //$NON-NLS-1$
				commandText = commandText.substring(0, commandText.length() - 1);
			printDebugMessage(String.format("Command '%s' is done", commandText)); //$NON-NLS-1$
		}
		// Reset the timestamp of the next command in the queue because
		// regardless how long it has been in the queue GDB will start
		// executing it only when the execution of the previous command
		// is completed.
		QueueEntry nextEntry = fCommandQueue.peek();
		if (nextEntry != null) {
			setTimeStamp(System.currentTimeMillis(), nextEntry);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener#preferenceChange(org.eclipse.core.runtime.preferences.IEclipsePreferences.PreferenceChangeEvent)
	 */
	@Override
	public void preferenceChange(PreferenceChangeEvent event) {
		String property = event.getKey();
		if (IGdbDebugPreferenceConstants.PREF_COMMAND_TIMEOUT.equals(property)) {
			// The new value is null when the timeout support is disabled.
			if (event.getNewValue() == null || !event.getNewValue().equals(event.getOldValue())) {
				fTimeoutEnabled = (event.getNewValue() != null) ? Boolean.parseBoolean(event.getNewValue().toString())
						: Boolean.FALSE;
				updateWaitTimeout();
				fTimerThread.setTimerThreadState(
						(fTimerThread.getWaitTimeout() > 0) ? TimerThreadState.RUNNING : TimerThreadState.HALTED);
			}
		} else if (IGdbDebugPreferenceConstants.PREF_COMMAND_TIMEOUT_VALUE.equals(property)) {
			if (event.getNewValue() == null || !event.getNewValue().equals(event.getOldValue())) {
				try {
					fTimeout = (event.getNewValue() != null) ? Integer.parseInt(event.getNewValue().toString())
							: IGdbDebugPreferenceConstants.COMMAND_TIMEOUT_VALUE_DEFAULT;
					updateWaitTimeout();
					fTimerThread.setTimerThreadState(
							(fTimerThread.getWaitTimeout() > 0) ? TimerThreadState.RUNNING : TimerThreadState.HALTED);
				} catch (NumberFormatException e) {
					GdbPlugin.getDefault().getLog()
							.log(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, "Invalid timeout value")); //$NON-NLS-1$
				}
			}
		} else if (IGdbDebugPreferenceConstants.PREF_COMMAND_CUSTOM_TIMEOUTS.equals(property)) {
			if (event.getNewValue() instanceof String) {
				fCustomTimeouts.initializeFromMemento((String) event.getNewValue());
			} else if (event.getNewValue() == null) {
				fCustomTimeouts.clear();
			}
			updateWaitTimeout();
			fTimerThread.setTimerThreadState(
					(fTimerThread.getWaitTimeout() > 0) ? TimerThreadState.RUNNING : TimerThreadState.HALTED);
		}
	}

	protected int getTimeoutForCommand(ICommand<? extends ICommandResult> command) {
		if (!(command instanceof MICommand<?>))
			return 0;
		@SuppressWarnings("unchecked")
		Integer timeout = fCustomTimeouts.get(((MICommand<? extends MIInfo>) command).getOperation());
		return (timeout != null) ? timeout.intValue() : fTimeout;
	}

	protected void processTimedOutCommand(ICommandToken token) {
		if (GdbDebugOptions.DEBUG_COMMAND_TIMEOUTS) {
			String commandText = token.getCommand().toString();
			if (commandText.endsWith("\n")) //$NON-NLS-1$
				commandText = commandText.substring(0, commandText.length() - 1);
			printDebugMessage(String.format("Command '%s' is timed out", commandText)); //$NON-NLS-1$
		}
		for (Object l : fListeners.getListeners()) {
			((ICommandTimeoutListener) l).commandTimedOut(token);
		}
	}

	public void addCommandTimeoutListener(ICommandTimeoutListener listener) {
		fListeners.add(listener);
	}

	public void removeCommandTimeoutListener(ICommandTimeoutListener listener) {
		fListeners.remove(listener);
	}

	private void updateWaitTimeout() {
		fTimerThread.setWaitTimout(calculateWaitTimeout());
	}

	private boolean isTimeoutEnabled() {
		return fTimeoutEnabled;
	}

	private void printDebugMessage(String message) {
		if (GdbDebugOptions.DEBUG_COMMAND_TIMEOUTS) {
			GdbDebugOptions
					.trace(String.format("%s %s  %s\n", GdbPlugin.getDebugTime(), TIMEOUT_TRACE_IDENTIFIER, message)); //$NON-NLS-1$
		}

	}

	private int calculateWaitTimeout() {
		int waitTimeout = 0;
		if (isTimeoutEnabled()) {
			waitTimeout = fTimeout;
			int minCustomTimeout = Integer.MAX_VALUE;
			for (Integer t : fCustomTimeouts.values()) {
				if (t.intValue() > 0) {
					minCustomTimeout = Math.min(minCustomTimeout, t.intValue());
				}
			}
			if (minCustomTimeout > 0) {
				waitTimeout = (waitTimeout == 0) ? minCustomTimeout : Math.min(waitTimeout, minCustomTimeout);
			}
		}
		return waitTimeout;
	}

	private void setTimeStamp(long currentTime, QueueEntry nextEntry) {
		if (nextEntry != null) {
			nextEntry.fTimestamp = currentTime;

			if (GdbDebugOptions.DEBUG_COMMAND_TIMEOUTS) {
				String commandText = nextEntry.fCommandToken.getCommand().toString();
				if (commandText.endsWith("\n")) //$NON-NLS-1$
					commandText = commandText.substring(0, commandText.length() - 1);
				printDebugMessage(String.format("Setting the timestamp for command '%s' to %d", commandText, //$NON-NLS-1$
						Long.valueOf(currentTime)));
			}
		}
	}
}

Back to the top