v8eval 0.2.3 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,211 +0,0 @@
1
- #include "dbgsrv.h"
2
- #include "v8eval.h"
3
-
4
- namespace v8eval {
5
-
6
- //
7
- // DbgSrv
8
- //
9
-
10
- // container_of helper function
11
- //
12
- // libuv does not accept opaque values in its callbacks, so we have to
13
- // recover the instance of the debug server (and associated v8 vm)
14
- // through a C++ version of offsetof().
15
- template<class A, class B, class C>
16
- A* container_of(B* ptr, const C A::* member) {
17
- size_t offset = (size_t) &(reinterpret_cast<A*>(0)->*member);
18
- return (A*)((char *)ptr - offset);
19
- }
20
-
21
- DbgSrv::DbgSrv(_V8& v8) : v8_(v8) {
22
- dbgsrv_port_ = 0;
23
- uv_loop_init(&dbgsrv_loop_);
24
-
25
- // Start up the Debugger Processing Loop
26
- uv_loop_init(&dbgproc_loop_);
27
- uv_async_init(&dbgproc_loop_, &dbgproc_proc_, dbgproc_do_proc);
28
- uv_async_init(&dbgproc_loop_, &dbgproc_stop_, dbgproc_do_stop);
29
- uv_thread_create(&dbgproc_thread_, dbgproc, this);
30
-
31
- status_ = dbgsrv_offline;
32
- }
33
-
34
- DbgSrv::~DbgSrv() {
35
- if (status_ != dbgsrv_offline) {
36
- v8_.debugger_stop();
37
- uv_async_send(&dbgsrv_stop_);
38
- uv_thread_join(&dbgsrv_thread_);
39
- }
40
- uv_loop_close(&dbgsrv_loop_);
41
-
42
- uv_async_send(&dbgproc_stop_);
43
- uv_thread_join(&dbgproc_thread_);
44
- uv_loop_close(&dbgproc_loop_);
45
- }
46
-
47
- static void end_write(uv_write_t *req, int status) {
48
- if (status) {
49
- fprintf(stderr, "write: %s\n", uv_strerror(status));
50
- }
51
- free(req);
52
- }
53
-
54
- void DbgSrv::dbgsrv_do_send(uv_async_t *async) {
55
- DbgSrv *db = container_of(async, &DbgSrv::dbgsrv_send_);
56
- uv_buf_t buf;
57
- uv_write_t *wreq;
58
-
59
- while (!db->msg_queue_.empty()) {
60
- std::string& str = db->msg_queue_.back();
61
-
62
- buf = uv_buf_init((char *)str.c_str(), (unsigned int)str.size());
63
- wreq = (uv_write_t *)malloc(sizeof(*wreq));
64
- uv_write(wreq, (uv_stream_t *)&db->dbgsrv_clnt_, &buf, 1, end_write);
65
- db->msg_queue_.pop_back();
66
- }
67
- }
68
-
69
- void DbgSrv::dbgsrv_do_clnt(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) {
70
- DbgSrv *db = container_of(client, &DbgSrv::dbgsrv_clnt_);
71
-
72
- if (nread == 0) return;
73
-
74
- if (nread < 0) {
75
- // Close the client
76
- uv_close((uv_handle_t *)&db->dbgsrv_send_, NULL);
77
- uv_close((uv_handle_t *)&db->dbgsrv_clnt_, NULL);
78
- db->status_ = dbgsrv_started;
79
- return;
80
- }
81
-
82
- const std::string string(buf->base, nread);
83
- db->v8_.debugger_send(string);
84
- free(buf->base);
85
-
86
- uv_async_send(&db->dbgproc_proc_);
87
- }
88
-
89
- static void alloc_buffer(uv_handle_t *handle, size_t size, uv_buf_t *buf) {
90
- buf->len = size;
91
- buf->base = (char*) malloc(size);
92
- }
93
-
94
- void DbgSrv::dbgsrv_do_serv(uv_stream_t *server, int status) {
95
- DbgSrv *db = container_of(server, &DbgSrv::dbgsrv_serv_);
96
-
97
- if (status < 0) {
98
- return;
99
- }
100
-
101
- // Connect with the client.
102
- uv_tcp_init(&db->dbgsrv_loop_, &db->dbgsrv_clnt_);
103
- if (uv_accept(server, (uv_stream_t *)&db->dbgsrv_clnt_)) {
104
- uv_close((uv_handle_t *)&db->dbgsrv_clnt_, NULL);
105
- return;
106
- }
107
-
108
- // Setup async R/W callbacks.
109
- uv_async_init(&db->dbgsrv_loop_, &db->dbgsrv_send_, dbgsrv_do_send);
110
- uv_read_start((uv_stream_t *)&db->dbgsrv_clnt_, alloc_buffer, dbgsrv_do_clnt);
111
-
112
- db->status_ = dbgsrv_connected;
113
- }
114
-
115
- void DbgSrv::dbgsrv_do_stop(uv_async_t *async) {
116
- DbgSrv *db = container_of(async, &DbgSrv::dbgsrv_stop_);
117
-
118
- // Stop Server Loop
119
- if (db->status_ == dbgsrv_connected) {
120
- uv_close((uv_handle_t *)&db->dbgsrv_send_, NULL);
121
- uv_close((uv_handle_t *)&db->dbgsrv_clnt_, NULL);
122
- db->status_ = dbgsrv_started;
123
- }
124
- if (db->status_ == dbgsrv_started) {
125
- uv_close((uv_handle_t *)&db->dbgsrv_serv_, NULL);
126
- uv_close((uv_handle_t *)&db->dbgsrv_stop_, NULL);
127
- }
128
- }
129
-
130
- void DbgSrv::dbgsrv(void *ptr) {
131
- DbgSrv *db = (DbgSrv*)ptr;
132
-
133
- uv_run(&db->dbgsrv_loop_, UV_RUN_DEFAULT);
134
- }
135
-
136
- void DbgSrv::dbgproc_do_stop(uv_async_t *async) {
137
- DbgSrv *db = container_of(async, &DbgSrv::dbgproc_stop_);
138
-
139
- uv_close((uv_handle_t *)&db->dbgproc_proc_, NULL);
140
- uv_close((uv_handle_t *)&db->dbgproc_stop_, NULL);
141
- }
142
-
143
- void DbgSrv::dbgproc_do_proc(uv_async_t *async) {
144
- DbgSrv *db = container_of(async, &DbgSrv::dbgproc_proc_);
145
-
146
- db->v8_.debugger_process();
147
- }
148
-
149
- void DbgSrv::dbgproc(void *ptr) {
150
- DbgSrv *db = (DbgSrv*)ptr;
151
-
152
- uv_run(&db->dbgproc_loop_, UV_RUN_DEFAULT);
153
- }
154
-
155
- void DbgSrv::recv_from_debugger(std::string& string, void *opq) {
156
- DbgSrv *db = (DbgSrv *)opq;
157
-
158
- db->msg_queue_.push_front(string);
159
- uv_async_send(&db->dbgsrv_send_);
160
- }
161
-
162
- bool DbgSrv::start(int port) {
163
- struct sockaddr_in addr;
164
-
165
- if (status_ != dbgsrv_offline) {
166
- return false;
167
- }
168
-
169
- if (port != (uint16_t)port) {
170
- return false;
171
- }
172
-
173
- // Set up the TCP Connection.
174
- uv_tcp_init(&dbgsrv_loop_, &dbgsrv_serv_);
175
- uv_ip4_addr("127.0.0.1", port, &addr);
176
- if (uv_tcp_bind(&dbgsrv_serv_, (const struct sockaddr*)&addr, 0)) {
177
- uv_close((uv_handle_t *)&dbgsrv_serv_, NULL);
178
- perror("bind");
179
- return false;
180
- }
181
-
182
- if (port == 0) {
183
- int addrlen = sizeof(addr);
184
- if (uv_tcp_getsockname(&dbgsrv_serv_, (struct sockaddr*)&addr, &addrlen)) {
185
- uv_close((uv_handle_t *)&dbgsrv_serv_, NULL);
186
- perror("getsockname");
187
- return false;
188
- }
189
- dbgsrv_port_ = ntohs(addr.sin_port);
190
- } else {
191
- dbgsrv_port_ = port;
192
- }
193
-
194
- if (uv_listen((uv_stream_t *)&dbgsrv_serv_, 0, dbgsrv_do_serv)) {
195
- uv_close((uv_handle_t *)&dbgsrv_serv_, NULL);
196
- perror("listen");
197
- return false;
198
- }
199
-
200
- // Start V8 debugger
201
- v8_.debugger_init(recv_from_debugger, this);
202
-
203
- // Start the Debug Server Loop
204
- uv_async_init(&dbgsrv_loop_, &dbgsrv_stop_, dbgsrv_do_stop);
205
- uv_thread_create(&dbgsrv_thread_, dbgsrv, this);
206
-
207
- status_ = dbgsrv_started;
208
- return true;
209
- }
210
-
211
- } // namespace v8eval
@@ -1,72 +0,0 @@
1
- #ifndef DBGSRV_H_
2
- #define DBGSRV_H_
3
-
4
- #include "uv.h"
5
- #include <string>
6
- #include <list>
7
-
8
- namespace v8eval {
9
-
10
- class _V8;
11
-
12
- /// \class DbgSrv
13
- ///
14
- /// A debugger server is associated to a _V8 instance and accepts
15
- /// TCP/IP connections to exchange messages in the V8 debugger
16
- /// protocol.
17
- class DbgSrv {
18
- public:
19
- DbgSrv(_V8& v8);
20
- ~DbgSrv();
21
-
22
- /// \brief Starts a debugger server
23
- /// \param port TCP/IP port the server will listen
24
- /// \return success or not as boolean
25
- ///
26
- /// The port can be set to 0 to have a port automatically assigned.
27
- bool start(int port);
28
-
29
- /// \brief Get the TCP/IP port the system is currently listening from
30
- /// \return A TCP/IP port or 0 if not currently set.
31
- inline int get_port() { return dbgsrv_port_; }
32
-
33
- private:
34
- static void recv_from_debugger(std::string& string, void *opq);
35
-
36
- static void dbgsrv_do_clnt(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf);
37
- static void dbgsrv_do_send(uv_async_t *async);
38
- static void dbgsrv_do_serv(uv_stream_t *server, int status);
39
- static void dbgsrv_do_stop(uv_async_t *async);
40
- static void dbgsrv(void *);
41
-
42
- static void dbgproc_do_proc(uv_async_t *);
43
- static void dbgproc_do_stop(uv_async_t *);
44
- static void dbgproc(void *);
45
-
46
- private:
47
- _V8& v8_;
48
-
49
- enum {
50
- dbgsrv_offline,
51
- dbgsrv_started,
52
- dbgsrv_connected
53
- } status_;
54
- std::list<std::string> msg_queue_;
55
-
56
- int dbgsrv_port_;
57
- uv_tcp_t dbgsrv_serv_;
58
- uv_tcp_t dbgsrv_clnt_;
59
- uv_async_t dbgsrv_send_;
60
- uv_async_t dbgsrv_stop_;
61
- uv_thread_t dbgsrv_thread_;
62
- uv_loop_t dbgsrv_loop_;
63
-
64
- uv_async_t dbgproc_proc_;
65
- uv_async_t dbgproc_stop_;
66
- uv_thread_t dbgproc_thread_;
67
- uv_loop_t dbgproc_loop_;
68
- };
69
-
70
- } // namespace v8eval
71
-
72
- #endif // DBGSRV_H_