File: MdbAdaptorFactory.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 (122 lines) | stat: -rw-r--r-- 4,637 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
// 
// MdbAdaptorFactory.cs
//  
// Author:
//       Lluis Sanchez Gual <lluis@novell.com>
// 
// Copyright (c) 2009 Novell, Inc (http://www.novell.com)
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Reflection;
using System.Diagnostics;
using System.IO;

namespace DebuggerServer
{
	public static class MdbAdaptorFactory
	{
		// Bump this version number if any change is in MdbAdaptor or subclases
		const int ApiVersion = 3;
		
		// Versions for which there is an adaptor. If an adaptor is a subclass of another adaptor,
		// the base adaptor version must be specified (recursively) after the adaptor version
		// using | as separator.
		static readonly string[] supportedVersions = new string[] {"2-4-4|2-4-2", "2-4-2", "2-0"};
		
		public static MdbAdaptor CreateAdaptor (string mdbVersion)
		{
			ProcessStartInfo pinfo = new ProcessStartInfo ();
			pinfo.FileName = "gmcs";
			
			if (mdbVersion != null) {
				MdbAdaptor mdb = TryCreateAdaptor (pinfo, mdbVersion);
				if (mdb == null)
					throw new InvalidOperationException ("Unsupported MDB version");
				return mdb;
			}
			
			foreach (string v in supportedVersions) {
				MdbAdaptor mdb = TryCreateAdaptor (pinfo, v);
				if (mdb != null)
					return mdb;
			}
			throw new InvalidOperationException ("Unsupported MDB version");
		}
		
		static MdbAdaptor TryCreateAdaptor (ProcessStartInfo pinfo, string versions)
		{
			string[] versionsArray = versions.Split ('|');
			string version = versionsArray [0];
			
			string tmpPath = Path.GetTempPath ();
			tmpPath = Path.Combine (tmpPath, "monodevelop-debugger-mdb");
			if (!Directory.Exists (tmpPath))
				Directory.CreateDirectory (tmpPath);
			
			string outFile = Path.Combine (tmpPath, "adaptor-" + ApiVersion + "--" + version + ".dll");
			DateTime thisTime = File.GetLastWriteTime (typeof(MdbAdaptorFactory).Assembly.Location);
			
			if (!File.Exists (outFile) || File.GetLastWriteTime (outFile) < thisTime) {
				string args = "/t:library ";
				args += "\"/out:" + outFile + "\" ";
				args += "\"/r:" + typeof(MdbAdaptorFactory).Assembly.Location + "\" ";
				args += "\"/r:" + typeof(Mono.Debugger.Debugger).Assembly.Location + "\" ";
				args += "\"/r:" + typeof(Mono.Debugging.Client.DebuggerSession).Assembly.Location + "\" ";
				args += "\"/r:" + typeof(Mono.Debugging.Backend.Mdb.IDebuggerServer).Assembly.Location + "\" ";
				
				// Write the source code for all required classes
				foreach (string ver in versionsArray) {
					string resName = "MdbAdaptor-" + ver + ".cs";
					Stream s = typeof(MdbAdaptorFactory).Assembly.GetManifestResourceStream (resName);
					if (s == null)
						throw new InvalidOperationException ("Resource not found: " + resName);
					StreamReader sr = new StreamReader (s);
					string txt = sr.ReadToEnd ();
					sr.Close ();
					s.Close ();
					
					string csfile = Path.Combine (tmpPath, "adaptor-" + ver + ".cs");
					File.WriteAllText (csfile, txt);
					args += "\"" + csfile + "\" ";
				}
				
				pinfo.Arguments = args;
				pinfo.RedirectStandardError = true;
				pinfo.RedirectStandardOutput = true;
				pinfo.UseShellExecute = false;
				Process proc = Process.Start (pinfo);
				proc.WaitForExit ();
				if (proc.ExitCode != 0)
					return null;
				Console.WriteLine ("Generated: " + outFile);
			}
			
			Assembly asm = Assembly.LoadFrom (outFile);
			Type at = asm.GetType ("DebuggerServer.MdbAdaptor_" + version.Replace ('-','_'));
			if (at != null) {
				MdbAdaptor a = (MdbAdaptor) Activator.CreateInstance (at);
				a.MdbVersion = version;
				return a;
			}
			return null;
		}
	}
}