Debugger.hx 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * Copyright (C)2005-2012 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package cpp.vm;
  23. /**
  24. * Parameter describes a function parameter. Instances of this class are
  25. * embedded in stack frame objects to describe the function parameters that
  26. * were used in the invocation of the function that defines that stack frame.
  27. **/
  28. class Parameter
  29. {
  30. public var name(default, null) : String;
  31. public var value(default, null) : Dynamic;
  32. public function new(name : String, value : Dynamic)
  33. {
  34. this.name = name;
  35. this.value = value;
  36. }
  37. }
  38. /**
  39. * StackFrame describes one call stack frame.
  40. **/
  41. class StackFrame
  42. {
  43. public var fileName(default, null) : String;
  44. public var lineNumber(default, null) : Int;
  45. public var className(default, null) : String;
  46. public var functionName(default, null) : String;
  47. public var parameters(default, null) : Array<Parameter>;
  48. public function new(fileName : String, lineNumber : Int,
  49. className : String, functionName : String)
  50. {
  51. this.fileName = fileName;
  52. this.lineNumber = lineNumber;
  53. this.className = className;
  54. this.functionName = functionName;
  55. this.parameters = new Array<Parameter>();
  56. }
  57. }
  58. /**
  59. * ThreadInfo describes the state of a single thread.
  60. **/
  61. class ThreadInfo
  62. {
  63. public static inline var STATUS_RUNNING = 1;
  64. public static inline var STATUS_STOPPED_BREAK_IMMEDIATE = 2;
  65. public static inline var STATUS_STOPPED_BREAKPOINT = 3;
  66. public static inline var STATUS_STOPPED_UNCAUGHT_EXCEPTION = 4;
  67. public static inline var STATUS_STOPPED_CRITICAL_ERROR = 5;
  68. // 0 is never a valid thread number
  69. public var number(default, null) : Int;
  70. public var status(default, null) : Int;
  71. // If status is "stopped breakpoint", this is the breakpoint number
  72. public var breakpoint(default, null) : Int;
  73. // If status is "critical error", this describes the error
  74. public var criticalErrorDescription(default, null) : String;
  75. // Stack will be listed with the lowest frame first
  76. public var stack(default, null) : Array<StackFrame>;
  77. public function new(number : Int, status : Int, breakpoint : Int = -1,
  78. criticalErrorDescription : String = null)
  79. {
  80. this.number = number;
  81. this.status = status;
  82. this.breakpoint = breakpoint;
  83. this.criticalErrorDescription = criticalErrorDescription;
  84. this.stack = new Array<StackFrame>();
  85. }
  86. }
  87. /**
  88. * This class wraps the hxcpp C++ implementation to provide a Haxe interface
  89. * to the low level debugging features
  90. **/
  91. class Debugger
  92. {
  93. public static inline var THREAD_CREATED = 1;
  94. public static inline var THREAD_TERMINATED = 2;
  95. public static inline var THREAD_STARTED = 3;
  96. public static inline var THREAD_STOPPED = 4;
  97. public static inline var STEP_INTO = 1;
  98. public static inline var STEP_OVER = 2;
  99. public static inline var STEP_OUT = 3;
  100. // This tagging value is returned by getStackVariableValue() and
  101. // setStackVariableValue if the requested value does not exist at the
  102. // requested stack frame
  103. public static var NONEXISTENT_VALUE = new String("NONEXISTENT_VALUE");
  104. // This tagging value is returned by getStackVariableValue and
  105. // setStackVariableValue if the stack variable that is being set is on a
  106. // thread that is running, in which case the set does not take place.
  107. public static var THREAD_NOT_STOPPED = new String("THREAD_NOT_STOPPED");
  108. /**
  109. * Sets the handler callback to be made when asynchronous events occur,
  110. * specifically, when threads are created, terminated, started, or
  111. * stopped. The calling thread becomes the "debugger" thread, which means
  112. * that it will be discluded from any breakpoints and will not be reported
  113. * on by any thread reporting requests.
  114. *
  115. * Be aware that this callback is made asynchronously and possibly by
  116. * multiple threads simultaneously.
  117. *
  118. * Setting this to null prevents further callbacks.
  119. *
  120. * Throws a string exception if the program does not support debugging
  121. * because it was not compiled with the HXCPP_DEBUGGER flag set.
  122. *
  123. * @param handler is a function that will be called back by asynchronous
  124. * thread events. Note that this function is called directly from
  125. * the thread experiencing the event and the handler should return
  126. * quickly to avoid blocking the calling thread unnecessarily.
  127. * The paramaters to handler are:
  128. * - threadNumber, the thread number of the event
  129. * - event, one of THREAD_CREATED, THREAD_TERMINATED,
  130. * THREAD_STARTED, or THREAD_STOPPED
  131. * - className, the class name at which the thread is stopped,
  132. * undefined if event is not THREAD_STOPPED
  133. * - functionName, the function name at which the thread is
  134. * stopped, undefined if event is not THREAD_STOPPED
  135. * - fileName, the file name at which the thread is stopped,
  136. * undefined if event is not THREAD_STOPPED
  137. * - lineNumber, the line number at which the thread is stopped,
  138. * undefined if event is not THREAD_STOPPED
  139. **/
  140. public static function setEventNotificationHandler(
  141. handler : Int -> Int -> String -> String -> String -> Int -> Void)
  142. {
  143. untyped __global__.__hxcpp_dbg_setEventNotificationHandler(handler);
  144. }
  145. /**
  146. * This can be called to turn off (and then back on) all stopping of
  147. * debugged threads temporarily. It should only be used by classes that
  148. * actually implement the debugger to hide themselves from the debugger as
  149. * necessary.
  150. **/
  151. public static function enableCurrentThreadDebugging(enabled : Bool)
  152. {
  153. untyped __global__.__hxcpp_dbg_enableCurrentThreadDebugging(enabled);
  154. }
  155. /**
  156. * Returns the thread number of the calling thread.
  157. *
  158. * @return the thread number of the calling thread.
  159. **/
  160. public static function getCurrentThreadNumber() : Int
  161. {
  162. return untyped __global__.__hxcpp_dbg_getCurrentThreadNumber();
  163. }
  164. /**
  165. * Returns the set of source files known to the debugger. This is a copy
  166. * of the original array and could be quite large. The caller should
  167. * cache this value to avoid multiple copies needing to be made.
  168. *
  169. * @return the set of source files known to the debugger.
  170. **/
  171. public static function getFiles() : Array<String>
  172. {
  173. return untyped __global__.__hxcpp_dbg_getFiles();
  174. }
  175. /**
  176. * Returns the set of class names of all classes known to the debugger.
  177. * This is a copy of the original array and could be quite large. The
  178. * caller should cache this value to avoid multiple copies needing to be
  179. * made.
  180. *
  181. * @return the set of class names of all classes known to the debugger.
  182. **/
  183. public static function getClasses() : Array<String>
  184. {
  185. return untyped __global__.__hxcpp_dbg_getClasses();
  186. }
  187. /**
  188. * Returns a ThreadInfo object describing every thread that existed at the
  189. * moment that the call was made, except for the debugger thread.
  190. **/
  191. public static function getThreadInfos() : Array<ThreadInfo>
  192. {
  193. return untyped __global__.__hxcpp_dbg_getThreadInfos();
  194. }
  195. /**
  196. * Returns a ThreadInfo object describing a single thread, or null if
  197. * there is no such thread or the thread queried about was the debugger
  198. * thread and unsafe was not true.
  199. **/
  200. public static function getThreadInfo(threadNumber : Int,
  201. unsafe : Bool) : ThreadInfo
  202. {
  203. return untyped __global__.__hxcpp_dbg_getThreadInfo
  204. (threadNumber, unsafe);
  205. }
  206. /**
  207. * Adds a new file:line breakpoint. The breakpoint number of the newly
  208. * added breakpoint is returned.
  209. **/
  210. public static function addFileLineBreakpoint(file : String,
  211. line : Int) : Int
  212. {
  213. return untyped __global__.__hxcpp_dbg_addFileLineBreakpoint
  214. (file, line);
  215. }
  216. /**
  217. * Adds a new class:function breakpoint. The breakpoint number of the
  218. * newly added breakpoint is returned.
  219. **/
  220. public static function addClassFunctionBreakpoint(className : String,
  221. functionName : String) : Int
  222. {
  223. return untyped __global__.__hxcpp_dbg_addClassFunctionBreakpoint
  224. (className, functionName);
  225. }
  226. /**
  227. * Deletes a breakpoint, or all breakpoints.
  228. **/
  229. public static function deleteBreakpoint(number : Null<Int>)
  230. {
  231. if (number == null) {
  232. untyped __global__.__hxcpp_dbg_deleteAllBreakpoints();
  233. }
  234. else {
  235. untyped __global__.__hxcpp_dbg_deleteBreakpoint
  236. (cast (number, Int));
  237. }
  238. }
  239. /**
  240. * Breaks all threads except the debugger thread (which should be the same
  241. * as the calling thread!).
  242. *
  243. * If [wait] is true, waits up to 2 seconds for all threads to be broken.
  244. * Threads which are in blocking system calls and cannot break after 2
  245. * seconds remain running when this function returns.
  246. **/
  247. public static function breakNow(wait : Bool = true)
  248. {
  249. untyped __global__.__hxcpp_dbg_breakNow(wait);
  250. }
  251. /**
  252. * Continue execution of all stopped threads. If specialThreadNumber
  253. * is a valid thread number, then it will be continued past
  254. * [continueCount] breakpoints instead of just 1 like all of the other
  255. * threads.
  256. **/
  257. public static function continueThreads(specialThreadNumber : Int,
  258. continueCount : Int)
  259. {
  260. untyped __global__.__hxcpp_dbg_continueThreads
  261. (specialThreadNumber, continueCount);
  262. }
  263. /**
  264. * Single steps the given thread.
  265. **/
  266. public static function stepThread(threadNumber : Int,
  267. stepType : Int,
  268. stepCount : Int = 1)
  269. {
  270. untyped __global__.__hxcpp_dbg_stepThread
  271. (threadNumber, stepType, stepCount);
  272. }
  273. /**
  274. * Returns the list of local variables (including "this", function
  275. * arguments, and local variables) visible to the given thread at the
  276. * given stack frame.
  277. *
  278. * Returns a list with a single entry, THREAD_NOT_STOPPED, if the
  279. * thread is not stopped and thus variables cannot be fetched and
  280. * unsafe is not true.
  281. *
  282. * @return the list of local variables (including "this", function
  283. * arguments, and local variables) visible to the given thread at
  284. * the given stack frame.
  285. **/
  286. public static function getStackVariables(threadNumber : Int,
  287. stackFrameNumber : Int,
  288. unsafe : Bool) : Array<String>
  289. {
  290. return untyped __global__.__hxcpp_dbg_getStackVariables
  291. (threadNumber, stackFrameNumber, unsafe, THREAD_NOT_STOPPED);
  292. }
  293. /**
  294. * Returns the value of a stack variable, or NONEXISTENT_VALUE if the
  295. * requested value does not exist. If the thread is actively running
  296. * and unsafe is not true, returns THREAD_NOT_STOPPED.
  297. **/
  298. public static function getStackVariableValue(threadNumber : Int,
  299. stackFrameNumber : Int,
  300. name : String,
  301. unsafe : Bool) : Dynamic
  302. {
  303. return untyped __global__.__hxcpp_dbg_getStackVariableValue
  304. (threadNumber, stackFrameNumber, name, unsafe, NONEXISTENT_VALUE,
  305. THREAD_NOT_STOPPED);
  306. }
  307. /**
  308. * Sets the value of a stack variable and returns that value. If the
  309. * variable does not exist, on the stack, this function returns
  310. * NONEXISTENT_VALUE. If the thread is actively running and unsafe is not
  311. * true, returns THREAD_NOT_STOPPED, and the value is not set.
  312. **/
  313. public static function setStackVariableValue(threadNumber : Int,
  314. stackFrameNumber : Int,
  315. name : String,
  316. value : Dynamic,
  317. unsafe : Bool) : Dynamic
  318. {
  319. return untyped __global__.__hxcpp_dbg_setStackVariableValue
  320. (threadNumber, stackFrameNumber, name, value, unsafe,
  321. NONEXISTENT_VALUE, THREAD_NOT_STOPPED);
  322. }
  323. // The hxcpp runtime calls back through these functions to create Haxe
  324. // objects as needed, which allows the C++ implementation code to create
  325. // Haxe objects without having to actually know the structure of those
  326. // objects
  327. private static function __init__()
  328. {
  329. untyped __global__.__hxcpp_dbg_setNewParameterFunction
  330. (
  331. function (name : String, value : Dynamic) : Dynamic {
  332. return new Parameter(name, value);
  333. }
  334. );
  335. untyped __global__.__hxcpp_dbg_setNewStackFrameFunction
  336. (
  337. function (fileName : String, lineNumber : Int,
  338. className : String, functionName : String)
  339. {
  340. return new StackFrame(fileName, lineNumber,
  341. className, functionName);
  342. }
  343. );
  344. untyped __global__.__hxcpp_dbg_setNewThreadInfoFunction
  345. (
  346. function (number : Int, status : Int, breakpoint : Int,
  347. criticalErrorDescription : String) : Dynamic {
  348. return new ThreadInfo(number, status, breakpoint,
  349. criticalErrorDescription);
  350. }
  351. );
  352. untyped __global__.__hxcpp_dbg_setAddParameterToStackFrameFunction
  353. (
  354. function(inStackFrame : Dynamic, inParameter : Dynamic) {
  355. var stackFrame : StackFrame = cast inStackFrame;
  356. var parameter : Parameter = cast inParameter;
  357. stackFrame.parameters.push(parameter);
  358. }
  359. );
  360. untyped __global__.__hxcpp_dbg_setAddStackFrameToThreadInfoFunction
  361. (
  362. function(inThreadInfo : Dynamic, inStackFrame : Dynamic) {
  363. var threadInfo : ThreadInfo = cast inThreadInfo;
  364. var stackFrame : StackFrame = cast inStackFrame;
  365. threadInfo.stack.push(stackFrame);
  366. }
  367. );
  368. }
  369. }