-
Notifications
You must be signed in to change notification settings - Fork 0
/
stackdriver_debugger.c
644 lines (556 loc) · 21.6 KB
/
stackdriver_debugger.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
/*
* Copyright 2017 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "php.h"
#include "main/SAPI.h"
#include "standard/php_string.h"
#include "standard/info.h"
#include "php_stackdriver_debugger.h"
#include "stackdriver_debugger.h"
#include "stackdriver_debugger_ast.h"
#include "stackdriver_debugger_logpoint.h"
#include "stackdriver_debugger_snapshot.h"
#include "zend_exceptions.h"
#include "stackdriver_debugger_time_functions.h"
#include "zend_alloc.h"
ZEND_DECLARE_MODULE_GLOBALS(stackdriver_debugger)
ZEND_BEGIN_ARG_INFO_EX(arginfo_stackdriver_debugger_snapshot, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, snapshotId, IS_STRING, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_stackdriver_debugger_add_snapshot, 0, 0, 2)
ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, line, IS_LONG, 0)
ZEND_ARG_ARRAY_INFO(0, options, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_stackdriver_debugger_logpoint, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, logpointId, IS_STRING, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_stackdriver_debugger_add_logpoint, 0, 0, 4)
ZEND_ARG_TYPE_INFO(0, filename, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, line, IS_LONG, 0)
ZEND_ARG_TYPE_INFO(0, logLevel, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, format, IS_STRING, 0)
ZEND_ARG_ARRAY_INFO(0, options, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_stackdriver_debugger_valid_statement, 0, 0, 1)
ZEND_ARG_TYPE_INFO(0, statement, IS_STRING, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_stackdriver_debugger_void, 0, 0, 0)
ZEND_END_ARG_INFO()
/* List of functions provided by this extension */
static zend_function_entry stackdriver_debugger_functions[] = {
PHP_FE(stackdriver_debugger_version, arginfo_stackdriver_debugger_void)
PHP_FE(stackdriver_debugger_snapshot, arginfo_stackdriver_debugger_snapshot)
PHP_FE(stackdriver_debugger_add_snapshot, arginfo_stackdriver_debugger_add_snapshot)
PHP_FE(stackdriver_debugger_list_snapshots, arginfo_stackdriver_debugger_void)
PHP_FE(stackdriver_debugger_logpoint, arginfo_stackdriver_debugger_logpoint)
PHP_FE(stackdriver_debugger_add_logpoint, arginfo_stackdriver_debugger_add_logpoint)
PHP_FE(stackdriver_debugger_list_logpoints, arginfo_stackdriver_debugger_void)
PHP_FE(stackdriver_debugger_valid_statement, arginfo_stackdriver_debugger_valid_statement)
PHP_FE_END
};
PHP_MINFO_FUNCTION(stackdriver_debugger)
{
php_info_print_table_start();
php_info_print_table_row(2, "Stackdriver Debugger support", "enabled");
php_info_print_table_row(2, "Stackdriver Debugger module version", PHP_STACKDRIVER_DEBUGGER_VERSION);
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* Registers the lifecycle hooks for this extension */
zend_module_entry stackdriver_debugger_module_entry = {
STANDARD_MODULE_HEADER,
PHP_STACKDRIVER_DEBUGGER_EXTNAME,
stackdriver_debugger_functions,
PHP_MINIT(stackdriver_debugger),
PHP_MSHUTDOWN(stackdriver_debugger),
PHP_RINIT(stackdriver_debugger),
PHP_RSHUTDOWN(stackdriver_debugger),
PHP_MINFO(stackdriver_debugger),
PHP_STACKDRIVER_DEBUGGER_VERSION,
STANDARD_MODULE_PROPERTIES
};
ZEND_GET_MODULE(stackdriver_debugger)
PHP_INI_MH(OnUpdate_stackdriver_debugger_max_time);
PHP_INI_MH(OnUpdate_stackdriver_debugger_max_time_percentage);
PHP_INI_MH(OnUpdate_stackdriver_debugger_max_memory);
/* Registers php.ini directives */
PHP_INI_BEGIN()
PHP_INI_ENTRY(PHP_STACKDRIVER_DEBUGGER_INI_ALLOWED_FUNCTIONS, NULL, PHP_INI_ALL, OnUpdate_stackdriver_debugger_allowed_functions)
PHP_INI_ENTRY(PHP_STACKDRIVER_DEBUGGER_INI_ALLOWED_METHODS, NULL, PHP_INI_ALL, OnUpdate_stackdriver_debugger_allowed_methods)
PHP_INI_ENTRY(PHP_STACKDRIVER_DEBUGGER_INI_MAX_TIME, "10", PHP_INI_ALL, NULL)
PHP_INI_ENTRY(PHP_STACKDRIVER_DEBUGGER_INI_MAX_TIME_PERCENTAGE, "1", PHP_INI_ALL, NULL)
PHP_INI_ENTRY(PHP_STACKDRIVER_DEBUGGER_INI_MAX_MEMORY, "10", PHP_INI_ALL, OnUpdate_stackdriver_debugger_max_memory)
PHP_INI_ENTRY(PHP_STACKDRIVER_DEBUGGER_INI_ALLOW_REGEX, "0", PHP_INI_ALL, OnUpdate_stackdriver_debugger_allow_regex)
PHP_INI_END()
/**
* Return the current version of the stackdriver_debugger extension
*
* @return string
*/
PHP_FUNCTION(stackdriver_debugger_version)
{
RETURN_STRING(PHP_STACKDRIVER_DEBUGGER_VERSION);
}
/* Constructor used for creating the stackdriver_debugger globals */
static void php_stackdriver_debugger_globals_ctor(void *pDest TSRMLS_DC)
{
zend_stackdriver_debugger_globals *stackdriver_debugger_global = (zend_stackdriver_debugger_globals *) pDest;
}
static double stackdriver_debugger_total_time_spent;
static int stackdriver_debugger_total_requests_handled;
/**
* Returns the max time that should be spent in the debugger in milliseconds.
*
* @return double
*/
static double stackdriver_debugger_max_time()
{
double percentage, max_time = INI_FLT(PHP_STACKDRIVER_DEBUGGER_INI_MAX_TIME) * 0.001;
if (stackdriver_debugger_total_requests_handled > 0) {
percentage = 0.01 * INI_FLT(PHP_STACKDRIVER_DEBUGGER_INI_MAX_TIME_PERCENTAGE) * stackdriver_debugger_total_time_spent / stackdriver_debugger_total_requests_handled;
if (percentage < max_time) {
return percentage;
}
}
return max_time;
}
/**
* Detects if opcache is available and enabled.
*
* @return zend_bool
*/
static zend_bool stackdriver_debugger_opcache_enabled()
{
zend_string *function_name = zend_string_init("opcache_invalidate", sizeof("opcache_invalidate") - 1, 0);
zend_function *invalidate = zend_hash_find_ptr(EG(function_table), function_name);
zend_bool enabled;
zend_string_release(function_name);
if (strcmp(sapi_module.name, "cli") == 0) {
enabled = INI_BOOL("opcache.enable_cli");
} else {
enabled = INI_BOOL("opcache.enable");
}
return invalidate != NULL && enabled;
}
/**
* Helper function to invoke `opcache_invalidate` from our C functions. Returns
* SUCCESS | FAILURE.
*/
static int stackdriver_debugger_opcache_invalidate(zend_string *filename)
{
zval params[2], function_name, ret;
if (STACKDRIVER_DEBUGGER_G(opcache_enabled)) {
ZVAL_STRING(&function_name, "opcache_invalidate");
ZVAL_STR(¶ms[0], filename);
ZVAL_BOOL(¶ms[1], 1);
return call_user_function(EG(function_table), NULL, &function_name, &ret, 2, params);
}
return FAILURE;
}
/**
* Return the collected list of debugger snapshots that have been collected for
* this request.
*
* @return array
*/
PHP_FUNCTION(stackdriver_debugger_list_snapshots)
{
array_init(return_value);
list_snapshots(return_value);
}
/**
* Return the collected list of logpoint messages that have been collected for
* this request.
*
* @return array
*/
PHP_FUNCTION(stackdriver_debugger_list_logpoints)
{
array_init(return_value);
list_logpoints(return_value);
}
/**
* Returns whether or not the provided PHP statement can be used for a
* breakpoint condition or as an evaluated expression.
*
* @param string $statement
* @return boolean
*/
PHP_FUNCTION(stackdriver_debugger_valid_statement)
{
zend_string *statement;
#ifndef FAST_ZPP
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S", &statement) == FAILURE) {
RETURN_FALSE;
}
#else
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STR(statement);
ZEND_PARSE_PARAMETERS_END();
#endif
if (valid_debugger_statement(statement) != SUCCESS) {
RETURN_FALSE;
}
RETURN_TRUE;
}
/**
* Executes the provided statement in the current execution context. Returns
* SUCCESS | FAILURE.
*/
static int test_conditional(zend_string *statement)
{
if (statement == NULL) {
return SUCCESS;
}
zval retval;
if (zend_eval_string(ZSTR_VAL(statement), &retval, "conditional") == SUCCESS) {
/*
* If there is an exception thrown in the conditional, we will ignore
* it. An exception is unexpected as we validate the type of AST
* commands allowed and "throw" is not allowed.
*
* Caveat: you may see warning messages for things like accessing
* properties on non-objects, but they should not show in the output
* unless the user's PHP configuration displays warning, info, notice,
* or debug messages. This scenario is unlikely for production
* environments.
*/
if (EG(exception) != NULL) {
zend_clear_exception();
return FAILURE;
}
/* internal function used to cast zval to boolean */
convert_to_boolean(&retval);
if (Z_TYPE(retval) == IS_TRUE) {
return SUCCESS;
} else {
return FAILURE;
}
}
php_error_docref(NULL, E_WARNING, "Failed to execute conditional.");
return FAILURE;
}
/**
* Capture the execution state for the provided snapshotId.
*
* @param string $snapshotId
* @return boolean
*/
PHP_FUNCTION(stackdriver_debugger_snapshot)
{
zend_string *snapshot_id = NULL;
stackdriver_debugger_snapshot_t *snapshot;
double start = 0;
size_t start_memory = 0, end_memory;
// if we've already spent more than the time allowed, skip further breakpoints
if (STACKDRIVER_DEBUGGER_G(time_spent) > stackdriver_debugger_max_time()) {
RETURN_FALSE;
}
// if we've already spent more than the time allowed, skip further breakpoints
if (STACKDRIVER_DEBUGGER_G(memory_used) > STACKDRIVER_DEBUGGER_G(max_memory)) {
RETURN_FALSE;
}
#ifndef FAST_ZPP
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S", &snapshot_id) == FAILURE) {
RETURN_FALSE;
}
#else
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STR(snapshot_id);
ZEND_PARSE_PARAMETERS_END();
#endif
start = stackdriver_debugger_now();
start_memory = zend_memory_usage(0);
snapshot = zend_hash_find_ptr(STACKDRIVER_DEBUGGER_G(snapshots_by_id), snapshot_id);
if (snapshot == NULL || snapshot->fulfilled || test_conditional(snapshot->condition) != SUCCESS) {
STACKDRIVER_DEBUGGER_G(time_spent) = STACKDRIVER_DEBUGGER_G(time_spent) + stackdriver_debugger_now() - start;
RETURN_FALSE;
}
evaluate_snapshot(execute_data, snapshot);
STACKDRIVER_DEBUGGER_G(time_spent) = STACKDRIVER_DEBUGGER_G(time_spent) + stackdriver_debugger_now() - start;
end_memory = zend_memory_usage(0);
if (end_memory > start_memory) {
STACKDRIVER_DEBUGGER_G(memory_used) = STACKDRIVER_DEBUGGER_G(memory_used) + end_memory - start_memory;
}
RETURN_TRUE;
}
/**
* Evaluate the logpoint for the provided logpointId.
*
* @param string $logpointId
* @return boolean
*/
PHP_FUNCTION(stackdriver_debugger_logpoint)
{
zend_string *logpoint_id = NULL;
stackdriver_debugger_logpoint_t *logpoint;
double start = 0;
size_t start_memory = 0, end_memory;
// if we've already spent more than the time allowed, skip further breakpoints
if (STACKDRIVER_DEBUGGER_G(time_spent) > stackdriver_debugger_max_time()) {
RETURN_FALSE;
}
// if we've already spent more than the time allowed, skip further breakpoints
if (STACKDRIVER_DEBUGGER_G(memory_used) > STACKDRIVER_DEBUGGER_G(max_memory)) {
RETURN_FALSE;
}
#ifndef FAST_ZPP
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S", &logpoint_id) == FAILURE) {
RETURN_FALSE;
}
#else
ZEND_PARSE_PARAMETERS_START(1, 1)
Z_PARAM_STR(logpoint_id);
ZEND_PARSE_PARAMETERS_END();
#endif
start = stackdriver_debugger_now();
start_memory = zend_memory_usage(0);
logpoint = zend_hash_find_ptr(STACKDRIVER_DEBUGGER_G(logpoints_by_id), logpoint_id);
if (logpoint == NULL || test_conditional(logpoint->condition) != SUCCESS) {
STACKDRIVER_DEBUGGER_G(time_spent) = STACKDRIVER_DEBUGGER_G(time_spent) + stackdriver_debugger_now() - start;
RETURN_FALSE;
}
evaluate_logpoint(execute_data, logpoint);
STACKDRIVER_DEBUGGER_G(time_spent) = STACKDRIVER_DEBUGGER_G(time_spent) + stackdriver_debugger_now() - start;
end_memory = zend_memory_usage(0);
if (end_memory > start_memory) {
STACKDRIVER_DEBUGGER_G(memory_used) = STACKDRIVER_DEBUGGER_G(memory_used) + end_memory - start_memory;
}
RETURN_TRUE;
}
/**
* Calculate the full filename given a relative path and the current file.
*
* Note: The return zend_string must be released by the caller.
*/
static zend_string *stackdriver_debugger_full_filename(zend_string *relative_or_full_path, const char *source_root, int source_root_length)
{
return strpprintf(source_root_length + 2 + ZSTR_LEN(relative_or_full_path), "%s%c%s", source_root, DEFAULT_SLASH, ZSTR_VAL(relative_or_full_path));
}
/**
* Register a snapshot for recording.
*
* @param string $filename
* @param int $line
* @param array $options [optional] {
* Configuration options.
*
* @type string $snapshotId Identifier for this snapshot. Defaults to a
* randomly generated value.
* @type string $condition If provided, this PHP statement will be
* executed at the snapshot point in that execution context. If the
* value is truthy, then the snapshot will be evaluated.
* @type array $expressions An array of additional statements to execute
* in the execution context that are captured along with the local
* variables in scope.
* @type string $sourceRoot Full path the the root directory of the
* application source code.
* @type callable $callback The callback to execute when the snapshot is
* hit.
* @type int $maxDepth The maximum number of stackframes whose variables
* are captured. If 0, then no limit. **Defaults to** 0.
* }
*/
PHP_FUNCTION(stackdriver_debugger_add_snapshot)
{
zend_string *filename, *full_filename, *snapshot_id = NULL, *condition = NULL, *source_root = NULL;
zend_long lineno;
HashTable *options = NULL, *expressions = NULL;
zval *zv = NULL, *callback = NULL;
zend_long max_stack_eval_depth = 0;
#ifndef FAST_ZPP
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Sl|h", &filename, &lineno, &options) == FAILURE) {
RETURN_FALSE;
}
#else
ZEND_PARSE_PARAMETERS_START(2, 3)
Z_PARAM_STR(filename);
Z_PARAM_LONG(lineno);
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_HT(options);
ZEND_PARSE_PARAMETERS_END();
#endif
if (options != NULL) {
if ((zv = zend_hash_str_find(options, "snapshotId", sizeof("snapshotId")-1)) != NULL && !Z_ISNULL_P(zv)) {
snapshot_id = Z_STR_P(zv);
}
if ((zv = zend_hash_str_find(options, "condition", sizeof("condition")-1)) != NULL && !Z_ISNULL_P(zv)) {
condition = Z_STR_P(zv);
}
if ((zv = zend_hash_str_find(options, "expressions", sizeof("expressions")-1)) != NULL && !Z_ISNULL_P(zv)) {
expressions = Z_ARRVAL_P(zv);
}
if ((zv = zend_hash_str_find(options, "sourceRoot", sizeof("sourceRoot")-1)) != NULL && !Z_ISNULL_P(zv)) {
source_root = Z_STR_P(zv);
}
if ((zv = zend_hash_str_find(options, "callback", sizeof("callback")-1)) != NULL && !Z_ISNULL_P(zv)) {
callback = zv;
}
if ((zv = zend_hash_str_find(options, "maxDepth", sizeof("maxDepth")-1)) != NULL && Z_TYPE_P(zv) == IS_LONG) {
max_stack_eval_depth = Z_LVAL_P(zv);
}
}
if (source_root == NULL) {
source_root = EX(prev_execute_data)->func->op_array.filename;
zend_string *current_file = zend_string_init(ZSTR_VAL(source_root), ZSTR_LEN(source_root), 0);
size_t dirlen = php_dirname(ZSTR_VAL(current_file), ZSTR_LEN(source_root));
full_filename = stackdriver_debugger_full_filename(filename, ZSTR_VAL(current_file), dirlen);
zend_string_release(current_file);
} else {
full_filename = stackdriver_debugger_full_filename(filename, ZSTR_VAL(source_root), ZSTR_LEN(source_root));
}
if (register_snapshot(snapshot_id, full_filename, lineno, condition, expressions, callback, max_stack_eval_depth) != SUCCESS) {
zend_string_release(full_filename);
RETURN_FALSE;
}
if (stackdriver_debugger_breakpoint_injected(full_filename, snapshot_id) != SUCCESS) {
stackdriver_debugger_opcache_invalidate(full_filename);
}
zend_string_release(full_filename);
RETURN_TRUE;
}
/**
* Register a logpoint for recording.
*
* @param string $filename
* @param int $line
* @param string $logLevel
* @param string $format
* @param array $options [optional] {
* Configuration options.
*
* @type string $snapshotId
* @type string $condition
* @type array $expressions
* @type string $sourceRoot
* }
*/
PHP_FUNCTION(stackdriver_debugger_add_logpoint)
{
zend_string *filename, *full_filename, *format = NULL, *snapshot_id = NULL, *condition = NULL, *source_root = NULL, *log_level = NULL;
zend_long lineno;
HashTable *options = NULL, *expressions = NULL;
zval *zv = NULL, *callback = NULL;
#ifndef FAST_ZPP
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "SlSS|h", &filename, &lineno, &log_level, &format, &options) == FAILURE) {
RETURN_FALSE;
}
#else
ZEND_PARSE_PARAMETERS_START(4, 5)
Z_PARAM_STR(filename);
Z_PARAM_LONG(lineno);
Z_PARAM_STR(log_level);
Z_PARAM_STR(format);
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_HT(options);
ZEND_PARSE_PARAMETERS_END();
#endif
if (options != NULL) {
if ((zv = zend_hash_str_find(options, "snapshotId", strlen("snapshotId"))) != NULL && !Z_ISNULL_P(zv)) {
snapshot_id = Z_STR_P(zv);
}
if ((zv = zend_hash_str_find(options, "condition", strlen("condition"))) != NULL && !Z_ISNULL_P(zv)) {
condition = Z_STR_P(zv);
}
if ((zv = zend_hash_str_find(options, "expressions", strlen("expressions"))) != NULL && !Z_ISNULL_P(zv)) {
expressions = Z_ARRVAL_P(zv);
}
if ((zv = zend_hash_str_find(options, "sourceRoot", strlen("sourceRoot"))) != NULL && !Z_ISNULL_P(zv)) {
source_root = Z_STR_P(zv);
}
if ((zv = zend_hash_str_find(options, "callback", strlen("callback"))) != NULL && !Z_ISNULL_P(zv)) {
callback = zv;
}
}
if (source_root == NULL) {
source_root = EX(prev_execute_data)->func->op_array.filename;
zend_string *current_file = zend_string_init(ZSTR_VAL(source_root), ZSTR_LEN(source_root), 0);
size_t dirlen = php_dirname(ZSTR_VAL(current_file), ZSTR_LEN(source_root));
full_filename = stackdriver_debugger_full_filename(filename, ZSTR_VAL(current_file), dirlen);
zend_string_release(current_file);
} else {
full_filename = stackdriver_debugger_full_filename(filename, ZSTR_VAL(source_root), ZSTR_LEN(source_root));
}
if (register_logpoint(snapshot_id, full_filename, lineno, log_level, condition, format, expressions, callback) != SUCCESS) {
zend_string_release(full_filename);
RETURN_FALSE;
}
if (stackdriver_debugger_breakpoint_injected(full_filename, snapshot_id) != SUCCESS) {
stackdriver_debugger_opcache_invalidate(full_filename);
}
zend_string_release(full_filename);
RETURN_TRUE;
}
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(stackdriver_debugger)
{
/* allocate global request variables */
#ifdef ZTS
ts_allocate_id(&stackdriver_debugger_globals_id, sizeof(zend_stackdriver_debugger_globals), php_stackdriver_debugger_globals_ctor, NULL);
#else
php_stackdriver_debugger_globals_ctor(&php_stackdriver_debugger_globals_ctor);
#endif
REGISTER_INI_ENTRIES();
stackdriver_debugger_ast_minit(INIT_FUNC_ARGS_PASSTHRU);
stackdriver_debugger_total_time_spent = 0.0;
stackdriver_debugger_total_requests_handled = 0;
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(stackdriver_debugger)
{
stackdriver_debugger_ast_mshutdown(SHUTDOWN_FUNC_ARGS_PASSTHRU);
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
/* }}} */
PHP_RINIT_FUNCTION(stackdriver_debugger)
{
STACKDRIVER_DEBUGGER_G(time_spent) = 0;
STACKDRIVER_DEBUGGER_G(request_start) = stackdriver_debugger_now();
STACKDRIVER_DEBUGGER_G(memory_used) = 0;
stackdriver_debugger_ast_rinit(TSRMLS_C);
stackdriver_debugger_snapshot_rinit(TSRMLS_C);
stackdriver_debugger_logpoint_rinit(TSRMLS_C);
STACKDRIVER_DEBUGGER_G(opcache_enabled) = stackdriver_debugger_opcache_enabled();
return SUCCESS;
}
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(stackdriver_debugger)
{
stackdriver_debugger_ast_rshutdown(TSRMLS_C);
stackdriver_debugger_snapshot_rshutdown(TSRMLS_C);
stackdriver_debugger_logpoint_rshutdown(TSRMLS_C);
stackdriver_debugger_total_time_spent += stackdriver_debugger_now() - STACKDRIVER_DEBUGGER_G(request_start) - STACKDRIVER_DEBUGGER_G(time_spent);
stackdriver_debugger_total_requests_handled++;
return SUCCESS;
}
/* }}} */
/**
* Callback for when the user changes the max memory php.ini setting.
*/
PHP_INI_MH(OnUpdate_stackdriver_debugger_max_memory)
{
if (new_value != NULL) {
/* 1MB = 1024 * 1024 bytes = 1048576 bytes */
STACKDRIVER_DEBUGGER_G(max_memory) = 1048576 * ZEND_STRTOL(ZSTR_VAL(new_value), NULL, 0);
}
return SUCCESS;
}