1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 5 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2016 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Authors: Felipe Pena <felipe@php.net> |
16 | Authors: Joe Watkins <joe.watkins@live.co.uk> |
17 | Authors: Bob Weinand <bwoebi@php.net> |
18 +----------------------------------------------------------------------+
19 */
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #ifndef _WIN32
25 # include <sys/mman.h>
26 # include <unistd.h>
27 #endif
28 #include <fcntl.h>
29 #include "phpdbg.h"
30 #include "phpdbg_list.h"
31 #include "phpdbg_utils.h"
32 #include "phpdbg_prompt.h"
33 #include "php_streams.h"
34
35 ZEND_EXTERN_MODULE_GLOBALS(phpdbg);
36
37 #define PHPDBG_LIST_COMMAND_D(f, h, a, m, l, s) \
38 PHPDBG_COMMAND_D_EXP(f, h, a, m, l, s, &phpdbg_prompt_commands[13])
39
40 const phpdbg_command_t phpdbg_list_commands[] = {
41 PHPDBG_LIST_COMMAND_D(lines, "lists the specified lines", 'l', list_lines, NULL, "l"),
42 PHPDBG_LIST_COMMAND_D(class, "lists the specified class", 'c', list_class, NULL, "s"),
43 PHPDBG_LIST_COMMAND_D(method, "lists the specified method", 'm', list_method, NULL, "m"),
44 PHPDBG_LIST_COMMAND_D(func, "lists the specified function", 'f', list_func, NULL, "s"),
45 PHPDBG_END_COMMAND
46 };
47
PHPDBG_LIST(lines)48 PHPDBG_LIST(lines) /* {{{ */
49 {
50 if (!PHPDBG_G(exec) && !zend_is_executing(TSRMLS_C)) {
51 phpdbg_error("Not executing, and execution context not set");
52 return SUCCESS;
53 }
54
55 switch (param->type) {
56 case NUMERIC_PARAM:
57 phpdbg_list_file(phpdbg_current_file(TSRMLS_C),
58 (param->num < 0 ? 1 - param->num : param->num),
59 (param->num < 0 ? param->num : 0) + zend_get_executed_lineno(TSRMLS_C),
60 0 TSRMLS_CC);
61 break;
62
63 case FILE_PARAM:
64 phpdbg_list_file(param->file.name, param->file.line, 0, 0 TSRMLS_CC);
65 break;
66
67 phpdbg_default_switch_case();
68 }
69
70 return SUCCESS;
71 } /* }}} */
72
PHPDBG_LIST(func)73 PHPDBG_LIST(func) /* {{{ */
74 {
75 phpdbg_list_function_byname(param->str, param->len TSRMLS_CC);
76
77 return SUCCESS;
78 } /* }}} */
79
PHPDBG_LIST(method)80 PHPDBG_LIST(method) /* {{{ */
81 {
82 zend_class_entry **ce;
83
84 if (zend_lookup_class(param->method.class, strlen(param->method.class), &ce TSRMLS_CC) == SUCCESS) {
85 zend_function *function;
86 char *lcname = zend_str_tolower_dup(param->method.name, strlen(param->method.name));
87
88 if (zend_hash_find(&(*ce)->function_table, lcname, strlen(lcname)+1, (void**) &function) == SUCCESS) {
89 phpdbg_list_function(function TSRMLS_CC);
90 } else {
91 phpdbg_error("Could not find %s::%s", param->method.class, param->method.name);
92 }
93
94 efree(lcname);
95 } else {
96 phpdbg_error("Could not find the class %s", param->method.class);
97 }
98
99 return SUCCESS;
100 } /* }}} */
101
PHPDBG_LIST(class)102 PHPDBG_LIST(class) /* {{{ */
103 {
104 zend_class_entry **ce;
105
106 if (zend_lookup_class(param->str, param->len, &ce TSRMLS_CC) == SUCCESS) {
107 if ((*ce)->type == ZEND_USER_CLASS) {
108 if ((*ce)->info.user.filename) {
109 phpdbg_list_file(
110 (*ce)->info.user.filename,
111 (*ce)->info.user.line_end - (*ce)->info.user.line_start + 1,
112 (*ce)->info.user.line_start, 0 TSRMLS_CC
113 );
114 } else {
115 phpdbg_error("The source of the requested class (%s) cannot be found", (*ce)->name);
116 }
117 } else {
118 phpdbg_error("The class requested (%s) is not user defined", (*ce)->name);
119 }
120 } else {
121 phpdbg_error("The requested class (%s) could not be found", param->str);
122 }
123
124 return SUCCESS;
125 } /* }}} */
126
phpdbg_list_file(const char * filename,long count,long offset,int highlight TSRMLS_DC)127 void phpdbg_list_file(const char *filename, long count, long offset, int highlight TSRMLS_DC) /* {{{ */
128 {
129 struct stat st;
130 char *opened = NULL;
131 char buffer[8096] = {0,};
132 long line = 0;
133
134 php_stream *stream = NULL;
135
136 if (VCWD_STAT(filename, &st) == FAILURE) {
137 phpdbg_error("Failed to stat file %s", filename);
138 return;
139 }
140
141 stream = php_stream_open_wrapper(filename, "rb", USE_PATH, &opened);
142
143 if (!stream) {
144 phpdbg_error("Failed to open file %s to list", filename);
145 return;
146 }
147
148 if (offset < 0) {
149 count += offset;
150 offset = 0;
151 }
152
153 while (php_stream_gets(stream, buffer, sizeof(buffer)) != NULL) {
154 long linelen = strlen(buffer);
155
156 ++line;
157
158 if (offset <= line) {
159 if (!highlight) {
160 phpdbg_write("%05ld: %s", line, buffer);
161 } else {
162 if (highlight != line) {
163 phpdbg_write(" %05ld: %s", line, buffer);
164 } else {
165 phpdbg_write(">%05ld: %s", line, buffer);
166 }
167 }
168
169 if (buffer[linelen - 1] != '\n') {
170 phpdbg_write("\n");
171 }
172 }
173
174 if (count > 0 && count + offset - 1 < line) {
175 break;
176 }
177 }
178
179 php_stream_close(stream);
180 } /* }}} */
181
phpdbg_list_function(const zend_function * fbc TSRMLS_DC)182 void phpdbg_list_function(const zend_function *fbc TSRMLS_DC) /* {{{ */
183 {
184 const zend_op_array *ops;
185
186 if (fbc->type != ZEND_USER_FUNCTION) {
187 phpdbg_error("The function requested (%s) is not user defined", fbc->common.function_name);
188 return;
189 }
190
191 ops = (zend_op_array*)fbc;
192
193 phpdbg_list_file(ops->filename,
194 ops->line_end - ops->line_start + 1, ops->line_start, 0 TSRMLS_CC);
195 } /* }}} */
196
phpdbg_list_function_byname(const char * str,size_t len TSRMLS_DC)197 void phpdbg_list_function_byname(const char *str, size_t len TSRMLS_DC) /* {{{ */
198 {
199 HashTable *func_table = EG(function_table);
200 zend_function* fbc;
201 char *func_name = (char*) str;
202 size_t func_name_len = len;
203
204 /* search active scope if begins with period */
205 if (func_name[0] == '.') {
206 if (EG(scope)) {
207 func_name++;
208 func_name_len--;
209
210 func_table = &EG(scope)->function_table;
211 } else {
212 phpdbg_error("No active class");
213 return;
214 }
215 } else if (!EG(function_table)) {
216 phpdbg_error("No function table loaded");
217 return;
218 } else {
219 func_table = EG(function_table);
220 }
221
222 /* use lowercase names, case insensitive */
223 func_name = zend_str_tolower_dup(func_name, func_name_len);
224
225 if (zend_hash_find(func_table, func_name, func_name_len+1, (void**)&fbc) == SUCCESS) {
226 phpdbg_list_function(fbc TSRMLS_CC);
227 } else {
228 phpdbg_error("Function %s not found", func_name);
229 }
230
231 efree(func_name);
232 } /* }}} */
233
234