Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
3.9 KiB

  1. #ifndef _SQ_DBGSERVER_H_
  2. #define _SQ_DBGSERVER_H_
  3. #define MAX_BP_PATH 512
  4. #define MAX_MSG_LEN 2049
  5. #if defined( _PS3 ) || defined( POSIX )
  6. #include <ctype.h>
  7. #include <unistd.h>
  8. #include <sys/socket.h>
  9. #include <netinet/in.h>
  10. #include <arpa/inet.h>
  11. #include "tier0/threadtools.h"
  12. typedef int SOCKET;
  13. #define INVALID_SOCKET -1
  14. #define SOCKET_ERROR -1
  15. #endif
  16. // Parts of squirrel do a #define of type, which leads to compile warnings/errors when STL files are included.
  17. // -- at least in VS 2013. Luckily we can temporarily #undef it and avoid the problem.
  18. // VALVE BUILD
  19. #undef type
  20. #include <set>
  21. #include <string>
  22. #include <vector>
  23. // Redefine type
  24. #define type SQ_TYPE
  25. /*
  26. see copyright notice in sqrdbg.h
  27. */
  28. #if !defined( _X360 ) && !defined( _PS3 ) && !defined( POSIX )
  29. #include <winsock.h>
  30. #endif
  31. typedef std::basic_string<SQChar> SQDBGString;
  32. inline bool dbg_less(const SQChar *x,const SQChar *y)
  33. {
  34. int n = 0;
  35. do {
  36. int xl = *x == '\\' ? '/' : tolower(*x);
  37. int yl = *y == '\\' ? '/' : tolower(*y);
  38. int diff = xl - yl;
  39. if(diff != 0)
  40. return diff > 0?true:false;
  41. x++; y++;
  42. }while(*x != 0 && *y != 0);
  43. return false;
  44. }
  45. struct BreakPoint{
  46. BreakPoint(){_line=0;}
  47. BreakPoint(int line, const SQChar *src){ _line = line; _src = src; }
  48. BreakPoint(const BreakPoint& bp){ _line = bp._line; _src=bp._src; }
  49. inline bool operator<(const BreakPoint& bp) const
  50. {
  51. if(_line<bp._line)
  52. return true;
  53. if(_line==bp._line){
  54. return dbg_less(_src.c_str(),bp._src.c_str());
  55. }
  56. return false;
  57. }
  58. int _line;
  59. SQDBGString _src;
  60. };
  61. struct Watch{
  62. Watch() { _id = 0; }
  63. Watch(int id,const SQChar *exp) { _id = id; _exp = exp; }
  64. Watch(const Watch &w) { _id = w._id; _exp = w._exp; }
  65. bool operator<(const Watch& w) const { return _id<w._id; }
  66. bool operator==(const Watch& w) const { return _id == w._id; }
  67. int _id;
  68. SQDBGString _exp;
  69. };
  70. typedef std::set<BreakPoint> BreakPointSet;
  71. typedef BreakPointSet::iterator BreakPointSetItor;
  72. typedef std::set<Watch> WatchSet;
  73. typedef WatchSet::iterator WatchSetItor;
  74. typedef std::vector<SQChar> SQCharVec;
  75. struct SQDbgServer{
  76. public:
  77. enum eDbgState{
  78. eDBG_Running,
  79. eDBG_StepOver,
  80. eDBG_StepInto,
  81. eDBG_StepReturn,
  82. eDBG_Suspended,
  83. eDBG_Disabled,
  84. };
  85. SQDbgServer(HSQUIRRELVM v);
  86. ~SQDbgServer();
  87. bool Init();
  88. bool IsConnected();
  89. //returns true if a message has been received
  90. bool WaitForClient();
  91. bool ReadMsg();
  92. void BusyWait();
  93. void Hook(int type,int line,const SQChar *src,const SQChar *func);
  94. void ParseMsg(const char *msg);
  95. bool ParseBreakpoint(const char *msg,BreakPoint &out);
  96. bool ParseWatch(const char *msg,Watch &out);
  97. bool ParseRemoveWatch(const char *msg,int &id);
  98. void Terminated();
  99. //
  100. void BreakExecution();
  101. void Send(const SQChar *s,...);
  102. void SendChunk(const SQChar *chunk);
  103. void Break(int line,const SQChar *src,const SQChar *type,const SQChar *error=NULL);
  104. void SerializeState();
  105. //COMMANDS
  106. void AddBreakpoint(BreakPoint &bp);
  107. void AddWatch(Watch &w);
  108. void RemoveWatch(int id);
  109. void RemoveBreakpoint(BreakPoint &bp);
  110. //
  111. void SetErrorHandlers();
  112. //XML RELATED STUFF///////////////////////
  113. #define MAX_NESTING 10
  114. struct XMLElementState {
  115. SQChar name[256];
  116. bool haschildren;
  117. };
  118. XMLElementState xmlstate[MAX_NESTING];
  119. int _xmlcurrentement;
  120. void BeginDocument() { _xmlcurrentement = -1; }
  121. void BeginElement(const SQChar *name);
  122. void Attribute(const SQChar *name, const SQChar *value);
  123. void EndElement(const SQChar *name);
  124. void EndDocument();
  125. const SQChar *escape_xml(const SQChar *x);
  126. //////////////////////////////////////////////
  127. HSQUIRRELVM _v;
  128. HSQOBJECT _debugroot;
  129. eDbgState _state;
  130. SOCKET _accept;
  131. SOCKET _endpoint;
  132. BreakPointSet _breakpoints;
  133. WatchSet _watches;
  134. int _recursionlevel;
  135. int _maxrecursion;
  136. int _nestedcalls;
  137. bool _ready;
  138. bool _autoupdate;
  139. HSQOBJECT _serializefunc;
  140. SQCharVec _scratchstring;
  141. };
  142. #ifdef _WIN32
  143. #define sqdbg_closesocket(x) closesocket((x))
  144. #else
  145. #define sqdbg_closesocket(x) close((x))
  146. #endif
  147. #endif //_SQ_DBGSERVER_H_