v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
code-tracer.h
Go to the documentation of this file.
1// Copyright 2018 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_DIAGNOSTICS_CODE_TRACER_H_
6#define V8_DIAGNOSTICS_CODE_TRACER_H_
7
8#include <optional>
9
12#include "src/base/strings.h"
13#include "src/base/vector.h"
14#include "src/flags/flags.h"
16#include "src/utils/ostreams.h"
17#include "src/utils/utils.h"
18
19namespace v8 {
20namespace internal {
21
22class CodeTracer final : public Malloced {
23 public:
24 explicit CodeTracer(int isolate_id) : file_(nullptr), scope_depth_(0) {
25 if (!ShouldRedirect()) {
26 file_ = stdout;
27 return;
28 }
29
30 if (v8_flags.redirect_code_traces_to != nullptr) {
31 base::StrNCpy(filename_, v8_flags.redirect_code_traces_to,
32 filename_.length());
33 } else if (isolate_id >= 0) {
34 base::SNPrintF(filename_, "code-%d-%d.asm",
35 base::OS::GetCurrentProcessId(), isolate_id);
36 } else {
38 }
39
40 WriteChars(filename_.begin(), "", 0, false);
41 }
42
44 public:
45 explicit Scope(CodeTracer* tracer) : tracer_(tracer) { tracer->OpenFile(); }
46 ~Scope() { tracer_->CloseFile(); }
47
48 FILE* file() const { return tracer_->file(); }
49
50 private:
52 };
53
55 public:
56 explicit StreamScope(CodeTracer* tracer) : Scope(tracer) {
57 FILE* file = this->file();
58 if (file == stdout) {
59 stdout_stream_.emplace();
60 } else {
61 file_stream_.emplace(file);
62 }
63 }
64
65 std::ostream& stream() {
66 if (stdout_stream_.has_value()) return stdout_stream_.value();
67 return file_stream_.value();
68 }
69
70 private:
71 // Exactly one of these two will be initialized.
72 std::optional<StdoutStream> stdout_stream_;
73 std::optional<OFStream> file_stream_;
74 };
75
76 void OpenFile() {
77 if (!ShouldRedirect()) {
78 return;
79 }
80
81 if (file_ == nullptr) {
83 CHECK_WITH_MSG(file_ != nullptr,
84 "could not open file. If on Android, try passing "
85 "--redirect-code-traces-to=/sdcard/Download/<file-name>");
86 }
87
89 }
90
91 void CloseFile() {
92 if (!ShouldRedirect()) {
93 return;
94 }
95
96 if (--scope_depth_ == 0) {
99 file_ = nullptr;
100 }
101 }
102
103 FILE* file() const { return file_; }
104
105 private:
106 static bool ShouldRedirect() { return v8_flags.redirect_code_traces; }
107
109 FILE* file_;
111};
112
113} // namespace internal
114} // namespace v8
115
116#endif // V8_DIAGNOSTICS_CODE_TRACER_H_
static FILE * FOpen(const char *path, const char *mode)
static int GetCurrentProcessId()
constexpr T * begin() const
Definition vector.h:96
Scope(CodeTracer *tracer)
Definition code-tracer.h:45
std::optional< StdoutStream > stdout_stream_
Definition code-tracer.h:72
std::optional< OFStream > file_stream_
Definition code-tracer.h:73
CodeTracer(int isolate_id)
Definition code-tracer.h:24
static bool ShouldRedirect()
base::EmbeddedVector< char, 128 > filename_
int SNPrintF(Vector< char > str, const char *format,...)
Definition strings.cc:20
int Fclose(FILE *stream)
Definition wrappers.h:22
int WriteChars(const char *filename, const char *str, int size, bool verbose)
Definition utils.cc:188
V8_EXPORT_PRIVATE FlagValues v8_flags
#define CHECK_WITH_MSG(condition, message)
Definition logging.h:118
#define DCHECK_NOT_NULL(val)
Definition logging.h:492
Symbol file
#define V8_NODISCARD
Definition v8config.h:693