[message] Print to a std::ostream from PrintCurrentStackTrace()

Signed-off-by: Darshan Sen <raisinten@gmail.com>
Change-Id: I51650e87261c817d6a58a34d56920b6fb8c1e281
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3112985
Reviewed-by: Camillo Bruni <cbruni@chromium.org>
Reviewed-by: Clemens Backes <clemensb@chromium.org>
Reviewed-by: Jakob Kummerow <jkummerow@chromium.org>
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Cr-Commit-Position: refs/heads/main@{#76788}
This commit is contained in:
Darshan Sen 2021-09-13 09:50:04 +05:30 committed by V8 LUCI CQ
parent e8708fe355
commit 4a02c06b53
6 changed files with 26 additions and 4 deletions

View File

@ -7,6 +7,8 @@
#include <stdio.h>
#include <ostream>
#include "v8-local-handle.h" // NOLINT(build/include_directory)
#include "v8-maybe.h" // NOLINT(build/include_directory)
#include "v8config.h" // NOLINT(build/include_directory)
@ -206,8 +208,9 @@ class V8_EXPORT Message {
bool IsSharedCrossOrigin() const;
bool IsOpaque() const;
// TODO(1245381): Print to a string instead of on a FILE.
V8_DEPRECATE_SOON("Use the version that takes a std::ostream&.")
static void PrintCurrentStackTrace(Isolate* isolate, FILE* out);
static void PrintCurrentStackTrace(Isolate* isolate, std::ostream& out);
static const int kNoLineNumberInfo = 0;
static const int kNoColumnInfo = 0;

View File

@ -7,6 +7,7 @@
#include <algorithm> // For min
#include <cmath> // For isnan.
#include <limits>
#include <sstream>
#include <string>
#include <utility> // For move
#include <vector>
@ -3094,6 +3095,14 @@ MaybeLocal<String> Message::GetSourceLine(Local<Context> context) const {
}
void Message::PrintCurrentStackTrace(Isolate* isolate, FILE* out) {
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
std::ostringstream stack_trace_stream;
i_isolate->PrintCurrentStackTrace(stack_trace_stream);
i::PrintF(out, "%s", stack_trace_stream.str().c_str());
}
void Message::PrintCurrentStackTrace(Isolate* isolate, std::ostream& out) {
i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate);
ENTER_V8_NO_SCRIPT_NO_EXCEPTION(i_isolate);
i_isolate->PrintCurrentStackTrace(out);

View File

@ -1564,7 +1564,9 @@ Handle<JSMessageObject> Isolate::CreateMessageOrAbort(
// print a user-friendly stack trace (not an internal one).
PrintF(stderr, "%s\n\nFROM\n",
MessageHandler::GetLocalizedMessage(this, message_obj).get());
PrintCurrentStackTrace(stderr);
std::ostringstream stack_trace_stream;
PrintCurrentStackTrace(stack_trace_stream);
PrintF(stderr, "%s", stack_trace_stream.str().c_str());
base::OS::Abort();
}
}
@ -2125,7 +2127,7 @@ Object Isolate::PromoteScheduledException() {
return ReThrow(thrown);
}
void Isolate::PrintCurrentStackTrace(FILE* out) {
void Isolate::PrintCurrentStackTrace(std::ostream& out) {
CaptureStackTraceOptions options;
options.limit = 0;
options.skip_mode = SKIP_NONE;

View File

@ -853,7 +853,7 @@ class V8_EXPORT_PRIVATE Isolate final : private HiddenFactory {
v8::Isolate::AbortOnUncaughtExceptionCallback callback);
enum PrintStackMode { kPrintStackConcise, kPrintStackVerbose };
void PrintCurrentStackTrace(FILE* out);
void PrintCurrentStackTrace(std::ostream& out);
void PrintStack(StringStream* accumulator,
PrintStackMode mode = kPrintStackVerbose);
void PrintStack(FILE* out, PrintStackMode mode = kPrintStackVerbose);

View File

@ -1453,6 +1453,13 @@ void String::PrintOn(FILE* file) {
}
}
void String::PrintOn(std::ostream& ostream) {
int length = this->length();
for (int i = 0; i < length; i++) {
ostream.put(Get(i));
}
}
Handle<String> SeqString::Truncate(Handle<SeqString> string, int new_length) {
if (new_length == 0) return string->GetReadOnlyRoots().empty_string_handle();

View File

@ -403,6 +403,7 @@ class String : public TorqueGeneratedString<String, Name> {
enum TrimMode { kTrim, kTrimStart, kTrimEnd };
V8_EXPORT_PRIVATE void PrintOn(FILE* out);
V8_EXPORT_PRIVATE void PrintOn(std::ostream& out);
// For use during stack traces. Performs rudimentary sanity check.
bool LooksValid();