Line data Source code
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 10 : explicit CodeTracer(int isolate_id) : file_(nullptr), scope_depth_(0) {
19 5 : if (!ShouldRedirect()) {
20 5 : file_ = stdout;
21 10 : return;
22 : }
23 :
24 0 : if (FLAG_redirect_code_traces_to != nullptr) {
25 0 : StrNCpy(filename_, FLAG_redirect_code_traces_to, filename_.length());
26 0 : } else if (isolate_id >= 0) {
27 : SNPrintF(filename_, "code-%d-%d.asm", base::OS::GetCurrentProcessId(),
28 0 : isolate_id);
29 : } else {
30 0 : SNPrintF(filename_, "code-%d.asm", base::OS::GetCurrentProcessId());
31 : }
32 :
33 0 : WriteChars(filename_.start(), "", 0, false);
34 : }
35 :
36 : class Scope {
37 : public:
38 278 : explicit Scope(CodeTracer* tracer) : tracer_(tracer) { tracer->OpenFile(); }
39 278 : ~Scope() { tracer_->CloseFile(); }
40 :
41 14366 : FILE* file() const { return tracer_->file(); }
42 :
43 : private:
44 : CodeTracer* tracer_;
45 : };
46 :
47 278 : void OpenFile() {
48 278 : if (!ShouldRedirect()) {
49 278 : return;
50 : }
51 :
52 0 : if (file_ == nullptr) {
53 0 : file_ = base::OS::FOpen(filename_.start(), "ab");
54 : }
55 :
56 0 : scope_depth_++;
57 : }
58 :
59 278 : void CloseFile() {
60 278 : if (!ShouldRedirect()) {
61 278 : return;
62 : }
63 :
64 0 : if (--scope_depth_ == 0) {
65 0 : fclose(file_);
66 0 : file_ = nullptr;
67 : }
68 : }
69 :
70 : FILE* file() const { return file_; }
71 :
72 : private:
73 561 : 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_
|