The Labs \ Source Viewer \ SSCLI \ System.Diagnostics \ Debugger

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. // The Debugger class is a part of the System.Diagnostics package
  16. // and is used for communicating with a debugger.
  17. namespace System.Diagnostics
  18. {
  19.     using System;
  20.     using System.IO;
  21.     using System.Collections;
  22.     using System.Reflection;
  23.     using System.Runtime.CompilerServices;
  24.     using System.Security;
  25.     using System.Security.Permissions;
  26.    
  27.    
  28.     // No data, does not need to be marked with the serializable attribute
  29.     [System.Runtime.InteropServices.ComVisible(true)]
  30.     public sealed class Debugger
  31.     {
  32.        
  33.         // Break causes a breakpoint to be signalled to an attached debugger. If no debugger
  34.         // is attached, the user is asked if he wants to attach a debugger. If yes, then the
  35.         // debugger is launched.
  36.         public static void Break()
  37.         {
  38.             if (!IsDebuggerAttached()) {
  39.                 // Try and demand UnmanagedCodePermission. This is done in a try block because if this
  40.                 // fails we want to be able to silently eat the exception and just return so
  41.                 // that the call to Break does not possibly cause an unhandled exception.
  42.                 // The idea here is that partially trusted code shouldn't be able to launch a debugger
  43.                 // without the user going through Watson.
  44.                 try {
  45.                     new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
  46.                 }
  47.                
  48.                 // If we enter this block, we do not have permission to break into the debugger
  49.                 // and so we just return.
  50.                 catch (SecurityException) {
  51.                     return;
  52.                 }
  53.             }
  54.            
  55.             // Causing a break is now allowed.
  56.             BreakInternal();
  57.         }
  58.        
  59.         static void BreakCanThrow()
  60.         {
  61.             if (!IsDebuggerAttached()) {
  62.                 new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
  63.             }
  64.            
  65.             // Causing a break is now allowed.
  66.             BreakInternal();
  67.         }
  68.        
  69.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  70.         private static extern void BreakInternal();
  71.        
  72.         // Launch launches & attaches a debugger to the process. If a debugger is already attached,
  73.         // nothing happens.
  74.         //
  75.         public static bool Launch()
  76.         {
  77.             if (IsDebuggerAttached())
  78.                 return (true);
  79.            
  80.             // Try and demand UnmanagedCodePermission. This is done in a try block because if this
  81.             // fails we want to be able to silently eat the exception and just return so
  82.             // that the call to Break does not possibly cause an unhandled exception.
  83.             // The idea here is that partially trusted code shouldn't be able to launch a debugger
  84.             // without the user going through Watson.
  85.             try {
  86.                 new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Demand();
  87.             }
  88.            
  89.             // If we enter this block, we do not have permission to break into the debugger
  90.             // and so we just return.
  91.             catch (SecurityException) {
  92.                 return (false);
  93.             }
  94.            
  95.             // Causing the debugger to launch is now allowed.
  96.             return (LaunchInternal());
  97.         }
  98.        
  99.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  100.         private static extern bool LaunchInternal();
  101.        
  102.         // Returns whether or not a debugger is attached to the process.
  103.         //
  104.         public static bool IsAttached {
  105.             get { return IsDebuggerAttached(); }
  106.         }
  107.        
  108.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  109.         private static extern bool IsDebuggerAttached();
  110.        
  111.         // Constants representing the importance level of messages to be logged.
  112.         //
  113.         // An attached debugger can enable or disable which messages will
  114.         // actually be reported to the user through the COM+ debugger
  115.         // services API. This info is communicated to the runtime so only
  116.         // desired events are actually reported to the debugger.
  117.         //
  118.         // Constant representing the default category
  119.         public static readonly string DefaultCategory = null;
  120.        
  121.         // Posts a message for the attached debugger. If there is no
  122.         // debugger attached, has no effect. The debugger may or may not
  123.         // report the message depending on its settings.
  124.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  125.         public static extern void Log(int level, string category, string message);
  126.        
  127.         // Checks to see if an attached debugger has logging enabled
  128.         //
  129.         [MethodImplAttribute(MethodImplOptions.InternalCall)]
  130.         public static extern bool IsLogging();
  131.        
  132.     }
  133. }

Developer Fusion