-
Notifications
You must be signed in to change notification settings - Fork 34
/
dumper.h
245 lines (223 loc) · 6.4 KB
/
dumper.h
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
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2012 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Eric Zhang <ericzhang.buaa@gmail.com> |
+----------------------------------------------------------------------+
*/
#ifndef OD_DUMPER_H
#define OD_DUMPER_H
#if PHP_VERSION_ID >= 50399
# define OD_ZNODE znode_op
# define OD_TYPE(z) z##_type
# define OD_ZNODE_ELEM(z,v) z.v
#else
# define OD_ZNODE znode
# define OD_TYPE(z) z.op_type
# define OD_ZNODE_ELEM(z,v) z.u.v
#endif
#define BUFF_SIZE 4096
static inline char* od_dump_type(zend_uchar type) {
switch (type) {
case IS_UNUSED:
return "UNUSED";
case IS_CONST:
return "CONST";
case IS_VAR:
return "VAR";
case IS_TMP_VAR:
return "TMP_VAR";
#if (PHP_MAJOR_VERSION > 5) || (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 1)
case IS_CV:
return "CV";
#endif
default:
return "";
}
}
static inline void od_dump_zval(zval val, char *output) {
switch (val.type) {
case IS_NULL:
sprintf(output, "NULL");
break;
case IS_LONG:
sprintf(output, "%ld", val.value.lval);
break;
case IS_DOUBLE:
sprintf(output, "%g", val.value.lval);
break;
case IS_STRING:
sprintf(output, "%s", val.value.str.val);
break;
case IS_BOOL:
sprintf(output, val.value.lval != 0 ? "TRUE" : "FALSE");
break;
case IS_RESOURCE:
sprintf(output, "res#%ld", val.value.lval);
break;
case IS_ARRAY:
sprintf(output, "[ARRAY]");
break;
case IS_OBJECT:
sprintf(output, "[OBJECT]");
break;
case IS_CONSTANT:
sprintf(output, "[CONSTANT]");
break;
case IS_CONSTANT_ARRAY:
sprintf(output, "[CONSTANT_ARRAY");
break;
default:
sprintf(output, "[UNKNOWN]");
break;
}
}
static inline void od_raw_dump_zval(zval val, char *output) {
switch (val.type) {
case IS_NULL:
case IS_LONG:
case IS_BOOL:
case IS_RESOURCE:
sprintf(output, "%ld", val.value.lval);
break;
case IS_DOUBLE:
sprintf(output, "%g", val.value.lval);
break;
case IS_STRING:
sprintf(output, "%s", val.value.str.val);
break;
case IS_ARRAY:
case IS_OBJECT:
case IS_CONSTANT:
case IS_CONSTANT_ARRAY:
default:
sprintf(output, "");
break;
}
}
static inline void od_dump_znode(OD_ZNODE node, zend_uchar type, char *output) {
switch (type) {
case IS_UNUSED:
sprintf(output, "UNUSED");
break;
case IS_CONST:
#if PHP_VERSION_ID >= 50399
od_dump_zval(*node.zv, output);
#else
od_dump_zval(node.u.constant, output);
#endif
break;
case IS_VAR:
sprintf(output, "$%u", OD_ZNODE_ELEM(node,var));
break;
case IS_TMP_VAR:
sprintf(output, "#%u", OD_ZNODE_ELEM(node,var));
break;
#if (PHP_MAJOR_VERSION > 5) || (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 1)
case IS_CV:
sprintf(output, "~%u", OD_ZNODE_ELEM(node,var));
break;
#endif
default:
sprintf(output, "");
break;
}
}
static inline void od_raw_dump_znode(OD_ZNODE node, zend_uchar type, char *output) {
switch (type) {
case IS_UNUSED:
sprintf(output, "");
break;
case IS_CONST:
#if PHP_VERSION_ID >= 50399
od_raw_dump_zval(*node.zv, output);
#else
od_raw_dump_zval(node.u.constant, output);
#endif
break;
case IS_VAR:
case IS_TMP_VAR:
#if (PHP_MAJOR_VERSION > 5) || (PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION >= 1)
case IS_CV:
#endif
sprintf(output, "%u", OD_ZNODE_ELEM(node,var));
break;
default:
sprintf(output, "");
break;
}
}
static inline zval* od_dump_op_array(zend_op op) {
zval *opcode;
char *op1 = (char *)emalloc(BUFF_SIZE);
char *op2 = (char *)emalloc(BUFF_SIZE);
char *result = (char *)emalloc(BUFF_SIZE);
od_dump_znode(op.op1, op.OD_TYPE(op1), op1);
od_dump_znode(op.op2, op.OD_TYPE(op2), op2);
od_dump_znode(op.result, op.OD_TYPE(result), result);
MAKE_STD_ZVAL(opcode);
array_init(opcode);
add_assoc_long(opcode, "lineno", op.lineno);
add_assoc_string(opcode, "opcode", (char *)od_opcodes[op.opcode], 1);
add_assoc_string(opcode, "op1_type", od_dump_type(op.OD_TYPE(op1)), 1);
add_assoc_string(opcode, "op2_type", od_dump_type(op.OD_TYPE(op2)), 1);
add_assoc_string(opcode, "result_type", od_dump_type(op.OD_TYPE(result)), 1);
add_assoc_string(opcode, "op1", op1, 1);
add_assoc_string(opcode, "op2", op2, 1);
add_assoc_string(opcode, "result", result, 1);
efree(op1);
efree(op2);
efree(result);
return opcode;
}
static inline void od_dump_op(zend_op op, char *opcode) {
char *op1 = (char *)emalloc(BUFF_SIZE);
char *op2 = (char *)emalloc(BUFF_SIZE);
char *result = (char *)emalloc(BUFF_SIZE);
od_dump_znode(op.op1, op.OD_TYPE(op1), op1);
od_dump_znode(op.op2, op.OD_TYPE(op2), op2);
od_dump_znode(op.result, op.OD_TYPE(result), result);
sprintf(opcode, "line: %u\nopcode: %s\nop1_type: %s\nop2_type: %s\nresult_type: %s\nop1: %s\nop2: %s\nresult: %s\n",
op.lineno,
od_opcodes[op.opcode],
od_dump_type(op.OD_TYPE(op1)),
od_dump_type(op.OD_TYPE(op2)),
od_dump_type(op.OD_TYPE(result)),
op1,
op2,
result);
efree(op1);
efree(op2);
efree(result);
}
static inline void od_raw_dump_op(zend_op op, char *opcode) {
char *op1 = (char *)emalloc(BUFF_SIZE);
char *op2 = (char *)emalloc(BUFF_SIZE);
char *result = (char *)emalloc(BUFF_SIZE);
od_dump_znode(op.op1, op.OD_TYPE(op1), op1);
od_dump_znode(op.op2, op.OD_TYPE(op2), op2);
od_dump_znode(op.result, op.OD_TYPE(result), result);
sprintf(opcode, "line: %u\topcode: %u\top1_type: %u\top2_type: %u\tresult_type: %u\top1: %s\top2: %s\tresult: %s\n",
op.lineno,
op.opcode,
op.OD_TYPE(op1),
op.OD_TYPE(op2),
op.OD_TYPE(result),
op1,
op2,
result);
efree(op1);
efree(op2);
efree(result);
}
#endif