Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(832)

Side by Side Diff: runtime/bin/dbg_connection_macos.cc

Issue 10357003: Beginnings of a debugger wire protocol (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 #include <errno.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/event.h>
10 #include <unistd.h>
11
12 #include "bin/dartutils.h"
13 #include "bin/dbg_connection.h"
14 #include "bin/fdutils.h"
15 #include "bin/socket.h"
16 #include "platform/thread.h"
17 #include "platform/utils.h"
18
19
20 #define INVALID_FD -1
21
22 int DebuggerConnectionImpl::kqueue_fd_ = INVALID_FD;
23 int DebuggerConnectionImpl::wakeup_fds_[2] = {INVALID_FD, INVALID_FD};
24
25
26 // Used by VM threads to send a message to the debugger connetion
27 // handler thread.
28 void DebuggerConnectionImpl::SendMessage(MessageType id) {
29 ASSERT(wakeup_fds_[1] != INVALID_FD);
30 struct Message msg;
31 msg.msg_id = id;
32 int result = FDUtils::WriteToBlocking(wakeup_fds_[1], &msg, sizeof(msg));
33 if (result != sizeof(msg)) {
34 if (result == -1) {
35 perror("Wakeup message failure: ");
36 }
37 FATAL1("Wakeup message failure. Wrote %d bytes.", result);
38 }
39 }
40
41
42 // Used by the debugger connection handler to read the messages sent
43 // by the VM.
44 bool DebuggerConnectionImpl::ReceiveMessage(Message* msg) {
45 int total_read = 0;
46 int bytes_read = 0;
47 int remaining = sizeof(Message);
48 uint8_t* buf = reinterpret_cast<uint8_t*>(msg);
49 while (remaining > 0) {
50 bytes_read =
51 TEMP_FAILURE_RETRY(read(wakeup_fds_[0], buf + total_read, remaining));
52 if ((bytes_read < 0) && (total_read == 0)) {
53 return false;
54 }
55 if (bytes_read > 0) {
56 total_read += bytes_read;
57 remaining -= bytes_read;
58 }
59 }
60 ASSERT(remaining >= 0);
61 return remaining == 0;
62 }
63
64
65 void DebuggerConnectionImpl::HandleEvent(struct kevent* event) {
66 int ident = event->ident;
67 if (ident == DebuggerConnectionHandler::listener_fd_) {
68 printf("knock knock\n");
69 if (DebuggerConnectionHandler::IsConnected()) {
70 FATAL("Cannot connect to more than one debugger.\n");
71 }
72 int fd = ServerSocket::Accept(ident);
73 if (fd < 0) {
74 FATAL("Accepting new debugger connection failed.\n");
75 }
76 FDUtils::SetBlocking(fd);
77 DebuggerConnectionHandler::AcceptDbgConnection(fd);
78
79 /* For now, don't poll the debugger connection.
80 struct kevent ev_add;
81 EV_SET(&ev_add, fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
82 int status =
83 TEMP_FAILURE_RETRY(kevent(kqueue_fd_, &ev_add, 1, NULL, 0, NULL));
84 if (status == -1) {
85 FATAL1("Failed adding debugger socket to kqueue: %s\n", strerror(errno));
86 }
87 */
88 } else if (ident == DebuggerConnectionHandler::debugger_fd_) {
89 printf("unexpected: receiving debugger connection event.\n");
90 UNIMPLEMENTED();
91 } else {
92 Message msg;
93 if (ReceiveMessage(&msg)) {
94 printf("Received sync message id %d.\n", msg.msg_id);
95 }
96 }
97 }
98
99
100 void DebuggerConnectionImpl::Handler(uword args) {
101 static const intptr_t kMaxEvents = 4;
102 struct kevent events[kMaxEvents];
103
104 while (1) {
105 // Wait indefinitely for an event.
106 int result = TEMP_FAILURE_RETRY(
107 kevent(kqueue_fd_, NULL, 0, events, kMaxEvents, NULL));
108 if (result == -1) {
109 FATAL1("kevent failed %s\n", strerror(errno));
110 } else {
111 ASSERT(result <= kMaxEvents);
112 for (int i = 0; i < result; i++) {
113 HandleEvent(&events[i]);
114 }
115 }
116 }
117 printf("shutting down debugger thread\n");
118 }
119
120
121 void DebuggerConnectionImpl::SetupPollQueue() {
122 int result;
123 result = TEMP_FAILURE_RETRY(pipe(wakeup_fds_));
124 if (result != 0) {
125 FATAL1("Pipe creation failed with error %d\n", result);
126 }
127 FDUtils::SetNonBlocking(wakeup_fds_[0]);
128
129 kqueue_fd_ = TEMP_FAILURE_RETRY(kqueue());
130 if (kqueue_fd_ == -1) {
131 FATAL("Failed creating kqueue\n");
132 }
133 // Register the wakeup_fd_ with the kqueue.
134 struct kevent event;
135 EV_SET(&event, wakeup_fds_[0], EVFILT_READ, EV_ADD, 0, 0, NULL);
136 int status = TEMP_FAILURE_RETRY(kevent(kqueue_fd_, &event, 1, NULL, 0, NULL));
137 if (status == -1) {
138 FATAL1("Failed adding wakeup pipe fd to kqueue: %s\n", strerror(errno));
139 }
140
141 // Register the listening socket.
142 EV_SET(&event, DebuggerConnectionHandler::listener_fd_,
143 EVFILT_READ, EV_ADD, 0, 0, NULL);
144 status = TEMP_FAILURE_RETRY(kevent(kqueue_fd_, &event, 1, NULL, 0, NULL));
145 if (status == -1) {
146 FATAL1("Failed adding listener socket to kqueue: %s\n", strerror(errno));
147 }
148 }
149
150
151 void DebuggerConnectionImpl::StartHandler(int port_number) {
152 ASSERT(DebuggerConnectionHandler::listener_fd_ != -1);
153 SetupPollQueue();
154 int result =
155 dart::Thread::Start(&DebuggerConnectionImpl::Handler, 0);
156 if (result != 0) {
157 FATAL1("Failed to start debugger connection handler thread: %d\n", result);
158 }
159 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698