Skip to main content
summaryrefslogtreecommitdiffstats
blob: 29428833d5c0cbacf7b25126de4c460ae5b2ed76 (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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
/*****************************************************************************
 * Copyright (c) 2013 CEA LIST.
 *
 *    
 * 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:
 *  CEA LIST - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.moka.debug;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import org.eclipse.core.resources.IMarkerDelta;
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.core.runtime.jobs.Job;
import org.eclipse.debug.core.DebugEvent;
import org.eclipse.debug.core.DebugException;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchManager;
import org.eclipse.debug.core.model.IBreakpoint;
import org.eclipse.debug.core.model.IDebugElement;
import org.eclipse.debug.core.model.IDebugTarget;
import org.eclipse.debug.core.model.IMemoryBlock;
import org.eclipse.debug.core.model.IProcess;
import org.eclipse.debug.core.model.IRegisterGroup;
import org.eclipse.debug.core.model.IStackFrame;
import org.eclipse.debug.core.model.IThread;
import org.eclipse.debug.core.model.IValue;
import org.eclipse.papyrus.infra.core.Activator;
import org.eclipse.papyrus.moka.MokaConstants;
import org.eclipse.papyrus.moka.communication.Marshaller;
import org.eclipse.papyrus.moka.communication.event.Start_Event;
import org.eclipse.papyrus.moka.communication.event.isuspendresume.Resume_Event;
import org.eclipse.papyrus.moka.communication.event.isuspendresume.Suspend_Event;
import org.eclipse.papyrus.moka.communication.event.iterminate.Terminate_Event;
import org.eclipse.papyrus.moka.communication.request.RequestMessage;
import org.eclipse.papyrus.moka.communication.request.ibreakpointlistener.AddBreakpoint_Request;
import org.eclipse.papyrus.moka.communication.request.ibreakpointlistener.RemoveBreakpoint_Request;
import org.eclipse.papyrus.moka.communication.request.idisconnect.Disconnect_Request;
import org.eclipse.papyrus.moka.communication.request.istackframe.GetVariables_Request;
import org.eclipse.papyrus.moka.communication.request.isuspendresume.Resume_Request;
import org.eclipse.papyrus.moka.communication.request.isuspendresume.Suspend_Request;
import org.eclipse.papyrus.moka.communication.request.iterminate.Terminate_Request;
import org.eclipse.papyrus.moka.communication.request.ithread.GetStackFrames_Request;
import org.eclipse.papyrus.moka.communication.request.ivalue.GetValueString_Request;
import org.eclipse.papyrus.moka.communication.request.ivariable.GetReferenceTypeName_Request;
import org.eclipse.papyrus.moka.communication.request.ivariable.GetValue_Request;

/**
 * An implementation of IDebugTarget.
 * More specifically, it implements a communication protocol which complies with AbstractExecutionEngine.
 * Communication rely on sockets. The EventDispatchJob associated with this MokaDebugTarget listens to events 
 * emitted by the target program (i.e., through the eventSocket), and fires corresponding debug events to 
 * the Debug plugin.
 * The requestSocket is used to emit requests to the execution engine.
 * The replySocket is used to receive replies from the execution engine.
 *
 */
public class MokaDebugTarget extends MokaDebugElement implements IDebugTarget {

	/**
	 * The launch object containing this debug target
	 */
	protected ILaunch launch;

	// sockets to communicate with Execution engine
	protected Socket requestSocket;
	protected PrintWriter requestWriter;
	protected Socket replySocket;
	protected BufferedReader replyReader;
	protected Socket eventSocket;
	protected BufferedReader eventReader;

	// event dispatch job
	protected EventDispatchJob eventDispatchJob;

	protected boolean isStarted = false ;

	/**
	 * Constructs a new moka debug target in the given launch for the associated Execution Engine process.
	 * 
	 * @param launch containing launch
	 * @param process The execution engine process
	 * @param requestPort port to send requests to the process
	 * @param eventPort port to read events from
	 * @exception CoreException if unable to connect to host
	 */
	public MokaDebugTarget(ILaunch launch, IProcess process) throws CoreException {
		super(null);
		this.launch = launch;
		this.debugTarget = this;
		this.executionEngineProcess = process ;
		DebugPlugin.getDefault().getBreakpointManager().addBreakpointListener(this);
	}

	/**
	 * Connects this debug target to the execution engine, and starts the event dispatch job.
	 * 
	 * @param requestPort The port for the request socket
	 * @param replyPort The port for the reply socket
	 * @param eventPort The port for the event socket
	 * @throws DebugException
	 */
	public void connect(int requestPort, int replyPort, int eventPort) throws DebugException {
		try {
			this.requestSocket = new Socket("localhost", requestPort);
			this.requestWriter = new PrintWriter(this.requestSocket.getOutputStream());
			this.replySocket = new Socket("localhost", replyPort);
			this.replyReader = new BufferedReader(new InputStreamReader(this.replySocket.getInputStream()));
			this.eventSocket = new Socket("localhost", eventPort);
			this.eventReader = new BufferedReader(new InputStreamReader(this.eventSocket.getInputStream()));
		} catch (UnknownHostException e) {
			abort("Unable to connect to Execution Engine", e);
		} catch (IOException e) {
			abort("Unable to connect to Execution Engine", e);
		}
		this.eventDispatchJob = new EventDispatchJob();
		this.eventDispatchJob.schedule();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.papyrus.moka.debug.MokaDebugElement#getLaunch()
	 */
	public ILaunch getLaunch() {
		return this.launch ;
	}

	///////////////////////////////////////////
	// Implementation of interface IDebugTarget
	///////////////////////////////////////////

	/**
	 * The name of this debug target.
	 */
	protected String name = null ;

	/**
	 * The threads for this debug target. 
	 * This collection of threads is initialized with the first Start_Event received in the event dispatch loop
	 * This collection of threads is updated with reception of a Suspend_Event in the event dispatch loop
	 *  
	 */
	protected MokaThread[] threads = new MokaThread[]{} ;

	/** 
	 * The process associated with this debug target. Hosts the target program, i.e., the actual execution engine
	 */
	protected IProcess executionEngineProcess;

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.IDebugTarget#getProcess()
	 */
	public IProcess getProcess() {
		return this.executionEngineProcess;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.IDebugTarget#getThreads()
	 */
	public IThread[] getThreads() throws DebugException {
		return threads ;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.IDebugTarget#hasThreads()
	 */
	public boolean hasThreads() throws DebugException {
		return this.isStarted && !this.isTerminated() && this.getThreads() != null && this.getThreads().length > 0 ;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.IDebugTarget#getName()
	 */
	public String getName() throws DebugException {
		if (this.name != null) {
			return this.name ;
		}
		return "" ;
	}

	/**
	 * Convenience method for setting the new name for this MokaDebugTarget
	 * 
	 * @param name
	 */
	public void setName(String name) {
		this.name = name ;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.IDebugTarget#supportsBreakpoint(org.eclipse.debug.core.model.IBreakpoint)
	 */
	public boolean supportsBreakpoint(IBreakpoint breakpoint) {
		// By default, a breakpoint is supported if it has MokaConstants.ID_MOKA_DEBUG_MODEL as a debug model identifier 
		if (breakpoint.getModelIdentifier().equals(MokaConstants.MOKA_DEBUG_MODEL_ID)) {			
			return true ;
		}
		return false;
	}

	//////////////////////////////////////////////////
	// Implementation of interface IBreakpointListener
	//////////////////////////////////////////////////

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.IBreakpointListener#breakpointAdded(org.eclipse.debug.core.model.IBreakpoint)
	 */
	public void breakpointAdded(IBreakpoint breakpoint) {
		if (this.supportsBreakpoint(breakpoint)) {
			try {
				if (breakpoint.isEnabled()) {
					try {
						// If the breakpoint is supported, this is a MokaBreakpoint
						MokaBreakpoint mokaBreakpoint = (MokaBreakpoint)breakpoint ;
						AddBreakpoint_Request request = new AddBreakpoint_Request(mokaBreakpoint) ;
						this.sendRequest(request) ;
						// Waits for an acknowledge from the execution engine
						@SuppressWarnings("unused")
						String ack = replyReader.readLine() ;
					} catch (IOException e) {
						Activator.log.error(e);
					}
				}
			} catch (CoreException e) {
				Activator.log.error(e) ;
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.IBreakpointListener#breakpointRemoved(org.eclipse.debug.core.model.IBreakpoint, org.eclipse.core.resources.IMarkerDelta)
	 */
	public void breakpointRemoved(IBreakpoint breakpoint, IMarkerDelta delta) {
		if (supportsBreakpoint(breakpoint)) {
			try {
				// If the breakpoint is supported, this is a MokaBreakpoint
				MokaBreakpoint mokaBreakpoint = (MokaBreakpoint)breakpoint ;
				RemoveBreakpoint_Request request = new RemoveBreakpoint_Request(mokaBreakpoint) ;
				this.sendRequest(request) ;
				// Waits for an acknowledge from the execution engine
				@SuppressWarnings("unused")
				String ack = replyReader.readLine() ;
			} catch (CoreException e) {
				Activator.log.error(e) ;
			} catch (IOException e) {
				Activator.log.error(e);
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.IBreakpointListener#breakpointChanged(org.eclipse.debug.core.model.IBreakpoint, org.eclipse.core.resources.IMarkerDelta)
	 */
	public void breakpointChanged(IBreakpoint breakpoint, IMarkerDelta delta) {
		if (this.supportsBreakpoint(breakpoint)) {
			try {
				if (breakpoint.isEnabled() && DebugPlugin.getDefault().getBreakpointManager().isEnabled()) {
					this.breakpointAdded(breakpoint);
				} else {
					this.breakpointRemoved(breakpoint, delta);
				}
			} catch (CoreException e) {
				Activator.log.error(e) ;
			}
		}
	}

	//////////////////////////////////////////
	// Implementation of interface IDisconnect
	//////////////////////////////////////////

	/**
	 * Determines whether the target program is disconnected or not from this debug target.
	 */
	protected boolean isDisconnected = false ;

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.IDisconnect#canDisconnect()
	 */
	public boolean canDisconnect() {
		// In this default implementation, disconnection can never occur. This can be overloaded by clients.
		return false;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.IDisconnect#disconnect()
	 */
	public void disconnect() throws DebugException {
		try {
			Disconnect_Request request = new Disconnect_Request() ;
			this.sendRequest(request) ;
			// Waits for an acknowledge from the execution engine
			@SuppressWarnings("unused")
			String ack = replyReader.readLine() ;
			this.isDisconnected = true ;
		} catch (CoreException e) {
			Activator.log.error(e) ;
		} catch (IOException e) {
			Activator.log.error(e);
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.IDisconnect#isDisconnected()
	 */
	public boolean isDisconnected() {
		return this.isDisconnected ;
	}

	////////////////////////////////////////////////////
	// Implementation of interface IMemoryBlockRetrieval
	////////////////////////////////////////////////////

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.IMemoryBlockRetrieval#supportsStorageRetrieval()
	 */
	public boolean supportsStorageRetrieval() {
		// By default, this default implementation does not support memory block retrieval. This can be overloaded by clients
		return false;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.IMemoryBlockRetrieval#getMemoryBlock(long, long)
	 */
	public IMemoryBlock getMemoryBlock(long startAddress, long length) throws DebugException {
		return null ;
	}

	///////////////////////////////////
	// Implementation of ISuspendResume
	///////////////////////////////////

	/**
	 * Determines whether this element is currently suspended.
	 */
	protected boolean isSuspended = false ;

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.ISuspendResume#canResume()
	 */
	public boolean canResume() {
		// The debug target can be resumed if it is currently suspended
		return this.isSuspended ;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.ISuspendResume#canSuspend()
	 */
	public boolean canSuspend() {
		// The debug target can be suspended if it is not already suspended and if it is not terminated
		return this.launch.getLaunchMode().equals(ILaunchManager.DEBUG_MODE) && !this.isSuspended && !this.isTerminated() ;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.ISuspendResume#isSuspended()
	 */
	public boolean isSuspended() {
		return this.isSuspended;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.ISuspendResume#resume()
	 */
	public void resume() throws DebugException {
		// A call to resume() has no effect on an element that is not suspended.
		//		if (this.isSuspended)
		this.resume(this, DebugEvent.CLIENT_REQUEST) ;
	}

	/**
	 * Handles occurrence of a resume event. The event is represented by the String message, and has been emitted by the target program.
	 * This method is called by the event dispatch job. 
	 * 
	 * @param message The String representation of the event
	 */
	protected void handle_Resume_Event(String message) {
		Resume_Event event = Marshaller.getInstance().resume_event_unmarshal(message) ;
		this.isSuspended = false ;
		this.fireEvent(event.getDebugEvent()) ;
	}

	/**
	 * Sends a request to the target program for resuming the given debugElement (which can be an IDebugTarget or an IThread) for the reason hold by resumeDetail.
	 * resumeDetail can be one of the following values: 
	 * - DebugEvent.CLIENT_REQUEST: Execution is resumed due to a resume client request
	 * - DebugEvent.STEP_INTO: Execution is resumed due to a step into client request 
	 * - DebugEvent.STEP_OVER: Execution is resumed due to a step over client request
	 * - DebugEvent.STEP_RETURN: Execution is resumed due to a step return client request
	 * In reply to this request, the target program will emit a Resume_Event. 
	 * This Resume_Event is handled by the event dispatch job, so that the appropriate RESUME DebugEvent is emitted to the DebugPlugin.
	 * 
	 * @param debugElement The debug element being resumed
	 * @param resumeDetail Details about the reason for resuming
	 */
	public void resume(IDebugElement debugElement, int resumeDetail) throws DebugException{
		Resume_Request request = new Resume_Request(debugElement, resumeDetail) ;
		try {
			this.sendRequest(request) ;
		} catch (DebugException e) {
			this.abort("Failed to resume execution of the given debug element", e) ;
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.ISuspendResume#suspend()
	 */
	public void suspend() throws DebugException {
		// A call to suspend() has no effect on an element that is already suspended.
		if (!this.isSuspended)
			this.suspend(this, DebugEvent.CLIENT_REQUEST) ;
	}

	/**
	 * Sends a request to the target program for suspending the given debugElement (which can be an IDebugTarget or an IThread) for the reason hold by suspendDetail.
	 * resumeDetail can be one of the following values: 
	 * - DebugEvent.CLIENT_REQUEST: Execution was suspended due to a suspend client request
	 * - DebugEvent.STEP_END: Execution was suspended due to the completion of a step action
	 * - DebugEvent.BREAKPOINT: Execution was suspended due to a breakpoint
	 * In reply to this request, the target program will emit a Suspend_Event. 
	 * This Suspend_Event is handled by the event dispatch job, so that the appropriate SUSPEND DebugEvent is emitted to the DebugPlugin.
	 * 
	 * @param debugElement The debug element being resumed
	 * @param suspendDetail Details about the reason for resuming
	 */
	public void suspend(IDebugElement debugElement, int suspendDetail) throws DebugException {
		Suspend_Request request = new Suspend_Request(debugElement, suspendDetail) ;
		try {
			this.sendRequest(request) ;
		} catch (DebugException e) {
			this.abort("Failed to suspend execution of the given debug element", e) ;
		}
	}

	/**
	 * Handles occurrence of a suspend event. The event is represented by the String message, and has been emitted by the target program.
	 * This method is called by the event dispatch job. 
	 * 
	 * @param message The String representation of the event
	 */
	protected void handle_Suspend_Event(String message) {
		Suspend_Event event = Marshaller.getInstance().suspend_event_unmarshal(message) ;
		this.isSuspended = true ;
		this.threads = event.getThreads() ;
		int suspendEventDetail = event.getDebugEvent().getDetail() ;
		// In the case where the suspend event occured due to a step end, the corresponding IThread must be explicitly stated that it is not stepping anymore.
		// @see MokaThrea.stepEnded()
		if (suspendEventDetail == DebugEvent.STEP_END || suspendEventDetail == DebugEvent.BREAKPOINT) {
			// By construction, the source element associated with this debug event is a MokaThread
			MokaThread thread = (MokaThread)event.getDebugEvent().getSource() ;
			thread.stepEnded() ;
		}
		this.fireEvent(event.getDebugEvent()) ;
	}

	///////////////////////////////
	// Implementation of ITerminate
	///////////////////////////////

	/**
	 *  Determines whether this element is terminated
	 */
	protected boolean isTerminated = false;

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.ITerminate#canTerminate()
	 */
	public boolean canTerminate() {
		// An element can terminate if it is not already terminated
		return ! this.isTerminated ;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.ITerminate#isTerminated()
	 */
	public boolean isTerminated() {
		return this.isTerminated ;
	}

	/**
	 * Terminates execution by firing a terminate event for the debug plugin
	 */
	protected void terminated() {
		this.isTerminated = true;
		this.isSuspended = false ;
		DebugPlugin.getDefault().getBreakpointManager().removeBreakpointListener(this) ;
		this.fireEvent(new Terminate_Event(this).getDebugEvent()) ;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.debug.core.model.ITerminate#terminate()
	 */
	public void terminate() throws DebugException {
		this.terminate(this) ;
	}

	/**
	 * Sends a request to the target program for terminating the given debugElement (which can be an IDebugTarget or an IThread).
	 * In reply to this request, the target program will emit a Terminate_Event. 
	 * This Terminate_Event is handled by the event dispatch job, so that the appropriate TERMINATE DebugEvent is emitted to the DebugPlugin.
	 * 
	 * @param debugElement The debug element being terminated
	 */
	public void terminate(IDebugElement debugElement) throws DebugException {
		Terminate_Request request = new Terminate_Request(debugElement) ;
		try {
			this.sendRequest(request) ;
		} catch (DebugException e) {
			this.abort("Failed to terminate execution of the given debug element", e) ;
		}
	}

	/**
	 * Handles occurrence of a terminate event, emitted by the target program.
	 * This method is called by the event dispatch job. 
	 * 
	 */
	protected void handle_Terminate_Event() {
		this.isTerminated = true ;
	}

	/**
	 * Listens to events from the Execution engine and fires corresponding debug events to the debug plugin
	 */
	class EventDispatchJob extends Job {

		public EventDispatchJob() {
			super("Moka Event Dispatch");
			setSystem(true);
		}

		/* (non-Javadoc)
		 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
		 */
		protected IStatus run(IProgressMonitor monitor) {
			String event = "";

			while (!isTerminated() && event != null) {
				try {
					event = eventReader.readLine();
					if (event != null) {
						isSuspended = false ;
						if (event.startsWith(MokaConstants.event_start)) {
							handle_Start_Event(event);
						}
						else if (event.startsWith(MokaConstants.event_resume)) {
							handle_Resume_Event(event);
						}
						else if (event.startsWith(MokaConstants.event_suspend)) {
							handle_Suspend_Event(event);
						}
						else if (event.startsWith(MokaConstants.event_terminate)) {
							handle_Terminate_Event() ;
						}
					}
				} catch (IOException e) {
					terminated();
				}
			}
			terminated() ;
			return Status.OK_STATUS;
		}
	}

	//////////////////////
	// Convenience methods
	//////////////////////

	/**
	 * Handles occurrence of a start event, which means that the target program is ready to receive requests.
	 * This method is called by the event dispatch job. 
	 * 
	 */
	protected void handle_Start_Event(String message) {
		Start_Event event = Marshaller.getInstance().start_event_unmarshal(message) ;
		this.threads = event.getThreads() ;
		this.fireEvent(event.getDebugEvent());
		// Installs deferred breakpoints only if the breakpoint manager is enabled
		//if (DebugPlugin.getDefault().getBreakpointManager().isEnabled())
		this.installDeferredBreakpoints();
		try {
			resume();
			this.isStarted = true ;
		} catch (DebugException e) {
			Activator.log.error(e) ;
		}
	}

	/**
	 * Install breakpoints that are already registered with the breakpoint manager
	 */
	protected void installDeferredBreakpoints() {
		IBreakpoint[] breakpoints = DebugPlugin.getDefault().getBreakpointManager().getBreakpoints(MokaConstants.MOKA_DEBUG_MODEL_ID) ;
		for (int i = 0; i < breakpoints.length; i++) {
			this.breakpointAdded(breakpoints[i]);
		}
	}

	/**
	 * Convenience methods for marshaling and sending a request to the target program, through the request socket
	 * 
	 * @param request The Request object to be marshaled and sent to the target program
	 * @throws DebugException
	 */
	protected void sendRequest(RequestMessage request) throws DebugException {
		synchronized (requestSocket) {
			requestWriter.println(request.marshall());
			requestWriter.flush();
		}
	}

	/**
	 * Emits a request to the target program to retrieve stack frames associated with a thread.
	 * The returned array is empty (but not null) if no stack frames are available, or if the thread is not suspended.
	 * This method is provided as a convenience for MokaThread, so that all requests (respectively replies) are emitted (respectively received) from (by) the debug target 
	 * 
	 * @param thread The thread for which stack frames have to be retrieved
	 * @return The (possibly empty) collection of stack frames associated with the given thread
	 * @throws DebugException
	 */
	public IStackFrame[] getStackFrames(IThread thread) throws DebugException {
		IStackFrame[] stackFrames = new IStackFrame[]{} ; 
		if (!thread.isSuspended())
			return stackFrames ;
		this.sendRequest(new GetStackFrames_Request(thread)) ;
		try {
			String reply = replyReader.readLine() ;
			stackFrames = Marshaller.getInstance().getStackFrames_reply_unmarshal(reply).getStackFrames() ;
		} catch (IOException e) {
			this.abort("Failed to retrieve stack frames", e) ;
		}
		return stackFrames ;
	}

	/**
	 * Emits a request to the target program to retrieve variables associated with a stack frame or value.
	 * The returned array is empty (but not null) if no variables are visible.
	 * This method is provided as a convenience for MokaStackFrame and MokaValue, so that all requests (respectively replies) are emitted (respectively received) from (by) the debug target
	 * stackFrameOrValue must be an IStackFrame or an IValue 
	 * 
	 * @param stackFrameOrValue The stack frame or value for which variables have to be retrieved
	 * @return The (possibly empty) collection of variables associated with this stack frame or value
	 * @throws DebugException
	 */
	public MokaVariable[] getVariables(IDebugElement stackFrameOrValue) throws DebugException {
		MokaVariable[] variables = new MokaVariable[]{} ; 
		this.sendRequest(new GetVariables_Request(stackFrameOrValue)) ;
		try {
			String reply = replyReader.readLine() ;
			variables = Marshaller.getInstance().getVariables_reply_unmarshal(reply).getVariables() ;
		} catch (IOException e) {
			this.abort("Failed to retrieve variables", e) ;
		}
		return variables ;
	}

	/**
	 * Not supported by this default implementation 
	 * 
	 * @param stackFrame The stack frame for which register groups have to be retrieved
	 * @return The (possibly empty) collection of register groups associated with this stack frame
	 * @throws DebugException
	 */
	public IRegisterGroup[] getRegisterGroups(IStackFrame stackFrame) throws DebugException {
		return new IRegisterGroup[]{} ;
	}

	/**
	 * Emits a request to the target program to retrieve the value associated with a variable.
	 * The returned value may be null.
	 * This method is provided as a convenience for MokaVariable, so that all requests (respectively replies) are emitted (respectively received) from (by) the debug target 
	 * 
	 * @param variable The variable for which value has to be retrieved
	 * @return The value of this variable, if any
	 * @throws DebugException
	 */
	public MokaValue getValue(MokaVariable variable) throws DebugException {
		MokaValue value = null ; 
		this.sendRequest(new GetValue_Request(variable)) ;
		try {
			String reply = replyReader.readLine() ;
			value = Marshaller.getInstance().getValue_reply_unmarshal(reply).getValue() ;
		} catch (IOException e) {
			this.abort("Failed to retrieve variable value", e) ;
		}
		return value ;
	}

	/**
	 * Emits a request to the target program to retrieve the reference type name of a variable or value.
	 * The returned value may be null.
	 * This method is provided as a convenience for MokaVariable, so that all requests (respectively replies) are emitted (respectively received) from (by) the debug target 
	 * 
	 * @param debugElement The variable or value for which the reference type name has to be retrieved
	 * @return The reference type name of this variable or value, if any
	 * @throws DebugException
	 */
	public String getReferenceTypeName(IDebugElement debugElement) throws DebugException {
		String referenceTypeName = null ; 
		this.sendRequest(new GetReferenceTypeName_Request(debugElement)) ;
		try {
			String reply = replyReader.readLine() ;
			referenceTypeName = Marshaller.getInstance().getReferenceTypeName_reply_unmarshal(reply).getReferenceTypeName() ;
		} catch (IOException e) {
			this.abort("Failed to retrieve variable value", e) ;
		}
		return referenceTypeName ;
	}

	/**
	 * Emits a request to the target program to retrieve the string representation of a value.
	 * The returned value may be null.
	 * This method is provided as a convenience for MokaVariable, so that all requests (respectively replies) are emitted (respectively received) from (by) the debug target 
	 * 
	 * @param value The value for which the string representation has to be retrieved
	 * @return The string representation of this value
	 * @throws DebugException
	 */
	public String getValueString(IValue value) throws DebugException {
		String textualRepresentation = null ; 
		this.sendRequest(new GetValueString_Request(value)) ;
		try {
			String reply = replyReader.readLine() ;
			textualRepresentation = Marshaller.getInstance().getValueString_reply_unmarshal(reply).getValueString() ;
		} catch (IOException e) {
			this.abort("Failed to retrieve textual representation of value", e) ;
		}
		return textualRepresentation ;
	}
}

Back to the top