This repository has been archived by the owner on Nov 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.c
321 lines (284 loc) · 7.86 KB
/
debug.c
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
315
316
317
318
319
320
321
/*
+------------------------------------------------------------------------+
| Zephir Language |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2014 Zephir Team (http://www.zephir-lang.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Andres Gutierrez <[email protected]> |
| Eduar Carvajal <[email protected]> |
+------------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <php.h>
#include "php_ext.h"
#include "kernel/debug.h"
#include "kernel/main.h"
#include "kernel/string.h"
#ifndef ZEPHIR_RELEASE
FILE *zephir_log = NULL;
int zephir_debug_trace = 0;
static zephir_debug_entry *start = NULL;
static zephir_debug_entry *active = NULL;
/**
* Stars debug on file pipe
*/
int zephir_start_debug(){
if(!zephir_log){
/*//zephir_log = fopen("/home/gutierrezandresfelipe/phalcon-debug.a", "w");
zephir_log = fopen("/tmp/phalcon-debug.a", "w");
if(!zephir_log){
fprintf(stderr, "Can't open debug log\n");
}*/
zephir_log = stderr;
}
return SUCCESS;
}
/**
* Stops debug process
*/
int zephir_stop_debug(){
zephir_debug_entry *ptr = active;
zephir_debug_entry *this_entry = NULL;
while(ptr){
this_entry = ptr;
ptr = ptr->prev;
efree(this_entry);
}
//fclose(zephir_log);
zephir_log = NULL;
return SUCCESS;
}
/**
* Executes a print_r on an interal zval
*/
int zephir_print_r(zval *userval TSRMLS_DC){
zend_print_zval_r(userval, 0 TSRMLS_CC);
return SUCCESS;
}
/**
* Internal fast zval dump
*/
int zephir_vdump(zval *uservar TSRMLS_DC){
zephir_start_debug();
if(!uservar){
fprintf(zephir_log, "Null pointer\n");
return SUCCESS;
}
switch(Z_TYPE_P(uservar)){
case IS_NULL:
fprintf(zephir_log, "NULL \n");
break;
case IS_BOOL:
fprintf(zephir_log, "Boolean: %s\n", Z_LVAL_P(uservar) ? "TRUE" : "FALSE");
break;
case IS_LONG:
fprintf(zephir_log, "Long: %ld at %p, refcount=%d\n", Z_LVAL_P(uservar), uservar, Z_REFCOUNT_P(uservar));
break;
case IS_DOUBLE:
fprintf(zephir_log, "Double: %f\n", Z_DVAL_P(uservar));
break;
case IS_STRING:
fprintf(zephir_log, "String: %s(%d) at %p, refcount=%d\n", Z_STRVAL_P(uservar), Z_STRLEN_P(uservar), uservar, Z_REFCOUNT_P(uservar));
break;
case IS_RESOURCE:
fprintf(zephir_log, "Resource\n");
break;
case IS_ARRAY:
fprintf(zephir_log, "Array at %p, refcount=%d\n", uservar, Z_REFCOUNT_P(uservar));
break;
case IS_OBJECT:
fprintf(zephir_log, "Object <%s> at %p\n", Z_OBJCE_P(uservar)->name, uservar);
break;
default:
fprintf(zephir_log, "Unknown\n");
}
return SUCCESS;
}
int zephir_dump_ce(zend_class_entry *ce TSRMLS_DC){
char *message = emalloc(sizeof(char *)*120);
if(ce){
sprintf(message, "- ClassType => %d", ce->type);
zephir_step_over(message);
if(ce->name){
sprintf(message, "- ClassName => %s", ce->name);
zephir_step_over(message);
} else {
zephir_step_over("- ClassName => NULL");
}
} else {
zephir_step_over("- NULL class entry :(");
}
return SUCCESS;
}
int zephir_class_debug(zval *val TSRMLS_DC){
char *message = emalloc(sizeof(char *)*120);
zend_class_entry *ce;
if(val){
ce = Z_OBJCE_P(val);
if(ce){
sprintf(message, "- MemoryAddress => %p", val);
zephir_step_over(message);
zephir_dump_ce(ce TSRMLS_CC);
} else {
zephir_step_over("- No class entry :(");
}
} else {
zephir_step_over("- this_ptr is null :(");
}
return SUCCESS;
}
/**
* Append debug information to file
*/
int zephir_debug_str(char *what, char *message){
fprintf(zephir_log, "%s", what);
fprintf(zephir_log, "%s", message);
fprintf(zephir_log, "\n");
return SUCCESS;
}
int zephir_debug_long(char *what, uint vlong){
fprintf(zephir_log, "%s", what);
fprintf(zephir_log, "%u", vlong);
fprintf(zephir_log, "\n");
return SUCCESS;
}
int zephir_debug_screen(char *message){
zephir_debug_space();
fprintf(zephir_log, "%s\n", message);
return SUCCESS;
}
int zephir_debug_method_call(zval *obj, char *method_name TSRMLS_DC){
if(Z_TYPE_P(obj)==IS_OBJECT){
zephir_debug_space();
} else {
zephir_error_space();
}
if(Z_TYPE_P(obj)==IS_OBJECT){
fprintf(zephir_log, "Calling method %s::%s on Object at %p\n", Z_OBJCE_P(obj)->name, method_name, obj);
} else {
fprintf(zephir_log, "Calling method %s on non object :(\n", method_name);
}
return SUCCESS;
}
int zephir_error_space(){
int i;
fprintf(zephir_log, "[ERROR] ");
for(i=0;i<zephir_debug_trace;i++){
fprintf(zephir_log, " ");
}
return SUCCESS;
}
int zephir_debug_space(){
int i;
fprintf(zephir_log, "[DEBUG] ");
for(i=0;i<zephir_debug_trace;i++){
fprintf(zephir_log, " ");
}
return SUCCESS;
}
int zephir_debug_param(zval *param TSRMLS_DC){
zephir_debug_space();
fprintf(zephir_log, "Push method Param > ");
zephir_vdump(param TSRMLS_CC);
return SUCCESS;
}
int zephir_debug_vdump(char *preffix, zval *value TSRMLS_DC){
zephir_debug_space();
fprintf(zephir_log, "%s", preffix);
zephir_vdump(value TSRMLS_CC);
return SUCCESS;
}
int zephir_debug_assign(char *name, zval *value TSRMLS_DC){
zephir_debug_space();
fprintf(zephir_log, "Assign on %s with ", name);
zephir_vdump(value TSRMLS_CC);
return SUCCESS;
}
int zephir_step_over(char *message){
zephir_debug_screen(message);
return SUCCESS;
}
int zephir_step_into(char *message){
zephir_debug_trace++;
zephir_debug_screen(message);
return SUCCESS;
}
int zephir_step_out(char *message){
zephir_debug_screen(message);
zephir_debug_trace--;
return SUCCESS;
}
/**
* Prints internal debug backtrace
*/
int zephir_debug_backtrace_internal(){
int step = 0;
char *message;
zephir_debug_entry *ptr = active;
while(ptr){
zephir_spprintf(&message, 0, "#%d %s::%s", step, ptr->class_name, ptr->method_name);
zephir_debug_screen(message);
efree(message);
ptr = ptr->prev;
step++;
}
return SUCCESS;
}
/**
* Appends a debug entry to internal execution scope
*/
int zephir_step_into_entry(char *class_name, char *method_name, int lineno){
char *message;
zephir_debug_entry *entry;
if (!start) {
start = (zephir_debug_entry *) emalloc(sizeof(zephir_debug_entry));
start->class_name = "__main__";
start->method_name = "__init__";
start->lineno = 0;
start->prev = NULL;
start->next = NULL;
active = start;
}
zephir_spprintf(&message, 0, "Step Into %s::%s", class_name, method_name);
zephir_debug_screen(message);
efree(message);
entry = emalloc(sizeof(zephir_debug_entry));
entry->class_name = class_name;
entry->method_name = method_name;
entry->lineno = lineno;
entry->prev = active;
active->next = entry;
active = entry;
zephir_debug_trace++;
return SUCCESS;
}
/**
* Steps out current stack
*/
int zephir_step_out_entry(){
char *message;
zephir_debug_entry *prev;
if(active){
zephir_debug_trace--;
zephir_spprintf(&message, 0, "Step out %s::%s", active->class_name, active->method_name);
zephir_debug_screen(message);
efree(message);
prev = active->prev;
efree(active);
active = prev;
} else {
fprintf(zephir_log, "Problem, stack?");
return FAILURE;
}
return SUCCESS;
}
#endif