// Copyright 2012 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef V8_DEBUG_DEBUG_H_ #define V8_DEBUG_DEBUG_H_ #include #include #include "src/codegen/source-position-table.h" #include "src/common/globals.h" #include "src/debug/debug-interface.h" #include "src/debug/interface-types.h" #include "src/execution/interrupts-scope.h" #include "src/execution/isolate.h" #include "src/handles/handles.h" #include "src/objects/debug-objects.h" namespace v8 { namespace internal { // Forward declarations. class AbstractCode; class DebugScope; class InterpretedFrame; class JavaScriptFrame; class JSGeneratorObject; class StackFrame; // Step actions. NOTE: These values are in macros.py as well. enum StepAction : int8_t { StepNone = -1, // Stepping not prepared. StepOut = 0, // Step out of the current function. StepNext = 1, // Step to the next statement in the current function. StepIn = 2, // Step into new functions invoked or the next statement // in the current function. LastStepAction = StepIn }; // Type of exception break. NOTE: These values are in macros.py as well. enum ExceptionBreakType { BreakException = 0, BreakUncaughtException = 1 }; enum DebugBreakType { NOT_DEBUG_BREAK, DEBUGGER_STATEMENT, DEBUG_BREAK_SLOT, DEBUG_BREAK_SLOT_AT_CALL, DEBUG_BREAK_SLOT_AT_RETURN, DEBUG_BREAK_SLOT_AT_SUSPEND, DEBUG_BREAK_AT_ENTRY, }; enum IgnoreBreakMode { kIgnoreIfAllFramesBlackboxed, kIgnoreIfTopFrameBlackboxed }; class BreakLocation { public: static BreakLocation Invalid() { return BreakLocation(-1, NOT_DEBUG_BREAK); } static BreakLocation FromFrame(Handle debug_info, JavaScriptFrame* frame); static void AllAtCurrentStatement(Handle debug_info, JavaScriptFrame* frame, std::vector* result_out); inline bool IsSuspend() const { return type_ == DEBUG_BREAK_SLOT_AT_SUSPEND; } inline bool IsReturn() const { return type_ == DEBUG_BREAK_SLOT_AT_RETURN; } inline bool IsReturnOrSuspend() const { return type_ >= DEBUG_BREAK_SLOT_AT_RETURN; } inline bool IsCall() const { return type_ == DEBUG_BREAK_SLOT_AT_CALL; } inline bool IsDebugBreakSlot() const { return type_ >= DEBUG_BREAK_SLOT; } inline bool IsDebuggerStatement() const { return type_ == DEBUGGER_STATEMENT; } inline bool IsDebugBreakAtEntry() const { bool result = type_ == DEBUG_BREAK_AT_ENTRY; return result; } bool HasBreakPoint(Isolate* isolate, Handle debug_info) const; inline int position() const { return position_; } debug::BreakLocationType type() const; JSGeneratorObject GetGeneratorObjectForSuspendedFrame( JavaScriptFrame* frame) const; private: BreakLocation(Handle abstract_code, DebugBreakType type, int code_offset, int position, int generator_obj_reg_index) : abstract_code_(abstract_code), code_offset_(code_offset), type_(type), position_(position), generator_obj_reg_index_(generator_obj_reg_index) { DCHECK_NE(NOT_DEBUG_BREAK, type_); } BreakLocation(int position, DebugBreakType type) : code_offset_(0), type_(type), position_(position), generator_obj_reg_index_(0) {} static int BreakIndexFromCodeOffset(Handle debug_info, Handle abstract_code, int offset); void SetDebugBreak(); void ClearDebugBreak(); Handle abstract_code_; int code_offset_; DebugBreakType type_; int position_; int generator_obj_reg_index_; friend class BreakIterator; }; class V8_EXPORT_PRIVATE BreakIterator { public: explicit BreakIterator(Handle debug_info); BreakLocation GetBreakLocation(); bool Done() const { return source_position_iterator_.done(); } void Next(); void SkipToPosition(int position); void SkipTo(int count) { while (count-- > 0) Next(); } int code_offset() { return source_position_iterator_.code_offset(); } int break_index() const { return break_index_; } inline int position() const { return position_; } inline int statement_position() const { return statement_position_; } void ClearDebugBreak(); void SetDebugBreak(); private: int BreakIndexFromPosition(int position); Isolate* isolate(); DebugBreakType GetDebugBreakType(); Handle debug_info_; int break_index_; int position_; int statement_position_; SourcePositionTableIterator source_position_iterator_; DisallowHeapAllocation no_gc_; DISALLOW_COPY_AND_ASSIGN(BreakIterator); }; // Linked list holding debug info objects. The debug info objects are kept as // weak handles to avoid a debug info object to keep a function alive. class DebugInfoListNode { public: DebugInfoListNode(Isolate* isolate, DebugInfo debug_info); ~DebugInfoListNode(); DebugInfoListNode* next() { return next_; } void set_next(DebugInfoListNode* next) { next_ = next; } Handle debug_info() { return Handle(debug_info_); } private: // Global (weak) handle to the debug info object. Address* debug_info_; // Next pointer for linked list. DebugInfoListNode* next_; }; class DebugFeatureTracker { public: enum Feature { kActive = 1, kBreakPoint = 2, kStepping = 3, kHeapSnapshot = 4, kAllocationTracking = 5, kProfiler = 6, kLiveEdit = 7, }; explicit DebugFeatureTracker(Isolate* isolate) : isolate_(isolate), bitfield_(0) {} void Track(Feature feature); private: Isolate* isolate_; uint32_t bitfield_; }; // This class contains the debugger support. The main purpose is to handle // setting break points in the code. // // This class controls the debug info for all functions which currently have // active breakpoints in them. This debug info is held in the heap root object // debug_info which is a FixedArray. Each entry in this list is of class // DebugInfo. class V8_EXPORT_PRIVATE Debug { public: // Debug event triggers. void OnDebugBreak(Handle break_points_hit, StepAction stepAction); base::Optional OnThrow(Handle exception) V8_WARN_UNUSED_RESULT; void OnPromiseReject(Handle promise, Handle value); void OnCompileError(Handle