Annotation of embedaddon/php/Zend/zend_exceptions.c, revision 1.1.1.2
1.1 misho 1: /*
2: +----------------------------------------------------------------------+
3: | Zend Engine |
4: +----------------------------------------------------------------------+
5: | Copyright (c) 1998-2012 Zend Technologies Ltd. (http://www.zend.com) |
6: +----------------------------------------------------------------------+
7: | This source file is subject to version 2.00 of the Zend license, |
8: | that is bundled with this package in the file LICENSE, and is |
9: | available through the world-wide-web at the following url: |
10: | http://www.zend.com/license/2_00.txt. |
11: | If you did not receive a copy of the Zend license and are unable to |
12: | obtain it through the world-wide-web, please send a note to |
13: | license@zend.com so we can mail you a copy immediately. |
14: +----------------------------------------------------------------------+
15: | Authors: Andi Gutmans <andi@zend.com> |
16: | Marcus Boerger <helly@php.net> |
17: | Sterling Hughes <sterling@php.net> |
18: | Zeev Suraski <zeev@zend.com> |
19: +----------------------------------------------------------------------+
20: */
21:
1.1.1.2 ! misho 22: /* $Id$ */
1.1 misho 23:
24: #include "zend.h"
25: #include "zend_API.h"
26: #include "zend_builtin_functions.h"
27: #include "zend_interfaces.h"
28: #include "zend_exceptions.h"
29: #include "zend_vm.h"
1.1.1.2 ! misho 30: #include "zend_dtrace.h"
1.1 misho 31:
32: zend_class_entry *default_exception_ce;
33: zend_class_entry *error_exception_ce;
34: static zend_object_handlers default_exception_handlers;
35: ZEND_API void (*zend_throw_exception_hook)(zval *ex TSRMLS_DC);
36:
37: void zend_exception_set_previous(zval *exception, zval *add_previous TSRMLS_DC)
38: {
39: zval *previous;
40:
41: if (exception == add_previous || !add_previous || !exception) {
42: return;
43: }
44: if (Z_TYPE_P(add_previous) != IS_OBJECT && !instanceof_function(Z_OBJCE_P(add_previous), default_exception_ce TSRMLS_CC)) {
45: zend_error(E_ERROR, "Cannot set non exception as previous exception");
46: return;
47: }
48: while (exception && exception != add_previous && Z_OBJ_HANDLE_P(exception) != Z_OBJ_HANDLE_P(add_previous)) {
49: previous = zend_read_property(default_exception_ce, exception, "previous", sizeof("previous")-1, 1 TSRMLS_CC);
50: if (Z_TYPE_P(previous) == IS_NULL) {
51: zend_update_property(default_exception_ce, exception, "previous", sizeof("previous")-1, add_previous TSRMLS_CC);
52: Z_DELREF_P(add_previous);
53: return;
54: }
55: exception = previous;
56: }
57: }
58:
59: void zend_exception_save(TSRMLS_D) /* {{{ */
60: {
61: if (EG(prev_exception)) {
62: zend_exception_set_previous(EG(exception), EG(prev_exception) TSRMLS_CC);
63: }
64: if (EG(exception)) {
65: EG(prev_exception) = EG(exception);
66: }
67: EG(exception) = NULL;
68: }
69: /* }}} */
70:
71: void zend_exception_restore(TSRMLS_D) /* {{{ */
72: {
73: if (EG(prev_exception)) {
74: if (EG(exception)) {
75: zend_exception_set_previous(EG(exception), EG(prev_exception) TSRMLS_CC);
76: } else {
77: EG(exception) = EG(prev_exception);
78: }
79: EG(prev_exception) = NULL;
80: }
81: }
82: /* }}} */
83:
84: void zend_throw_exception_internal(zval *exception TSRMLS_DC) /* {{{ */
85: {
1.1.1.2 ! misho 86: #ifdef HAVE_DTRACE
! 87: if (DTRACE_EXCEPTION_THROWN_ENABLED()) {
! 88: char *classname;
! 89: int name_len;
! 90:
! 91: if (exception != NULL) {
! 92: zend_get_object_classname(exception, &classname, &name_len TSRMLS_CC);
! 93: DTRACE_EXCEPTION_THROWN(classname);
! 94: } else {
! 95: DTRACE_EXCEPTION_THROWN(NULL);
! 96: }
! 97: }
! 98: #endif /* HAVE_DTRACE */
! 99:
1.1 misho 100: if (exception != NULL) {
101: zval *previous = EG(exception);
102: zend_exception_set_previous(exception, EG(exception) TSRMLS_CC);
103: EG(exception) = exception;
104: if (previous) {
105: return;
106: }
107: }
108: if (!EG(current_execute_data)) {
109: if(EG(exception)) {
110: zend_exception_error(EG(exception), E_ERROR TSRMLS_CC);
111: }
112: zend_error(E_ERROR, "Exception thrown without a stack frame");
113: }
114:
115: if (zend_throw_exception_hook) {
116: zend_throw_exception_hook(exception TSRMLS_CC);
117: }
118:
119: if (EG(current_execute_data)->opline == NULL ||
120: (EG(current_execute_data)->opline+1)->opcode == ZEND_HANDLE_EXCEPTION) {
121: /* no need to rethrow the exception */
122: return;
123: }
124: EG(opline_before_exception) = EG(current_execute_data)->opline;
125: EG(current_execute_data)->opline = EG(exception_op);
126: }
127: /* }}} */
128:
129: ZEND_API void zend_clear_exception(TSRMLS_D) /* {{{ */
130: {
131: if (EG(prev_exception)) {
132: zval_ptr_dtor(&EG(prev_exception));
133: EG(prev_exception) = NULL;
134: }
135: if (!EG(exception)) {
136: return;
137: }
138: zval_ptr_dtor(&EG(exception));
139: EG(exception) = NULL;
140: EG(current_execute_data)->opline = EG(opline_before_exception);
141: #if ZEND_DEBUG
142: EG(opline_before_exception) = NULL;
143: #endif
144: }
145: /* }}} */
146:
147: static zend_object_value zend_default_exception_new_ex(zend_class_entry *class_type, int skip_top_traces TSRMLS_DC) /* {{{ */
148: {
1.1.1.2 ! misho 149: zval obj;
1.1 misho 150: zend_object *object;
151: zval *trace;
152:
153: Z_OBJVAL(obj) = zend_objects_new(&object, class_type TSRMLS_CC);
154: Z_OBJ_HT(obj) = &default_exception_handlers;
155:
1.1.1.2 ! misho 156: object_properties_init(object, class_type);
1.1 misho 157:
158: ALLOC_ZVAL(trace);
159: Z_UNSET_ISREF_P(trace);
160: Z_SET_REFCOUNT_P(trace, 0);
1.1.1.2 ! misho 161: zend_fetch_debug_backtrace(trace, skip_top_traces, 0, 0 TSRMLS_CC);
1.1 misho 162:
163: zend_update_property_string(default_exception_ce, &obj, "file", sizeof("file")-1, zend_get_executed_filename(TSRMLS_C) TSRMLS_CC);
164: zend_update_property_long(default_exception_ce, &obj, "line", sizeof("line")-1, zend_get_executed_lineno(TSRMLS_C) TSRMLS_CC);
165: zend_update_property(default_exception_ce, &obj, "trace", sizeof("trace")-1, trace TSRMLS_CC);
166:
167: return Z_OBJVAL(obj);
168: }
169: /* }}} */
170:
171: static zend_object_value zend_default_exception_new(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
172: {
173: return zend_default_exception_new_ex(class_type, 0 TSRMLS_CC);
174: }
175: /* }}} */
176:
177: static zend_object_value zend_error_exception_new(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
178: {
179: return zend_default_exception_new_ex(class_type, 2 TSRMLS_CC);
180: }
181: /* }}} */
182:
183: /* {{{ proto Exception Exception::__clone()
184: Clone the exception object */
185: ZEND_METHOD(exception, __clone)
186: {
187: /* Should never be executable */
188: zend_throw_exception(NULL, "Cannot clone object using __clone()", 0 TSRMLS_CC);
189: }
190: /* }}} */
191:
192: /* {{{ proto Exception::__construct(string message, int code [, Exception previous])
193: Exception constructor */
194: ZEND_METHOD(exception, __construct)
195: {
196: char *message = NULL;
197: long code = 0;
198: zval *object, *previous = NULL;
199: int argc = ZEND_NUM_ARGS(), message_len;
200:
201: if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC, "|slO!", &message, &message_len, &code, &previous, default_exception_ce) == FAILURE) {
202: zend_error(E_ERROR, "Wrong parameters for Exception([string $exception [, long $code [, Exception $previous = NULL]]])");
203: }
204:
205: object = getThis();
206:
207: if (message) {
1.1.1.2 ! misho 208: zend_update_property_stringl(default_exception_ce, object, "message", sizeof("message")-1, message, message_len TSRMLS_CC);
1.1 misho 209: }
210:
211: if (code) {
212: zend_update_property_long(default_exception_ce, object, "code", sizeof("code")-1, code TSRMLS_CC);
213: }
214:
215: if (previous) {
216: zend_update_property(default_exception_ce, object, "previous", sizeof("previous")-1, previous TSRMLS_CC);
217: }
218: }
219: /* }}} */
220:
221: /* {{{ proto ErrorException::__construct(string message, int code, int severity [, string filename [, int lineno [, Exception previous]]])
222: ErrorException constructor */
223: ZEND_METHOD(error_exception, __construct)
224: {
225: char *message = NULL, *filename = NULL;
226: long code = 0, severity = E_ERROR, lineno;
227: zval *object, *previous = NULL;
228: int argc = ZEND_NUM_ARGS(), message_len, filename_len;
229:
230: if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, argc TSRMLS_CC, "|sllslO!", &message, &message_len, &code, &severity, &filename, &filename_len, &lineno, &previous, default_exception_ce) == FAILURE) {
231: zend_error(E_ERROR, "Wrong parameters for ErrorException([string $exception [, long $code, [ long $severity, [ string $filename, [ long $lineno [, Exception $previous = NULL]]]]]])");
232: }
233:
234: object = getThis();
235:
236: if (message) {
237: zend_update_property_string(default_exception_ce, object, "message", sizeof("message")-1, message TSRMLS_CC);
238: }
239:
240: if (code) {
241: zend_update_property_long(default_exception_ce, object, "code", sizeof("code")-1, code TSRMLS_CC);
242: }
243:
244: if (previous) {
245: zend_update_property(default_exception_ce, object, "previous", sizeof("previous")-1, previous TSRMLS_CC);
246: }
247:
248: zend_update_property_long(default_exception_ce, object, "severity", sizeof("severity")-1, severity TSRMLS_CC);
249:
250: if (argc >= 4) {
251: zend_update_property_string(default_exception_ce, object, "file", sizeof("file")-1, filename TSRMLS_CC);
252: if (argc < 5) {
253: lineno = 0; /* invalidate lineno */
254: }
255: zend_update_property_long(default_exception_ce, object, "line", sizeof("line")-1, lineno TSRMLS_CC);
256: }
257: }
258: /* }}} */
259:
260: #define DEFAULT_0_PARAMS \
261: if (zend_parse_parameters_none() == FAILURE) { \
262: return; \
263: }
264:
265: static void _default_exception_get_entry(zval *object, char *name, int name_len, zval *return_value TSRMLS_DC) /* {{{ */
266: {
267: zval *value;
268:
269: value = zend_read_property(default_exception_ce, object, name, name_len, 0 TSRMLS_CC);
270:
271: *return_value = *value;
272: zval_copy_ctor(return_value);
273: INIT_PZVAL(return_value);
274: }
275: /* }}} */
276:
277: /* {{{ proto string Exception::getFile()
278: Get the file in which the exception occurred */
279: ZEND_METHOD(exception, getFile)
280: {
281: DEFAULT_0_PARAMS;
282:
283: _default_exception_get_entry(getThis(), "file", sizeof("file")-1, return_value TSRMLS_CC);
284: }
285: /* }}} */
286:
287: /* {{{ proto int Exception::getLine()
288: Get the line in which the exception occurred */
289: ZEND_METHOD(exception, getLine)
290: {
291: DEFAULT_0_PARAMS;
292:
293: _default_exception_get_entry(getThis(), "line", sizeof("line")-1, return_value TSRMLS_CC);
294: }
295: /* }}} */
296:
297: /* {{{ proto string Exception::getMessage()
298: Get the exception message */
299: ZEND_METHOD(exception, getMessage)
300: {
301: DEFAULT_0_PARAMS;
302:
303: _default_exception_get_entry(getThis(), "message", sizeof("message")-1, return_value TSRMLS_CC);
304: }
305: /* }}} */
306:
307: /* {{{ proto int Exception::getCode()
308: Get the exception code */
309: ZEND_METHOD(exception, getCode)
310: {
311: DEFAULT_0_PARAMS;
312:
313: _default_exception_get_entry(getThis(), "code", sizeof("code")-1, return_value TSRMLS_CC);
314: }
315: /* }}} */
316:
317: /* {{{ proto array Exception::getTrace()
318: Get the stack trace for the location in which the exception occurred */
319: ZEND_METHOD(exception, getTrace)
320: {
321: DEFAULT_0_PARAMS;
322:
323: _default_exception_get_entry(getThis(), "trace", sizeof("trace")-1, return_value TSRMLS_CC);
324: }
325: /* }}} */
326:
327: /* {{{ proto int ErrorException::getSeverity()
328: Get the exception severity */
329: ZEND_METHOD(error_exception, getSeverity)
330: {
331: DEFAULT_0_PARAMS;
332:
333: _default_exception_get_entry(getThis(), "severity", sizeof("severity")-1, return_value TSRMLS_CC);
334: }
335: /* }}} */
336:
337: /* {{{ gettraceasstring() macros */
338: #define TRACE_APPEND_CHR(chr) \
339: *str = (char*)erealloc(*str, *len + 1 + 1); \
340: (*str)[(*len)++] = chr
341:
342: #define TRACE_APPEND_STRL(val, vallen) \
343: { \
344: int l = vallen; \
345: *str = (char*)erealloc(*str, *len + l + 1); \
346: memcpy((*str) + *len, val, l); \
347: *len += l; \
348: }
349:
350: #define TRACE_APPEND_STR(val) \
351: TRACE_APPEND_STRL(val, sizeof(val)-1)
352:
353: #define TRACE_APPEND_KEY(key) \
354: if (zend_hash_find(ht, key, sizeof(key), (void**)&tmp) == SUCCESS) { \
355: TRACE_APPEND_STRL(Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp)); \
356: }
357:
358: /* }}} */
359:
360: static int _build_trace_args(zval **arg TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key) /* {{{ */
361: {
362: char **str;
363: int *len;
364:
365: str = va_arg(args, char**);
366: len = va_arg(args, int*);
367:
368: /* the trivial way would be to do:
369: * conver_to_string_ex(arg);
370: * append it and kill the now tmp arg.
371: * but that could cause some E_NOTICE and also damn long lines.
372: */
373:
374: switch (Z_TYPE_PP(arg)) {
375: case IS_NULL:
376: TRACE_APPEND_STR("NULL, ");
377: break;
378: case IS_STRING: {
379: int l_added;
380: TRACE_APPEND_CHR('\'');
381: if (Z_STRLEN_PP(arg) > 15) {
382: TRACE_APPEND_STRL(Z_STRVAL_PP(arg), 15);
383: TRACE_APPEND_STR("...', ");
384: l_added = 15 + 6 + 1; /* +1 because of while (--l_added) */
385: } else {
386: l_added = Z_STRLEN_PP(arg);
387: TRACE_APPEND_STRL(Z_STRVAL_PP(arg), l_added);
388: TRACE_APPEND_STR("', ");
389: l_added += 3 + 1;
390: }
391: while (--l_added) {
392: if ((*str)[*len - l_added] < 32) {
393: (*str)[*len - l_added] = '?';
394: }
395: }
396: break;
397: }
398: case IS_BOOL:
399: if (Z_LVAL_PP(arg)) {
400: TRACE_APPEND_STR("true, ");
401: } else {
402: TRACE_APPEND_STR("false, ");
403: }
404: break;
405: case IS_RESOURCE:
406: TRACE_APPEND_STR("Resource id #");
407: /* break; */
408: case IS_LONG: {
409: long lval = Z_LVAL_PP(arg);
410: char s_tmp[MAX_LENGTH_OF_LONG + 1];
411: int l_tmp = zend_sprintf(s_tmp, "%ld", lval); /* SAFE */
412: TRACE_APPEND_STRL(s_tmp, l_tmp);
413: TRACE_APPEND_STR(", ");
414: break;
415: }
416: case IS_DOUBLE: {
417: double dval = Z_DVAL_PP(arg);
418: char *s_tmp;
419: int l_tmp;
420:
421: s_tmp = emalloc(MAX_LENGTH_OF_DOUBLE + EG(precision) + 1);
422: l_tmp = zend_sprintf(s_tmp, "%.*G", (int) EG(precision), dval); /* SAFE */
423: TRACE_APPEND_STRL(s_tmp, l_tmp);
424: /* %G already handles removing trailing zeros from the fractional part, yay */
425: efree(s_tmp);
426: TRACE_APPEND_STR(", ");
427: break;
428: }
429: case IS_ARRAY:
430: TRACE_APPEND_STR("Array, ");
431: break;
432: case IS_OBJECT: {
1.1.1.2 ! misho 433: const char *class_name;
1.1 misho 434: zend_uint class_name_len;
435: int dup;
436:
437: TRACE_APPEND_STR("Object(");
438:
439: dup = zend_get_object_classname(*arg, &class_name, &class_name_len TSRMLS_CC);
440:
441: TRACE_APPEND_STRL(class_name, class_name_len);
442: if(!dup) {
1.1.1.2 ! misho 443: efree((char*)class_name);
1.1 misho 444: }
445:
446: TRACE_APPEND_STR("), ");
447: break;
448: }
449: default:
450: break;
451: }
452: return ZEND_HASH_APPLY_KEEP;
453: }
454: /* }}} */
455:
456: static int _build_trace_string(zval **frame TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key) /* {{{ */
457: {
458: char *s_tmp, **str;
459: int *len, *num;
460: long line;
461: HashTable *ht = Z_ARRVAL_PP(frame);
462: zval **file, **tmp;
463:
464: str = va_arg(args, char**);
465: len = va_arg(args, int*);
466: num = va_arg(args, int*);
467:
468: s_tmp = emalloc(1 + MAX_LENGTH_OF_LONG + 1 + 1);
469: sprintf(s_tmp, "#%d ", (*num)++);
470: TRACE_APPEND_STRL(s_tmp, strlen(s_tmp));
471: efree(s_tmp);
472: if (zend_hash_find(ht, "file", sizeof("file"), (void**)&file) == SUCCESS) {
473: if (zend_hash_find(ht, "line", sizeof("line"), (void**)&tmp) == SUCCESS) {
474: line = Z_LVAL_PP(tmp);
475: } else {
476: line = 0;
477: }
478: s_tmp = emalloc(Z_STRLEN_PP(file) + MAX_LENGTH_OF_LONG + 4 + 1);
479: sprintf(s_tmp, "%s(%ld): ", Z_STRVAL_PP(file), line);
480: TRACE_APPEND_STRL(s_tmp, strlen(s_tmp));
481: efree(s_tmp);
482: } else {
483: TRACE_APPEND_STR("[internal function]: ");
484: }
485: TRACE_APPEND_KEY("class");
486: TRACE_APPEND_KEY("type");
487: TRACE_APPEND_KEY("function");
488: TRACE_APPEND_CHR('(');
489: if (zend_hash_find(ht, "args", sizeof("args"), (void**)&tmp) == SUCCESS) {
490: int last_len = *len;
491: zend_hash_apply_with_arguments(Z_ARRVAL_PP(tmp) TSRMLS_CC, (apply_func_args_t)_build_trace_args, 2, str, len);
492: if (last_len != *len) {
493: *len -= 2; /* remove last ', ' */
494: }
495: }
496: TRACE_APPEND_STR(")\n");
497: return ZEND_HASH_APPLY_KEEP;
498: }
499: /* }}} */
500:
501: /* {{{ proto string Exception::getTraceAsString()
502: Obtain the backtrace for the exception as a string (instead of an array) */
503: ZEND_METHOD(exception, getTraceAsString)
504: {
505: zval *trace;
506: char *res, **str, *s_tmp;
507: int res_len = 0, *len = &res_len, num = 0;
508:
509: DEFAULT_0_PARAMS;
510:
511: res = estrdup("");
512: str = &res;
513:
514: trace = zend_read_property(default_exception_ce, getThis(), "trace", sizeof("trace")-1, 1 TSRMLS_CC);
515: zend_hash_apply_with_arguments(Z_ARRVAL_P(trace) TSRMLS_CC, (apply_func_args_t)_build_trace_string, 3, str, len, &num);
516:
517: s_tmp = emalloc(1 + MAX_LENGTH_OF_LONG + 7 + 1);
518: sprintf(s_tmp, "#%d {main}", num);
519: TRACE_APPEND_STRL(s_tmp, strlen(s_tmp));
520: efree(s_tmp);
521:
522: res[res_len] = '\0';
523: RETURN_STRINGL(res, res_len, 0);
524: }
525: /* }}} */
526:
527: /* {{{ proto string Exception::getPrevious()
528: Return previous Exception or NULL. */
529: ZEND_METHOD(exception, getPrevious)
530: {
531: zval *previous;
532:
533: DEFAULT_0_PARAMS;
534:
535: previous = zend_read_property(default_exception_ce, getThis(), "previous", sizeof("previous")-1, 1 TSRMLS_CC);
536: RETURN_ZVAL(previous, 1, 0);
537: }
538:
539: int zend_spprintf(char **message, int max_len, char *format, ...) /* {{{ */
540: {
541: va_list arg;
542: int len;
543:
544: va_start(arg, format);
545: len = zend_vspprintf(message, max_len, format, arg);
546: va_end(arg);
547: return len;
548: }
549: /* }}} */
550:
551: /* {{{ proto string Exception::__toString()
552: Obtain the string representation of the Exception object */
553: ZEND_METHOD(exception, __toString)
554: {
555: zval message, file, line, *trace, *exception;
556: char *str, *prev_str;
557: int len = 0;
558: zend_fcall_info fci;
559: zval fname;
560:
561: DEFAULT_0_PARAMS;
562:
563: str = estrndup("", 0);
564:
565: exception = getThis();
566: ZVAL_STRINGL(&fname, "gettraceasstring", sizeof("gettraceasstring")-1, 1);
567:
568: while (exception && Z_TYPE_P(exception) == IS_OBJECT) {
569: prev_str = str;
570: _default_exception_get_entry(exception, "message", sizeof("message")-1, &message TSRMLS_CC);
571: _default_exception_get_entry(exception, "file", sizeof("file")-1, &file TSRMLS_CC);
572: _default_exception_get_entry(exception, "line", sizeof("line")-1, &line TSRMLS_CC);
573:
574: convert_to_string(&message);
575: convert_to_string(&file);
576: convert_to_long(&line);
577:
578: fci.size = sizeof(fci);
579: fci.function_table = &Z_OBJCE_P(exception)->function_table;
580: fci.function_name = &fname;
581: fci.symbol_table = NULL;
582: fci.object_ptr = exception;
583: fci.retval_ptr_ptr = &trace;
584: fci.param_count = 0;
585: fci.params = NULL;
586: fci.no_separation = 1;
587:
588: zend_call_function(&fci, NULL TSRMLS_CC);
589:
590: if (Z_TYPE_P(trace) != IS_STRING) {
591: zval_ptr_dtor(&trace);
592: trace = NULL;
593: }
594:
595: if (Z_STRLEN(message) > 0) {
596: len = zend_spprintf(&str, 0, "exception '%s' with message '%s' in %s:%ld\nStack trace:\n%s%s%s",
597: Z_OBJCE_P(exception)->name, Z_STRVAL(message), Z_STRVAL(file), Z_LVAL(line),
598: (trace && Z_STRLEN_P(trace)) ? Z_STRVAL_P(trace) : "#0 {main}\n",
599: len ? "\n\nNext " : "", prev_str);
600: } else {
601: len = zend_spprintf(&str, 0, "exception '%s' in %s:%ld\nStack trace:\n%s%s%s",
602: Z_OBJCE_P(exception)->name, Z_STRVAL(file), Z_LVAL(line),
603: (trace && Z_STRLEN_P(trace)) ? Z_STRVAL_P(trace) : "#0 {main}\n",
604: len ? "\n\nNext " : "", prev_str);
605: }
606: efree(prev_str);
607: zval_dtor(&message);
608: zval_dtor(&file);
609: zval_dtor(&line);
610:
611: exception = zend_read_property(default_exception_ce, exception, "previous", sizeof("previous")-1, 0 TSRMLS_CC);
612:
613: if (trace) {
614: zval_ptr_dtor(&trace);
615: }
1.1.1.2 ! misho 616:
1.1 misho 617: }
618: zval_dtor(&fname);
619:
620: /* We store the result in the private property string so we can access
621: * the result in uncaught exception handlers without memleaks. */
622: zend_update_property_string(default_exception_ce, getThis(), "string", sizeof("string")-1, str TSRMLS_CC);
623:
624: RETURN_STRINGL(str, len, 0);
625: }
626: /* }}} */
627:
628: /* {{{ internal structs */
629: /* All functions that may be used in uncaught exception handlers must be final
630: * and must not throw exceptions. Otherwise we would need a facility to handle
631: * such exceptions in that handler.
632: * Also all getXY() methods are final because thy serve as read only access to
633: * their corresponding properties, no more, no less. If after all you need to
634: * override somthing then it is method __toString().
635: * And never try to change the state of exceptions and never implement anything
636: * that gives the user anything to accomplish this.
637: */
638: ZEND_BEGIN_ARG_INFO_EX(arginfo_exception___construct, 0, 0, 0)
639: ZEND_ARG_INFO(0, message)
640: ZEND_ARG_INFO(0, code)
641: ZEND_ARG_INFO(0, previous)
642: ZEND_END_ARG_INFO()
643:
644: const static zend_function_entry default_exception_functions[] = {
645: ZEND_ME(exception, __clone, NULL, ZEND_ACC_PRIVATE|ZEND_ACC_FINAL)
646: ZEND_ME(exception, __construct, arginfo_exception___construct, ZEND_ACC_PUBLIC)
647: ZEND_ME(exception, getMessage, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
648: ZEND_ME(exception, getCode, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
649: ZEND_ME(exception, getFile, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
650: ZEND_ME(exception, getLine, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
651: ZEND_ME(exception, getTrace, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
652: ZEND_ME(exception, getPrevious, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
653: ZEND_ME(exception, getTraceAsString, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
654: ZEND_ME(exception, __toString, NULL, 0)
655: {NULL, NULL, NULL}
656: };
657:
658: ZEND_BEGIN_ARG_INFO_EX(arginfo_error_exception___construct, 0, 0, 0)
659: ZEND_ARG_INFO(0, message)
660: ZEND_ARG_INFO(0, code)
661: ZEND_ARG_INFO(0, severity)
662: ZEND_ARG_INFO(0, filename)
663: ZEND_ARG_INFO(0, lineno)
664: ZEND_ARG_INFO(0, previous)
665: ZEND_END_ARG_INFO()
666:
667: static const zend_function_entry error_exception_functions[] = {
668: ZEND_ME(error_exception, __construct, arginfo_error_exception___construct, ZEND_ACC_PUBLIC)
669: ZEND_ME(error_exception, getSeverity, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
670: {NULL, NULL, NULL}
671: };
672: /* }}} */
673:
674: void zend_register_default_exception(TSRMLS_D) /* {{{ */
675: {
676: zend_class_entry ce;
677:
678: INIT_CLASS_ENTRY(ce, "Exception", default_exception_functions);
679: default_exception_ce = zend_register_internal_class(&ce TSRMLS_CC);
680: default_exception_ce->create_object = zend_default_exception_new;
681: memcpy(&default_exception_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
682: default_exception_handlers.clone_obj = NULL;
683:
684: zend_declare_property_string(default_exception_ce, "message", sizeof("message")-1, "", ZEND_ACC_PROTECTED TSRMLS_CC);
685: zend_declare_property_string(default_exception_ce, "string", sizeof("string")-1, "", ZEND_ACC_PRIVATE TSRMLS_CC);
686: zend_declare_property_long(default_exception_ce, "code", sizeof("code")-1, 0, ZEND_ACC_PROTECTED TSRMLS_CC);
687: zend_declare_property_null(default_exception_ce, "file", sizeof("file")-1, ZEND_ACC_PROTECTED TSRMLS_CC);
688: zend_declare_property_null(default_exception_ce, "line", sizeof("line")-1, ZEND_ACC_PROTECTED TSRMLS_CC);
689: zend_declare_property_null(default_exception_ce, "trace", sizeof("trace")-1, ZEND_ACC_PRIVATE TSRMLS_CC);
690: zend_declare_property_null(default_exception_ce, "previous", sizeof("previous")-1, ZEND_ACC_PRIVATE TSRMLS_CC);
691:
692: INIT_CLASS_ENTRY(ce, "ErrorException", error_exception_functions);
693: error_exception_ce = zend_register_internal_class_ex(&ce, default_exception_ce, NULL TSRMLS_CC);
694: error_exception_ce->create_object = zend_error_exception_new;
695: zend_declare_property_long(error_exception_ce, "severity", sizeof("severity")-1, E_ERROR, ZEND_ACC_PROTECTED TSRMLS_CC);
696: }
697: /* }}} */
698:
699: ZEND_API zend_class_entry *zend_exception_get_default(TSRMLS_D) /* {{{ */
700: {
701: return default_exception_ce;
702: }
703: /* }}} */
704:
705: ZEND_API zend_class_entry *zend_get_error_exception(TSRMLS_D) /* {{{ */
706: {
707: return error_exception_ce;
708: }
709: /* }}} */
710:
711: ZEND_API zval * zend_throw_exception(zend_class_entry *exception_ce, char *message, long code TSRMLS_DC) /* {{{ */
712: {
713: zval *ex;
714:
715: MAKE_STD_ZVAL(ex);
716: if (exception_ce) {
717: if (!instanceof_function(exception_ce, default_exception_ce TSRMLS_CC)) {
718: zend_error(E_NOTICE, "Exceptions must be derived from the Exception base class");
719: exception_ce = default_exception_ce;
720: }
721: } else {
722: exception_ce = default_exception_ce;
723: }
724: object_init_ex(ex, exception_ce);
725:
726:
727: if (message) {
728: zend_update_property_string(default_exception_ce, ex, "message", sizeof("message")-1, message TSRMLS_CC);
729: }
730: if (code) {
731: zend_update_property_long(default_exception_ce, ex, "code", sizeof("code")-1, code TSRMLS_CC);
732: }
733:
734: zend_throw_exception_internal(ex TSRMLS_CC);
735: return ex;
736: }
737: /* }}} */
738:
739: ZEND_API zval * zend_throw_exception_ex(zend_class_entry *exception_ce, long code TSRMLS_DC, char *format, ...) /* {{{ */
740: {
741: va_list arg;
742: char *message;
743: zval *zexception;
744:
745: va_start(arg, format);
746: zend_vspprintf(&message, 0, format, arg);
747: va_end(arg);
748: zexception = zend_throw_exception(exception_ce, message, code TSRMLS_CC);
749: efree(message);
750: return zexception;
751: }
752: /* }}} */
753:
754: ZEND_API zval * zend_throw_error_exception(zend_class_entry *exception_ce, char *message, long code, int severity TSRMLS_DC) /* {{{ */
755: {
756: zval *ex = zend_throw_exception(exception_ce, message, code TSRMLS_CC);
757: zend_update_property_long(default_exception_ce, ex, "severity", sizeof("severity")-1, severity TSRMLS_CC);
758: return ex;
759: }
760: /* }}} */
761:
762: static void zend_error_va(int type, const char *file, uint lineno, const char *format, ...) /* {{{ */
763: {
764: va_list args;
765:
766: va_start(args, format);
767: zend_error_cb(type, file, lineno, format, args);
768: va_end(args);
769: }
770: /* }}} */
771:
772: /* This function doesn't return if it uses E_ERROR */
773: ZEND_API void zend_exception_error(zval *exception, int severity TSRMLS_DC) /* {{{ */
774: {
775: zend_class_entry *ce_exception = Z_OBJCE_P(exception);
776: if (instanceof_function(ce_exception, default_exception_ce TSRMLS_CC)) {
777: zval *str, *file, *line;
778:
779: EG(exception) = NULL;
780:
781: zend_call_method_with_0_params(&exception, ce_exception, NULL, "__tostring", &str);
782: if (!EG(exception)) {
783: if (Z_TYPE_P(str) != IS_STRING) {
784: zend_error(E_WARNING, "%s::__toString() must return a string", ce_exception->name);
785: } else {
786: zend_update_property_string(default_exception_ce, exception, "string", sizeof("string")-1, EG(exception) ? ce_exception->name : Z_STRVAL_P(str) TSRMLS_CC);
787: }
788: }
789: zval_ptr_dtor(&str);
790:
791: if (EG(exception)) {
792: /* do the best we can to inform about the inner exception */
793: if (instanceof_function(ce_exception, default_exception_ce TSRMLS_CC)) {
794: file = zend_read_property(default_exception_ce, EG(exception), "file", sizeof("file")-1, 1 TSRMLS_CC);
795: line = zend_read_property(default_exception_ce, EG(exception), "line", sizeof("line")-1, 1 TSRMLS_CC);
796: } else {
797: file = NULL;
798: line = NULL;
799: }
800: zend_error_va(E_WARNING, file ? Z_STRVAL_P(file) : NULL, line ? Z_LVAL_P(line) : 0, "Uncaught %s in exception handling during call to %s::__tostring()", Z_OBJCE_P(EG(exception))->name, ce_exception->name);
801: }
802:
803: str = zend_read_property(default_exception_ce, exception, "string", sizeof("string")-1, 1 TSRMLS_CC);
804: file = zend_read_property(default_exception_ce, exception, "file", sizeof("file")-1, 1 TSRMLS_CC);
805: line = zend_read_property(default_exception_ce, exception, "line", sizeof("line")-1, 1 TSRMLS_CC);
806:
807: zend_error_va(severity, Z_STRVAL_P(file), Z_LVAL_P(line), "Uncaught %s\n thrown", Z_STRVAL_P(str));
808: } else {
809: zend_error(severity, "Uncaught exception '%s'", ce_exception->name);
810: }
811: }
812: /* }}} */
813:
814: ZEND_API void zend_throw_exception_object(zval *exception TSRMLS_DC) /* {{{ */
815: {
816: zend_class_entry *exception_ce;
817:
818: if (exception == NULL || Z_TYPE_P(exception) != IS_OBJECT) {
819: zend_error(E_ERROR, "Need to supply an object when throwing an exception");
820: }
821:
822: exception_ce = Z_OBJCE_P(exception);
823:
824: if (!exception_ce || !instanceof_function(exception_ce, default_exception_ce TSRMLS_CC)) {
825: zend_error(E_ERROR, "Exceptions must be valid objects derived from the Exception base class");
826: }
827: zend_throw_exception_internal(exception TSRMLS_CC);
828: }
829: /* }}} */
830:
831: /*
832: * Local variables:
833: * tab-width: 4
834: * c-basic-offset: 4
835: * indent-tabs-mode: t
836: * End:
837: */
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>