Skip to content

Commit dcdef03

Browse files
committed
src: implement JavaScript API
1 parent c907d4e commit dcdef03

File tree

11 files changed

+766
-19
lines changed

11 files changed

+766
-19
lines changed

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ all:
55
@echo "Please take a look at README.md"
66

77
.PHONY: install-osx
8-
install-osx:
8+
install-osx: plugin
99
mkdir -p ~/Library/Application\ Support/LLDB/PlugIns/
1010
cp -rf ./llnode.dylib \
1111
~/Library/Application\ Support/LLDB/PlugIns/
@@ -15,7 +15,7 @@ uninstall-osx:
1515
rm ~/Library/Application\ Support/LLDB/PlugIns/llnode.dylib
1616

1717
.PHONY: install-linux
18-
install-linux:
18+
install-linux: plugin
1919
mkdir -p /usr/lib/lldb/plugins
2020
cp -rf ./llnode.so /usr/lib/lldb/plugins
2121

@@ -37,6 +37,10 @@ plugin: configure
3737
node-gyp rebuild
3838
node scripts/cleanup.js
3939

40+
.PHONY: addon
41+
addon: configure
42+
node-gyp rebuild
43+
4044
.PHONY: _travis
4145
_travis:
4246
TEST_LLDB_BINARY="$(TEST_LLDB_BINARY)" \

binding.gyp

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"variables": {
88
# gyp does not appear to let you test for undefined variables, so define
99
# lldb_lib_dir as empty so we can test it later.
10-
"lldb_lib_dir%": ""
10+
"lldb_lib_dir%": "",
11+
"lldb_lib_so%": ""
1112
},
1213

1314
"target_defaults": {
@@ -54,7 +55,6 @@
5455
[ "OS == 'win'", {
5556
"sources": [
5657
"<(lldb_include_dir)/../source/Host/common/GetOptInc.cpp",
57-
"windows/llnode.def",
5858
],
5959
"include_dirs": [
6060
"windows/include",
@@ -77,6 +77,55 @@
7777
"src/llv8-constants.cc",
7878
"src/llscan.cc",
7979
"src/node-constants.cc",
80+
],
81+
"conditions": [
82+
[ "OS == 'win'", {
83+
"sources": [
84+
"windows/llnode.def",
85+
],
86+
}]
87+
]
88+
}, {
89+
"target_name": "addon",
90+
"type": "loadable_module",
91+
"include_dirs": [
92+
"<!@(node -p \"require('node-addon-api').include\")"
93+
],
94+
"defines": [ "NAPI_DISABLE_CPP_EXCEPTIONS" ],
95+
"sources": [
96+
"src/addon.cc",
97+
"src/llnode_module.cc",
98+
"src/llnode_api.cc",
99+
"src/constants.cc",
100+
"src/error.cc",
101+
"src/llv8.cc",
102+
"src/llv8-constants.cc",
103+
"src/llscan.cc",
104+
"src/node-constants.cc",
105+
],
106+
"cflags!": [ "-fno-exceptions" ],
107+
"cflags_cc!": [ "-fno-exceptions" ],
108+
"conditions": [
109+
[ "OS=='linux' or OS=='freebsd'", {
110+
"conditions": [
111+
# If we could not locate the lib dir, then we will have to search
112+
# from the global search paths during linking and at runtime
113+
[ "lldb_lib_dir != ''", {
114+
"ldflags": [
115+
"-L<(lldb_lib_dir)",
116+
"-Wl,-rpath,<(lldb_lib_dir)"
117+
]
118+
}],
119+
# If we cannot find a non-versioned library liblldb(-x.y).so,
120+
# then we will have to link to the versioned library
121+
# liblldb(-x.y).so.z loaded by the detected lldb executable
122+
[ "lldb_lib_so != ''", {
123+
"libraries": ["<(lldb_lib_so)"]
124+
}, {
125+
"libraries": ["-l<(lldb_lib)"]
126+
}]
127+
]
128+
}]
80129
]
81-
}],
130+
}]
82131
}

index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
const {
4+
fromCoredump,
5+
LLNodeHeapType,
6+
nextInstance
7+
} = require('bindings')('addon');
8+
9+
function *next() {
10+
let instance;
11+
while (instance = nextInstance(this)) {
12+
yield instance;
13+
}
14+
}
15+
16+
Object.defineProperty(LLNodeHeapType.prototype, 'instances', {
17+
enumerable: false,
18+
configurable: false,
19+
get: function() {
20+
return {
21+
[Symbol.iterator]: next.bind(this)
22+
}
23+
}
24+
});
25+
26+
module.exports = {
27+
fromCoredump
28+
}

package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "llnode",
33
"version": "1.7.1",
44
"description": "Node.js plugin for LLDB",
5-
"main": "no-entry-sorry.js",
5+
"main": "index.js",
66
"directories": {
77
"test": "test"
88
},
@@ -24,7 +24,8 @@
2424
"binding.gyp",
2525
"llnode.gypi",
2626
"src/",
27-
"scripts/"
27+
"scripts/",
28+
"index.js"
2829
],
2930
"keywords": [
3031
"llnode",
@@ -40,5 +41,9 @@
4041
"homepage": "https://github.com/nodejs/llnode#readme",
4142
"devDependencies": {
4243
"tape": "^4.4.0"
44+
},
45+
"dependencies": {
46+
"bindings": "^1.3.0",
47+
"node-addon-api": "^1.1.0"
4348
}
4449
}

scripts/configure.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ function configureInstallation(osName, buildDir) {
4545
if (installation.libDir) {
4646
config.variables['lldb_lib_dir%'] = installation.libDir;
4747
}
48+
if (installation.libPath) {
49+
config.variables['lldb_lib_so%'] = installation.libPath;
50+
}
4851
config.variables['lldb_lib%'] = installation.libName;
4952
} else if (osName === 'FreeBSD') {
5053
installation = require('./freebsd').getLldbInstallation();

scripts/linux.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function getIncludeDir(llvmConfig) {
8585
* undefined, the shared library will be searched from the global search paths
8686
* @param {string} lldbExe Path to the corresponding lldb executable
8787
* @param {string|undefined} llvmConfig Path to llvm-config, if it's installed
88-
* @returns {{dir:string, name:string}}
88+
* @returns {{dir:string|undefined, name:string, so: string|undefined}}
8989
*/
9090
function getLib(lldbExe, llvmConfig) {
9191
// First look for the libraries in the directory returned by
@@ -95,7 +95,7 @@ function getLib(lldbExe, llvmConfig) {
9595
llvmConfig, ['--libdir']
9696
).toString().trim();
9797
if (fs.existsSync(path.join(libDir, 'liblldb.so'))) {
98-
return { dir: libDir, name: 'lldb' };
98+
return { dir: libDir, name: 'lldb', so: undefined };
9999
}
100100
}
101101

@@ -106,13 +106,13 @@ function getLib(lldbExe, llvmConfig) {
106106
'ldd', [lldbExe]
107107
).toString().trim();
108108
} catch (err) {
109-
return { dir: undefined, name: 'lldb' };
109+
return { dir: undefined, name: 'lldb', so: undefined };
110110
}
111111

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

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

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

135136
return {
136-
dir: libDir,
137-
name: libName
137+
dir: undefined,
138+
name: libName,
139+
so: lib
138140
};
139141
}
140142

@@ -181,7 +183,9 @@ function getLldbInstallation() {
181183
const lib = getLib(lldbExe, llvmConfig);
182184
if (!lib.dir) {
183185
console.log(`Could not find non-versioned lib${lib.name}.so in the system`);
184-
console.log(`Symbols will be resolved by the lldb executable at runtime`);
186+
console.log('Plugin symbols will be resolved by the lldb executable ' +
187+
'at runtime');
188+
console.log(`Addon will be linked to ${lib.so}`);
185189
} else {
186190
console.log(`Found lib${lib.name}.so in ${lib.dir}`);
187191
}
@@ -191,7 +195,8 @@ function getLldbInstallation() {
191195
version: lldbVersion,
192196
includeDir: includeDir,
193197
libDir: lib.dir,
194-
libName: lib.name
198+
libName: lib.name,
199+
libPath: lib.so
195200
};
196201
}
197202

src/addon.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "llnode_module.h"
2+
#include "napi.h"
3+
4+
namespace llnode {
5+
6+
Napi::Object InitAll(Napi::Env env, Napi::Object exports) {
7+
Napi::Object new_exports = LLNode::Init(env, exports);
8+
return LLNodeHeapType::Init(env, new_exports);
9+
// return new_exports;
10+
}
11+
12+
NODE_API_MODULE(addon, InitAll)
13+
14+
} // namespace llnode

0 commit comments

Comments
 (0)