zend_dtrace.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 2.00 of the Zend license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.zend.com/license/2_00.txt. |
  11. | If you did not receive a copy of the Zend license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@zend.com so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: David Soria Parra <david.soriaparra@sun.com> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include "zend.h"
  19. #include "zend_API.h"
  20. #include "zend_dtrace.h"
  21. #ifdef HAVE_DTRACE
  22. ZEND_API zend_op_array *(*zend_dtrace_compile_file)(zend_file_handle *file_handle, int type);
  23. ZEND_API void (*zend_dtrace_execute)(zend_op_array *op_array);
  24. ZEND_API void (*zend_dtrace_execute_internal)(zend_execute_data *execute_data, zval *return_value);
  25. /* PHP DTrace probes {{{ */
  26. static inline const char *dtrace_get_executed_filename(void)
  27. {
  28. zend_execute_data *ex = EG(current_execute_data);
  29. while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) {
  30. ex = ex->prev_execute_data;
  31. }
  32. if (ex) {
  33. return ZSTR_VAL(ex->func->op_array.filename);
  34. } else {
  35. return zend_get_executed_filename();
  36. }
  37. }
  38. ZEND_API zend_op_array *dtrace_compile_file(zend_file_handle *file_handle, int type)
  39. {
  40. zend_op_array *res;
  41. DTRACE_COMPILE_FILE_ENTRY(ZSTR_VAL(file_handle->opened_path), ZSTR_VAL(file_handle->filename));
  42. res = compile_file(file_handle, type);
  43. DTRACE_COMPILE_FILE_RETURN(ZSTR_VAL(file_handle->opened_path), ZSTR_VAL(file_handle->filename));
  44. return res;
  45. }
  46. /* We wrap the execute function to have fire the execute-entry/return and function-entry/return probes */
  47. ZEND_API void dtrace_execute_ex(zend_execute_data *execute_data)
  48. {
  49. int lineno;
  50. const char *scope, *filename, *funcname, *classname;
  51. scope = filename = funcname = classname = NULL;
  52. /* we need filename and lineno for both execute and function probes */
  53. if (DTRACE_EXECUTE_ENTRY_ENABLED() || DTRACE_EXECUTE_RETURN_ENABLED()
  54. || DTRACE_FUNCTION_ENTRY_ENABLED() || DTRACE_FUNCTION_RETURN_ENABLED()) {
  55. filename = dtrace_get_executed_filename();
  56. lineno = zend_get_executed_lineno();
  57. }
  58. if (DTRACE_FUNCTION_ENTRY_ENABLED() || DTRACE_FUNCTION_RETURN_ENABLED()) {
  59. classname = get_active_class_name(&scope);
  60. funcname = get_active_function_name();
  61. }
  62. if (DTRACE_EXECUTE_ENTRY_ENABLED()) {
  63. DTRACE_EXECUTE_ENTRY((char *)filename, lineno);
  64. }
  65. if (DTRACE_FUNCTION_ENTRY_ENABLED() && funcname != NULL) {
  66. DTRACE_FUNCTION_ENTRY((char *)funcname, (char *)filename, lineno, (char *)classname, (char *)scope);
  67. }
  68. execute_ex(execute_data);
  69. if (DTRACE_FUNCTION_RETURN_ENABLED() && funcname != NULL) {
  70. DTRACE_FUNCTION_RETURN((char *)funcname, (char *)filename, lineno, (char *)classname, (char *)scope);
  71. }
  72. if (DTRACE_EXECUTE_RETURN_ENABLED()) {
  73. DTRACE_EXECUTE_RETURN((char *)filename, lineno);
  74. }
  75. }
  76. ZEND_API void dtrace_execute_internal(zend_execute_data *execute_data, zval *return_value)
  77. {
  78. int lineno;
  79. const char *filename;
  80. if (DTRACE_EXECUTE_ENTRY_ENABLED() || DTRACE_EXECUTE_RETURN_ENABLED()) {
  81. filename = dtrace_get_executed_filename();
  82. lineno = zend_get_executed_lineno();
  83. }
  84. if (DTRACE_EXECUTE_ENTRY_ENABLED()) {
  85. DTRACE_EXECUTE_ENTRY((char *)filename, lineno);
  86. }
  87. execute_internal(execute_data, return_value);
  88. if (DTRACE_EXECUTE_RETURN_ENABLED()) {
  89. DTRACE_EXECUTE_RETURN((char *)filename, lineno);
  90. }
  91. }
  92. void dtrace_error_notify_cb(int type, zend_string *error_filename, uint32_t error_lineno, zend_string *message)
  93. {
  94. if (DTRACE_ERROR_ENABLED()) {
  95. DTRACE_ERROR(ZSTR_VAL(message), ZSTR_VAL(error_filename), error_lineno);
  96. }
  97. }
  98. /* }}} */
  99. #endif /* HAVE_DTRACE */