File: v8_unwinder.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (95 lines) | stat: -rw-r--r-- 3,316 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_RENDERER_V8_UNWINDER_H_
#define CHROME_RENDERER_V8_UNWINDER_H_

#include <memory>
#include <set>

#include "base/check_op.h"
#include "base/memory/raw_ptr.h"
#include "base/profiler/unwinder.h"
#include "v8/include/v8-unwinder.h"

namespace v8 {
class Isolate;
}  // namespace v8

// Implements stack frame unwinding for V8 generated code frames, for use with
// the StackSamplingProfiler.
class V8Unwinder : public base::Unwinder {
 public:
  explicit V8Unwinder(v8::Isolate* isolate);
  ~V8Unwinder() override;

  V8Unwinder(const V8Unwinder&) = delete;
  V8Unwinder& operator=(const V8Unwinder&) = delete;

  // Unwinder:
  void InitializeModules() override;
  std::unique_ptr<base::UnwinderStateCapture> CreateUnwinderStateCapture()
      override;
  void OnStackCapture(base::UnwinderStateCapture* capture_state) override;
  void UpdateModules(base::UnwinderStateCapture* capture_state) override;
  bool CanUnwindFrom(const base::Frame& current_frame) const override;
  base::UnwindResult TryUnwind(base::UnwinderStateCapture* capture_state,
                               base::RegisterContext* thread_context,
                               uintptr_t stack_top,
                               std::vector<base::Frame>* stack) override;

  // Build ids generated by the unwinder. Exposed for test use.
  static const char kV8EmbeddedCodeRangeBuildId[];
  static const char kV8CodeRangeBuildId[];

 protected:
  // Invokes CopyCodePages on the Isolate. Virtual to provide a seam for testing
  // of module processing behavior.
  virtual size_t CopyCodePages(size_t capacity, v8::MemoryRange* code_pages);

 private:
  // Custom container for storing V8 code memory ranges. We use this type rather
  // than std::vector to guarantee that no heap allocation occurs during the
  // operations used in OnStackCapture().
  class MemoryRanges : public base::UnwinderStateCapture {
   public:
    explicit MemoryRanges(size_t size);
    ~MemoryRanges() override;

    // Functions that must not heap allocate:
    // Returns a pointer to the start of the internal buffer.
    v8::MemoryRange* buffer() { return ranges_.get(); }
    const v8::MemoryRange* buffer() const { return ranges_.get(); }
    size_t size() const { return size_; }

    // Shrinks the size if `size_` is larger than `size`.
    void ShrinkSize(size_t size);

   private:
    size_t size_;
    std::unique_ptr<v8::MemoryRange[]> ranges_;
  };

  // Compares modules by their extents in memory.
  struct ModuleCompare {
    bool operator()(const base::ModuleCache::Module* a,
                    const base::ModuleCache::Module* b) const;
  };

  const raw_ptr<v8::Isolate> isolate_;
  const v8::JSEntryStubs js_entry_stubs_;
  const v8::MemoryRange embedded_code_range_;

  // The number of code ranges required to represent all of ranges supplied by
  // V8 on the last call to CopyCodePages().
  size_t required_code_ranges_capacity_ = 0;

  // Records the currently active V8 modules, ordered by their extents in
  // memory.
  std::set<raw_ptr<const base::ModuleCache::Module, SetExperimental>,
           ModuleCompare>
      modules_;
};

#endif  // CHROME_RENDERER_V8_UNWINDER_H_