Skip to content

Instantly share code, notes, and snippets.

@abhi-bit
Last active September 8, 2016 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhi-bit/20b8569bc5d655659b51d425564eb49d to your computer and use it in GitHub Desktop.
Save abhi-bit/20b8569bc5d655659b51d425564eb49d to your computer and use it in GitHub Desktop.
V8 based JS Debugger
debug_testing.ccmain312STARTING UP Process thread
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":0}
"Logged from JS world" "Chrome Canary" 0
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":1}
"Logged from JS world" "Chrome Canary" 1
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":2}
"Logged from JS world" "Chrome Canary" 2
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":3}
"Logged from JS world" "Chrome Canary" 3
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":4}
"Logged from JS world" "Chrome Canary" 4
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":5}
"Logged from JS world" "Chrome Canary" 5
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":6}
"Logged from JS world" "Chrome Canary" 6
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":7}
"Logged from JS world" "Chrome Canary" 7
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":8}
"Logged from JS world" "Chrome Canary" 8
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":9}
"Logged from JS world" "Chrome Canary" 9
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":10}
"Logged from JS world" "Chrome Canary" 10
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":11}
"Logged from JS world" "Chrome Canary" 11
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":12}
"Logged from JS world" "Chrome Canary" 12
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":13}
"Logged from JS world" "Chrome Canary" 13
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":14}
"Logged from JS world" "Chrome Canary" 14
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":15}
"Logged from JS world" "Chrome Canary" 15
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":16}
"Logged from JS world" "Chrome Canary" 16
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":17}
"Logged from JS world" "Chrome Canary" 17
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":18}
"Logged from JS world" "Chrome Canary" 18
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":19}
"Logged from JS world" "Chrome Canary" 19
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":20}
"Logged from JS world" "Chrome Canary" 20
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":21}
"Logged from JS world" "Chrome Canary" 21
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":22}
"Logged from JS world" "Chrome Canary" 22
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":23}
"Logged from JS world" "Chrome Canary" 23
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":24}
"Logged from JS world" "Chrome Canary" 24
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":25}
"Logged from JS world" "Chrome Canary" 25
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":26}
"Logged from JS world" "Chrome Canary" 26
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":27}
"Logged from JS world" "Chrome Canary" 27
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":28}
"Logged from JS world" "Chrome Canary" 28
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":29}
"Logged from JS world" "Chrome Canary" 29
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":30}
"Logged from JS world" "Chrome Canary" 30
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":31}
"Logged from JS world" "Chrome Canary" 31
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":32}
"Logged from JS world" "Chrome Canary" 32
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":33}
"Logged from JS world" "Chrome Canary" 33
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":34}
"Logged from JS world" "Chrome Canary" 34
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":35}
"Logged from JS world" "Chrome Canary" 35
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":36}
"Logged from JS world" "Chrome Canary" 36
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":37}
"Logged from JS world" "Chrome Canary" 37
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":38}
"Logged from JS world" "Chrome Canary" 38
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":39}
"Logged from JS world" "Chrome Canary" 39
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":40}
"Logged from JS world" "Chrome Canary" 40
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":41}
"Logged from JS world" "Chrome Canary" 41
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":42}
"Logged from JS world" "Chrome Canary" 42
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":43}
"Logged from JS world" "Chrome Canary" 43
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":44}
"Logged from JS world" "Chrome Canary" 44
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":45}
"Logged from JS world" "Chrome Canary" 45
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":46}
"Logged from JS world" "Chrome Canary" 46
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":47}
"Logged from JS world" "Chrome Canary" 47
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":48}
"Logged from JS world" "Chrome Canary" 48
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":49}
"Logged from JS world" "Chrome Canary" 49
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":50}
"Logged from JS world" "Chrome Canary" 50
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":51}
"Logged from JS world" "Chrome Canary" 51
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":52}
"Logged from JS world" "Chrome Canary" 52
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":53}
"Logged from JS world" "Chrome Canary" 53
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":54}
"Logged from JS world" "Chrome Canary" 54
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":55}
"Logged from JS world" "Chrome Canary" 55
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":56}
"Logged from JS world" "Chrome Canary" 56
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":57}
"Logged from JS world" "Chrome Canary" 57
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":58}
"Logged from JS world" "Chrome Canary" 58
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":59}
"Logged from JS world" "Chrome Canary" 59
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":60}
"Logged from JS world" "Chrome Canary" 60
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":61}
"Logged from JS world" "Chrome Canary" 61
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":62}
"Logged from JS world" "Chrome Canary" 62
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":63}
"Logged from JS world" "Chrome Canary" 63
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":64}
"Logged from JS world" "Chrome Canary" 64
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":65}
"Logged from JS world" "Chrome Canary" 65
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":66}
"Logged from JS world" "Chrome Canary" 66
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":67}
"Logged from JS world" "Chrome Canary" 67
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":68}
"Logged from JS world" "Chrome Canary" 68
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":69}
"Logged from JS world" "Chrome Canary" 69
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":70}
"Logged from JS world" "Chrome Canary" 70
debug_testing.ccProcessRequest190
2016-09-08.15:05:39ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":71}
"Logged from JS world" "Chrome Canary" 71
debug_testing.ccProcessRequest190
2016-09-08.15:05:40ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":72}
"Logged from JS world" "Chrome Canary" 72
debug_testing.ccProcessRequest190
2016-09-08.15:05:40ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":73}
"Logged from JS world" "Chrome Canary" 73
debug_testing.ccProcessRequest190
2016-09-08.15:05:40ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":74}
"Logged from JS world" "Chrome Canary" 74
debug_testing.ccProcessRequest190
2016-09-08.15:05:40ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":75}
"Logged from JS world" "Chrome Canary" 75
debug_testing.ccProcessRequest190
2016-09-08.15:05:40ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":76}
"Logged from JS world" "Chrome Canary" 76
debug_testing.ccProcessRequest190
2016-09-08.15:05:40ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":77}
"Logged from JS world" "Chrome Canary" 77
debug_testing.ccProcessRequest190
2016-09-08.15:05:40ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":78}
"Logged from JS world" "Chrome Canary" 78
debug_testing.ccProcessRequest190
2016-09-08.15:05:40ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":79}
"Logged from JS world" "Chrome Canary" 79
debug_testing.ccProcessRequest190
2016-09-08.15:05:40ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":80}
"Logged from JS world" "Chrome Canary" 80
debug_testing.ccProcessRequest190
2016-09-08.15:05:40ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":81}
"Logged from JS world" "Chrome Canary" 81
debug_testing.ccProcessRequest190
2016-09-08.15:05:40ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":82}
"Logged from JS world" "Chrome Canary" 82
debug_testing.ccProcessRequest190
2016-09-08.15:05:40ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":83}
"Logged from JS world" "Chrome Canary" 83
debug_testing.ccProcessRequest190
2016-09-08.15:05:40ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":84}
"Logged from JS world" "Chrome Canary" 84
debug_testing.ccmain317STARTING UP Set breakpoint thread
debug_testing.ccTestSetBreakPoints198
2016-09-08.15:05:40 DebugSetBreakpointHandler {"seq":0,"request_seq":1,"type":"response","command":"setbreakpoint","success":true,"body":{"type":"scriptId","breakpoint":1,"script_id":33,"line":1,"column":0,"actual_locations":[{"line":1,"column":4,"script_id":33}]},"refs":[],"running":true}
debug_testing.ccProcessRequest190
2016-09-08.15:05:40ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":85}
2016-09-08.15:05:40 DebugSetBreakpointHandler {"seq":1,"type":"event","event":"break","body":{"invocationText":"DebugUserRequest(doc=#<Object>)","sourceLine":1,"sourceColumn":4,"sourceLineText":" if (doc.type === \"json\") {","script":{"id":33,"name":null,"lineOffset":0,"columnOffset":0,"lineCount":6},"breakpoints":[1]}}
debug_testing.ccmain322STARTING UP List breakpoint thread
debug_testing.ccTestListBreakPoints210
debug_testing.ccTestListBreakPoints218
2016-09-08.15:05:41 DebugListBreakpointHandler {"seq":2,"request_seq":3,"type":"response","command":"listbreakpoints","success":true,"body":{"breakpoints":[{"number":1,"line":1,"column":0,"groupId":null,"active":true,"condition":null,"actual_locations":[{"line":1,"column":4,"script_id":33}],"type":"scriptId","script_id":33}],"breakOnExceptions":false,"breakOnUncaughtExceptions":false},"refs":[],"running":false}
debug_testing.ccmain329STARTING UP clear breakpoint thread
debug_testing.ccTestClearBreakPoints222
debug_testing.ccTestClearBreakPoints230
2016-09-08.15:05:45 DebugClearBreakpointHandler {"seq":3,"request_seq":10,"type":"response","command":"clearbreakpoint","success":true,"body":{"breakpoint":1},"refs":[],"running":false}
debug_testing.ccmain334STARTING UP AGAIN List breakpoint thread
debug_testing.ccTestListBreakPoints210
debug_testing.ccTestListBreakPoints218
2016-09-08.15:05:48 DebugListBreakpointHandler {"seq":4,"request_seq":3,"type":"response","command":"listbreakpoints","success":true,"body":{"breakpoints":[],"breakOnExceptions":false,"breakOnUncaughtExceptions":false},"refs":[],"running":false}
debug_testing.ccmain340STARTING UP Evaluate thread
debug_testing.ccTestEvaluate246
debug_testing.ccTestEvaluate254
2016-09-08.15:05:51 DebugEvaluateHandler {"seq":5,"request_seq":12,"type":"response","command":"evaluate","success":false,"message":"ReferenceError: doc is not defined","running":false}
debug_testing.ccmain346STARTING UP Continue thread
debug_testing.ccTestContinue234
debug_testing.ccTestContinue242
2016-09-08.15:05:54 DebugContinueHandler {"seq":6,"request_seq":11,"type":"response","command":"continue","success":true,"running":true}
"Logged from JS world" "Chrome Canary" 85
debug_testing.ccProcessRequest190
2016-09-08.15:05:54ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":86}
"Logged from JS world" "Chrome Canary" 86
debug_testing.ccProcessRequest190
2016-09-08.15:05:54ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":87}
"Logged from JS world" "Chrome Canary" 87
debug_testing.ccProcessRequest190
2016-09-08.15:05:54ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":88}
"Logged from JS world" "Chrome Canary" 88
debug_testing.ccProcessRequest190
2016-09-08.15:05:54ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":89}
"Logged from JS world" "Chrome Canary" 89
debug_testing.ccProcessRequest190
2016-09-08.15:05:54ProcessDebugUserRequest{"type": "json", "client": "Chrome Canary", "counter":90}
"Logged from JS world" "Chrome Canary" 90
<------Truncating log here------->
function DebugUserRequest(doc) {
if (doc.type === "json") {
log("Logged from JS world", doc.client, doc.counter);
}
}
#include <cassert>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <streambuf>
#include <string>
#include <thread>
#include <unistd.h>
#include <vector>
#include <atomic>
#include <include/v8.h>
#include <include/v8-debug.h>
#include <include/libplatform/libplatform.h>
using namespace std;
using namespace v8;
Isolate* isolate;
Persistent<Context> context_;
Persistent<Function> debug_user_request;
volatile std::atomic<bool> exitflag;
const string currentDateTime() {
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);
return buf;
}
int AsciiToUtf16(const char* input_buffer, uint16_t* output_buffer) {
int i;
for (i = 0; input_buffer[i] != '\0'; ++i) {
// ASCII does not use chars > 127, but be careful anyway.
output_buffer[i] = static_cast<unsigned char>(input_buffer[i]);
}
output_buffer[i] = 0;
return i;
}
const char* ToCString(const String::Utf8Value& value) {
return *value ? *value : "<string conversion failed>";
}
const char* ToJson(Isolate* isolate, Handle<Value> object) {
HandleScope handle_scope(isolate);
Local<Context> context = isolate->GetCurrentContext();
Local<Object> global = context->Global();
Local<Object> JSON = global->Get(String::NewFromUtf8(isolate, "JSON"))->ToObject();
Local<Function> JSON_stringify = Local<Function>::Cast(
JSON->Get(
String::NewFromUtf8(isolate, "stringify")));
Local<Value> result;
Local<Value> args[1];
args[0] = { object };
result = JSON_stringify->Call(context->Global(), 1, args);
String::Utf8Value str(result->ToString());
return ToCString(str);
}
string ObjectToString(Local<Value> value) {
String::Utf8Value utf8_value(value);
return string(*utf8_value);
}
string ToString(Isolate* isolate, Handle<Value> object) {
HandleScope handle_scope(isolate);
Local<Context> context = isolate->GetCurrentContext();
Local<Object> global = context->Global();
Local<Object> JSON = global->Get(String::NewFromUtf8(isolate, "JSON"))->ToObject();
Local<Function> JSON_stringify =
Local<Function>::Cast(JSON->Get(
String::NewFromUtf8(isolate, "stringify")));
Local<Value> result;
Local<Value> args[1];
args[0] = { object };
result = JSON_stringify->Call(context->Global(), 1, args);
return ObjectToString(result);
}
void Print(const FunctionCallbackInfo<Value>& args) {
bool first = true;
for (int i = 0; i < args.Length(); i++) {
HandleScope handle_scope(args.GetIsolate());
if (first) {
first = false;
} else {
printf(" ");
}
String::Utf8Value str(args[i]);
const char* cstr = ToJson(args.GetIsolate(), args[i]);
printf("%s", cstr);
}
printf("\n");
fflush(stdout);
}
// ================================================================
static void DebugSetBreakpointHandler(const v8::Debug::Message& message) {
v8::Local<v8::String> json = message.GetJSON();
v8::String::Utf8Value utf8(json);
cout << currentDateTime() << " " << __FUNCTION__
<< " " << *utf8 << endl;
}
static void DebugClearBreakpointHandler(const v8::Debug::Message& message) {
v8::Local<v8::String> json = message.GetJSON();
v8::String::Utf8Value utf8(json);
cout << currentDateTime() << " " << __FUNCTION__
<< " " << *utf8 << endl;
}
static void DebugContinueHandler(const v8::Debug::Message& message) {
v8::Local<v8::String> json = message.GetJSON();
v8::String::Utf8Value utf8(json);
cout << currentDateTime() << " " << __FUNCTION__
<< " " << *utf8 << endl;
}
static void DebugEvaluateHandler(const v8::Debug::Message& message) {
v8::Local<v8::String> json = message.GetJSON();
v8::String::Utf8Value utf8(json);
cout << currentDateTime() << " " << __FUNCTION__
<< " " << *utf8 << endl;
}
static void DebugListBreakpointHandler(const v8::Debug::Message& message) {
v8::Local<v8::String> json = message.GetJSON();
v8::String::Utf8Value utf8(json);
cout << currentDateTime() << " " << __FUNCTION__
<< " " << *utf8 << endl;
}
// ================================================================
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
public:
virtual void* Allocate(size_t length) {
void* data = AllocateUninitialized(length);
return data == NULL ? data : memset(data, 0, length);
}
virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
virtual void Free(void* data, size_t) { free(data); }
};
void ProcessDebugUserRequest(string request) {
Locker locker(isolate);
Isolate::Scope isolate_Scope(isolate);
HandleScope handle_Scope(isolate);
Local<Context> context = Local<Context>::New(isolate, context_);
Context::Scope context_scope(context);
Handle<Value> args[1];
args[0] = JSON::Parse(
String::NewFromUtf8(isolate, request.c_str()));
cout << currentDateTime() << __FUNCTION__
<< request << " " << endl;
Local<Function> handle_user_req_fun = Local<Function>::New(
isolate, debug_user_request);
handle_user_req_fun->Call(context->Global(), 1, args);
Debug::ProcessDebugMessages(isolate);
}
void ProcessRequest() {
string prefix("{\"type\": \"json\", \"client\": \"Chrome Canary\", \"counter\":");
int i = 0;
while(exitflag) {
string request;
request.append(prefix);
request.append(to_string(i++));
request.append("}");
cout << __FILE__ << __FUNCTION__ << __LINE__ << endl;
ProcessDebugUserRequest(request);
}
cout << __FILE__ << __FUNCTION__ << __LINE__ << endl;
}
void TestSetBreakPoints() {
cout << __FILE__ << __FUNCTION__ << __LINE__ << endl;
const int kBufferSize = 1000;
uint16_t buffer[kBufferSize];
string brkp_cmd("{\"command\": \"setbreakpoint\", \"type\": \"request\", \"arguments\": {\"type\": \"function\", \"target\": \"DebugUserRequest\", \"line\":1, \"column\": 0}, \"seq\": 1}");
Debug::SetMessageHandler(isolate,
DebugSetBreakpointHandler);
Debug::SendCommand(isolate, buffer, AsciiToUtf16(
brkp_cmd.c_str(), buffer));
}
void TestListBreakPoints() {
cout << __FILE__ << __FUNCTION__ << __LINE__ << endl;
const int kBufferSize = 1000;
uint16_t buffer[kBufferSize];
string lbrkp_cmd("{\"command\": \"listbreakpoints\", \"type\": \"request\", \"seq\": 3}");
Debug::SetMessageHandler(isolate,
DebugListBreakpointHandler);
Debug::SendCommand(isolate, buffer, AsciiToUtf16(
lbrkp_cmd.c_str(), buffer));
cout << __FILE__ << __FUNCTION__ << __LINE__ << endl;
}
void TestClearBreakPoints() {
cout << __FILE__ << __FUNCTION__ << __LINE__ << endl;
const int kBufferSize = 1000;
uint16_t buffer[kBufferSize];
string clear_brkp_cmd("{\"seq\":10,\"type\":\"request\",\"command\":\"clearbreakpoint\",\"arguments\":{\"type\":\"function\",\"breakpoint\":1}}");
Debug::SetMessageHandler(isolate, DebugClearBreakpointHandler);
Debug::SendCommand(isolate, buffer, AsciiToUtf16(
clear_brkp_cmd.c_str(), buffer));
cout << __FILE__ << __FUNCTION__ << __LINE__ << endl;
}
void TestContinue() {
cout << __FILE__ << __FUNCTION__ << __LINE__ << endl;
const int kBufferSize = 1000;
uint16_t buffer[kBufferSize];
string continue_cmd("{\"seq\":11,\"type\":\"request\",\"command\":\"continue\"}");
Debug::SetMessageHandler(isolate, DebugContinueHandler);
Debug::SendCommand(isolate, buffer, AsciiToUtf16(
continue_cmd.c_str(), buffer));
cout << __FILE__ << __FUNCTION__ << __LINE__ << endl;
}
void TestEvaluate() {
cout << __FILE__ << __FUNCTION__ << __LINE__ << endl;
const int kBufferSize = 1000;
uint16_t buffer[kBufferSize];
string evaluate_cmd("{\"seq\":12,\"type\":\"request\",\"command\":\"evaluate\",\"arguments\":{\"expression\":\"doc\",\"global\":true,\"disable_break\":true}}");
Debug::SetMessageHandler(isolate, DebugEvaluateHandler);
Debug::SendCommand(isolate, buffer, AsciiToUtf16(
evaluate_cmd.c_str(), buffer));
cout << __FILE__ << __FUNCTION__ << __LINE__ << endl;
}
int main(int argc, char* argv[]) {
std::cout.setf( std::ios_base::unitbuf );
V8::InitializeICU();
V8::InitializeExternalStartupData(argv[0]);
Platform* platform = platform::CreateDefaultPlatform();
V8::InitializePlatform(platform);
V8::Initialize();
ArrayBufferAllocator allocator;
Isolate::CreateParams create_params;
create_params.array_buffer_allocator = &allocator;
isolate = Isolate::New(create_params);
{
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
Local<ObjectTemplate> global = ObjectTemplate::New(isolate);
global->Set(String::NewFromUtf8(isolate, "log"),
FunctionTemplate::New(isolate, Print));
Local<Context> context = Context::New(isolate, NULL, global);
context_.Reset(isolate, context);
Context::Scope context_scope(context);
ifstream file_name(argv[1]);
string src((istreambuf_iterator<char>(file_name)),
istreambuf_iterator<char>());
Local<String> source =
String::NewFromUtf8(isolate, src.c_str(),
NewStringType::kNormal).ToLocalChecked();
Local<Script> script = Script::Compile(context,
source).ToLocalChecked();
Local<Value> result = script->Run(context).ToLocalChecked();
Local<String> handle_user_req =
String::NewFromUtf8(isolate,
"DebugUserRequest", NewStringType::kNormal)
.ToLocalChecked();
Local<Value> handle_user_req_val;
if(!context->Global()->Get(context,
handle_user_req).ToLocal(&handle_user_req_val))
cout << "Failed to grab DebugUserRequest function " << endl;
Local<Function> handle_user_req_fun =
Local<Function>::Cast(handle_user_req_val);
assert(handle_user_req_fun->IsFunction());
debug_user_request.Reset(isolate, handle_user_req_fun);
}
exitflag = true;
cout << __FILE__ << __FUNCTION__ << __LINE__
<< "STARTING UP Process thread" << endl;
thread send_debug_user_req_thr(ProcessRequest);
sleep(1);
cout << __FILE__ << __FUNCTION__ << __LINE__
<< "STARTING UP Set breakpoint thread" << endl;
thread set_break_point(TestSetBreakPoints);
sleep(1);
cout << __FILE__ << __FUNCTION__ << __LINE__
<< "STARTING UP List breakpoint thread" << endl;
thread set_listbp2(TestListBreakPoints);
sleep(3);
set_listbp2.join();
sleep(1);
cout << __FILE__ << __FUNCTION__ << __LINE__
<< "STARTING UP clear breakpoint thread" << endl;
thread clear_break_point(TestClearBreakPoints);
sleep(3);
cout << __FILE__ << __FUNCTION__ << __LINE__
<< "STARTING UP AGAIN List breakpoint thread" << endl;
thread set_listbp3(TestListBreakPoints);
sleep(3);
set_listbp3.join();
cout << __FILE__ << __FUNCTION__ << __LINE__
<< "STARTING UP Evaluate thread" << endl;
thread test_eval1(TestEvaluate);
sleep(3);
test_eval1.join();
cout << __FILE__ << __FUNCTION__ << __LINE__
<< "STARTING UP Continue thread" << endl;
thread continue1(TestContinue);
sleep(3);
continue1.join();
exitflag = false;
cout << __FILE__ << __FUNCTION__ << __LINE__ << endl;
set_break_point.join();
cout << __FILE__ << __FUNCTION__ << __LINE__ << endl;
clear_break_point.join();
cout << __FILE__ << __FUNCTION__ << __LINE__ << endl;
send_debug_user_req_thr.join();
cout << __FILE__ << __FUNCTION__ << __LINE__ << endl;
isolate->Dispose();
V8::Dispose();
V8::ShutdownPlatform();
delete platform;
return 0;
}
@abhi-bit
Copy link
Author

abhi-bit commented Sep 8, 2016

Steps to run this sample example:

  • Compile the cpp code
  • Then run the generated executable against a sample file containing a JS function(src.js attached with this gist)
$ ./a.out src.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment