forked from Jimbly/node-mtrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmtrace.cc
71 lines (60 loc) · 1.71 KB
/
mtrace.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#if defined(__APPLE__) || defined(__CYGWIN__) || defined(WIN32)
// mtrace doesn't seem to exist on OSX, simply have this module do nothing
#define DISABLED
#endif
#include <nan.h>
#ifndef DISABLED
#include <mcheck.h>
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace node;
using namespace v8;
NAN_METHOD(GCMTrace) {
Nan::HandleScope scope;
while (!Nan::IdleNotification(500));
info.GetReturnValue().SetUndefined();
}
NAN_METHOD(wrapMTrace) {
Nan::HandleScope scope;
#ifndef DISABLED
const char *filename;
std::string sfilename;
char buf[64];
if (info.Length() >= 1 && info[0]->IsString()) {
// get filename
String::Utf8Value utf8_value(info[0]);
sfilename.assign(*utf8_value);
filename = sfilename.c_str();
} else {
static int counter = 0;
pid_t pid = getpid();
long long int llpid = pid;
sprintf(buf, "mtrace.%Ld.%d", llpid, counter++);
filename = buf;
}
setenv("MALLOC_TRACE", filename, 1);
mtrace();
info.GetReturnValue().Set(Nan::New<String>(filename).ToLocalChecked());
#else
info.GetReturnValue().SetUndefined();
#endif
}
NAN_METHOD(wrapMUnTrace) {
Nan::HandleScope scope;
#ifndef DISABLED
muntrace();
#endif
info.GetReturnValue().SetUndefined();
}
extern "C" {
NAN_MODULE_INIT(init) {
Nan::HandleScope scope;
Nan::Set(target, Nan::New("mtrace").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(wrapMTrace)).ToLocalChecked() );
Nan::Set(target, Nan::New("muntrace").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(wrapMUnTrace)).ToLocalChecked());
Nan::Set(target, Nan::New("gc").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(GCMTrace)).ToLocalChecked());
}
NODE_MODULE(mtrace, init);
}