summaryrefslogtreecommitdiff
path: root/sapi/phpdbg/phpdbg_info.c
blob: 97f88bfa1ec59437c6829dfb0f28323195a36df2 (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
/*
   +----------------------------------------------------------------------+
   | 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 "php.h"
#include "phpdbg.h"
#include "phpdbg_utils.h"
#include "phpdbg_info.h"
#include "phpdbg_bp.h"
#include "phpdbg_prompt.h"

ZEND_EXTERN_MODULE_GLOBALS(phpdbg);

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

const phpdbg_command_t phpdbg_info_commands[] = {
	PHPDBG_INFO_COMMAND_D(break,    "show breakpoints",              'b', info_break,   NULL, 0),
	PHPDBG_INFO_COMMAND_D(files,    "show included files",           'F', info_files,   NULL, 0),
	PHPDBG_INFO_COMMAND_D(classes,  "show loaded classes",           'c', info_classes, NULL, 0),
	PHPDBG_INFO_COMMAND_D(funcs,    "show loaded classes",           'f', info_funcs,   NULL, 0),
	PHPDBG_INFO_COMMAND_D(error,    "show last error",               'e', info_error,   NULL, 0),
	PHPDBG_INFO_COMMAND_D(vars,     "show active variables",         'v', info_vars,    NULL, 0),
	PHPDBG_INFO_COMMAND_D(literal,  "show active literal constants", 'l', info_literal, NULL, 0),
	PHPDBG_INFO_COMMAND_D(memory,   "show memory manager stats",     'm', info_memory,  NULL, 0),
	PHPDBG_END_COMMAND
};

PHPDBG_INFO(break) /* {{{ */
{
	phpdbg_print_breakpoints(PHPDBG_BREAK_FILE TSRMLS_CC);
	phpdbg_print_breakpoints(PHPDBG_BREAK_SYM TSRMLS_CC);
	phpdbg_print_breakpoints(PHPDBG_BREAK_METHOD TSRMLS_CC);
	phpdbg_print_breakpoints(PHPDBG_BREAK_OPLINE TSRMLS_CC);
	phpdbg_print_breakpoints(PHPDBG_BREAK_FILE_OPLINE TSRMLS_CC);
	phpdbg_print_breakpoints(PHPDBG_BREAK_FUNCTION_OPLINE TSRMLS_CC);
	phpdbg_print_breakpoints(PHPDBG_BREAK_METHOD_OPLINE TSRMLS_CC);
	phpdbg_print_breakpoints(PHPDBG_BREAK_COND TSRMLS_CC);
	phpdbg_print_breakpoints(PHPDBG_BREAK_OPCODE TSRMLS_CC);

	return SUCCESS;
} /* }}} */

PHPDBG_INFO(files) /* {{{ */
{
	HashPosition pos;
	char *fname;

	phpdbg_notice("Included files: %d",
		zend_hash_num_elements(&EG(included_files)));

	zend_hash_internal_pointer_reset_ex(&EG(included_files), &pos);
	while (zend_hash_get_current_key_ex(&EG(included_files), &fname,
		NULL, NULL, 0, &pos) == HASH_KEY_IS_STRING) {
		phpdbg_writeln("File: %s", fname);
		zend_hash_move_forward_ex(&EG(included_files), &pos);
	}

	return SUCCESS;
} /* }}} */

PHPDBG_INFO(error) /* {{{ */
{
	if (PG(last_error_message)) {
		phpdbg_writeln("Last error: %s at %s line %d",
			PG(last_error_message), PG(last_error_file), PG(last_error_lineno));
	} else {
		phpdbg_notice("No error found!");
	}
	return SUCCESS;
} /* }}} */

PHPDBG_INFO(vars) /* {{{ */
{
	HashTable vars;
	HashPosition pos;
	char *var;
	zval **data;

	if (!EG(active_op_array)) {
		phpdbg_error("No active op array!");
		return SUCCESS;
	}

	if (!EG(active_symbol_table)) {
		zend_rebuild_symbol_table(TSRMLS_C);

		if (!EG(active_symbol_table)) {
			phpdbg_error("No active symbol table!");
			return SUCCESS;
		}
	}

	zend_hash_init(&vars, 8, NULL, NULL, 0);

	zend_hash_internal_pointer_reset_ex(EG(active_symbol_table), &pos);
	while (zend_hash_get_current_key_ex(EG(active_symbol_table), &var,
		NULL, NULL, 0, &pos) == HASH_KEY_IS_STRING) {
		zend_hash_get_current_data_ex(EG(active_symbol_table), (void **)&data, &pos);
		if (*var != '_') {
			zend_hash_update(
				&vars, var, strlen(var)+1, (void**)data, sizeof(zval*), NULL);
		}
		zend_hash_move_forward_ex(EG(active_symbol_table), &pos);
	}

	{
		zend_op_array *ops = EG(active_op_array);
		
		if (ops->function_name) {
			if (ops->scope) {
				phpdbg_notice(
				"Variables in %s::%s() (%d)", ops->scope->name, ops->function_name, zend_hash_num_elements(&vars));
			} else {
				phpdbg_notice(
					"Variables in %s() (%d)", ops->function_name, zend_hash_num_elements(&vars));
			}
		} else {
			if (ops->filename) {
				phpdbg_notice(
				"Variables in %s (%d)", ops->filename, zend_hash_num_elements(&vars));
			} else {
				phpdbg_notice(
					"Variables @ %p (%d)", ops, zend_hash_num_elements(&vars));
			}
		}
	}

	if (zend_hash_num_elements(&vars)) {
		phpdbg_writeln("Address\t\tRefs\tType\t\tVariable");
		for (zend_hash_internal_pointer_reset_ex(&vars, &pos);
			zend_hash_get_current_data_ex(&vars, (void**) &data, &pos) == SUCCESS;
			zend_hash_move_forward_ex(&vars, &pos)) {
			char *var;

			zend_hash_get_current_key_ex(&vars, &var, NULL, NULL, 0, &pos);

			if (*data) {
				phpdbg_write(
				"%p\t%d\t",
					*data,
					Z_REFCOUNT_PP(data));

				switch (Z_TYPE_PP(data)) {
					case IS_STRING: 	phpdbg_write("(string)\t"); 	break;
					case IS_LONG: 		phpdbg_write("(integer)\t"); 	break;
					case IS_DOUBLE: 	phpdbg_write("(float)\t"); 		break;
					case IS_RESOURCE:	phpdbg_write("(resource)\t"); 	break;
					case IS_ARRAY:		phpdbg_write("(array)\t"); 		break;
					case IS_OBJECT:		phpdbg_write("(object)\t"); 	break;
					case IS_NULL:		phpdbg_write("(null)\t"); 		break;
				}

				if (Z_TYPE_PP(data) == IS_RESOURCE) {
					int type;

					phpdbg_writeln(
						"%s$%s", Z_ISREF_PP(data) ? "&": "", var);
					if (zend_list_find(Z_RESVAL_PP(data), &type)) {
						phpdbg_write(
							"|-------(typeof)------> (%s)",
							zend_rsrc_list_get_rsrc_type(type TSRMLS_CC));
					} else {
						phpdbg_write(
							"|-------(typeof)------> (unknown)");
					}
					phpdbg_writeln(EMPTY);
				} else if (Z_TYPE_PP(data) == IS_OBJECT) {
					phpdbg_writeln(
						"%s$%s", Z_ISREF_PP(data) ? "&": "", var);
					phpdbg_write(
						"|-----(instanceof)----> (%s)", Z_OBJCE_PP(data)->name);
					phpdbg_writeln(EMPTY);
				} else {
					phpdbg_write(
						"%s$%s", Z_ISREF_PP(data) ? "&": "", var);
				}
			} else {
				phpdbg_write(
					"n/a\tn/a\tn/a\t$%s", var);
			}
			phpdbg_writeln(EMPTY);
		}
	}

	zend_hash_destroy(&vars);

	return SUCCESS;
} /* }}} */

PHPDBG_INFO(literal) /* {{{ */
{
	if ((EG(in_execution) && EG(active_op_array)) || PHPDBG_G(ops)) {
		zend_op_array *ops = EG(active_op_array) ? EG(active_op_array) : PHPDBG_G(ops);
		int literal = 0, count = ops->last_literal-1;

		if (ops->function_name) {
			if (ops->scope) {
				phpdbg_notice(
				"Literal Constants in %s::%s() (%d)", ops->scope->name, ops->function_name, count);
			} else {
				phpdbg_notice(
					"Literal Constants in %s() (%d)", ops->function_name, count);
			}
		} else {
			if (ops->filename) {
				phpdbg_notice(
				"Literal Constants in %s (%d)", ops->filename, count);
			} else {
				phpdbg_notice(
					"Literal Constants @ %p (%d)", ops, count);
			}
		}

		while (literal < ops->last_literal) {
			if (Z_TYPE(ops->literals[literal].constant) != IS_NULL) {
				phpdbg_write("|-------- C%u -------> [", literal);
				zend_print_zval(
					&ops->literals[literal].constant, 0);
				phpdbg_write("]");
				phpdbg_writeln(EMPTY);
			}
			literal++;
		}
	} else {
		phpdbg_error("Not executing!");
	}

	return SUCCESS;
} /* }}} */

PHPDBG_INFO(memory) /* {{{ */
{
	if (is_zend_mm(TSRMLS_C)) {
		phpdbg_notice("Memory Manager Information");
		phpdbg_notice("Current");
		phpdbg_writeln("|-------> Used:\t%.3f kB", 
			(float) (zend_memory_usage(0 TSRMLS_CC)/1024));
		phpdbg_writeln("|-------> Real:\t%.3f kB", 
			(float) (zend_memory_usage(1 TSRMLS_CC)/1024));
		phpdbg_notice("Peak");
		phpdbg_writeln("|-------> Used:\t%.3f kB", 
			(float) (zend_memory_peak_usage(0 TSRMLS_CC)/1024));
		phpdbg_writeln("|-------> Real:\t%.3f kB", 
			(float) (zend_memory_peak_usage(1 TSRMLS_CC)/1024));
	} else {
		phpdbg_error("Memory Manager Disabled!");
	}
	return SUCCESS;
} /* }}} */

static inline void phpdbg_print_class_name(zend_class_entry **ce TSRMLS_DC) /* {{{ */
{
	phpdbg_write(
		"%s %s %s (%d)",
		((*ce)->type == ZEND_USER_CLASS) ?
			"User" : "Internal",
		((*ce)->ce_flags & ZEND_ACC_INTERFACE) ?
			"Interface" :
			((*ce)->ce_flags & ZEND_ACC_ABSTRACT) ?
				"Abstract Class" :
					"Class",
		(*ce)->name, zend_hash_num_elements(&(*ce)->function_table));
} /* }}} */

PHPDBG_INFO(classes) /* {{{ */
{
	HashPosition position;
	zend_class_entry **ce;
	HashTable classes;

	zend_hash_init(&classes, 8, NULL, NULL, 0);

	for (zend_hash_internal_pointer_reset_ex(EG(class_table), &position);
		zend_hash_get_current_data_ex(EG(class_table), (void**)&ce, &position) == SUCCESS;
		zend_hash_move_forward_ex(EG(class_table), &position)) {

		if ((*ce)->type == ZEND_USER_CLASS) {
			zend_hash_next_index_insert(
				&classes, ce, sizeof(ce), NULL);
		}
	}

	phpdbg_notice("User Classes (%d)",
		zend_hash_num_elements(&classes));

	for (zend_hash_internal_pointer_reset_ex(&classes, &position);
		zend_hash_get_current_data_ex(&classes, (void**)&ce, &position) == SUCCESS;
		zend_hash_move_forward_ex(&classes, &position)) {

		phpdbg_print_class_name(ce TSRMLS_CC);
		phpdbg_writeln(EMPTY);

		if ((*ce)->parent) {
			zend_class_entry *pce = (*ce)->parent;
			do {
				phpdbg_write("|-------- ");
				phpdbg_print_class_name(&pce TSRMLS_CC);
				phpdbg_writeln(EMPTY);
			} while ((pce = pce->parent));
		}

		if ((*ce)->info.user.filename) {
			phpdbg_writeln(
				"|---- in %s on line %u",
				(*ce)->info.user.filename,
				(*ce)->info.user.line_start);
		} else {
			phpdbg_writeln("|---- no source code");
		}
	    	phpdbg_writeln(EMPTY);
	}

	zend_hash_destroy(&classes);

	return SUCCESS;
} /* }}} */

PHPDBG_INFO(funcs) /* {{{ */
{
	HashPosition position;
	zend_function *zf, **pzf;
	HashTable functions;

	zend_hash_init(&functions, 8, NULL, NULL, 0);

	for (zend_hash_internal_pointer_reset_ex(EG(function_table), &position);
		zend_hash_get_current_data_ex(EG(function_table), (void**)&zf, &position) == SUCCESS;
		zend_hash_move_forward_ex(EG(function_table), &position)) {

		if (zf->type == ZEND_USER_FUNCTION) {
			zend_hash_next_index_insert(
				&functions, (void**) &zf, sizeof(zend_function), NULL);
		}
	}

	phpdbg_notice("User Functions (%d)",
		zend_hash_num_elements(&functions));

	for (zend_hash_internal_pointer_reset_ex(&functions, &position);
		zend_hash_get_current_data_ex(&functions, (void**)&pzf, &position) == SUCCESS;
		zend_hash_move_forward_ex(&functions, &position)) {
		zend_op_array *op_array = &((*pzf)->op_array);

		phpdbg_writeln(
			"|-------- %s in %s on line %d",
			op_array->function_name ? op_array->function_name : "{main}",
			op_array->filename ? op_array->filename : "(no source code)",
			op_array->line_start);
	}

	zend_hash_destroy(&functions);

	return SUCCESS;
} /* }}} */