Skip to content
Browse files

Support v8 remote debugger.

The feature is enabled only if make is given ENABLE_DEBUGGER_SUPPORT=1,
because we are not sure how much of v8 installations are with debug
enabled.  Maybe it is safe to flip the flag to DISABLE_DEBUGGER_SUPPORT.
On the other hand, with static build, we are sure we can support it.
  • Loading branch information...
1 parent 7af3243 commit a9c05802541a4a444d86844e9385a7bd502545ca @umitanuki umitanuki committed Dec 2, 2012
Showing with 68 additions and 1 deletion.
  1. +7 −1 Makefile
  2. +2 −0 Makefile.v8
  3. +56 −0 plv8.cc
  4. +3 −0 plv8.h
View
8 Makefile
@@ -3,6 +3,7 @@
# Makefile for plv8
#
# @param DISABLE_DIALECT if defined, not build dialects (i.e. plcoffee, etc)
+# @param ENABLE_DEBUGGER_SUPPORT enables v8 deubbger agent
#
# There are two ways to build plv8.
# 1. Dynamic link to v8 (default)
@@ -41,8 +42,13 @@ REGRESS += dialect
endif
SHLIB_LINK += -lv8
+# v8's remote debugger is optional at the moment, since we don't know
+# how much of the v8 installation is built with debugger enabled.
+ifdef ENABLE_DEBUGGER_SUPPORT
+OPT_ENABLE_DEBUGGER_SUPPORT = -DENABLE_DEBUGGER_SUPPORT=1
+endif
OPTFLAGS = -O2
-CCFLAGS = -Wall $(OPTFLAGS)
+CCFLAGS = -Wall $(OPTFLAGS) $(OPT_ENABLE_DEBUGGER_SUPPORT)
all:
View
2 Makefile.v8
@@ -14,6 +14,8 @@ AUTOV8_LIB = $(AUTOV8_OUT)/libv8_snapshot.a
AUTOV8_STATIC_LIBS = libv8_base.a libv8_snapshot.a
SHLIB_LINK += $(addprefix $(AUTOV8_OUT)/, $(AUTOV8_STATIC_LIBS))
+# We are sure the static buid can support debugger
+ENABLE_DEBUGGER_SUPPORT=1
all: $(AUTOV8_LIB)
View
56 plv8.cc
@@ -155,12 +155,36 @@ static Persistent<ObjectTemplate> GetGlobalObjectTemplate();
/* A GUC to specify a custom start up function to call */
static char *plv8_start_proc = NULL;
+
+/* A GUC to specify the remote debugger port */
+static int plv8_debugger_port;
/*
* We use vector instead of hash since the size of this array
* is expected to be short in most cases.
*/
static std::vector<plv8_context *> ContextVector;
+#ifdef ENABLE_DEBUGGER_SUPPORT
+v8::Persistent<v8::Context> debug_message_context;
+
+void DispatchDebugMessages() {
+ // We are in some random thread. We should already have v8::Locker acquired
+ // (we requested this when registered this callback). We was called
+ // because new debug messages arrived; they may have already been processed,
+ // but we shouldn't worry about this.
+ //
+ // All we have to do is to set context and call ProcessDebugMessages.
+ //
+ // We should decide which V8 context to use here. This is important for
+ // "evaluate" command, because it must be executed some context.
+ // In our sample we have only one context, so there is nothing really to
+ // think about.
+ v8::Context::Scope scope(debug_message_context);
+
+ v8::Debug::ProcessDebugMessages();
+}
+#endif // ENABLE_DEBUGGER_SUPPORT
+
void
_PG_init(void)
{
@@ -184,6 +208,19 @@ _PG_init(void)
NULL,
NULL);
+ DefineCustomIntVariable("plv8.debugger_port",
+ gettext_noop("V8 remote debug port."),
+ gettext_noop("The default value is 35432. "
+ "This is effective only if PLV8 is built with ENABLE_DEBUGGER_SUPPORT."),
+ &plv8_debugger_port,
+ 35432, 0, 65536,
+ PGC_USERSET, 0,
+#if PG_VERSION_NUM >= 90100
+ NULL,
+#endif
+ NULL,
+ NULL);
+
RegisterXactCallback(plv8_xact_cb, NULL);
EmitWarningsOnPlaceholders("plv8");
@@ -236,6 +273,9 @@ common_pl_call_handler(PG_FUNCTION_ARGS, Dialect dialect) throw()
try
{
+#ifdef ENABLE_DEBUGGER_SUPPORT
+ Locker lock;
+#endif // ENABLE_DEBUGGER_SUPPORT
HandleScope handle_scope;
if (!fcinfo->flinfo->fn_extra)
@@ -292,6 +332,9 @@ common_pl_inline_handler(PG_FUNCTION_ARGS, Dialect dialect) throw()
try
{
+#ifdef ENABLE_DEBUGGER_SUPPORT
+ Locker lock;
+#endif // ENABLE_DEBUGGER_SUPPORT
HandleScope handle_scope;
char *source_text = codeblock->source_text;
@@ -676,6 +719,9 @@ common_pl_call_validator(PG_FUNCTION_ARGS, Dialect dialect) throw()
try
{
+#ifdef ENABLE_DEBUGGER_SUPPORT
+ Locker lock;
+#endif // ENABLE_DEBUGGER_SUPPORT
plv8_proc *proc = Compile(fn_oid, fcinfo->flinfo->fn_mcxt,
true, is_trigger, dialect);
(void) CreateExecEnv(proc->cache->function);
@@ -1236,6 +1282,16 @@ GetGlobalContext()
throw js_error(try_catch);
}
}
+
+#ifdef ENABLE_DEBUGGER_SUPPORT
+ debug_message_context = v8::Persistent<v8::Context>::New(global_context);
+
+ v8::Locker locker;
+
+ v8::Debug::SetDebugMessageDispatchHandler(DispatchDebugMessages, true);
+
+ v8::Debug::EnableAgent("plv8", plv8_debugger_port, false);
+#endif // ENABLE_DEBUGGER_SUPPORT
}
return global_context;
View
3 plv8.h
@@ -10,6 +10,9 @@
#include "plv8_config.h"
#include <v8.h>
+#ifdef ENABLE_DEBUGGER_SUPPORT
+#include <v8-debug.h>
+#endif // ENABLE_DEBUGGER_SUPPORT
#include <vector>
extern "C" {

0 comments on commit a9c0580

Please sign in to comment.
Something went wrong with that request. Please try again.