v8js_debug.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2015 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | http://www.opensource.org/licenses/mit-license.php MIT License |
  8. +----------------------------------------------------------------------+
  9. | Author: Stefan Siegl <[email protected]> |
  10. +----------------------------------------------------------------------+
  11. */
  12. #ifdef HAVE_CONFIG_H
  13. #include "config.h"
  14. #endif
  15. extern "C" {
  16. #include "php.h"
  17. }
  18. #include "php_v8js_macros.h"
  19. #include "v8js_debug.h"
  20. #ifdef ENABLE_DEBUGGER_SUPPORT
  21. v8js_ctx *v8js_debug_context;
  22. int v8js_debug_auto_break_mode;
  23. static void DispatchDebugMessages() { /* {{{ */
  24. if(v8js_debug_context == NULL) {
  25. return;
  26. }
  27. v8::Isolate* isolate = v8js_debug_context->isolate;
  28. v8::Isolate::Scope isolate_scope(isolate);
  29. v8::HandleScope handle_scope(isolate);
  30. v8::Local<v8::Context> context =
  31. v8::Local<v8::Context>::New(isolate, v8js_debug_context->context);
  32. v8::Context::Scope scope(context);
  33. v8::Debug::ProcessDebugMessages();
  34. }
  35. /* }}} */
  36. /* {{{ proto void V8Js::__destruct()
  37. __destruct for V8Js */
  38. PHP_METHOD(V8Js, __destruct)
  39. {
  40. v8js_ctx *c = (v8js_ctx *) zend_object_store_get_object(getThis() TSRMLS_CC);
  41. if(!c->isolate) {
  42. /* c->isolate is initialized by __construct, which wasn't called if this
  43. * instance was deserialized (which we already caught in __wakeup). */
  44. return;
  45. }
  46. V8JS_CTX_PROLOGUE(c);
  47. if(v8js_debug_context == c) {
  48. v8::Debug::DisableAgent();
  49. v8js_debug_context = NULL;
  50. }
  51. }
  52. /* }}} */
  53. /* {{{ proto bool V8Js::startDebugAgent(string agent_name[, int port[, int auto_break]])
  54. */
  55. PHP_METHOD(V8Js, startDebugAgent)
  56. {
  57. char *str = NULL;
  58. int str_len = 0;
  59. long port = 0, auto_break = 0;
  60. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sll", &str, &str_len, &port, &auto_break) == FAILURE) {
  61. return;
  62. }
  63. if(!port) {
  64. port = 9222;
  65. }
  66. V8JS_BEGIN_CTX(c, getThis());
  67. if(v8js_debug_context == c) {
  68. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Debug agent already started for this V8Js instance");
  69. RETURN_BOOL(0);
  70. }
  71. if(v8js_debug_context != NULL) {
  72. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Debug agent already started for a different V8Js instance");
  73. RETURN_BOOL(0);
  74. }
  75. v8js_debug_context = c;
  76. v8js_debug_auto_break_mode = auto_break;
  77. v8::Debug::SetDebugMessageDispatchHandler(DispatchDebugMessages, true);
  78. v8::Debug::EnableAgent(str_len ? str : "V8Js", port, auto_break > 0);
  79. if(auto_break) {
  80. /* v8::Debug::EnableAgent doesn't really do what we want it to do,
  81. since it only breaks processing on the default isolate.
  82. Hence just trigger another DebugBreak, no for our main isolate. */
  83. v8::Debug::DebugBreak(c->isolate);
  84. }
  85. RETURN_BOOL(1);
  86. }
  87. /* }}} */
  88. #endif /* ENABLE_DEBUGGER_SUPPORT */