debug_interface.rst 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. .. _embedding_debug_interface:
  2. ===============
  3. Debug Interface
  4. ===============
  5. The squirrel VM exposes a very simple debug interface that allows to easily built a full
  6. featured debugger.
  7. Through the functions sq_setdebughook and sq_setnativedebughook is possible in fact to set a callback function that
  8. will be called every time the VM executes an new line of a script or if a function get
  9. called/returns. The callback will pass as argument the current line the current source and the
  10. current function name (if any).::
  11. SQUIRREL_API void sq_setdebughook(HSQUIRRELVM v);
  12. or ::
  13. SQUIRREL_API void sq_setnativedebughook(HSQUIRRELVM v,SQDEBUGHOOK hook);
  14. The following code shows how a debug hook could look like(obviously is possible to
  15. implement this function in C as well). ::
  16. function debughook(event_type,sourcefile,line,funcname)
  17. {
  18. local fname=funcname?funcname:"unknown";
  19. local srcfile=sourcefile?sourcefile:"unknown"
  20. switch (event_type) {
  21. case 'l': //called every line(that contains some code)
  22. ::print("LINE line [" + line + "] func [" + fname + "]");
  23. ::print("file [" + srcfile + "]\n");
  24. break;
  25. case 'c': //called when a function has been called
  26. ::print("LINE line [" + line + "] func [" + fname + "]");
  27. ::print("file [" + srcfile + "]\n");
  28. break;
  29. case 'r': //called when a function returns
  30. ::print("LINE line [" + line + "] func [" + fname + "]");
  31. ::print("file [" + srcfile + "]\n");
  32. break;
  33. }
  34. }
  35. The parameter *event_type* can be 'l' ,'c' or 'r' ; a hook with a 'l' event is called for each line that
  36. gets executed, 'c' every time a function gets called and 'r' every time a function returns.
  37. A full-featured debugger always allows displaying local variables and calls stack.
  38. The call stack information are retrieved through sq_getstackinfos()::
  39. SQInteger sq_stackinfos(HSQUIRRELVM v,SQInteger level,SQStackInfos *si);
  40. While the local variables info through sq_getlocal()::
  41. SQInteger sq_getlocal(HSQUIRRELVM v,SQUnsignedInteger level,SQUnsignedInteger nseq);
  42. In order to receive line callbacks the scripts have to be compiled with debug infos enabled
  43. this is done through sq_enabledebuginfo(); ::
  44. void sq_enabledebuginfo(HSQUIRRELVM v, SQInteger debuginfo);