]> granicus.if.org Git - php/commitdiff
- Add DTrace support.
authorDavid Soria Parra <dsp@php.net>
Sun, 19 Jul 2009 14:00:25 +0000 (14:00 +0000)
committerDavid Soria Parra <dsp@php.net>
Sun, 19 Jul 2009 14:00:25 +0000 (14:00 +0000)
12 files changed:
NEWS
Zend/zend.c
Zend/zend_dtrace.c [new file with mode: 0644]
Zend/zend_dtrace.d [new file with mode: 0644]
Zend/zend_dtrace.h [new file with mode: 0644]
Zend/zend_exceptions.c
Zend/zend_execute.c
Zend/zend_vm_def.h
Zend/zend_vm_execute.h
acinclude.m4
configure.in
main/main.c

diff --git a/NEWS b/NEWS
index 6b629c59338d84ac44b21a8595d660edeed23fac..e7387d478853e33a44f6e831418353b4d6f5d875 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -38,6 +38,7 @@ PHP                                                                        NEWS
 - Improved ext/dba:
   . Added support for Tokyo Cabinet. (Michael)
 
+- Added DTrace support. (David Soria Parra)
 - Added runtime JIT auto-globals fetching and caching. (Dmitry, Sara)
 - Added E_STRICT to E_ALL. (Dmitry)
 - Added "context" and "binary_pipes" params in "other_options" for proc_open().
index 112d66b16a2d2d76fd9866dba7eabf8db4205f32..e629160bf07937d2d156abb3f3cc4716a4e1834b 100644 (file)
@@ -30,6 +30,7 @@
 #include "zend_ini.h"
 #include "zend_vm.h"
 #include "zend_unicode.h"
+#include "zend_dtrace.h"
 
 #ifdef ZTS
 # define GLOBAL_FUNCTION_TABLE         global_function_table
@@ -1133,10 +1134,18 @@ int zend_startup(zend_utility_functions *utility_functions, char **extensions TS
        zend_getenv = utility_functions->getenv_function;
        zend_resolve_path = utility_functions->resolve_path_function;
 
+#if HAVE_SYS_SDT_H
+/* build with dtrace support */
+       zend_compile_file = dtrace_compile_file;
+       zend_execute = dtrace_execute;
+       zend_execute_internal = dtrace_execute_internal;
+#else
        zend_compile_file = compile_file;
-       zend_compile_string = compile_string;
        zend_execute = execute;
        zend_execute_internal = NULL;
+#endif /* HAVE_SYS_SDT_H */
+
+       zend_compile_string = compile_string;
        zend_throw_exception_hook = NULL;
 
        zend_init_opcodes_handlers();
@@ -1537,6 +1546,7 @@ ZEND_API void zend_error(int type, const char *format, ...) /* {{{ */
        zval *orig_user_error_handler;
        zend_bool in_compilation;
        zend_class_entry *saved_class_entry;
+       char dtrace_error_buffer[1024];
        TSRMLS_FETCH();
 
        /* Obtain relevant filename and lineno */
@@ -1581,6 +1591,12 @@ ZEND_API void zend_error(int type, const char *format, ...) /* {{{ */
 
        va_start(args, format);
 
+       if(DTRACE_ERROR_ENABLED()) {
+               vsprintf(dtrace_error_buffer, format, args);
+       }
+       DTRACE_ERROR(dtrace_error_buffer, error_filename, error_lineno);
+
+
        /* if we don't have a user defined error handler */
        if (!EG(user_error_handler)
                || !(EG(user_error_handler_error_reporting) & type)
diff --git a/Zend/zend_dtrace.c b/Zend/zend_dtrace.c
new file mode 100644 (file)
index 0000000..41bc0ef
--- /dev/null
@@ -0,0 +1,122 @@
+/*
+   +----------------------------------------------------------------------+
+   | Zend Engine                                                          |
+   +----------------------------------------------------------------------+
+   | Copyright (c) 1998-2009 Zend Technologies Ltd. (http://www.zend.com) |
+   +----------------------------------------------------------------------+
+   | This source file is subject to version 2.00 of the Zend license,     |
+   | that is bundled with this package in the file LICENSE, and is        |
+   | available through the world-wide-web at the following url:           |
+   | http://www.zend.com/license/2_00.txt.                                |
+   | If you did not receive a copy of the Zend license and are unable to  |
+   | obtain it through the world-wide-web, please send a note to          |
+   | license@zend.com so we can mail you a copy immediately.              |
+   +----------------------------------------------------------------------+
+   | Authors: David Soria Parra <david.soriaparra@sun.com>                |
+   +----------------------------------------------------------------------+
+*/
+
+/* $Id$ */
+
+#include "zend.h"
+#include "zend_API.h"
+#include "zend_dtrace.h"
+
+#ifdef HAVE_SYS_SDT_H
+/* PHP DTrace probes {{{ */
+static inline char *dtrace_get_executed_filename(TSRMLS_D)
+{
+       if (EG(current_execute_data) && EG(current_execute_data)->op_array) {
+               return EG(current_execute_data)->op_array->filename;
+       } else {
+               return zend_get_executed_filename(TSRMLS_C);
+       }
+}
+
+ZEND_API zend_op_array *dtrace_compile_file(zend_file_handle *file_handle, int type TSRMLS_DC)
+{
+       zend_op_array *res;
+       DTRACE_COMPILE_FILE_ENTRY(file_handle->opened_path, file_handle->filename);
+       res = compile_file(file_handle, type TSRMLS_CC);
+       DTRACE_COMPILE_FILE_RETURN(file_handle->opened_path, file_handle->filename);
+
+       return res;
+}
+
+/* We wrap the execute function to have fire the execute-entry/return and function-entry/return probes */
+ZEND_API void dtrace_execute(zend_op_array *op_array TSRMLS_DC)
+{
+       int lineno, s_funcname_len, s_classname_len;
+       char *scope, *filename, *s_funcname, *s_classname;
+       zstr classname, funcname;
+
+       scope = filename = s_funcname = s_classname = NULL;
+       classname = funcname = EMPTY_ZSTR;
+
+       /* we need filename and lineno for both execute and function probes */
+       if (DTRACE_EXECUTE_ENTRY_ENABLED() || DTRACE_EXECUTE_RETURN_ENABLED()
+               || DTRACE_FUNCTION_ENTRY_ENABLED() || DTRACE_FUNCTION_RETURN_ENABLED()) {
+               filename = dtrace_get_executed_filename(TSRMLS_C);
+               lineno = zend_get_executed_lineno(TSRMLS_C);
+       }
+
+       if (DTRACE_FUNCTION_ENTRY_ENABLED() || DTRACE_FUNCTION_RETURN_ENABLED()) {
+               funcname = get_active_function_name(TSRMLS_C);
+               classname = get_active_class_name(&scope TSRMLS_CC);
+
+               /* We encode everything to utf8 so that we have a predictable output */
+               if (funcname.u != NULL)
+                       zend_unicode_to_string(ZEND_U_CONVERTER(UG(utf8_conv)), &s_funcname, &s_funcname_len, funcname.u, u_strlen(funcname.u) TSRMLS_CC);
+
+               /* classname might be EMPTY_STR, in that case we have pass NULL to the probe, so that
+                  we use appropriate predicates in our dtrace scripts to detect if we are in class context */
+               if (u_strlen(classname.u) > 0)
+                       zend_unicode_to_string(ZEND_U_CONVERTER(UG(utf8_conv)), &s_classname, &s_classname_len, classname.u, u_strlen(classname.u) TSRMLS_CC);
+       }
+
+       if (DTRACE_EXECUTE_ENTRY_ENABLED()) {
+               DTRACE_EXECUTE_ENTRY(filename, lineno);
+       }
+
+       if (DTRACE_FUNCTION_ENTRY_ENABLED() && funcname.u != NULL) {
+               DTRACE_FUNCTION_ENTRY(s_funcname, filename, lineno, s_classname, scope);
+       }
+
+       execute(op_array TSRMLS_CC);
+
+       if (DTRACE_FUNCTION_RETURN_ENABLED() && funcname.u != NULL) {
+               DTRACE_FUNCTION_RETURN(s_funcname, filename, lineno, s_classname, scope);
+       }
+
+       if (DTRACE_EXECUTE_RETURN_ENABLED()) {
+               DTRACE_EXECUTE_RETURN(filename, lineno);
+       }
+
+       if (DTRACE_FUNCTION_ENTRY_ENABLED() || DTRACE_FUNCTION_RETURN_ENABLED()) {
+               /* s_funcname and s_classname were allocated by zend_unicode_to_string */
+               if (s_funcname != NULL)
+                       efree(s_funcname);
+               if (s_classname != NULL)
+                       efree(s_classname);
+       }
+}
+
+ZEND_API void dtrace_execute_internal(zend_execute_data *execute_data_ptr, int return_value_used TSRMLS_DC)
+{
+       int lineno;
+       char *filename;
+       if (DTRACE_EXECUTE_ENTRY_ENABLED() || DTRACE_EXECUTE_RETURN_ENABLED()) {
+               filename = dtrace_get_executed_filename(TSRMLS_C);
+               lineno = zend_get_executed_lineno(TSRMLS_C);
+       }
+
+       DTRACE_EXECUTE_ENTRY(filename, lineno);
+
+       execute_internal(execute_data_ptr, return_value_used TSRMLS_CC);
+
+       DTRACE_EXECUTE_RETURN(filename, lineno);
+}
+
+/* }}} */
+#endif
+
diff --git a/Zend/zend_dtrace.d b/Zend/zend_dtrace.d
new file mode 100644 (file)
index 0000000..73ee562
--- /dev/null
@@ -0,0 +1,17 @@
+provider php {
+       probe exception__caught(char *classname);
+       probe exception__thrown(char* classname);
+       probe request__startup(char* request_file, char* request_uri, char* request_method);
+       probe request__shutdown(char* request_file, char* request_uri, char* request_method);
+       probe compile__file__entry(char * compile_file, char *compile_file_translated);
+       probe compile__file__return(char *compile_file, char *compile_file_translated);
+       probe error(char *errormsg, char *request_file, int lineno);
+       probe execute__entry(char* request_file, int lineno);
+       probe execute__return(char* request_file, int lineno);
+       probe function__entry(char* function_name, char* request_file, int lineno, char* classname, char* scope);
+       probe function__return(char* function_name, char* request_file, int lineno, char* classname, char* scope);
+};
+
+#pragma D attributes Evolving/Evolving/Common provider php provider
+#pragma D attributes Private/Private/Unknown provider php module
+#pragma D attributes Private/Private/Unknown provider php function
diff --git a/Zend/zend_dtrace.h b/Zend/zend_dtrace.h
new file mode 100644 (file)
index 0000000..9268894
--- /dev/null
@@ -0,0 +1,55 @@
+/*
+ * Generated by dtrace(1M).
+ */
+
+#ifndef        _ZEND_DTRACE_H
+#define        _ZEND_DTRACE_H
+
+#include <unistd.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef HAVE_SYS_SDT_H
+ZEND_API zend_op_array *(*zend_dtrace_compile_file)(zend_file_handle *file_handle, int type TSRMLS_DC);
+ZEND_API void (*zend_dtrace_execute)(zend_op_array *op_array TSRMLS_DC);
+ZEND_API void (*zend_dtrace_execute_internal)(zend_execute_data *execute_data_ptr, int return_value_used TSRMLS_DC);
+
+ZEND_API zend_op_array *dtrace_compile_file(zend_file_handle *file_handle, int type TSRMLS_DC);
+ZEND_API void dtrace_execute(zend_op_array *op_array TSRMLS_DC);
+ZEND_API void dtrace_execute_internal(zend_execute_data *execute_data_ptr, int return_value_used TSRMLS_DC);
+#include <zend_dtrace_gen.h>
+
+#else
+
+#define DTRACE_COMPILE_FILE_ENTRY(arg0, arg1)
+#define DTRACE_COMPILE_FILE_ENTRY_ENABLED() (0)
+#define DTRACE_COMPILE_FILE_RETURN(arg0, arg1)
+#define DTRACE_COMPILE_FILE_RETURN_ENABLED() (0)
+#define DTRACE_ERROR(arg0, arg1, arg2)
+#define DTRACE_ERROR_ENABLED() (0)
+#define DTRACE_EXCEPTION_CAUGHT(arg0)
+#define DTRACE_EXCEPTION_CAUGHT_ENABLED() (0)
+#define DTRACE_EXCEPTION_THROWN(arg0)
+#define DTRACE_EXCEPTION_THROWN_ENABLED() (0)
+#define DTRACE_EXECUTE_ENTRY(arg0, arg1)
+#define DTRACE_EXECUTE_ENTRY_ENABLED() (0)
+#define DTRACE_EXECUTE_RETURN(arg0, arg1)
+#define DTRACE_EXECUTE_RETURN_ENABLED() (0)
+#define DTRACE_FUNCTION_ENTRY(arg0, arg1, arg2, arg3, arg4)
+#define DTRACE_FUNCTION_ENTRY_ENABLED() (0)
+#define DTRACE_FUNCTION_RETURN(arg0, arg1, arg2, arg3, arg4)
+#define DTRACE_FUNCTION_RETURN_ENABLED() (0)
+#define DTRACE_REQUEST_SHUTDOWN(arg0, arg1, arg2)
+#define DTRACE_REQUEST_SHUTDOWN_ENABLED() (0)
+#define DTRACE_REQUEST_STARTUP(arg0, arg1, arg2)
+#define DTRACE_REQUEST_STARTUP_ENABLED() (0)
+
+#endif /* HAVE_SYS_SDT */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _ZEND_DTRACE_H */
index 519ab6ee3829fc54247e322ae4bb135f8df0ab36..8a196248543fb50cc841b45033c109f650c543a2 100644 (file)
@@ -27,6 +27,7 @@
 #include "zend_interfaces.h"
 #include "zend_exceptions.h"
 #include "zend_vm.h"
+#include "zend_dtrace.h"
 
 zend_class_entry *default_exception_ce;
 zend_class_entry *error_exception_ce;
@@ -82,6 +83,21 @@ void zend_exception_restore(TSRMLS_D) /* {{{ */
 
 void zend_throw_exception_internal(zval *exception TSRMLS_DC) /* {{{ */
 {
+       if (DTRACE_EXCEPTION_THROWN_ENABLED()) {
+               zstr classname;
+               char *s_classname;
+               int name_len, s_classname_len;
+
+               classname = NULL_ZSTR;
+               zend_get_object_classname(exception, &classname, &name_len);
+               zend_unicode_to_string(ZEND_U_CONVERTER(UG(utf8_conv)), &s_classname, &s_classname_len, classname.u, u_strlen(classname.u) TSRMLS_CC);
+
+               DTRACE_EXCEPTION_THROWN(s_classname);
+
+               efree(classname.v);
+               efree(s_classname);
+       }
+
        if (exception != NULL) {
                zval *previous = EG(exception);
                zend_exception_set_previous(exception, EG(exception) TSRMLS_CC);
index 5b80aa54ca07d84291cfba349c631af608075c90..aa592fd4605cf5fe1065018abda52ffea4c8ba51 100644 (file)
@@ -37,6 +37,7 @@
 #include "zend_closures.h"
 #include "zend_vm.h"
 #include "zend_unicode.h"
+#include "zend_dtrace.h"
 
 /* Virtual current working directory support */
 #include "tsrm_virtual_cwd.h"
index 5db41a380454eff7d66d3f3d831bb097b7b411c9..a839a256a155e25ef7cb69ff87a8c9b5c12de3a8 100644 (file)
@@ -2626,6 +2626,15 @@ ZEND_VM_HANDLER(107, ZEND_CATCH, ANY, CV)
                ZEND_VM_CONTINUE(); /* CHECK_ME */
        }
        ce = Z_OBJCE_P(EG(exception));
+
+       if (DTRACE_EXCEPTION_CAUGHT_ENABLED()) {
+               char *s_classname;
+               int s_classname_len;
+               zend_unicode_to_string(ZEND_U_CONVERTER(UG(utf8_conv)), &s_classname, &s_classname_len, ce->name.u, u_strlen(ce->name.u) TSRMLS_CC);
+               DTRACE_EXCEPTION_CAUGHT(s_classname);
+               efree(s_classname);
+       }
+
        if (ce != EX_T(opline->op1.u.var).class_entry) {
                if (!instanceof_function(ce, EX_T(opline->op1.u.var).class_entry TSRMLS_CC)) {
                        if (opline->op1.u.EA.type) {
index e1f54d57c646dca58f86c72dbfb1bd4cd7bac5d8..8395db4c82b2641dab92241b8ec66173efd739e9 100644 (file)
@@ -1190,6 +1190,15 @@ static int ZEND_FASTCALL  ZEND_CATCH_SPEC_CV_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
                ZEND_VM_CONTINUE(); /* CHECK_ME */
        }
        ce = Z_OBJCE_P(EG(exception));
+
+       if (DTRACE_EXCEPTION_CAUGHT_ENABLED()) {
+               char *s_classname;
+               int s_classname_len;
+               zend_unicode_to_string(ZEND_U_CONVERTER(UG(utf8_conv)), &s_classname, &s_classname_len, ce->name.u, u_strlen(ce->name.u) TSRMLS_CC);
+               DTRACE_EXCEPTION_CAUGHT(s_classname);
+               efree(s_classname);
+       }
+
        if (ce != EX_T(opline->op1.u.var).class_entry) {
                if (!instanceof_function(ce, EX_T(opline->op1.u.var).class_entry TSRMLS_CC)) {
                        if (opline->op1.u.EA.type) {
index 486d28d0b45a9ea89dc543320cabe6d826da8cc7..46d6a0090cbbce279261057e1708bab6ed92dec5 100644 (file)
@@ -2874,3 +2874,58 @@ main()
   fi
 ])
 
+dnl
+dnl Generate dtrace targets
+dnl
+AC_DEFUN([PHP_GENERATE_DTRACE],[
+  old_IFS=[$]IFS
+  IFS=.
+  set $ac_src
+  IFS=$old_IFS
+  build_target=$2
+  PHP_GLOBAL_OBJS="[$]PHP_GLOBAL_OBJS $1.o"
+  for src in $PHP_DTRACE_OBJS; do
+    case [$]build_target in
+      program|static)
+        obj="$obj `dirname $src`/`basename $src | sed 's,\.lo$,.o,'` " ;;
+      *)
+        obj="$obj `dirname $src`/.libs/`basename $src | sed 's,\.lo$,.o,'` " ;;
+    esac
+  done
+
+  cat >>Makefile.objects<<EOF
+$1.o: \$(PHP_DTRACE_OBJS)
+       dtrace -G -o $1.o -s $1 $obj
+EOF
+
+])
+
+dnl
+dnl Link given source files with dtrace
+dnl PHP_ADD_DTRACE(providerdesc, sources, module)
+dnl
+AC_DEFUN([PHP_ADD_DTRACE],[
+   case "$3" in
+    ""[)] unset ac_bdir;;
+    /*[)] ac_bdir=$ac_srcdir;;
+    *[)] extdir=PHP_EXT_DIR($3); ac_bdir="$extdir/";;
+    esac
+  old_IFS=[$]IFS
+  for ac_src in $2; do
+    IFS=.
+    set $ac_src
+    ac_obj=[$]1
+    IFS=$old_IFS
+
+    PHP_DTRACE_OBJS="[$]PHP_DTRACE_OBJS [$]ac_bdir[$]ac_obj.lo"
+  done;
+])
+
+dnl
+dnl Generate platform specific dtrace header
+dnl
+AC_DEFUN([PHP_INIT_DTRACE], [
+  dtrace -h -C -s $1 -o $2
+  $SED -ibak 's,PHP_,DTRACE_,g' $2
+])
+
index 747771bc50ca97929819b2ee7ee885c3d4cbe2b1..4dc7bd888bd9308699e6f3f83a995343053e845c 100644 (file)
@@ -895,6 +895,22 @@ if test "$PHP_IPV6" != "no" && test "$ac_cv_ipv6_support" = yes; then
   AC_DEFINE(HAVE_IPV6, 1, [Whether to enable IPv6 support])
 fi
 
+dnl ## DTRACE CHECHKS
+dnl ## this needs to be done before SAPI configureation
+PHP_ARG_ENABLE(dtrace, whether to enable DTrace support,
+[  --enable-dtrace         Enable DTrace support], no, no)
+
+dnl ## DTRACE CHECHKS
+dnl ## this needs to be done before SAPI configureation
+if test "$PHP_DTRACE" = "yes"; then
+  AC_CHECK_HEADERS([sys/sdt.h],
+    [PHP_ADD_DTRACE([Zend/zend_dtrace.d], [main/main.c, Zend/zend_API.c \
+       Zend/zend_execute.c Zend/zend_exceptions.c \
+       Zend/zend_dtrace.c Zend/zend.c])
+     PHP_INIT_DTRACE([Zend/zend_dtrace.d], [Zend/zend_dtrace_gen.h])],
+    [])
+fi
+
 AC_MSG_CHECKING([how big to make fd sets])
 PHP_ARG_ENABLE(fd-setsize,,
 [  --enable-fd-setsize     Set size of descriptor sets], no, no)
@@ -1163,6 +1179,7 @@ PHP_SUBST_OLD(PHP_INSTALLED_SAPIS)
 PHP_SUBST(PHP_CLI_TARGET)
 PHP_SUBST(PHP_SAPI_OBJS)
 PHP_SUBST(PHP_CLI_OBJS)
+PHP_SUBST(PHP_DTRACE_OBJS)
 PHP_SUBST(PHP_GLOBAL_OBJS)
 
 PHP_SUBST(PHP_MODULES)
@@ -1352,7 +1369,7 @@ esac
 PHP_ADD_SOURCES(Zend, \
     zend_language_parser.c zend_language_scanner.c \
     zend_ini_parser.c zend_ini_scanner.c \
-    zend_alloc.c zend_compile.c zend_constants.c \
+    zend_alloc.c zend_compile.c zend_constants.c zend_dtrace.c \
     zend_execute_API.c zend_unicode.c zend_highlight.c zend_llist.c \
     zend_opcode.c zend_operators.c zend_ptr_stack.c zend_stack.c \
     zend_variables.c zend.c zend_API.c zend_extensions.c zend_hash.c \
@@ -1384,6 +1401,13 @@ PHP_ADD_MAKEFILE_FRAGMENT($abs_srcdir/scripts/Makefile.frag,$abs_srcdir/scripts,
 PHP_ADD_MAKEFILE_FRAGMENT($abs_srcdir/Makefile.frag,$abs_srcdir/Zend,Zend)
 PHP_ADD_MAKEFILE_FRAGMENT($abs_srcdir/Zend/Makefile.frag,$abs_srcdir/Zend,Zend)
 
+if test "$PHP_DTRACE" = "yes"; then
+  case $host_alias in
+    *solaris*)
+      PHP_GENERATE_DTRACE([Zend/zend_dtrace.d],$php_build_target);;
+  esac
+fi
+
 PHP_GEN_BUILD_DIRS
 PHP_GEN_GLOBAL_MAKEFILE
 
index 7c58ae3b9df3827a5052762298d2190a5f6b010e..d79c2c3fd57c6c2be603ef0d3ed1a8cbff530a0a 100644 (file)
@@ -81,6 +81,7 @@
 #include "zend_indent.h"
 #include "zend_extensions.h"
 #include "zend_ini.h"
+#include "zend_dtrace.h"
 
 #include "php_content_types.h"
 #include "php_ticks.h"
@@ -1440,6 +1441,8 @@ int php_request_startup(TSRMLS_D)
 {
        int retval = SUCCESS;
 
+       DTRACE_REQUEST_STARTUP(SAFE_FILENAME(SG(request_info).path_translated), SAFE_FILENAME(SG(request_info).request_uri), SAFE_FILENAME(SG(request_info).request_method));
+
 #ifdef PHP_WIN32
        PG(com_initialized) = 0;
 #endif
@@ -1720,6 +1723,8 @@ void php_request_shutdown(void *dummy)
                PG(com_initialized) = 0;
        }
 #endif
+
+       DTRACE_REQUEST_SHUTDOWN(SAFE_FILENAME(SG(request_info).path_translated), SAFE_FILENAME(SG(request_info).request_uri), SAFE_FILENAME(SG(request_info).request_method));
 }
 /* }}} */