File: DebuggerOptions.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 (531 lines) | stat: -rw-r--r-- 13,134 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
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
using System;
using System.IO;
using System.Reflection;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml;
using System.Xml.XPath;

using Mono.Debugger.Languages;
using Mono.Debugger.Backend;

namespace Mono.Debugger
{
	public class DebuggerOptions : DebuggerMarshalByRefObject
	{
		static int next_id = 0;
		public readonly int ID = ++next_id;

		string file;
		string[] inferior_args;
		string jit_optimizations;
		string[] jit_arguments;
		string working_directory;
		bool is_script, start_target;
		bool has_debug_flags;
		DebugFlags debug_flags = DebugFlags.None;
		string debug_output;
		bool in_emacs;
		bool stop_in_main = true;
		string mono_prefix, mono_path;

		/* The executable file we're debugging */
		public string File {
			get { return file; }
			set { file = value; }
		}

		/* argv[1...n] for the inferior process */
		public string[] InferiorArgs {
			get { return inferior_args; }
			set { inferior_args = value; }
		}

		/* JIT optimization flags affecting the inferior
		 * process */
		public string JitOptimizations {
			get { return jit_optimizations; }
			set { jit_optimizations = value; }
		}

		public string[] JitArguments {
			get { return jit_arguments; }
			set { jit_arguments = value; }
		}

		/* The inferior process's working directory */
		public string WorkingDirectory {
			get { return working_directory; }
			set { working_directory = value; }
		}

		/* true if we're running in a script */
		public bool IsScript {
			get { return is_script; }
			set { is_script = value; }
		}

		/* true if we want to start the application immediately */
		public bool StartTarget {
			get { return start_target; }
			set { start_target = value; }
		}
	  
		/* the value of the -debug-flags: command line argument */
		public DebugFlags DebugFlags {
			get { return debug_flags; }
			set {
				debug_flags = value;
				has_debug_flags = true;
			}
		}

		public bool HasDebugFlags {
			get { return has_debug_flags; }
		}

		public string DebugOutput {
			get { return debug_output; }
			set { debug_output = value; }
		}

		/* true if -f/-fullname is specified on the command line */
		public bool InEmacs {
			get { return in_emacs; }
			set { in_emacs = value; }
		}

		/* non-null if the user specified the -mono-prefix
		 * command line argument */
		public string MonoPrefix {
			get { return mono_prefix; }
			set { mono_prefix = value; }
		}

		/* non-null if the user specified the -mono command line argument */
		public string MonoPath {
			get { return mono_path; }
			set { mono_path = value; }
		}

		public bool StopInMain {
			get { return stop_in_main; }
			set { stop_in_main = value; }
		}

		Hashtable user_environment;

		string[] clone (string[] array)
		{
			if (array == null)
				return null;
			string[] new_array = new string [array.Length];
			array.CopyTo (new_array, 0);
			return new_array;
		}

		Hashtable clone (Hashtable hash)
		{
			if (hash == null)
				return null;
			Hashtable new_hash = new Hashtable ();
			foreach (string key in hash.Keys)
				new_hash.Add (key, hash [key]);
			return new_hash;
		}

		public DebuggerOptions Clone ()
		{
			DebuggerOptions options = new DebuggerOptions ();
			options.file = file;
			options.inferior_args = clone (inferior_args);
			options.jit_optimizations = jit_optimizations;
			options.jit_arguments = clone (jit_arguments);
			options.working_directory = working_directory;
			options.is_script = is_script;
			options.start_target = start_target;
			options.debug_flags = debug_flags;
			options.has_debug_flags = has_debug_flags;
			options.debug_output = debug_output;
			options.in_emacs = in_emacs;
			options.mono_prefix = mono_prefix;
			options.mono_path = mono_path;
			options.user_environment = clone (user_environment);
			return options;
		}

		public Hashtable UserEnvironment {
			get { return user_environment; }
		}

		public void SetEnvironment (string name, string value)
		{
			if (user_environment == null)
				user_environment = new Hashtable ();

			if (user_environment.Contains (name)) {
				if (value == null)
					user_environment.Remove (name);
				else
					user_environment [name] = value;
			} else if (value != null)
				user_environment.Add (name, value);
		}

		internal void GetSessionData (XmlElement root)
		{
			XmlElement file_e = root.OwnerDocument.CreateElement ("File");
			file_e.InnerText = file;
			root.AppendChild (file_e);

			if (InferiorArgs != null) {
				foreach (string arg in InferiorArgs) {
					XmlElement arg_e = root.OwnerDocument.CreateElement ("InferiorArgs");
					arg_e.InnerText = arg;
					root.AppendChild (arg_e);
				}
			}

			if (JitArguments != null) {
				foreach (string arg in JitArguments) {
					XmlElement arg_e = root.OwnerDocument.CreateElement ("JitArguments");
					arg_e.InnerText = arg;
					root.AppendChild (arg_e);
				}
			}

			if (JitOptimizations != null) {
				XmlElement opt_e = root.OwnerDocument.CreateElement ("JitOptimizations");
				opt_e.InnerText = JitOptimizations;
				root.AppendChild (opt_e);
			}
			if (WorkingDirectory != null) {
				XmlElement cwd_e = root.OwnerDocument.CreateElement ("WorkingDirectory");
				cwd_e.InnerText = WorkingDirectory;
				root.AppendChild (cwd_e);
			}
			if (MonoPrefix != null) {
				XmlElement prefix_e = root.OwnerDocument.CreateElement ("MonoPrefix");
				prefix_e.InnerText = MonoPrefix;
				root.AppendChild (prefix_e);
			}
			if (MonoPath != null) {
				XmlElement path_e = root.OwnerDocument.CreateElement ("MonoPath");
				path_e.InnerText = MonoPath;
				root.AppendChild (path_e);
			}
		}

		private DebuggerOptions ()
		{ }

		void append_array (ref string[] array, string value)
		{
			if (array == null) {
				array = new string [1];
				array [0] = value;
			} else {
				string[] new_array = new string [array.Length + 1];
				array.CopyTo (new_array, 0);
				new_array [array.Length] = value;
				array = new_array;
			}
		}

		internal DebuggerOptions (XPathNodeIterator iter)
		{
			while (iter.MoveNext ()) {
				switch (iter.Current.Name) {
				case "File":
					file = iter.Current.Value;
					break;
				case "InferiorArgs":
					append_array (ref inferior_args, iter.Current.Value);
					break;
				case "JitArguments":
					append_array (ref jit_arguments, iter.Current.Value);
					break;
				case "WorkingDirectory":
					working_directory = iter.Current.Value;
					break;
				case "MonoPrefix":
					mono_prefix = iter.Current.Value;
					break;
				case "MonoPath":
					mono_path = iter.Current.Value;
					break;
				default:
					throw new InternalError ();
				}
			}

			if (inferior_args == null)
				inferior_args = new string [0];
		}

		static void About ()
		{
			Console.WriteLine (
				"The Mono Debugger is (C) 2003-2007 Novell, Inc.\n\n" +
				"The debugger source code is released under the terms of the GNU GPL\n\n" +

				"For more information on Mono, visit the project Web site\n" +
				"   http://www.go-mono.com\n\n" +

				"The debugger was written by Martin Baulig and Chris Toshok");

			Environment.Exit (0);
		}

		static void Usage ()
		{
			Console.WriteLine (
				"Mono Debugger, (C) 2003-2007 Novell, Inc.\n" +
				"mdb [options] [exe-file]\n" +
				"mdb [options] -args exe-file [inferior-arguments ...]\n\n" +
				
				"   -args                     Arguments after exe-file are passed to inferior\n" +
				"   -debug-flags:PARAM        Sets the debugging flags\n" +
				"   -fullname                 Sets the debugging flags (short -f)\n" +
				"   -jit-arg:PARAM	      Additional argument for the inferior mono\n" +
				"   -jit-optimizations:PARAM  Set jit optimizations used on the inferior process\n" +
				"   -mono:PATH                Override the inferior mono\n" +
				"   -mono-prefix:PATH         Override the mono prefix\n" +
				"   -run                      Start inferior without halting in Main()\n" +
				"   -start                    Start inferior and stop in Main()\n" +
				"   -script                  \n" +
				"   -usage                   \n" +
				"   -version                  Display version and licensing information (short -V)\n" +
				"   -working-directory:DIR    Sets the working directory (short -cd)\n"
				);
		}

		public static DebuggerOptions ParseCommandLine (string[] args)
		{
			DebuggerOptions options = new DebuggerOptions ();
			int i;
			bool parsing_options = true;
			bool args_follow = false;

			for (i = 0; i < args.Length; i++) {
				string arg = args[i];

				if (arg == "")
					continue;

				if (!parsing_options)
					break;

				if (arg.StartsWith ("-")) {
					if (ParseOption (options, arg, ref args, ref i, ref args_follow))
						continue;
					Usage ();
					Console.WriteLine ("Unknown argument: {0}", arg);
					Environment.Exit (1);
				} else if (arg.StartsWith ("/")) {
					string unix_opt = "-" + arg.Substring (1);
					if (ParseOption (options, unix_opt, ref args, ref i, ref args_follow))
						continue;
				}

				options.File = arg;
				break;
			}

			if (args_follow) {
				string[] argv = new string [args.Length - i - 1];
				Array.Copy (args, i + 1, argv, 0, args.Length - i - 1);
				options.InferiorArgs = argv;
			} else {
				options.InferiorArgs = new string [0];
			}

			return options;
		}

		static string GetValue (ref string[] args, ref int i, string ms_val)
		{
			if (ms_val == "")
				return null;

			if (ms_val != null)
				return ms_val;

			if (i >= args.Length)
				return null;

			return args[++i];
		}

		static bool ParseDebugFlags (DebuggerOptions options, string value)
		{
			if (value == null)
				return false;

			int pos = value.IndexOf (':');
			if (pos > 0) {
				string filename = value.Substring (0, pos);
				value = value.Substring (pos + 1);
				
				options.DebugOutput = filename;
			}
			try {
				options.DebugFlags = (DebugFlags) Int32.Parse (value);
			} catch {
				return false;
			}
			return true;
		}

		static bool ParseOption (DebuggerOptions debug_options,
					 string option,
					 ref string [] args,
					 ref int i,
					 ref bool args_follow_exe)
		{
			int idx = option.IndexOf (':');
			string arg, value, ms_value = null;

			if (idx == -1){
				arg = option;
			} else {
				arg = option.Substring (0, idx);
				ms_value = option.Substring (idx + 1);
			}

			switch (arg) {
			case "-args":
				if (ms_value != null) {
					Usage ();
					Environment.Exit (1);
				}
				args_follow_exe = true;
				return true;

			case "-working-directory":
			case "-cd":
				value = GetValue (ref args, ref i, ms_value);
				if (value == null) {
					Usage ();
					Environment.Exit (1);
				}
				debug_options.WorkingDirectory = value;
				return true;

			case "-debug-flags":
				value = GetValue (ref args, ref i, ms_value);
				if (!ParseDebugFlags (debug_options, value)) {
					Usage ();
					Environment.Exit (1);
				}
				return true;

			case "-jit-optimizations":
				value = GetValue (ref args, ref i, ms_value);
				if (value == null) {
					Usage ();
					Environment.Exit (1);
				}
				debug_options.JitOptimizations = value;
				return true;

			case "-jit-arg":
				value = GetValue (ref args, ref i, ms_value);
				if (ms_value == null) {
					Usage ();
					Environment.Exit (1);
				}
				if (debug_options.JitArguments != null) {
					string[] old = debug_options.JitArguments;
					string[] new_args = new string [old.Length + 1];
					old.CopyTo (new_args, 0);
					new_args [old.Length] = value;
					debug_options.JitArguments = new_args;
				} else {
					debug_options.JitArguments = new string[] { value };
				}
				return true;

			case "-fullname":
			case "-f":
				if (ms_value != null) {
					Usage ();
					Environment.Exit (1);
				}
				debug_options.InEmacs = true;
				return true;

			case "-mono-prefix":
				value = GetValue (ref args, ref i, ms_value);
				if (value == null) {
					Usage ();
					Environment.Exit (1);
				}
				debug_options.MonoPrefix = value;
				return true;

			case "-mono":
				value = GetValue (ref args, ref i, ms_value);
				if (value == null) {
					Usage ();
					Environment.Exit (1);
				}
				debug_options.MonoPath = value;
				return true;

			case "-script":
				if (ms_value != null) {
					Usage ();
					Environment.Exit (1);
				}
				debug_options.IsScript = true;
				return true;

			case "-version":
			case "-V":
				if (ms_value != null) {
					Usage ();
					Environment.Exit (1);
				}
				About();
				Environment.Exit (1);
				return true;

			case "-help":
			case "--help":
			case "-h":
			case "-usage":
				if (ms_value != null) {
					Usage ();
					Environment.Exit (1);
				}
				Usage();
				Environment.Exit (1);
				return true;

			case "-run":
				if (ms_value != null) {
					Usage ();
					Environment.Exit (1);
				}
				debug_options.StartTarget = true;
				debug_options.StopInMain = false;
				return true;

			case "-start":
				if (ms_value != null) {
					Usage ();
					Environment.Exit (1);
				}
				debug_options.StartTarget = true;
				debug_options.StopInMain = true;
				return true;
			}

			return false;
		}
	}
}