summaryrefslogtreecommitdiff
path: root/sapi/phpdbg/phpdbg_list.c
blob: a1403dbc30a05bf15ea95c6ef976f9312ecc3235 (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
   +----------------------------------------------------------------------+
   | PHP Version 7                                                        |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2016 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 <stdio.h>
#include <string.h>
#include <sys/stat.h>
#ifndef _WIN32
#	include <sys/mman.h>
#	include <unistd.h>
#endif
#include <fcntl.h>
#include "phpdbg.h"
#include "phpdbg_list.h"
#include "phpdbg_utils.h"
#include "phpdbg_prompt.h"
#include "php_streams.h"
#include "zend_exceptions.h"

ZEND_EXTERN_MODULE_GLOBALS(phpdbg)

#define PHPDBG_LIST_COMMAND_D(f, h, a, m, l, s, flags) \
	PHPDBG_COMMAND_D_EXP(f, h, a, m, l, s, &phpdbg_prompt_commands[12], flags)

const phpdbg_command_t phpdbg_list_commands[] = {
	PHPDBG_LIST_COMMAND_D(lines,     "lists the specified lines",    'l', list_lines,  NULL, "l", PHPDBG_ASYNC_SAFE),
	PHPDBG_LIST_COMMAND_D(class,     "lists the specified class",    'c', list_class,  NULL, "s", PHPDBG_ASYNC_SAFE),
	PHPDBG_LIST_COMMAND_D(method,    "lists the specified method",   'm', list_method, NULL, "m", PHPDBG_ASYNC_SAFE),
	PHPDBG_LIST_COMMAND_D(func,      "lists the specified function", 'f', list_func,   NULL, "s", PHPDBG_ASYNC_SAFE),
	PHPDBG_END_COMMAND
};

PHPDBG_LIST(lines) /* {{{ */
{
	if (!PHPDBG_G(exec) && !zend_is_executing()) {
		phpdbg_error("inactive", "type=\"execution\"", "Not executing, and execution context not set");
		return SUCCESS;
	}

	switch (param->type) {
		case NUMERIC_PARAM: {
			const char *char_file = phpdbg_current_file();
			zend_string *file = zend_string_init(char_file, strlen(char_file), 0);
			phpdbg_list_file(file, param->num < 0 ? 1 - param->num : param->num, (param->num < 0 ? param->num : 0) + zend_get_executed_lineno(), 0);
			efree(file);
		} break;

		case FILE_PARAM: {
			zend_string *file = zend_string_init(param->file.name, strlen(param->file.name), 0);
			phpdbg_list_file(file, param->file.line, 0, 0);
			efree(file);
		} break;

		phpdbg_default_switch_case();
	}

	return SUCCESS;
} /* }}} */

PHPDBG_LIST(func) /* {{{ */
{
	phpdbg_list_function_byname(param->str, param->len);

	return SUCCESS;
} /* }}} */

PHPDBG_LIST(method) /* {{{ */
{
	zend_class_entry *ce;

	if (phpdbg_safe_class_lookup(param->method.class, strlen(param->method.class), &ce) == SUCCESS) {
		zend_function *function;
		char *lcname = zend_str_tolower_dup(param->method.name, strlen(param->method.name));

		if ((function = zend_hash_str_find_ptr(&ce->function_table, lcname, strlen(lcname)))) {
			phpdbg_list_function(function);
		} else {
			phpdbg_error("list", "type=\"notfound\" method=\"%s::%s\"", "Could not find %s::%s", param->method.class, param->method.name);
		}

		efree(lcname);
	} else {
		phpdbg_error("list", "type=\"notfound\" class=\"%s\"", "Could not find the class %s", param->method.class);
	}

	return SUCCESS;
} /* }}} */

PHPDBG_LIST(class) /* {{{ */
{
	zend_class_entry *ce;

	if (phpdbg_safe_class_lookup(param->str, param->len, &ce) == SUCCESS) {
		if (ce->type == ZEND_USER_CLASS) {
			if (ce->info.user.filename) {
				phpdbg_list_file(ce->info.user.filename, ce->info.user.line_end - ce->info.user.line_start + 1, ce->info.user.line_start, 0);
			} else {
				phpdbg_error("list", "type=\"nosource\" class=\"%s\"", "The source of the requested class (%s) cannot be found", ZSTR_VAL(ce->name));
			}
		} else {
			phpdbg_error("list", "type=\"internalclass\" class=\"%s\"", "The class requested (%s) is not user defined", ZSTR_VAL(ce->name));
		}
	} else {
		phpdbg_error("list", "type=\"notfound\" class=\"%s\"", "The requested class (%s) could not be found", param->str);
	}

	return SUCCESS;
} /* }}} */

void phpdbg_list_file(zend_string *filename, uint count, int offset, uint highlight) /* {{{ */
{
	uint line, lastline;
	phpdbg_file_source *data;
	char resolved_path_buf[MAXPATHLEN];
	const char *abspath;

	if (VCWD_REALPATH(ZSTR_VAL(filename), resolved_path_buf)) {
		abspath = resolved_path_buf;
	} else {
		abspath = ZSTR_VAL(filename);
	}

	if (!(data = zend_hash_str_find_ptr(&PHPDBG_G(file_sources), abspath, strlen(abspath)))) {
		phpdbg_error("list", "type=\"unknownfile\"", "Could not find information about included file...");
		return;
	}

	if (offset < 0) {
		count += offset;
		offset = 0;
	}

	lastline = offset + count;

	if (lastline > data->lines) {
		lastline = data->lines;
	}

	phpdbg_xml("<list %r file=\"%s\">", ZSTR_VAL(filename));

	for (line = offset; line < lastline;) {
		uint linestart = data->line[line++];
		uint linelen = data->line[line] - linestart;
		char *buffer = data->buf + linestart;

		if (!highlight) {
			phpdbg_write("line", "line=\"%u\" code=\"%.*s\"", " %05u: %.*s", line, linelen, buffer);
		} else {
			if (highlight != line) {
				phpdbg_write("line", "line=\"%u\" code=\"%.*s\"", " %05u: %.*s", line, linelen, buffer);
			} else {
				phpdbg_write("line", "line=\"%u\" code=\"%.*s\" current=\"current\"", ">%05u: %.*s", line, linelen, buffer);
			}
		}

		if (*(buffer + linelen - 1) != '\n' || !linelen) {
			phpdbg_out("\n");
		}
	}

	phpdbg_xml("</list>");
} /* }}} */

void phpdbg_list_function(const zend_function *fbc) /* {{{ */
{
	const zend_op_array *ops;

	if (fbc->type != ZEND_USER_FUNCTION) {
		phpdbg_error("list", "type=\"internalfunction\" function=\"%s\"", "The function requested (%s) is not user defined", ZSTR_VAL(fbc->common.function_name));
		return;
	}

	ops = (zend_op_array *) fbc;

	phpdbg_list_file(ops->filename, ops->line_end - ops->line_start + 1, ops->line_start, 0);
} /* }}} */

void phpdbg_list_function_byname(const char *str, size_t len) /* {{{ */
{
	HashTable *func_table = EG(function_table);
	zend_function* fbc;
	char *func_name = (char*) str;
	size_t func_name_len = len;

	/* search active scope if begins with period */
	if (func_name[0] == '.') {
		zend_class_entry *scope = zend_get_executed_scope();
		if (scope) {
			func_name++;
			func_name_len--;

			func_table = &scope->function_table;
		} else {
			phpdbg_error("inactive", "type=\"noclasses\"", "No active class");
			return;
		}
	} else if (!EG(function_table)) {
		phpdbg_error("inactive", "type=\"function_table\"", "No function table loaded");
		return;
	} else {
		func_table = EG(function_table);
	}

	/* use lowercase names, case insensitive */
	func_name = zend_str_tolower_dup(func_name, func_name_len);

	phpdbg_try_access {
		if ((fbc = zend_hash_str_find_ptr(func_table, func_name, func_name_len))) {
			phpdbg_list_function(fbc);
		} else {
			phpdbg_error("list", "type=\"nofunction\" function=\"%s\"", "Function %s not found", func_name);
		}
	} phpdbg_catch_access {
		phpdbg_error("signalsegv", "function=\"%s\"", "Could not list function %s, invalid data source", func_name);
	} phpdbg_end_try_access();

	efree(func_name);
} /* }}} */

/* Note: do not free the original file handler, let original compile_file() or caller do that. Caller may rely on its value to check success */
zend_op_array *phpdbg_compile_file(zend_file_handle *file, int type) {
	phpdbg_file_source data, *dataptr;
	zend_file_handle fake;
	zend_op_array *ret;
	char *filename = (char *)(file->opened_path ? ZSTR_VAL(file->opened_path) : file->filename);
	uint line;
	char *bufptr, *endptr;
	char resolved_path_buf[MAXPATHLEN];

	if (zend_stream_fixup(file, &bufptr, &data.len) == FAILURE) {
		return PHPDBG_G(compile_file)(file, type);
	}

	data.buf = emalloc(data.len + ZEND_MMAP_AHEAD + 1);
	if (data.len > 0) {
		memcpy(data.buf, bufptr, data.len);
	}
	memset(data.buf + data.len, 0, ZEND_MMAP_AHEAD + 1);
	data.filename = filename;
	data.line[0] = 0;

	memset(&fake, 0, sizeof(fake));
	fake.type = ZEND_HANDLE_MAPPED;
	fake.handle.stream.mmap.buf = data.buf;
	fake.handle.stream.mmap.len = data.len;
	fake.free_filename = 0;
	fake.filename = filename;
	fake.opened_path = file->opened_path;

	*(dataptr = emalloc(sizeof(phpdbg_file_source) + sizeof(uint) * data.len)) = data;
	if (VCWD_REALPATH(filename, resolved_path_buf)) {
		filename = resolved_path_buf;
	}

	for (line = 0, bufptr = data.buf - 1, endptr = data.buf + data.len; ++bufptr < endptr;) {
		if (*bufptr == '\n') {
			dataptr->line[++line] = (uint)(bufptr - data.buf) + 1;
		}
	}
	dataptr->lines = ++line;
	dataptr->line[line] = endptr - data.buf;

	ret = PHPDBG_G(compile_file)(&fake, type);

	if (ret == NULL) {
		efree(data.buf);
		efree(dataptr);

		fake.opened_path = NULL;
		zend_file_handle_dtor(&fake);

		return NULL;
	}

	dataptr = erealloc(dataptr, sizeof(phpdbg_file_source) + sizeof(uint) * line);
	zend_hash_str_add_ptr(&PHPDBG_G(file_sources), filename, strlen(filename), dataptr);
	phpdbg_resolve_pending_file_break(filename);

	fake.opened_path = NULL;
	zend_file_handle_dtor(&fake);

	return ret;
}

zend_op_array *phpdbg_init_compile_file(zend_file_handle *file, int type) {
	char *filename = (char *)(file->opened_path ? ZSTR_VAL(file->opened_path) : file->filename);
	char resolved_path_buf[MAXPATHLEN];
	zend_op_array *op_array;
	phpdbg_file_source *dataptr;

	if (VCWD_REALPATH(filename, resolved_path_buf)) {
		filename = resolved_path_buf;
	}

	op_array = PHPDBG_G(init_compile_file)(file, type);

	if (op_array == NULL) {
		return NULL;
	}

	dataptr = zend_hash_str_find_ptr(&PHPDBG_G(file_sources), filename, strlen(filename));
	ZEND_ASSERT(dataptr != NULL);

	if (op_array->vars) {
		int i;
		/* un-intern these strings to prevent zend_restore_strings from invalidating our string pointers too early */
		for (i = 0; i < op_array->last_var; i++) {
			zend_string **s = op_array->vars + i;
			if (ZSTR_IS_INTERNED(*s)) {
				*s = zend_string_init(ZSTR_VAL(*s), ZSTR_LEN(*s), 0);
			}
		}
	}

	dataptr->op_array = *op_array;
	if (dataptr->op_array.refcount) {
		++*dataptr->op_array.refcount;
	}

	return op_array;
}

zend_op_array *phpdbg_compile_string(zval *source_string, char *filename) {
	zend_string *fake_name;
	zend_op_array *op_array;
	phpdbg_file_source *dataptr;
	uint line;
	char *bufptr, *endptr;

	if (PHPDBG_G(flags) & PHPDBG_IN_EVAL) {
		return PHPDBG_G(compile_string)(source_string, filename);
	}

	dataptr = emalloc(sizeof(phpdbg_file_source) + sizeof(uint) * Z_STRLEN_P(source_string));
	dataptr->buf = estrndup(Z_STRVAL_P(source_string), Z_STRLEN_P(source_string));
	dataptr->len = Z_STRLEN_P(source_string);
	dataptr->line[0] = 0;
	for (line = 0, bufptr = dataptr->buf - 1, endptr = dataptr->buf + dataptr->len; ++bufptr < endptr;) {
		if (*bufptr == '\n') {
			dataptr->line[++line] = (uint)(bufptr - dataptr->buf) + 1;
		}
	}
	dataptr->lines = ++line;
	dataptr->line[line] = endptr - dataptr->buf;

	op_array = PHPDBG_G(compile_string)(source_string, filename);

	if (op_array == NULL) {
		efree(dataptr->buf);
		efree(dataptr);
		return NULL;
	}

	fake_name = strpprintf(0, "%s\0%p", filename, op_array->opcodes);

	dataptr = erealloc(dataptr, sizeof(phpdbg_file_source) + sizeof(uint) * line);
	zend_hash_add_ptr(&PHPDBG_G(file_sources), fake_name, dataptr);

	dataptr->filename = estrndup(ZSTR_VAL(fake_name), ZSTR_LEN(fake_name));
	zend_string_release(fake_name);

	dataptr->op_array = *op_array;
	if (dataptr->op_array.refcount) {
		++*dataptr->op_array.refcount;
	}

	return op_array;
}

void phpdbg_init_list(void) {
	PHPDBG_G(compile_file) = zend_compile_file;
	PHPDBG_G(compile_string) = zend_compile_string;
	zend_compile_file = phpdbg_compile_file;
	zend_compile_string = phpdbg_compile_string;
}

void phpdbg_list_update(void) {
	PHPDBG_G(init_compile_file) = zend_compile_file;
	zend_compile_file = phpdbg_init_compile_file;
}