PageRenderTime 625ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/xdebug_tracing.c

http://github.com/derickr/xdebug
C | 174 lines | 137 code | 19 blank | 18 comment | 29 complexity | 676cdb94e7dbe8745aaff6134e0ef98d MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Xdebug |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2002-2016 Derick Rethans |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 1.0 of the Xdebug license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available at through the world-wide-web at |
  10. | http://xdebug.derickrethans.nl/license.php |
  11. | If you did not receive a copy of the Xdebug license and are unable |
  12. | to obtain it through the world-wide-web, please send a note to |
  13. | xdebug@derickrethans.nl so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Derick Rethans <derick@xdebug.org> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include "php_xdebug.h"
  19. #include "xdebug_private.h"
  20. #include "xdebug_str.h"
  21. #include "xdebug_tracing.h"
  22. #include "xdebug_var.h"
  23. #include "ext/standard/php_string.h"
  24. #include "xdebug_compat.h"
  25. #include "xdebug_tracing.h"
  26. #include "xdebug_trace_textual.h"
  27. #include "xdebug_trace_computerized.h"
  28. #include "xdebug_trace_html.h"
  29. ZEND_EXTERN_MODULE_GLOBALS(xdebug)
  30. xdebug_trace_handler_t *xdebug_select_trace_handler(int options TSRMLS_DC)
  31. {
  32. xdebug_trace_handler_t *tmp;
  33. switch (XG(trace_format)) {
  34. case 0: tmp = &xdebug_trace_handler_textual; break;
  35. case 1: tmp = &xdebug_trace_handler_computerized; break;
  36. case 2: tmp = &xdebug_trace_handler_html; break;
  37. default:
  38. php_error(E_NOTICE, "A wrong value for xdebug.trace_format was selected (%d), defaulting to the textual format.", (int) XG(trace_format));
  39. tmp = &xdebug_trace_handler_textual; break;
  40. }
  41. if (options & XDEBUG_TRACE_OPTION_COMPUTERIZED) {
  42. tmp = &xdebug_trace_handler_computerized;
  43. }
  44. if (options & XDEBUG_TRACE_OPTION_HTML) {
  45. tmp = &xdebug_trace_handler_html;
  46. }
  47. return tmp;
  48. }
  49. FILE *xdebug_trace_open_file(char *fname, long options, char **used_fname TSRMLS_DC)
  50. {
  51. FILE *file;
  52. char *filename;
  53. if (fname && strlen(fname)) {
  54. filename = xdstrdup(fname);
  55. } else {
  56. if (!strlen(XG(trace_output_name)) ||
  57. xdebug_format_output_filename(&fname, XG(trace_output_name), NULL) <= 0
  58. ) {
  59. /* Invalid or empty xdebug.trace_output_name */
  60. return NULL;
  61. }
  62. if (IS_SLASH(XG(trace_output_dir)[strlen(XG(trace_output_dir)) - 1])) {
  63. filename = xdebug_sprintf("%s%s", XG(trace_output_dir), fname);
  64. } else {
  65. filename = xdebug_sprintf("%s%c%s", XG(trace_output_dir), DEFAULT_SLASH, fname);
  66. }
  67. xdfree(fname);
  68. }
  69. if (options & XDEBUG_TRACE_OPTION_APPEND) {
  70. file = xdebug_fopen(filename, "a", (options & XDEBUG_TRACE_OPTION_NAKED_FILENAME) ? NULL : "xt", used_fname);
  71. } else {
  72. file = xdebug_fopen(filename, "w", (options & XDEBUG_TRACE_OPTION_NAKED_FILENAME) ? NULL : "xt", used_fname);
  73. }
  74. xdfree(filename);
  75. return file;
  76. }
  77. char* xdebug_start_trace(char* fname, long options TSRMLS_DC)
  78. {
  79. XG(trace_handler) = xdebug_select_trace_handler(options TSRMLS_CC);
  80. XG(trace_context) = (void*) XG(trace_handler)->init(fname, options TSRMLS_CC);
  81. if (XG(trace_context)) {
  82. XG(do_trace) = 1;
  83. XG(trace_handler)->write_header(XG(trace_context) TSRMLS_CC);
  84. return xdstrdup(XG(trace_handler)->get_filename(XG(trace_context) TSRMLS_CC));
  85. }
  86. return NULL;
  87. }
  88. void xdebug_stop_trace(TSRMLS_D)
  89. {
  90. XG(do_trace) = 0;
  91. if (XG(trace_context)) {
  92. XG(trace_handler)->write_footer(XG(trace_context) TSRMLS_CC);
  93. XG(trace_handler)->deinit(XG(trace_context) TSRMLS_CC);
  94. XG(trace_context) = NULL;
  95. }
  96. }
  97. PHP_FUNCTION(xdebug_start_trace)
  98. {
  99. char *fname = NULL;
  100. #if PHP_VERSION_ID >= 70000
  101. size_t fname_len = 0;
  102. #else
  103. int fname_len = 0;
  104. #endif
  105. char *trace_fname;
  106. zppLONG options = XG(trace_options);
  107. if (XG(do_trace) == 0) {
  108. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sl", &fname, &fname_len, &options) == FAILURE) {
  109. return;
  110. }
  111. if ((trace_fname = xdebug_start_trace(fname, options TSRMLS_CC)) != NULL) {
  112. XG(do_trace) = 1;
  113. #if PHP_VERSION_ID >= 70000
  114. RETVAL_STRING(trace_fname);
  115. #else
  116. RETVAL_STRING(trace_fname, 1);
  117. #endif
  118. xdfree(trace_fname);
  119. return;
  120. } else {
  121. php_error(E_NOTICE, "Trace could not be started");
  122. }
  123. XG(do_trace) = 0;
  124. RETURN_FALSE;
  125. } else {
  126. php_error(E_NOTICE, "Function trace already started");
  127. RETURN_FALSE;
  128. }
  129. }
  130. PHP_FUNCTION(xdebug_stop_trace)
  131. {
  132. if (XG(do_trace) == 1) {
  133. #if PHP_VERSION_ID >= 70000
  134. RETVAL_STRING(XG(trace_handler)->get_filename(XG(trace_context) TSRMLS_CC));
  135. #else
  136. RETVAL_STRING(XG(trace_handler)->get_filename(XG(trace_context) TSRMLS_CC), 1);
  137. #endif
  138. xdebug_stop_trace(TSRMLS_C);
  139. } else {
  140. RETVAL_FALSE;
  141. php_error(E_NOTICE, "Function trace was not started");
  142. }
  143. }
  144. PHP_FUNCTION(xdebug_get_tracefile_name)
  145. {
  146. if (XG(do_trace) == 1 && XG(trace_handler) && XG(trace_handler)->get_filename) {
  147. #if PHP_VERSION_ID >= 70000
  148. RETVAL_STRING(XG(trace_handler)->get_filename(XG(trace_context) TSRMLS_CC));
  149. #else
  150. RETVAL_STRING(XG(trace_handler)->get_filename(XG(trace_context) TSRMLS_CC), 1);
  151. #endif
  152. } else {
  153. RETURN_FALSE;
  154. }
  155. }