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