From 937fb86a4a93e38bdaabc29121a9b2d635f88cd5 Mon Sep 17 00:00:00 2001 From: Matheus Marchini Date: Wed, 18 Sep 2019 11:37:26 -0700 Subject: [PATCH] src: improve messages on PrintInDebugMode Add function, file and line information to debug messages. --- src/constants.cc | 12 +++++------- src/error.cc | 10 +++++++--- src/error.h | 9 +++++++-- src/llnode.cc | 8 +++----- src/llscan.cc | 2 +- src/llv8-inl.h | 4 ++-- src/llv8.cc | 14 ++++++-------- src/printer.cc | 5 ++--- 8 files changed, 33 insertions(+), 31 deletions(-) diff --git a/src/constants.cc b/src/constants.cc index 782f0fd6..09611dd3 100644 --- a/src/constants.cc +++ b/src/constants.cc @@ -79,8 +79,8 @@ int64_t Constants::LoadRawConstant(const char* name, int64_t def) { Error err; int64_t v = Constants::LookupConstant(target_, name, def, err); if (err.Fail()) { - Error::PrintInDebugMode( - "Failed to load raw constant %s, default to %" PRId64, name, def); + PRINT_DEBUG("Failed to load raw constant %s, default to %" PRId64, name, + def); } return v; @@ -96,8 +96,7 @@ int64_t Constants::LoadConstant(const char* name, int64_t def) { Error err; int64_t v = LoadConstant(name, err, def); if (err.Fail()) { - Error::PrintInDebugMode("Failed to load constant %s, default to %" PRId64, - name, def); + PRINT_DEBUG("Failed to load constant %s, default to %" PRId64, name, def); } return v; @@ -109,9 +108,8 @@ int64_t Constants::LoadConstant(const char* name, const char* fallback, int64_t v = LoadConstant(name, err, def); if (err.Fail()) v = LoadConstant(fallback, err, def); if (err.Fail()) { - Error::PrintInDebugMode( - "Failed to load constant %s, fallback %s, default to %" PRId64, name, - fallback, def); + PRINT_DEBUG("Failed to load constant %s, fallback %s, default to %" PRId64, + name, fallback, def); } return v; diff --git a/src/error.cc b/src/error.cc index 463b5cbc..7764f5d4 100644 --- a/src/error.cc +++ b/src/error.cc @@ -16,12 +16,14 @@ Error::Error(bool failed, const char* format, ...) { } -void Error::PrintInDebugMode(const char* format, ...) { +void Error::PrintInDebugMode(const char* file, int line, const char* funcname, + const char* format, ...) { if (!is_debug_mode) { return; } char fmt[kMaxMessageLength]; - snprintf(fmt, sizeof(fmt), "[llv8] %s\n", format); + snprintf(fmt, sizeof(fmt), "[llnode][%s %s:%lld] %s\n", funcname, file, line, + format); va_list arglist; va_start(arglist, format); vfprintf(stderr, fmt, arglist); @@ -30,7 +32,9 @@ void Error::PrintInDebugMode(const char* format, ...) { Error Error::Failure(std::string msg) { - PrintInDebugMode("%s", msg.c_str()); + // TODO(mmarchini): The file and function information here won't be relevant. + // But then again, maybe we should rethink Error::Failure. + PRINT_DEBUG("%s", msg.c_str()); return Error(true, msg); } diff --git a/src/error.h b/src/error.h index 73018c9b..2063e7ac 100644 --- a/src/error.h +++ b/src/error.h @@ -2,6 +2,10 @@ #define SRC_ERROR_H_ #include +#include + +#define PRINT_DEBUG(format, ...) \ + Error::PrintInDebugMode(__FILE__, __LINE__, __func__, format, ##__VA_ARGS__) namespace llnode { @@ -16,8 +20,9 @@ class Error { static Error Failure(std::string msg); static Error Failure(const char* format, ...) __attribute__((format(printf, 1, 2))); - static void PrintInDebugMode(const char* format, ...) - __attribute__((format(printf, 1, 2))); + static void PrintInDebugMode(const char* file, int line, const char* funcname, + const char* format, ...) + __attribute__((format(printf, 4, 5))); inline bool Success() const { return !Fail(); } inline bool Fail() const { return failed_; } diff --git a/src/llnode.cc b/src/llnode.cc index f69aa09a..cc8c4b59 100644 --- a/src/llnode.cc +++ b/src/llnode.cc @@ -79,7 +79,7 @@ bool BacktraceCmd::DoExecute(SBDebugger d, char** cmd, res.c_str()); continue; } else { - Error::PrintInDebugMode("%s", err.GetMessage()); + PRINT_DEBUG("%s", err.GetMessage()); } } @@ -352,8 +352,7 @@ std::string GetActiveHandlesCmd::GetResultMessage(node::Environment* env, Printer printer(llv8(), printer_options); std::string res = printer.Stringify(v8_object, err); if (err.Fail()) { - Error::PrintInDebugMode("Failed to load object at address %" PRIx64, - raw_object); + PRINT_DEBUG("Failed to load object at address %" PRIx64, raw_object); break; } @@ -385,8 +384,7 @@ std::string GetActiveRequestsCmd::GetResultMessage(node::Environment* env, Printer printer(llv8(), printer_options); std::string res = printer.Stringify(v8_object, err); if (err.Fail()) { - Error::PrintInDebugMode("Failed to load object at address %" PRIx64, - raw_object); + PRINT_DEBUG("Failed to load object at address %" PRIx64, raw_object); break; } diff --git a/src/llscan.cc b/src/llscan.cc index 6b56b940..09de5045 100644 --- a/src/llscan.cc +++ b/src/llscan.cc @@ -802,7 +802,7 @@ void FindReferencesCmd::ReferenceScanner::PrintContextRefs( if (err.Success()) name = maybe_name; else - Error::PrintInDebugMode( + PRINT_DEBUG( "Couldn't get the variable name for 0x%" PRIx64 " in context 0x%" PRIx64, search_value_.raw(), c.raw()); diff --git a/src/llv8-inl.h b/src/llv8-inl.h index 1206567b..038ef080 100644 --- a/src/llv8-inl.h +++ b/src/llv8-inl.h @@ -406,7 +406,7 @@ Value SharedFunctionInfo::GetInferredName(Error& err) { err = Error::Ok(); Value maybe_uncompiled_data = function_data(err); if (!maybe_uncompiled_data.IsUncompiledData(err)) { - Error::PrintInDebugMode("Couldn't get UncompiledData"); + PRINT_DEBUG("Couldn't get UncompiledData"); return Value(); } @@ -430,7 +430,7 @@ Script SharedFunctionInfo::GetScript(Error& err) { HeapObject maybe_script = script_or_debug_info(err); if (maybe_script.IsScript(err)) return maybe_script; - Error::PrintInDebugMode("Couldn't get Script in SharedFunctionInfo"); + PRINT_DEBUG("Couldn't get Script in SharedFunctionInfo"); return Script(); } diff --git a/src/llv8.cc b/src/llv8.cc index 27bdacae..9ae66f6c 100644 --- a/src/llv8.cc +++ b/src/llv8.cc @@ -705,7 +705,7 @@ std::string JSDate::ToString(Error& err) { return s; } - Error::PrintInDebugMode("JSDate is not a Smi neither a HeapNumber"); + PRINT_DEBUG("JSDate is not a Smi neither a HeapNumber"); return ""; } @@ -1236,21 +1236,20 @@ JSArray JSError::GetFrameArray(Error& err) { v8::Value maybe_stack = GetProperty(stack_trace_property(), err); if (err.Fail() || maybe_stack.raw() == -1) { - Error::PrintInDebugMode( - "Couldn't find a symbol property in the Error object."); + PRINT_DEBUG("Couldn't find a symbol property in the Error object."); return JSArray(); } int64_t type = v8::HeapObject(maybe_stack).GetType(err); if (err.Fail()) { - Error::PrintInDebugMode("Symbol property references an invalid object."); + PRINT_DEBUG("Symbol property references an invalid object."); return JSArray(); } // NOTE (mmarchini): The stack is stored as a JSArray if (type != v8()->types()->kJSArrayType) { - Error::PrintInDebugMode("Symbol property doesn't have the right type."); + PRINT_DEBUG("Symbol property doesn't have the right type."); return JSArray(); } @@ -1275,8 +1274,7 @@ StackTrace::StackTrace(JSArray frame_array, Error& err) v8::Value maybe_stack_len = frame_array.GetArrayElement(0, err); if (err.Fail()) { - Error::PrintInDebugMode( - "Couldn't get the first element from the stack array"); + PRINT_DEBUG("Couldn't get the first element from the stack array"); return; } @@ -1291,7 +1289,7 @@ StackTrace::StackTrace(JSArray frame_array, Error& err) multiplier_ = 4; if ((len_ != 0) || ((frame_array_.GetArrayLength(err) - 1) % multiplier_ != 0)) { - Error::PrintInDebugMode( + PRINT_DEBUG( "JSArray doesn't look like a Stack Frames array. stack_len: %d " "array_len: %ld", len_, frame_array_.GetArrayLength(err)); diff --git a/src/printer.cc b/src/printer.cc index 422ded37..bdc970a9 100644 --- a/src/printer.cc +++ b/src/printer.cc @@ -756,9 +756,8 @@ std::string Printer::Stringify(v8::HeapObject heap_object, Error& err) { return pre + Stringify(date, err); } - Error::PrintInDebugMode("Unknown HeapObject Type %" PRId64 " at 0x%016" PRIx64 - "", - type, heap_object.raw()); + PRINT_DEBUG("Unknown HeapObject Type %" PRId64 " at 0x%016" PRIx64 "", type, + heap_object.raw()); std::stringstream ss; ss << rang::fg::yellow << "" << rang::fg::reset;