Skip to content

Instantly share code, notes, and snippets.

@trevnorris
Created February 5, 2015 18:25
Show Gist options
  • Save trevnorris/2d4036f54b1b2714ca1e to your computer and use it in GitHub Desktop.
Save trevnorris/2d4036f54b1b2714ca1e to your computer and use it in GitHub Desktop.
How to hook into additional V8 tracing API. Also, commented out is how to use backtrace(3)
#include <v8.h>
#include <node.h>
#include <assert.h>
#include <execinfo.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
using namespace v8;
//const int ARRAY_LENGTH = 10;
static int* counter_lookup_callback(const char* name) {
//fprintf(stderr, "lookup: %s\n", name);
return NULL;
}
static void* create_hist_cb(const char* name, int min, int max, size_t bckts) {
//fprintf(stderr, "hist: %s\n", name);
return NULL;
}
static void add_hist_cb(void* histogram, int sample) {
}
static void jit_code_handler(const JitCodeEvent* event) {
//fprintf(stderr, "jit: %i\n", event->type);
}
static void log_event_cb(const char* name, int event) {
//fprintf(stderr, "log: %s\n", name);
}
static void call_complete_cb() {
//fprintf(stderr, "call\n");
}
static void counter_cb(Isolate* isolate, Isolate::UseCounterFeature feature) {
//fprintf(stderr, "counter: %i\n", feature);
}
static void mem_alloc_cb(ObjectSpace space, AllocationAction action, int size) {
//fprintf(stderr, "alloc\n");
}
static void promise_reject(PromiseRejectMessage message) {
//fprintf(stderr, "reject: %i\n", message.GetEvent());
}
static uintptr_t address_loc_resolver(uintptr_t return_addr_location) {
//fprintf(stderr, "addr: %lu\n", return_addr_location);
return return_addr_location;
}
void RunMe(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
isolate->SetCounterFunction(counter_lookup_callback);
isolate->SetCreateHistogramFunction(create_hist_cb);
isolate->SetAddHistogramSampleFunction(add_hist_cb);
isolate->SetJitCodeEventHandler(kJitCodeEventDefault, jit_code_handler);
isolate->SetEventLogger(log_event_cb);
isolate->AddCallCompletedCallback(call_complete_cb);
isolate->SetUseCounterCallback(counter_cb);
isolate->AddMemoryAllocationCallback(mem_alloc_cb,
kObjectSpaceOldPointerSpace,
kAllocationActionAllocate);
isolate->SetPromiseRejectCallback(promise_reject);
V8::SetReturnAddressLocationResolver(address_loc_resolver);
/*
void* array[ARRAY_LENGTH];
int32_t size;
size = backtrace(array, ARRAY_LENGTH);
backtrace_symbols_fd(array, ARRAY_LENGTH, STDERR_FILENO);
args.GetReturnValue().Set(size);
*/
}
void init(Handle<Object> exports) {
NODE_SET_METHOD(exports, "runMe", RunMe);
}
NODE_MODULE(addon, init)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment