Commit 262fbcbe authored by Camillo Bruni's avatar Camillo Bruni Committed by Commit Bot

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

The new helper function allows us to write tests for log parsing
without the need of first generating a log file.
This makes it easier guard against errors when the log format changes.

- add d8.log.getAndStop helper
- add basic log test
- fix test that regresses due to changed gc timing

Bug: v8:10668
Change-Id: Ie57171fa98fe90428b89c26289d55fcbf2a70615
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2403245Reviewed-by: 's avatarToon Verwaest <verwaest@chromium.org>
Commit-Queue: Toon Verwaest <verwaest@chromium.org>
Auto-Submit: Camillo Bruni <cbruni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#69987}
parent 66e4c99c
...@@ -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(
...@@ -2218,6 +2254,13 @@ Local<ObjectTemplate> Shell::CreateRealmTemplate(Isolate* isolate) { ...@@ -2218,6 +2254,13 @@ Local<ObjectTemplate> Shell::CreateRealmTemplate(Isolate* isolate) {
Local<ObjectTemplate> Shell::CreateD8Template(Isolate* isolate) { Local<ObjectTemplate> Shell::CreateD8Template(Isolate* isolate) {
Local<ObjectTemplate> d8_template = ObjectTemplate::New(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; return d8_template;
} }
......
...@@ -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(
......
...@@ -2,7 +2,8 @@ ...@@ -2,7 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// Flags: --allow-natives-syntax // Flags: --allow-natives-syntax --no-flush-bytecode
// Flags: --no-stress-flush-bytecode
// Files: test/mjsunit/code-coverage-utils.js // Files: test/mjsunit/code-coverage-utils.js
%DebugToggleBlockCoverage(true); %DebugToggleBlockCoverage(true);
......
// 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