xref: /PHP-8.4/Zend/zend_dtrace.c (revision 953143d8)
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 
19 #include "zend.h"
20 #include "zend_API.h"
21 #include "zend_dtrace.h"
22 
23 #ifdef HAVE_DTRACE
24 
25 ZEND_API zend_op_array *(*zend_dtrace_compile_file)(zend_file_handle *file_handle, int type);
26 ZEND_API void (*zend_dtrace_execute)(zend_op_array *op_array);
27 ZEND_API void (*zend_dtrace_execute_internal)(zend_execute_data *execute_data, zval *return_value);
28 
29 /* PHP DTrace probes {{{ */
dtrace_get_executed_filename(void)30 static inline const char *dtrace_get_executed_filename(void)
31 {
32 	zend_execute_data *ex = EG(current_execute_data);
33 
34 	while (ex && (!ex->func || !ZEND_USER_CODE(ex->func->type))) {
35 		ex = ex->prev_execute_data;
36 	}
37 	if (ex) {
38 		return ZSTR_VAL(ex->func->op_array.filename);
39 	} else {
40 		return zend_get_executed_filename();
41 	}
42 }
43 
dtrace_compile_file(zend_file_handle * file_handle,int type)44 ZEND_API zend_op_array *dtrace_compile_file(zend_file_handle *file_handle, int type)
45 {
46 	zend_op_array *res;
47 	DTRACE_COMPILE_FILE_ENTRY(ZSTR_VAL(file_handle->opened_path), ZSTR_VAL(file_handle->filename));
48 	res = compile_file(file_handle, type);
49 	DTRACE_COMPILE_FILE_RETURN(ZSTR_VAL(file_handle->opened_path), ZSTR_VAL(file_handle->filename));
50 
51 	return res;
52 }
53 
54 /* We wrap the execute function to have fire the execute-entry/return and function-entry/return probes */
dtrace_execute_ex(zend_execute_data * execute_data)55 ZEND_API void dtrace_execute_ex(zend_execute_data *execute_data)
56 {
57 	int lineno;
58 	const char *scope, *filename, *funcname, *classname;
59 	scope = filename = funcname = classname = NULL;
60 
61 	/* we need filename and lineno for both execute and function probes */
62 	if (DTRACE_EXECUTE_ENTRY_ENABLED() || DTRACE_EXECUTE_RETURN_ENABLED()
63 		|| DTRACE_FUNCTION_ENTRY_ENABLED() || DTRACE_FUNCTION_RETURN_ENABLED()) {
64 		filename = dtrace_get_executed_filename();
65 		lineno = zend_get_executed_lineno();
66 	}
67 
68 	if (DTRACE_FUNCTION_ENTRY_ENABLED() || DTRACE_FUNCTION_RETURN_ENABLED()) {
69 		classname = get_active_class_name(&scope);
70 		funcname = get_active_function_name();
71 	}
72 
73 	if (DTRACE_EXECUTE_ENTRY_ENABLED()) {
74 		DTRACE_EXECUTE_ENTRY((char *)filename, lineno);
75 	}
76 
77 	if (DTRACE_FUNCTION_ENTRY_ENABLED() && funcname != NULL) {
78 		DTRACE_FUNCTION_ENTRY((char *)funcname, (char *)filename, lineno, (char *)classname, (char *)scope);
79 	}
80 
81 	execute_ex(execute_data);
82 
83 	if (DTRACE_FUNCTION_RETURN_ENABLED() && funcname != NULL) {
84 		DTRACE_FUNCTION_RETURN((char *)funcname, (char *)filename, lineno, (char *)classname, (char *)scope);
85 	}
86 
87 	if (DTRACE_EXECUTE_RETURN_ENABLED()) {
88 		DTRACE_EXECUTE_RETURN((char *)filename, lineno);
89 	}
90 }
91 
dtrace_execute_internal(zend_execute_data * execute_data,zval * return_value)92 ZEND_API void dtrace_execute_internal(zend_execute_data *execute_data, zval *return_value)
93 {
94 	int lineno;
95 	const char *filename;
96 	if (DTRACE_EXECUTE_ENTRY_ENABLED() || DTRACE_EXECUTE_RETURN_ENABLED()) {
97 		filename = dtrace_get_executed_filename();
98 		lineno = zend_get_executed_lineno();
99 	}
100 
101 	if (DTRACE_EXECUTE_ENTRY_ENABLED()) {
102 		DTRACE_EXECUTE_ENTRY((char *)filename, lineno);
103 	}
104 
105 	execute_internal(execute_data, return_value);
106 
107 	if (DTRACE_EXECUTE_RETURN_ENABLED()) {
108 		DTRACE_EXECUTE_RETURN((char *)filename, lineno);
109 	}
110 }
111 
dtrace_error_notify_cb(int type,zend_string * error_filename,uint32_t error_lineno,zend_string * message)112 void dtrace_error_notify_cb(int type, zend_string *error_filename, uint32_t error_lineno, zend_string *message)
113 {
114 	if (DTRACE_ERROR_ENABLED()) {
115 		DTRACE_ERROR(ZSTR_VAL(message), ZSTR_VAL(error_filename), error_lineno);
116 	}
117 }
118 
119 /* }}} */
120 
121 #endif /* HAVE_DTRACE */
122