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 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
|
using System;
using System.IO;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Lifetime;
using System.Text;
using ST = System.Threading;
using Mono.Debugger;
using MD = Mono.Debugger;
using ML = Mono.Debugger.Languages;
using DL = Mono.Debugging.Client;
using DB = Mono.Debugging.Backend;
using Mono.Debugging.Client;
using Mono.Debugging.Evaluation;
using Mono.Debugging.Backend.Mdb;
namespace DebuggerServer
{
class DebuggerServer : MarshalByRefObject, IDebuggerServer, ISponsor
{
IDebuggerController controller;
MD.Debugger debugger;
MD.DebuggerSession session;
MD.Process process;
MD.GUIManager guiManager;
MdbAdaptor mdbAdaptor;
int max_frames;
List<ST.WaitCallback> stoppedWorkQueue = new List<ST.WaitCallback> ();
List<ST.WaitCallback> eventQueue = new List<ST.WaitCallback> ();
bool initializing;
bool running;
bool exited;
NRefactoryEvaluator evaluator = new NRefactoryEvaluator ();
MD.Thread activeThread;
Dictionary<int,BreakEvent> events = new Dictionary<int,BreakEvent> ();
Dictionary<int,string> lastConditionValue = new Dictionary<int,string> ();
DateTime lastBreakEventUpdate = DateTime.Now;
Dictionary<int, ST.WaitCallback> breakUpdates = new Dictionary<int,ST.WaitCallback> ();
bool breakUpdateEventsQueued;
MdbObjectValueAdaptor mdbObjectValueAdaptor;
const int BreakEventUpdateNotifyDelay = 500;
public DebuggerSessionOptions SessionOptions;
public DebuggerServer (IDebuggerController dc)
{
this.controller = dc;
mdbObjectValueAdaptor = new MdbObjectValueAdaptor ();
MarshalByRefObject mbr = (MarshalByRefObject)controller;
ILease lease = mbr.GetLifetimeService() as ILease;
lease.Register(this);
max_frames = 100;
ST.Thread t = new ST.Thread ((ST.ThreadStart)EventDispatcher);
t.IsBackground = true;
t.Start ();
}
public override object InitializeLifetimeService ()
{
return null;
}
public MdbObjectValueAdaptor MdbObjectValueAdaptor {
get { return mdbObjectValueAdaptor; }
}
public Mono.Debugging.Evaluation.ExpressionEvaluator Evaluator {
get { return evaluator; }
}
public MdbAdaptor MdbAdaptor {
get { return mdbAdaptor; }
}
#region IDebugger Members
public string InitializeMdb (string mdbVersion, int mdPid)
{
mdbAdaptor = MdbAdaptorFactory.CreateAdaptor (mdbVersion);
WatchParentProcess (mdPid);
return mdbAdaptor.MdbVersion;
}
void WatchParentProcess (int pid)
{
ST.Thread t = new ST.Thread (delegate () {
while (true) {
try {
System.Diagnostics.Process.GetProcessById (pid);
} catch {
ForceShutdown ();
}
ST.Thread.Sleep (1000);
}
});
t.IsBackground = true;
t.Start ();
}
void ForceShutdown ()
{
try {
Exit ();
} catch {
}
Environment.Exit (1);
}
public void Run (MonoDebuggerStartInfo startInfo, DebuggerSessionOptions sessionOptions)
{
try {
if (startInfo == null)
throw new ArgumentNullException ("startInfo");
Console.WriteLine ("MDB version: " + mdbAdaptor.MdbVersion);
this.SessionOptions = sessionOptions;
mdbAdaptor.StartInfo = startInfo;
Report.Initialize ();
DebuggerConfiguration config = new DebuggerConfiguration ();
config.LoadConfiguration ();
mdbAdaptor.Configuration = config;
mdbAdaptor.InitializeConfiguration ();
debugger = new MD.Debugger (config);
debugger.ModuleLoadedEvent += OnModuleLoadedEvent;
debugger.ModuleUnLoadedEvent += OnModuleUnLoadedEvent;
debugger.ProcessReachedMainEvent += delegate (MD.Debugger deb, MD.Process proc) {
OnInitialized (deb, proc);
};
if (startInfo.IsXsp) {
mdbAdaptor.SetupXsp ();
config.FollowFork = false;
}
config.OpaqueFileNames = false;
DebuggerOptions options = DebuggerOptions.ParseCommandLine (new string[] { startInfo.Command } );
options.WorkingDirectory = startInfo.WorkingDirectory;
Environment.CurrentDirectory = startInfo.WorkingDirectory;
options.StopInMain = false;
if (!string.IsNullOrEmpty (startInfo.Arguments))
options.InferiorArgs = ToArgsArray (startInfo.Arguments);
if (startInfo.EnvironmentVariables != null) {
foreach (KeyValuePair<string,string> env in startInfo.EnvironmentVariables)
options.SetEnvironment (env.Key, env.Value);
}
session = new MD.DebuggerSession (config, options, "main", null);
mdbAdaptor.Session = session;
mdbAdaptor.InitializeSession ();
ST.ThreadPool.QueueUserWorkItem (delegate {
// Run in a thread to avoid a deadlock, since NotifyStarted calls back to the client.
NotifyStarted ();
debugger.Run(session);
});
} catch (Exception e) {
Console.WriteLine ("error: " + e.ToString ());
throw;
}
}
public void AttachToProcess (long pid, DebuggerSessionOptions sessionOptions)
{
Report.Initialize ();
this.SessionOptions = sessionOptions;
DebuggerConfiguration config = new DebuggerConfiguration ();
mdbAdaptor.Configuration = config;
mdbAdaptor.InitializeConfiguration ();
config.LoadConfiguration ();
debugger = new MD.Debugger (config);
DebuggerOptions options = DebuggerOptions.ParseCommandLine (new string[0]);
options.StopInMain = false;
session = new MD.DebuggerSession (config, options, "main", (IExpressionParser) null);
mdbAdaptor.Session = session;
Process proc = debugger.Attach (session, (int)pid);
OnInitialized (debugger, proc);
ST.ThreadPool.QueueUserWorkItem (delegate {
NotifyStarted ();
});
}
public void Detach ()
{
CancelRuntimeInvokes ();
try {
debugger.Detach ();
} catch (Exception ex) {
Console.WriteLine (ex);
} finally {
running = false;
}
}
public void Stop ()
{
CancelRuntimeInvokes ();
QueueTask (delegate {
guiManager.Stop (process.MainThread);
});
}
public void Exit ()
{
CancelRuntimeInvokes ();
ResetTaskQueue ();
debugger.Kill ();
running = false;
}
public void NextLine ()
{
if (running)
throw new InvalidOperationException ("Target already running");
OnStartRunning ();
guiManager.StepOver (activeThread);
}
public void StepLine ()
{
if (running)
throw new InvalidOperationException ("Target already running");
OnStartRunning ();
guiManager.StepInto (activeThread);
}
public void StepInstruction ()
{
if (running)
throw new InvalidOperationException ("Target already running");
OnStartRunning ();
activeThread.StepInstruction ();
}
public void NextInstruction ()
{
if (running)
throw new InvalidOperationException ("Target already running");
OnStartRunning ();
activeThread.NextInstruction ();
}
public void Finish ()
{
if (running)
throw new InvalidOperationException ("Target already running");
OnStartRunning ();
guiManager.StepOut (activeThread);
}
public void Continue ()
{
if (running)
throw new InvalidOperationException ("Target already running");
OnStartRunning ();
QueueTask (delegate {
guiManager.Continue (activeThread);
});
}
public bool AllowBreakpointChanges {
get {
return mdbAdaptor == null || mdbAdaptor.AllowBreakEventChanges;
}
}
public int InsertBreakEvent (DL.BreakEvent be, bool enable)
{
CancelRuntimeInvokes ();
DL.Breakpoint bp = be as DL.Breakpoint;
MD.Event ev = null;
if (bp != null) {
MD.SourceLocation location = new MD.SourceLocation (bp.FileName, bp.Line);
MD.SourceBreakpoint sbp = new MD.SourceBreakpoint (session, ThreadGroup.Global, location);
mdbAdaptor.InitializeBreakpoint (sbp);
session.AddEvent (sbp);
ev = sbp;
}
else if (be is Catchpoint) {
lock (pendingCatchpoints) {
Catchpoint cp = (Catchpoint) be;
ML.TargetType exc = null;
if (process != null) {
foreach (Module mod in process.Modules) {
exc = mod.Language.LookupType (cp.ExceptionName);
if (exc != null)
break;
}
}
if (exc != null)
ev = session.InsertExceptionCatchPoint (process.MainThread, ThreadGroup.Global, exc);
else {
pendingCatchpoints.Add (cp);
return -1;
}
}
}
ev.IsEnabled = enable;
if (!initializing) {
lock (debugger) {
mdbAdaptor.ActivateEvent (ev);
}
}
if (bp != null && !running && !initializing && activeThread.CurrentFrame != null && !string.IsNullOrEmpty (bp.ConditionExpression) && bp.BreakIfConditionChanges) {
// Initial expression evaluation
MdbEvaluationContext ctx = new MdbEvaluationContext (activeThread, activeThread.CurrentFrame, null, SessionOptions.EvaluationOptions);
ML.TargetObject ob = EvaluateExp (ctx, bp.ConditionExpression);
if (ob != null)
lastConditionValue [ev.Index] = evaluator.TargetObjectToExpression (ctx, ob).Value;
}
events [ev.Index] = be;
return ev.Index;
}
public void RemoveBreakEvent (int handle)
{
if (exited)
return;
CancelRuntimeInvokes ();
Event ev = session.GetEvent (handle);
mdbAdaptor.RemoveEvent (ev);
}
public void EnableBreakEvent (int handle, bool enable)
{
CancelRuntimeInvokes ();
Event ev = session.GetEvent (handle);
if (enable)
ev.Activate (process.MainThread);
else
ev.Deactivate (process.MainThread);
}
public object UpdateBreakEvent (object handle, DL.BreakEvent bp)
{
events [(int)handle] = bp;
return handle;
}
bool BreakEventCheck (MD.TargetEventArgs args)
{
MD.StackFrame frame = args.Frame;
if (!(args.Data is int))
return true;
int eventHandle = (int) args.Data;
DL.BreakEvent be;
if (!events.TryGetValue (eventHandle, out be))
return true;
// Check hit count
if (be.HitCount > 0) {
be.HitCount--;
DispatchEvent (delegate {
NotifyBreakEventUpdate (eventHandle, be.HitCount, null);
});
return false;
}
MdbEvaluationContext ctx = new MdbEvaluationContext (frame.Thread, frame, null, SessionOptions.EvaluationOptions);
DL.Breakpoint bp = be as DL.Breakpoint;
if (bp != null && !string.IsNullOrEmpty (bp.ConditionExpression)) {
ML.TargetObject val = EvaluateExp (ctx, bp.ConditionExpression);
if (val == null)
return false;
if (bp.BreakIfConditionChanges) {
string current = evaluator.TargetObjectToExpression (ctx, val).Value;
string last;
bool found = lastConditionValue.TryGetValue (eventHandle, out last);
lastConditionValue [eventHandle] = current;
if (!found || last == current)
return false;
} else {
ML.TargetFundamentalObject fob = val as ML.TargetFundamentalObject;
if (fob == null)
return false;
object ob = fob.GetObject (frame.Thread);
if (!(ob is bool) || !(bool)ob)
return false;
}
}
switch (be.HitAction) {
case HitAction.Break:
return true;
case HitAction.CustomAction:
return controller.OnCustomBreakpointAction (be.CustomActionId, eventHandle);
case HitAction.PrintExpression:
if (string.IsNullOrEmpty (be.TraceExpression) || frame == null)
return false;
ML.TargetObject val = EvaluateExp (ctx, be.TraceExpression);
if (val != null) {
string str = evaluator.TargetObjectToString (ctx, val);
DispatchEvent (delegate {
NotifyBreakEventUpdate (eventHandle, -1, str);
});
}
return false;
}
return false;
}
ML.TargetObject EvaluateExp (EvaluationContext ctx, string exp)
{
ValueReference var;
try {
EvaluationContext cctx = ctx.Clone ();
EvaluationOptions ops = cctx.Options;
ops.AllowMethodEvaluation = true;
cctx.Options = ops;
var = (ValueReference) Server.Instance.Evaluator.Evaluate (cctx, exp);
return (ML.TargetObject) var.Value;
} catch {
return null;
}
}
void NotifyBreakEventUpdate (int eventHandle, int hitCount, string lastTrace)
{
bool notify = false;
lock (breakUpdates)
{
int span = (int) (DateTime.Now - lastBreakEventUpdate).TotalMilliseconds;
if (span >= BreakEventUpdateNotifyDelay && !breakUpdateEventsQueued) {
// Last update was more than 0.5s ago. The update can be sent.
lastBreakEventUpdate = DateTime.Now;
notify = true;
} else {
// Queue the event notifications to avoid wasting too much time
breakUpdates [eventHandle] = delegate {
controller.UpdateBreakpoint (eventHandle, hitCount, lastTrace);
};
if (!breakUpdateEventsQueued) {
breakUpdateEventsQueued = true;
ST.ThreadPool.QueueUserWorkItem (delegate {
ST.Thread.Sleep (BreakEventUpdateNotifyDelay - span);
List<ST.WaitCallback> copy;
lock (breakUpdates) {
copy = new List<ST.WaitCallback> (breakUpdates.Values);
breakUpdates.Clear ();
breakUpdateEventsQueued = false;
lastBreakEventUpdate = DateTime.Now;
}
foreach (ST.WaitCallback wc in copy)
wc (null);
});
}
}
}
if (notify)
controller.UpdateBreakpoint (eventHandle, hitCount, lastTrace);
}
public ThreadInfo[] GetThreads (long processId)
{
MD.Process p = GetProcess (processId);
if (p == null)
return new ThreadInfo [0];
List<DL.ThreadInfo> list = new List<DL.ThreadInfo> ();
foreach (MD.Thread t in p.GetThreads ()) {
DL.ThreadInfo ct = CreateThreadInfo (t);
list.Add (ct);
}
return list.ToArray ();
}
public ProcessInfo[] GetProcesses ()
{
List<DL.ProcessInfo> list = new List<DL.ProcessInfo> ();
foreach (MD.Process p in debugger.Processes)
list.Add (new DL.ProcessInfo (p.ID, p.TargetApplication + " " + string.Join (" ", p.CommandLineArguments)));
return list.ToArray ();
}
ThreadInfo CreateThreadInfo (MD.Thread t)
{
string loc;
if (t.CurrentFrame != null && t.CurrentFrame.SourceLocation != null) {
loc = t.CurrentFrame.ToString ();
} else
loc = "<Unknown>";
return new ThreadInfo (t.Process.ID, t.ID, t.Name, loc);
}
public DL.Backtrace GetThreadBacktrace (long processId, long threadId)
{
MD.Thread t = GetThread (processId, threadId);
if (t != null && t.IsStopped)
return CreateBacktrace (t, null);
else
return null;
}
public void SetActiveThread (long processId, long threadId)
{
activeThread = GetThread (processId, threadId);
}
MD.Thread GetThread (long procId, long threadId)
{
MD.Process proc = GetProcess (procId);
if (proc != null) {
foreach (MD.Thread t in proc.GetThreads ()) {
if (t.ID == (int)threadId)
return t;
}
}
return null;
}
MD.Process GetProcess (long id)
{
foreach (MD.Process p in debugger.Processes) {
if (p.ID == (int)id)
return p;
}
return null;
}
public AssemblyLine[] DisassembleFile (string file)
{
CancelRuntimeInvokes ();
// Not working yet
return null;
/* SourceFile sourceFile = session.FindFile (file);
List<AssemblyLine> lines = new List<AssemblyLine> ();
foreach (MethodSource met in sourceFile.Methods) {
TargetAddress addr = met.NativeMethod.StartAddress;
TargetAddress endAddr = met.NativeMethod.EndAddress;
while (addr < endAddr) {
SourceAddress line = met.NativeMethod.LineNumberTable.Lookup (addr);
AssemblerLine aline = process.MainThread.DisassembleInstruction (met.NativeMethod, addr);
if (aline != null) {
if (line != null)
lines.Add (new DL.AssemblyLine (addr.Address, aline.Text, line.Row));
else
lines.Add (new DL.AssemblyLine (addr.Address, aline.Text));
addr += aline.InstructionSize;
} else
addr++;
}
}
lines.Sort (delegate (DL.AssemblyLine l1, DL.AssemblyLine l2) {
return l1.SourceLine.CompareTo (l2.SourceLine);
});
return lines.ToArray ();
*/
}
#endregion
public void Dispose ()
{
MarshalByRefObject mbr = (MarshalByRefObject)controller;
ILease lease = mbr.GetLifetimeService() as ILease;
lease.Unregister(this);
}
public void WriteDebuggerOutput (string msg, params object[] args)
{
DispatchEvent (delegate {
controller.OnDebuggerOutput (false, string.Format (msg, args));
});
}
public void WriteDebuggerError (Exception ex)
{
if (ex is EvaluatorException)
Console.WriteLine (ex.Message);
else
Console.WriteLine (ex);
}
public ML.TargetObject RuntimeInvoke (MdbEvaluationContext ctx, ML.TargetFunctionType function,
ML.TargetStructObject object_argument,
params ML.TargetObject[] param_objects)
{
MethodCall mc = new MethodCall (ctx, function, object_argument, param_objects);
ctx.Adapter.AsyncExecute (mc, ctx.Options.EvaluationTimeout);
return mc.ReturnValue;
}
DL.Backtrace CreateBacktrace (MD.Thread thread, ML.TargetObject exception)
{
List<MD.StackFrame> frames = new List<MD.StackFrame> ();
DateTime t = DateTime.Now;
if (!thread.CurrentFrame.Language.IsManaged) {
MD.Backtrace bt = thread.GetBacktrace (MD.Backtrace.Mode.Native, max_frames);
if (bt != null) {
Console.WriteLine ("GetBacktrace native time: {0} ms n:{1}", (DateTime.Now - t).TotalMilliseconds, bt.Count);
frames.AddRange (bt.Frames);
}
} else {
t = DateTime.Now;
MD.Backtrace backtrace = thread.GetBacktrace (MD.Backtrace.Mode.Managed, max_frames);
if (backtrace != null) {
Console.WriteLine ("GetBacktrace managed time: {0} ms n:{1}", (DateTime.Now - t).TotalMilliseconds, backtrace.Count);
frames.AddRange (backtrace.Frames);
}
}
if (frames.Count > 0) {
BacktraceWrapper wrapper = new BacktraceWrapper (frames.ToArray (), exception);
return new DL.Backtrace (wrapper);
} else if (thread.CurrentBacktrace != null) {
BacktraceWrapper wrapper = new BacktraceWrapper (thread.CurrentBacktrace.Frames, exception);
return new DL.Backtrace (wrapper);
}
return null;
}
#region ISponsor Members
public TimeSpan Renewal(ILease lease)
{
return TimeSpan.FromSeconds(7);
}
#endregion
private void OnInitialized (MD.Debugger debugger, Process process)
{
Console.WriteLine (">> OnInitialized");
this.process = process;
this.debugger = debugger;
mdbAdaptor.Process = process;
guiManager = process.StartGUIManager ();
//FIXME: conditionally add event handlers
process.TargetOutputEvent += OnTargetOutput;
debugger.ProcessCreatedEvent += OnProcessCreatedEvent;
debugger.ProcessExecdEvent += OnProcessExecdEvent;
debugger.ProcessExitedEvent += OnProcessExitedEvent;
debugger.ThreadCreatedEvent += OnThreadCreatedEvent;
debugger.ThreadExitedEvent += OnThreadExitedEvent;
debugger.TargetExitedEvent += OnTargetExitedEvent;
guiManager.TargetEvent += OnTargetEvent;
// Not supported
//guiManager.BreakpointHitHandler = BreakEventCheck;
activeThread = process.MainThread;
running = true;
Console.WriteLine ("<< OnInitialized");
}
void NotifyStarted ()
{
try {
initializing = true;
controller.NotifyStarted();
initializing = false;
} catch (Exception ex) {
Console.WriteLine (ex);
}
}
void OnTargetOutput (bool is_stderr, string text)
{
DispatchEvent (delegate {
controller.OnTargetOutput (is_stderr, text);
});
}
void QueueTask (ST.WaitCallback cb)
{
lock (debugger) {
if (stoppedWorkQueue.Count > 0)
stoppedWorkQueue.Add (cb);
else
cb (null);
}
}
void ResetTaskQueue ()
{
lock (debugger) {
stoppedWorkQueue.Clear ();
}
}
void LogEvent (MD.TargetEventArgs args)
{
Console.WriteLine ("Server OnTargetEvent: {0} stopped:{1} data:{2} queue:{3} thread:{4} running:{5}", args.Type, args.IsStopped, args.Data, stoppedWorkQueue.Count, args.Frame != null ? args.Frame.Thread : null, running);
}
private void OnTargetEvent (MD.Thread thread, MD.TargetEventArgs args)
{
try {
if (!running) {
LogEvent (args);
return;
}
bool notifyToClient = args.IsStopped || args.Type == MD.TargetEventType.UnhandledException || args.Type == MD.TargetEventType.Exception || args.Type == MD.TargetEventType.TargetInterrupted;
LogEvent (args);
bool isStop = args.Type != MD.TargetEventType.FrameChanged &&
args.Type != MD.TargetEventType.TargetExited &&
args.Type != MD.TargetEventType.TargetRunning;
if (isStop) {
lock (debugger) {
if (stoppedWorkQueue.Count > 0) {
// Execute queued work in another thread with a small delay
// since it is not safe to execute it here
System.Threading.ThreadPool.QueueUserWorkItem (delegate {
System.Threading.Thread.Sleep (50);
bool resume = false;
lock (debugger) {
foreach (ST.WaitCallback cb in stoppedWorkQueue) {
cb (null);
}
stoppedWorkQueue.Clear ();
}
if (resume)
guiManager.Continue (process.MainThread);
else if (notifyToClient)
NotifyTargetEvent (thread, args);
});
return;
}
}
}
if (notifyToClient)
NotifyTargetEvent (thread, args);
} catch (Exception e) {
Console.WriteLine ("*** DS.OnTargetEvent1, exception : {0}", e.ToString ());
}
}
void NotifyTargetEvent (MD.Thread thread, MD.TargetEventArgs args)
{
if (args.Frame != null)
activeThread = args.Frame.Thread;
try {
if (args.Type == MD.TargetEventType.TargetStopped && ((int)args.Data) != 0) {
DispatchEvent (delegate {
controller.OnDebuggerOutput (false, string.Format ("Thread {0:x} received signal {1}.\n", args.Frame.Thread.ID, args.Data));
});
}
DL.TargetEventType type;
switch (args.Type) {
case MD.TargetEventType.Exception: type = DL.TargetEventType.ExceptionThrown; break;
case MD.TargetEventType.TargetHitBreakpoint: type = DL.TargetEventType.TargetHitBreakpoint; break;
case MD.TargetEventType.TargetInterrupted: type = DL.TargetEventType.TargetInterrupted; break;
case MD.TargetEventType.TargetSignaled: type = DL.TargetEventType.TargetSignaled; break;
case MD.TargetEventType.TargetStopped: type = DL.TargetEventType.TargetStopped; break;
case MD.TargetEventType.UnhandledException: type = DL.TargetEventType.UnhandledException; break;
default:
return;
}
OnCleanFrameData ();
DL.TargetEventArgs targetArgs = new DL.TargetEventArgs (type);
if (args.Type != MD.TargetEventType.TargetExited) {
ML.TargetObject exception = null;
if (args.Type == MD.TargetEventType.UnhandledException || args.Type == MD.TargetEventType.Exception)
exception = args.Frame.ExceptionObject;
targetArgs.Backtrace = CreateBacktrace (thread, exception);
targetArgs.Thread = CreateThreadInfo (activeThread);
}
running = false;
DispatchEvent (delegate {
controller.OnTargetEvent (targetArgs);
});
} catch (Exception e) {
Console.WriteLine ("*** DS.OnTargetEvent2, exception : {0}", e.ToString ());
}
}
void OnStartRunning ()
{
OnCleanFrameData ();
running = true;
}
void OnCleanFrameData ()
{
// Dispose all previous remote objects
RemoteFrameObject.DisconnectAll ();
CancelRuntimeInvokes ();
}
public void CancelRuntimeInvokes ()
{
mdbObjectValueAdaptor.CancelAsyncOperations ();
}
/* public void WaitRuntimeInvokes ()
{
invokeManager.WaitForAll ();
}
*/
private void OnProcessCreatedEvent (MD.Debugger debugger, MD.Process process)
{
WriteDebuggerOutput (string.Format ("Process {0} created.\n", process.ID));
}
private void OnProcessExitedEvent (MD.Debugger debugger, MD.Process process)
{
WriteDebuggerOutput (string.Format ("Process {0} exited.\n", process.ID));
}
private void OnProcessExecdEvent (MD.Debugger debugger, MD.Process process)
{
WriteDebuggerOutput (string.Format ("Process {0} execd.\n", process.ID));
}
private void OnThreadCreatedEvent (MD.Debugger debugger, MD.Thread thread)
{
WriteDebuggerOutput (string.Format ("Thread {0} created.\n", thread.ID));
}
private void OnThreadExitedEvent (MD.Debugger debugger, MD.Thread thread)
{
WriteDebuggerOutput (string.Format ("Thread {0} exited.\n", thread.ID));
}
List<Catchpoint> pendingCatchpoints = new List<Catchpoint> ();
private void OnModuleLoadedEvent (Module module)
{
SourceFile[] sfiles = module.Sources;
string[] files = new string [sfiles.Length];
for (int n=0; n<files.Length; n++)
files [n] = sfiles [n].FileName;
controller.NotifySourceFileLoaded (files);
WriteDebuggerOutput (string.Format ("Module loaded: {0}.\n", module.Name));
if (process == null)
return;
lock (pendingCatchpoints) {
foreach (Catchpoint cp in pendingCatchpoints.ToArray ()) {
ML.TargetType exc = module.Language.LookupType (cp.ExceptionName);
if (exc != null) {
session.InsertExceptionCatchPoint (process.MainThread, ThreadGroup.Global, exc);
pendingCatchpoints.Remove (cp);
}
}
}
}
private void OnModuleUnLoadedEvent (Module module)
{
SourceFile[] sfiles = module.Sources;
string[] files = new string [sfiles.Length];
for (int n=0; n<files.Length; n++)
files [n] = sfiles [n].FileName;
controller.NotifySourceFileUnloaded (files);
WriteDebuggerOutput (string.Format ("Module unloaded: {0}.\n", module.Name));
}
private void OnTargetExitedEvent (MD.Debugger debugger)
{
exited = true;
DispatchEvent (delegate {
controller.OnDebuggerOutput (false, "Target exited.\n");
DL.TargetEventArgs args = new DL.TargetEventArgs (DL.TargetEventType.TargetExited);
controller.OnTargetEvent (args);
});
}
void DispatchEvent (ST.WaitCallback eventCallback)
{
lock (eventQueue) {
eventQueue.Add (eventCallback);
ST.Monitor.PulseAll (eventQueue);
}
}
void EventDispatcher ()
{
while (true) {
ST.WaitCallback[] cbs;
lock (eventQueue) {
if (eventQueue.Count == 0)
ST.Monitor.Wait (eventQueue);
cbs = new ST.WaitCallback [eventQueue.Count];
eventQueue.CopyTo (cbs, 0);
eventQueue.Clear ();
}
foreach (ST.WaitCallback wc in cbs) {
try {
wc (null);
} catch (Exception ex) {
Console.WriteLine (ex);
}
}
}
}
string[] ToArgsArray (string str)
{
List<string> args = new List<string> ();
bool escaping = false;
StringBuilder currentArg = new StringBuilder ();
for (int n=0; n<str.Length; n++) {
char c = str [n];
if (c == '\\' && n + 1 < str.Length) {
if (!escaping || (escaping && (str[n+1] == '"' || str[n+1] == '\\')))
c = str [++n];
} else if (c == '"') {
escaping = !escaping;
continue;
} else if (c == ' ' && !escaping) {
args.Add (currentArg.ToString ());
currentArg = new StringBuilder ();
continue;
}
currentArg.Append (c);
}
if (currentArg.Length > 0)
args.Add (currentArg.ToString ());
return args.ToArray ();
}
}
}
|