summaryrefslogtreecommitdiff
path: root/sapi/phpdbg/phpdbg_frame.c
blob: 3384a2c9d1d2d672771b96a81cf574df4adec9f2 (plain)
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
   +----------------------------------------------------------------------+
   | PHP Version 5                                                        |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2015 The PHP Group                                |
   +----------------------------------------------------------------------+
   | This source file is subject to version 3.01 of the PHP 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.php.net/license/3_01.txt                                  |
   | If you did not receive a copy of the PHP license and are unable to   |
   | obtain it through the world-wide-web, please send a note to          |
   | license@php.net so we can mail you a copy immediately.               |
   +----------------------------------------------------------------------+
   | Authors: Felipe Pena <felipe@php.net>                                |
   | Authors: Joe Watkins <joe.watkins@live.co.uk>                        |
   | Authors: Bob Weinand <bwoebi@php.net>                                |
   +----------------------------------------------------------------------+
*/

#include "zend.h"
#include "phpdbg.h"
#include "phpdbg_utils.h"
#include "phpdbg_frame.h"
#include "phpdbg_list.h"

ZEND_EXTERN_MODULE_GLOBALS(phpdbg);

void phpdbg_restore_frame(void) /* {{{ */
{
	if (PHPDBG_FRAME(num) == 0) {
		return;
	}

	PHPDBG_FRAME(num) = 0;

	/* move things back */
	EG(current_execute_data) = PHPDBG_FRAME(execute_data);

	EG(scope) = PHPDBG_EX(func)->op_array.scope;
} /* }}} */

void phpdbg_switch_frame(int frame) /* {{{ */
{
	zend_execute_data *execute_data = PHPDBG_FRAME(num)?PHPDBG_FRAME(execute_data):EG(current_execute_data);
	int i = 0;

	if (PHPDBG_FRAME(num) == frame) {
		phpdbg_notice("frame", "id=\"%d\"", "Already in frame #%d", frame);
		return;
	}

	phpdbg_try_access {
		while (execute_data) {
			if (i++ == frame) {
				break;
			}

			do {
				execute_data = execute_data->prev_execute_data;
			} while (execute_data && execute_data->opline == NULL);
		}
	} phpdbg_catch_access {
		phpdbg_error("signalsegv", "", "Couldn't switch frames, invalid data source");
		return;
	} phpdbg_end_try_access();

	if (execute_data == NULL) {
		phpdbg_error("frame", "type=\"maxnum\" id=\"%d\"", "No frame #%d", frame);
		return;
	}

	phpdbg_restore_frame();

	if (frame > 0) {
		PHPDBG_FRAME(num) = frame;

		/* backup things and jump back */
		PHPDBG_FRAME(execute_data) = EG(current_execute_data);
		EG(current_execute_data) = execute_data;

		EG(scope) = PHPDBG_EX(func)->op_array.scope;
	}

	phpdbg_notice("frame", "id=\"%d\"", "Switched to frame #%d", frame);

	{
		const char *file_chr = zend_get_executed_filename();
		zend_string *file = zend_string_init(file_chr, strlen(file_chr), 0);
		phpdbg_list_file(file, 3, zend_get_executed_lineno() - 1, zend_get_executed_lineno());
		efree(file);
	}
} /* }}} */

static void phpdbg_dump_prototype(zval *tmp) /* {{{ */
{
	zval *funcname, *class, class_zv, *type, *args, *argstmp;

	funcname = zend_hash_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("function"));

	if ((class = zend_hash_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("object")))) {
		ZVAL_NEW_STR(&class_zv, Z_OBJCE_P(class)->name);
		class = &class_zv;
	} else {
		class = zend_hash_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("class"));
	}

	if (class) {
		type = zend_hash_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("type"));
	}

	args = zend_hash_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("args"));

	phpdbg_xml(" symbol=\"%s%s%s\"", class ? Z_STRVAL_P(class) : "", class ? Z_STRVAL_P(type) : "", Z_STRVAL_P(funcname));

	if (args) {
		phpdbg_xml(">");
	} else {
		phpdbg_xml(" />");
	}

	phpdbg_out("%s%s%s(", class ? Z_STRVAL_P(class) : "", class ? Z_STRVAL_P(type) : "", Z_STRVAL_P(funcname));

	if (args) {
		const zend_function *func = NULL;
		const zend_arg_info *arginfo = NULL;
		zend_bool is_variadic = 0;
		int j = 0, m;

		phpdbg_try_access {
			/* assuming no autoloader call is necessary, class should have been loaded if it's in backtrace ... */
			if ((func = phpdbg_get_function(Z_STRVAL_P(funcname), class ? Z_STRVAL_P(class) : NULL))) {
				arginfo = func->common.arg_info;
			}
		} phpdbg_end_try_access();

		m = func ? func->common.num_args : 0;

		ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(args), argstmp) {
			if (j) {
				phpdbg_out(", ");
			}
			phpdbg_xml("<arg %r");
			if (m && j < m) {
				char *arg_name = NULL;

				if (arginfo) {
					if (func->type == ZEND_INTERNAL_FUNCTION) {
						arg_name = (char *)((zend_internal_arg_info *)&arginfo[j])->name;
					} else {
						arg_name = arginfo[j].name->val;
					}
				}

				if (!is_variadic) {
					is_variadic = arginfo ? arginfo[j].is_variadic : 0;
				}

				phpdbg_xml(" variadic=\"%s\" name=\"%s\">", is_variadic ? "variadic" : "", arg_name ? arg_name : "");
				phpdbg_out("%s=%s", arg_name ? arg_name : "?", is_variadic ? "[": "");

			} else {
				phpdbg_xml(">");
			}
			++j;

			zend_print_flat_zval_r(argstmp);

			phpdbg_xml("</arg>");
		} ZEND_HASH_FOREACH_END();

		if (is_variadic) {
			phpdbg_out("]");
		}
		phpdbg_xml("</frame>");
	}
	phpdbg_out(")");
}

void phpdbg_dump_backtrace(size_t num) /* {{{ */
{
	HashPosition position;
	zval zbacktrace;
	zval *tmp;
	zval *file, *line;
	int i = 0, limit = num;

	PHPDBG_OUTPUT_BACKUP();

	if (limit < 0) {
		phpdbg_error("backtrace", "type=\"minnum\"", "Invalid backtrace size %d", limit);

		PHPDBG_OUTPUT_BACKUP_RESTORE();
		return;
	}

	phpdbg_try_access {
		zend_fetch_debug_backtrace(&zbacktrace, 0, 0, limit);
	} phpdbg_catch_access {
		phpdbg_error("signalsegv", "", "Couldn't fetch backtrace, invalid data source");
		return;
	} phpdbg_end_try_access();

	phpdbg_xml("<backtrace %r>");

	zend_hash_internal_pointer_reset_ex(Z_ARRVAL(zbacktrace), &position);
	tmp = zend_hash_get_current_data_ex(Z_ARRVAL(zbacktrace), &position);
	while (1) {
		file = zend_hash_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("file"));
		line = zend_hash_str_find(Z_ARRVAL_P(tmp), ZEND_STRL("line"));
		zend_hash_move_forward_ex(Z_ARRVAL(zbacktrace), &position);

		if (!(tmp = zend_hash_get_current_data_ex(Z_ARRVAL(zbacktrace), &position))) {
			phpdbg_write("frame", "id=\"%d\" symbol=\"{main}\" file=\"%s\" line=\"%d\"", "frame #%d: {main} at %s:%ld", i, Z_STRVAL_P(file), Z_LVAL_P(line));
			break;
		}

		if (file) { /* userland */
			phpdbg_out("frame #%d: ", i);
			phpdbg_xml("<frame %r id=\"%d\" file=\"%s\" line=\"%d\"", i, Z_STRVAL_P(file), Z_LVAL_P(line));
			phpdbg_dump_prototype(tmp);
			phpdbg_out(" at %s:%ld\n", Z_STRVAL_P(file), Z_LVAL_P(line));
			i++;
		} else {
			phpdbg_out(" => ");
			phpdbg_xml("<frame %r id=\"%d\" internal=\"internal\"", i);
			phpdbg_dump_prototype(tmp);
			phpdbg_out(" (internal function)\n");
		}
	}

	phpdbg_out("\n");
	phpdbg_xml("</backtrace>");

	zval_dtor(&zbacktrace);

	PHPDBG_OUTPUT_BACKUP_RESTORE();
} /* }}} */