| 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_MESSAGE_H_ |
| 6 #define BIN_DBG_MESSAGE_H_ |
| 7 |
| 8 #include "bin/builtin.h" |
| 9 #include "bin/utils.h" |
| 10 |
| 11 #include "include/dart_debugger_api.h" |
| 12 |
| 13 #include "platform/globals.h" |
| 14 #include "platform/json.h" |
| 15 #include "platform/thread.h" |
| 16 |
| 17 |
| 18 // TODO(hausner): Need better error handling. |
| 19 #define ASSERT_NOT_ERROR(handle) \ |
| 20 ASSERT(!Dart_IsError(handle)) |
| 21 |
| 22 #define RETURN_IF_ERROR(handle) \ |
| 23 if (Dart_IsError(handle)) { \ |
| 24 return Dart_GetError(handle); \ |
| 25 } |
| 26 |
| 27 |
| 28 // Class to parse a JSON debug command message. |
| 29 class MessageParser { |
| 30 public: |
| 31 MessageParser(const char* buf, int buf_length) |
| 32 : buf_(buf), buf_length_(buf_length) { |
| 33 } |
| 34 ~MessageParser() { } |
| 35 |
| 36 // Accessors. |
| 37 const char* buf() const { return buf_; } |
| 38 |
| 39 bool IsValidMessage() const; |
| 40 int MessageId() const; |
| 41 |
| 42 const char* Params() const; |
| 43 intptr_t GetIntParam(const char* name) const; |
| 44 intptr_t GetOptIntParam(const char* name, intptr_t default_val) const; |
| 45 |
| 46 // GetStringParam mallocs the buffer that it returns. Caller must free. |
| 47 char* GetStringParam(const char* name) const; |
| 48 |
| 49 private: |
| 50 const char* buf_; |
| 51 int buf_length_; |
| 52 |
| 53 DISALLOW_COPY_AND_ASSIGN(MessageParser); |
| 54 }; |
| 55 |
| 56 |
| 57 // Class which represents a debug command message and handles it. |
| 58 class DbgMessage { |
| 59 public: |
| 60 DbgMessage(int32_t cmd_idx, const char* start, |
| 61 const char* end, int debug_fd) |
| 62 : next_(NULL), cmd_idx_(cmd_idx), |
| 63 buffer_(NULL), buffer_len_(end - start), |
| 64 debug_fd_(debug_fd) { |
| 65 buffer_ = reinterpret_cast<char*>(malloc(buffer_len_)); |
| 66 ASSERT(buffer_ != NULL); |
| 67 memmove(buffer_, start, buffer_len_); |
| 68 } |
| 69 ~DbgMessage() { |
| 70 next_ = NULL; |
| 71 free(buffer_); |
| 72 } |
| 73 |
| 74 // Accessors. |
| 75 DbgMessage* next() const { return next_; } |
| 76 void set_next(DbgMessage* next) { next_ = next; } |
| 77 char* buffer() const { return buffer_; } |
| 78 int32_t buffer_len() const { return buffer_len_; } |
| 79 int debug_fd() const { return debug_fd_; } |
| 80 |
| 81 // Handle debugger command message. |
| 82 // Returns true if the execution needs to resume after this message is |
| 83 // handled, false otherwise. |
| 84 bool HandleMessage(); |
| 85 |
| 86 // Send reply after successful handling of command. |
| 87 void SendReply(dart::TextBuffer* msg); |
| 88 |
| 89 // Reply error returned by the command handler. |
| 90 void SendErrorReply(int msg_id, const char* err_msg); |
| 91 |
| 92 // Handlers for specific commands, they are static because these |
| 93 // functions are populated as function pointers into a dispatch table. |
| 94 static bool HandleResumeCmd(DbgMessage* msg); |
| 95 static bool HandleStepIntoCmd(DbgMessage* msg); |
| 96 static bool HandleStepOverCmd(DbgMessage* msg); |
| 97 static bool HandleStepOutCmd(DbgMessage* msg); |
| 98 static bool HandleGetLibrariesCmd(DbgMessage* msg); |
| 99 static bool HandleGetClassPropsCmd(DbgMessage* msg); |
| 100 static bool HandleGetLibPropsCmd(DbgMessage* msg); |
| 101 static bool HandleSetLibPropsCmd(DbgMessage* msg); |
| 102 static bool HandleGetGlobalsCmd(DbgMessage* msg); |
| 103 static bool HandleGetObjPropsCmd(DbgMessage* msg); |
| 104 static bool HandleGetListCmd(DbgMessage* msg); |
| 105 static bool HandleGetScriptURLsCmd(DbgMessage* msg); |
| 106 static bool HandleGetSourceCmd(DbgMessage* msg); |
| 107 static bool HandleGetStackTraceCmd(DbgMessage* msg); |
| 108 static bool HandlePauseOnExcCmd(DbgMessage* msg); |
| 109 static bool HandleSetBpCmd(DbgMessage* msg); |
| 110 static bool HandleRemBpCmd(DbgMessage* msg); |
| 111 |
| 112 private: |
| 113 DbgMessage* next_; // Next message in the queue. |
| 114 int32_t cmd_idx_; // Isolate specific debugger command index. |
| 115 char* buffer_; // Debugger command message. |
| 116 int32_t buffer_len_; // Length of the debugger command message. |
| 117 int debug_fd_; // Debugger connection on which replies are to be sent. |
| 118 |
| 119 DISALLOW_IMPLICIT_CONSTRUCTORS(DbgMessage); |
| 120 }; |
| 121 |
| 122 |
| 123 // Class which represents a queue of debug command messages sent to an isolate. |
| 124 class DbgMessageQueue { |
| 125 public: |
| 126 static const int32_t kInvalidCommand = -1; |
| 127 |
| 128 DbgMessageQueue() : is_running_(true), |
| 129 is_interrupted_(false), |
| 130 msglist_head_(NULL), |
| 131 msglist_tail_(NULL), |
| 132 queued_output_messages_(64) { |
| 133 } |
| 134 ~DbgMessageQueue() { |
| 135 // Cleanup the message queue |
| 136 } |
| 137 |
| 138 // Add specified debug command message to the queue. |
| 139 void AddMessage(int32_t cmd_idx, |
| 140 const char* start, |
| 141 const char* end, |
| 142 int debug_fd); |
| 143 |
| 144 // Handle all debug command messages in the queue. |
| 145 void HandleMessages(); |
| 146 |
| 147 // Queue output messages, these are sent out when we hit an event. |
| 148 void QueueOutputMsg(dart::TextBuffer* msg); |
| 149 |
| 150 // Send all queued messages over to the debugger. |
| 151 void SendQueuedMsgs(); |
| 152 |
| 153 // Send breakpoint event message over to the debugger. |
| 154 void SendBreakpointEvent(Dart_StackTrace trace); |
| 155 |
| 156 // Send Exception event message over to the debugger. |
| 157 void SendExceptionEvent(Dart_Handle exception, Dart_StackTrace trace); |
| 158 |
| 159 // Initialize the message queue processing by setting up the appropriate |
| 160 // handlers. |
| 161 static void Initialize(); |
| 162 |
| 163 // Parses the input message and returns the command index if the message |
| 164 // contains a valid isolate specific command. |
| 165 // It returns kInvalidCommand otherwise. |
| 166 static int32_t LookupIsolateCommand(const char* buf, int32_t buflen); |
| 167 |
| 168 // Get Debugger Message Queue corresponding to the Isolate. |
| 169 static DbgMessageQueue* GetIsolateMessageQueue(Dart_Isolate isolate); |
| 170 |
| 171 // Generic handlers for breakpoints, exceptions and delayed breakpoint |
| 172 // resolution. |
| 173 static void BptResolvedHandler(intptr_t bp_id, |
| 174 Dart_Handle url, |
| 175 intptr_t line_number); |
| 176 static void BreakpointHandler(Dart_Breakpoint bpt, Dart_StackTrace trace); |
| 177 static void ExceptionThrownHandler(Dart_Handle exception, |
| 178 Dart_StackTrace stack_trace); |
| 179 static void IsolateEventHandler(Dart_Isolate isolate, |
| 180 Dart_IsolateEvent kind); |
| 181 |
| 182 private: |
| 183 bool is_running_; // True if isolate is running and not in the debugger loop. |
| 184 bool is_interrupted_; // True if interrupt command is pending. |
| 185 |
| 186 DbgMessage* msglist_head_; // Start of debugger messages list. |
| 187 DbgMessage* msglist_tail_; // End of debugger messages list. |
| 188 |
| 189 // Text buffer that accumulates messages to be output. |
| 190 dart::TextBuffer queued_output_messages_; |
| 191 |
| 192 // The waits on this condition variable when it needs to process |
| 193 // debug messages. |
| 194 dart::Monitor msg_queue_lock_; |
| 195 |
| 196 DISALLOW_COPY_AND_ASSIGN(DbgMessageQueue); |
| 197 }; |
| 198 |
| 199 #endif // BIN_DBG_MESSAGE_H_ |
| OLD | NEW |