File: DebuggerController.cs

package info (click to toggle)
monodevelop-debugger-mdb 2.4-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 332 kB
  • ctags: 563
  • sloc: cs: 3,507; makefile: 365; sh: 148; xml: 42
file content (255 lines) | stat: -rw-r--r-- 6,844 bytes parent folder | download
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Serialization.Formatters.Binary;
using Mono.Remoting.Channels.Unix;
using System.Threading;
using Mono.Debugging.Backend;
using Mono.Debugging.Client;

namespace Mono.Debugging.Backend.Mdb
{
	class DebuggerController : MarshalByRefObject, IDebuggerController
	{
		string remotingChannel = "tcp";
		IDebuggerServer debugger;
		Process process;
		IDebuggerSessionFrontend frontend;
		MonoDebuggerSession session;
		string unixRemotingFile;
		
		ManualResetEvent runningEvent = new ManualResetEvent (false);
		ManualResetEvent exitRequestEvent = new ManualResetEvent (false);
		ManualResetEvent exitedEvent = new ManualResetEvent (false);

		public IDebuggerServer DebuggerServer {
			get { return debugger; }
		}
		
		public DebuggerController (MonoDebuggerSession session, IDebuggerSessionFrontend frontend)
		{
			this.session = session;
			this.frontend = frontend;
		}

		#region IDebuggerController Members

		public void RegisterDebugger (IDebuggerServer debugger)
		{
			lock (this)
			{
				this.debugger = debugger;
				runningEvent.Set ();
			}
		}

		public void WaitForExit()
		{
			exitRequestEvent.WaitOne();
		}

		public void NotifyStarted ()
		{
			frontend.NotifyStarted ();
		}

		public void OnTargetEvent (TargetEventArgs args)
		{
			frontend.NotifyTargetEvent (args);
			if (args.Type == TargetEventType.TargetExited)
				StopDebugger ();
		}
		
		public void OnTargetOutput (bool isStderr, string line)
		{
			frontend.NotifyTargetOutput (isStderr, line);
		}
		
		public void OnDebuggerOutput (bool isStderr, string line)
		{
			frontend.NotifyDebuggerOutput (isStderr, line);
		}

		public bool OnCustomBreakpointAction (string actionId, object handle)
		{
			return frontend.NotifyCustomBreakpointAction (actionId, handle);
		}
		
		public void UpdateBreakpoint (object handle, int count, string lastTrace)
		{
			session.UpdateBreakEvent (handle, count, lastTrace);
		}
		
		public void NotifySourceFileLoaded (string[] fullFilePaths)
		{
			foreach (string file in fullFilePaths)
				frontend.NotifySourceFileLoaded (file);
		}

		public void NotifySourceFileUnloaded (string[] fullFilePaths)
		{
			foreach (string file in fullFilePaths)
				frontend.NotifySourceFileUnloaded (file);
		}
		
		#endregion

		public void StartDebugger (MonoDebuggerStartInfo startInfo)
		{
			lock (this)
			{
				exitRequestEvent.Reset ();

				string chId = RegisterRemotingChannel();

				BinaryFormatter bf = new BinaryFormatter();
				ObjRef oref = RemotingServices.Marshal(this);
				MemoryStream ms = new MemoryStream();
				bf.Serialize(ms, oref);
				string sref = Convert.ToBase64String(ms.ToArray());
				try
				{
					Process process = new Process();
					process.Exited += new EventHandler (ProcessExited);
					string location = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
					string argv = string.Empty;
					//if (isDebugMode) argv += " --debug";
					argv += " --debug '" + Path.Combine(location, "DebuggerServer.exe") + "' ";

					process.StartInfo = new ProcessStartInfo ("mono", argv);
					
					if (startInfo != null) {
						string monoPath = Path.Combine (startInfo.MonoPrefix, "bin");
						process.StartInfo.FileName = Path.Combine (monoPath, "mono");
						if (startInfo.ServerEnvironment != null) {
							foreach (KeyValuePair<string,string> evar in startInfo.ServerEnvironment)
								process.StartInfo.EnvironmentVariables [evar.Key] = evar.Value;
						}
					}
					
					process.StartInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
					process.StartInfo.UseShellExecute = false;
					process.StartInfo.RedirectStandardInput = true;
					process.EnableRaisingEvents = true;
					process.Start();
					
					// The server expects 3 lines with the following content:
					// 1) location of the Mono.Debugging assembly (needed since it may be located
					//    in a different directory)
					// 2) Remting channel to use
					// 3) Serialized reference to the IDebuggerController
					process.StandardInput.WriteLine (typeof(DebuggerSession).Assembly.Location);
					process.StandardInput.WriteLine (chId);
					process.StandardInput.WriteLine (sref);
					process.StandardInput.Flush();
					this.process = process;
				}
				catch (Exception ex)
				{
					Console.WriteLine("Error launching server: " + ex.ToString());
					throw;
				}
			}
			
			if (!runningEvent.WaitOne (15000, false)) {
				throw new ApplicationException ("Could not create the debugger process.");
			}
		}

		void ProcessExited (object sender, EventArgs args)
		{
			lock (this) {
				exitedEvent.Set ();
				Process p = (Process) sender;
				if (p != process) return;

				// The process suddently died
				runningEvent.Reset ();
				debugger = null;
				process = null;
			}
		}
		
		public void Exit ()
		{
			object elock = new object ();
			lock (elock) {
				ThreadPool.QueueUserWorkItem (delegate {
					try {
						debugger.Exit ();
						lock (elock) {
							Monitor.PulseAll (elock);
						}
					} catch {
						// Ignore
					}
				});
				
				// If the debugger does not stop, kill it.
				if (!Monitor.Wait (elock, 3000)) {
					StopDebugger ();
					OnTargetEvent (new TargetEventArgs (TargetEventType.TargetExited));
				}
			}
		}
		
		public void StopDebugger ()
		{
			try {
				Process oldProcess;
				lock (this) {
					if (debugger == null)
						return;
					runningEvent.Reset ();
					exitedEvent.Reset ();
					exitRequestEvent.Set ();
					oldProcess = process;
					debugger = null;
					process = null;
				}
	
				if (!exitedEvent.WaitOne (2000, false)) {
					try {
						oldProcess.Kill ();
					} catch {
					}
				}
			} catch (Exception ex) {
				Console.WriteLine (ex);
			}
		}

		string RegisterRemotingChannel()
		{
			if (remotingChannel == "tcp")
			{
				IChannel ch = ChannelServices.GetChannel("tcp");
				if (ch == null)
				{
					IDictionary dict = new Hashtable();
					BinaryClientFormatterSinkProvider clientProvider = new BinaryClientFormatterSinkProvider();
					BinaryServerFormatterSinkProvider serverProvider = new BinaryServerFormatterSinkProvider();

					dict["port"] = 0;
					serverProvider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;

					ChannelServices.RegisterChannel(new TcpChannel(dict, clientProvider, serverProvider), false);
				}
			}
			else
			{
				IChannel ch = ChannelServices.GetChannel ("unix");
				if (ch == null) {
					unixRemotingFile = Path.GetTempFileName (); 
					ChannelServices.RegisterChannel (new UnixChannel (unixRemotingFile), false);
				}
			}
			return remotingChannel;
		}
	}
}