Skip to content

Commit fbdcf01

Browse files
committed
Add debug instructions
- update README with debug instructions - add `debug.js` script under `/scripts` - update `package.json` with debug scripts
1 parent 7608be9 commit fbdcf01

File tree

3 files changed

+101
-5
lines changed

3 files changed

+101
-5
lines changed

README.md

+60-3
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,18 @@ You may also need to [follow these steps](https://github.com/nodejs/node-gyp#use
4444
When using memoryjs, the target process should match the platform architecture of the Node version running.
4545
For example if you want to target a 64 bit process, you should try and use a 64 bit version of Node.
4646

47-
You also need to recompile the library and target the platform you want. Head to the memoryjs node module directory, open up a terminal and to run the compile scripts, type:
47+
You also need to recompile the library and target the platform you want. Head to the memoryjs node module directory, open up a terminal and to run the compile scripts, type one of the following:
4848

49-
`npm run build32` if you want to target 32 bit processes
49+
```bash
50+
# will automatically compile based on the detected Node architecture
51+
npm run build
5052

51-
`npm run build64` if you want to target 64 bit processes
53+
# compile to target 32 bit processes
54+
npm run build32
55+
56+
# compile to target 64 bit processes
57+
npm run build64
58+
```
5259

5360
# Node Webkit / Electron
5461

@@ -581,3 +588,53 @@ setInterval(() => {
581588

582589
Note: a loop is not required, e.g. no loop required if you want to simply wait until the first detection of the address being accessed or written to.
583590

591+
# Debug
592+
593+
### 1. Re-compile the project to be debugged
594+
595+
Go to the root directory of the module and run one of the following commands:
596+
```bash
597+
# will automatically compile based on the detected Node architecture
598+
npm run debug
599+
600+
# compile to target 32 bit processes
601+
npm run debug32
602+
603+
# compile to target 64 bit processes
604+
npm run debug64
605+
```
606+
607+
### 2. Change the `index.js` file to require the debug module
608+
609+
Go to the root directory and change the line in `index.js` from:
610+
```javascript
611+
const memoryjs = require('./build/Release/memoryjs');
612+
```
613+
614+
To the following:
615+
```javascript
616+
const memoryjs = require('./build/Debug/memoryjs');
617+
```
618+
619+
### 3. Open the project in Visual Studio
620+
621+
Open the `binding.sln` solution in Visual Studio, found in the `build` folder in the project's root directory.
622+
623+
### 4. Setup Visual Studio debug configuration
624+
625+
1. In the toolbar, click "Project" then "Properties"
626+
2. Under "Configuration Properties", click "Debugging"
627+
3. Set the "Command" property to the location of your `node.exe` file (e.g. `C:\nodejs\node.exe`)
628+
4. Set the "Command Arguments" property to the location of your script file (e.g. `C:\project\test.js`)
629+
630+
### 5. Set breakpoints
631+
632+
Explore the project files in Visual Studio (by expanding `..` and then `lib` in the Solution Explorer). Header files can be viewed by holding `Alt` and cilcking on the header file names at the top of the source code files.
633+
634+
Breakpoints are set by clicking to the left of the line number.
635+
636+
### 6. Run the debugger
637+
638+
Start debugging by either pressing `F5`, by clicking "Debug" in the toolbar and then "Start Debugging" or by clicking "Local Windows Debugger".
639+
640+
The script you've set as the command argument in step 4 will be run, and Visual Studio will pause on the breakpoints set and allow you to step through the code line by line and inspect variables.

package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55
"main": "index.js",
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1",
8-
"install": "node ./scripts/install.js",
8+
"install": "npm run build",
9+
"build": "node ./scripts/install.js",
910
"build32": "node-gyp clean configure build --arch=ia32",
1011
"build64": "node-gyp clean configure build --arch=x64",
11-
"buildtest": "cd test && MSBuild.exe project.sln //p:Configuration=Release"
12+
"buildtest": "cd test && MSBuild.exe project.sln //p:Configuration=Release",
13+
"debug": "node ./scripts/debug.js",
14+
"debug32": "node-gyp configure rebuild --debug --arch=xia32",
15+
"debug64": "node-gyp configure rebuild --debug --arch=x64"
1216
},
1317
"repository": {
1418
"type": "git",

scripts/debug.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* [npm debug script]
3+
* Purpose is to automatically detect Node process architecture and run the
4+
* corresponding script to build the library for debugging.
5+
* Defaults to `node-gyp rebuild` if unable to detect the architecture.
6+
*/
7+
8+
/* eslint-disable no-console */
9+
const { spawn } = require('child_process');
10+
11+
function run(script) {
12+
console.log(`Node architecture is ${process.arch}: running "${script}"`);
13+
14+
const program = script.split(' ')[0];
15+
const args = script.split(' ').slice(1);
16+
17+
// inherit stdio to print colour (helpful for warnings/errors readability)
18+
const child = spawn(program, args, { stdio: 'inherit' });
19+
20+
child.on('close', code => console.log(`Script "${script}" exited with ${code}`));
21+
}
22+
23+
const buildScripts = {
24+
x64: 'run debug64',
25+
ia32: 'run debug32',
26+
};
27+
28+
if (Object.prototype.hasOwnProperty.call(buildScripts, process.arch)) {
29+
// on Windows, npm is actually `npm.cmd`
30+
const npm = /^win/.test(process.platform) ? 'npm.cmd' : 'npm';
31+
run(`${npm} ${buildScripts[process.arch]}`);
32+
} else {
33+
console.log('Unfamiliar architecture detected, this library is probably not compatible with your OS.');
34+
run('node-gyp --debug configure rebuild');
35+
}

0 commit comments

Comments
 (0)