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
|
using System;
using System.Text;
using System.IO;
using System.Reflection;
using System.Collections;
using System.Globalization;
using System.Runtime.InteropServices;
using Mono.Debugger;
using Mono.Debugger.Languages;
using Mono.GetOptions;
namespace Mono.Debugger.Frontend
{
public class ScriptingException : Exception
{
public ScriptingException (string format, params object[] args)
: base (String.Format (format, args))
{ }
}
public enum ModuleOperation
{
Ignore,
UnIgnore,
Step,
DontStep
}
public class ScriptingContext : DebuggerMarshalByRefObject
{
Thread current_thread;
Process current_process;
StackFrame current_frame;
Interpreter interpreter;
public ScriptingContext (Interpreter interpreter)
{
this.interpreter = interpreter;
}
public Interpreter Interpreter {
get { return interpreter; }
}
public bool HasTarget {
get {
return interpreter.HasTarget;
}
}
public Process CurrentProcess {
get {
if (current_process == null)
throw new TargetException (TargetError.NoTarget);
return current_process;
}
set {
current_process = value;
}
}
public Thread CurrentThread {
get {
if (current_thread == null)
throw new TargetException (TargetError.NoTarget);
return current_thread;
}
set {
current_thread = value;
if (value != null)
CurrentProcess = value.Process;
}
}
public bool HasFrame {
get { return current_frame != null; }
}
public StackFrame CurrentFrame {
get {
if (current_frame == null)
throw new TargetException (TargetError.NoTarget);
return current_frame;
}
set {
current_frame = value;
if (value != null)
CurrentThread = value.Thread;
}
}
public StackFrame GetFrame (int number)
{
Thread thread = CurrentThread;
if (!thread.IsStopped)
throw new TargetException (TargetError.NotStopped);
if (number == -1)
return thread.CurrentFrame;
Backtrace bt = thread.GetBacktrace ();
if (number >= bt.Count)
throw new ScriptingException ("No such frame: {0}", number);
return bt [number];
}
public Language CurrentLanguage {
get {
StackFrame frame = CurrentFrame;
if (frame.Language == null)
throw new ScriptingException (
"Stack frame has no source language.");
return frame.Language;
}
}
public string[] GetNamespaces (StackFrame frame)
{
Method method = frame.Method;
if ((method == null) || !method.HasLineNumbers)
return null;
return method.GetNamespaces ();
}
public string[] GetNamespaces ()
{
return GetNamespaces (CurrentFrame);
}
public SourceLocation CurrentLocation {
get {
StackFrame frame = CurrentFrame;
if (frame.SourceLocation == null)
throw new ScriptingException (
"Current location doesn't have source code");
return frame.SourceLocation;
}
}
public AddressDomain AddressDomain {
get {
return CurrentThread.AddressDomain;
}
}
public void Print (string message)
{
interpreter.Print (message);
}
public void Print (string format, params object[] args)
{
interpreter.Print (String.Format (format, args));
}
public void Print (object obj)
{
interpreter.Print (obj);
}
string MonoObjectToString (TargetClassObject obj)
{
TargetClassObject cobj = obj;
again:
TargetClassType ctype = cobj.Type;
if ((ctype.Name == "System.Object") || (ctype.Name == "System.ValueType"))
return null;
TargetMethodInfo[] methods = ctype.Methods;
foreach (TargetMethodInfo minfo in methods) {
if (minfo.Name != "ToString")
continue;
TargetFunctionType ftype = minfo.Type;
if (ftype.ParameterTypes.Length != 0)
continue;
if (ftype.ReturnType != ftype.Language.StringType)
continue;
RuntimeInvokeResult result = CurrentThread.RuntimeInvoke (
ftype, obj, new TargetObject [0], true, false);
Interpreter.Wait (result);
if ((result.ExceptionMessage != null) || (result.ReturnObject == null))
return null;
TargetObject retval = (TargetObject) result.ReturnObject;
object value = ((TargetFundamentalObject) retval).GetObject (CurrentThread);
return String.Format ("({0}) {{ \"{1}\" }}", obj.Type.Name, value);
}
cobj = cobj.GetParentObject (CurrentThread);
if (cobj != null)
goto again;
return null;
}
string DoFormatObject (TargetObject obj, DisplayFormat format)
{
if (format == DisplayFormat.Object) {
TargetClassObject cobj = obj as TargetClassObject;
if (cobj != null) {
string formatted = MonoObjectToString (cobj);
if (formatted != null)
return formatted;
}
}
return CurrentThread.PrintObject (interpreter.Style, obj, format);
}
public string FormatObject (object obj, DisplayFormat format)
{
string formatted;
try {
if (obj is TargetObject)
formatted = DoFormatObject ((TargetObject) obj, format);
else
formatted = interpreter.Style.FormatObject (
CurrentThread, obj, format);
} catch {
formatted = "<cannot display object>";
}
return formatted;
}
public string FormatType (TargetType type)
{
string formatted;
try {
formatted = CurrentThread.PrintType (
interpreter.Style, type);
} catch {
formatted = "<cannot display type>";
}
return (formatted);
}
public void PrintMethods (MethodSource[] methods)
{
for (int i = 0; i < methods.Length; i++) {
interpreter.Print ("{0,4} {1}", i+1, methods [i].Name);
}
}
public void PrintMethods (SourceFile source)
{
Print ("Methods from source file {0}: {1}", source.ID, source.FileName);
PrintMethods (source.Methods);
}
public void Dump (object obj)
{
if (obj == null)
Print ("null");
else if (obj is TargetObject)
Print (DumpObject ((TargetObject) obj));
else
Print ("unknown:{0}:{1}", obj.GetType (), obj);
}
public string DumpObject (TargetObject obj)
{
return String.Format ("object:{0}", DumpType (obj.Type));
}
public string DumpType (TargetType type)
{
StringBuilder sb = new StringBuilder ();
sb.Append (type.Name);
sb.Append (":");
sb.Append (type.HasFixedSize);
sb.Append (":");
sb.Append (type.Size);
sb.Append (":");
sb.Append (type.Kind);
sb.Append (" ");
switch (type.Kind) {
case TargetObjectKind.Fundamental:
sb.Append (((TargetFundamentalType) type).FundamentalKind);
break;
case TargetObjectKind.Pointer: {
TargetPointerType ptype = (TargetPointerType) type;
sb.Append (ptype.IsTypesafe);
sb.Append (":");
sb.Append (ptype.HasStaticType);
if (ptype.HasStaticType) {
sb.Append (":");
sb.Append (ptype.StaticType.Name);
}
break;
}
case TargetObjectKind.Array:
sb.Append (((TargetArrayType) type).ElementType.Name);
break;
#if FIXME
case TargetObjectKind.Alias: {
TargetTypeAlias alias = (TargetTypeAlias) type;
sb.Append (alias.TargetName);
if (alias.TargetType != null) {
sb.Append (":");
sb.Append (alias.TargetType.Name);
}
break;
}
#endif
}
return sb.ToString ();
}
public SourceLocation FindMethod (string name)
{
return CurrentProcess.FindMethod (name);
}
public void ShowSources (Module module)
{
if (!module.SymbolsLoaded)
return;
Print ("Sources for module {0}:", module.Name);
foreach (SourceFile source in module.Sources)
Print ("{0,4} {1}", source.ID, source.FileName);
}
public void LoadLibrary (Thread thread, string filename)
{
string pathname = Path.GetFullPath (filename);
if (!File.Exists (pathname))
throw new ScriptingException (
"No such file: `{0}'", pathname);
try {
CurrentProcess.LoadLibrary (thread, pathname);
} catch (TargetException ex) {
throw new ScriptingException (
"Cannot load library `{0}': {1}",
pathname, ex.Message);
}
Print ("Loaded library {0}.", filename);
}
public void ShowDisplay (Display display)
{
if (!HasTarget) {
Print ("Display {0}: {1}", display.Index, display.Text);
return;
}
try {
string text = Interpreter.ExpressionParser.EvaluateExpression (
this, display.Text, DisplayFormat.Object);
Print ("Display {0} (\"{1}\"): {2}", display.Index, display.Text, text);
} catch (ScriptingException ex) {
Print ("Display {0} (\"{1}\"): {2}", display.Index, display.Text,
ex.Message);
} catch (Exception ex) {
Print ("Display {0} (\"{1}\"): {2}", display.Index, display.Text,
ex);
}
}
}
}
|