1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
%error-verbose
%{
/*
* phpdbg_parser.y
* (from php-src root)
* flex sapi/phpdbg/dev/phpdbg_lexer.l
* bison sapi/phpdbg/dev/phpdbg_parser.y
*/
#include "phpdbg.h"
#include "phpdbg_cmd.h"
#include "phpdbg_utils.h"
#include "phpdbg_prompt.h"
#define YYSTYPE phpdbg_param_t
#include "phpdbg_parser.h"
#include "phpdbg_lexer.h"
ZEND_EXTERN_MODULE_GLOBALS(phpdbg);
int yyerror(phpdbg_param_t *stack, yyscan_t scanner, const char *msg) {
phpdbg_error("Parse Error: %s", msg);
}
void phpdbg_debug_param(const phpdbg_param_t *param, const char *msg) {
if (param && param->type) {
switch (param->type) {
case STR_PARAM:
fprintf(stderr, "%s STR_PARAM(%s=%d)\n", msg, param->str, param->len);
break;
case ADDR_PARAM:
fprintf(stderr, "%s ADDR_PARAM(%lu)\n", msg, param->addr);
break;
case FILE_PARAM:
fprintf(stderr, "%s FILE_PARAM(%s:%d)\n", msg, param->file.name, param->file.line);
break;
case METHOD_PARAM:
fprintf(stderr, "%s METHOD_PARAM(%s::%s)\n", msg, param->method.class, param->method.name);
break;
case NUMERIC_METHOD_PARAM:
fprintf(stderr, "%s NUMERIC_METHOD_PARAM(%s::%s)\n", msg, param->method.class, param->method.name);
break;
case NUMERIC_FUNCTION_PARAM:
fprintf(stderr, "%s NUMERIC_FUNCTION_PARAM(%s::%s)\n", msg, param->str, param->num);
break;
case NUMERIC_PARAM:
fprintf(stderr, "%s NUMERIC_PARAM(%ld)\n", msg, param->num);
break;
case COND_PARAM:
fprintf(stderr, "%s COND_PARAM(%s=%d)\n", msg, param->str, param->len);
break;
}
}
}
void phpdbg_stack_free(phpdbg_param_t *stack) {
if (stack && stack->next) {
phpdbg_param_t *remove = stack->next;
while (remove) {
phpdbg_param_t *next = NULL;
if (remove->next)
next = remove->next;
switch (remove->type) {
case STR_PARAM:
if (remove->str) {
free(remove->str);
}
break;
case FILE_PARAM:
if (remove->file.name) {
free(remove->file.name);
}
break;
}
free(remove);
if (next)
remove = next;
else break;
}
}
}
static void phpdbg_stack_push(phpdbg_param_t *stack, phpdbg_param_t *param) {
phpdbg_param_t *next = calloc(1, sizeof(phpdbg_param_t));
if (!next)
return;
*(next) = *(param);
if (stack->top == NULL) {
stack->top = next;
stack->next = next;
} else {
stack->top->next = next;
next->top = stack->top;
stack->top = next;
}
phpdbg_debug_param(next, "push ->");
stack->len++;
}
phpdbg_command_t* phpdbg_stack_resolve(const phpdbg_command_t *commands, phpdbg_param_t **top, char **why) {
const phpdbg_command_t *command = commands;
phpdbg_param_t *name = *top;
phpdbg_command_t *matched[3] = {NULL, NULL, NULL};
ulong matches = 0L;
while (command && command->name && command->handler) {
if (command->name_len >= name->len) {
if (memcmp(command->name, name->str, name->len) == SUCCESS) {
if (matches < 3) {
matched[matches] = command;
matches++;
} else break;
}
}
command++;
}
switch (matches) {
case 0: {
asprintf(
why,
"The command %s could not be found",
name->str);
} break;
case 1: {
(*top) = (*top)->next;
if (matched[0]->subs && (*top) && ((*top)->type == STR_PARAM)) {
command = phpdbg_stack_resolve(matched[0]->subs, top, why);
if (command) {
phpdbg_notice(
"Command matching with sub command %s %s",
matched[0]->name, command->name);
return command;
}
}
phpdbg_notice(
"Command matching with %s",
matched[0]->name);
return matched[0];
} break;
default: {
asprintf(
why,
"The command %s is ambigious, matching %d commands",
name->str, matches);
}
}
return NULL;
}
int phpdbg_stack_execute(phpdbg_param_t *stack, char **why) {
phpdbg_param_t *command = NULL,
*params = NULL;
phpdbg_command_t *handler = NULL;
if (stack->type != STACK_PARAM) {
asprintf(
why, "the passed argument was not a stack !!");
return FAILURE;
}
if (!stack->len) {
asprintf(
why, "the stack contains nothing !!");
return FAILURE;
}
command = (phpdbg_param_t*) stack->next;
switch (command->type) {
case EVAL_PARAM:
PHPDBG_COMMAND_HANDLER(eval)(command, NULL TSRMLS_CC);
break;
case SHELL_PARAM:
PHPDBG_COMMAND_HANDLER(shell)(command, NULL TSRMLS_CC);
break;
case STR_PARAM: {
/* do resolve command(s) */
handler = phpdbg_stack_resolve(
phpdbg_prompt_commands, &command, why);
if (handler) {
/* get top of stack */
params = command;
/* prepare params */
while (params) {
phpdbg_debug_param(params, "-> ...");
params = params->next;
}
return SUCCESS;
} else {
return FAILURE;
}
} break;
default:
asprintf(
why, "the first parameter makes no sense !!");
return FAILURE;
}
return SUCCESS;
}
%}
%code requires {
#include "phpdbg.h"
#ifndef YY_TYPEDEF_YY_SCANNER_T
#define YY_TYPEDEF_YY_SCANNER_T
typedef void* yyscan_t;
#endif
}
%expect 1
%output "sapi/phpdbg/phpdbg_parser.c"
%defines "sapi/phpdbg/phpdbg_parser.h"
%define api.pure
%lex-param { yyscan_t scanner }
%parse-param { phpdbg_param_t *stack }
%parse-param { yyscan_t scanner }
%token C_TRUTHY "truthy (true, on, yes or enabled)"
%token C_FALSY "falsy (false, off, no or disabled)"
%token C_STRING "string (some input, perhaps)"
%token C_EVAL "eval"
%token C_SHELL "shell"
%token C_IF "if (condition)"
%token T_DIGITS "digits (numbers)"
%token T_LITERAL "literal (string)"
%token T_METHOD "method"
%token T_NUMERIC_METHOD "method opline address"
%token T_NUMERIC_FUNCTION "function opline address"
%token T_OPLINE "opline"
%token T_FILE "file"
%token T_ID "identifier (command or function name)"
%token T_INPUT "input (input string or data)"
%token T_UNEXPECTED "input"
%%
input
: handler
;
parameters
: parameter { phpdbg_stack_push(stack, &$1); }
| parameters parameter { phpdbg_stack_push(stack, &$2); }
;
params
: parameters
| /* empty */
;
normal
: T_ID { phpdbg_stack_push(stack, &$1); }
| normal T_ID { phpdbg_stack_push(stack, &$2); }
;
special
: C_EVAL T_INPUT { $$ = $2; $$.type = EVAL_PARAM; }
| C_SHELL T_INPUT { $$ = $2; $$.type = SHELL_PARAM; }
;
command
: normal
| special { phpdbg_stack_push(stack, &$1); }
;
parameter
: T_DIGITS { $$ = $1; }
| T_FILE { $$ = $1; }
| T_METHOD { $$ = $1; }
| T_NUMERIC_METHOD { $$ = $1; }
| T_NUMERIC_FUNCTION { $$ = $1; }
| T_OPLINE { $$ = $1; }
| T_ID { $$ = $1; }
| T_LITERAL { $$ = $1; }
| C_TRUTHY { $$ = $1; }
| C_FALSY { $$ = $1; }
| C_IF T_INPUT { $$ = $2; $$.type = COND_PARAM; }
;
handler
: command params
;
%%
|