// 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 "src/allocation.h" #include "src/assembler.h" #include "src/base/atomicops.h" #include "src/base/hashmap.h" #include "src/base/platform/platform.h" #include "src/debug/debug-interface.h" #include "src/debug/interface-types.h" #include "src/execution.h" #include "src/factory.h" #include "src/flags.h" #include "src/frames.h" #include "src/globals.h" #include "src/runtime/runtime.h" #include "src/source-position-table.h" #include "src/string-stream.h" #include "src/v8threads.h" #include "include/v8-debug.h" namespace v8 { namespace internal { // Forward declarations. class DebugScope; // 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 }; // The different types of breakpoint position alignments. // Must match Debug.BreakPositionAlignment in debug.js enum BreakPositionAlignment { STATEMENT_ALIGNED = 0, BREAK_POSITION_ALIGNED = 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_TAIL_CALL, }; class BreakLocation { public: static BreakLocation FromFrame(Handle debug_info, JavaScriptFrame* frame); static void AllAtCurrentStatement(Handle debug_info, JavaScriptFrame* frame, List* result_out); inline bool IsReturn() const { return type_ == DEBUG_BREAK_SLOT_AT_RETURN; } inline bool IsCall() const { return type_ == DEBUG_BREAK_SLOT_AT_CALL; } inline bool IsTailCall() const { return type_ == DEBUG_BREAK_SLOT_AT_TAIL_CALL; } inline bool IsDebugBreakSlot() const { return type_ >= DEBUG_BREAK_SLOT; } inline bool IsDebuggerStatement() const { return type_ == DEBUGGER_STATEMENT; } bool HasBreakPoint(Handle debug_info) const; inline int position() const { return position_; } private: BreakLocation(Handle abstract_code, DebugBreakType type, int code_offset, int position) : abstract_code_(abstract_code), code_offset_(code_offset), type_(type), position_(position) { DCHECK_NE(NOT_DEBUG_BREAK, type_); } 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_; friend class CodeBreakIterator; friend class BytecodeArrayBreakIterator; }; class BreakIterator { public: static std::unique_ptr GetIterator( Handle debug_info, Handle abstract_code); virtual ~BreakIterator() {} virtual BreakLocation GetBreakLocation() = 0; virtual bool Done() const = 0; virtual void Next() = 0; void SkipTo(int count) { while (count-- > 0) Next(); } virtual int code_offset() = 0; int break_index() const { return break_index_; } inline int position() const { return position_; } inline int statement_position() const { return statement_position_; } virtual bool IsDebugBreak() = 0; virtual void ClearDebugBreak() = 0; virtual void SetDebugBreak() = 0; protected: explicit BreakIterator(Handle debug_info); int BreakIndexFromPosition(int position, BreakPositionAlignment alignment); Isolate* isolate() { return debug_info_->GetIsolate(); } Handle debug_info_; int break_index_; int position_; int statement_position_; private: DisallowHeapAllocation no_gc_; DISALLOW_COPY_AND_ASSIGN(BreakIterator); }; class CodeBreakIterator : public BreakIterator { public: explicit CodeBreakIterator(Handle debug_info); ~CodeBreakIterator() override {} BreakLocation GetBreakLocation() override; bool Done() const override { return reloc_iterator_.done(); } void Next() override; bool IsDebugBreak() override; void ClearDebugBreak() override; void SetDebugBreak() override; void SkipToPosition(int position, BreakPositionAlignment alignment); int code_offset() override { return static_cast(rinfo()->pc() - debug_info_->DebugCode()->instruction_start()); } private: int GetModeMask(); DebugBreakType GetDebugBreakType(); RelocInfo::Mode rmode() { return reloc_iterator_.rinfo()->rmode(); } RelocInfo* rinfo() { return reloc_iterator_.rinfo(); } RelocIterator reloc_iterator_; SourcePositionTableIterator source_position_iterator_; DISALLOW_COPY_AND_ASSIGN(CodeBreakIterator); }; class BytecodeArrayBreakIterator : public BreakIterator { public: explicit BytecodeArrayBreakIterator(Handle debug_info); ~BytecodeArrayBreakIterator() override {} BreakLocation GetBreakLocation() override; bool Done() const override { return source_position_iterator_.done(); } void Next() override; bool IsDebugBreak() override; void ClearDebugBreak() override; void SetDebugBreak() override; void SkipToPosition(int position, BreakPositionAlignment alignment); int code_offset() override { return source_position_iterator_.code_offset(); } private: DebugBreakType GetDebugBreakType(); SourcePositionTableIterator source_position_iterator_; DISALLOW_COPY_AND_ASSIGN(BytecodeArrayBreakIterator); }; // 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: explicit DebugInfoListNode(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. DebugInfo** 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 Debug { public: // Debug event triggers. void OnDebugBreak(Handle break_points_hit); void OnThrow(Handle exception); void OnPromiseReject(Handle promise, Handle value); void OnCompileError(Handle