Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
153 lines (110 sloc) 4.79 KB
#include "dtrace_provider.h"
#include <v8.h>
#include <node.h>
#include <stdio.h>
namespace node {
using namespace v8;
Persistent<FunctionTemplate> DTraceProvider::constructor_template;
void DTraceProvider::Initialize(Handle<Object> target) {
HandleScope scope;
Local<FunctionTemplate> t = FunctionTemplate::New(DTraceProvider::New);
constructor_template = Persistent<FunctionTemplate>::New(t);
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
constructor_template->SetClassName(String::NewSymbol("DTraceProvider"));
NODE_SET_PROTOTYPE_METHOD(constructor_template, "addProbe", DTraceProvider::AddProbe);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "removeProbe", DTraceProvider::RemoveProbe);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "enable", DTraceProvider::Enable);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "disable", DTraceProvider::Disable);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "fire", DTraceProvider::Fire);
target->Set(String::NewSymbol("DTraceProvider"), constructor_template->GetFunction());
DTraceProbe::Initialize(target);
}
Handle<Value> DTraceProvider::New(const Arguments& args) {
HandleScope scope;
DTraceProvider *p = new DTraceProvider();
p->Wrap(args.This());
if (args.Length() != 1 || !args[0]->IsString()) {
return ThrowException(Exception::Error(String::New(
"Must give provider name as argument")));
}
String::AsciiValue name(args[0]->ToString());
if ((p->provider = usdt_create_provider(*name, "modname")) == NULL) {
return ThrowException(Exception::Error(String::New(
"usdt_create_provider failed")));
}
return args.This();
}
Handle<Value> DTraceProvider::AddProbe(const Arguments& args) {
HandleScope scope;
const char *types[USDT_ARG_MAX];
int argc = 0;
Handle<Object> obj = args.Holder();
DTraceProvider *provider = ObjectWrap::Unwrap<DTraceProvider>(obj);
// create a DTraceProbe object
Handle<Function> klass = DTraceProbe::constructor_template->GetFunction();
Handle<Object> pd = Persistent<Object>::New(klass->NewInstance());
// store in provider object
DTraceProbe *probe = ObjectWrap::Unwrap<DTraceProbe>(pd->ToObject());
obj->Set(args[0]->ToString(), pd);
// add probe to provider
for (int i = 0; i < USDT_ARG_MAX; i++) {
if (i < args.Length() - 1) {
String::AsciiValue type(args[i + 1]->ToString());
types[i] = strdup(*type);
argc++;
}
}
String::AsciiValue name(args[0]->ToString());
probe->probedef = usdt_create_probe(*name, *name, argc, types);
usdt_provider_add_probe(provider->provider, probe->probedef);
return pd;
}
Handle<Value> DTraceProvider::RemoveProbe(const Arguments& args) {
HandleScope scope;
Handle<Object> provider_obj = args.Holder();
DTraceProvider *provider = ObjectWrap::Unwrap<DTraceProvider>(provider_obj);
Handle<Object> probe_obj = Local<Object>::Cast(args[0]);
DTraceProbe *probe = ObjectWrap::Unwrap<DTraceProbe>(probe_obj);
Handle<String> name = String::New(probe->probedef->name);
provider_obj->Delete(name);
if (usdt_provider_remove_probe(provider->provider, probe->probedef) != 0)
return ThrowException(Exception::Error(String::New(usdt_errstr(provider->provider))));
return True();
}
Handle<Value> DTraceProvider::Enable(const Arguments& args) {
HandleScope scope;
DTraceProvider *provider = ObjectWrap::Unwrap<DTraceProvider>(args.Holder());
if (usdt_provider_enable(provider->provider) != 0)
return ThrowException(Exception::Error(String::New(usdt_errstr(provider->provider))));
return Undefined();
}
Handle<Value> DTraceProvider::Disable(const Arguments& args) {
HandleScope scope;
DTraceProvider *provider = ObjectWrap::Unwrap<DTraceProvider>(args.Holder());
if (usdt_provider_disable(provider->provider) != 0)
return ThrowException(Exception::Error(String::New(usdt_errstr(provider->provider))));
return Undefined();
}
Handle<Value> DTraceProvider::Fire(const Arguments& args) {
HandleScope scope;
if (!args[0]->IsString()) {
return ThrowException(Exception::Error(String::New(
"Must give probe name as first argument")));
}
if (!args[1]->IsFunction()) {
return ThrowException(Exception::Error(String::New(
"Must give probe value callback as second argument")));
}
Handle<Object> provider = args.Holder();
Handle<Object> probe = Local<Object>::Cast(provider->Get(args[0]));
DTraceProbe *p = ObjectWrap::Unwrap<DTraceProbe>(probe);
if (p == NULL)
return Undefined();
p->_fire(args[1]);
return True();
}
extern "C" void
init(Handle<Object> target) {
DTraceProvider::Initialize(target);
}
} // namespace node