PageRenderTime 50ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/Zend/zend_exceptions.c

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