1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
26 #include "php_psi_stdinc.h"
30 #include "ext/standard/info.h"
31 #include "zend_constants.h"
32 #include "zend_operators.h"
41 # define PSI_ENGINE "jit"
46 # define PSI_ENGINE "ffi"
49 ZEND_DECLARE_MODULE_GLOBALS(psi
);
52 STD_PHP_INI_ENTRY("psi.engine", PSI_ENGINE
, PHP_INI_SYSTEM
, OnUpdateString
, engine
, zend_psi_globals
, psi_globals
)
53 STD_PHP_INI_ENTRY("psi.directory", "psi.d", PHP_INI_SYSTEM
, OnUpdateString
, directory
, zend_psi_globals
, psi_globals
)
56 static zend_object_handlers psi_object_handlers
;
57 static zend_class_entry
*psi_class_entry
;
59 zend_class_entry
*psi_object_get_class_entry()
61 return psi_class_entry
;
64 static void psi_object_free(zend_object
*o
)
66 psi_object
*obj
= PSI_OBJ(NULL
, o
);
74 zend_object_std_dtor(o
);
77 zend_object
*psi_object_init_ex(zend_class_entry
*ce
, void *data
, void (*dtor
)(void *))
85 o
= ecalloc(1, sizeof(*o
) + zend_object_properties_size(ce
));
90 zend_object_std_init(&o
->std
, ce
);
91 object_properties_init(&o
->std
, ce
);
92 o
->std
.handlers
= &psi_object_handlers
;
96 zend_object
*psi_object_init(zend_class_entry
*ce
)
98 return psi_object_init_ex(ce
, NULL
, NULL
);
101 ZEND_BEGIN_ARG_INFO_EX(ai_psi_dump
, 0, 0, 0)
102 ZEND_ARG_INFO(0, stream
)
104 static PHP_FUNCTION(psi_dump
) {
107 int fd
= STDOUT_FILENO
;
109 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS(), "|r!", &r
)) {
113 php_stream_from_zval(s
, r
);
115 if (SUCCESS
!= php_stream_cast(s
, PHP_STREAM_AS_FD
| PHP_STREAM_CAST_INTERNAL
, (void **)&fd
, 1)) {
119 psi_context_dump(PSI_G(context
), fd
);
122 ZEND_BEGIN_ARG_INFO_EX(ai_psi_validate
, 0, 0, 1)
123 ZEND_ARG_INFO(0, file
)
125 static PHP_FUNCTION(psi_validate
) {
128 struct psi_data D
= {0};
130 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS(), "P", &file
)) {
134 if (!psi_parser_init(&P
, file
->val
, psi_error_wrapper
, 0)) {
138 while (0 < psi_parser_scan(&P
)) {
139 psi_parser_parse(&P
, psi_token_alloc(&P
));
140 if (P
.num
== PSI_T_EOF
) {
144 psi_parser_parse(&P
, NULL
);
146 psi_data_ctor(&D
, P
.error
, P
.flags
);
147 RETVAL_BOOL(psi_data_validate(&D
, PSI_DATA(&P
)) && !P
.errors
);
153 static PHP_MINIT_FUNCTION(psi
)
155 struct psi_context_ops
*ops
= NULL
;
156 zend_class_entry ce
= {0};
159 if (psi_check_env("PSI_DEBUG")) {
162 if (psi_check_env("PSI_SILENT")) {
166 REGISTER_INI_ENTRIES();
168 INIT_NS_CLASS_ENTRY(ce
, "psi", "object", NULL
);
169 psi_class_entry
= zend_register_internal_class_ex(&ce
, NULL
);
170 psi_class_entry
->create_object
= psi_object_init
;
172 memcpy(&psi_object_handlers
, zend_get_std_object_handlers(), sizeof(zend_object_handlers
));
173 psi_object_handlers
.offset
= XtOffsetOf(psi_object
, std
);
174 psi_object_handlers
.free_obj
= psi_object_free
;
175 psi_object_handlers
.clone_obj
= NULL
;
178 if (!strcasecmp(PSI_G(engine
), "jit")) {
179 ops
= psi_libjit_ops();
183 ops
= psi_libffi_ops();
187 php_error(E_WARNING
, "No PSI engine found");
191 PSI_G(context
) = psi_context_init(NULL
, ops
, psi_error_wrapper
, flags
);
192 psi_context_build(PSI_G(context
), PSI_G(directory
));
194 if (psi_check_env("PSI_DUMP")) {
195 psi_context_dump(PSI_G(context
), STDOUT_FILENO
);
201 static PHP_MSHUTDOWN_FUNCTION(psi
)
203 psi_context_free(&PSI_G(context
));
205 UNREGISTER_INI_ENTRIES();
210 #if defined(COMPILE_DL_PSI) && defined(ZTS)
211 static PHP_RINIT_FUNCTION(psi
)
213 ZEND_TSRMLS_CACHE_UPDATE();
218 static PHP_MINFO_FUNCTION(psi
)
220 php_info_print_table_start();
221 php_info_print_table_header(2, "PSI Support", "enabled");
222 php_info_print_table_row(2, "Extension Version", PHP_PSI_VERSION
);
223 php_info_print_table_end();
224 php_info_print_table_start();
225 php_info_print_table_header(3, "Used Library", "Compiled", "Linked");
226 php_info_print_table_row(3, "libffi",
227 #ifndef PHP_PSI_LIBFFI_VERSION
228 # define PHP_PSI_LIBFFI_VERSION "unknown"
231 PHP_PSI_LIBFFI_VERSION
, "unknown"
233 "disabled", "disabled"
236 php_info_print_table_row(3, "libjit",
240 "disabled", "disabled"
244 DISPLAY_INI_ENTRIES();
247 static const zend_function_entry psi_functions
[] = {
248 PHP_FE(psi_dump
, ai_psi_dump
)
249 PHP_FE(psi_validate
, ai_psi_validate
)
253 zend_module_entry psi_module_entry
= {
254 STANDARD_MODULE_HEADER
,
259 #if defined(COMPILE_DL_PSI) && defined(ZTS)
260 PHP_RINIT(psi
), /* Replace with NULL if there's nothing to do at request start */
267 STANDARD_MODULE_PROPERTIES
270 #ifdef COMPILE_DL_PSI
272 ZEND_TSRMLS_CACHE_DEFINE();
282 * vim600: noet sw=4 ts=4 fdm=marker
283 * vim<600: noet sw=4 ts=4