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