xref: /PHP-8.3/sapi/phpdbg/phpdbg_help.c (revision 119b0621)
1 /*
2    +----------------------------------------------------------------------+
3    | Copyright (c) The PHP Group                                          |
4    +----------------------------------------------------------------------+
5    | This source file is subject to version 3.01 of the PHP license,      |
6    | that is bundled with this package in the file LICENSE, and is        |
7    | available through the world-wide-web at the following url:           |
8    | https://www.php.net/license/3_01.txt                                 |
9    | If you did not receive a copy of the PHP license and are unable to   |
10    | obtain it through the world-wide-web, please send a note to          |
11    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Felipe Pena <felipe@php.net>                                |
14    | Authors: Joe Watkins <joe.watkins@live.co.uk>                        |
15    | Authors: Bob Weinand <bwoebi@php.net>                                |
16    | Authors: Terry Ellison <terry@ellisons.org.uk>                       |
17    +----------------------------------------------------------------------+
18 */
19 
20 #include "phpdbg.h"
21 #include "phpdbg_help.h"
22 #include "phpdbg_prompt.h"
23 #include "zend.h"
24 
25 ZEND_EXTERN_MODULE_GLOBALS(phpdbg)
26 
27 /* {{{ Commands Table */
28 #define PHPDBG_COMMAND_HELP_D(name, tip, alias, action) \
29 	{PHPDBG_STRL(#name), tip, sizeof(tip)-1, alias, action, &phpdbg_prompt_commands[16], 0, NULL, (bool) 0}
30 
31 const phpdbg_command_t phpdbg_help_commands[] = {
32 	PHPDBG_COMMAND_HELP_D(aliases,    "show alias list", 'a', phpdbg_do_help_aliases),
33 	PHPDBG_COMMAND_HELP_D(options,    "command line options", 0, NULL),
34 	PHPDBG_COMMAND_HELP_D(overview,   "help overview", 0, NULL),
35 	PHPDBG_COMMAND_HELP_D(phpdbginit, "phpdbginit file format", 0, NULL),
36 	PHPDBG_COMMAND_HELP_D(syntax,     "syntax overview", 0, NULL),
37 	PHPDBG_END_COMMAND
38 };  /* }}} */
39 
40 /* {{{ pretty_print.  Formatting escapes and wrapping text in a string before printing it. */
pretty_print(const char * text)41 static void pretty_print(const char *text)
42 {
43 	char *new, *q;
44 
45 	const char  *prompt_escape = phpdbg_get_prompt();
46 	size_t prompt_escape_len = strlen(prompt_escape);
47 	size_t prompt_len = strlen(PHPDBG_G(prompt)[0]);
48 
49 	const char  *bold_on_escape  = PHPDBG_G(flags) & PHPDBG_IS_COLOURED ? "\033[1m" : "";
50 	const char  *bold_off_escape = PHPDBG_G(flags) & PHPDBG_IS_COLOURED ? "\033[0m" : "";
51 	size_t bold_escape_len = strlen(bold_on_escape);
52 
53 	uint32_t term_width = phpdbg_get_terminal_width();
54 	uint32_t size = 0;
55 
56 	int in_bold = 0;
57 
58 	char *last_new_blank = NULL;  /* position in new buffer of last blank char */
59 	uint32_t last_blank_count = 0;    /* printable char offset of last blank char */
60 	uint32_t line_count = 0;          /* number printable chars on current line */
61 
62 	/* First pass calculates a safe size for the pretty print version */
63 	const char *p;
64 	for (p = text; *p; p++) {
65 		if (UNEXPECTED(p[0] == '*') && p[1] == '*') {
66 			size += bold_escape_len - 2;
67 			p++;
68 		} else if (UNEXPECTED(p[0] == '$') && p[1] == 'P') {
69 			size += prompt_escape_len - 2;
70 			p++;
71 		} else if (UNEXPECTED(p[0] == '\\')) {
72 			p++;
73 		}
74 	}
75 	size += (p-text)+1;
76 
77 	new = emalloc(size);
78 	/*
79 	 * Second pass substitutes the bold and prompt escape sequences and line wrap
80 	 *
81 	 * ** toggles bold on and off if PHPDBG_IS_COLOURED flag is set
82 	 * $P substitutes the prompt sequence
83 	 * Lines are wrapped by replacing the last blank with a CR before <term width>
84 	 * characters.  (This defaults to 100 if the width can't be detected).  In the
85 	 * pathological case where no blanks are found, then the wrap occurs at the
86 	 * first blank.
87 	 */
88 	for (p = text, q = new; *p; p++) {
89 		if (UNEXPECTED(*p == ' ')) {
90 			last_new_blank = q;
91 			last_blank_count = line_count++;
92 			*q++ = ' ';
93 		} else if (UNEXPECTED(*p == '\n')) {
94 			last_new_blank = NULL;
95 			*q++ = *p;
96 			last_blank_count = 0;
97 			line_count = 0;
98 		} else if (UNEXPECTED(p[0] == '*') && p[1] == '*') {
99 			if (bold_escape_len) {
100 				in_bold = !in_bold;
101 				memcpy (q, in_bold ? bold_on_escape : bold_off_escape, bold_escape_len);
102 				q += bold_escape_len;
103 				/* bold on/off has zero print width so line count is unchanged */
104 			}
105 			p++;
106 		} else if (UNEXPECTED(p[0] == '$') && p[1] == 'P') {
107 			memcpy (q, prompt_escape, prompt_escape_len);
108 			q += prompt_escape_len;
109 			line_count += prompt_len;
110 			p++;
111 		} else if (UNEXPECTED(p[0] == '\\')) {
112 			p++;
113 			*q++ = *p;
114 			line_count++;
115 		} else {
116 			*q++ = *p;
117 			line_count++;
118 		}
119 
120 		if (UNEXPECTED(line_count>=term_width) && last_new_blank) {
121 			*last_new_blank = '\n';
122 			last_new_blank = NULL;
123 			line_count -= last_blank_count;
124 			last_blank_count = 0;
125 		}
126 	}
127 	*q++ = '\0';
128 
129 	if ((q-new)>size) {
130 		phpdbg_error("Output overrun of %" PRIu32 " bytes", (uint32_t) ((q - new) - size));
131 	}
132 
133 	phpdbg_out("%s\n", new);
134 	efree(new);
135 }  /* }}} */
136 
137 /* {{{ summary_print.  Print a summary line giving, the command, its alias and tip */
summary_print(phpdbg_command_t const * const cmd)138 void summary_print(phpdbg_command_t const * const cmd)
139 {
140 	char *summary;
141 	spprintf(&summary, 0, "Command: **%s**  Alias: **%c**  **%s**\n", cmd->name, cmd->alias, cmd->tip);
142 	pretty_print(summary);
143 	efree(summary);
144 }
145 
146 /* {{{ get_help. Retries and formats text from the phpdbg help text table */
get_help(const char * const key)147 static const char *get_help(const char * const key)
148 {
149 	const phpdbg_help_text_t *p;
150 
151 	/* Note that phpdbg_help_text is not assumed to be collated in key order.  This is an
152 	   inconvenience that means that help can't be logically grouped Not worth
153 	   the savings */
154 
155 	for (p = phpdbg_help_text; p->key; p++) {
156 		if (!strcmp(p->key, key)) {
157 			return p->text;
158 		}
159 	}
160 	return "";   /* return empty string to denote no match found */
161 } /* }}} */
162 
163 /* {{{ get_command.  Return number of matching commands from a command table.
164  * Unlike the command parser, the help search is sloppy that is partial matches can occur
165  *   * Any single character key is taken as an alias.
166  *   * Other keys are matched again the table on the first len characters.
167  *   * This means that non-unique keys can generate multiple matches.
168  *   * The first matching command is returned as an OUT parameter. *
169  * The rationale here is to assist users in finding help on commands. So unique matches
170  * will be used to generate a help message but non-unique one will be used to list alternatives.
171  */
get_command(const char * key,size_t len,phpdbg_command_t const ** command,phpdbg_command_t const * commands)172 static int get_command(
173 	const char *key, size_t len,      /* pointer and length of key */
174 	phpdbg_command_t const **command, /* address of first matching command  */
175 	phpdbg_command_t const * commands /* command table to be scanned */
176 	)
177 {
178 	const phpdbg_command_t *c;
179 	unsigned int num_matches = 0;
180 
181 	if (len == 1) {
182 		for (c=commands; c->name; c++) {
183 			if (c->alias == key[0]) {
184 				num_matches++;
185 				if ( num_matches == 1 && command) {
186 					*command = c;
187 				}
188 			}
189 		}
190 	} else {
191 		for (c=commands; c->name; c++) {
192 			if (!strncmp(c->name, key, len)) {
193 				++num_matches;
194 				if ( num_matches == 1 && command) {
195 					*command = c;
196 				}
197 			}
198 		}
199 	}
200 
201 	return num_matches;
202 
203 } /* }}} */
204 
phpdbg_do_help_cmd(const char * type)205 void phpdbg_do_help_cmd(const char *type) { /* {{{ */
206 	const char *help;
207 
208 	if (!type) {
209 		pretty_print(get_help("overview!"));
210 		return;
211 	}
212 
213 	help = get_help(type);
214 
215 	if (!help || memcmp(help, "", sizeof("")) == SUCCESS) {
216 		pretty_print(get_help("overview!"));
217 		pretty_print(
218 			"\nrequested help page could not be found");
219 		return;
220 	}
221 
222 	pretty_print(help);
223 } /* }}} */
224 
PHPDBG_COMMAND(help)225 PHPDBG_COMMAND(help) /* {{{ */
226 {
227 	phpdbg_command_t const *cmd;
228 	int n;
229 
230 	if (!param || param->type == EMPTY_PARAM) {
231 		pretty_print(get_help("overview!"));
232 		return SUCCESS;
233 	}
234 
235 	if (param && param->type == STR_PARAM) {
236 	    n = get_command(param->str, param->len, &cmd, phpdbg_prompt_commands);
237 
238 		if (n==1) {
239 			summary_print(cmd);
240 			pretty_print(get_help(cmd->name));
241 			return SUCCESS;
242 
243 		} else if (n>1) {
244 			if (param->len > 1) {
245 				for (cmd=phpdbg_prompt_commands; cmd->name; cmd++) {
246 					if (!strncmp(cmd->name, param->str, param->len)) {
247 						summary_print(cmd);
248 					}
249 				}
250 				pretty_print(get_help("duplicate!"));
251 				return SUCCESS;
252 			} else {
253 				phpdbg_error("Internal help error, non-unique alias \"%c\"", param->str[0]);
254 				return FAILURE;
255 			}
256 
257 		} else { /* no prompt command found so try help topic */
258 		    n = get_command( param->str, param->len, &cmd, phpdbg_help_commands);
259 
260 			if (n>0) {
261 				if (cmd->alias == 'a') {   /* help aliases executes a canned routine */
262 					return cmd->handler(param);
263 				} else {
264 					pretty_print(get_help(cmd->name));
265 					return SUCCESS;
266 				}
267 			} else {
268 			    phpdbg_error("No help topic found for %s", param->str);
269 			}
270 		}
271 	}
272 
273 	return FAILURE;
274 
275 } /* }}} */
276 
PHPDBG_HELP(aliases)277 PHPDBG_HELP(aliases) /* {{{ */
278 {
279 	const phpdbg_command_t *c, *c_sub;
280 	int len;
281 
282 	/* Print out aliases for all commands except help as this one comes last */
283 	phpdbg_writeln("Below are the aliased, short versions of all supported commands");
284 
285 	for(c = phpdbg_prompt_commands; c->name; c++) {
286 		if (c->alias && c->alias != 'h') {
287 			phpdbg_writeln(" %c     %-20s  %s", c->alias, c->name, c->tip);
288 			if (c->subs) {
289 				len = 20 - 1 - c->name_len;
290 				for(c_sub = c->subs; c_sub->alias; c_sub++) {
291 					if (c_sub->alias) {
292 						phpdbg_writeln(" %c %c   %s %-*s  %s",
293 							c->alias, c_sub->alias, c->name, len, c_sub->name, c_sub->tip);
294 					}
295 				}
296 			}
297 		}
298 	}
299 
300 	/* Print out aliases for help as this one comes last, with the added text on how aliases are used */
301 	get_command("h", 1, &c, phpdbg_prompt_commands);
302 	phpdbg_writeln(" %c     %-20s  %s\n", c->alias, c->name, c->tip);
303 
304 	len = 20 - 1 - c->name_len;
305 	for(c_sub = c->subs; c_sub->alias; c_sub++) {
306 		if (c_sub->alias) {
307 			phpdbg_writeln(" %c %c   %s %-*s  %s",
308 				c->alias, c_sub->alias, c->name, len, c_sub->name, c_sub->tip);
309 		}
310 	}
311 
312 	pretty_print(get_help("aliases!"));
313 	return SUCCESS;
314 } /* }}} */
315 
316 
317 /* {{{ Help Text Table
318  * Contains help text entries keyed by a lowercase ascii key.
319  * Text is in ascii and enriched by a simple markup:
320  *   ** toggles bold font emphasis.
321  *   $P insert an bold phpdbg> prompt.
322  *   \  escapes the following character. Note that this is itself escaped inside string
323  *      constants so \\\\ is required to output a single \ e.g. as in namespace names.
324  *
325  * Text will be wrapped according to the STDOUT terminal width, so paragraphs are
326  * flowed using the C stringizing and the CR definition.  Also note that entries
327  * are collated in alphabetic order on key.
328  *
329  * Also note the convention that help text not directly referenceable as a help param
330  * has a key ending in !
331  */
332 #define CR "\n"
333 const phpdbg_help_text_t phpdbg_help_text[] = {
334 
335 /******************************** General Help Topics ********************************/
336 {"overview!", CR
337 "**phpdbg** is a lightweight, powerful and easy to use debugging platform for PHP5.4+" CR
338 "It supports the following commands:" CR CR
339 
340 "**Information**" CR
341 "  **list**      list PHP source" CR
342 "  **info**      displays information on the debug session" CR
343 "  **print**     show opcodes" CR
344 "  **frame**     select a stack frame and print a stack frame summary" CR
345 "  **generator** show active generators or select a generator frame" CR
346 "  **back**      shows the current backtrace" CR
347 "  **help**      provide help on a topic" CR CR
348 
349 "**Starting and Stopping Execution**" CR
350 "  **exec**      set execution context" CR
351 "  **stdin**     set executing script from stdin" CR
352 "  **run**       attempt execution" CR
353 "  **step**      continue execution until other line is reached" CR
354 "  **continue**  continue execution" CR
355 "  **until**     continue execution up to the given location" CR
356 "  **next**      continue execution up to the given location and halt on the first line after it" CR
357 "  **finish**    continue up to end of the current execution frame" CR
358 "  **leave**     continue up to end of the current execution frame and halt after the calling instruction" CR
359 "  **break**     set a breakpoint at the specified target" CR
360 "  **watch**     set a watchpoint on $variable" CR
361 "  **clear**     clear one or all breakpoints" CR
362 "  **clean**     clean the execution environment" CR CR
363 
364 "**Miscellaneous**" CR
365 "  **set**       set the phpdbg configuration" CR
366 "  **source**    execute a phpdbginit script" CR
367 "  **register**  register a phpdbginit function as a command alias" CR
368 "  **sh**        shell a command" CR
369 "  **ev**        evaluate some code" CR
370 "  **quit**      exit phpdbg" CR CR
371 
372 "Type **help <command>** or (**help alias**) to get detailed help on any of the above commands, "
373 "for example **help list** or **h l**.  Note that help will also match partial commands if unique "
374 "(and list out options if not unique), so **help exp** will give help on the **export** command, "
375 "but **help ex** will list the summary for **exec** and **export**." CR CR
376 
377 "Type **help aliases** to show a full alias list, including any registered phpdbginit functions" CR
378 "Type **help syntax** for a general introduction to the command syntax." CR
379 "Type **help options** for a list of phpdbg command line options." CR
380 "Type **help phpdbginit** to show how to customize the debugger environment."
381 },
382 {"options", CR
383 "Below are the command line options supported by phpdbg" CR CR
384                           /* note the extra 4 space index in because of the extra **** */
385 "**Command Line Options and Flags**" CR
386 "  **Option**  **Example Argument**    **Description**" CR
387 "  **-c**      **-c**/my/php.ini       Set php.ini file to load" CR
388 "  **-d**      **-d**memory_limit=4G   Set a php.ini directive" CR
389 "  **-n**                          Disable default php.ini" CR
390 "  **-q**                          Suppress welcome banner" CR
391 "  **-v**                          Enable oplog output" CR
392 "  **-b**                          Disable colour" CR
393 "  **-i**      **-i**my.init           Set .phpdbginit file" CR
394 "  **-I**                          Ignore default .phpdbginit" CR
395 "  **-r**                          Run execution context" CR
396 "  **-rr**                         Run execution context and quit after execution (not respecting breakpoints)" CR
397 "  **-e**                          Generate extended information for debugger/profiler" CR
398 "  **-E**                          Enable step through eval, careful!" CR
399 "  **-s**      **-s=**, **-s**=foo         Read code to execute from stdin with an optional delimiter" CR
400 "  **-S**      **-S**cli               Override SAPI name, careful!" CR
401 "  **-p**      **-p**, **-p=func**, **-p* **   Output opcodes and quit" CR
402 "  **-z**      **-z**extlib            Load Zend extension" CR
403 "  **-h**                          Print the help overview" CR
404 "  **-V**                          Print version number" CR
405 "  **--**      **--** arg1 arg2        Use to delimit phpdbg arguments and php $argv; append any $argv "
406 "argument after it" CR CR
407 
408 "**Reading from stdin**" CR CR
409 
410 "The **-s** option allows inputting a script to execute directly from stdin. The given delimiter "
411 "(\"foo\" in the example) needs to be specified at the end of the input on its own line, followed by "
412 "a line break. If **-rr** has been specified, it is allowed to omit the delimiter (**-s=**) and "
413 "it will read until EOF. See also the help entry for the **stdin** command." CR CR
414 
415 "**Opcode output**" CR CR
416 
417 "Outputting opcodes requires that a file path is passed as last argument. Modes of execution:" CR
418 "**-p** Outputs the main execution context" CR
419 "**-p* **Outputs all opcodes in the whole file (including classes and functions)" CR
420 "**-p=function_name** Outputs opcodes of a given function in the file" CR
421 "**-p=class_name::** Outputs opcodes of all the methods of a given class" CR
422 "**-p=class_name::method** Outputs opcodes of a given method"
423 },
424 
425 {"phpdbginit", CR
426 "Phpdbg uses an debugger script file to initialize the debugger context.  By default, phpdbg looks "
427 "for the file named **.phpdbginit** in the current working directory.  This location can be "
428 "overridden on the command line using the **-i** switch (see **help options** for a more "
429 "details)." CR CR
430 
431 "Debugger scripts can also be executed using the **source** command." CR CR
432 
433 "A script file can contain a sequence of valid debugger commands, comments and embedded PHP "
434 "code. " CR CR
435 
436 "Comment lines are prefixed by the **#** character.  Note that comments are only allowed in script "
437 "files and not in interactive sessions." CR CR
438 
439 "PHP code is delimited by the start and end escape tags **<:** and **:>**. PHP code can be used "
440 "to define application context for a debugging session and also to extend the debugger by defining "
441 "and **register** PHP functions as new commands." CR CR
442 
443 "Also note that executing a **clear** command will cause the current **phpdbginit** to be reparsed "
444 "/ reloaded."
445 },
446 
447 {"syntax", CR
448 "Commands start with a keyword, and some (**break**, "
449 "**info**, **set**, **print** and **list**) may include a subcommand keyword.  All keywords are "
450 "lower case but also have a single letter alias that may be used as an alternative to typing in the"
451 "keyword in full.  Note some aliases are uppercase, and that keywords cannot be abbreviated other "
452 "than by substitution by the alias." CR CR
453 
454 "Some commands take an argument.  Arguments are typed according to their format:" CR
455 "     *  **omitted**" CR
456 "     *  **address**      **0x** followed by a hex string" CR
457 "     *  **number**       an optionally signed number" CR
458 "     *  **method**       a valid **Class::methodName** expression" CR
459 "     *  **func#op**      a valid **Function name** follow by # and an integer" CR
460 "     *  **method#op**    a valid **Class::methodName** follow by # and an integer" CR
461 "     *  **string**       a general string" CR
462 "     *  **function**     a valid **Function name**" CR
463 "     *  **file:line**    a valid **filename** follow by : and an integer" CR CR
464 
465 "In some cases the type of the argument enables the second keyword to be omitted." CR CR
466 
467 "Type **help** for an overview of all commands and type **help <command>** to get detailed help "
468 "on any specific command." CR CR
469 
470 "**Valid Examples**" CR CR
471 
472 "     $P quit" CR
473 "     $P q" CR
474 "     Quit the debugger" CR CR
475 
476 "     $P ev $total[2]" CR
477 "     Evaluate and print the variable $total[2] in the current stack frame" CR
478 "    " CR
479 "     $P break 200" CR
480 "     $P b my_source.php:200" CR
481 "     Break at line 200 in the current source and in file **my_source.php**. " CR CR
482 
483 "     $P b @ ClassX::get_args if $arg[0] == \"fred\"" CR
484 "     $P b ~ 3" CR
485 "     Break at ClassX::get_args() if $arg[0] == \"fred\" and delete breakpoint 3" CR CR
486 
487 "**Examples of invalid commands**" CR
488 
489 "     $P #This is a comment" CR
490 "     Comments introduced by the **#** character are only allowed in **phpdbginit** script files."
491 },
492 
493 /******************************** Help Codicils ********************************/
494 {"aliases!", CR
495 "Note that aliases can be used for either command or sub-command keywords or both, so **info b** "
496 "is a synonym for **info break** and **l func** for **list func**, etc." CR CR
497 
498 "Note that help will also accept any alias as a parameter and provide help on that command, for example **h p** will provide help on the print command."
499 },
500 
501 {"duplicate!", CR
502 "Parameter is not unique. For detailed help select help on one of the above commands."
503 },
504 
505 /******************************** Help on Commands ********************************/
506 {"back",
507 "Provide a formatted backtrace using the standard debug_backtrace() functionality.  An optional "
508 "unsigned integer argument specifying the maximum number of frames to be traced; if omitted then "
509 "a complete backtrace is given." CR CR
510 
511 "**Examples**" CR CR
512 "    $P back 5" CR
513 "    $P t " CR
514 " " CR
515 "A backtrace can be executed at any time during execution."
516 },
517 
518 {"break",
519 "Breakpoints can be set at a range of targets within the execution environment.  Execution will "
520 "be paused if the program flow hits a breakpoint.  The break target can be one of the following "
521 "types:" CR CR
522 
523 "  **Target**   **Alias** **Purpose**" CR
524 "  **at**       **@**     specify breakpoint by location and condition" CR
525 "  **del**      **~**     delete breakpoint by breakpoint identifier number" CR CR
526 
527 "**Break at** takes two arguments. The first is any valid target. The second "
528 "is a valid PHP expression which will trigger the break in "
529 "execution, if evaluated as true in a boolean context at the specified target." CR CR
530 
531 "Note that breakpoints can also be disabled and re-enabled by the **set break** command." CR CR
532 
533 "**Examples**" CR CR
534 "    $P break test.php:100" CR
535 "    $P b test.php:100" CR
536 "    Break execution at line 100 of test.php" CR CR
537 
538 "    $P break 200" CR
539 "    $P b 200" CR
540 "    Break execution at line 200 of the currently PHP script file" CR CR
541 
542 "    $P break \\\\mynamespace\\\\my_function" CR
543 "    $P b \\\\mynamespace\\\\my_function" CR
544 "    Break execution on entry to \\\\mynamespace\\\\my_function" CR CR
545 
546 "    $P break classX::method" CR
547 "    $P b classX::method" CR
548 "    Break execution on entry to classX::method" CR CR
549 
550 "    $P break 0x7ff68f570e08" CR
551 "    $P b 0x7ff68f570e08" CR
552 "    Break at the opline at the address 0x7ff68f570e08" CR CR
553 
554 "    $P break my_function#14" CR
555 "    $P b my_function#14" CR
556 "    Break at the opline #14 of the function my_function" CR CR
557 
558 "    $P break \\\\my\\\\class::method#2" CR
559 "    $P b \\\\my\\\\class::method#2" CR
560 "    Break at the opline #2 of the method \\\\my\\\\class::method" CR CR
561 
562 "    $P break test.php:#3" CR
563 "    $P b test.php:#3" CR
564 "    Break at opline #3 in test.php" CR CR
565 
566 "    $P break if $cnt > 10" CR
567 "    $P b if $cnt > 10" CR
568 "    Break when the condition ($cnt > 10) evaluates to true" CR CR
569 
570 "    $P break at phpdbg::isGreat if $opt == 'S'" CR
571 "    $P break @ phpdbg::isGreat if $opt == 'S'" CR
572 "    Break at any opcode in phpdbg::isGreat when the condition ($opt == 'S') is true" CR CR
573 
574 "    $P break at test.php:20 if !isset($x)" CR
575 "    Break at every opcode on line 20 of test.php when the condition evaluates to true" CR CR
576 
577 "    $P break ZEND_ADD" CR
578 "    $P b ZEND_ADD" CR
579 "    Break on any occurrence of the opcode ZEND_ADD" CR CR
580 
581 "    $P break del 2" CR
582 "    $P b ~ 2" CR
583 "    Remove breakpoint 2" CR CR
584 
585 "Note: Conditional breaks are costly in terms of runtime overhead. Use them only when required "
586 "as they significantly slow execution." CR CR
587 
588 "Note: An address is only valid for the current compilation."
589 },
590 
591 {"clean",
592 "Classes, constants or functions can only be declared once in PHP.  You may experience errors "
593 "during a debug session if you attempt to recompile a PHP source.  The clean command clears "
594 "the Zend runtime tables which holds the sets of compiled classes, constants and functions, "
595 "releasing any associated storage back into the storage pool.  This enables recompilation to "
596 "take place." CR CR
597 
598 "Note that you cannot selectively trim any of these resource pools. You can only do a complete "
599 "clean."
600 },
601 
602 {"clear",
603 "Clearing breakpoints means you can once again run code without interruption." CR CR
604 
605 "Note: use break delete N to clear a specific breakpoint." CR CR
606 
607 "Note: if all breakpoints are cleared, then the PHP script will run until normal completion."
608 },
609 
610 {"ev",
611 "The **ev** command takes a string expression which it evaluates and then displays. It "
612 "evaluates in the context of the lowest (that is the executing) frame, unless this has first "
613 "been explicitly changed by issuing a **frame** command. " CR CR
614 
615 "**Examples**" CR CR
616 "    $P ev $variable" CR
617 "    Will print_r($variable) on the console, if it is defined" CR CR
618 
619 "    $P ev $variable = \"Hello phpdbg :)\"" CR
620 "    Will set $variable in the current scope" CR CR
621 
622 "Note that **ev** allows any valid PHP expression including assignments, function calls and "
623 "other write statements.  This enables you to change the environment during execution, so care "
624 "is needed here.  You can even call PHP functions which have breakpoints defined. " CR CR
625 
626 "Note: **ev** will always show the result, so do not prefix the code with **return**"
627 },
628 
629 {"exec",
630 "The **exec** command sets the execution context, that is the script to be executed.  The "
631 "execution context must be defined either by executing the **exec** command or by using the "
632 "**-e** command line option." CR CR
633 
634 "Note that the **exec** command also can be used to replace a previously defined execution "
635 "context." CR CR
636 
637 "**Examples**" CR CR
638 
639 "    $P exec /tmp/script.php" CR
640 "    $P e /tmp/script.php" CR
641 "    Set the execution context to **/tmp/script.php**"
642 },
643 
644 {"stdin",
645 "The **stdin** command takes a string serving as delimiter. It will then read all the input from "
646 "stdin until encountering the given delimiter on a standalone line. It can also be passed at "
647 "startup using the **-s=** command line option (the delimiter then is optional if **-rr** is "
648 "also passed - in that case it will just read until EOF)." CR
649 "This input will be then compiled as PHP code and set as execution context." CR CR
650 
651 "**Example**" CR CR
652 
653 "    $P stdin foo" CR
654 "    <?php" CR
655 "    echo \"Hello, world!\\n\";" CR
656 "    foo"
657 },
658 
659 //*********** Does F skip any breakpoints lower stack frames or only the current??
660 {"finish",
661 "The **finish** command causes control to be passed back to the vm, continuing execution.  Any "
662 "breakpoints that are encountered within the current stack frame will be skipped.  Execution "
663 "will then continue until the next breakpoint after leaving the stack frame or until "
664 "completion of the script" CR CR
665 
666 "Note when **step**ping is enabled, any opcode steps within the current stack frame are also "
667 "skipped. "CR CR
668 
669 "Note **finish** will trigger a \"not executing\" error if not executing."
670 },
671 
672 {"frame",
673 "The **frame** takes an optional integer argument. If omitted, then the current frame is displayed. "
674 "If specified, then the current scope is set to the corresponding frame listed in a **back** trace. "
675 "This can be used to allowing access to the variables in a higher stack frame than that currently being executed." CR CR
676 
677 "**Examples**" CR CR
678 "    $P frame 2" CR
679 "    $P ev $count" CR
680 "    Go to frame 2 and print out variable **$count** in that frame" CR CR
681 
682 "Note that this frame scope is discarded when execution continues, with the execution frame "
683 "then reset to the lowest executing frame."
684 },
685 
686 {"generator",
687 "The **generator** command takes an optional integer argument. If omitted, then a list of the "
688 "currently active generators is displayed. If specified then the current scope is set to the frame "
689 "of the generator with the corresponding object handle. This can be used to inspect any generators "
690 "not in the current **back** trace." CR CR
691 
692 "**Examples**" CR CR
693 "    $P generator" CR
694 "    List of generators, with the #id being the object handle, e.g.:" CR
695 "    #3: my_generator(argument=\"value\") at test.php:5" CR
696 "    $P g 3" CR
697 "    $P ev $i" CR
698 "    Go to frame of generator with object handle 3 and print out variable **$i** in that frame" CR CR
699 
700 "Note that this frame scope is discarded when execution continues, with the execution frame "
701 "then reset to the lowest executing frame."
702 },
703 
704 {"info",
705 "**info** commands provide quick access to various types of information about the PHP environment" CR
706 "By default general information about environment and PHP build is shown." CR
707 "Specific info commands are show below:" CR CR
708 
709 "  **Target**   **Alias**  **Purpose**" CR
710 "  **break**      **b**      show current breakpoints" CR
711 "  **files**      **F**      show included files" CR
712 "  **classes**    **c**      show loaded classes" CR
713 "  **funcs**      **f**      show loaded functions" CR
714 "  **error**      **e**      show last error" CR
715 "  **constants**  **d**      show user-defined constants" CR
716 "  **vars**       **v**      show active variables" CR
717 "  **globals**    **g**      show superglobal variables" CR
718 "  **literal**    **l**      show active literal constants" CR
719 "  **memory**     **m**      show memory manager stats"
720 },
721 
722 // ******** same issue about breakpoints in called frames
723 {"leave",
724 "The **leave** command causes control to be passed back to the vm, continuing execution.  Any "
725 "breakpoints that are encountered within the current stack frame will be skipped.  In effect a "
726 "temporary breakpoint is associated with any return opcode, so that a break in execution occurs "
727 "before leaving the current stack frame. This allows inspection / modification of any frame "
728 "variables including the return value before it is returned" CR CR
729 
730 "**Examples**" CR CR
731 
732 "    $P leave" CR
733 "    $P L" CR CR
734 
735 "Note when **step**ping is enabled, any opcode steps within the current stack frame are also "
736 "skipped. "CR CR
737 
738 "Note **leave** will trigger a \"not executing\" error if not executing."
739 },
740 
741 {"list",
742 "The list command displays source code for the given argument.  The target type is specified by "
743 "a second subcommand keyword:" CR CR
744 
745 "  **Type**     **Alias**  **Purpose**" CR
746 "  **lines**    **l**      List N lines from the current execution point" CR
747 "  **func**     **f**      List the complete source for a specified function" CR
748 "  **method**   **m**      List the complete source for a specified class::method" CR
749 "  **class**    **c**      List the complete source for a specified class" CR CR
750 
751 "Note that the context of **lines**, **func** and **method** can be determined by parsing the "
752 "argument, so these subcommands are optional.  However, you must specify the **class** keyword "
753 "to list off a class." CR CR
754 
755 "**Examples**" CR CR
756 "    $P list 2" CR
757 "    $P l l 2" CR
758 "    List the next 2 lines from the current file" CR CR
759 
760 "    $P list my_function" CR
761 "    $P l f my_function" CR
762 "    List the source of the function **my_function**" CR CR
763 
764 //************ ????
765 "    $P list func .mine" CR
766 "    $P l f .mine" CR
767 "    List the source of the method **mine** from the active class in scope" CR CR
768 
769 "    $P list m my::method" CR
770 "    $P l my::method" CR
771 "    List the source of **my::method**" CR CR
772 
773 "    $P list c myClass" CR
774 "    $P l c myClass" CR
775 "    List the source of **myClass**" CR CR
776 
777 "Note that functions and classes can only be listed if the corresponding classes and functions "
778 "table in the Zend executor has a corresponding entry.  You can use the compile command to "
779 "populate these tables for a given execution context."
780 },
781 
782 {"continue",
783 "Continue with execution after hitting a break or watchpoint" CR CR
784 
785 "**Examples**" CR CR
786 "    $P continue" CR
787 "    $P c" CR
788 "    Continue executing until the next break or watchpoint" CR CR
789 
790 "Note **continue** will trigger a \"not running\" error if not executing."
791 },
792 
793 {"print",
794 "By default, print will show the opcodes of the current execution context." CR
795 "Other printing commands give access to instruction information." CR
796 "Specific printers loaded are show below:" CR CR
797 
798 "  **Type**    **Alias**  **Purpose**" CR
799 "  **exec**    **e**      print out the instructions in the execution context" CR
800 "  **opline**  **o**      print out the instruction in the current opline" CR
801 "  **class**   **c**      print out the instructions in the specified class" CR
802 "  **method**  **m**      print out the instructions in the specified method" CR
803 "  **func**    **f**      print out the instructions in the specified function" CR
804 "  **stack**   **s**      print out the instructions in the current stack" CR CR
805 
806 "In case passed argument does not match a specific printing command, it will treat it as function or method name and print its opcodes" CR CR
807 
808 "**Examples**" CR CR
809 "    $P print class \\\\my\\\\class" CR
810 "    $P p c \\\\my\\\\class" CR
811 "    Print the instructions for the methods in \\\\my\\\\class" CR CR
812 
813 "    $P print method \\\\my\\\\class::method" CR
814 "    $P p m \\\\my\\\\class::method" CR
815 "    Print the instructions for \\\\my\\\\class::method" CR CR
816 
817 "    $P print func .getSomething" CR
818 "    $P p f .getSomething" CR
819 //************* Check this local method scope
820 "    Print the instructions for ::getSomething in the active scope" CR CR
821 
822 "    $P print func my_function" CR
823 "    $P p f my_function" CR
824 "    Print the instructions for the global function my_function" CR CR
825 
826 "    $P print opline" CR
827 "    $P p o" CR
828 "    Print the instruction for the current opline" CR CR
829 
830 "    $P print exec" CR
831 "    $P p e" CR
832 "    Print the instructions for the execution context" CR CR
833 
834 "    $P print stack" CR
835 "    $P p s" CR
836 "    Print the instructions for the current stack"
837 },
838 
839 {"register",
840 //******* Needs a general explanation of the how registered functions work
841 "Register any global function for use as a command in phpdbg console" CR CR
842 
843 "**Examples**" CR CR
844 "    $P register scandir" CR
845 "    $P R scandir" CR
846 "    Will register the scandir function for use in phpdbg" CR CR
847 
848 "Note: arguments passed as strings, return (if present) print_r'd on console"
849 },
850 
851 {"run",
852 "Enter the vm, starting execution. Execution will then continue until the next breakpoint "
853 "or completion of the script. Add parameters you want to use as $argv. Add a trailing "
854 "**< filename** for reading STDIN from a file." CR CR
855 
856 "**Examples**" CR CR
857 
858 "    $P run" CR
859 "    $P r" CR
860 "    Will cause execution of the context, if it is set" CR CR
861 "    $P r test < foo.txt" CR
862 "    Will execute with $argv[1] == \"test\" and read from the foo.txt file for STDIN" CR CR
863 
864 "Note that the execution context must be set. If not previously compiled, then the script will "
865 "be compiled before execution."
866 },
867 
868 {"set",
869 "The **set** command is used to configure how phpdbg looks and behaves.  Specific set commands "
870 "are as follows:" CR CR
871 
872 "   **Type**    **Alias**    **Purpose**" CR
873 "   **prompt**     **p**     set the prompt" CR
874 "   **color**      **c**     set color  <element> <color>" CR
875 "   **colors**     **C**     set colors [<on|off>]" CR
876 "   **break**      **b**     set break **id** <on|off>" CR
877 "   **breaks**     **B**     set breaks [<on|off>]" CR
878 "   **quiet**      **q**     set quiet [<on|off>]" CR
879 "   **stepping**   **s**     set stepping [<opcode|line>]" CR
880 "   **refcount**   **r**     set refcount [<on|off>] " CR CR
881 
882 "Valid colors are **none**, **white**, **red**, **green**, **yellow**, **blue**, **purple**, "
883 "**cyan** and **black**.  All colours except **none** can be followed by an optional "
884 "**-bold** or **-underline** qualifier." CR CR
885 
886 "Color elements can be one of **prompt**, **notice**, or **error**." CR CR
887 
888 "**Examples**" CR CR
889 "     $P S C on" CR
890 "     Set colors on" CR CR
891 
892 "     $P set p >" CR
893 "     $P set color prompt white-bold" CR
894 "     Set the prompt to a bold >" CR CR
895 
896 "     $P S c error red-bold" CR
897 "     Use red bold for errors" CR CR
898 
899 "     $P S refcount on" CR
900 "     Enable refcount display when hitting watchpoints" CR CR
901 
902 "     $P S b 4 off" CR
903 "     Temporarily disable breakpoint 4.  This can be subsequently re-enabled by a **S b 4 on**." CR
904 //*********** check oplog syntax
905 },
906 
907 {"sh",
908 "Direct access to shell commands saves having to switch windows/consoles" CR CR
909 
910 "**Examples**" CR CR
911 "    $P sh ls /usr/src/php-src" CR
912 "    Will execute ls /usr/src/php-src, displaying the output in the console"
913 //*********** what does this mean????Note: read only commands please!
914 },
915 
916 {"source",
917 "Sourcing a **phpdbginit** script during your debugging session might save some time." CR CR
918 
919 "**Examples**" CR CR
920 
921 "    $P source /my/init" CR
922 "    $P < /my/init" CR
923 "    Will execute the phpdbginit file at /my/init" CR CR
924 },
925 
926 {"export",
927 "Exporting breakpoints allows you to share, and or save your current debugging session" CR CR
928 
929 "**Examples**" CR CR
930 
931 "    $P export /my/exports" CR
932 "    $P > /my/exports" CR
933 "    Will export all breakpoints to /my/exports" CR CR
934 },
935 
936 {"step",
937 "Execute opcodes until next line" CR CR
938 
939 "**Examples**" CR CR
940 
941 "    $P s" CR
942 "    Will continue and break again in the next encountered line" CR CR
943 },
944 {"next",
945 "The **next** command causes control to be passed back to the vm, continuing execution. Any "
946 "breakpoints that are encountered before the next source line will be skipped. Execution will"
947 "be stopped when that line is left." CR CR
948 
949 "Note when **step**ping is enabled, any opcode steps within the current line are also skipped. "CR CR
950 
951 "Note that if the next line is **not** executed then **all** subsequent breakpoints will be "
952 "skipped. " CR CR
953 
954 "Note **next** will trigger a \"not executing\" error if not executing."
955 
956 },
957 {"until",
958 "The **until** command causes control to be passed back to the vm, continuing execution. Any "
959 "breakpoints that are encountered before the next source line will be skipped. Execution "
960 "will then continue until the next breakpoint or completion of the script" CR CR
961 
962 "Note when **step**ping is enabled, any opcode steps within the current line are also skipped. "CR CR
963 
964 "Note that if the next line is **not** executed then **all** subsequent breakpoints will be "
965 "skipped. " CR CR
966 
967 "Note **until** will trigger a \"not executing\" error if not executing."
968 
969 },
970 {"watch",
971 "Sets watchpoints on variables as long as they are defined" CR
972 "Passing no parameter to **watch**, lists all actually active watchpoints" CR CR
973 
974 "**Format for $variable**" CR CR
975 "   **$var**      Variable $var" CR
976 "   **$var[]**    All array elements of $var" CR
977 "   **$var->**    All properties of $var" CR
978 "   **$var->a**   Property $var->a" CR
979 "   **$var[b]**   Array element with key b in array $var" CR CR
980 
981 "Subcommands of **watch**:" CR CR
982 
983 "   **Type**     **Alias**      **Purpose**" CR
984 "   **array**       **a**       Sets watchpoint on array/object to observe if an entry is added or removed" CR
985 "   **recursive**   **r**       Watches variable recursively and automatically adds watchpoints if some entry is added to an array/object" CR
986 "   **delete**      **d**       Removes watchpoint" CR CR
987 
988 "Note when **recursive** watchpoints are removed, watchpoints on all the children are removed too" CR CR
989 
990 "**Examples**" CR CR
991 "     $P watch" CR
992 "     List currently active watchpoints" CR CR
993 
994 "     $P watch $array" CR
995 "     $P w $array" CR
996 "     Set watchpoint on $array" CR CR
997 
998 "     $P watch recursive $obj->" CR
999 "     $P w r $obj->" CR
1000 "     Set recursive watchpoint on $obj->" CR CR
1001 
1002 "     $P watch delete $obj->a" CR
1003 "     $P w d $obj->a" CR
1004 "     Remove watchpoint $obj->a" CR CR
1005 
1006 "Technical note: If using this feature with a debugger, you will get many segmentation faults, each time when a memory page containing a watched address is hit." CR
1007 "                You then you can continue, phpdbg will remove the write protection, so that the program can continue." CR
1008 "                If phpdbg could not handle that segfault, the same segfault is triggered again and this time phpdbg will abort."
1009 },
1010 {NULL, NULL /* end of table marker */}
1011 };  /* }}} */
1012