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