#ifndef V8_DEBUG_DEBUG_H_
#define V8_DEBUG_DEBUG_H_
#include <memory>
#include <vector>
#include "src/base/enum-set.h"
#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"
#include "src/objects/shared-function-info.h"
namespace v8 {
namespace internal {
class AbstractCode;
class DebugScope;
class InterpretedFrame;
class JavaScriptFrame;
class JSGeneratorObject;
class StackFrame;
enum StepAction : int8_t {
StepNone = -1,
StepOut = 0,
StepOver = 1,
StepInto = 2,
LastStepAction = StepInto
};
enum ExceptionBreakType { BreakException = 0, BreakUncaughtException = 1 };
enum DebugBreakType {
NOT_DEBUG_BREAK,
DEBUGGER_STATEMENT,
DEBUG_BREAK_AT_ENTRY,
DEBUG_BREAK_SLOT,
DEBUG_BREAK_SLOT_AT_CALL,
DEBUG_BREAK_SLOT_AT_RETURN,
DEBUG_BREAK_SLOT_AT_SUSPEND,
};
enum IgnoreBreakMode {
kIgnoreIfAllFramesBlackboxed,
kIgnoreIfTopFrameBlackboxed
};
class BreakLocation {
public:
static BreakLocation Invalid() { return BreakLocation(-1, NOT_DEBUG_BREAK); }
static BreakLocation FromFrame(Handle<DebugInfo> debug_info,
JavaScriptFrame* frame);
static void AllAtCurrentStatement(Handle<DebugInfo> debug_info,
JavaScriptFrame* frame,
std::vector<BreakLocation>* result_out);
bool IsSuspend() const { return type_ == DEBUG_BREAK_SLOT_AT_SUSPEND; }
bool IsReturn() const { return type_ == DEBUG_BREAK_SLOT_AT_RETURN; }
bool IsReturnOrSuspend() const { return type_ >= DEBUG_BREAK_SLOT_AT_RETURN; }
bool IsCall() const { return type_ == DEBUG_BREAK_SLOT_AT_CALL; }
bool IsDebugBreakSlot() const { return type_ >= DEBUG_BREAK_SLOT; }
bool IsDebuggerStatement() const { return type_ == DEBUGGER_STATEMENT; }
bool IsDebugBreakAtEntry() const { return type_ == DEBUG_BREAK_AT_ENTRY; }
bool HasBreakPoint(Isolate* isolate, Handle<DebugInfo> debug_info) const;
int generator_suspend_id() { return generator_suspend_id_; }
int position() const { return position_; }
debug::BreakLocationType type() const;
JSGeneratorObject GetGeneratorObjectForSuspendedFrame(
JavaScriptFrame* frame) const;
private:
BreakLocation(Handle<AbstractCode> abstract_code, DebugBreakType type,
int code_offset, int position, int generator_obj_reg_index,
int generator_suspend_id)
: abstract_code_(abstract_code),
code_offset_(code_offset),
type_(type),
position_(position),
generator_obj_reg_index_(generator_obj_reg_index),
generator_suspend_id_(generator_suspend_id) {
DCHECK_NE(NOT_DEBUG_BREAK, type_);
}
BreakLocation(int position, DebugBreakType type)
: code_offset_(0),
type_(type),
position_(position),
generator_obj_reg_index_(0),
generator_suspend_id_(-1) {}
static int BreakIndexFromCodeOffset(Handle<DebugInfo> debug_info,
Handle<AbstractCode> abstract_code,
int offset);
void SetDebugBreak();
void ClearDebugBreak();
Handle<AbstractCode> abstract_code_;
int code_offset_;
DebugBreakType type_;
int position_;
int generator_obj_reg_index_;
int generator_suspend_id_;
friend class BreakIterator;
};
class V8_EXPORT_PRIVATE BreakIterator {
public:
explicit BreakIterator(Handle<DebugInfo> debug_info);
BreakIterator(const BreakIterator&) = delete;
BreakIterator& operator=(const BreakIterator&) = delete;
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<DebugInfo> debug_info_;
int break_index_;
int position_;
int statement_position_;
SourcePositionTableIterator source_position_iterator_;
DISALLOW_GARBAGE_COLLECTION(no_gc_)
};
class DebugInfoListNode {
public:
DebugInfoListNode(Isolate* isolate, DebugInfo debug_info);
~DebugInfoListNode();
DebugInfoListNode* next() { return next_; }
void set_next(DebugInfoListNode* next) { next_ = next; }
Handle<DebugInfo> debug_info() { return Handle<DebugInfo>(debug_info_); }
private:
Address* debug_info_;
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_;
};
class V8_EXPORT_PRIVATE Debug {
public:
Debug(const Debug&) = delete;
Debug& operator=(const Debug&) = delete;
void OnDebugBreak(Handle<FixedArray> break_points_hit, StepAction stepAction,
debug::BreakReasons break_reasons = {});
base::Optional<Object> OnThrow(Handle<Object> exception)
V8_WARN_UNUSED_RESULT;
void OnPromiseReject(Handle<Object> promise, Handle<Object> value);
void OnCompileError(Handle<Script> script);
void OnAfterCompile(Handle<Script> script);
void HandleDebugBreak(IgnoreBreakMode ignore_break_mode,
debug::BreakReasons break_reasons);
void Break(JavaScriptFrame* frame, Handle<JSFunction> break_target);
Handle<FixedArray> GetLoadedScripts();
bool SetBreakpoint(Handle<SharedFunctionInfo> shared,
Handle<BreakPoint> break_point, int* source_position);
void ClearBreakPoint(Handle<BreakPoint> break_point);
void ChangeBreakOnException(ExceptionBreakType type, bool enable);
bool IsBreakOnException(ExceptionBreakType type);
void SetTerminateOnResume();
bool SetBreakPointForScript(Handle<Script> script, Handle<String> condition,
int* source_position, int* id);
bool SetBreakpointForFunction(Handle<SharedFunctionInfo> shared,
Handle<String> condition, int* id);
void RemoveBreakpoint(int id);
#if V8_ENABLE_WEBASSEMBLY
void SetOnEntryBreakpointForWasmScript(Handle<Script> script, int* id);
void RemoveBreakpointForWasmScript(Handle<Script> script, int id);
void RecordWasmScriptWithBreakpoints(Handle<Script> script);
#endif
MaybeHandle<FixedArray> GetHitBreakPoints(Handle<DebugInfo> debug_info,
int position);
void PrepareStep(StepAction step_action);
void PrepareStepIn(Handle<JSFunction> function);
void PrepareStepInSuspendedGenerator();
void PrepareStepOnThrow();
void ClearStepping();
void SetBreakOnNextFunctionCall();
void ClearBreakOnNextFunctionCall();
void DiscardBaselineCode(SharedFunctionInfo shared);
void DiscardAllBaselineCode();
void DeoptimizeFunction(Handle<SharedFunctionInfo> shared);
void PrepareFunctionForDebugExecution(Handle<SharedFunctionInfo> shared);
void InstallDebugBreakTrampoline();
bool GetPossibleBreakpoints(Handle<Script> script, int start_position,
int end_position, bool restrict_to_function,
std::vector<BreakLocation>* locations);
bool IsBlackboxed(Handle<SharedFunctionInfo> shared);
bool ShouldBeSkipped();
bool CanBreakAtEntry(Handle<SharedFunctionInfo> shared);
void SetDebugDelegate(debug::DebugDelegate* delegate);
bool EnsureBreakInfo(Handle<SharedFunctionInfo> shared);
void CreateBreakInfo(Handle<SharedFunctionInfo> shared);
Handle<DebugInfo> GetOrCreateDebugInfo(Handle<SharedFunctionInfo> shared);
void InstallCoverageInfo(Handle<SharedFunctionInfo> shared,
Handle<CoverageInfo> coverage_info);
void RemoveAllCoverageInfos();
Handle<Object> FindInnermostContainingFunctionInfo(Handle<Script> script,
int position);
Handle<SharedFunctionInfo> FindClosestSharedFunctionInfoFromPosition(
int position, Handle<Script> script,
Handle<SharedFunctionInfo> outer_shared);
bool FindSharedFunctionInfosIntersectingRange(
Handle<Script> script, int start_position, int end_position,
std::vector<Handle<SharedFunctionInfo>>* candidates);
static Handle<Object> GetSourceBreakLocations(
Isolate* isolate, Handle<SharedFunctionInfo> shared);
bool IsBreakAtReturn(JavaScriptFrame* frame);
bool AllFramesOnStackAreBlackboxed();
bool SetScriptSource(Handle<Script> script, Handle<String> source,
bool preview, debug::LiveEditResult* result);
int GetFunctionDebuggingId(Handle<JSFunction> function);
char* ArchiveDebug(char* to);
char* RestoreDebug(char* from);
static int ArchiveSpacePerThread();
void FreeThreadResources() {}
void Iterate(RootVisitor* v);
void InitThread(const ExecutionAccess& lock) { ThreadInit(); }
bool CheckExecutionState() { return is_active(); }
void StartSideEffectCheckMode();
void StopSideEffectCheckMode();
void ApplySideEffectChecks(Handle<DebugInfo> debug_info);
void ClearSideEffectChecks(Handle<DebugInfo> debug_info);
bool PerformSideEffectCheck(Handle<JSFunction> function,
Handle<Object> receiver);
enum AccessorKind { kNotAccessor, kGetter, kSetter };
bool PerformSideEffectCheckForCallback(Handle<Object> callback_info,
Handle<Object> receiver,
AccessorKind accessor_kind);
bool PerformSideEffectCheckAtBytecode(InterpretedFrame* frame);
bool PerformSideEffectCheckForObject(Handle<Object> object);
inline bool is_active() const { return is_active_; }
inline bool in_debug_scope() const {
return !!base::Relaxed_Load(&thread_local_.current_debug_scope_);
}
inline bool needs_check_on_function_call() const {
return hook_on_function_call_;
}
void set_break_points_active(bool v) { break_points_active_ = v; }
bool break_points_active() const { return break_points_active_; }
StackFrameId break_frame_id() { return thread_local_.break_frame_id_; }
Handle<Object> return_value_handle();
Object return_value() { return thread_local_.return_value_; }
void set_return_value(Object value) { thread_local_.return_value_ = value; }
Address is_active_address() { return reinterpret_cast<Address>(&is_active_); }
Address hook_on_function_call_address() {
return reinterpret_cast<Address>(&hook_on_function_call_);
}
Address suspended_generator_address() {
return reinterpret_cast<Address>(&thread_local_.suspended_generator_);
}
StepAction last_step_action() { return thread_local_.last_step_action_; }
bool break_on_next_function_call() const {
return thread_local_.break_on_next_function_call_;
}
inline bool break_disabled() const { return break_disabled_; }
DebugFeatureTracker* feature_tracker() { return &feature_tracker_; }
static const int kBreakAtEntryPosition = 0;
void RemoveBreakInfoAndMaybeFree(Handle<DebugInfo> debug_info);
static char* Iterate(RootVisitor* v, char* thread_storage);
private:
explicit Debug(Isolate* isolate);
~Debug();
void UpdateDebugInfosForExecutionMode();
void UpdateState();
void UpdateHookOnFunctionCall();
void Unload();
int CurrentFrameCount();
inline bool ignore_events() const {
return is_suppressed_ || !is_active_ ||
isolate_->debug_execution_mode() == DebugInfo::kSideEffects;
}
void clear_suspended_generator() {
thread_local_.suspended_generator_ = Smi::zero();
}
bool has_suspended_generator() const {
return thread_local_.suspended_generator_ != Smi::zero();
}
bool IsExceptionBlackboxed(bool uncaught);
void OnException(Handle<Object> exception, Handle<Object> promise,
v8::debug::ExceptionType exception_type);
void ProcessCompileEvent(bool has_compile_error, Handle<Script> script);
int FindBreakablePosition(Handle<DebugInfo> debug_info, int source_position);
void ApplyBreakPoints(Handle<DebugInfo> debug_info);
void ClearBreakPoints(Handle<DebugInfo> debug_info);
void ClearAllBreakPoints();
void FloodWithOneShot(Handle<SharedFunctionInfo> function,
bool returns_only = false);
void ClearOneShot();
bool IsFrameBlackboxed(JavaScriptFrame* frame);
void ActivateStepOut(StackFrame* frame);
MaybeHandle<FixedArray> CheckBreakPoints(Handle<DebugInfo> debug_info,
BreakLocation* location,
bool* has_break_points = nullptr);
MaybeHandle<FixedArray> GetHitBreakpointsAtCurrentStatement(
JavaScriptFrame* frame, bool* hasBreakpoints);
bool IsMutedAtCurrentLocation(JavaScriptFrame* frame);
bool CheckBreakPoint(Handle<BreakPoint> break_point, bool is_break_at_entry);
inline void AssertDebugContext() { DCHECK(in_debug_scope()); }
void ThreadInit();
void PrintBreakLocation();
void ClearAllDebuggerHints();
using DebugInfoClearFunction = std::function<void(Handle<DebugInfo>)>;
void ClearAllDebugInfos(const DebugInfoClearFunction& clear_function);
void FindDebugInfo(Handle<DebugInfo> debug_info, DebugInfoListNode** prev,
DebugInfoListNode** curr);
void FreeDebugInfoListNode(DebugInfoListNode* prev, DebugInfoListNode* node);
void SetTemporaryObjectTrackingDisabled(bool disabled);
bool GetTemporaryObjectTrackingDisabled() const;
debug::DebugDelegate* debug_delegate_ = nullptr;
bool is_active_;
bool hook_on_function_call_;
bool is_suppressed_;
bool running_live_edit_ = false;
bool break_disabled_;
bool break_points_active_;
bool break_on_exception_;
bool break_on_uncaught_exception_;
bool side_effect_check_failed_;
DebugInfoListNode* debug_info_list_;
class TemporaryObjectsTracker;
std::unique_ptr<TemporaryObjectsTracker> temporary_objects_;
Handle<RegExpMatchInfo> regexp_match_info_;
DebugFeatureTracker feature_tracker_;
class ThreadLocal {
public:
base::AtomicWord current_debug_scope_;
StackFrameId break_frame_id_;
StepAction last_step_action_;
Object ignore_step_into_function_;
bool fast_forward_to_return_;
int last_statement_position_;
int last_frame_count_;
int target_frame_count_;
Object return_value_;
Object suspended_generator_;
int last_breakpoint_id_;
bool break_on_next_function_call_;
};
static void Iterate(RootVisitor* v, ThreadLocal* thread_local_data);
ThreadLocal thread_local_;
#if V8_ENABLE_WEBASSEMBLY
Handle<WeakArrayList> wasm_scripts_with_break_points_;
#endif
Isolate* isolate_;
friend class Isolate;
friend class DebugScope;
friend class DisableBreak;
friend class DisableTemporaryObjectTracking;
friend class LiveEdit;
friend class SuppressDebug;
friend Handle<FixedArray> GetDebuggedFunctions();
friend void CheckDebuggerUnloaded();
};
class V8_NODISCARD DebugScope {
public:
explicit DebugScope(Debug* debug);
~DebugScope();
void set_terminate_on_resume();
private:
Isolate* isolate() { return debug_->isolate_; }
Debug* debug_;
DebugScope* prev_;
StackFrameId break_frame_id_;
PostponeInterruptsScope no_interrupts_;
bool terminate_on_resume_ = false;
};
class V8_NODISCARD ReturnValueScope {
public:
explicit ReturnValueScope(Debug* debug);
~ReturnValueScope();
private:
Debug* debug_;
Handle<Object> return_value_;
};
class DisableBreak {
public:
explicit DisableBreak(Debug* debug, bool disable = true)
: debug_(debug), previous_break_disabled_(debug->break_disabled_) {
debug_->break_disabled_ = disable;
}
~DisableBreak() { debug_->break_disabled_ = previous_break_disabled_; }
DisableBreak(const DisableBreak&) = delete;
DisableBreak& operator=(const DisableBreak&) = delete;
private:
Debug* debug_;
bool previous_break_disabled_;
};
class DisableTemporaryObjectTracking {
public:
explicit DisableTemporaryObjectTracking(Debug* debug)
: debug_(debug),
previous_tracking_disabled_(
debug->GetTemporaryObjectTrackingDisabled()) {
debug_->SetTemporaryObjectTrackingDisabled(true);
}
~DisableTemporaryObjectTracking() {
debug_->SetTemporaryObjectTrackingDisabled(previous_tracking_disabled_);
}
DisableTemporaryObjectTracking(const DisableTemporaryObjectTracking&) =
delete;
DisableTemporaryObjectTracking& operator=(
const DisableTemporaryObjectTracking&) = delete;
private:
Debug* debug_;
bool previous_tracking_disabled_;
};
class SuppressDebug {
public:
explicit SuppressDebug(Debug* debug)
: debug_(debug), old_state_(debug->is_suppressed_) {
debug_->is_suppressed_ = true;
}
~SuppressDebug() { debug_->is_suppressed_ = old_state_; }
SuppressDebug(const SuppressDebug&) = delete;
SuppressDebug& operator=(const SuppressDebug&) = delete;
private:
Debug* debug_;
bool old_state_;
};
}
}
#endif