code-tracer.h 2.71 KB
Newer Older
1 2 3 4
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#ifndef V8_DIAGNOSTICS_CODE_TRACER_H_
#define V8_DIAGNOSTICS_CODE_TRACER_H_
7

8
#include "src/base/optional.h"
9 10
#include "src/common/globals.h"
#include "src/flags/flags.h"
11
#include "src/utils/allocation.h"
12
#include "src/utils/ostreams.h"
13 14
#include "src/utils/utils.h"
#include "src/utils/vector.h"
15 16 17 18 19 20 21 22 23 24 25 26

namespace v8 {
namespace internal {

class CodeTracer final : public Malloced {
 public:
  explicit CodeTracer(int isolate_id) : file_(nullptr), scope_depth_(0) {
    if (!ShouldRedirect()) {
      file_ = stdout;
      return;
    }

27 28 29
    if (FLAG_redirect_code_traces_to != nullptr) {
      StrNCpy(filename_, FLAG_redirect_code_traces_to, filename_.length());
    } else if (isolate_id >= 0) {
30 31 32
      SNPrintF(filename_, "code-%d-%d.asm", base::OS::GetCurrentProcessId(),
               isolate_id);
    } else {
33
      SNPrintF(filename_, "code-%d.asm", base::OS::GetCurrentProcessId());
34 35
    }

36
    WriteChars(filename_.begin(), "", 0, false);
37 38 39 40 41 42 43 44 45 46 47 48 49
  }

  class Scope {
   public:
    explicit Scope(CodeTracer* tracer) : tracer_(tracer) { tracer->OpenFile(); }
    ~Scope() { tracer_->CloseFile(); }

    FILE* file() const { return tracer_->file(); }

   private:
    CodeTracer* tracer_;
  };

50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
  class StreamScope : public Scope {
   public:
    explicit StreamScope(CodeTracer* tracer) : Scope(tracer) {
      FILE* file = this->file();
      if (file == stdout) {
        stdout_stream_.emplace();
      } else {
        file_stream_.emplace(file);
      }
    }

    std::ostream& stream() {
      if (stdout_stream_.has_value()) return stdout_stream_.value();
      return file_stream_.value();
    }

   private:
    // Exactly one of these two will be initialized.
    base::Optional<StdoutStream> stdout_stream_;
    base::Optional<OFStream> file_stream_;
  };

72 73 74 75 76 77
  void OpenFile() {
    if (!ShouldRedirect()) {
      return;
    }

    if (file_ == nullptr) {
78
      file_ = base::OS::FOpen(filename_.begin(), "ab");
79 80 81
      CHECK_WITH_MSG(file_ != nullptr,
                     "could not open file. If on Android, try passing "
                     "--redirect-code-traces-to=/sdcard/Download/<file-name>");
82 83 84 85 86 87 88 89 90 91 92
    }

    scope_depth_++;
  }

  void CloseFile() {
    if (!ShouldRedirect()) {
      return;
    }

    if (--scope_depth_ == 0) {
93
      DCHECK_NOT_NULL(file_);
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
      fclose(file_);
      file_ = nullptr;
    }
  }

  FILE* file() const { return file_; }

 private:
  static bool ShouldRedirect() { return FLAG_redirect_code_traces; }

  EmbeddedVector<char, 128> filename_;
  FILE* file_;
  int scope_depth_;
};

}  // namespace internal
}  // namespace v8

112
#endif  // V8_DIAGNOSTICS_CODE_TRACER_H_