Coder Social home page Coder Social logo

Comments (4)

ValYouW avatar ValYouW commented on July 30, 2024

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.

christophschw avatar christophschw commented on July 30, 2024

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.

ValYouW avatar ValYouW commented on July 30, 2024

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.

christophschw avatar christophschw commented on July 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.