2
0

Debugger.hx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 full paths of the set of source files known to the debugger.
  177. * This is a copy of the original array and could be quite large.
  178. * It is possible that this set will be empty, in which case the full paths are not known.
  179. * The index of these files matches the index from "getFiles", so the full path for
  180. * a given short path can be calculated.
  181. *
  182. * @return the known full paths of the set of source files
  183. **/
  184. public static function getFilesFullPath() : Array<String>
  185. {
  186. return untyped __global__.__hxcpp_dbg_getFilesFullPath();
  187. }
  188. /**
  189. * Returns the set of class names of all classes known to the debugger.
  190. * This is a copy of the original array and could be quite large. The
  191. * caller should cache this value to avoid multiple copies needing to be
  192. * made.
  193. *
  194. * @return the set of class names of all classes known to the debugger.
  195. **/
  196. public static function getClasses() : Array<String>
  197. {
  198. return untyped __global__.__hxcpp_dbg_getClasses();
  199. }
  200. /**
  201. * Returns a ThreadInfo object describing every thread that existed at the
  202. * moment that the call was made, except for the debugger thread.
  203. **/
  204. public static function getThreadInfos() : Array<ThreadInfo>
  205. {
  206. return untyped __global__.__hxcpp_dbg_getThreadInfos();
  207. }
  208. /**
  209. * Returns a ThreadInfo object describing a single thread, or null if
  210. * there is no such thread or the thread queried about was the debugger
  211. * thread and unsafe was not true.
  212. **/
  213. public static function getThreadInfo(threadNumber : Int,
  214. unsafe : Bool) : ThreadInfo
  215. {
  216. return untyped __global__.__hxcpp_dbg_getThreadInfo
  217. (threadNumber, unsafe);
  218. }
  219. /**
  220. * Adds a new file:line breakpoint. The breakpoint number of the newly
  221. * added breakpoint is returned.
  222. **/
  223. public static function addFileLineBreakpoint(file : String,
  224. line : Int) : Int
  225. {
  226. return untyped __global__.__hxcpp_dbg_addFileLineBreakpoint
  227. (file, line);
  228. }
  229. /**
  230. * Adds a new class:function breakpoint. The breakpoint number of the
  231. * newly added breakpoint is returned.
  232. **/
  233. public static function addClassFunctionBreakpoint(className : String,
  234. functionName : String) : Int
  235. {
  236. return untyped __global__.__hxcpp_dbg_addClassFunctionBreakpoint
  237. (className, functionName);
  238. }
  239. /**
  240. * Deletes a breakpoint, or all breakpoints.
  241. **/
  242. public static function deleteBreakpoint(number : Null<Int>)
  243. {
  244. if (number == null) {
  245. untyped __global__.__hxcpp_dbg_deleteAllBreakpoints();
  246. }
  247. else {
  248. untyped __global__.__hxcpp_dbg_deleteBreakpoint
  249. (cast (number, Int));
  250. }
  251. }
  252. /**
  253. * Breaks all threads except the debugger thread (which should be the same
  254. * as the calling thread!).
  255. *
  256. * If [wait] is true, waits up to 2 seconds for all threads to be broken.
  257. * Threads which are in blocking system calls and cannot break after 2
  258. * seconds remain running when this function returns.
  259. **/
  260. public static function breakNow(wait : Bool = true)
  261. {
  262. untyped __global__.__hxcpp_dbg_breakNow(wait);
  263. }
  264. /**
  265. * Continue execution of all stopped threads. If specialThreadNumber
  266. * is a valid thread number, then it will be continued past
  267. * [continueCount] breakpoints instead of just 1 like all of the other
  268. * threads.
  269. **/
  270. public static function continueThreads(specialThreadNumber : Int,
  271. continueCount : Int)
  272. {
  273. untyped __global__.__hxcpp_dbg_continueThreads
  274. (specialThreadNumber, continueCount);
  275. }
  276. /**
  277. * Single steps the given thread.
  278. **/
  279. public static function stepThread(threadNumber : Int,
  280. stepType : Int,
  281. stepCount : Int = 1)
  282. {
  283. untyped __global__.__hxcpp_dbg_stepThread
  284. (threadNumber, stepType, stepCount);
  285. }
  286. /**
  287. * Returns the list of local variables (including "this", function
  288. * arguments, and local variables) visible to the given thread at the
  289. * given stack frame.
  290. *
  291. * Returns a list with a single entry, THREAD_NOT_STOPPED, if the
  292. * thread is not stopped and thus variables cannot be fetched and
  293. * unsafe is not true.
  294. *
  295. * @return the list of local variables (including "this", function
  296. * arguments, and local variables) visible to the given thread at
  297. * the given stack frame.
  298. **/
  299. public static function getStackVariables(threadNumber : Int,
  300. stackFrameNumber : Int,
  301. unsafe : Bool) : Array<String>
  302. {
  303. return untyped __global__.__hxcpp_dbg_getStackVariables
  304. (threadNumber, stackFrameNumber, unsafe, THREAD_NOT_STOPPED);
  305. }
  306. /**
  307. * Returns the value of a stack variable, or NONEXISTENT_VALUE if the
  308. * requested value does not exist. If the thread is actively running
  309. * and unsafe is not true, returns THREAD_NOT_STOPPED.
  310. **/
  311. public static function getStackVariableValue(threadNumber : Int,
  312. stackFrameNumber : Int,
  313. name : String,
  314. unsafe : Bool) : Dynamic
  315. {
  316. return untyped __global__.__hxcpp_dbg_getStackVariableValue
  317. (threadNumber, stackFrameNumber, name, unsafe, NONEXISTENT_VALUE,
  318. THREAD_NOT_STOPPED);
  319. }
  320. /**
  321. * Sets the value of a stack variable and returns that value. If the
  322. * variable does not exist, on the stack, this function returns
  323. * NONEXISTENT_VALUE. If the thread is actively running and unsafe is not
  324. * true, returns THREAD_NOT_STOPPED, and the value is not set.
  325. **/
  326. public static function setStackVariableValue(threadNumber : Int,
  327. stackFrameNumber : Int,
  328. name : String,
  329. value : Dynamic,
  330. unsafe : Bool) : Dynamic
  331. {
  332. return untyped __global__.__hxcpp_dbg_setStackVariableValue
  333. (threadNumber, stackFrameNumber, name, value, unsafe,
  334. NONEXISTENT_VALUE, THREAD_NOT_STOPPED);
  335. }
  336. // The hxcpp runtime calls back through these functions to create Haxe
  337. // objects as needed, which allows the C++ implementation code to create
  338. // Haxe objects without having to actually know the structure of those
  339. // objects
  340. private static function __init__()
  341. {
  342. untyped __global__.__hxcpp_dbg_setNewParameterFunction
  343. (
  344. function (name : String, value : Dynamic) : Dynamic {
  345. return new Parameter(name, value);
  346. }
  347. );
  348. untyped __global__.__hxcpp_dbg_setNewStackFrameFunction
  349. (
  350. function (fileName : String, lineNumber : Int,
  351. className : String, functionName : String)
  352. {
  353. return new StackFrame(fileName, lineNumber,
  354. className, functionName);
  355. }
  356. );
  357. untyped __global__.__hxcpp_dbg_setNewThreadInfoFunction
  358. (
  359. function (number : Int, status : Int, breakpoint : Int,
  360. criticalErrorDescription : String) : Dynamic {
  361. return new ThreadInfo(number, status, breakpoint,
  362. criticalErrorDescription);
  363. }
  364. );
  365. untyped __global__.__hxcpp_dbg_setAddParameterToStackFrameFunction
  366. (
  367. function(inStackFrame : Dynamic, inParameter : Dynamic) {
  368. var stackFrame : StackFrame = cast inStackFrame;
  369. var parameter : Parameter = cast inParameter;
  370. stackFrame.parameters.push(parameter);
  371. }
  372. );
  373. untyped __global__.__hxcpp_dbg_setAddStackFrameToThreadInfoFunction
  374. (
  375. function(inThreadInfo : Dynamic, inStackFrame : Dynamic) {
  376. var threadInfo : ThreadInfo = cast inThreadInfo;
  377. var stackFrame : StackFrame = cast inStackFrame;
  378. threadInfo.stack.push(stackFrame);
  379. }
  380. );
  381. }
  382. }