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

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

Issue 10990089: First step towards support for being able to interrupt a running Dart Isolate (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 2 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
« runtime/bin/dbg_message.h ('K') | « runtime/bin/dbg_message.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "bin/dbg_connection.h"
6 #include "bin/dbg_message.h"
7 #include "bin/dartutils.h"
8 #include "bin/thread.h"
9 #include "bin/utils.h"
10
11 #include "platform/globals.h"
12 #include "platform/json.h"
13 #include "platform/thread.h"
14 #include "platform/utils.h"
15
16 #include "include/dart_api.h"
17
18 bool MessageParser::IsValidMessage() const {
19 if (buf_length_ == 0) {
20 return false;
21 }
22 dart::JSONReader msg_reader(buf_);
23 return msg_reader.EndOfObject() != NULL;
24 }
25
26
27 int MessageParser::MessageId() const {
28 dart::JSONReader r(buf_);
29 r.Seek("id");
30 if (r.Type() == dart::JSONReader::kInteger) {
31 return atoi(r.ValueChars());
32 } else {
33 return -1;
34 }
35 }
36
37
38 const char* MessageParser::Params() const {
39 dart::JSONReader r(buf_);
40 r.Seek("params");
41 if (r.Type() == dart::JSONReader::kObject) {
42 return r.ValueChars();
43 } else {
44 return NULL;
45 }
46 }
47
48
49 intptr_t MessageParser::GetIntParam(const char* name) const {
50 const char* params = Params();
51 ASSERT(params != NULL);
52 dart::JSONReader r(params);
53 r.Seek(name);
54 ASSERT(r.Type() == dart::JSONReader::kInteger);
55 return strtol(r.ValueChars(), NULL, 10);
56 }
57
58
59 intptr_t MessageParser::GetOptIntParam(const char* name,
60 intptr_t default_val) const {
61 const char* params = Params();
62 ASSERT(params != NULL);
63 dart::JSONReader r(params);
64 r.Seek(name);
65 if (r.Type() == dart::JSONReader::kInteger) {
66 return strtol(r.ValueChars(), NULL, 10);
67 } else {
68 return default_val;
69 }
70 }
71
72
73 static const char* GetStringChars(Dart_Handle str) {
74 ASSERT(Dart_IsString(str));
75 const char* chars;
76 Dart_Handle res = Dart_StringToCString(str, &chars);
77 ASSERT(!Dart_IsError(res));
78 return chars;
79 }
80
81
82 static int GetIntValue(Dart_Handle int_handle) {
83 int64_t int64_val = -1;
84 ASSERT(Dart_IsInteger(int_handle));
85 Dart_Handle res = Dart_IntegerToInt64(int_handle, &int64_val);
86 ASSERT_NOT_ERROR(res);
87 // TODO(hausner): Range check.
88 return int64_val;
89 }
90
91
92 char* MessageParser::GetStringParam(const char* name) const {
93 const char* params = Params();
94 ASSERT(params != NULL);
95 dart::JSONReader pr(params);
96 pr.Seek(name);
97 if (pr.Type() != dart::JSONReader::kString) {
98 return NULL;
99 }
100 intptr_t buflen = pr.ValueLen() + 1;
101 char* param_chars = reinterpret_cast<char*>(malloc(buflen));
102 pr.GetValueChars(param_chars, buflen);
103 // TODO(hausner): Decode escape sequences.
104 return param_chars;
105 }
106
107
108 static void FormatEncodedString(dart::TextBuffer* buf, Dart_Handle str) {
109 intptr_t str_len = 0;
110 Dart_Handle res = Dart_StringLength(str, &str_len);
111 ASSERT_NOT_ERROR(res);
112 uint32_t* codepoints =
113 reinterpret_cast<uint32_t*>(malloc(str_len * sizeof(uint32_t)));
114 ASSERT(codepoints != NULL);
115 intptr_t actual_len = str_len;
116 res = Dart_StringGet32(str, codepoints, &actual_len);
117 ASSERT_NOT_ERROR(res);
118 ASSERT(str_len == actual_len);
119 buf->AddChar('\"');
120 for (int i = 0; i < str_len; i++) {
121 buf->AddEscapedChar(codepoints[i]);
122 }
123 buf->AddChar('\"');
124 free(codepoints);
125 }
126
127
128 static void FormatErrorMsg(dart::TextBuffer* buf, Dart_Handle err) {
129 // TODO(hausner): Turn message into Dart string and
130 // properly encode the message.
131 ASSERT(Dart_IsError(err));
132 const char* msg = Dart_GetError(err);
133 buf->Printf("\"%s\"", msg);
134 }
135
136
137 static void FormatTextualValue(dart::TextBuffer* buf, Dart_Handle object) {
138 Dart_Handle text;
139 if (Dart_IsNull(object)) {
140 text = Dart_Null();
141 } else {
142 Dart_ExceptionPauseInfo savedState = Dart_GetExceptionPauseInfo();
143
144 // TODO(hausner): Check whether recursive/reentrant pauses on exceptions
145 // should be prevented in Debugger::SignalExceptionThrown() instead.
146 if (savedState != kNoPauseOnExceptions) {
147 Dart_Handle res = Dart_SetExceptionPauseInfo(kNoPauseOnExceptions);
148 ASSERT_NOT_ERROR(res);
149 }
150
151 text = Dart_ToString(object);
152
153 if (savedState != kNoPauseOnExceptions) {
154 Dart_Handle res = Dart_SetExceptionPauseInfo(savedState);
155 ASSERT_NOT_ERROR(res);
156 }
157 }
158 buf->Printf("\"text\":");
159 if (Dart_IsNull(text)) {
160 buf->Printf("null");
161 } else if (Dart_IsError(text)) {
162 FormatErrorMsg(buf, text);
163 } else {
164 FormatEncodedString(buf, text);
165 }
166 }
167
168
169 static void FormatValue(dart::TextBuffer* buf, Dart_Handle object) {
170 if (Dart_IsInteger(object)) {
171 buf->Printf("\"kind\":\"integer\",");
172 } else if (Dart_IsString(object)) {
173 buf->Printf("\"kind\":\"string\",");
174 } else if (Dart_IsBoolean(object)) {
175 buf->Printf("\"kind\":\"boolean\",");
176 } else if (Dart_IsList(object)) {
177 intptr_t len = 0;
178 Dart_Handle res = Dart_ListLength(object, &len);
179 ASSERT_NOT_ERROR(res);
180 buf->Printf("\"kind\":\"list\",\"length\":%"Pd",", len);
181 } else {
182 buf->Printf("\"kind\":\"object\",");
183 }
184 FormatTextualValue(buf, object);
185 }
186
187
188 static void FormatValueObj(dart::TextBuffer* buf, Dart_Handle object) {
189 buf->Printf("{");
190 FormatValue(buf, object);
191 buf->Printf("}");
192 }
193
194
195 static void FormatRemoteObj(dart::TextBuffer* buf, Dart_Handle object) {
196 intptr_t obj_id = Dart_CacheObject(object);
197 ASSERT(obj_id >= 0);
198 buf->Printf("{\"objectId\":%"Pd",", obj_id);
199 FormatValue(buf, object);
200 buf->Printf("}");
201 }
202
203
204 static void FormatNamedValue(dart::TextBuffer* buf,
205 Dart_Handle object_name,
206 Dart_Handle object) {
207 ASSERT(Dart_IsString(object_name));
208 buf->Printf("{\"name\":\"%s\",", GetStringChars(object_name));
209 buf->Printf("\"value\":");
210 FormatRemoteObj(buf, object);
211 buf->Printf("}");
212 }
213
214
215 static void FormatNamedValueList(dart::TextBuffer* buf,
216 Dart_Handle obj_list) {
217 ASSERT(Dart_IsList(obj_list));
218 intptr_t list_length = 0;
219 Dart_Handle res = Dart_ListLength(obj_list, &list_length);
220 ASSERT_NOT_ERROR(res);
221 ASSERT(list_length % 2 == 0);
222 buf->Printf("[");
223 for (int i = 0; i + 1 < list_length; i += 2) {
224 Dart_Handle name_handle = Dart_ListGetAt(obj_list, i);
225 ASSERT_NOT_ERROR(name_handle);
226 Dart_Handle value_handle = Dart_ListGetAt(obj_list, i + 1);
227 ASSERT_NOT_ERROR(value_handle);
228 if (i > 0) {
229 buf->Printf(",");
230 }
231 FormatNamedValue(buf, name_handle, value_handle);
232 }
233 buf->Printf("]");
234 }
235
236
237 static const char* FormatClassProps(dart::TextBuffer* buf,
238 intptr_t cls_id) {
239 Dart_Handle name, static_fields;
240 intptr_t super_id = -1;
241 intptr_t library_id = -1;
242 Dart_Handle res =
243 Dart_GetClassInfo(cls_id, &name, &library_id, &super_id, &static_fields);
244 RETURN_IF_ERROR(res);
245 RETURN_IF_ERROR(name);
246 buf->Printf("{\"name\":\"%s\",", GetStringChars(name));
247 if (super_id > 0) {
248 buf->Printf("\"superclassId\":%"Pd",", super_id);
249 }
250 buf->Printf("\"libraryId\":%"Pd",", library_id);
251 RETURN_IF_ERROR(static_fields);
252 buf->Printf("\"fields\":");
253 FormatNamedValueList(buf, static_fields);
254 buf->Printf("}");
255 return NULL;
256 }
257
258
259 static const char* FormatLibraryProps(dart::TextBuffer* buf,
260 intptr_t lib_id) {
261 Dart_Handle url = Dart_GetLibraryURL(lib_id);
262 RETURN_IF_ERROR(url);
263 buf->Printf("{\"url\":");
264 FormatEncodedString(buf, url);
265
266 // Whether debugging is enabled.
267 bool is_debuggable = false;
268 Dart_Handle res = Dart_GetLibraryDebuggable(lib_id, &is_debuggable);
269 RETURN_IF_ERROR(res);
270 buf->Printf(",\"debuggingEnabled\":%s",
271 is_debuggable ? "\"true\"" : "\"false\"");
272
273 // Imports and prefixes.
274 Dart_Handle import_list = Dart_GetLibraryImports(lib_id);
275 RETURN_IF_ERROR(import_list);
276 ASSERT(Dart_IsList(import_list));
277 intptr_t list_length = 0;
278 res = Dart_ListLength(import_list, &list_length);
279 RETURN_IF_ERROR(res);
280 buf->Printf(",\"imports\":[");
281 for (int i = 0; i + 1 < list_length; i += 2) {
282 Dart_Handle lib_id = Dart_ListGetAt(import_list, i + 1);
283 ASSERT_NOT_ERROR(lib_id);
284 buf->Printf("%s{\"libraryId\":%d,",
285 (i > 0) ? ",": "",
286 GetIntValue(lib_id));
287
288 Dart_Handle name = Dart_ListGetAt(import_list, i);
289 ASSERT_NOT_ERROR(name);
290 buf->Printf("\"prefix\":\"%s\"}",
291 Dart_IsNull(name) ? "" : GetStringChars(name));
292 }
293 buf->Printf("],");
294
295 // Global variables in the library.
296 Dart_Handle global_vars = Dart_GetLibraryFields(lib_id);
297 RETURN_IF_ERROR(global_vars);
298 buf->Printf("\"globals\":");
299 FormatNamedValueList(buf, global_vars);
300 buf->Printf("}");
301 return NULL;
302 }
303
304
305 static const char* FormatObjProps(dart::TextBuffer* buf,
306 Dart_Handle object) {
307 intptr_t class_id;
308 if (Dart_IsNull(object)) {
309 buf->Printf("{\"classId\":-1,\"fields\":[]}");
310 return NULL;
311 }
312 Dart_Handle res = Dart_GetObjClassId(object, &class_id);
313 RETURN_IF_ERROR(res);
314 buf->Printf("{\"classId\": %"Pd",", class_id);
315 buf->Printf("\"kind\":\"object\",\"fields\":");
316 Dart_Handle fields = Dart_GetInstanceFields(object);
317 RETURN_IF_ERROR(fields);
318 FormatNamedValueList(buf, fields);
319 buf->Printf("}");
320 return NULL;
321 }
322
323
324 static const char* FormatListSlice(dart::TextBuffer* buf,
325 Dart_Handle list,
326 intptr_t list_length,
327 intptr_t index,
328 intptr_t slice_length) {
329 intptr_t end_index = index + slice_length;
330 ASSERT(end_index <= list_length);
331 buf->Printf("{\"index\":%"Pd",", index);
332 buf->Printf("\"length\":%"Pd",", slice_length);
333 buf->Printf("\"elements\":[");
334 for (intptr_t i = index; i < end_index; i++) {
335 Dart_Handle value = Dart_ListGetAt(list, i);
336 if (i > index) {
337 buf->Printf(",");
338 }
339 FormatValueObj(buf, value);
340 }
341 buf->Printf("]}");
342 return NULL;
343 }
344
345
346 static void FormatCallFrames(dart::TextBuffer* msg, Dart_StackTrace trace) {
347 intptr_t trace_len = 0;
348 Dart_Handle res = Dart_StackTraceLength(trace, &trace_len);
349 ASSERT_NOT_ERROR(res);
350 msg->Printf("\"callFrames\" : [ ");
351 for (int i = 0; i < trace_len; i++) {
352 Dart_ActivationFrame frame;
353 res = Dart_GetActivationFrame(trace, i, &frame);
354 ASSERT_NOT_ERROR(res);
355 Dart_Handle func_name;
356 Dart_Handle script_url;
357 intptr_t line_number = 0;
358 intptr_t library_id = 0;
359 res = Dart_ActivationFrameInfo(
360 frame, &func_name, &script_url, &line_number, &library_id);
361 ASSERT_NOT_ERROR(res);
362 ASSERT(Dart_IsString(func_name));
363 msg->Printf("%s{\"functionName\":", (i > 0) ? "," : "");
364 FormatEncodedString(msg, func_name);
365 msg->Printf(",\"libraryId\": %"Pd",", library_id);
366
367 ASSERT(Dart_IsString(script_url));
368 msg->Printf("\"location\": { \"url\":");
369 FormatEncodedString(msg, script_url);
370 msg->Printf(",\"lineNumber\":%"Pd"},", line_number);
371
372 Dart_Handle locals = Dart_GetLocalVariables(frame);
373 ASSERT_NOT_ERROR(locals);
374 msg->Printf("\"locals\":");
375 FormatNamedValueList(msg, locals);
376 msg->Printf("}");
377 }
378 msg->Printf("]");
379 }
380
381
382 typedef void (*CommandHandler)(DbgMessage* msg);
383
384 struct JSONDebuggerCommand {
385 const char* cmd_string;
386 CommandHandler handler_function;
387 };
388
389
390 static JSONDebuggerCommand debugger_commands[] = {
391 { "resume", DbgMessage::HandleResumeCmd },
392 { "stepInto", DbgMessage::HandleStepIntoCmd },
393 { "stepOut", DbgMessage::HandleStepOutCmd },
394 { "stepOver", DbgMessage::HandleStepOverCmd },
395 { "getLibraries", DbgMessage::HandleGetLibrariesCmd },
396 { "getClassProperties", DbgMessage::HandleGetClassPropsCmd },
397 { "getLibraryProperties", DbgMessage::HandleGetLibPropsCmd },
398 { "setLibraryProperties", DbgMessage::HandleSetLibPropsCmd },
399 { "getObjectProperties", DbgMessage::HandleGetObjPropsCmd },
400 { "getListElements", DbgMessage::HandleGetListCmd },
401 { "getGlobalVariables", DbgMessage::HandleGetGlobalsCmd },
402 { "getScriptURLs", DbgMessage::HandleGetScriptURLsCmd },
403 { "getScriptSource", DbgMessage::HandleGetSourceCmd },
404 { "getStackTrace", DbgMessage::HandleGetStackTraceCmd },
405 { "setBreakpoint", DbgMessage::HandleSetBpCmd },
406 { "setPauseOnException", DbgMessage::HandlePauseOnExcCmd },
407 { "removeBreakpoint", DbgMessage::HandleRemBpCmd },
408 { NULL, NULL }
409 };
410
411
412 void DbgMessage::HandleMessage() {
413 // Dispatch to the appropriate handler for the command.
414 int max_index = (sizeof(debugger_commands) / sizeof(JSONDebuggerCommand));
415 ASSERT(cmd_idx_ < max_index);
416 (*debugger_commands[cmd_idx_].handler_function)(this);
417 }
418
419
420 void DbgMessage::SendReply(dart::TextBuffer* reply) {
421 DebuggerConnectionHandler::SendMsg(debug_fd(), reply);
422 }
423
424
425 void DbgMessage::SendErrorReply(int msg_id, const char* err_msg) {
426 DebuggerConnectionHandler::SendError(debug_fd(), msg_id, err_msg);
427 }
428
429
430 void DbgMessage::HandleResumeCmd(DbgMessage* in_msg) {
431 ASSERT(in_msg != NULL);
432 MessageParser msg_parser(in_msg->buffer(), in_msg->buffer_len());
433 int msg_id = msg_parser.MessageId();
434 dart::TextBuffer msg(64);
435 msg.Printf("{ \"id\": %d }", msg_id);
436 in_msg->SendReply(&msg);
437 in_msg->set_request_resume(true);
438 }
439
440
441 void DbgMessage::HandleStepIntoCmd(DbgMessage* in_msg) {
442 Dart_Handle res = Dart_SetStepInto();
443 ASSERT_NOT_ERROR(res);
444 HandleResumeCmd(in_msg);
445 }
446
447
448 void DbgMessage::HandleStepOutCmd(DbgMessage* in_msg) {
449 Dart_Handle res = Dart_SetStepOut();
450 ASSERT_NOT_ERROR(res);
451 HandleResumeCmd(in_msg);
452 }
453
454
455 void DbgMessage::HandleStepOverCmd(DbgMessage* in_msg) {
456 Dart_Handle res = Dart_SetStepOver();
457 ASSERT_NOT_ERROR(res);
458 HandleResumeCmd(in_msg);
459 }
460
461
462 void DbgMessage::HandleGetLibrariesCmd(DbgMessage* in_msg) {
463 ASSERT(in_msg != NULL);
464 MessageParser msg_parser(in_msg->buffer(), in_msg->buffer_len());
465 int msg_id = msg_parser.MessageId();
466 dart::TextBuffer msg(64);
467 msg.Printf("{ \"id\": %d, \"result\": { \"libraries\": [", msg_id);
468 Dart_Handle lib_ids = Dart_GetLibraryIds();
469 ASSERT_NOT_ERROR(lib_ids);
470 intptr_t num_libs;
471 Dart_Handle res = Dart_ListLength(lib_ids, &num_libs);
472 ASSERT_NOT_ERROR(res);
473 for (int i = 0; i < num_libs; i++) {
474 Dart_Handle lib_id_handle = Dart_ListGetAt(lib_ids, i);
475 ASSERT(Dart_IsInteger(lib_id_handle));
476 int lib_id = GetIntValue(lib_id_handle);
477 Dart_Handle lib_url = Dart_GetLibraryURL(lib_id);
478 ASSERT_NOT_ERROR(lib_url);
479 ASSERT(Dart_IsString(lib_url));
480 msg.Printf("%s{\"id\":%d,\"url\":", (i == 0) ? "" : ", ", lib_id);
481 FormatEncodedString(&msg, lib_url);
482 msg.Printf("}");
483 }
484 msg.Printf("]}}");
485 in_msg->SendReply(&msg);
486 }
487
488
489 void DbgMessage::HandleGetClassPropsCmd(DbgMessage* in_msg) {
490 ASSERT(in_msg != NULL);
491 MessageParser msg_parser(in_msg->buffer(), in_msg->buffer_len());
492 int msg_id = msg_parser.MessageId();
493 intptr_t cls_id = msg_parser.GetIntParam("classId");
494 dart::TextBuffer msg(64);
495 msg.Printf("{\"id\":%d, \"result\":", msg_id);
496 const char* err = FormatClassProps(&msg, cls_id);
497 if (err != NULL) {
498 in_msg->SendErrorReply(msg_id, err);
499 return;
500 }
501 msg.Printf("}");
502 in_msg->SendReply(&msg);
503 }
504
505
506 void DbgMessage::HandleGetLibPropsCmd(DbgMessage* in_msg) {
507 ASSERT(in_msg != NULL);
508 MessageParser msg_parser(in_msg->buffer(), in_msg->buffer_len());
509 int msg_id = msg_parser.MessageId();
510 intptr_t lib_id = msg_parser.GetIntParam("libraryId");
511 dart::TextBuffer msg(64);
512 msg.Printf("{\"id\":%d, \"result\":", msg_id);
513 const char* err = FormatLibraryProps(&msg, lib_id);
514 if (err != NULL) {
515 in_msg->SendErrorReply(msg_id, err);
516 return;
517 }
518 msg.Printf("}");
519 in_msg->SendReply(&msg);
520 }
521
522
523 void DbgMessage::HandleSetLibPropsCmd(DbgMessage* in_msg) {
524 ASSERT(in_msg != NULL);
525 MessageParser msg_parser(in_msg->buffer(), in_msg->buffer_len());
526 int msg_id = msg_parser.MessageId();
527 intptr_t lib_id = msg_parser.GetIntParam("libraryId");
528 const char* enable_request = msg_parser.GetStringParam("debuggingEnabled");
529 bool enable;
530 if (strcmp(enable_request, "true") == 0) {
531 enable = true;
532 } else if (strcmp(enable_request, "false") == 0) {
533 enable = false;
534 } else {
535 in_msg->SendErrorReply(msg_id, "illegal argument for 'debuggingEnabled'");
536 return;
537 }
538 Dart_Handle res = Dart_SetLibraryDebuggable(lib_id, enable);
539 if (Dart_IsError(res)) {
540 in_msg->SendErrorReply(msg_id, Dart_GetError(res));
541 return;
542 }
543 bool enabled = false;
544 res = Dart_GetLibraryDebuggable(lib_id, &enabled);
545 if (Dart_IsError(res)) {
546 in_msg->SendErrorReply(msg_id, Dart_GetError(res));
547 return;
548 }
549 dart::TextBuffer msg(64);
550 msg.Printf("{\"id\":%d, \"result\": {\"debuggingEnabled\": \"%s\"}}",
551 msg_id,
552 enabled ? "true" : "false");
553 in_msg->SendReply(&msg);
554 }
555
556
557 void DbgMessage::HandleGetObjPropsCmd(DbgMessage* in_msg) {
558 ASSERT(in_msg != NULL);
559 MessageParser msg_parser(in_msg->buffer(), in_msg->buffer_len());
560 int msg_id = msg_parser.MessageId();
561 intptr_t obj_id = msg_parser.GetIntParam("objectId");
562 Dart_Handle obj = Dart_GetCachedObject(obj_id);
563 if (Dart_IsError(obj)) {
564 in_msg->SendErrorReply(msg_id, Dart_GetError(obj));
565 return;
566 }
567 dart::TextBuffer msg(64);
568 msg.Printf("{\"id\":%d, \"result\":", msg_id);
569 const char* err = FormatObjProps(&msg, obj);
570 if (err != NULL) {
571 in_msg->SendErrorReply(msg_id, err);
572 return;
573 }
574 msg.Printf("}");
575 in_msg->SendReply(&msg);
576 }
577
578
579 void DbgMessage::HandleGetListCmd(DbgMessage* in_msg) {
580 const intptr_t kDefaultSliceLength = 100;
581 ASSERT(in_msg != NULL);
582 MessageParser msg_parser(in_msg->buffer(), in_msg->buffer_len());
583 int msg_id = msg_parser.MessageId();
584 intptr_t obj_id = msg_parser.GetIntParam("objectId");
585 Dart_Handle list = Dart_GetCachedObject(obj_id);
586 if (Dart_IsError(list)) {
587 in_msg->SendErrorReply(msg_id, Dart_GetError(list));
588 return;
589 }
590 if (!Dart_IsList(list)) {
591 in_msg->SendErrorReply(msg_id, "object is not a list");
592 return;
593 }
594 intptr_t list_length = 0;
595 Dart_Handle res = Dart_ListLength(list, &list_length);
596 if (Dart_IsError(res)) {
597 in_msg->SendErrorReply(msg_id, Dart_GetError(res));
598 return;
599 }
600
601 intptr_t index = msg_parser.GetIntParam("index");
602 if (index < 0) {
603 index = 0;
604 } else if (index > list_length) {
605 index = list_length;
606 }
607
608 // If no slice length is given, get only one element. If slice length
609 // is given as 0, get entire list.
610 intptr_t slice_length = msg_parser.GetOptIntParam("length", 1);
611 if (slice_length == 0) {
612 slice_length = list_length - index;
613 }
614 if ((index + slice_length) > list_length) {
615 slice_length = list_length - index;
616 }
617 ASSERT(slice_length >= 0);
618 if (slice_length > kDefaultSliceLength) {
619 slice_length = kDefaultSliceLength;
620 }
621 dart::TextBuffer msg(64);
622 msg.Printf("{\"id\":%d, \"result\":", msg_id);
623 if (slice_length == 1) {
624 Dart_Handle value = Dart_ListGetAt(list, index);
625 FormatRemoteObj(&msg, value);
626 } else {
627 FormatListSlice(&msg, list, list_length, index, slice_length);
628 }
629 msg.Printf("}");
630 in_msg->SendReply(&msg);
631 }
632
633
634 void DbgMessage::HandleGetGlobalsCmd(DbgMessage* in_msg) {
635 ASSERT(in_msg != NULL);
636 MessageParser msg_parser(in_msg->buffer(), in_msg->buffer_len());
637 int msg_id = msg_parser.MessageId();
638 intptr_t lib_id = msg_parser.GetIntParam("libraryId");
639 dart::TextBuffer msg(64);
640 msg.Printf("{\"id\":%d, \"result\": { \"globals\":", msg_id);
641 Dart_Handle globals = Dart_GetGlobalVariables(lib_id);
642 ASSERT_NOT_ERROR(globals);
643 FormatNamedValueList(&msg, globals);
644 msg.Printf("}}");
645 in_msg->SendReply(&msg);
646 }
647
648
649 void DbgMessage::HandleGetScriptURLsCmd(DbgMessage* in_msg) {
650 ASSERT(in_msg != NULL);
651 MessageParser msg_parser(in_msg->buffer(), in_msg->buffer_len());
652 int msg_id = msg_parser.MessageId();
653 dart::TextBuffer msg(64);
654 intptr_t lib_id = msg_parser.GetIntParam("libraryId");
655 Dart_Handle lib_url = Dart_GetLibraryURL(lib_id);
656 ASSERT_NOT_ERROR(lib_url);
657 Dart_Handle urls = Dart_GetScriptURLs(lib_url);
658 if (Dart_IsError(urls)) {
659 in_msg->SendErrorReply(msg_id, Dart_GetError(urls));
660 return;
661 }
662 ASSERT(Dart_IsList(urls));
663 intptr_t num_urls = 0;
664 Dart_ListLength(urls, &num_urls);
665 msg.Printf("{ \"id\": %d, ", msg_id);
666 msg.Printf("\"result\": { \"urls\": [");
667 for (int i = 0; i < num_urls; i++) {
668 Dart_Handle script_url = Dart_ListGetAt(urls, i);
669 if (i > 0) {
670 msg.Printf(",");
671 }
672 FormatEncodedString(&msg, script_url);
673 }
674 msg.Printf("]}}");
675 in_msg->SendReply(&msg);
676 }
677
678
679 void DbgMessage::HandleGetSourceCmd(DbgMessage* in_msg) {
680 ASSERT(in_msg != NULL);
681 MessageParser msg_parser(in_msg->buffer(), in_msg->buffer_len());
682 int msg_id = msg_parser.MessageId();
683 dart::TextBuffer msg(64);
684 intptr_t lib_id = msg_parser.GetIntParam("libraryId");
685 char* url_chars = msg_parser.GetStringParam("url");
686 ASSERT(url_chars != NULL);
687 Dart_Handle url = Dart_NewString(url_chars);
688 ASSERT_NOT_ERROR(url);
689 free(url_chars);
690 url_chars = NULL;
691 Dart_Handle source = Dart_ScriptGetSource(lib_id, url);
692 if (Dart_IsError(source)) {
693 in_msg->SendErrorReply(msg_id, Dart_GetError(source));
694 return;
695 }
696 msg.Printf("{ \"id\": %d, ", msg_id);
697 msg.Printf("\"result\": { \"text\": ");
698 FormatEncodedString(&msg, source);
699 msg.Printf("}}");
700 in_msg->SendReply(&msg);
701 }
702
703
704 void DbgMessage::HandleGetStackTraceCmd(DbgMessage* in_msg) {
705 ASSERT(in_msg != NULL);
706 MessageParser msg_parser(in_msg->buffer(), in_msg->buffer_len());
707 int msg_id = msg_parser.MessageId();
708 Dart_StackTrace trace;
709 Dart_Handle res = Dart_GetStackTrace(&trace);
710 ASSERT_NOT_ERROR(res);
711 dart::TextBuffer msg(128);
712 msg.Printf("{ \"id\": %d, \"result\": {", msg_id);
713 FormatCallFrames(&msg, trace);
714 msg.Printf("}}");
715 in_msg->SendReply(&msg);
716 }
717
718
719 void DbgMessage::HandleSetBpCmd(DbgMessage* in_msg) {
720 ASSERT(in_msg != NULL);
721 MessageParser msg_parser(in_msg->buffer(), in_msg->buffer_len());
722 int msg_id = msg_parser.MessageId();
723 char* url_chars = msg_parser.GetStringParam("url");
724 ASSERT(url_chars != NULL);
725 Dart_Handle url = Dart_NewString(url_chars);
726 ASSERT_NOT_ERROR(url);
727 free(url_chars);
728 url_chars = NULL;
729 intptr_t line_number = msg_parser.GetIntParam("line");
730 Dart_Handle bp_id = Dart_SetBreakpoint(url, line_number);
731 if (Dart_IsError(bp_id)) {
732 in_msg->SendErrorReply(msg_id, Dart_GetError(bp_id));
733 return;
734 }
735 ASSERT(Dart_IsInteger(bp_id));
736 uint64_t bp_id_value;
737 Dart_Handle res = Dart_IntegerToUint64(bp_id, &bp_id_value);
738 ASSERT_NOT_ERROR(res);
739 dart::TextBuffer msg(64);
740 msg.Printf("{ \"id\": %d, \"result\": { \"breakpointId\": %"Pu64" }}",
741 msg_id, bp_id_value);
742 in_msg->SendReply(&msg);
743 }
744
745
746 void DbgMessage::HandlePauseOnExcCmd(DbgMessage* in_msg) {
747 ASSERT(in_msg != NULL);
748 MessageParser msg_parser(in_msg->buffer(), in_msg->buffer_len());
749 int msg_id = msg_parser.MessageId();
750 char* exc_chars = msg_parser.GetStringParam("exceptions");
751 Dart_ExceptionPauseInfo info = kNoPauseOnExceptions;
752 if (strcmp(exc_chars, "none") == 0) {
753 info = kNoPauseOnExceptions;
754 } else if (strcmp(exc_chars, "all") == 0) {
755 info = kPauseOnAllExceptions;
756 } else if (strcmp(exc_chars, "unhandled") == 0) {
757 info = kPauseOnUnhandledExceptions;
758 } else {
759 in_msg->SendErrorReply(msg_id, "illegal value for parameter 'exceptions'");
760 return;
761 }
762 Dart_Handle res = Dart_SetExceptionPauseInfo(info);
763 ASSERT_NOT_ERROR(res);
764 dart::TextBuffer msg(32);
765 msg.Printf("{ \"id\": %d }", msg_id);
766 in_msg->SendReply(&msg);
767 }
768
769
770 void DbgMessage::HandleRemBpCmd(DbgMessage* in_msg) {
771 ASSERT(in_msg != NULL);
772 MessageParser msg_parser(in_msg->buffer(), in_msg->buffer_len());
773 int msg_id = msg_parser.MessageId();
774 int bpt_id = msg_parser.GetIntParam("breakpointId");
775 Dart_Handle res = Dart_RemoveBreakpoint(bpt_id);
776 if (Dart_IsError(res)) {
777 in_msg->SendErrorReply(msg_id, Dart_GetError(res));
778 return;
779 }
780 dart::TextBuffer msg(32);
781 msg.Printf("{ \"id\": %d }", msg_id);
782 in_msg->SendReply(&msg);
783 }
784
785
786 void DbgMessageQueue::AddMessage(int32_t cmd_idx,
787 const char* start,
788 const char* end,
789 int debug_fd) {
790 if ((end > start) && ((end - start) < kMaxUint32)) {
791 MonitorLocker ml(&msg_queue_lock_);
792 bool notify = (msglist_ == NULL);
793 DbgMessage* msg = new DbgMessage(msglist_, cmd_idx, start, end, debug_fd);
794 msglist_ = msg;
hausner 2012/09/28 17:03:01 It seems that you are inserting the message at the
siva 2012/09/28 23:32:29 Very good point. That was not intentional... Chang
795 if (notify) {
796 ml.Notify();
797 }
798 }
799 }
800
801
802 void DbgMessageQueue::HandleMessages() {
803 bool resume_requested = false;
804 MonitorLocker ml(&msg_queue_lock_);
805 is_running_ = false;
806 while (!resume_requested) {
807 while (msglist_ == NULL) {
808 dart::Monitor::WaitResult res = ml.Wait(); // Wait for debugger commands.
809 ASSERT(res == dart::Monitor::kNotified);
810 }
811 while (msglist_ != NULL) {
812 DbgMessage* msg = msglist_;
813 msglist_ = msglist_->next();
814 msg->HandleMessage();
815 resume_requested = msg->request_resume();
hausner 2012/09/28 17:03:01 This is the only place where you need to know whet
siva 2012/09/28 23:32:29 HandleMessage() now returns true to indicate a res
816 delete msg;
817 }
818 }
819 is_running_ = true;
820 }
821
822
823 void DbgMessageQueue::QueueOutputMsg(dart::TextBuffer* msg) {
824 queued_output_messages_.Printf("%s", msg->buf());
825 }
826
827
828 void DbgMessageQueue::SendQueuedMsgs() {
829 if (queued_output_messages_.length() > 0) {
830 DebuggerConnectionHandler::BroadcastMsg(&queued_output_messages_);
831 queued_output_messages_.Clear();
832 }
833 }
834
835
836 void DbgMessageQueue::SendBreakpointEvent(Dart_StackTrace trace) {
837 dart::TextBuffer msg(128);
838 msg.Printf("{ \"event\": \"paused\", \"params\": { ");
839 msg.Printf("\"reason\": \"breakpoint\", ");
840 FormatCallFrames(&msg, trace);
841 msg.Printf("}}");
842 DebuggerConnectionHandler::BroadcastMsg(&msg);
843 }
844
845
846 void DbgMessageQueue::SendExceptionEvent(Dart_Handle exception,
847 Dart_StackTrace stack_trace) {
848 intptr_t exception_id = Dart_CacheObject(exception);
849 ASSERT(exception_id >= 0);
850 dart::TextBuffer msg(128);
851 msg.Printf("{ \"event\": \"paused\", \"params\": {");
852 msg.Printf("\"reason\": \"exception\", ");
853 msg.Printf("\"exception\":");
854 FormatRemoteObj(&msg, exception);
855 msg.Printf(", ");
856 FormatCallFrames(&msg, stack_trace);
857 msg.Printf("}}");
858 DebuggerConnectionHandler::BroadcastMsg(&msg);
859 }
860
861
862 // TODO(asiva): Get rid of this statis variabel one we have a means
hausner 2012/09/28 17:03:01 static variable
siva 2012/09/28 23:32:29 Done.
863 // for associating an isolate with a debugger message queue object.
864 static DbgMessageQueue* message_queue = NULL;
865
866
867 void DbgMessageQueue::Initialize() {
868 // TODO(asiva): Need to setup a message queue when an Isolate is created.
869 // For now we use a static message queue object as we are only supporting
870 // debugging of a single isolate.
871 message_queue = new DbgMessageQueue();
872
873 // Setup handlers for breakpoints, exceptions and delayed breakpoints.
874 Dart_SetBreakpointHandler(BreakpointHandler);
875 Dart_SetBreakpointResolvedHandler(BptResolvedHandler);
876 Dart_SetExceptionThrownHandler(ExceptionThrownHandler);
877 }
878
879
880 int32_t DbgMessageQueue::LookupIsolateCommand(const char* buf,
881 int32_t buflen) {
882 // Check if we have a isolate specific debugger command.
883 int32_t i = 0;
884 while (debugger_commands[i].cmd_string != NULL) {
885 if (strncmp(buf, debugger_commands[i].cmd_string, buflen) == 0) {
886 return i;
887 }
888 i++;
889 }
890 return kInvalidCommand;
891 }
892
893
894 DbgMessageQueue* DbgMessageQueue::GetIsolateMessageQueue(Dart_Isolate isolate) {
895 // TODO(asiva): Return a message queue corresponding to the isolate.
896 // For now we use a static message queue object as we are only supporting
897 // debugging of a single isolate.
898 return message_queue;
899 }
900
901
902 void DbgMessageQueue::BptResolvedHandler(intptr_t bp_id,
903 Dart_Handle url,
904 intptr_t line_number) {
905 Dart_EnterScope();
906 dart::TextBuffer msg(128);
907 msg.Printf("{ \"event\": \"breakpointResolved\", \"params\": {");
908 msg.Printf("\"breakpointId\": %"Pd", \"url\":", bp_id);
909 FormatEncodedString(&msg, url);
910 msg.Printf(",\"line\": %"Pd" }}", line_number);
911 DbgMessageQueue* msg_queue = GetIsolateMessageQueue(Dart_CurrentIsolate());
912 ASSERT(msg_queue != NULL);
913 msg_queue->QueueOutputMsg(&msg);
914 Dart_ExitScope();
915 }
916
917
918 void DbgMessageQueue::BreakpointHandler(Dart_Breakpoint bpt,
919 Dart_StackTrace trace) {
920 DebuggerConnectionHandler::WaitForConnection();
921 Dart_EnterScope();
922 DbgMessageQueue* msg_queue = GetIsolateMessageQueue(Dart_CurrentIsolate());
923 ASSERT(msg_queue != NULL);
924 msg_queue->SendQueuedMsgs();
925 msg_queue->SendBreakpointEvent(trace);
926 msg_queue->HandleMessages();
927 Dart_ExitScope();
928 }
929
930
931 void DbgMessageQueue::ExceptionThrownHandler(Dart_Handle exception,
932 Dart_StackTrace stack_trace) {
933 DebuggerConnectionHandler::WaitForConnection();
934 Dart_EnterScope();
935 DbgMessageQueue* msg_queue = GetIsolateMessageQueue(Dart_CurrentIsolate());
936 ASSERT(msg_queue != NULL);
937 msg_queue->SendQueuedMsgs();
938 msg_queue->SendExceptionEvent(exception, stack_trace);
939 msg_queue->HandleMessages();
940 Dart_ExitScope();
941 }
OLDNEW
« runtime/bin/dbg_message.h ('K') | « runtime/bin/dbg_message.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698