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