Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DebuggingServer.h
Go to the documentation of this file.
1 /* Copyright (C) 2013 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_DEBUGGINGSERVER
19 #define INCLUDED_DEBUGGINGSERVER
20 
22 #include "ScriptInterface.h"
23 
25 
26 class CBreakPoint;
27 class CThreadDebugger;
28 
31 
33 {
34 public:
37 
38  /** @brief Register a new ScriptInerface for debugging the scripts it executes
39  *
40  * @param name A name for the ScriptInterface (will be sent to the debugging client an probably displayed to the user)
41  * @param pScriptInterface A pointer to the ScriptInterface. This pointer must stay valid until UnRegisterScriptInterface ist called!
42  */
43  void RegisterScriptinterface(std::string name, ScriptInterface* pScriptInterface);
44 
45  /** @brief Unregister a ScriptInerface that was previously registered using RegisterScriptinterface.
46  *
47  * @param pScriptInterface A pointer to the ScriptInterface
48  */
49  void UnRegisterScriptinterface(ScriptInterface* pScriptInterface);
50 
51 
52  // Mongoose callback when request comes from a client
53  void* MgDebuggingServerCallback(mg_event event, struct mg_connection *conn, const struct mg_request_info *request_info);
54 
55  /** @brief Aquire exclusive read and write access to the list of breakpoints.
56  *
57  * @param breakPoints A pointer to the list storing all breakpoints.
58  *
59  * @return A number you need to pass to ReleaseBreakPointAccess().
60  *
61  * Make sure to call ReleaseBreakPointAccess after you don't need access any longer.
62  * Using this function you get exclusive access and other threads won't be able to access the breakpoints until you call ReleaseBreakPointAccess!
63  */
64  double AquireBreakPointAccess(std::list<CBreakPoint>** breakPoints);
65 
66  /** @brief See AquireBreakPointAccess(). You must not access the pointer returend by AquireBreakPointAccess() any longer after you call this function.
67  *
68  * @param breakPointsLockID The number you got when aquiring the access. It's used to make sure that this function never gets
69  * used by the wrong thread.
70  */
71  void ReleaseBreakPointAccess(double breakPointsLockID);
72 
73 
74  /// Called from multiple Mongoose threads and multiple ScriptInterface threads
77  // Should other threads be stopped as soon as possible after a breakpoint is triggered in a thread
79  // Should the debugger break on any JS-Exception? If set to false, it will only break when the exceptions text is "Breakpoint".
81  void SetBreakRequestedByThread(bool Enabled);
82  void SetBreakRequestedByUser(bool Enabled);
83 
84 private:
85  static const char* header400;
86 
87  /// Webserver helper function (can be called by multiple mongooser threads)
88  bool GetWebArgs(struct mg_connection *conn, const struct mg_request_info* request_info, std::string argName, uint& arg);
89  bool GetWebArgs(struct mg_connection *conn, const struct mg_request_info* request_info, std::string argName, std::string& arg);
90 
91  /// Functions that are made available via http (can be called by multiple mongoose threads)
92  void GetThreadDebuggerStatus(std::stringstream& response);
93  void ToggleBreakPoint(std::string filename, uint line);
94  void GetAllCallstacks(std::stringstream& response);
95  void GetStackFrameData(std::stringstream& response, uint nestingLevel, uint threadDebuggerID, STACK_INFO stackInfoKind);
96  bool SetNextDbgCmd(uint threadDebuggerID, DBGCMD dbgCmd);
97  void SetSettingSimultaneousThreadBreak(bool Enabled);
98  void SetSettingBreakOnException(bool Enabled);
99 
100  /** @brief Returns a list of the full vfs paths to all files with the extension .js found in the vfs root
101  *
102  * @param response This will contain the list as JSON array.
103  */
104  void EnumVfsJSFiles(std::stringstream& response);
105 
106  /** @brief Get the content of a .js file loaded into vfs
107  *
108  * @param filename A full vfs path (as returned by EnumVfsJSFiles).
109  * @param response This will contain the contents of the requested file.
110  */
111  void GetFile(std::string filename, std::stringstream& response);
112 
113  /// Shared between multiple mongoose threads
114 
115 
118 
119  /// Shared between multiple scriptinterface threads
121 
122  /// Shared between multiple scriptinerface threads and multiple mongoose threads
123  std::list<CThreadDebugger*> m_ThreadDebuggers;
124 
125  // The CThreadDebuggers will check this value and break the thread if it's true.
126  // This only works for JS code, so C++ code will not break until it executes JS-code again.
129 
130  // The breakpoint is uniquely identified using filename an line-number.
131  // Since the filename is the whole vfs path it should really be unique.
132  std::list<CBreakPoint> m_BreakPoints;
133 
134  /// Used for controlling access to m_BreakPoints
137 
138  /// Mutexes used to ensure thread-safety. Currently we just use one Mutex (m_Mutex) and if we detect possible sources
139  /// of deadlocks, we use the second mutex for some members to avoid it.
142 
143  /// Not important for this class' thread-safety
144  void EnableHTTP();
146 };
147 
149 
150 
151 #endif // INCLUDED_DEBUGGINGSERVER
CDebuggingServer * g_DebuggingServer
void SDL_sem
Definition: wsdl.h:109
static const char * header400
std::list< CBreakPoint > m_BreakPoints
STACK_INFO
bool SetNextDbgCmd(uint threadDebuggerID, DBGCMD dbgCmd)
bool GetSettingBreakOnException()
void RegisterScriptinterface(std::string name, ScriptInterface *pScriptInterface)
Register a new ScriptInerface for debugging the scripts it executes.
bool GetWebArgs(struct mg_connection *conn, const struct mg_request_info *request_info, std::string argName, uint &arg)
Webserver helper function (can be called by multiple mongooser threads)
CMutex m_Mutex
Mutexes used to ensure thread-safety.
void * MgDebuggingServerCallback(mg_event event, struct mg_connection *conn, const struct mg_request_info *request_info)
A non-recursive mutual exclusion lock.
Definition: ThreadUtil.h:45
std::list< CThreadDebugger * > m_ThreadDebuggers
Shared between multiple scriptinerface threads and multiple mongoose threads.
void GetThreadDebuggerStatus(std::stringstream &response)
Functions that are made available via http (can be called by multiple mongoose threads) ...
SDL_sem * m_BreakPointsSem
Used for controlling access to m_BreakPoints.
uint m_LastThreadDebuggerID
Shared between multiple scriptinterface threads.
void ReleaseBreakPointAccess(double breakPointsLockID)
See AquireBreakPointAccess().
void EnableHTTP()
Not important for this class&#39; thread-safety.
void GetStackFrameData(std::stringstream &response, uint nestingLevel, uint threadDebuggerID, STACK_INFO stackInfoKind)
void SetBreakRequestedByThread(bool Enabled)
mg_event
Definition: mongoose.h:55
void EnumVfsJSFiles(std::stringstream &response)
Returns a list of the full vfs paths to all files with the extension .js found in the vfs root...
mg_context * m_MgContext
bool m_SettingSimultaneousThreadBreak
Shared between multiple mongoose threads.
double AquireBreakPointAccess(std::list< CBreakPoint > **breakPoints)
Aquire exclusive read and write access to the list of breakpoints.
bool GetSettingSimultaneousThreadBreak()
DBGCMD
Abstraction around a SpiderMonkey JSContext.
void ToggleBreakPoint(std::string filename, uint line)
void GetAllCallstacks(std::stringstream &response)
void SetSettingBreakOnException(bool Enabled)
void SetBreakRequestedByUser(bool Enabled)
void UnRegisterScriptinterface(ScriptInterface *pScriptInterface)
Unregister a ScriptInerface that was previously registered using RegisterScriptinterface.
void GetFile(std::string filename, std::stringstream &response)
Get the content of a .js file loaded into vfs.
bool GetBreakRequestedByThread()
Called from multiple Mongoose threads and multiple ScriptInterface threads.
void SetSettingSimultaneousThreadBreak(bool Enabled)