Comments (4)
Hi!
Hmmm, this is not possible I'm afraid, at least not something I know of....
I went to check a bit on how these files are being used, and if I understood correctly, during build time (of node.exe) there is a script (tools/js2c.py
) that embed all internal javascript code inside a c++ file (node_javascript.cc
) that is then linked into node. Then inside node_native_module.cc
they are being compiled (at runtime, method: LookupAndCompile
), which means we can't touch this...
What you can do is "hijack" all the functions inside these modules and "trace" them, say you want to hikack fs
, then after calling njsTrace.inject
do this:
function wrapFunc(module, file, fname) {
var orig = module[fname];
module[fname] = function() {
// note we dont really have line numbers for this method, use 0
var __njsEntryData__ = __njsTraceEntry__({ file: file, name: fname, line: '0', args: arguments });
var res = orig.apply(fs, arguments);
__njsTraceExit__({ entryData: __njsEntryData__, exception: false, line: '0', returnValue: res });
return res;
}
}
var fs = require('fs');
var fsFuncs = Object.getOwnPropertyNames(fs).filter(x => typeof (fs[x]) === 'function');
for (let i = 0; i < fsFuncs.length; i++) {
wrapFunc(fs, 'fs.js', fsFuncs[i]);
}
But since this is too "dangerous" I don't think it should be part of this library... try the above method and keep me posted, it's interesting :)
from njstrace.
Hey, thank you again for the explanation and especially for the code! That's my understanding too from what I read how Node handles the core modules.
Edit: My bad, I should learn to read my logs correctly 🤦♂️ This code works completely as intended in the whole context of teh app! 🎉
Big thank you! This is awesome!
Now, I will extend this for all core modules 😊
Edit 2:
Tracing almost all modules 😊
'use strict'
var coreModules = require('builtin-modules');
// Error due to events and zlib! Exclude others for performance reasons
var excludeModules = ['events', 'zlib', 'crypto', 'stream', 'util'];
// Wrap original function with tracer variables
// https://github.com/ValYouW/njsTrace/issues/18#issuecomment-575951138
function wrapFunc(module, file, fname) {
var orig = module[fname];
module[fname] = function () {
// Note: we don't really have line numbers for this method, use 0
var __njsEntryData__ = __njsTraceEntry__({ file: file, name: fname, line: '0', args: arguments });
var res = orig.apply(module, arguments);
__njsTraceExit__({ entryData: __njsEntryData__, exception: false, line: '0', returnValue: res });
return res;
}
}
for (let j = 0; j < coreModules.length; j++) {
// Skip crypto module due to performance reasons
if (!excludeModules.includes(coreModules[j])) {
var coreModuleObj = require(coreModules[j])
var coreModuleFuncs = Object.getOwnPropertyNames(coreModuleObj).filter(x => typeof (coreModuleObj[x]) === 'function');
for (let i = 0; i < coreModuleFuncs.length; i++) {
// Skip Buffer methods
if (!coreModuleFuncs[i].includes('Buffer')) {
wrapFunc(coreModuleObj, coreModules[j] + '.core', coreModuleFuncs[i]);
}
}
}
}
from njstrace.
Great!
BTW, one time I wanted to understand something in node so I just compiled it from source and debugged it (the c++ code). So you can always clone "node", modify the "libs" as you want and compile, you will have your own modified version of the libs....
Send a link to the paper when you are done with your project :)
from njstrace.
Yes, this is a very good idea.
Unfortunately, the overall report will be in German, but at least the abstract will be translated. I secretly hope that maybe my professor or someone at the chair will take the results publish it in english 😉 I'll keep you posted 😊
Thanks again!
from njstrace.
Related Issues (20)
- Print function arguments and return value
- Don't leak the "arguments" object
- Check why retLine is off by 1 line HOT 1
- this.config.files throws an error HOT 1
- Doesn't work when running from a different working directory HOT 1
- Async call handling HOT 11
- Not working with generators HOT 6
- Won't parse spread operator HOT 2
- arrow functions without { } HOT 10
- Error instrumenting bluebird HOT 5
- How to trace ES6 Code call stack HOT 3
- Support try/catch without error HOT 2
- Error instrumenting OWASP's juice-shop HOT 7
- babel-node: this.log is not a function HOT 3
- Problem with tracing node_modules HOT 2
- trace js code on each step of code execution along with storing the variables value change. HOT 2
- Tracing vm Script HOT 1
- Tracing code execution of an Angular 2+/TypeScript/JavaScript application HOT 1
- [Question]: Running njsTrace in the context of tests HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from njstrace.