11cb0ef41Sopenharmony_ci// Copyright 2016 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#ifndef V8_INSPECTOR_V8_STACK_TRACE_IMPL_H_
61cb0ef41Sopenharmony_ci#define V8_INSPECTOR_V8_STACK_TRACE_IMPL_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include <memory>
91cb0ef41Sopenharmony_ci#include <vector>
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci#include "include/v8-inspector.h"
121cb0ef41Sopenharmony_ci#include "include/v8-local-handle.h"
131cb0ef41Sopenharmony_ci#include "src/base/macros.h"
141cb0ef41Sopenharmony_ci#include "src/inspector/protocol/Runtime.h"
151cb0ef41Sopenharmony_ci#include "src/inspector/string-16.h"
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_cinamespace v8 {
181cb0ef41Sopenharmony_ciclass StackFrame;
191cb0ef41Sopenharmony_ciclass StackTrace;
201cb0ef41Sopenharmony_ci}  // namespace v8
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_cinamespace v8_inspector {
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ciclass AsyncStackTrace;
251cb0ef41Sopenharmony_ciclass V8Debugger;
261cb0ef41Sopenharmony_cistruct V8StackTraceId;
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ciclass StackFrame {
291cb0ef41Sopenharmony_ci public:
301cb0ef41Sopenharmony_ci  StackFrame(String16&& functionName, int scriptId, String16&& sourceURL,
311cb0ef41Sopenharmony_ci             int lineNumber, int columnNumber, bool hasSourceURLComment);
321cb0ef41Sopenharmony_ci  ~StackFrame() = default;
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci  const String16& functionName() const;
351cb0ef41Sopenharmony_ci  int scriptId() const;
361cb0ef41Sopenharmony_ci  const String16& sourceURL() const;
371cb0ef41Sopenharmony_ci  int lineNumber() const;    // 0-based.
381cb0ef41Sopenharmony_ci  int columnNumber() const;  // 0-based.
391cb0ef41Sopenharmony_ci  std::unique_ptr<protocol::Runtime::CallFrame> buildInspectorObject(
401cb0ef41Sopenharmony_ci      V8InspectorClient* client) const;
411cb0ef41Sopenharmony_ci  bool isEqual(StackFrame* frame) const;
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_ci private:
441cb0ef41Sopenharmony_ci  String16 m_functionName;
451cb0ef41Sopenharmony_ci  int m_scriptId;
461cb0ef41Sopenharmony_ci  String16 m_sourceURL;
471cb0ef41Sopenharmony_ci  int m_lineNumber;    // 0-based.
481cb0ef41Sopenharmony_ci  int m_columnNumber;  // 0-based.
491cb0ef41Sopenharmony_ci  bool m_hasSourceURLComment;
501cb0ef41Sopenharmony_ci};
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ciclass V8StackTraceImpl : public V8StackTrace {
531cb0ef41Sopenharmony_ci public:
541cb0ef41Sopenharmony_ci  static constexpr int kDefaultMaxCallStackSizeToCapture = 200;
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ci  static std::unique_ptr<V8StackTraceImpl> create(V8Debugger*,
571cb0ef41Sopenharmony_ci                                                  v8::Local<v8::StackTrace>,
581cb0ef41Sopenharmony_ci                                                  int maxStackSize);
591cb0ef41Sopenharmony_ci  static std::unique_ptr<V8StackTraceImpl> capture(V8Debugger*,
601cb0ef41Sopenharmony_ci                                                   int maxStackSize);
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  ~V8StackTraceImpl() override;
631cb0ef41Sopenharmony_ci  V8StackTraceImpl(const V8StackTraceImpl&) = delete;
641cb0ef41Sopenharmony_ci  V8StackTraceImpl& operator=(const V8StackTraceImpl&) = delete;
651cb0ef41Sopenharmony_ci  std::unique_ptr<protocol::Runtime::StackTrace> buildInspectorObjectImpl(
661cb0ef41Sopenharmony_ci      V8Debugger* debugger) const;
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci  std::unique_ptr<protocol::Runtime::StackTrace> buildInspectorObjectImpl(
691cb0ef41Sopenharmony_ci      V8Debugger* debugger, int maxAsyncDepth) const;
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci  // V8StackTrace implementation.
721cb0ef41Sopenharmony_ci  // This method drops the async stack trace.
731cb0ef41Sopenharmony_ci  std::unique_ptr<V8StackTrace> clone() override;
741cb0ef41Sopenharmony_ci  StringView firstNonEmptySourceURL() const override;
751cb0ef41Sopenharmony_ci  bool isEmpty() const override;
761cb0ef41Sopenharmony_ci  StringView topSourceURL() const override;
771cb0ef41Sopenharmony_ci  int topLineNumber() const override;    // 1-based.
781cb0ef41Sopenharmony_ci  int topColumnNumber() const override;  // 1-based.
791cb0ef41Sopenharmony_ci  int topScriptId() const override;
801cb0ef41Sopenharmony_ci  StringView topFunctionName() const override;
811cb0ef41Sopenharmony_ci  std::unique_ptr<protocol::Runtime::API::StackTrace> buildInspectorObject(
821cb0ef41Sopenharmony_ci      int maxAsyncDepth) const override;
831cb0ef41Sopenharmony_ci  std::unique_ptr<StringBuffer> toString() const override;
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci  bool isEqualIgnoringTopFrame(V8StackTraceImpl* stackTrace) const;
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci private:
881cb0ef41Sopenharmony_ci  V8StackTraceImpl(std::vector<std::shared_ptr<StackFrame>> frames,
891cb0ef41Sopenharmony_ci                   int maxAsyncDepth,
901cb0ef41Sopenharmony_ci                   std::shared_ptr<AsyncStackTrace> asyncParent,
911cb0ef41Sopenharmony_ci                   const V8StackTraceId& externalParent);
921cb0ef41Sopenharmony_ci
931cb0ef41Sopenharmony_ci  class StackFrameIterator {
941cb0ef41Sopenharmony_ci   public:
951cb0ef41Sopenharmony_ci    explicit StackFrameIterator(const V8StackTraceImpl* stackTrace);
961cb0ef41Sopenharmony_ci
971cb0ef41Sopenharmony_ci    void next();
981cb0ef41Sopenharmony_ci    StackFrame* frame();
991cb0ef41Sopenharmony_ci    bool done();
1001cb0ef41Sopenharmony_ci
1011cb0ef41Sopenharmony_ci   private:
1021cb0ef41Sopenharmony_ci    std::vector<std::shared_ptr<StackFrame>>::const_iterator m_currentIt;
1031cb0ef41Sopenharmony_ci    std::vector<std::shared_ptr<StackFrame>>::const_iterator m_currentEnd;
1041cb0ef41Sopenharmony_ci    AsyncStackTrace* m_parent;
1051cb0ef41Sopenharmony_ci  };
1061cb0ef41Sopenharmony_ci
1071cb0ef41Sopenharmony_ci  std::vector<std::shared_ptr<StackFrame>> m_frames;
1081cb0ef41Sopenharmony_ci  int m_maxAsyncDepth;
1091cb0ef41Sopenharmony_ci  std::weak_ptr<AsyncStackTrace> m_asyncParent;
1101cb0ef41Sopenharmony_ci  V8StackTraceId m_externalParent;
1111cb0ef41Sopenharmony_ci};
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ciclass AsyncStackTrace {
1141cb0ef41Sopenharmony_ci public:
1151cb0ef41Sopenharmony_ci  AsyncStackTrace(const AsyncStackTrace&) = delete;
1161cb0ef41Sopenharmony_ci  AsyncStackTrace& operator=(const AsyncStackTrace&) = delete;
1171cb0ef41Sopenharmony_ci  static std::shared_ptr<AsyncStackTrace> capture(V8Debugger*,
1181cb0ef41Sopenharmony_ci                                                  const String16& description,
1191cb0ef41Sopenharmony_ci                                                  bool skipTopFrame = false);
1201cb0ef41Sopenharmony_ci  static uintptr_t store(V8Debugger* debugger,
1211cb0ef41Sopenharmony_ci                         std::shared_ptr<AsyncStackTrace> stack);
1221cb0ef41Sopenharmony_ci
1231cb0ef41Sopenharmony_ci  std::unique_ptr<protocol::Runtime::StackTrace> buildInspectorObject(
1241cb0ef41Sopenharmony_ci      V8Debugger* debugger, int maxAsyncDepth) const;
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_ci  const String16& description() const;
1271cb0ef41Sopenharmony_ci  std::weak_ptr<AsyncStackTrace> parent() const;
1281cb0ef41Sopenharmony_ci  bool isEmpty() const;
1291cb0ef41Sopenharmony_ci  const V8StackTraceId& externalParent() const { return m_externalParent; }
1301cb0ef41Sopenharmony_ci
1311cb0ef41Sopenharmony_ci  const std::vector<std::shared_ptr<StackFrame>>& frames() const {
1321cb0ef41Sopenharmony_ci    return m_frames;
1331cb0ef41Sopenharmony_ci  }
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_ci private:
1361cb0ef41Sopenharmony_ci  AsyncStackTrace(const String16& description,
1371cb0ef41Sopenharmony_ci                  std::vector<std::shared_ptr<StackFrame>> frames,
1381cb0ef41Sopenharmony_ci                  std::shared_ptr<AsyncStackTrace> asyncParent,
1391cb0ef41Sopenharmony_ci                  const V8StackTraceId& externalParent);
1401cb0ef41Sopenharmony_ci
1411cb0ef41Sopenharmony_ci  uintptr_t m_id;
1421cb0ef41Sopenharmony_ci  String16 m_description;
1431cb0ef41Sopenharmony_ci
1441cb0ef41Sopenharmony_ci  std::vector<std::shared_ptr<StackFrame>> m_frames;
1451cb0ef41Sopenharmony_ci  std::weak_ptr<AsyncStackTrace> m_asyncParent;
1461cb0ef41Sopenharmony_ci  V8StackTraceId m_externalParent;
1471cb0ef41Sopenharmony_ci};
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ci}  // namespace v8_inspector
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_ci#endif  // V8_INSPECTOR_V8_STACK_TRACE_IMPL_H_
152