Skip to content

Commit

Permalink
src: implement JavaScript API
Browse files Browse the repository at this point in the history
Co-authored-by: Richard Chamberlain <[email protected]>
PR-URL: #206
Refs: #14
Reviewed-By: Matheus Marchini <[email protected]>
  • Loading branch information
joyeecheung and rnchamberlain committed Jul 6, 2018
1 parent c907d4e commit c00c41e
Show file tree
Hide file tree
Showing 11 changed files with 766 additions and 19 deletions.
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ all:
@echo "Please take a look at README.md"

.PHONY: install-osx
install-osx:
install-osx: plugin
mkdir -p ~/Library/Application\ Support/LLDB/PlugIns/
cp -rf ./llnode.dylib \
~/Library/Application\ Support/LLDB/PlugIns/
Expand All @@ -15,7 +15,7 @@ uninstall-osx:
rm ~/Library/Application\ Support/LLDB/PlugIns/llnode.dylib

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

Expand All @@ -37,6 +37,10 @@ plugin: configure
node-gyp rebuild
node scripts/cleanup.js

.PHONY: addon
addon: configure
node-gyp rebuild

.PHONY: _travis
_travis:
TEST_LLDB_BINARY="$(TEST_LLDB_BINARY)" \
Expand Down
55 changes: 52 additions & 3 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"variables": {
# gyp does not appear to let you test for undefined variables, so define
# lldb_lib_dir as empty so we can test it later.
"lldb_lib_dir%": ""
"lldb_lib_dir%": "",
"lldb_lib_so%": ""
},

"target_defaults": {
Expand Down Expand Up @@ -54,7 +55,6 @@
[ "OS == 'win'", {
"sources": [
"<(lldb_include_dir)/../source/Host/common/GetOptInc.cpp",
"windows/llnode.def",
],
"include_dirs": [
"windows/include",
Expand All @@ -77,6 +77,55 @@
"src/llv8-constants.cc",
"src/llscan.cc",
"src/node-constants.cc",
],
"conditions": [
[ "OS == 'win'", {
"sources": [
"windows/llnode.def",
],
}]
]
}, {
"target_name": "addon",
"type": "loadable_module",
"include_dirs": [
"<!@(node -p \"require('node-addon-api').include\")"
],
"defines": [ "NAPI_DISABLE_CPP_EXCEPTIONS" ],
"sources": [
"src/addon.cc",
"src/llnode_module.cc",
"src/llnode_api.cc",
"src/constants.cc",
"src/error.cc",
"src/llv8.cc",
"src/llv8-constants.cc",
"src/llscan.cc",
"src/node-constants.cc",
],
"cflags!": [ "-fno-exceptions" ],
"cflags_cc!": [ "-fno-exceptions" ],
"conditions": [
[ "OS=='linux' or OS=='freebsd'", {
"conditions": [
# If we could not locate the lib dir, then we will have to search
# from the global search paths during linking and at runtime
[ "lldb_lib_dir != ''", {
"ldflags": [
"-L<(lldb_lib_dir)",
"-Wl,-rpath,<(lldb_lib_dir)"
]
}],
# If we cannot find a non-versioned library liblldb(-x.y).so,
# then we will have to link to the versioned library
# liblldb(-x.y).so.z loaded by the detected lldb executable
[ "lldb_lib_so != ''", {
"libraries": ["<(lldb_lib_so)"]
}, {
"libraries": ["-l<(lldb_lib)"]
}]
]
}]
]
}],
}]
}
28 changes: 28 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

const {
fromCoredump,
LLNodeHeapType,
nextInstance
} = require('bindings')('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
}
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "llnode",
"version": "1.7.1",
"description": "Node.js plugin for LLDB",
"main": "no-entry-sorry.js",
"main": "index.js",
"directories": {
"test": "test"
},
Expand All @@ -24,7 +24,8 @@
"binding.gyp",
"llnode.gypi",
"src/",
"scripts/"
"scripts/",
"index.js"
],
"keywords": [
"llnode",
Expand All @@ -40,5 +41,9 @@
"homepage": "https://github.com/nodejs/llnode#readme",
"devDependencies": {
"tape": "^4.4.0"
},
"dependencies": {
"bindings": "^1.3.0",
"node-addon-api": "^1.1.0"
}
}
3 changes: 3 additions & 0 deletions scripts/configure.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ function configureInstallation(osName, buildDir) {
if (installation.libDir) {
config.variables['lldb_lib_dir%'] = installation.libDir;
}
if (installation.libPath) {
config.variables['lldb_lib_so%'] = installation.libPath;
}
config.variables['lldb_lib%'] = installation.libName;
} else if (osName === 'FreeBSD') {
installation = require('./freebsd').getLldbInstallation();
Expand Down
29 changes: 17 additions & 12 deletions scripts/linux.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function getIncludeDir(llvmConfig) {
* undefined, the shared library will be searched from the global search paths
* @param {string} lldbExe Path to the corresponding lldb executable
* @param {string|undefined} llvmConfig Path to llvm-config, if it's installed
* @returns {{dir:string, name:string}}
* @returns {{dir:string|undefined, name:string, so: string|undefined}}
*/
function getLib(lldbExe, llvmConfig) {
// First look for the libraries in the directory returned by
Expand All @@ -95,7 +95,7 @@ function getLib(lldbExe, llvmConfig) {
llvmConfig, ['--libdir']
).toString().trim();
if (fs.existsSync(path.join(libDir, 'liblldb.so'))) {
return { dir: libDir, name: 'lldb' };
return { dir: libDir, name: 'lldb', so: undefined };
}
}

Expand All @@ -106,13 +106,13 @@ function getLib(lldbExe, llvmConfig) {
'ldd', [lldbExe]
).toString().trim();
} catch (err) {
return { dir: undefined, name: 'lldb' };
return { dir: undefined, name: 'lldb', so: undefined };
}

const lib = libs.split(/\s/).find(
line => line.includes('liblldb') && line.startsWith('/'));
if (!lib) {
return { dir: undefined, name: 'lldb' };
return { dir: undefined, name: 'lldb', so: undefined };
}

console.log(`From ldd: ${lldbExe} loads ${lib}`);
Expand All @@ -121,20 +121,22 @@ function getLib(lldbExe, llvmConfig) {
// On Ubuntu the libraries are suffixed and installed globally
const libName = path.basename(lib).match(/lib(lldb.*?)\.so/)[1];

// TODO(joyeecheung): on RedHat there might not be a non-versioned liblldb.so
// On RedHat there might not be a non-versioned liblldb.so
// in the system. It's fine in the case of plugins since the lldb executable
// will load the library before loading the plugin, but we will have to link
// to the versioned library file for addons.
if (!fs.existsSync(path.join(libDir, `lib${libName}.so`))) {
if (fs.existsSync(path.join(libDir, `lib${libName}.so`))) {
return {
dir: undefined,
name: libName
dir: libDir,
name: libName,
so: undefined
};
}

return {
dir: libDir,
name: libName
dir: undefined,
name: libName,
so: lib
};
}

Expand Down Expand Up @@ -181,7 +183,9 @@ function getLldbInstallation() {
const lib = getLib(lldbExe, llvmConfig);
if (!lib.dir) {
console.log(`Could not find non-versioned lib${lib.name}.so in the system`);
console.log(`Symbols will be resolved by the lldb executable at runtime`);
console.log('Plugin symbols will be resolved by the lldb executable ' +
'at runtime');
console.log(`Addon will be linked to ${lib.so}`);
} else {
console.log(`Found lib${lib.name}.so in ${lib.dir}`);
}
Expand All @@ -191,7 +195,8 @@ function getLldbInstallation() {
version: lldbVersion,
includeDir: includeDir,
libDir: lib.dir,
libName: lib.name
libName: lib.name,
libPath: lib.so
};
}

Expand Down
14 changes: 14 additions & 0 deletions src/addon.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "llnode_module.h"
#include "napi.h"

namespace llnode {

Napi::Object InitAll(Napi::Env env, Napi::Object exports) {
Napi::Object new_exports = LLNode::Init(env, exports);
return LLNodeHeapType::Init(env, new_exports);
// return new_exports;
}

NODE_API_MODULE(addon, InitAll)

} // namespace llnode
Loading

0 comments on commit c00c41e

Please sign in to comment.