Skip to content

Commit

Permalink
src: implement JavaScript API
Browse files Browse the repository at this point in the history
  • Loading branch information
joyeecheung committed Nov 19, 2017
1 parent 98caafc commit 31cd764
Show file tree
Hide file tree
Showing 12 changed files with 625 additions and 9 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
*.dSYM
tools/
out/
lldb/
lldb
lldb-*
build/
out/
npm-debug.log
node_modules/
# generated by scripts/configure.js
options.gypi
.project
addon.node
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
*
!src/**
!binding.gyp
!test/**
22 changes: 19 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
all:
@echo "Please take a look at README.md"

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

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

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

Expand All @@ -24,4 +24,20 @@ _travis:
make -C out/
TEST_LLDB_BINARY=`which lldb-3.6` npm test

.PHONY: all
clean:
rm -rf out
rm -rf build
rm -rf options.gypi
rm -rf lldb
rm -rf addon.node llnode.so

addon:
node scripts/configure.js
node-gyp rebuild

plugin:
node ./scripts/configure.js
./gyp_llnode
$(MAKE) -C out/

.PHONY: all addon clean plugin format
86 changes: 85 additions & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
@@ -1,3 +1,87 @@
{
"targets": [{ "target_name": "none", "type": "none" }]
"includes": [
"common.gypi",
"options.gypi"
],

"variables": {
# gyp does not appear to let you test for undefined variables, so define
# lldb_build_dir as empty so we can test it later.
# this variable is used when we don't link with frameworks on Macos
"lldb_build_dir%": ""
},

"targets": [{
"target_name": "addon",
"sources": [
"src/addon.cc",
"src/llnode_module.cc",
"src/llnode_api.cc",
"src/llv8.cc",
"src/llv8-constants.cc",
"src/llscan.cc"
],
"include_dirs": [
".",
"<(lldb_dir)/include",
"<!(node -e \"require('nan')\")"
],
"conditions": [
[ "OS == 'mac'", {
"conditions": [
[ "lldb_build_dir == ''", {
"variables": {
"mac_shared_frameworks": "/Applications/Xcode.app/Contents/SharedFrameworks",
},
"xcode_settings": {
"OTHER_LDFLAGS": [
"-F<(mac_shared_frameworks)",
"-Wl,-rpath,<(mac_shared_frameworks)",
"-framework LLDB",
],
},
},
# lldb_builddir != ""
{
"xcode_settings": {
"OTHER_LDFLAGS": [
"-Wl,-rpath,<(lldb_build_dir)/lib",
"-L<(lldb_build_dir)/lib",
"-l<(lldb_lib)",
],
},
}],
],
}],
[ "OS=='linux'", {
"conditions": [
[ "lldb_build_dir == ''", {
"libraries": ["<(lldb_dir)/lib/liblldb.so.1" ],
"ldflags": [
"-Wl,-rpath,<(lldb_dir)/lib",
"-L<(lldb_dir)/lib",
"-l<(lldb_lib)",
]
},
# lldb_builddir != ""
{
"libraries": ["<(lldb_build_dir)/lib/liblldb.so.1" ],
"ldflags": [
"-Wl,-rpath,<(lldb_build_dir)/lib",
"-L<(lldb_build_dir)/lib",
"-l<(lldb_lib)",
]
}
],
]
}]
]
},
{
"target_name": "install",
"type":"none",
"dependencies" : [ "addon" ]
},
],
}

4 changes: 2 additions & 2 deletions common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"msvs_multi_core_compile": "0", # we do enable multicore compiles, but not using the V8 way
"gcc_version%": "unknown",
"clang%": 1,
"lldb_dir%": "lldb",
"lldb_lib%": "lldb",
"lldb_dir%": "lldb", # location of the lldb installation
"lldb_lib%": "lldb", # name of the -l library to link
"conditions": [
["GENERATOR == 'ninja'", {
"OBJ_DIR": "<(PRODUCT_DIR)/obj",
Expand Down
30 changes: 30 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const addon = require('bindings')('addon');
const {
fromCoredump,
LLNodeHeapType,
nextInstance
} = addon;

function *next() {
let instance;
while (instance = nextInstance(this)) {
yield instance;
}
}

Object.defineProperty(LLNodeHeapType.prototype, 'instances', {
enumerable: false,
configurable: false,
get: function() {
return {
[Symbol.iterator]: next.bind(this)
}
}
});

module.exports = {
fromCoredump
}

1 change: 1 addition & 0 deletions llnode.gyp.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"variables": {
# gyp does not appear to let you test for undefined variables, so define
# lldb_build_dir as empty so we can test it later.
# this variable is used when we don't link with frameworks on Macos
"lldb_build_dir%": ""
},

Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "llnode",
"version": "1.6.2",
"description": "Node.js plugin for LLDB",
"main": "no-entry-sorry.js",
"main": "index.js",
"directories": {
"test": "test"
},
Expand Down Expand Up @@ -42,5 +42,9 @@
"homepage": "https://github.com/nodejs/llnode#readme",
"devDependencies": {
"tape": "^4.4.0"
},
"dependencies": {
"bindings": "^1.3.0",
"nan": "^2.7.0"
}
}
54 changes: 53 additions & 1 deletion scripts/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ if (osName === 'Darwin') {
fs.writeFileSync('options.gypi', '{}', 'utf-8');
} else {
lldbInstallDir = installedDir;
setLinuxBuildDir();
}
} else if (osName === 'FreeBSD') {
lldbExe = getLldbExecutable();
Expand Down Expand Up @@ -121,6 +122,26 @@ if (process.env.npm_config_global) {
gypSubDir = 'npm/node_modules/node-gyp';
}

function setLinuxBuildDir() {
const libStat = getLinuxLib(lldbVersion);
if (!libStat) {
console.log('Could not locate the liblldb.so,' +
' addon build may fail');
} else {
const config = JSON.stringify({
variables: {
"lldb_build_dir%": libStat.buildDir,
"lldb_lib%": libStat.lib
}
}, null, 2);
const soPath = `${libStat.buildDir}/lib/lib${libStat.lib}.so`;
console.log(`The addon will be linked to ${soPath}`);
console.log('Writing config to options.gypi...');
console.log(config);
fs.writeFileSync('options.gypi', config, 'utf-8');
}
}

// npm can be in a different location than the current
// location for global installs so we need find out where the npm is
var npmLocation = child_process.execFileSync('which', ['npm']);
Expand Down Expand Up @@ -195,7 +216,9 @@ function setDarwinBuildDir() {
'--prefix'
]).toString().trim();
const options = JSON.stringify({
variables: { 'lldb_build_dir%': prefix }
variables: {
'lldb_build_dir%': prefix
}
}, null, 2);
fs.writeFileSync('options.gypi', options, 'utf-8');
console.log('Overwriting options.gypi with output from llvm-config:');
Expand Down Expand Up @@ -311,6 +334,35 @@ function getLinuxInstallDir(version) {
}
return undefined;
}
function getLinuxLib(version) {
// Get the directory which should contain the shared library and
// check if they are present.
console.log('Checking for shared libraries, version is ' + version);
try {
const libDir = child_process.execFileSync('llvm-config-' + version,
['--libdir']).toString().trim();
const soPath = path.join(libDir, `liblldb-${version}.so`);
const stat = fs.lstatSync(soPath);
if (stat.isFile() || stat.isSymbolicLink()) {
return {
buildDir: path.dirname(libDir),
lib: `lldb-${version}`
};
}
} catch (err) {
console.log(err);
// Return undefined, we will download the headers.
}
// On Redhat lib are just installed in /usr/lib
if (fs.existsSync('/usr/lib/lldblldb.so')) {
return {
buildDir: '/usr',
lib: 'lldb'
}
}

return undefined;
}

function scriptText(lldbExe) {
let lib = 'llnode.so';
Expand Down
13 changes: 13 additions & 0 deletions src/addon.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <nan.h>
#include "llnode_module.h"

namespace llnode {

NAN_MODULE_INIT(InitAll) {
LLNode::Init(target);
LLNodeHeapType::Init(target);
}

NODE_MODULE(addon, InitAll)

} // namespace llnode
Loading

0 comments on commit 31cd764

Please sign in to comment.