fix macro expansion
[m6w6/ext-psi] / src / error.c
1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #else
29 # include "php_config.h"
30 #endif
31 #include "data.h"
32
33 /* zend_error_cb */
34 #include "Zend/zend.h"
35 /* is executing/compiling query API */
36 #include "Zend/zend_compile.h"
37 #include "Zend/zend_execute.h"
38
39 /* PG(), strlcpy, vslprintf */
40 #include "php.h"
41
42 void psi_error_wrapper(struct psi_data *context, struct psi_token *t, int type, const char *msg, ...)
43 {
44 va_list argv;
45 const char *fn = NULL;
46 unsigned ln = 0;
47
48 if (context) {
49 if (context->flags & PSI_SILENT) {
50 /* context->last_error may be an argument to print */
51 char error[sizeof(context->last_error)];
52
53 va_start(argv, msg);
54 vslprintf(error, sizeof(error), msg, argv);
55 va_end(argv);
56
57 memcpy(context->last_error, error,
58 sizeof(context->last_error));
59 return;
60 }
61 }
62
63 if (t) {
64 fn = t->file->val;
65 ln = t->line;
66 } else if (zend_is_executing()) {
67 fn = zend_get_executed_filename();
68 ln = zend_get_executed_lineno();
69 } else if (zend_is_compiling()) {
70 fn = zend_get_compiled_filename()->val;
71 ln = zend_get_compiled_lineno();
72 } else {
73 fn = "PSI module startup";
74 }
75
76 va_start(argv, msg);
77 psi_verror(type, fn, ln, msg, argv);
78 va_end(argv);
79
80 va_start(argv, msg);
81 PSI_DEBUG_LOCK(context,
82 PSI_DEBUG_PRINTV(context, msg, argv);
83 PSI_DEBUG_PRINT(context, "\n");
84 );
85 va_end(argv);
86
87 if (context) {
88 strlcpy(context->last_error, PG(last_error_message),
89 sizeof(context->last_error));
90 }
91 }
92
93 void psi_error(int type, const char *fn, unsigned ln, const char *msg, ...)
94 {
95 va_list argv;
96
97 va_start(argv, msg);
98 psi_verror(type, fn, ln, msg, argv);
99 va_end(argv);
100 }
101
102 void psi_verror(int type, const char *fn, unsigned ln, const char *msg, va_list argv)
103 {
104 zend_error_cb(type, fn, ln, msg, argv);
105 }