-
Notifications
You must be signed in to change notification settings - Fork 3
/
scout_internal_handlers.c
234 lines (211 loc) · 10.2 KB
/
scout_internal_handlers.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
/*
* Scout APM extension for PHP
*
* Copyright (C) 2021
* For license information, please see the LICENSE file.
*/
#include "zend_scoutapm.h"
#include "scout_extern.h"
#if HAVE_SCOUT_CURL
extern ZEND_NAMED_FUNCTION(scoutapm_curl_setopt_handler);
extern ZEND_NAMED_FUNCTION(scoutapm_curl_exec_handler);
#endif
extern ZEND_NAMED_FUNCTION(scoutapm_fopen_handler);
extern ZEND_NAMED_FUNCTION(scoutapm_fread_handler);
extern ZEND_NAMED_FUNCTION(scoutapm_fwrite_handler);
extern ZEND_NAMED_FUNCTION(scoutapm_pdo_prepare_handler);
extern ZEND_NAMED_FUNCTION(scoutapm_pdostatement_execute_handler);
/* This is simply a map of function names to an index in original_handlers */
indexed_handler_lookup handler_lookup[] = {
/* define each function we want to overload, which maps to an index in the `original_handlers` array */
{ 0, "file_get_contents"},
{ 1, "file_put_contents"},
{ 2, "curl_setopt"},
{ 3, "curl_exec"},
{ 4, "fopen"},
{ 5, "fread"},
{ 6, "fwrite"},
{ 7, "pdo->exec"},
{ 8, "pdo->query"},
{ 9, "pdo->prepare"},
{10, "pdostatement->execute"},
{11, "redis->append"},
{12, "redis->decr"},
{13, "redis->decrby"},
{14, "redis->get"},
{15, "redis->getbit"},
{16, "redis->getrange"},
{17, "redis->getset"},
{18, "redis->incr"},
{19, "redis->incrby"},
{20, "redis->mget"},
{21, "redis->mset"},
{22, "redis->msetnx"},
{23, "redis->set"},
{24, "redis->setbit"},
{25, "redis->setex"},
{26, "redis->psetex"},
{27, "redis->setnx"},
{28, "redis->setrange"},
{29, "redis->strlen"},
{30, "redis->del"},
{31, "memcached->add"},
{32, "memcached->addbykey"},
{33, "memcached->append"},
{34, "memcached->appendbykey"},
{35, "memcached->cas"},
{36, "memcached->casbykey"},
{37, "memcached->decrement"},
{38, "memcached->decrementbykey"},
{39, "memcached->delete"},
{40, "memcached->deletebykey"},
{41, "memcached->deletemulti"},
{42, "memcached->deletemultibykey"},
{43, "memcached->flush"},
{44, "memcached->get"},
{45, "memcached->getallkeys"},
{46, "memcached->getbykey"},
{47, "memcached->getmulti"},
{48, "memcached->getmultibykey"},
{49, "memcached->increment"},
{50, "memcached->incrementbykey"},
{51, "memcached->prepend"},
{52, "memcached->prependbykey"},
{53, "memcached->replace"},
{54, "memcached->replacebykey"},
{55, "memcached->set"},
{56, "memcached->setbykey"},
{57, "memcached->setmulti"},
{58, "memcached->setmultibykey"},
};
const int handler_lookup_size = sizeof(handler_lookup);
/* handlers count needs to be bigger than the number of handler_lookup entries */
#define ORIGINAL_HANDLERS_TO_ALLOCATE 60
zif_handler original_handlers[ORIGINAL_HANDLERS_TO_ALLOCATE] = {NULL};
int setup_recording_for_internal_handlers()
{
zend_function *original_function;
int handler_index;
zend_class_entry *ce;
SCOUT_OVERLOAD_FUNCTION("file_get_contents", scoutapm_default_handler)
SCOUT_OVERLOAD_FUNCTION("file_put_contents", scoutapm_default_handler)
#if HAVE_SCOUT_CURL
SCOUT_OVERLOAD_FUNCTION("curl_setopt", scoutapm_curl_setopt_handler)
SCOUT_OVERLOAD_FUNCTION("curl_exec", scoutapm_curl_exec_handler)
#endif
SCOUT_OVERLOAD_FUNCTION("fopen", scoutapm_fopen_handler)
SCOUT_OVERLOAD_FUNCTION("fwrite", scoutapm_fwrite_handler)
SCOUT_OVERLOAD_FUNCTION("fread", scoutapm_fread_handler)
SCOUT_OVERLOAD_METHOD("pdo", "exec", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("pdo", "query", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("pdo", "prepare", scoutapm_pdo_prepare_handler)
SCOUT_OVERLOAD_METHOD("pdostatement", "execute", scoutapm_pdostatement_execute_handler)
SCOUT_OVERLOAD_METHOD("redis", "append", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("redis", "decr", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("redis", "decrby", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("redis", "get", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("redis", "getbit", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("redis", "getrange", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("redis", "getset", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("redis", "incr", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("redis", "incrby", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("redis", "mget", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("redis", "mset", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("redis", "msetnx", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("redis", "set", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("redis", "setbit", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("redis", "setex", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("redis", "psetex", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("redis", "setnx", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("redis", "setrange", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("redis", "strlen", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("redis", "del", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("memcached", "add", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("memcached", "addbykey", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("memcached", "append", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("memcached", "appendbykey", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("memcached", "cas", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("memcached", "casbykey", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("memcached", "decrement", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("memcached", "decrementbykey", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("memcached", "delete", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("memcached", "deletebykey", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("memcached", "deletemulti", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("memcached", "deletemultibykey", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("memcached", "flush", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("memcached", "get", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("memcached", "getallkeys", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("memcached", "getbykey", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("memcached", "getmulti", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("memcached", "getmultibykey", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("memcached", "increment", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("memcached", "incrementbykey", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("memcached", "prepend", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("memcached", "prependbykey", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("memcached", "replace", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("memcached", "replacebykey", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("memcached", "set", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("memcached", "setbykey", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("memcached", "setmulti", scoutapm_default_handler)
SCOUT_OVERLOAD_METHOD("memcached", "setmultibykey", scoutapm_default_handler)
return SUCCESS;
}
/*
* This handles most recorded calls by grabbing all arguments (we treat it as a variadic), finding the "original" handler
* for the function and calling it. Once the function is called, record how long it took. Some "special" calls (e.g.
* curl_exec) have special handling because the arguments to curl_exec don't indicate the URL, for example.
*/
ZEND_NAMED_FUNCTION(scoutapm_default_handler)
{
int handler_index;
double entered = scoutapm_microtime();
int argc;
zval *argv = NULL;
const char *called_function;
SCOUT_PASSTHRU_IF_ALREADY_INSTRUMENTING(called_function)
/* note - no strdup needed as we copy it in record_observed_stack_frame */
called_function = determine_function_name(execute_data);
ZEND_PARSE_PARAMETERS_START(0, -1)
Z_PARAM_VARIADIC(' ', argv, argc)
ZEND_PARSE_PARAMETERS_END();
handler_index = handler_index_for_function(called_function);
original_handlers[handler_index](INTERNAL_FUNCTION_PARAM_PASSTHRU);
record_observed_stack_frame(called_function, entered, scoutapm_microtime(), argc, argv);
free((void*) called_function);
}
int unchecked_handler_index_for_function(const char *function_to_lookup)
{
int i = 0;
const char *current = handler_lookup[i].function_name;
while (current) {
if (strcasecmp(current, function_to_lookup) == 0) {
if (handler_lookup[i].index >= ORIGINAL_HANDLERS_TO_ALLOCATE) {
/*
* Note: as this is done in startup, and a critical failure, php_error_docref or zend_throw_exception_ex
* aren't suitable here, as they are "exceptions"; therefore the most informative thing we can do is
* write a message directly, and return -1, capture this in the PHP_RINIT_FUNCTION and return FAILURE.
* The -1 should be checked in SCOUT_OVERLOAD_CLASS_ENTRY_FUNCTION and SCOUT_OVERLOAD_FUNCTION
*/
php_printf("ScoutAPM overwrote a handler for '%s' but but we did not allocate enough original_handlers", function_to_lookup);
return -1;
}
return handler_lookup[i].index;
}
current = handler_lookup[++i].function_name;
}
/* Practically speaking, this shouldn't happen as long as we defined the handlers properly */
zend_throw_exception_ex(NULL, 0, "ScoutAPM overwrote a handler for '%s' but did not have a handler lookup for it", function_to_lookup);
return -1;
}
/*
* In our handler_lookup, find what the "index" in our overridden handlers is for a particular function name
*/
int handler_index_for_function(const char *function_to_lookup)
{
int handler_index = unchecked_handler_index_for_function(function_to_lookup);
if (original_handlers[handler_index] == NULL) {
zend_throw_exception_ex(NULL, 0, "ScoutAPM overwrote a handler for '%s' but the handler for index '%d' was not defined", function_to_lookup, handler_lookup[handler_index].index);
return -1;
}
return handler_index;
}