Skip to content

Commit 4846299

Browse files
authored
src: print more debug info when LLNODE_DEBUG=true
PR-URL: #151 Refs: nodejs/post-mortem#50 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent 182e764 commit 4846299

7 files changed

Lines changed: 167 additions & 54 deletions

File tree

Makefile

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,53 @@
1+
TEST_LLDB_BINARY ?= $(shell which lldb-3.9)
2+
3+
.PHONY: all
14
all:
25
@echo "Please take a look at README.md"
36

7+
.PHONY: install-osx
48
install-osx:
59
mkdir -p ~/Library/Application\ Support/LLDB/PlugIns/
610
cp -rf ./out/Release/llnode.dylib \
711
~/Library/Application\ Support/LLDB/PlugIns/
812

13+
.PHONY: uninstall-osx
914
uninstall-osx:
1015
rm ~/Library/Application\ Support/LLDB/PlugIns/llnode.dylib
1116

17+
.PHONY: install-linux
1218
install-linux:
1319
mkdir -p /usr/lib/lldb/plugins
1420
cp -rf ./out/Release/lib.target/llnode.so /usr/lib/lldb/plugins
1521

22+
.PHONY: uninstall-linux
1623
uninstall-linux:
1724
rm /usr/lib/lldb/plugins/llnode.so
1825

26+
.PHONY: format
1927
format:
2028
clang-format -i src/*
2129

22-
configure: scripts/configure.js
30+
# This depends on the system setting e.g. $PATH so can't actually be skipped
31+
.PHONY: configure
32+
configure:
2333
node scripts/configure.js
34+
./gyp_llnode
2435

36+
.PHONY: plugin
2537
plugin: configure
26-
./gyp_llnode
2738
$(MAKE) -C out/
28-
29-
_travis: plugin
30-
TEST_LLDB_BINARY=`which lldb-3.9` TEST_LLNODE_DEBUG=true npm test
31-
32-
.PHONY: all
39+
node scripts/cleanup.js
40+
41+
.PHONY: _travis
42+
_travis:
43+
TEST_LLDB_BINARY="$(TEST_LLDB_BINARY)" \
44+
TEST_LLNODE_DEBUG=true \
45+
LLNODE_DEBUG=true \
46+
npm test
47+
48+
.PHONY: clean
49+
clean:
50+
$(RM) -r out
51+
$(RM) options.gypi
52+
$(RM) lldb
53+
$(RM) llnode.so llnode.dylib

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"url": "git+ssh://git@github.com/nodejs/llnode.git"
2222
},
2323
"files": [
24+
"Makefile",
2425
"llnode.gyp.json",
2526
"gyp_llnode",
2627
"common.gypi",

src/llnode.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,11 +299,24 @@ bool ListCmd::DoExecute(SBDebugger d, char** cmd,
299299
return true;
300300
}
301301

302+
303+
void InitDebugMode() {
304+
bool is_debug_mode = false;
305+
char* var = getenv("LLNODE_DEBUG");
306+
if (var != nullptr && strlen(var) != 0) {
307+
is_debug_mode = true;
308+
}
309+
310+
v8::Error::SetDebugMode(is_debug_mode);
311+
}
312+
302313
} // namespace llnode
303314

304315
namespace lldb {
305316

306317
bool PluginInitialize(SBDebugger d) {
318+
llnode::InitDebugMode();
319+
307320
SBCommandInterpreter interpreter = d.GetCommandInterpreter();
308321

309322
SBCommand v8 = interpreter.AddMultiwordCommand("v8", "Node.js helpers");

src/llv8-constants.cc

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stdlib.h>
22
#include <string.h>
33

4+
#include <cinttypes>
45
#include <string>
56

67
#include <lldb/API/SBExpressionOptions.h>
@@ -23,14 +24,6 @@ using lldb::addr_t;
2324

2425
static std::string kConstantPrefix = "v8dbg_";
2526

26-
static bool IsDebugMode() {
27-
char* var = getenv("LLNODE_DEBUG");
28-
if (var == nullptr) return false;
29-
30-
return strlen(var) != 0;
31-
}
32-
33-
3427
void Module::Assign(SBTarget target, Common* common) {
3528
loaded_ = false;
3629
target_ = target;
@@ -46,20 +39,20 @@ static int64_t LookupConstant(SBTarget target, const char* name, int64_t def,
4639

4740
SBSymbolContextList context_list = target.FindSymbols(name);
4841
if (!context_list.IsValid() || context_list.GetSize() == 0) {
49-
err = Error::Failure("Failed to find symbol");
42+
err = Error::Failure("Failed to find symbol %s", name);
5043
return res;
5144
}
5245

5346
SBSymbolContext context = context_list.GetContextAtIndex(0);
5447
SBSymbol symbol = context.GetSymbol();
5548
if (!symbol.IsValid()) {
56-
err = Error::Failure("Failed to fetch symbol");
49+
err = Error::Failure("Failed to fetch symbol %s", name);
5750
return res;
5851
}
5952

6053
SBAddress start = symbol.GetStartAddress();
6154
SBAddress end = symbol.GetEndAddress();
62-
size_t size = end.GetOffset() - start.GetOffset();
55+
uint32_t size = end.GetOffset() - start.GetOffset();
6356

6457
SBError sberr;
6558

@@ -79,12 +72,13 @@ static int64_t LookupConstant(SBTarget target, const char* name, int64_t def,
7972
int8_t tmp = process.ReadUnsignedFromMemory(addr, size, sberr);
8073
res = static_cast<int64_t>(tmp);
8174
} else {
82-
err = Error::Failure("Unexpected symbol size");
75+
err = Error::Failure("Unexpected symbol size %" PRIu32 " of symbol %s",
76+
size, name);
8377
return res;
8478
}
8579

8680
if (sberr.Fail())
87-
err = Error::Failure("Failed to load symbol");
81+
err = Error::Failure("Failed to load symbol %s", name);
8882
else
8983
err = Error::Ok();
9084

@@ -95,7 +89,9 @@ static int64_t LookupConstant(SBTarget target, const char* name, int64_t def,
9589
int64_t Module::LoadRawConstant(const char* name, int64_t def) {
9690
Error err;
9791
int64_t v = LookupConstant(target_, name, def, err);
98-
if (err.Fail() && IsDebugMode()) fprintf(stderr, "Failed to load %s\n", name);
92+
if (err.Fail()) {
93+
Error::PrintInDebugMode("Failed to load raw constant %s", name);
94+
}
9995

10096
return v;
10197
}
@@ -105,7 +101,9 @@ int64_t Module::LoadConstant(const char* name, int64_t def) {
105101
Error err;
106102
int64_t v =
107103
LookupConstant(target_, (kConstantPrefix + name).c_str(), def, err);
108-
if (err.Fail() && IsDebugMode()) fprintf(stderr, "Failed to load %s\n", name);
104+
if (err.Fail()) {
105+
Error::PrintInDebugMode("Failed to load constant %s", name);
106+
}
109107

110108
return v;
111109
}
@@ -118,7 +116,10 @@ int64_t Module::LoadConstant(const char* name, const char* fallback,
118116
LookupConstant(target_, (kConstantPrefix + name).c_str(), def, err);
119117
if (err.Fail())
120118
v = LookupConstant(target_, (kConstantPrefix + fallback).c_str(), def, err);
121-
if (err.Fail() && IsDebugMode()) fprintf(stderr, "Failed to load %s\n", name);
119+
if (err.Fail()) {
120+
Error::PrintInDebugMode("Failed to load constant %s, fallback %s", name,
121+
fallback);
122+
}
122123

123124
return v;
124125
}

src/llv8-inl.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef SRC_LLV8_INL_H_
22
#define SRC_LLV8_INL_H_
33

4+
#include <cinttypes>
45
#include "llv8.h"
56

67
namespace llnode {
@@ -19,7 +20,9 @@ inline T LLV8::LoadValue(int64_t addr, Error& err) {
1920

2021
T res = T(this, ptr);
2122
if (!res.Check()) {
22-
err = Error::Failure("Invalid value");
23+
// TODO(joyeecheung): use Error::Failure() to report information when
24+
// there is less noise from here.
25+
err = Error(true, "Invalid value");
2326
return T();
2427
}
2528

@@ -63,7 +66,8 @@ inline T HeapObject::LoadFieldValue(int64_t off, Error& err) {
6366
T res = v8()->LoadValue<T>(LeaField(off), err);
6467
if (err.Fail()) return T();
6568
if (!res.Check()) {
66-
err = Error::Failure("Invalid value");
69+
err = Error::Failure("Invalid field value %s at 0x%016" PRIx64,
70+
T::ClassName(), off);
6771
return T();
6872
}
6973

0 commit comments

Comments
 (0)