Skip to content

Commit 9449d99

Browse files
committed
src: improve messages on PrintInDebugMode
Add function, file and line information to debug messages. PR-URL: #293 Reviewed-By: Joyee Cheung <[email protected]>
1 parent a501635 commit 9449d99

File tree

8 files changed

+35
-34
lines changed

8 files changed

+35
-34
lines changed

src/constants.cc

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ int64_t Constants::LoadRawConstant(const char* name, int64_t def) {
7979
Error err;
8080
int64_t v = Constants::LookupConstant(target_, name, def, err);
8181
if (err.Fail()) {
82-
Error::PrintInDebugMode(
83-
"Failed to load raw constant %s, default to %" PRId64, name, def);
82+
PRINT_DEBUG("Failed to load raw constant %s, default to %" PRId64, name,
83+
def);
8484
}
8585

8686
return v;
@@ -96,8 +96,7 @@ int64_t Constants::LoadConstant(const char* name, int64_t def) {
9696
Error err;
9797
int64_t v = LoadConstant(name, err, def);
9898
if (err.Fail()) {
99-
Error::PrintInDebugMode("Failed to load constant %s, default to %" PRId64,
100-
name, def);
99+
PRINT_DEBUG("Failed to load constant %s, default to %" PRId64, name, def);
101100
}
102101

103102
return v;
@@ -109,9 +108,8 @@ int64_t Constants::LoadConstant(const char* name, const char* fallback,
109108
int64_t v = LoadConstant(name, err, def);
110109
if (err.Fail()) v = LoadConstant(fallback, err, def);
111110
if (err.Fail()) {
112-
Error::PrintInDebugMode(
113-
"Failed to load constant %s, fallback %s, default to %" PRId64, name,
114-
fallback, def);
111+
PRINT_DEBUG("Failed to load constant %s, fallback %s, default to %" PRId64,
112+
name, fallback, def);
115113
}
116114

117115
return v;

src/error.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ Error::Error(bool failed, const char* format, ...) {
1616
}
1717

1818

19-
void Error::PrintInDebugMode(const char* format, ...) {
19+
void Error::PrintInDebugMode(const char* file, int line, const char* funcname,
20+
const char* format, ...) {
2021
if (!is_debug_mode) {
2122
return;
2223
}
2324
char fmt[kMaxMessageLength];
24-
snprintf(fmt, sizeof(fmt), "[llv8] %s\n", format);
25+
snprintf(fmt, sizeof(fmt), "[llnode][%s %s:%lld] %s\n", funcname, file, line,
26+
format);
2527
va_list arglist;
2628
va_start(arglist, format);
2729
vfprintf(stderr, fmt, arglist);
@@ -30,7 +32,9 @@ void Error::PrintInDebugMode(const char* format, ...) {
3032

3133

3234
Error Error::Failure(std::string msg) {
33-
PrintInDebugMode("%s", msg.c_str());
35+
// TODO(mmarchini): The file and function information here won't be relevant.
36+
// But then again, maybe we should rethink Error::Failure.
37+
PRINT_DEBUG("%s", msg.c_str());
3438
return Error(true, msg);
3539
}
3640

src/error.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
#define SRC_ERROR_H_
33

44
#include <string>
5+
#include <typeinfo>
6+
7+
#define PRINT_DEBUG(format, ...) \
8+
Error::PrintInDebugMode(__FILE__, __LINE__, __func__, format, ##__VA_ARGS__)
59

610
namespace llnode {
711

@@ -16,8 +20,9 @@ class Error {
1620
static Error Failure(std::string msg);
1721
static Error Failure(const char* format, ...)
1822
__attribute__((format(printf, 1, 2)));
19-
static void PrintInDebugMode(const char* format, ...)
20-
__attribute__((format(printf, 1, 2)));
23+
static void PrintInDebugMode(const char* file, int line, const char* funcname,
24+
const char* format, ...)
25+
__attribute__((format(printf, 4, 5)));
2126

2227
inline bool Success() const { return !Fail(); }
2328
inline bool Fail() const { return failed_; }

src/llnode.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ bool BacktraceCmd::DoExecute(SBDebugger d, char** cmd,
7979
res.c_str());
8080
continue;
8181
} else {
82-
Error::PrintInDebugMode("%s", err.GetMessage());
82+
PRINT_DEBUG("%s", err.GetMessage());
8383
}
8484
}
8585

@@ -352,8 +352,7 @@ std::string GetActiveHandlesCmd::GetResultMessage(node::Environment* env,
352352
Printer printer(llv8(), printer_options);
353353
std::string res = printer.Stringify(v8_object, err);
354354
if (err.Fail()) {
355-
Error::PrintInDebugMode("Failed to load object at address %" PRIx64,
356-
raw_object);
355+
PRINT_DEBUG("Failed to load object at address %" PRIx64, raw_object);
357356
break;
358357
}
359358

@@ -385,8 +384,7 @@ std::string GetActiveRequestsCmd::GetResultMessage(node::Environment* env,
385384
Printer printer(llv8(), printer_options);
386385
std::string res = printer.Stringify(v8_object, err);
387386
if (err.Fail()) {
388-
Error::PrintInDebugMode("Failed to load object at address %" PRIx64,
389-
raw_object);
387+
PRINT_DEBUG("Failed to load object at address %" PRIx64, raw_object);
390388
break;
391389
}
392390

src/llscan.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -802,10 +802,9 @@ void FindReferencesCmd::ReferenceScanner::PrintContextRefs(
802802
if (err.Success())
803803
name = maybe_name;
804804
else
805-
Error::PrintInDebugMode(
806-
"Couldn't get the variable name for 0x%" PRIx64
807-
" in context 0x%" PRIx64,
808-
search_value_.raw(), c.raw());
805+
PRINT_DEBUG("Couldn't get the variable name for 0x%" PRIx64
806+
" in context 0x%" PRIx64,
807+
search_value_.raw(), c.raw());
809808
}
810809

811810
std::stringstream ss;

src/llv8-inl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ Value SharedFunctionInfo::GetInferredName(Error& err) {
406406
err = Error::Ok();
407407
Value maybe_uncompiled_data = function_data(err);
408408
if (!maybe_uncompiled_data.IsUncompiledData(err)) {
409-
Error::PrintInDebugMode("Couldn't get UncompiledData");
409+
PRINT_DEBUG("Couldn't get UncompiledData");
410410
return Value();
411411
}
412412

@@ -430,7 +430,7 @@ Script SharedFunctionInfo::GetScript(Error& err) {
430430
HeapObject maybe_script = script_or_debug_info(err);
431431
if (maybe_script.IsScript(err)) return maybe_script;
432432

433-
Error::PrintInDebugMode("Couldn't get Script in SharedFunctionInfo");
433+
PRINT_DEBUG("Couldn't get Script in SharedFunctionInfo");
434434
return Script();
435435
}
436436

src/llv8.cc

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ std::string JSDate::ToString(Error& err) {
705705
return s;
706706
}
707707

708-
Error::PrintInDebugMode("JSDate is not a Smi neither a HeapNumber");
708+
PRINT_DEBUG("JSDate is not a Smi neither a HeapNumber");
709709
return "";
710710
}
711711

@@ -1236,21 +1236,20 @@ JSArray JSError::GetFrameArray(Error& err) {
12361236
v8::Value maybe_stack = GetProperty(stack_trace_property(), err);
12371237

12381238
if (err.Fail() || maybe_stack.raw() == -1) {
1239-
Error::PrintInDebugMode(
1240-
"Couldn't find a symbol property in the Error object.");
1239+
PRINT_DEBUG("Couldn't find a symbol property in the Error object.");
12411240
return JSArray();
12421241
}
12431242

12441243
int64_t type = v8::HeapObject(maybe_stack).GetType(err);
12451244

12461245
if (err.Fail()) {
1247-
Error::PrintInDebugMode("Symbol property references an invalid object.");
1246+
PRINT_DEBUG("Symbol property references an invalid object.");
12481247
return JSArray();
12491248
}
12501249

12511250
// NOTE (mmarchini): The stack is stored as a JSArray
12521251
if (type != v8()->types()->kJSArrayType) {
1253-
Error::PrintInDebugMode("Symbol property doesn't have the right type.");
1252+
PRINT_DEBUG("Symbol property doesn't have the right type.");
12541253
return JSArray();
12551254
}
12561255

@@ -1275,8 +1274,7 @@ StackTrace::StackTrace(JSArray frame_array, Error& err)
12751274
v8::Value maybe_stack_len = frame_array.GetArrayElement(0, err);
12761275

12771276
if (err.Fail()) {
1278-
Error::PrintInDebugMode(
1279-
"Couldn't get the first element from the stack array");
1277+
PRINT_DEBUG("Couldn't get the first element from the stack array");
12801278
return;
12811279
}
12821280

@@ -1291,7 +1289,7 @@ StackTrace::StackTrace(JSArray frame_array, Error& err)
12911289
multiplier_ = 4;
12921290
if ((len_ != 0) ||
12931291
((frame_array_.GetArrayLength(err) - 1) % multiplier_ != 0)) {
1294-
Error::PrintInDebugMode(
1292+
PRINT_DEBUG(
12951293
"JSArray doesn't look like a Stack Frames array. stack_len: %d "
12961294
"array_len: %ld",
12971295
len_, frame_array_.GetArrayLength(err));

src/printer.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -756,9 +756,8 @@ std::string Printer::Stringify(v8::HeapObject heap_object, Error& err) {
756756
return pre + Stringify(date, err);
757757
}
758758

759-
Error::PrintInDebugMode("Unknown HeapObject Type %" PRId64 " at 0x%016" PRIx64
760-
"",
761-
type, heap_object.raw());
759+
PRINT_DEBUG("Unknown HeapObject Type %" PRId64 " at 0x%016" PRIx64 "", type,
760+
heap_object.raw());
762761

763762
std::stringstream ss;
764763
ss << rang::fg::yellow << "<unknown>" << rang::fg::reset;

0 commit comments

Comments
 (0)