OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 #ifndef BIN_DBG_CONNECTION_H_ | |
6 #define BIN_DBG_CONNECTION_H_ | |
7 | |
8 #include "bin/builtin.h" | |
9 #include "bin/utils.h" | |
10 | |
11 #include "include/dart_tools_api.h" | |
12 | |
13 #include "platform/globals.h" | |
14 #include "platform/json.h" | |
15 #include "bin/thread.h" | |
16 // Declare the OS-specific types ahead of defining the generic class. | |
17 #if defined(TARGET_OS_ANDROID) | |
18 #include "bin/dbg_connection_android.h" | |
19 #elif defined(TARGET_OS_LINUX) | |
20 #include "bin/dbg_connection_linux.h" | |
21 #elif defined(TARGET_OS_MACOS) | |
22 #include "bin/dbg_connection_macos.h" | |
23 #elif defined(TARGET_OS_WINDOWS) | |
24 #include "bin/dbg_connection_win.h" | |
25 #else | |
26 #error Unknown target os. | |
27 #endif | |
28 | |
29 | |
30 namespace dart { | |
31 namespace bin { | |
32 | |
33 // Forward declarations. | |
34 class DbgMessage; | |
35 class MessageBuffer; | |
36 | |
37 | |
38 class DebuggerConnectionHandler { | |
39 public: | |
40 explicit DebuggerConnectionHandler(intptr_t debug_fd); | |
41 ~DebuggerConnectionHandler(); | |
42 | |
43 // Accessors. | |
44 intptr_t debug_fd() const { return debug_fd_; } | |
45 | |
46 // Return message id of current debug command message. | |
47 int MessageId(); | |
48 | |
49 // Starts the native thread which listens for connections from | |
50 // debugger clients, reads and dispatches debug command messages | |
51 // from the client. | |
52 static int StartHandler(const char* address, int port_number); | |
53 | |
54 // Stops the native thread. | |
55 static void StopHandler(); | |
56 | |
57 // Initializes the parts of the debugger which are needed by the vm | |
58 // service. This function should only be called when StartHandler | |
59 // is not called. | |
60 static void InitForVmService(); | |
61 | |
62 // Called by Isolates when they need to wait for a connection | |
63 // from debugger clients. | |
64 static void WaitForConnection(); | |
65 | |
66 // Sends a reply or an error message to a specific debugger client. | |
67 static void SendMsg(intptr_t debug_fd, dart::TextBuffer* msg); | |
68 static void SendError(intptr_t debug_fd, int msg_id, const char* err_msg); | |
69 | |
70 // Sends an event message to all debugger clients that are connected. | |
71 static void BroadcastMsg(dart::TextBuffer* msg); | |
72 | |
73 private: | |
74 void HandleUnknownMsg(); | |
75 void HandleMessages(); | |
76 | |
77 void CloseDbgConnection(); | |
78 | |
79 // The socket that connects with the debugger client. | |
80 // The descriptor is created and closed by the debugger connection thread. | |
81 intptr_t debug_fd_; | |
82 | |
83 // Buffer holding the messages received over the wire from the debugger | |
84 // front end.. | |
85 MessageBuffer* msgbuf_; | |
86 | |
87 // Accepts connection requests from debugger client and sets up a | |
88 // connection handler object to read and handle messages from the client. | |
89 static void AcceptDbgConnection(intptr_t debug_fd); | |
90 | |
91 // Handlers for generic debug command messages which are not specific to | |
92 // an isolate. | |
93 static void HandleInterruptCmd(DbgMessage* msg); | |
94 static void HandleIsolatesListCmd(DbgMessage* msg); | |
95 | |
96 // Helper methods to manage debugger client connections. | |
97 static void AddNewDebuggerConnection(intptr_t debug_fd); | |
98 static void RemoveDebuggerConnection(intptr_t debug_fd); | |
99 static DebuggerConnectionHandler* GetDebuggerConnectionHandler( | |
100 intptr_t debug_fd); | |
101 static bool IsConnected(); | |
102 | |
103 // Helper method for sending messages back to a debugger client. | |
104 static void SendMsgHelper(intptr_t debug_fd, dart::TextBuffer* msg); | |
105 | |
106 // mutex/condition variable used by isolates when writing back to the | |
107 // debugger. This is also used to ensure that the isolate waits for | |
108 // a debugger to be attached when that is requested on the command line. | |
109 static Monitor* handler_lock_; | |
110 | |
111 static bool IsListening() { | |
112 return listener_fd_ != -1; | |
113 } | |
114 | |
115 // The socket that is listening for incoming debugger connections. | |
116 // This descriptor is created and closed by a native thread. | |
117 static intptr_t listener_fd_; | |
118 | |
119 friend class DebuggerConnectionImpl; | |
120 | |
121 DISALLOW_IMPLICIT_CONSTRUCTORS(DebuggerConnectionHandler); | |
122 }; | |
123 | |
124 } // namespace bin | |
125 } // namespace dart | |
126 | |
127 #endif // BIN_DBG_CONNECTION_H_ | |
OLD | NEW |