@@ -30,6 +30,7 @@ extern char **environ;
3030namespace node {
3131
3232static int dash_dash_index = 0 ;
33+ static bool use_debug_agent = false ;
3334
3435enum encoding ParseEncoding (Handle<Value> encoding_v, enum encoding _default) {
3536 HandleScope scope;
@@ -325,6 +326,21 @@ static void EIOWantPoll(void) {
325326 ev_async_send (EV_DEFAULT_UC_ &eio_watcher);
326327}
327328
329+ static ev_async debug_watcher;
330+
331+ static void DebugMessageCallback (EV_P_ ev_async *watcher, int revents) {
332+ HandleScope scope;
333+ assert (watcher == &debug_watcher);
334+ assert (revents == EV_ASYNC );
335+ ExecuteString (String::New (" 1+1;" ),
336+ String::New (" debug_poll" ));
337+ }
338+
339+ static void DebugMessageDispatch (void ) {
340+ ev_async_send (EV_DEFAULT_UC_ &debug_watcher);
341+ }
342+
343+
328344static void ExecuteNativeJS (const char *filename, const char *data) {
329345 HandleScope scope;
330346 TryCatch try_catch;
@@ -434,18 +450,24 @@ static void CallExitHandler() {
434450static void PrintHelp () {
435451 printf (" Usage: node [options] [--] script.js [arguments] \n "
436452 " -v, --version print node's version\n "
453+ " --debug enable remote debugging\n " // TODO specify port
437454 " --cflags print pre-processor and compiler flags\n "
438455 " --v8-options print v8 command line options\n\n "
439456 " Documentation can be found at http://tinyclouds.org/node/api.html"
440457 " or with 'man node'\n " );
441458}
442459
443460static void ParseArgs (int *argc, char **argv) {
461+ // TODO use parse opts
444462 for (int i = 1 ; i < *argc; i++) {
445463 const char *arg = argv[i];
446464 if (strcmp (arg, " --" ) == 0 ) {
447465 dash_dash_index = i;
448466 break ;
467+ } else if (strcmp (arg, " --debug" ) == 0 ) {
468+ argv[i] = reinterpret_cast <const char *>(" " );
469+ use_debug_agent = true ;
470+ dash_dash_index = i;
449471 } else if (strcmp (arg, " --version" ) == 0 || strcmp (arg, " -v" ) == 0 ) {
450472 printf (" %s\n " , NODE_VERSION );
451473 exit (0 );
@@ -488,6 +510,20 @@ int main(int argc, char *argv[]) {
488510
489511 HandleScope handle_scope;
490512
513+ #define AUTO_BREAK_FLAG " --debugger_auto_break"
514+ if (node::use_debug_agent) {
515+ V8::SetFlagsFromString (AUTO_BREAK_FLAG , sizeof (AUTO_BREAK_FLAG ));
516+ ev_async_init (&node::debug_watcher, node::DebugMessageCallback);
517+ Debug::SetDebugMessageDispatchHandler (node::DebugMessageDispatch);
518+ ev_async_start (EV_DEFAULT_UC_ &node::debug_watcher);
519+ ev_unref (EV_DEFAULT_UC );
520+
521+ bool r = Debug::EnableAgent (" node " NODE_VERSION , 5858 );
522+ assert (r);
523+ printf (" debugger listening on port 5858\n "
524+ " Use 'd8 --remote_debugger' to access it.\n " );
525+ }
526+
491527 Local<FunctionTemplate> process_template = FunctionTemplate::New ();
492528
493529 // The global object / "process" is an instance of EventEmitter. For
0 commit comments