Skip to content

Commit

Permalink
src: print more debug info when LLNODE_DEBUG=true
Browse files Browse the repository at this point in the history
PR-URL: #151
Refs: nodejs/post-mortem#50
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
  • Loading branch information
joyeecheung committed Dec 19, 2017
1 parent 182e764 commit 4846299
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 54 deletions.
35 changes: 28 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,32 +1,53 @@
TEST_LLDB_BINARY ?= $(shell which lldb-3.9)

.PHONY: all
all:
@echo "Please take a look at README.md"

.PHONY: install-osx
install-osx:
mkdir -p ~/Library/Application\ Support/LLDB/PlugIns/
cp -rf ./out/Release/llnode.dylib \
~/Library/Application\ Support/LLDB/PlugIns/

.PHONY: uninstall-osx
uninstall-osx:
rm ~/Library/Application\ Support/LLDB/PlugIns/llnode.dylib

.PHONY: install-linux
install-linux:
mkdir -p /usr/lib/lldb/plugins
cp -rf ./out/Release/lib.target/llnode.so /usr/lib/lldb/plugins

.PHONY: uninstall-linux
uninstall-linux:
rm /usr/lib/lldb/plugins/llnode.so

.PHONY: format
format:
clang-format -i src/*

configure: scripts/configure.js
# This depends on the system setting e.g. $PATH so can't actually be skipped
.PHONY: configure
configure:
node scripts/configure.js
./gyp_llnode

.PHONY: plugin
plugin: configure
./gyp_llnode
$(MAKE) -C out/

_travis: plugin
TEST_LLDB_BINARY=`which lldb-3.9` TEST_LLNODE_DEBUG=true npm test

.PHONY: all
node scripts/cleanup.js

.PHONY: _travis
_travis:
TEST_LLDB_BINARY="$(TEST_LLDB_BINARY)" \
TEST_LLNODE_DEBUG=true \
LLNODE_DEBUG=true \
npm test

.PHONY: clean
clean:
$(RM) -r out
$(RM) options.gypi
$(RM) lldb
$(RM) llnode.so llnode.dylib
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"url": "git+ssh://git@github.com/nodejs/llnode.git"
},
"files": [
"Makefile",
"llnode.gyp.json",
"gyp_llnode",
"common.gypi",
Expand Down
13 changes: 13 additions & 0 deletions src/llnode.cc
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,24 @@ bool ListCmd::DoExecute(SBDebugger d, char** cmd,
return true;
}


void InitDebugMode() {
bool is_debug_mode = false;
char* var = getenv("LLNODE_DEBUG");
if (var != nullptr && strlen(var) != 0) {
is_debug_mode = true;
}

v8::Error::SetDebugMode(is_debug_mode);
}

} // namespace llnode

namespace lldb {

bool PluginInitialize(SBDebugger d) {
llnode::InitDebugMode();

SBCommandInterpreter interpreter = d.GetCommandInterpreter();

SBCommand v8 = interpreter.AddMultiwordCommand("v8", "Node.js helpers");
Expand Down
33 changes: 17 additions & 16 deletions src/llv8-constants.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stdlib.h>
#include <string.h>

#include <cinttypes>
#include <string>

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

static std::string kConstantPrefix = "v8dbg_";

static bool IsDebugMode() {
char* var = getenv("LLNODE_DEBUG");
if (var == nullptr) return false;

return strlen(var) != 0;
}


void Module::Assign(SBTarget target, Common* common) {
loaded_ = false;
target_ = target;
Expand All @@ -46,20 +39,20 @@ static int64_t LookupConstant(SBTarget target, const char* name, int64_t def,

SBSymbolContextList context_list = target.FindSymbols(name);
if (!context_list.IsValid() || context_list.GetSize() == 0) {
err = Error::Failure("Failed to find symbol");
err = Error::Failure("Failed to find symbol %s", name);
return res;
}

SBSymbolContext context = context_list.GetContextAtIndex(0);
SBSymbol symbol = context.GetSymbol();
if (!symbol.IsValid()) {
err = Error::Failure("Failed to fetch symbol");
err = Error::Failure("Failed to fetch symbol %s", name);
return res;
}

SBAddress start = symbol.GetStartAddress();
SBAddress end = symbol.GetEndAddress();
size_t size = end.GetOffset() - start.GetOffset();
uint32_t size = end.GetOffset() - start.GetOffset();

SBError sberr;

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

if (sberr.Fail())
err = Error::Failure("Failed to load symbol");
err = Error::Failure("Failed to load symbol %s", name);
else
err = Error::Ok();

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

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

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

return v;
}
Expand Down
8 changes: 6 additions & 2 deletions src/llv8-inl.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef SRC_LLV8_INL_H_
#define SRC_LLV8_INL_H_

#include <cinttypes>
#include "llv8.h"

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

T res = T(this, ptr);
if (!res.Check()) {
err = Error::Failure("Invalid value");
// TODO(joyeecheung): use Error::Failure() to report information when
// there is less noise from here.
err = Error(true, "Invalid value");
return T();
}

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

Expand Down

0 comments on commit 4846299

Please sign in to comment.