sqdbgserver.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #ifndef _SQ_DBGSERVER_H_
  2. #define _SQ_DBGSERVER_H_
  3. #define MAX_BP_PATH 512
  4. #define MAX_MSG_LEN 2049
  5. #include <set>
  6. #include <map>
  7. #include <string>
  8. #include <vector>
  9. #ifdef _WIN32
  10. # include <winsock.h>
  11. #else
  12. # include <unistd.h>
  13. # define SOCKET int
  14. # define INVALID_SOCKET (-1)
  15. #endif
  16. typedef std::basic_string<SQChar> SQDBGString;
  17. struct BreakPoint{
  18. BreakPoint(){_line=0;}
  19. BreakPoint(SQInteger line, const SQChar *src){ _line = line; _src = src; }
  20. BreakPoint(const BreakPoint& bp){ _line = bp._line; _src=bp._src; }
  21. bool operator<(const BreakPoint& bp) const
  22. {
  23. if(_line<bp._line)
  24. return true;
  25. if(_line==bp._line){
  26. if(_src<bp._src){
  27. return true;
  28. }
  29. return false;
  30. }
  31. return false;
  32. }
  33. bool operator==(const BreakPoint& other)
  34. {
  35. if(_line==other._line
  36. && (_src==other._src))
  37. return true;
  38. return false;
  39. }
  40. SQInteger _line;
  41. SQDBGString _src;
  42. };
  43. struct Watch{
  44. Watch() { _id = 0; }
  45. Watch(SQInteger id,const SQChar *exp) { _id = id; _exp = exp; }
  46. Watch(const Watch &w) { _id = w._id; _exp = w._exp; }
  47. bool operator<(const Watch& w) const { return _id<w._id; }
  48. bool operator==(const Watch& w) const { return _id == w._id; }
  49. SQInteger _id;
  50. SQDBGString _exp;
  51. };
  52. struct VMState {
  53. VMState() { _nestedcalls = 0;}
  54. SQInteger _nestedcalls;
  55. };
  56. typedef std::map<HSQUIRRELVM,VMState*> VMStateMap;
  57. typedef std::set<BreakPoint> BreakPointSet;
  58. typedef BreakPointSet::iterator BreakPointSetItor;
  59. typedef std::set<Watch> WatchSet;
  60. typedef WatchSet::iterator WatchSetItor;
  61. typedef std::vector<SQChar> SQCharVec;
  62. struct SQDbgServer{
  63. public:
  64. enum eDbgState{
  65. eDBG_Running,
  66. eDBG_StepOver,
  67. eDBG_StepInto,
  68. eDBG_StepReturn,
  69. eDBG_Suspended,
  70. eDBG_Disabled,
  71. };
  72. SQDbgServer(HSQUIRRELVM v);
  73. ~SQDbgServer();
  74. bool Init();
  75. //returns true if a message has been received
  76. bool WaitForClient();
  77. bool ReadMsg();
  78. void BusyWait();
  79. void Hook(HSQUIRRELVM v,SQInteger type,SQInteger line,const SQChar *src,const SQChar *func);
  80. void ParseMsg(const char *msg);
  81. bool ParseBreakpoint(const char *msg,BreakPoint &out);
  82. bool ParseWatch(const char *msg,Watch &out);
  83. bool ParseRemoveWatch(const char *msg,SQInteger &id);
  84. void Terminated();
  85. //
  86. void BreakExecution();
  87. void Send(const SQChar *s,...);
  88. void SendChunk(const SQChar *chunk);
  89. void Break(HSQUIRRELVM v,SQInteger line,const SQChar *src,const SQChar *type,const SQChar *error=NULL);
  90. void SerializeState(HSQUIRRELVM v);
  91. //COMMANDS
  92. void AddBreakpoint(BreakPoint &bp);
  93. void AddWatch(Watch &w);
  94. void RemoveWatch(SQInteger id);
  95. void RemoveBreakpoint(BreakPoint &bp);
  96. //
  97. void SetErrorHandlers(HSQUIRRELVM v);
  98. VMState *GetVMState(HSQUIRRELVM v);
  99. //XML RELATED STUFF///////////////////////
  100. #define MAX_NESTING 10
  101. struct XMLElementState {
  102. SQChar name[256];
  103. bool haschildren;
  104. };
  105. XMLElementState xmlstate[MAX_NESTING];
  106. SQInteger _xmlcurrentement;
  107. void BeginDocument();
  108. void BeginElement(const SQChar *name);
  109. void Attribute(const SQChar *name, const SQChar *value);
  110. void EndElement(const SQChar *name);
  111. void EndDocument();
  112. const SQChar *escape_xml(const SQChar *x);
  113. //////////////////////////////////////////////
  114. HSQUIRRELVM _v;
  115. HSQOBJECT _debugroot;
  116. eDbgState _state;
  117. SOCKET _accept;
  118. SOCKET _endpoint;
  119. BreakPointSet _breakpoints;
  120. WatchSet _watches;
  121. //int _recursionlevel;
  122. //int _maxrecursion;
  123. bool _ready;
  124. bool _autoupdate;
  125. HSQOBJECT _serializefunc;
  126. SQCharVec _scratchstring;
  127. SQInteger _line;
  128. SQDBGString _src;
  129. SQDBGString _break_type;
  130. VMStateMap _vmstate;
  131. };
  132. #ifdef _WIN32
  133. #define sqdbg_closesocket(x) closesocket((x))
  134. #else
  135. #define sqdbg_closesocket(x) close((x))
  136. #endif
  137. #endif //_SQ_DBGSERVER_H_