File: ProcessStart.cs

package info (click to toggle)
mono-debugger 0.60%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 9,556 kB
  • ctags: 22,787
  • sloc: ansic: 99,407; cs: 42,429; sh: 9,192; makefile: 376
file content (295 lines) | stat: -rw-r--r-- 7,258 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
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
using System;
using System.IO;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;

namespace Mono.Debugger.Backend
{
	internal sealed class ProcessStart : DebuggerMarshalByRefObject
	{
		public readonly int PID;
		public readonly string CoreFile;
		public readonly bool IsNative;
		public readonly bool LoadNativeSymbolTable = true;

		string cwd;
		string base_dir;
		bool stop_in_main = true;
		string[] argv;
		string[] envp;
		DebuggerOptions options;
		DebuggerSession session;

		public static string MonoPath;

		static ProcessStart ()
		{
			/* Use relative path based on where mscorlib.dll is at to enable relocation */
			string prefix = new DirectoryInfo (Path.GetDirectoryName (typeof (int).Assembly.Location)).Parent.Parent.Parent.FullName;
			MonoPath = Path.Combine (Path.Combine (prefix, "bin"), "mono");
		}

		protected static bool IsMonoAssembly (string filename)
		{
			try {
				using (FileStream stream = new FileStream (filename, FileMode.Open, FileAccess.Read)) {
					byte[] data = new byte [128];
					if (stream.Read (data, 0, 128) != 128)
						return false;

					if ((data [0] != 'M') || (data [1] != 'Z'))
						return false;

					int offset = data [60] + (data [61] << 8) +
						(data [62] << 16) + (data [63] << 24);

					stream.Position = offset;

					data = new byte [28];
					if (stream.Read (data, 0, 28) != 28)
						return false;

					if ((data [0] != 'P') && (data [1] != 'E') &&
					    (data [2] != 0) && (data [3] != 0))
						return false;

					return true;
				}
			} catch {
				return false;
			}
		}

		internal ProcessStart (DebuggerSession session)
		{
			if (session == null)
				throw new ArgumentException ();

			this.session = session;
			this.options = session.Options;

			if ((options.File == null) || (options.File == ""))
				throw new ArgumentException ();
			if (options.InferiorArgs == null)
				throw new ArgumentException ();

			stop_in_main = options.StopInMain;

			cwd = options.WorkingDirectory;
			if (cwd == null)
				cwd = options.WorkingDirectory = System.Environment.CurrentDirectory;
			string mono_path = options.MonoPath != null ?
				options.MonoPath : MonoPath;

			if (IsMonoAssembly (options.File)) {
				IsNative = false;

				ArrayList start_argv = new ArrayList ();
				start_argv.Add (mono_path);
				start_argv.Add ("--inside-mdb");
				if (options.JitOptimizations != null)
					start_argv.Add ("--optimize=" + options.JitOptimizations);
				if (options.JitArguments != null)
					start_argv.AddRange (options.JitArguments);

				this.argv = new string [options.InferiorArgs.Length + start_argv.Count + 1];
				start_argv.CopyTo (this.argv, 0);
				argv [start_argv.Count] = options.File;
				options.InferiorArgs.CopyTo (this.argv, start_argv.Count + 1);
			} else {
				IsNative = true;

				this.argv = new string [options.InferiorArgs.Length + 1];
				argv [0] = options.File;
				options.InferiorArgs.CopyTo (this.argv, 1);
			}

			if (!File.Exists (options.File))
				throw new TargetException (TargetError.CannotStartTarget,
							   "No such file or directory: `{0}'",
							   options.File);

			try {
				base_dir = GetFullPath (Path.GetDirectoryName (options.File));
			} catch {
				throw new TargetException (TargetError.CannotStartTarget,
							   "Invalid directory: `{0}'", options.File);
			}

			SetupEnvironment ();
		}

		internal ProcessStart (DebuggerSession session, int pid)
		{
			this.session = session;
			this.options = session.Options;
			this.PID = pid;

			stop_in_main = options.StopInMain;

			IsNative = true;
		}

		internal ProcessStart (DebuggerSession session, string core_file)
			: this (session)
		{
			this.CoreFile = core_file;
		}

		internal ProcessStart (ProcessStart parent, int pid)
		{
			this.PID = pid;

			this.session = parent.session;
			this.options = parent.options;
			this.cwd = parent.cwd;
			this.argv = parent.argv;
			this.base_dir = parent.base_dir;

			SetupEnvironment ();
		}

		public void SetupApplication (string exe_file, string cwd, string[] cmdline_args)
		{
			this.cwd = cwd;

			cmdline_args [0] = exe_file;
			this.argv = cmdline_args;

			base_dir = GetFullPath (Path.GetDirectoryName (argv [0]));

			options = options.Clone ();
			options.File = exe_file;
			options.InferiorArgs = cmdline_args;
			options.WorkingDirectory = cwd;

			stop_in_main = false;

			SetupEnvironment ();
		}

		public DebuggerSession Session {
			get { return session; }
		}

		public DebuggerOptions Options {
			get { return options; }
		}

		public string[] CommandLineArguments {
			get { return argv; }
		}

		public string[] Environment {
			get { return envp; }
		}

		public string TargetApplication {
			get { return argv [0]; }
		}

		public string BaseDirectory {
			get { return base_dir; }
		}

		public string WorkingDirectory {
			get { return cwd; }
		}

		public bool StopInMain {
			get { return stop_in_main; }
		}

		void AddUserEnvironment (Hashtable hash)
		{
			if (options.UserEnvironment == null)
				return;
			foreach (string name in options.UserEnvironment.Keys)
				hash.Add (name, (string) options.UserEnvironment [name]);
		}

		void add_env_path (Hashtable hash, string name, string value)
		{
			if (!hash.Contains (name))
				hash.Add (name, value);
			else {
				hash [name] = value + ":" + value;
			}
		}

		protected void SetupEnvironment ()
		{
			Hashtable hash = new Hashtable ();
			AddUserEnvironment (hash);

			IDictionary env_vars = System.Environment.GetEnvironmentVariables ();
			foreach (string var in env_vars.Keys) {
				if (var == "GC_DONT_GC")
					continue;

				// Allow `UserEnvironment' to override env vars.
				if (hash.Contains (var))
					continue;
				hash.Add (var, env_vars [var]);
			}

			if (Options.MonoPrefix != null) {
				string prefix = Options.MonoPrefix;
				add_env_path (hash, "MONO_GAC_PREFIX", prefix);
				add_env_path (hash, "MONO_PATH", prefix + "/lib");
				add_env_path (hash, "LD_LIBRARY_PATH", prefix + "/lib");
				add_env_path (hash, "PATH", prefix + "/bin");
			}

			add_env_path (hash, "MONO_SHARED_HOSTNAME", "mdb");
			add_env_path (hash, "MONO_INSIDE_MDB", "1");

			ArrayList list = new ArrayList ();
			foreach (DictionaryEntry entry in hash) {
				string key = (string) entry.Key;
				string value = (string) entry.Value;

				list.Add (key + "=" + value);
			}

			envp = new string [list.Count];
			list.CopyTo (envp, 0);
		}


		protected string GetFullPath (string path)
		{
			string full_path;
			if (path.StartsWith ("./"))
				full_path = Path.GetFullPath (path.Substring (2));
			else if (path.Length > 0)
				full_path = Path.GetFullPath (path);
			else // FIXME: should search $PATH or something too
				full_path = Path.GetFullPath ("./");

			if (full_path.EndsWith ("/."))
				full_path = full_path.Substring (0, full_path.Length-2);
			return full_path;
		}

		protected string print_argv (string[] argv)
		{
			if (argv == null)
				return "null";
			else
				return String.Join (":", argv);
		}

		public string CommandLine {
			get { return print_argv (argv); }
		}

		public override string ToString ()
		{
			return String.Format ("{0} ({1}:{2}:{3})",
					      GetType (), WorkingDirectory,
					      print_argv (argv), IsNative);
		}
	}
}