Commit 23531d82 authored by Camillo Bruni's avatar Camillo Bruni Committed by Commit Bot

Reland "[test][d8] Add d8.log.getAndStop helper"

This is a reland of 95aa697b

Original change's description:
> [test][d8] Add d8.log.getAndStop helper
>
> The new helper function allows us to write tests for log parsing
> without the need to first generating a log file. This makes it easier
> to spot errors when the log format changes.
>
> - Add d8 global variable
> - Add file_name accessor to Logger and Log classes
> - Change OS::LogFileOpenMode to w+ / wb+
> - Use separate Log::WriteLogHeader method
> - Remove unused logger_ instance variable from Log
>
> Bug: v8:10644
> Change-Id: Ifc7e35aa4e91b3f01f0847843263946e085944c3
> Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2387563
> Commit-Queue: Camillo Bruni <cbruni@chromium.org>
> Reviewed-by: Michael Lippautz <mlippautz@chromium.org>
> Reviewed-by: Toon Verwaest <verwaest@chromium.org>
> Reviewed-by: Sathya Gunasekaran  <gsathya@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#69715}

Bug: v8:10644

TBR=verwaest@chromium.org

Change-Id: I54741344834d88a376b74e2e3a2047e880a94624
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2396081
Commit-Queue: Camillo Bruni <cbruni@chromium.org>
Reviewed-by: 's avatarMichael Lippautz <mlippautz@chromium.org>
Reviewed-by: 's avatarSathya Gunasekaran  <gsathya@chromium.org>
Cr-Commit-Position: refs/heads/master@{#69769}
parent a49a9710
...@@ -679,9 +679,7 @@ FILE* OS::OpenTemporaryFile() { ...@@ -679,9 +679,7 @@ FILE* OS::OpenTemporaryFile() {
return tmpfile(); return tmpfile();
} }
const char* const OS::LogFileOpenMode = "w+";
const char* const OS::LogFileOpenMode = "w";
void OS::Print(const char* format, ...) { void OS::Print(const char* format, ...) {
va_list args; va_list args;
......
...@@ -603,8 +603,7 @@ FILE* OS::OpenTemporaryFile() { ...@@ -603,8 +603,7 @@ FILE* OS::OpenTemporaryFile() {
// Open log file in binary mode to avoid /n -> /r/n conversion. // Open log file in binary mode to avoid /n -> /r/n conversion.
const char* const OS::LogFileOpenMode = "wb"; const char* const OS::LogFileOpenMode = "wb+";
// Print (debug) message to console. // Print (debug) message to console.
void OS::Print(const char* format, ...) { void OS::Print(const char* format, ...) {
......
...@@ -43,6 +43,7 @@ ...@@ -43,6 +43,7 @@
#include "src/init/v8.h" #include "src/init/v8.h"
#include "src/interpreter/interpreter.h" #include "src/interpreter/interpreter.h"
#include "src/logging/counters.h" #include "src/logging/counters.h"
#include "src/logging/log-utils.h"
#include "src/objects/managed.h" #include "src/objects/managed.h"
#include "src/objects/objects-inl.h" #include "src/objects/objects-inl.h"
#include "src/objects/objects.h" #include "src/objects/objects.h"
...@@ -1503,6 +1504,41 @@ void Shell::RealmSharedSet(Local<String> property, Local<Value> value, ...@@ -1503,6 +1504,41 @@ void Shell::RealmSharedSet(Local<String> property, Local<Value> value,
data->realm_shared_.Reset(isolate, value); data->realm_shared_.Reset(isolate, value);
} }
void Shell::LogGetAndStop(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate* isolate = args.GetIsolate();
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
HandleScope handle_scope(isolate);
std::string file_name = i_isolate->logger()->file_name();
if (i::Log::IsLoggingToConsole(file_name)) {
Throw(isolate, "Logging to console instead of file.");
return;
}
if (!i_isolate->logger()->is_logging()) {
Throw(isolate, "Logging not enabled.");
return;
}
std::string raw_log;
FILE* log_file = i_isolate->logger()->TearDownAndGetLogFile();
CHECK_NOT_NULL(log_file);
bool exists = false;
raw_log = i::ReadFile(log_file, &exists, true);
fclose(log_file);
if (!exists) {
Throw(isolate, "Unable to read log file.");
return;
}
Local<String> result =
String::NewFromUtf8(isolate, raw_log.c_str(), NewStringType::kNormal,
static_cast<int>(raw_log.size()))
.ToLocalChecked();
args.GetReturnValue().Set(result);
}
// async_hooks.createHook() registers functions to be called for different // async_hooks.createHook() registers functions to be called for different
// lifetime events of each async operation. // lifetime events of each async operation.
void Shell::AsyncHooksCreateHook( void Shell::AsyncHooksCreateHook(
...@@ -2071,6 +2107,11 @@ Local<String> Shell::Stringify(Isolate* isolate, Local<Value> value) { ...@@ -2071,6 +2107,11 @@ Local<String> Shell::Stringify(Isolate* isolate, Local<Value> value) {
Local<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) { Local<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) {
Local<ObjectTemplate> global_template = ObjectTemplate::New(isolate); Local<ObjectTemplate> global_template = ObjectTemplate::New(isolate);
global_template->Set(Symbol::GetToStringTag(isolate),
String::NewFromUtf8Literal(isolate, "global"));
global_template->Set(isolate, "version",
FunctionTemplate::New(isolate, Version));
global_template->Set(isolate, "print", FunctionTemplate::New(isolate, Print)); global_template->Set(isolate, "print", FunctionTemplate::New(isolate, Print));
global_template->Set(isolate, "printErr", global_template->Set(isolate, "printErr",
FunctionTemplate::New(isolate, PrintErr)); FunctionTemplate::New(isolate, PrintErr));
...@@ -2089,57 +2130,37 @@ Local<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) { ...@@ -2089,57 +2130,37 @@ Local<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) {
if (!options.omit_quit) { if (!options.omit_quit) {
global_template->Set(isolate, "quit", FunctionTemplate::New(isolate, Quit)); global_template->Set(isolate, "quit", FunctionTemplate::New(isolate, Quit));
} }
Local<ObjectTemplate> test_template = ObjectTemplate::New(isolate); global_template->Set(isolate, "testRunner",
global_template->Set(isolate, "testRunner", test_template); Shell::CreateTestRunnerTemplate(isolate));
test_template->Set(isolate, "notifyDone", global_template->Set(isolate, "Realm", Shell::CreateRealmTemplate(isolate));
FunctionTemplate::New(isolate, NotifyDone)); global_template->Set(isolate, "performance",
test_template->Set(isolate, "waitUntilDone", Shell::CreatePerformanceTemplate(isolate));
FunctionTemplate::New(isolate, WaitUntilDone)); global_template->Set(isolate, "Worker", Shell::CreateWorkerTemplate(isolate));
// Reliable access to quit functionality. The "quit" method function global_template->Set(isolate, "os", Shell::CreateOSTemplate(isolate));
// installed on the global object can be hidden with the --omit-quit flag global_template->Set(isolate, "d8", Shell::CreateD8Template(isolate));
// (e.g. on asan bots).
test_template->Set(isolate, "quit", FunctionTemplate::New(isolate, Quit));
global_template->Set(isolate, "version", #ifdef V8_FUZZILLI
FunctionTemplate::New(isolate, Version)); global_template->Set(
global_template->Set(Symbol::GetToStringTag(isolate), String::NewFromUtf8(isolate, "fuzzilli", NewStringType::kNormal)
String::NewFromUtf8Literal(isolate, "global")); .ToLocalChecked(),
FunctionTemplate::New(isolate, Fuzzilli), PropertyAttribute::DontEnum);
#endif // V8_FUZZILLI
// Bind the Realm object. if (i::FLAG_expose_async_hooks) {
Local<ObjectTemplate> realm_template = ObjectTemplate::New(isolate); global_template->Set(isolate, "async_hooks",
realm_template->Set(isolate, "current", Shell::CreateAsyncHookTemplate(isolate));
FunctionTemplate::New(isolate, RealmCurrent)); }
realm_template->Set(isolate, "owner",
FunctionTemplate::New(isolate, RealmOwner));
realm_template->Set(isolate, "global",
FunctionTemplate::New(isolate, RealmGlobal));
realm_template->Set(isolate, "create",
FunctionTemplate::New(isolate, RealmCreate));
realm_template->Set(
isolate, "createAllowCrossRealmAccess",
FunctionTemplate::New(isolate, RealmCreateAllowCrossRealmAccess));
realm_template->Set(isolate, "navigate",
FunctionTemplate::New(isolate, RealmNavigate));
realm_template->Set(isolate, "detachGlobal",
FunctionTemplate::New(isolate, RealmDetachGlobal));
realm_template->Set(isolate, "dispose",
FunctionTemplate::New(isolate, RealmDispose));
realm_template->Set(isolate, "switch",
FunctionTemplate::New(isolate, RealmSwitch));
realm_template->Set(isolate, "eval",
FunctionTemplate::New(isolate, RealmEval));
realm_template->SetAccessor(String::NewFromUtf8Literal(isolate, "shared"),
RealmSharedGet, RealmSharedSet);
global_template->Set(isolate, "Realm", realm_template);
Local<ObjectTemplate> performance_template = ObjectTemplate::New(isolate); return global_template;
performance_template->Set(isolate, "now", }
FunctionTemplate::New(isolate, PerformanceNow));
performance_template->Set( Local<ObjectTemplate> Shell::CreateOSTemplate(Isolate* isolate) {
isolate, "measureMemory", Local<ObjectTemplate> os_template = ObjectTemplate::New(isolate);
FunctionTemplate::New(isolate, PerformanceMeasureMemory)); AddOSMethods(isolate, os_template);
global_template->Set(isolate, "performance", performance_template); return os_template;
}
Local<FunctionTemplate> Shell::CreateWorkerTemplate(Isolate* isolate) {
Local<FunctionTemplate> worker_fun_template = Local<FunctionTemplate> worker_fun_template =
FunctionTemplate::New(isolate, WorkerNew); FunctionTemplate::New(isolate, WorkerNew);
Local<Signature> worker_signature = Local<Signature> worker_signature =
...@@ -2164,23 +2185,12 @@ Local<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) { ...@@ -2164,23 +2185,12 @@ Local<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) {
FunctionTemplate::New(isolate, WorkerGetMessage, Local<Value>(), FunctionTemplate::New(isolate, WorkerGetMessage, Local<Value>(),
worker_signature)); worker_signature));
worker_fun_template->InstanceTemplate()->SetInternalFieldCount(1); worker_fun_template->InstanceTemplate()->SetInternalFieldCount(1);
global_template->Set(isolate, "Worker", worker_fun_template); return worker_fun_template;
}
Local<ObjectTemplate> os_templ = ObjectTemplate::New(isolate);
AddOSMethods(isolate, os_templ);
global_template->Set(isolate, "os", os_templ);
#ifdef V8_FUZZILLI
global_template->Set(
String::NewFromUtf8(isolate, "fuzzilli", NewStringType::kNormal)
.ToLocalChecked(),
FunctionTemplate::New(isolate, Fuzzilli), PropertyAttribute::DontEnum);
#endif // V8_FUZZILLI
if (i::FLAG_expose_async_hooks) { Local<ObjectTemplate> Shell::CreateAsyncHookTemplate(Isolate* isolate) {
Local<ObjectTemplate> async_hooks_templ = ObjectTemplate::New(isolate); Local<ObjectTemplate> async_hooks_templ = ObjectTemplate::New(isolate);
async_hooks_templ->Set( async_hooks_templ->Set(isolate, "createHook",
isolate, "createHook",
FunctionTemplate::New(isolate, AsyncHooksCreateHook)); FunctionTemplate::New(isolate, AsyncHooksCreateHook));
async_hooks_templ->Set( async_hooks_templ->Set(
isolate, "executionAsyncId", isolate, "executionAsyncId",
...@@ -2188,10 +2198,70 @@ Local<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) { ...@@ -2188,10 +2198,70 @@ Local<ObjectTemplate> Shell::CreateGlobalTemplate(Isolate* isolate) {
async_hooks_templ->Set( async_hooks_templ->Set(
isolate, "triggerAsyncId", isolate, "triggerAsyncId",
FunctionTemplate::New(isolate, AsyncHooksTriggerAsyncId)); FunctionTemplate::New(isolate, AsyncHooksTriggerAsyncId));
global_template->Set(isolate, "async_hooks", async_hooks_templ); return async_hooks_templ;
} }
return global_template; Local<ObjectTemplate> Shell::CreateTestRunnerTemplate(Isolate* isolate) {
Local<ObjectTemplate> test_template = ObjectTemplate::New(isolate);
test_template->Set(isolate, "notifyDone",
FunctionTemplate::New(isolate, NotifyDone));
test_template->Set(isolate, "waitUntilDone",
FunctionTemplate::New(isolate, WaitUntilDone));
// Reliable access to quit functionality. The "quit" method function
// installed on the global object can be hidden with the --omit-quit flag
// (e.g. on asan bots).
test_template->Set(isolate, "quit", FunctionTemplate::New(isolate, Quit));
return test_template;
}
Local<ObjectTemplate> Shell::CreatePerformanceTemplate(Isolate* isolate) {
Local<ObjectTemplate> performance_template = ObjectTemplate::New(isolate);
performance_template->Set(isolate, "now",
FunctionTemplate::New(isolate, PerformanceNow));
performance_template->Set(
isolate, "measureMemory",
FunctionTemplate::New(isolate, PerformanceMeasureMemory));
return performance_template;
}
Local<ObjectTemplate> Shell::CreateRealmTemplate(Isolate* isolate) {
Local<ObjectTemplate> realm_template = ObjectTemplate::New(isolate);
realm_template->Set(isolate, "current",
FunctionTemplate::New(isolate, RealmCurrent));
realm_template->Set(isolate, "owner",
FunctionTemplate::New(isolate, RealmOwner));
realm_template->Set(isolate, "global",
FunctionTemplate::New(isolate, RealmGlobal));
realm_template->Set(isolate, "create",
FunctionTemplate::New(isolate, RealmCreate));
realm_template->Set(
isolate, "createAllowCrossRealmAccess",
FunctionTemplate::New(isolate, RealmCreateAllowCrossRealmAccess));
realm_template->Set(isolate, "navigate",
FunctionTemplate::New(isolate, RealmNavigate));
realm_template->Set(isolate, "detachGlobal",
FunctionTemplate::New(isolate, RealmDetachGlobal));
realm_template->Set(isolate, "dispose",
FunctionTemplate::New(isolate, RealmDispose));
realm_template->Set(isolate, "switch",
FunctionTemplate::New(isolate, RealmSwitch));
realm_template->Set(isolate, "eval",
FunctionTemplate::New(isolate, RealmEval));
realm_template->SetAccessor(String::NewFromUtf8Literal(isolate, "shared"),
RealmSharedGet, RealmSharedSet);
return realm_template;
}
Local<ObjectTemplate> Shell::CreateD8Template(Isolate* isolate) {
Local<ObjectTemplate> d8_template = ObjectTemplate::New(isolate);
{
Local<ObjectTemplate> log_template = ObjectTemplate::New(isolate);
log_template->Set(isolate, "getAndStop",
FunctionTemplate::New(isolate, LogGetAndStop));
d8_template->Set(isolate, "log", log_template);
}
return d8_template;
} }
static void PrintMessageCallback(Local<Message> message, Local<Value> error) { static void PrintMessageCallback(Local<Message> message, Local<Value> error) {
......
...@@ -401,6 +401,8 @@ class Shell : public i::AllStatic { ...@@ -401,6 +401,8 @@ class Shell : public i::AllStatic {
static void RealmSharedSet(Local<String> property, Local<Value> value, static void RealmSharedSet(Local<String> property, Local<Value> value,
const PropertyCallbackInfo<void>& info); const PropertyCallbackInfo<void>& info);
static void LogGetAndStop(const v8::FunctionCallbackInfo<v8::Value>& args);
static void AsyncHooksCreateHook( static void AsyncHooksCreateHook(
const v8::FunctionCallbackInfo<v8::Value>& args); const v8::FunctionCallbackInfo<v8::Value>& args);
static void AsyncHooksExecutionAsyncId( static void AsyncHooksExecutionAsyncId(
...@@ -539,7 +541,16 @@ class Shell : public i::AllStatic { ...@@ -539,7 +541,16 @@ class Shell : public i::AllStatic {
static Local<String> Stringify(Isolate* isolate, Local<Value> value); static Local<String> Stringify(Isolate* isolate, Local<Value> value);
static void RunShell(Isolate* isolate); static void RunShell(Isolate* isolate);
static bool SetOptions(int argc, char* argv[]); static bool SetOptions(int argc, char* argv[]);
static Local<ObjectTemplate> CreateGlobalTemplate(Isolate* isolate); static Local<ObjectTemplate> CreateGlobalTemplate(Isolate* isolate);
static Local<ObjectTemplate> CreateOSTemplate(Isolate* isolate);
static Local<FunctionTemplate> CreateWorkerTemplate(Isolate* isolate);
static Local<ObjectTemplate> CreateAsyncHookTemplate(Isolate* isolate);
static Local<ObjectTemplate> CreateTestRunnerTemplate(Isolate* isolate);
static Local<ObjectTemplate> CreatePerformanceTemplate(Isolate* isolate);
static Local<ObjectTemplate> CreateRealmTemplate(Isolate* isolate);
static Local<ObjectTemplate> CreateD8Template(Isolate* isolate);
static MaybeLocal<Context> CreateRealm( static MaybeLocal<Context> CreateRealm(
const v8::FunctionCallbackInfo<v8::Value>& args, int index, const v8::FunctionCallbackInfo<v8::Value>& args, int index,
v8::MaybeLocal<Value> global_object); v8::MaybeLocal<Value> global_object);
......
...@@ -3077,7 +3077,8 @@ void Isolate::Deinit() { ...@@ -3077,7 +3077,8 @@ void Isolate::Deinit() {
cancelable_task_manager()->CancelAndWait(); cancelable_task_manager()->CancelAndWait();
heap_.TearDown(); heap_.TearDown();
logger_->TearDown(); FILE* logfile = logger_->TearDownAndGetLogFile();
if (logfile != nullptr) fclose(logfile);
if (wasm_engine_) { if (wasm_engine_) {
wasm_engine_->RemoveIsolate(this); wasm_engine_->RemoveIsolate(this);
......
...@@ -23,28 +23,43 @@ const char* const Log::kLogToTemporaryFile = "&"; ...@@ -23,28 +23,43 @@ const char* const Log::kLogToTemporaryFile = "&";
const char* const Log::kLogToConsole = "-"; const char* const Log::kLogToConsole = "-";
// static // static
FILE* Log::CreateOutputHandle(const char* file_name) { FILE* Log::CreateOutputHandle(std::string file_name) {
// If we're logging anything, we need to open the log file. // If we're logging anything, we need to open the log file.
if (!Log::InitLogAtStart()) { if (!Log::InitLogAtStart()) {
return nullptr; return nullptr;
} else if (strcmp(file_name, kLogToConsole) == 0) { } else if (Log::IsLoggingToConsole(file_name)) {
return stdout; return stdout;
} else if (strcmp(file_name, kLogToTemporaryFile) == 0) { } else if (Log::IsLoggingToTemporaryFile(file_name)) {
return base::OS::OpenTemporaryFile(); return base::OS::OpenTemporaryFile();
} else { } else {
return base::OS::FOpen(file_name, base::OS::LogFileOpenMode); return base::OS::FOpen(file_name.c_str(), base::OS::LogFileOpenMode);
} }
} }
Log::Log(Logger* logger, const char* file_name) // static
: output_handle_(Log::CreateOutputHandle(file_name)), bool Log::IsLoggingToConsole(std::string file_name) {
return file_name.compare(Log::kLogToConsole) == 0;
}
// static
bool Log::IsLoggingToTemporaryFile(std::string file_name) {
return file_name.compare(Log::kLogToTemporaryFile) == 0;
}
Log::Log(std::string file_name)
: file_name_(file_name),
output_handle_(Log::CreateOutputHandle(file_name)),
os_(output_handle_ == nullptr ? stdout : output_handle_), os_(output_handle_ == nullptr ? stdout : output_handle_),
is_enabled_(output_handle_ != nullptr), is_enabled_(output_handle_ != nullptr),
format_buffer_(NewArray<char>(kMessageBufferSize)), format_buffer_(NewArray<char>(kMessageBufferSize)) {
logger_(logger) {
if (output_handle_ == nullptr) return; if (output_handle_ == nullptr) return;
Log::MessageBuilder msg(this); WriteLogHeader();
}
void Log::WriteLogHeader() {
std::unique_ptr<Log::MessageBuilder> msg_ptr = NewMessageBuilder();
if (!msg_ptr) return;
Log::MessageBuilder& msg = *msg_ptr.get();
LogSeparator kNext = LogSeparator::kSeparator; LogSeparator kNext = LogSeparator::kSeparator;
msg << "v8-version" << kNext << Version::GetMajor() << kNext msg << "v8-version" << kNext << Version::GetMajor() << kNext
<< Version::GetMinor() << kNext << Version::GetBuild() << kNext << Version::GetMinor() << kNext << Version::GetBuild() << kNext
...@@ -71,20 +86,17 @@ FILE* Log::Close() { ...@@ -71,20 +86,17 @@ FILE* Log::Close() {
base::MutexGuard guard(&mutex_); base::MutexGuard guard(&mutex_);
FILE* result = nullptr; FILE* result = nullptr;
if (output_handle_ != nullptr) { if (output_handle_ != nullptr) {
if (strcmp(FLAG_logfile, kLogToTemporaryFile) != 0) { fflush(output_handle_);
fclose(output_handle_);
} else {
result = output_handle_; result = output_handle_;
} }
}
output_handle_ = nullptr; output_handle_ = nullptr;
format_buffer_.reset(); format_buffer_.reset();
is_enabled_.store(false, std::memory_order_relaxed); is_enabled_.store(false, std::memory_order_relaxed);
return result; return result;
} }
std::string Log::file_name() const { return file_name_; }
Log::MessageBuilder::MessageBuilder(Log* log) Log::MessageBuilder::MessageBuilder(Log* log)
: log_(log), lock_guard_(&log_->mutex_) { : log_(log), lock_guard_(&log_->mutex_) {
DCHECK_NOT_NULL(log_->format_buffer_.get()); DCHECK_NOT_NULL(log_->format_buffer_.get());
......
...@@ -30,22 +30,27 @@ enum class LogSeparator { kSeparator }; ...@@ -30,22 +30,27 @@ enum class LogSeparator { kSeparator };
// Functions and data for performing output of log messages. // Functions and data for performing output of log messages.
class Log { class Log {
public: public:
Log(Logger* log, const char* log_file_name); explicit Log(std::string log_file_name);
static bool InitLogAtStart() { static bool InitLogAtStart() {
return FLAG_log || FLAG_log_api || FLAG_log_code || FLAG_log_handles || return FLAG_log || FLAG_log_all || FLAG_log_api || FLAG_log_code ||
FLAG_log_suspect || FLAG_ll_prof || FLAG_perf_basic_prof || FLAG_log_handles || FLAG_log_suspect || FLAG_ll_prof ||
FLAG_perf_prof || FLAG_log_source_code || FLAG_gdbjit || FLAG_perf_basic_prof || FLAG_perf_prof || FLAG_log_source_code ||
FLAG_log_internal_timer_events || FLAG_prof_cpp || FLAG_trace_ic || FLAG_gdbjit || FLAG_log_internal_timer_events || FLAG_prof_cpp ||
FLAG_log_function_events || FLAG_trace_zone_stats || FLAG_trace_ic || FLAG_log_function_events || FLAG_trace_zone_stats ||
FLAG_turbo_profiling_log_builtins; FLAG_turbo_profiling_log_builtins;
} }
V8_EXPORT_PRIVATE static bool IsLoggingToConsole(std::string file_name);
V8_EXPORT_PRIVATE static bool IsLoggingToTemporaryFile(std::string file_name);
// Frees all resources acquired in Initialize and Open... functions. // Frees all resources acquired in Initialize and Open... functions.
// When a temporary file is used for the log, returns its stream descriptor, // When a temporary file is used for the log, returns its stream descriptor,
// leaving the file open. // leaving the file open.
FILE* Close(); FILE* Close();
std::string file_name() const;
// Returns whether logging is enabled. // Returns whether logging is enabled.
bool IsEnabled() { return is_enabled_.load(std::memory_order_relaxed); } bool IsEnabled() { return is_enabled_.load(std::memory_order_relaxed); }
...@@ -109,19 +114,15 @@ class Log { ...@@ -109,19 +114,15 @@ class Log {
std::unique_ptr<Log::MessageBuilder> NewMessageBuilder(); std::unique_ptr<Log::MessageBuilder> NewMessageBuilder();
private: private:
static FILE* CreateOutputHandle(const char* file_name); static FILE* CreateOutputHandle(std::string file_name);
// Implementation of writing to a log file. void WriteLogHeader();
int WriteToFile(const char* msg, int length) {
DCHECK_NOT_NULL(output_handle_);
os_.write(msg, length);
DCHECK(!os_.bad());
return length;
}
std::string file_name_;
// When logging is active output_handle_ is used to store a pointer to log // When logging is active output_handle_ is used to store a pointer to log
// destination. mutex_ should be acquired before using output_handle_. // destination. mutex_ should be acquired before using output_handle_.
FILE* output_handle_; FILE* output_handle_;
OFStream os_; OFStream os_;
// Stores whether logging is enabled. // Stores whether logging is enabled.
...@@ -134,10 +135,6 @@ class Log { ...@@ -134,10 +135,6 @@ class Log {
// Buffer used for formatting log messages. This is a singleton buffer and // Buffer used for formatting log messages. This is a singleton buffer and
// mutex_ should be acquired before using it. // mutex_ should be acquired before using it.
std::unique_ptr<char[]> format_buffer_; std::unique_ptr<char[]> format_buffer_;
Logger* logger_;
friend class Logger;
}; };
template <> template <>
......
...@@ -1974,16 +1974,13 @@ void Logger::LogAllMaps() { ...@@ -1974,16 +1974,13 @@ void Logger::LogAllMaps() {
} }
} }
static void AddIsolateIdIfNeeded(std::ostream& os, // NOLINT static void AddIsolateIdIfNeeded(std::ostream& os, Isolate* isolate) {
Isolate* isolate) { if (!FLAG_logfile_per_isolate) return;
if (FLAG_logfile_per_isolate) { os << "isolate-" << isolate << "-" << base::OS::GetCurrentProcessId() << "-";
os << "isolate-" << isolate << "-" << base::OS::GetCurrentProcessId()
<< "-";
}
} }
static void PrepareLogFileName(std::ostream& os, // NOLINT static void PrepareLogFileName(std::ostream& os, Isolate* isolate,
Isolate* isolate, const char* file_name) { const char* file_name) {
int dir_separator_count = 0; int dir_separator_count = 0;
for (const char* p = file_name; *p; p++) { for (const char* p = file_name; *p; p++) {
if (base::OS::isDirectorySeparator(*p)) dir_separator_count++; if (base::OS::isDirectorySeparator(*p)) dir_separator_count++;
...@@ -2032,9 +2029,8 @@ bool Logger::SetUp(Isolate* isolate) { ...@@ -2032,9 +2029,8 @@ bool Logger::SetUp(Isolate* isolate) {
is_initialized_ = true; is_initialized_ = true;
std::ostringstream log_file_name; std::ostringstream log_file_name;
std::ostringstream source_log_file_name;
PrepareLogFileName(log_file_name, isolate, FLAG_logfile); PrepareLogFileName(log_file_name, isolate, FLAG_logfile);
log_ = std::make_unique<Log>(this, log_file_name.str().c_str()); log_ = std::make_unique<Log>(log_file_name.str());
#if V8_OS_LINUX #if V8_OS_LINUX
if (FLAG_perf_basic_prof) { if (FLAG_perf_basic_prof) {
...@@ -2063,9 +2059,7 @@ bool Logger::SetUp(Isolate* isolate) { ...@@ -2063,9 +2059,7 @@ bool Logger::SetUp(Isolate* isolate) {
ticker_ = std::make_unique<Ticker>(isolate, FLAG_prof_sampling_interval); ticker_ = std::make_unique<Ticker>(isolate, FLAG_prof_sampling_interval);
if (Log::InitLogAtStart()) { if (Log::InitLogAtStart()) is_logging_ = true;
is_logging_ = true;
}
timer_.Start(); timer_.Start();
...@@ -2075,9 +2069,7 @@ bool Logger::SetUp(Isolate* isolate) { ...@@ -2075,9 +2069,7 @@ bool Logger::SetUp(Isolate* isolate) {
profiler_->Engage(); profiler_->Engage();
} }
if (is_logging_) { if (is_logging_) AddCodeEventListener(this);
AddCodeEventListener(this);
}
return true; return true;
} }
...@@ -2104,6 +2096,7 @@ void Logger::SetCodeEventHandler(uint32_t options, ...@@ -2104,6 +2096,7 @@ void Logger::SetCodeEventHandler(uint32_t options,
} }
sampler::Sampler* Logger::sampler() { return ticker_.get(); } sampler::Sampler* Logger::sampler() { return ticker_.get(); }
std::string Logger::file_name() const { return log_.get()->file_name(); }
void Logger::StopProfilerThread() { void Logger::StopProfilerThread() {
if (profiler_ != nullptr) { if (profiler_ != nullptr) {
...@@ -2112,14 +2105,16 @@ void Logger::StopProfilerThread() { ...@@ -2112,14 +2105,16 @@ void Logger::StopProfilerThread() {
} }
} }
FILE* Logger::TearDown() { FILE* Logger::TearDownAndGetLogFile() {
if (!is_initialized_) return nullptr; if (!is_initialized_) return nullptr;
is_initialized_ = false; is_initialized_ = false;
is_logging_ = false;
// Stop the profiler thread before closing the file. // Stop the profiler thread before closing the file.
StopProfilerThread(); StopProfilerThread();
ticker_.reset(); ticker_.reset();
timer_.Stop();
#if V8_OS_LINUX #if V8_OS_LINUX
if (perf_basic_logger_) { if (perf_basic_logger_) {
......
...@@ -126,18 +126,19 @@ class Logger : public CodeEventListener { ...@@ -126,18 +126,19 @@ class Logger : public CodeEventListener {
// Acquires resources for logging if the right flags are set. // Acquires resources for logging if the right flags are set.
bool SetUp(Isolate* isolate); bool SetUp(Isolate* isolate);
// Frees resources acquired in SetUp.
// When a temporary file is used for the log, returns its stream descriptor,
// leaving the file open.
V8_EXPORT_PRIVATE FILE* TearDownAndGetLogFile();
// Sets the current code event handler. // Sets the current code event handler.
void SetCodeEventHandler(uint32_t options, JitCodeEventHandler event_handler); void SetCodeEventHandler(uint32_t options, JitCodeEventHandler event_handler);
sampler::Sampler* sampler(); sampler::Sampler* sampler();
V8_EXPORT_PRIVATE std::string file_name() const;
V8_EXPORT_PRIVATE void StopProfilerThread(); V8_EXPORT_PRIVATE void StopProfilerThread();
// Frees resources acquired in SetUp.
// When a temporary file is used for the log, returns its stream descriptor,
// leaving the file open.
V8_EXPORT_PRIVATE FILE* TearDown();
// Emits an event with a string value -> (name, value). // Emits an event with a string value -> (name, value).
V8_EXPORT_PRIVATE void StringEvent(const char* name, const char* value); V8_EXPORT_PRIVATE void StringEvent(const char* name, const char* value);
......
...@@ -83,8 +83,8 @@ class ScopedLoggerInitializer { ...@@ -83,8 +83,8 @@ class ScopedLoggerInitializer {
~ScopedLoggerInitializer() { ~ScopedLoggerInitializer() {
env_->Exit(); env_->Exit();
logger_->TearDown(); FILE* log_file = logger_->TearDownAndGetLogFile();
if (temp_file_ != nullptr) fclose(temp_file_); if (log_file != nullptr) fclose(log_file);
i::FLAG_prof = saved_prof_; i::FLAG_prof = saved_prof_;
i::FLAG_log = saved_log_; i::FLAG_log = saved_log_;
} }
...@@ -203,9 +203,8 @@ class ScopedLoggerInitializer { ...@@ -203,9 +203,8 @@ class ScopedLoggerInitializer {
private: private:
FILE* StopLoggingGetTempFile() { FILE* StopLoggingGetTempFile() {
temp_file_ = logger_->TearDown(); temp_file_ = logger_->TearDownAndGetLogFile();
CHECK(temp_file_); CHECK(temp_file_);
fflush(temp_file_);
rewind(temp_file_); rewind(temp_file_);
return temp_file_; return temp_file_;
} }
......
// Copyright 2020 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.
// Flags: --log-all --log --no-stress-opt
function testFunctionWithFunnyName(o) {
return o.a;
}
(function testLoopWithFunnyName() {
const o = {a:1};
let result = 0;
for (let i = 0; i < 1000; i++) {
result += testFunctionWithFunnyName(o);
}
})();
const log = d8.log.getAndStop();
// Check that we have a minimally working log file.
assertTrue(log.length > 0);
assertTrue(log.indexOf('v8-version') == 0);
assertTrue(log.indexOf('testFunctionWithFunnyName') >= 10);
assertTrue(log.indexOf('testLoopWithFunnyName') >= 10);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment