summaryrefslogtreecommitdiff
path: root/sapi/phpdbg/phpdbg_list.c
blob: e8db4e605c73f62e0bc96e107f5ee5a4337f47ec (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
/*
   +----------------------------------------------------------------------+
   | PHP Version 5                                                        |
   +----------------------------------------------------------------------+
   | Copyright (c) 1997-2014 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"

ZEND_EXTERN_MODULE_GLOBALS(phpdbg);

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

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

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

	switch (param->type) {
		case NUMERIC_PARAM:
			phpdbg_list_file(phpdbg_current_file(TSRMLS_C),
				(param->num < 0 ? 1 - param->num : param->num),
				(param->num < 0 ? param->num : 0) + zend_get_executed_lineno(TSRMLS_C),
				0 TSRMLS_CC);
			break;
			
		case FILE_PARAM:
			phpdbg_list_file(param->file.name, param->file.line, 0, 0 TSRMLS_CC);
			break;

		phpdbg_default_switch_case();
	}

	return SUCCESS;
} /* }}} */

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

	return SUCCESS;
} /* }}} */

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

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

		if (zend_hash_find(&(*ce)->function_table, lcname, strlen(lcname)+1, (void**) &function) == SUCCESS) {
			phpdbg_list_function(function TSRMLS_CC);
		} else {
			phpdbg_error("Could not find %s::%s", param->method.class, param->method.name);
		}

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

	return SUCCESS;
} /* }}} */

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

	if (zend_lookup_class(param->str, param->len, &ce TSRMLS_CC) == 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 TSRMLS_CC
				);
			} else {
				phpdbg_error("The source of the requested class (%s) cannot be found", (*ce)->name);
			}
		} else {
			phpdbg_error("The class requested (%s) is not user defined", (*ce)->name);
		}
	} else {
		phpdbg_error("The requested class (%s) could not be found", param->str);
	}

	return SUCCESS;
} /* }}} */

void phpdbg_list_file(const char *filename, long count, long offset, int highlight TSRMLS_DC) /* {{{ */
{
	struct stat st;
	char *opened = NULL;
	char buffer[8096] = {0,};
	long line = 0;

	php_stream *stream = NULL;
	
	if (VCWD_STAT(filename, &st) == FAILURE) {
		phpdbg_error("Failed to stat file %s", filename);
		return;
	}

	stream = php_stream_open_wrapper(filename, "rb", USE_PATH, &opened);
	
	if (!stream) {
		phpdbg_error("Failed to open file %s to list", filename);
		return;
	}
	
	if (offset < 0) {
		count += offset;
		offset = 0;
	}
	
	while (php_stream_gets(stream, buffer, sizeof(buffer)) != NULL) {
		long linelen = strlen(buffer);

		++line;
		
		if (offset <= line) {
			if (!highlight) {
				phpdbg_write("%05ld: %s", line, buffer);
			} else {
				if (highlight != line) {
					phpdbg_write(" %05ld: %s", line, buffer);
				} else {
					phpdbg_write(">%05ld: %s", line, buffer);
				}
			}

			if (buffer[linelen - 1] != '\n') {
				phpdbg_write("\n");
			}
		}

		if (count > 0 && count + offset - 1 < line) {
			break;
		}
	}
	
	php_stream_close(stream);
} /* }}} */

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

	if (fbc->type != ZEND_USER_FUNCTION) {
		phpdbg_error("The function requested (%s) is not user defined", 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 TSRMLS_CC);
} /* }}} */

void phpdbg_list_function_byname(const char *str, size_t len TSRMLS_DC) /* {{{ */
{
	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] == '.') {
		if (EG(scope)) {
			func_name++;
			func_name_len--;

			func_table = &EG(scope)->function_table;
		} else {
			phpdbg_error("No active class");
			return;
		}
	} else if (!EG(function_table)) {
		phpdbg_error("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);

	if (zend_hash_find(func_table, func_name, func_name_len+1, (void**)&fbc) == SUCCESS) {
		phpdbg_list_function(fbc TSRMLS_CC);
	} else {
		phpdbg_error("Function %s not found", func_name);
	}

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