DebugHelper.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2008 Novell, Inc.
  21. //
  22. // Authors:
  23. // Andreia Gaita ([email protected])
  24. //
  25. //#define DEBUG
  26. //#define TRACE
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Text;
  31. using System.IO;
  32. using System.Diagnostics;
  33. namespace System.Windows.Forms
  34. {
  35. internal class DebugHelper
  36. {
  37. static DebugHelper () {
  38. Debug.AutoFlush = true;
  39. }
  40. struct Data {
  41. public MethodBase method;
  42. public object[] args;
  43. public Data (MethodBase m, object[] a) {
  44. this.method = m;
  45. this.args = a;
  46. }
  47. }
  48. static Stack<Data> methods = new Stack<Data>();
  49. [Conditional("DEBUG")]
  50. internal static void DumpCallers () {
  51. StackTrace trace = new StackTrace(true);
  52. int count = trace.FrameCount;
  53. Debug.Indent ();
  54. for (int i = 1; i < count; i++) {
  55. StackFrame parentFrame = trace.GetFrame(i);
  56. MethodBase parentMethod = parentFrame.GetMethod();
  57. string file = parentFrame.GetFileName();
  58. if (file != null && file.Length > 1)
  59. file = file.Substring (file.LastIndexOf (Path.DirectorySeparatorChar) + 1);
  60. Debug.WriteLine(parentMethod.DeclaringType.Name + "." + parentMethod.Name +
  61. " at " + file + ":" + parentFrame.GetFileLineNumber()
  62. );
  63. }
  64. Debug.Unindent ();
  65. }
  66. [Conditional("DEBUG")]
  67. internal static void DumpCallers (int count) {
  68. StackTrace trace = new StackTrace(true);
  69. int c = (count > trace.FrameCount ? trace.FrameCount : count);
  70. Debug.Indent ();
  71. for (int i = 1; i < c; i++) {
  72. StackFrame parentFrame = trace.GetFrame(i);
  73. MethodBase parentMethod = parentFrame.GetMethod();
  74. string file = parentFrame.GetFileName();
  75. if (file != null && file.Length > 1)
  76. file = file.Substring (file.LastIndexOf (Path.DirectorySeparatorChar) + 1);
  77. Debug.WriteLine(parentMethod.DeclaringType.Name + "." + parentMethod.Name +
  78. " at " + file + ":" + parentFrame.GetFileLineNumber()
  79. );
  80. }
  81. Debug.Unindent ();
  82. }
  83. [Conditional("DEBUG")]
  84. internal static void Enter ()
  85. {
  86. StackTrace trace = new StackTrace();
  87. methods.Push (new Data (trace.GetFrame(1).GetMethod(), null));
  88. Print ();
  89. Debug.Indent ();
  90. }
  91. [Conditional("DEBUG")]
  92. internal static void Enter (object[] args)
  93. {
  94. StackTrace trace = new StackTrace();
  95. methods.Push (new Data (trace.GetFrame(1).GetMethod(), args));
  96. Print ();
  97. Debug.Indent ();
  98. }
  99. [Conditional("DEBUG")]
  100. internal static void Leave ()
  101. {
  102. if (methods.Count > 0) {
  103. methods.Pop ();
  104. Debug.Unindent ();
  105. }
  106. }
  107. [Conditional("DEBUG")]
  108. internal static void Print ()
  109. {
  110. if (methods.Count == 0)
  111. return;
  112. Data data = methods.Peek ();
  113. Debug.WriteLine (data.method.DeclaringType.Name + "." + data.method.Name);
  114. }
  115. [Conditional("DEBUG")]
  116. internal static void Print (int index)
  117. {
  118. if (methods.Count == 0 || methods.Count <= index || index < 0)
  119. return;
  120. Stack<Data> temp = new Stack<Data>(index-1);
  121. for (int i = 0; i < index; i++)
  122. temp.Push (methods.Pop ());
  123. Data data = methods.Peek ();
  124. for (int i = 0; i < temp.Count; i++)
  125. methods.Push (temp.Pop());
  126. temp = null;
  127. Debug.WriteLine (data.method.DeclaringType.Name + "." + data.method.Name);
  128. }
  129. [Conditional("DEBUG")]
  130. internal static void Print (string methodName, string parameterName)
  131. {
  132. if (methods.Count == 0)
  133. return;
  134. Stack<Data> temp = new Stack<Data>();
  135. Data data = methods.Peek ();
  136. bool foundit = false;
  137. for (int i = 0; i < methods.Count; i++)
  138. {
  139. data = methods.Peek ();
  140. if (data.method.Name.Equals (methodName)) {
  141. foundit = true;
  142. break;
  143. }
  144. temp.Push (methods.Pop ());
  145. }
  146. for (int i = 0; i < temp.Count; i++)
  147. methods.Push (temp.Pop());
  148. temp = null;
  149. if (!foundit)
  150. return;
  151. Debug.WriteLine (data.method.DeclaringType.Name + "." + data.method.Name);
  152. ParameterInfo[] pi = data.method.GetParameters ();
  153. for (int i = 0; i < pi.Length; i++) {
  154. if (pi[i].Name == parameterName) {
  155. Debug.Indent ();
  156. Debug.Write (parameterName + "=");
  157. if (pi[i].ParameterType == typeof(IntPtr))
  158. Debug.WriteLine (String.Format ("0x{0:x}", ((IntPtr)data.args[i]).ToInt32()));
  159. else
  160. Debug.WriteLine (data.args[i]);
  161. Debug.Unindent ();
  162. }
  163. }
  164. }
  165. [Conditional("DEBUG")]
  166. internal static void Print (string parameterName)
  167. {
  168. if (methods.Count == 0)
  169. return;
  170. Data data = methods.Peek ();
  171. ParameterInfo[] pi = data.method.GetParameters ();
  172. for (int i = 0; i < pi.Length; i++) {
  173. if (pi[i].Name == parameterName) {
  174. Debug.Indent ();
  175. Debug.Write (parameterName + "=");
  176. if (pi[i].ParameterType == typeof(IntPtr))
  177. Debug.WriteLine (String.Format ("0x{0:x}", data.args[i]));
  178. else
  179. Debug.WriteLine (data.args[i]);
  180. Debug.Unindent ();
  181. }
  182. }
  183. }
  184. [Conditional("DEBUG")]
  185. internal static void WriteLine (object arg)
  186. {
  187. Debug.WriteLine (arg);
  188. }
  189. [Conditional("DEBUG")]
  190. internal static void WriteLine (string format, params object[] arg)
  191. {
  192. Debug.WriteLine (String.Format (format, arg));
  193. }
  194. [Conditional("DEBUG")]
  195. internal static void WriteLine (string message)
  196. {
  197. Debug.WriteLine (message);
  198. }
  199. [Conditional("DEBUG")]
  200. internal static void Indent ()
  201. {
  202. Debug.Indent ();
  203. }
  204. [Conditional("DEBUG")]
  205. internal static void Unindent ()
  206. {
  207. Debug.Unindent ();
  208. }
  209. [Conditional("TRACE")]
  210. internal static void TraceWriteLine (string format, params object[] arg)
  211. {
  212. Debug.WriteLine (String.Format (format, arg));
  213. }
  214. [Conditional("TRACE")]
  215. internal static void TraceWriteLine (string message)
  216. {
  217. Debug.WriteLine (message);
  218. }
  219. }
  220. }