V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
code-tracer.h
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_CODE_TRACER_H_
6 #define V8_CODE_TRACER_H_
7 
8 #include "src/allocation.h"
9 #include "src/flags.h"
10 #include "src/globals.h"
11 #include "src/utils.h"
12 
13 namespace v8 {
14 namespace internal {
15 
16 class CodeTracer final : public Malloced {
17  public:
18  explicit CodeTracer(int isolate_id) : file_(nullptr), scope_depth_(0) {
19  if (!ShouldRedirect()) {
20  file_ = stdout;
21  return;
22  }
23 
24  if (FLAG_redirect_code_traces_to != nullptr) {
25  StrNCpy(filename_, FLAG_redirect_code_traces_to, filename_.length());
26  } else if (isolate_id >= 0) {
27  SNPrintF(filename_, "code-%d-%d.asm", base::OS::GetCurrentProcessId(),
28  isolate_id);
29  } else {
30  SNPrintF(filename_, "code-%d.asm", base::OS::GetCurrentProcessId());
31  }
32 
33  WriteChars(filename_.start(), "", 0, false);
34  }
35 
36  class Scope {
37  public:
38  explicit Scope(CodeTracer* tracer) : tracer_(tracer) { tracer->OpenFile(); }
39  ~Scope() { tracer_->CloseFile(); }
40 
41  FILE* file() const { return tracer_->file(); }
42 
43  private:
44  CodeTracer* tracer_;
45  };
46 
47  void OpenFile() {
48  if (!ShouldRedirect()) {
49  return;
50  }
51 
52  if (file_ == nullptr) {
53  file_ = base::OS::FOpen(filename_.start(), "ab");
54  }
55 
56  scope_depth_++;
57  }
58 
59  void CloseFile() {
60  if (!ShouldRedirect()) {
61  return;
62  }
63 
64  if (--scope_depth_ == 0) {
65  fclose(file_);
66  file_ = nullptr;
67  }
68  }
69 
70  FILE* file() const { return file_; }
71 
72  private:
73  static bool ShouldRedirect() { return FLAG_redirect_code_traces; }
74 
75  EmbeddedVector<char, 128> filename_;
76  FILE* file_;
77  int scope_depth_;
78 };
79 
80 } // namespace internal
81 } // namespace v8
82 
83 #endif // V8_CODE_TRACER_H_
Definition: libplatform.h:13