Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef V8_DEBUG_DEBUG_H_ |
| 6 | #define V8_DEBUG_DEBUG_H_ |
| 7 | |
| 8 | #include "src/allocation.h" |
| 9 | #include "src/arguments.h" |
| 10 | #include "src/assembler.h" |
| 11 | #include "src/base/atomicops.h" |
| 12 | #include "src/base/platform/platform.h" |
| 13 | #include "src/debug/liveedit.h" |
| 14 | #include "src/execution.h" |
| 15 | #include "src/factory.h" |
| 16 | #include "src/flags.h" |
| 17 | #include "src/frames.h" |
| 18 | #include "src/hashmap.h" |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 19 | #include "src/interpreter/source-position-table.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 20 | #include "src/runtime/runtime.h" |
| 21 | #include "src/string-stream.h" |
| 22 | #include "src/v8threads.h" |
| 23 | |
| 24 | #include "include/v8-debug.h" |
| 25 | |
| 26 | namespace v8 { |
| 27 | namespace internal { |
| 28 | |
| 29 | |
| 30 | // Forward declarations. |
| 31 | class DebugScope; |
| 32 | |
| 33 | |
| 34 | // Step actions. NOTE: These values are in macros.py as well. |
| 35 | enum StepAction : int8_t { |
| 36 | StepNone = -1, // Stepping not prepared. |
| 37 | StepOut = 0, // Step out of the current function. |
| 38 | StepNext = 1, // Step to the next statement in the current function. |
| 39 | StepIn = 2, // Step into new functions invoked or the next statement |
| 40 | // in the current function. |
| 41 | StepFrame = 3 // Step into a new frame or return to previous frame. |
| 42 | }; |
| 43 | |
| 44 | |
| 45 | // Type of exception break. NOTE: These values are in macros.py as well. |
| 46 | enum ExceptionBreakType { |
| 47 | BreakException = 0, |
| 48 | BreakUncaughtException = 1 |
| 49 | }; |
| 50 | |
| 51 | |
| 52 | // Type of exception break. |
| 53 | enum BreakLocatorType { ALL_BREAK_LOCATIONS, CALLS_AND_RETURNS }; |
| 54 | |
| 55 | |
| 56 | // The different types of breakpoint position alignments. |
| 57 | // Must match Debug.BreakPositionAlignment in debug.js |
| 58 | enum BreakPositionAlignment { |
| 59 | STATEMENT_ALIGNED = 0, |
| 60 | BREAK_POSITION_ALIGNED = 1 |
| 61 | }; |
| 62 | |
| 63 | |
| 64 | class BreakLocation { |
| 65 | public: |
| 66 | // Find the break point at the supplied address, or the closest one before |
| 67 | // the address. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 68 | static BreakLocation FromCodeOffset(Handle<DebugInfo> debug_info, int offset); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 69 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 70 | static BreakLocation FromFrame(Handle<DebugInfo> debug_info, |
| 71 | JavaScriptFrame* frame); |
| 72 | |
| 73 | static void FromCodeOffsetSameStatement(Handle<DebugInfo> debug_info, |
| 74 | int offset, |
| 75 | List<BreakLocation>* result_out); |
| 76 | |
| 77 | static void AllForStatementPosition(Handle<DebugInfo> debug_info, |
| 78 | int statement_position, |
| 79 | List<BreakLocation>* result_out); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 80 | |
| 81 | static BreakLocation FromPosition(Handle<DebugInfo> debug_info, int position, |
| 82 | BreakPositionAlignment alignment); |
| 83 | |
| 84 | bool IsDebugBreak() const; |
| 85 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 86 | inline bool IsReturn() const { return type_ == DEBUG_BREAK_SLOT_AT_RETURN; } |
| 87 | inline bool IsCall() const { return type_ == DEBUG_BREAK_SLOT_AT_CALL; } |
| 88 | inline bool IsDebugBreakSlot() const { return type_ >= DEBUG_BREAK_SLOT; } |
| 89 | inline bool IsDebuggerStatement() const { |
| 90 | return type_ == DEBUGGER_STATEMENT; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 91 | } |
| 92 | inline bool HasBreakPoint() const { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 93 | return debug_info_->HasBreakPoint(code_offset_); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | Handle<Object> BreakPointObjects() const; |
| 97 | |
| 98 | void SetBreakPoint(Handle<Object> break_point_object); |
| 99 | void ClearBreakPoint(Handle<Object> break_point_object); |
| 100 | |
| 101 | void SetOneShot(); |
| 102 | void ClearOneShot(); |
| 103 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 104 | inline int position() const { return position_; } |
| 105 | inline int statement_position() const { return statement_position_; } |
| 106 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 107 | inline int code_offset() const { return code_offset_; } |
| 108 | inline Isolate* isolate() { return debug_info_->GetIsolate(); } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 109 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 110 | inline AbstractCode* abstract_code() const { |
| 111 | return debug_info_->abstract_code(); |
| 112 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 113 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 114 | protected: |
| 115 | enum DebugBreakType { |
| 116 | NOT_DEBUG_BREAK, |
| 117 | DEBUGGER_STATEMENT, |
| 118 | DEBUG_BREAK_SLOT, |
| 119 | DEBUG_BREAK_SLOT_AT_CALL, |
| 120 | DEBUG_BREAK_SLOT_AT_RETURN |
| 121 | }; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 122 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 123 | BreakLocation(Handle<DebugInfo> debug_info, DebugBreakType type, |
| 124 | int code_offset, int position, int statement_position); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 125 | |
| 126 | class Iterator { |
| 127 | public: |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 128 | virtual ~Iterator() {} |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 129 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 130 | virtual BreakLocation GetBreakLocation() = 0; |
| 131 | virtual bool Done() const = 0; |
| 132 | virtual void Next() = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 133 | |
| 134 | void SkipTo(int count) { |
| 135 | while (count-- > 0) Next(); |
| 136 | } |
| 137 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 138 | virtual int code_offset() = 0; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 139 | int break_index() const { return break_index_; } |
| 140 | inline int position() const { return position_; } |
| 141 | inline int statement_position() const { return statement_position_; } |
| 142 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 143 | protected: |
| 144 | explicit Iterator(Handle<DebugInfo> debug_info); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 145 | |
| 146 | Handle<DebugInfo> debug_info_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 147 | int break_index_; |
| 148 | int position_; |
| 149 | int statement_position_; |
| 150 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 151 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 152 | DisallowHeapAllocation no_gc_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 153 | DISALLOW_COPY_AND_ASSIGN(Iterator); |
| 154 | }; |
| 155 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 156 | class CodeIterator : public Iterator { |
| 157 | public: |
| 158 | CodeIterator(Handle<DebugInfo> debug_info, BreakLocatorType type); |
| 159 | ~CodeIterator() override {} |
| 160 | |
| 161 | BreakLocation GetBreakLocation() override; |
| 162 | bool Done() const override { return reloc_iterator_.done(); } |
| 163 | void Next() override; |
| 164 | |
| 165 | int code_offset() override { |
| 166 | return static_cast<int>( |
| 167 | rinfo()->pc() - |
| 168 | debug_info_->abstract_code()->GetCode()->instruction_start()); |
| 169 | } |
| 170 | |
| 171 | private: |
| 172 | static int GetModeMask(BreakLocatorType type); |
| 173 | RelocInfo::Mode rmode() { return reloc_iterator_.rinfo()->rmode(); } |
| 174 | RelocInfo* rinfo() { return reloc_iterator_.rinfo(); } |
| 175 | |
| 176 | RelocIterator reloc_iterator_; |
| 177 | DISALLOW_COPY_AND_ASSIGN(CodeIterator); |
| 178 | }; |
| 179 | |
| 180 | class BytecodeArrayIterator : public Iterator { |
| 181 | public: |
| 182 | BytecodeArrayIterator(Handle<DebugInfo> debug_info, BreakLocatorType type); |
| 183 | ~BytecodeArrayIterator() override {} |
| 184 | |
| 185 | BreakLocation GetBreakLocation() override; |
| 186 | bool Done() const override { return source_position_iterator_.done(); } |
| 187 | void Next() override; |
| 188 | |
| 189 | int code_offset() override { |
| 190 | return source_position_iterator_.bytecode_offset(); |
| 191 | } |
| 192 | |
| 193 | private: |
| 194 | DebugBreakType GetDebugBreakType(); |
| 195 | |
| 196 | interpreter::SourcePositionTableIterator source_position_iterator_; |
| 197 | BreakLocatorType break_locator_type_; |
| 198 | int start_position_; |
| 199 | DISALLOW_COPY_AND_ASSIGN(BytecodeArrayIterator); |
| 200 | }; |
| 201 | |
| 202 | static Iterator* GetIterator(Handle<DebugInfo> debug_info, |
| 203 | BreakLocatorType type = ALL_BREAK_LOCATIONS); |
| 204 | |
| 205 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 206 | friend class Debug; |
| 207 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 208 | static int BreakIndexFromCodeOffset(Handle<DebugInfo> debug_info, int offset); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 209 | |
| 210 | void SetDebugBreak(); |
| 211 | void ClearDebugBreak(); |
| 212 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 213 | Handle<DebugInfo> debug_info_; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 214 | int code_offset_; |
| 215 | DebugBreakType type_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 216 | int position_; |
| 217 | int statement_position_; |
| 218 | }; |
| 219 | |
| 220 | |
| 221 | // Linked list holding debug info objects. The debug info objects are kept as |
| 222 | // weak handles to avoid a debug info object to keep a function alive. |
| 223 | class DebugInfoListNode { |
| 224 | public: |
| 225 | explicit DebugInfoListNode(DebugInfo* debug_info); |
| 226 | ~DebugInfoListNode(); |
| 227 | |
| 228 | DebugInfoListNode* next() { return next_; } |
| 229 | void set_next(DebugInfoListNode* next) { next_ = next; } |
| 230 | Handle<DebugInfo> debug_info() { return Handle<DebugInfo>(debug_info_); } |
| 231 | |
| 232 | private: |
| 233 | // Global (weak) handle to the debug info object. |
| 234 | DebugInfo** debug_info_; |
| 235 | |
| 236 | // Next pointer for linked list. |
| 237 | DebugInfoListNode* next_; |
| 238 | }; |
| 239 | |
| 240 | |
| 241 | |
| 242 | // Message delivered to the message handler callback. This is either a debugger |
| 243 | // event or the response to a command. |
| 244 | class MessageImpl: public v8::Debug::Message { |
| 245 | public: |
| 246 | // Create a message object for a debug event. |
| 247 | static MessageImpl NewEvent(DebugEvent event, |
| 248 | bool running, |
| 249 | Handle<JSObject> exec_state, |
| 250 | Handle<JSObject> event_data); |
| 251 | |
| 252 | // Create a message object for the response to a debug command. |
| 253 | static MessageImpl NewResponse(DebugEvent event, |
| 254 | bool running, |
| 255 | Handle<JSObject> exec_state, |
| 256 | Handle<JSObject> event_data, |
| 257 | Handle<String> response_json, |
| 258 | v8::Debug::ClientData* client_data); |
| 259 | |
| 260 | // Implementation of interface v8::Debug::Message. |
| 261 | virtual bool IsEvent() const; |
| 262 | virtual bool IsResponse() const; |
| 263 | virtual DebugEvent GetEvent() const; |
| 264 | virtual bool WillStartRunning() const; |
| 265 | virtual v8::Local<v8::Object> GetExecutionState() const; |
| 266 | virtual v8::Local<v8::Object> GetEventData() const; |
| 267 | virtual v8::Local<v8::String> GetJSON() const; |
| 268 | virtual v8::Local<v8::Context> GetEventContext() const; |
| 269 | virtual v8::Debug::ClientData* GetClientData() const; |
| 270 | virtual v8::Isolate* GetIsolate() const; |
| 271 | |
| 272 | private: |
| 273 | MessageImpl(bool is_event, |
| 274 | DebugEvent event, |
| 275 | bool running, |
| 276 | Handle<JSObject> exec_state, |
| 277 | Handle<JSObject> event_data, |
| 278 | Handle<String> response_json, |
| 279 | v8::Debug::ClientData* client_data); |
| 280 | |
| 281 | bool is_event_; // Does this message represent a debug event? |
| 282 | DebugEvent event_; // Debug event causing the break. |
| 283 | bool running_; // Will the VM start running after this event? |
| 284 | Handle<JSObject> exec_state_; // Current execution state. |
| 285 | Handle<JSObject> event_data_; // Data associated with the event. |
| 286 | Handle<String> response_json_; // Response JSON if message holds a response. |
| 287 | v8::Debug::ClientData* client_data_; // Client data passed with the request. |
| 288 | }; |
| 289 | |
| 290 | |
| 291 | // Details of the debug event delivered to the debug event listener. |
| 292 | class EventDetailsImpl : public v8::Debug::EventDetails { |
| 293 | public: |
| 294 | EventDetailsImpl(DebugEvent event, |
| 295 | Handle<JSObject> exec_state, |
| 296 | Handle<JSObject> event_data, |
| 297 | Handle<Object> callback_data, |
| 298 | v8::Debug::ClientData* client_data); |
| 299 | virtual DebugEvent GetEvent() const; |
| 300 | virtual v8::Local<v8::Object> GetExecutionState() const; |
| 301 | virtual v8::Local<v8::Object> GetEventData() const; |
| 302 | virtual v8::Local<v8::Context> GetEventContext() const; |
| 303 | virtual v8::Local<v8::Value> GetCallbackData() const; |
| 304 | virtual v8::Debug::ClientData* GetClientData() const; |
| 305 | private: |
| 306 | DebugEvent event_; // Debug event causing the break. |
| 307 | Handle<JSObject> exec_state_; // Current execution state. |
| 308 | Handle<JSObject> event_data_; // Data associated with the event. |
| 309 | Handle<Object> callback_data_; // User data passed with the callback |
| 310 | // when it was registered. |
| 311 | v8::Debug::ClientData* client_data_; // Data passed to DebugBreakForCommand. |
| 312 | }; |
| 313 | |
| 314 | |
| 315 | // Message send by user to v8 debugger or debugger output message. |
| 316 | // In addition to command text it may contain a pointer to some user data |
| 317 | // which are expected to be passed along with the command reponse to message |
| 318 | // handler. |
| 319 | class CommandMessage { |
| 320 | public: |
| 321 | static CommandMessage New(const Vector<uint16_t>& command, |
| 322 | v8::Debug::ClientData* data); |
| 323 | CommandMessage(); |
| 324 | |
| 325 | // Deletes user data and disposes of the text. |
| 326 | void Dispose(); |
| 327 | Vector<uint16_t> text() const { return text_; } |
| 328 | v8::Debug::ClientData* client_data() const { return client_data_; } |
| 329 | private: |
| 330 | CommandMessage(const Vector<uint16_t>& text, |
| 331 | v8::Debug::ClientData* data); |
| 332 | |
| 333 | Vector<uint16_t> text_; |
| 334 | v8::Debug::ClientData* client_data_; |
| 335 | }; |
| 336 | |
| 337 | |
| 338 | // A Queue of CommandMessage objects. A thread-safe version is |
| 339 | // LockingCommandMessageQueue, based on this class. |
| 340 | class CommandMessageQueue BASE_EMBEDDED { |
| 341 | public: |
| 342 | explicit CommandMessageQueue(int size); |
| 343 | ~CommandMessageQueue(); |
| 344 | bool IsEmpty() const { return start_ == end_; } |
| 345 | CommandMessage Get(); |
| 346 | void Put(const CommandMessage& message); |
| 347 | void Clear() { start_ = end_ = 0; } // Queue is empty after Clear(). |
| 348 | private: |
| 349 | // Doubles the size of the message queue, and copies the messages. |
| 350 | void Expand(); |
| 351 | |
| 352 | CommandMessage* messages_; |
| 353 | int start_; |
| 354 | int end_; |
| 355 | int size_; // The size of the queue buffer. Queue can hold size-1 messages. |
| 356 | }; |
| 357 | |
| 358 | |
| 359 | // LockingCommandMessageQueue is a thread-safe circular buffer of CommandMessage |
| 360 | // messages. The message data is not managed by LockingCommandMessageQueue. |
| 361 | // Pointers to the data are passed in and out. Implemented by adding a |
| 362 | // Mutex to CommandMessageQueue. Includes logging of all puts and gets. |
| 363 | class LockingCommandMessageQueue BASE_EMBEDDED { |
| 364 | public: |
| 365 | LockingCommandMessageQueue(Logger* logger, int size); |
| 366 | bool IsEmpty() const; |
| 367 | CommandMessage Get(); |
| 368 | void Put(const CommandMessage& message); |
| 369 | void Clear(); |
| 370 | private: |
| 371 | Logger* logger_; |
| 372 | CommandMessageQueue queue_; |
| 373 | mutable base::Mutex mutex_; |
| 374 | DISALLOW_COPY_AND_ASSIGN(LockingCommandMessageQueue); |
| 375 | }; |
| 376 | |
| 377 | |
| 378 | class DebugFeatureTracker { |
| 379 | public: |
| 380 | enum Feature { |
| 381 | kActive = 1, |
| 382 | kBreakPoint = 2, |
| 383 | kStepping = 3, |
| 384 | kHeapSnapshot = 4, |
| 385 | kAllocationTracking = 5, |
| 386 | kProfiler = 6, |
| 387 | kLiveEdit = 7, |
| 388 | }; |
| 389 | |
| 390 | explicit DebugFeatureTracker(Isolate* isolate) |
| 391 | : isolate_(isolate), bitfield_(0) {} |
| 392 | void Track(Feature feature); |
| 393 | |
| 394 | private: |
| 395 | Isolate* isolate_; |
| 396 | uint32_t bitfield_; |
| 397 | }; |
| 398 | |
| 399 | |
| 400 | // This class contains the debugger support. The main purpose is to handle |
| 401 | // setting break points in the code. |
| 402 | // |
| 403 | // This class controls the debug info for all functions which currently have |
| 404 | // active breakpoints in them. This debug info is held in the heap root object |
| 405 | // debug_info which is a FixedArray. Each entry in this list is of class |
| 406 | // DebugInfo. |
| 407 | class Debug { |
| 408 | public: |
| 409 | // Debug event triggers. |
| 410 | void OnDebugBreak(Handle<Object> break_points_hit, bool auto_continue); |
| 411 | |
| 412 | void OnThrow(Handle<Object> exception); |
| 413 | void OnPromiseReject(Handle<JSObject> promise, Handle<Object> value); |
| 414 | void OnCompileError(Handle<Script> script); |
| 415 | void OnBeforeCompile(Handle<Script> script); |
| 416 | void OnAfterCompile(Handle<Script> script); |
| 417 | void OnPromiseEvent(Handle<JSObject> data); |
| 418 | void OnAsyncTaskEvent(Handle<JSObject> data); |
| 419 | |
| 420 | // API facing. |
| 421 | void SetEventListener(Handle<Object> callback, Handle<Object> data); |
| 422 | void SetMessageHandler(v8::Debug::MessageHandler handler); |
| 423 | void EnqueueCommandMessage(Vector<const uint16_t> command, |
| 424 | v8::Debug::ClientData* client_data = NULL); |
| 425 | MUST_USE_RESULT MaybeHandle<Object> Call(Handle<Object> fun, |
| 426 | Handle<Object> data); |
| 427 | Handle<Context> GetDebugContext(); |
| 428 | void HandleDebugBreak(); |
| 429 | void ProcessDebugMessages(bool debug_command_only); |
| 430 | |
| 431 | // Internal logic |
| 432 | bool Load(); |
| 433 | void Break(Arguments args, JavaScriptFrame*); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 434 | Object* SetAfterBreakTarget(JavaScriptFrame* frame); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 435 | |
| 436 | // Scripts handling. |
| 437 | Handle<FixedArray> GetLoadedScripts(); |
| 438 | |
| 439 | // Break point handling. |
| 440 | bool SetBreakPoint(Handle<JSFunction> function, |
| 441 | Handle<Object> break_point_object, |
| 442 | int* source_position); |
| 443 | bool SetBreakPointForScript(Handle<Script> script, |
| 444 | Handle<Object> break_point_object, |
| 445 | int* source_position, |
| 446 | BreakPositionAlignment alignment); |
| 447 | void ClearBreakPoint(Handle<Object> break_point_object); |
| 448 | void ClearAllBreakPoints(); |
| 449 | void FloodWithOneShot(Handle<JSFunction> function, |
| 450 | BreakLocatorType type = ALL_BREAK_LOCATIONS); |
| 451 | void ChangeBreakOnException(ExceptionBreakType type, bool enable); |
| 452 | bool IsBreakOnException(ExceptionBreakType type); |
| 453 | |
| 454 | // Stepping handling. |
| 455 | void PrepareStep(StepAction step_action); |
| 456 | void PrepareStepIn(Handle<JSFunction> function); |
| 457 | void PrepareStepOnThrow(); |
| 458 | void ClearStepping(); |
| 459 | void ClearStepOut(); |
| 460 | void EnableStepIn(); |
| 461 | |
| 462 | void GetStepinPositions(JavaScriptFrame* frame, StackFrame::Id frame_id, |
| 463 | List<int>* results_out); |
| 464 | |
| 465 | bool PrepareFunctionForBreakPoints(Handle<SharedFunctionInfo> shared); |
| 466 | |
| 467 | // Returns whether the operation succeeded. Compilation can only be triggered |
| 468 | // if a valid closure is passed as the second argument, otherwise the shared |
| 469 | // function needs to be compiled already. |
| 470 | bool EnsureDebugInfo(Handle<SharedFunctionInfo> shared, |
| 471 | Handle<JSFunction> function); |
| 472 | void CreateDebugInfo(Handle<SharedFunctionInfo> shared); |
| 473 | static Handle<DebugInfo> GetDebugInfo(Handle<SharedFunctionInfo> shared); |
| 474 | |
| 475 | template <typename C> |
| 476 | bool CompileToRevealInnerFunctions(C* compilable); |
| 477 | |
| 478 | // This function is used in FunctionNameUsing* tests. |
| 479 | Handle<Object> FindSharedFunctionInfoInScript(Handle<Script> script, |
| 480 | int position); |
| 481 | |
| 482 | static Handle<Object> GetSourceBreakLocations( |
| 483 | Handle<SharedFunctionInfo> shared, |
| 484 | BreakPositionAlignment position_aligment); |
| 485 | |
| 486 | // Check whether a global object is the debug global object. |
| 487 | bool IsDebugGlobal(JSGlobalObject* global); |
| 488 | |
| 489 | // Check whether this frame is just about to return. |
| 490 | bool IsBreakAtReturn(JavaScriptFrame* frame); |
| 491 | |
| 492 | // Support for LiveEdit |
| 493 | void FramesHaveBeenDropped(StackFrame::Id new_break_frame_id, |
| 494 | LiveEdit::FrameDropMode mode); |
| 495 | |
| 496 | // Threading support. |
| 497 | char* ArchiveDebug(char* to); |
| 498 | char* RestoreDebug(char* from); |
| 499 | static int ArchiveSpacePerThread(); |
| 500 | void FreeThreadResources() { } |
| 501 | |
| 502 | // Record function from which eval was called. |
| 503 | static void RecordEvalCaller(Handle<Script> script); |
| 504 | |
| 505 | bool CheckExecutionState(int id) { |
| 506 | return is_active() && !debug_context().is_null() && break_id() != 0 && |
| 507 | break_id() == id; |
| 508 | } |
| 509 | |
| 510 | // Flags and states. |
| 511 | DebugScope* debugger_entry() { |
| 512 | return reinterpret_cast<DebugScope*>( |
| 513 | base::NoBarrier_Load(&thread_local_.current_debug_scope_)); |
| 514 | } |
| 515 | inline Handle<Context> debug_context() { return debug_context_; } |
| 516 | |
| 517 | void set_live_edit_enabled(bool v) { live_edit_enabled_ = v; } |
| 518 | bool live_edit_enabled() const { |
| 519 | return FLAG_enable_liveedit && live_edit_enabled_; |
| 520 | } |
| 521 | |
| 522 | inline bool is_active() const { return is_active_; } |
| 523 | inline bool is_loaded() const { return !debug_context_.is_null(); } |
| 524 | inline bool in_debug_scope() const { |
| 525 | return !!base::NoBarrier_Load(&thread_local_.current_debug_scope_); |
| 526 | } |
| 527 | void set_break_points_active(bool v) { break_points_active_ = v; } |
| 528 | bool break_points_active() const { return break_points_active_; } |
| 529 | |
| 530 | StackFrame::Id break_frame_id() { return thread_local_.break_frame_id_; } |
| 531 | int break_id() { return thread_local_.break_id_; } |
| 532 | |
| 533 | // Support for embedding into generated code. |
| 534 | Address is_active_address() { |
| 535 | return reinterpret_cast<Address>(&is_active_); |
| 536 | } |
| 537 | |
| 538 | Address after_break_target_address() { |
| 539 | return reinterpret_cast<Address>(&after_break_target_); |
| 540 | } |
| 541 | |
| 542 | Address step_in_enabled_address() { |
| 543 | return reinterpret_cast<Address>(&thread_local_.step_in_enabled_); |
| 544 | } |
| 545 | |
| 546 | StepAction last_step_action() { return thread_local_.last_step_action_; } |
| 547 | |
| 548 | DebugFeatureTracker* feature_tracker() { return &feature_tracker_; } |
| 549 | |
| 550 | private: |
| 551 | explicit Debug(Isolate* isolate); |
| 552 | |
| 553 | void UpdateState(); |
| 554 | void Unload(); |
| 555 | void SetNextBreakId() { |
| 556 | thread_local_.break_id_ = ++thread_local_.break_count_; |
| 557 | } |
| 558 | |
| 559 | // Check whether there are commands in the command queue. |
| 560 | inline bool has_commands() const { return !command_queue_.IsEmpty(); } |
| 561 | inline bool ignore_events() const { return is_suppressed_ || !is_active_; } |
| 562 | inline bool break_disabled() const { |
| 563 | return break_disabled_ || in_debug_event_listener_; |
| 564 | } |
| 565 | |
| 566 | void OnException(Handle<Object> exception, Handle<Object> promise); |
| 567 | |
| 568 | // Constructors for debug event objects. |
| 569 | MUST_USE_RESULT MaybeHandle<Object> MakeExecutionState(); |
| 570 | MUST_USE_RESULT MaybeHandle<Object> MakeBreakEvent( |
| 571 | Handle<Object> break_points_hit); |
| 572 | MUST_USE_RESULT MaybeHandle<Object> MakeExceptionEvent( |
| 573 | Handle<Object> exception, |
| 574 | bool uncaught, |
| 575 | Handle<Object> promise); |
| 576 | MUST_USE_RESULT MaybeHandle<Object> MakeCompileEvent( |
| 577 | Handle<Script> script, v8::DebugEvent type); |
| 578 | MUST_USE_RESULT MaybeHandle<Object> MakePromiseEvent( |
| 579 | Handle<JSObject> promise_event); |
| 580 | MUST_USE_RESULT MaybeHandle<Object> MakeAsyncTaskEvent( |
| 581 | Handle<JSObject> task_event); |
| 582 | |
| 583 | // Mirror cache handling. |
| 584 | void ClearMirrorCache(); |
| 585 | |
| 586 | MaybeHandle<Object> PromiseHasUserDefinedRejectHandler( |
| 587 | Handle<JSObject> promise); |
| 588 | |
| 589 | void CallEventCallback(v8::DebugEvent event, |
| 590 | Handle<Object> exec_state, |
| 591 | Handle<Object> event_data, |
| 592 | v8::Debug::ClientData* client_data); |
| 593 | void ProcessCompileEvent(v8::DebugEvent event, Handle<Script> script); |
| 594 | void ProcessDebugEvent(v8::DebugEvent event, |
| 595 | Handle<JSObject> event_data, |
| 596 | bool auto_continue); |
| 597 | void NotifyMessageHandler(v8::DebugEvent event, |
| 598 | Handle<JSObject> exec_state, |
| 599 | Handle<JSObject> event_data, |
| 600 | bool auto_continue); |
| 601 | void InvokeMessageHandler(MessageImpl message); |
| 602 | |
| 603 | void ClearOneShot(); |
| 604 | void ActivateStepOut(StackFrame* frame); |
| 605 | void RemoveDebugInfoAndClearFromShared(Handle<DebugInfo> debug_info); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 606 | Handle<Object> CheckBreakPoints(BreakLocation* location, |
| 607 | bool* has_break_points = nullptr); |
| 608 | bool IsMutedAtCurrentLocation(JavaScriptFrame* frame); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 609 | bool CheckBreakPoint(Handle<Object> break_point_object); |
| 610 | MaybeHandle<Object> CallFunction(const char* name, int argc, |
| 611 | Handle<Object> args[]); |
| 612 | |
| 613 | inline void AssertDebugContext() { |
| 614 | DCHECK(isolate_->context() == *debug_context()); |
| 615 | DCHECK(in_debug_scope()); |
| 616 | } |
| 617 | |
| 618 | void ThreadInit(); |
| 619 | |
| 620 | // Global handles. |
| 621 | Handle<Context> debug_context_; |
| 622 | Handle<Object> event_listener_; |
| 623 | Handle<Object> event_listener_data_; |
| 624 | |
| 625 | v8::Debug::MessageHandler message_handler_; |
| 626 | |
| 627 | static const int kQueueInitialSize = 4; |
| 628 | base::Semaphore command_received_; // Signaled for each command received. |
| 629 | LockingCommandMessageQueue command_queue_; |
| 630 | |
| 631 | bool is_active_; |
| 632 | bool is_suppressed_; |
| 633 | bool live_edit_enabled_; |
| 634 | bool break_disabled_; |
| 635 | bool break_points_active_; |
| 636 | bool in_debug_event_listener_; |
| 637 | bool break_on_exception_; |
| 638 | bool break_on_uncaught_exception_; |
| 639 | |
| 640 | DebugInfoListNode* debug_info_list_; // List of active debug info objects. |
| 641 | |
| 642 | // Storage location for jump when exiting debug break calls. |
| 643 | // Note that this address is not GC safe. It should be computed immediately |
| 644 | // before returning to the DebugBreakCallHelper. |
| 645 | Address after_break_target_; |
| 646 | |
| 647 | // Used to collect histogram data on debugger feature usage. |
| 648 | DebugFeatureTracker feature_tracker_; |
| 649 | |
| 650 | // Per-thread data. |
| 651 | class ThreadLocal { |
| 652 | public: |
| 653 | // Top debugger entry. |
| 654 | base::AtomicWord current_debug_scope_; |
| 655 | |
| 656 | // Counter for generating next break id. |
| 657 | int break_count_; |
| 658 | |
| 659 | // Current break id. |
| 660 | int break_id_; |
| 661 | |
| 662 | // Frame id for the frame of the current break. |
| 663 | StackFrame::Id break_frame_id_; |
| 664 | |
| 665 | // Step action for last step performed. |
| 666 | StepAction last_step_action_; |
| 667 | |
| 668 | // Source statement position from last step next action. |
| 669 | int last_statement_position_; |
| 670 | |
| 671 | // Frame pointer from last step next or step frame action. |
| 672 | Address last_fp_; |
| 673 | |
| 674 | // Frame pointer of the target frame we want to arrive at. |
| 675 | Address target_fp_; |
| 676 | |
| 677 | // Whether functions are flooded on entry for step-in and step-frame. |
| 678 | // If we stepped out to the embedder, disable flooding to spill stepping |
| 679 | // to the next call that the embedder makes. |
| 680 | bool step_in_enabled_; |
| 681 | |
| 682 | // Stores the way how LiveEdit has patched the stack. It is used when |
| 683 | // debugger returns control back to user script. |
| 684 | LiveEdit::FrameDropMode frame_drop_mode_; |
| 685 | }; |
| 686 | |
| 687 | // Storage location for registers when handling debug break calls |
| 688 | ThreadLocal thread_local_; |
| 689 | |
| 690 | Isolate* isolate_; |
| 691 | |
| 692 | friend class Isolate; |
| 693 | friend class DebugScope; |
| 694 | friend class DisableBreak; |
| 695 | friend class LiveEdit; |
| 696 | friend class SuppressDebug; |
| 697 | |
| 698 | friend Handle<FixedArray> GetDebuggedFunctions(); // In test-debug.cc |
| 699 | friend void CheckDebuggerUnloaded(bool check_functions); // In test-debug.cc |
| 700 | |
| 701 | DISALLOW_COPY_AND_ASSIGN(Debug); |
| 702 | }; |
| 703 | |
| 704 | |
| 705 | // This scope is used to load and enter the debug context and create a new |
| 706 | // break state. Leaving the scope will restore the previous state. |
| 707 | // On failure to load, FailedToEnter returns true. |
| 708 | class DebugScope BASE_EMBEDDED { |
| 709 | public: |
| 710 | explicit DebugScope(Debug* debug); |
| 711 | ~DebugScope(); |
| 712 | |
| 713 | // Check whether loading was successful. |
| 714 | inline bool failed() { return failed_; } |
| 715 | |
| 716 | // Get the active context from before entering the debugger. |
| 717 | inline Handle<Context> GetContext() { return save_.context(); } |
| 718 | |
| 719 | private: |
| 720 | Isolate* isolate() { return debug_->isolate_; } |
| 721 | |
| 722 | Debug* debug_; |
| 723 | DebugScope* prev_; // Previous scope if entered recursively. |
| 724 | StackFrame::Id break_frame_id_; // Previous break frame id. |
| 725 | int break_id_; // Previous break id. |
| 726 | bool failed_; // Did the debug context fail to load? |
| 727 | SaveContext save_; // Saves previous context. |
| 728 | PostponeInterruptsScope no_termination_exceptons_; |
| 729 | }; |
| 730 | |
| 731 | |
| 732 | // Stack allocated class for disabling break. |
| 733 | class DisableBreak BASE_EMBEDDED { |
| 734 | public: |
| 735 | explicit DisableBreak(Debug* debug, bool disable_break) |
| 736 | : debug_(debug), |
| 737 | previous_break_disabled_(debug->break_disabled_), |
| 738 | previous_in_debug_event_listener_(debug->in_debug_event_listener_) { |
| 739 | debug_->break_disabled_ = disable_break; |
| 740 | debug_->in_debug_event_listener_ = disable_break; |
| 741 | } |
| 742 | ~DisableBreak() { |
| 743 | debug_->break_disabled_ = previous_break_disabled_; |
| 744 | debug_->in_debug_event_listener_ = previous_in_debug_event_listener_; |
| 745 | } |
| 746 | |
| 747 | private: |
| 748 | Debug* debug_; |
| 749 | bool previous_break_disabled_; |
| 750 | bool previous_in_debug_event_listener_; |
| 751 | DISALLOW_COPY_AND_ASSIGN(DisableBreak); |
| 752 | }; |
| 753 | |
| 754 | |
| 755 | class SuppressDebug BASE_EMBEDDED { |
| 756 | public: |
| 757 | explicit SuppressDebug(Debug* debug) |
| 758 | : debug_(debug), old_state_(debug->is_suppressed_) { |
| 759 | debug_->is_suppressed_ = true; |
| 760 | } |
| 761 | ~SuppressDebug() { debug_->is_suppressed_ = old_state_; } |
| 762 | |
| 763 | private: |
| 764 | Debug* debug_; |
| 765 | bool old_state_; |
| 766 | DISALLOW_COPY_AND_ASSIGN(SuppressDebug); |
| 767 | }; |
| 768 | |
| 769 | |
| 770 | // Code generator routines. |
| 771 | class DebugCodegen : public AllStatic { |
| 772 | public: |
| 773 | enum DebugBreakCallHelperMode { |
| 774 | SAVE_RESULT_REGISTER, |
| 775 | IGNORE_RESULT_REGISTER |
| 776 | }; |
| 777 | |
| 778 | static void GenerateDebugBreakStub(MacroAssembler* masm, |
| 779 | DebugBreakCallHelperMode mode); |
| 780 | |
| 781 | // FrameDropper is a code replacement for a JavaScript frame with possibly |
| 782 | // several frames above. |
| 783 | // There is no calling conventions here, because it never actually gets |
| 784 | // called, it only gets returned to. |
| 785 | static void GenerateFrameDropperLiveEdit(MacroAssembler* masm); |
| 786 | |
| 787 | |
| 788 | static void GenerateSlot(MacroAssembler* masm, RelocInfo::Mode mode); |
| 789 | |
| 790 | static void PatchDebugBreakSlot(Isolate* isolate, Address pc, |
| 791 | Handle<Code> code); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 792 | static bool DebugBreakSlotIsPatched(Address pc); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 793 | static void ClearDebugBreakSlot(Isolate* isolate, Address pc); |
| 794 | }; |
| 795 | |
| 796 | |
| 797 | } // namespace internal |
| 798 | } // namespace v8 |
| 799 | |
| 800 | #endif // V8_DEBUG_DEBUG_H_ |