-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstackdriver_debugger_snapshot.c
547 lines (472 loc) · 17.5 KB
/
stackdriver_debugger_snapshot.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
/*
* 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 "php_stackdriver_debugger.h"
#include "stackdriver_debugger_ast.h"
#include "stackdriver_debugger_snapshot.h"
#include "zend_exceptions.h"
#include "stackdriver_debugger_random.h"
#include "spl/php_spl.h"
/* Initialize an empty, allocated variable */
static void init_variable(stackdriver_debugger_variable_t *variable)
{
variable->name = NULL;
ZVAL_NULL(&variable->value);
variable->indirect = 0;
}
/* Cleanup an allocated variable including freeing memory */
static void destroy_variable(stackdriver_debugger_variable_t *variable)
{
if (variable->name) {
zend_string_release(variable->name);
}
efree(variable);
}
static void stackframe_locals_dtor(zval *zv)
{
stackdriver_debugger_variable_t *variable = (stackdriver_debugger_variable_t *)Z_PTR_P(zv);
destroy_variable(variable);
ZVAL_PTR_DTOR(zv);
}
/* Initialize an empty, allocated stackframe */
static void init_stackframe(stackdriver_debugger_stackframe_t *stackframe)
{
stackframe->function = NULL;
stackframe->filename = NULL;
stackframe->lineno = -1;
ALLOC_HASHTABLE(stackframe->locals);
zend_hash_init(stackframe->locals, 16, NULL, stackframe_locals_dtor, 0);
}
/* Cleanup an allocated stackframe including freeing memory */
static void destroy_stackframe(stackdriver_debugger_stackframe_t *stackframe)
{
int i;
if (stackframe->function) {
zend_string_release(stackframe->function);
}
if (stackframe->filename) {
zend_string_release(stackframe->filename);
}
zend_hash_destroy(stackframe->locals);
FREE_HASHTABLE(stackframe->locals);
efree(stackframe);
}
static void stackframes_dtor(zval *zv)
{
stackdriver_debugger_stackframe_t *sf = (stackdriver_debugger_stackframe_t *)Z_PTR_P(zv);
destroy_stackframe(sf);
ZVAL_PTR_DTOR(zv);
}
/* Initialize an empty, allocated snapshot */
static void init_snapshot(stackdriver_debugger_snapshot_t *snapshot)
{
snapshot->id = NULL;
snapshot->filename = NULL;
snapshot->lineno = -1;
snapshot->condition = NULL;
snapshot->fulfilled = 0;
ALLOC_HASHTABLE(snapshot->expressions);
zend_hash_init(snapshot->expressions, 16, NULL, ZVAL_PTR_DTOR, 0);
ALLOC_HASHTABLE(snapshot->evaluated_expressions);
zend_hash_init(snapshot->evaluated_expressions, 16, NULL, ZVAL_PTR_DTOR, 0);
ALLOC_HASHTABLE(snapshot->stackframes);
zend_hash_init(snapshot->stackframes, 16, NULL, stackframes_dtor, 0);
ZVAL_NULL(&snapshot->callback);
}
/* Cleanup an allocated snapshot including freeing memory */
static void destroy_snapshot(stackdriver_debugger_snapshot_t *snapshot)
{
int i;
zend_string_release(snapshot->id);
zend_string_release(snapshot->filename);
if (snapshot->condition) {
zend_string_release(snapshot->condition);
}
zend_hash_destroy(snapshot->expressions);
FREE_HASHTABLE(snapshot->expressions);
zend_hash_destroy(snapshot->evaluated_expressions);
FREE_HASHTABLE(snapshot->evaluated_expressions);
zend_hash_destroy(snapshot->stackframes);
FREE_HASHTABLE(snapshot->stackframes);
if (Z_TYPE(snapshot->callback) != IS_NULL) {
ZVAL_PTR_DTOR(&(snapshot->callback));
}
efree(snapshot);
}
/**
* Convert a collected stackdriver_debugger_variable_t into an array zval with
* keys of name and value.
*/
static void variable_to_zval(zval *return_value, stackdriver_debugger_variable_t *variable)
{
zend_string *hash = NULL;
array_init(return_value);
add_assoc_str(return_value, "name", variable->name);
add_assoc_zval(return_value, "value", &variable->value);
switch (Z_TYPE(variable->value)) {
case IS_OBJECT:
/* Use the spl_object_hash value */
#if PHP_VERSION_ID < 80100
hash = php_spl_object_hash(&variable->value);
#else
zend_object *object = Z_OBJ_P(&variable->value);
hash = php_spl_object_hash(object);
#endif
break;
case IS_ARRAY:
/* Use the memory address of the zend_array */
hash = strpprintf(16, "%016zx", (size_t)Z_ARR(variable->value));
break;
case IS_STRING:
/* Use the internal HashTable value */
hash = strpprintf(32, "%016zx", Z_STRHASH(variable->value));
break;
}
if (hash != NULL) {
add_assoc_str(return_value, "id", hash);
}
}
/* Capture a variable with provided name and zval into a collected variable */
static stackdriver_debugger_variable_t *create_variable(zend_string *name, zval *zv)
{
stackdriver_debugger_variable_t *variable = (stackdriver_debugger_variable_t *)emalloc(sizeof(stackdriver_debugger_variable_t));
init_variable(variable);
variable->name = zend_string_dup(name, 0);
/* If the zval is an indirect, dereference it */
while (Z_TYPE_P(zv) == IS_INDIRECT) {
variable->indirect = 1;
zv = Z_INDIRECT_P(zv);
}
ZVAL_COPY(&variable->value, zv);
return variable;
}
/**
* Convert a collected stackdriver_debugger_stackframe_t into an array zval
* with function, filename, line, and locals.
*/
static void stackframe_to_zval(zval *return_value, stackdriver_debugger_stackframe_t *stackframe)
{
int i;
array_init(return_value);
if (stackframe->function) {
add_assoc_str(return_value, "function", zend_string_dup(stackframe->function, 0));
}
add_assoc_str(return_value, "filename", zend_string_copy(stackframe->filename));
add_assoc_long(return_value, "line", stackframe->lineno);
zval locals;
array_init(&locals);
stackdriver_debugger_variable_t *local_variable;
ZEND_HASH_FOREACH_PTR(stackframe->locals, local_variable) {
zval local;
variable_to_zval(&local, local_variable);
add_next_index_zval(&locals, &local);
} ZEND_HASH_FOREACH_END();
add_assoc_zval(return_value, "locals", &locals);
}
/**
* Convert a collected stackdriver_debugger_snapshot_t into an array zval
* of arrays converted from the contained stackframes
*/
static void stackframes_to_zval(zval *return_value, stackdriver_debugger_snapshot_t *snapshot)
{
int i;
array_init(return_value);
stackdriver_debugger_stackframe_t *stackframe;
ZEND_HASH_FOREACH_PTR(snapshot->stackframes, stackframe) {
zval zstackframe;
stackframe_to_zval(&zstackframe, stackframe);
add_next_index_zval(return_value, &zstackframe);
} ZEND_HASH_FOREACH_END();
}
/**
* Convert a collected evaluated_expressions into an array zval of values.
*/
static void expressions_to_zval(zval *return_value, stackdriver_debugger_snapshot_t *snapshot)
{
array_init(return_value);
zend_hash_copy(Z_ARR_P(return_value), snapshot->evaluated_expressions, NULL);
}
static void snapshot_to_zval(zval *return_value, stackdriver_debugger_snapshot_t *snapshot)
{
zval zstackframes, zexpressions;
array_init(return_value);
stackframes_to_zval(&zstackframes, snapshot);
expressions_to_zval(&zexpressions, snapshot);
add_assoc_str(return_value, "id", snapshot->id);
add_assoc_zval(return_value, "stackframes", &zstackframes);
add_assoc_zval(return_value, "evaluatedExpressions", &zexpressions);
}
/**
* Given a provided zend_execute_data, find or rebuild the HashTable of
* local variables at that moment.
*/
static int execute_data_to_symbol_table(zend_execute_data *execute_data, zend_array **symbol_table_p)
{
zend_op_array *op_array = &execute_data->func->op_array;
#if PHP_VERSION_ID < 70100
if (execute_data->symbol_table) {
#else
if (ZEND_CALL_INFO(execute_data) & ZEND_CALL_HAS_SYMBOL_TABLE) {
#endif
*symbol_table_p = execute_data->symbol_table;
return 0;
} else {
int i;
zend_array *symbol_table;
ALLOC_HASHTABLE(symbol_table);
zend_hash_init(symbol_table, op_array->last_var, NULL, ZVAL_PTR_DTOR, 0);
zend_string *name;
for (i = 0; i < op_array->last_var; i++) {
zval copy, *var;
name = op_array->vars[i];
var = ZEND_CALL_VAR_NUM(execute_data, i);
if (Z_TYPE_P(var) == IS_UNDEF) {
ZVAL_NULL(©);
} else {
ZVAL_COPY(©, ZEND_CALL_VAR_NUM(execute_data, i));
}
zend_hash_add(symbol_table, name, ©);
}
*symbol_table_p = symbol_table;
return 1;
}
}
/**
* Capture all local variables at the given execution scope from `execute_data`
* into the provided stackframe struct.
*/
static void capture_locals(zend_execute_data *execute_data, stackdriver_debugger_stackframe_t *stackframe)
{
zend_array *symbol_table;
zend_string *name;
zval *value;
int i = 0;
int allocated = execute_data_to_symbol_table(execute_data, &symbol_table);
ZEND_HASH_FOREACH_STR_KEY_VAL(symbol_table, name, value) {
stackdriver_debugger_variable_t *local = create_variable(name, value);
zend_hash_next_index_insert_ptr(stackframe->locals, local);
} ZEND_HASH_FOREACH_END();
/* Free symbol table if necessary (potential memory leak) */
if (allocated != 0) {
zend_hash_destroy(symbol_table);
FREE_HASHTABLE(symbol_table);
}
}
/**
* Capture the execution state from `execute_data`
*/
static stackdriver_debugger_stackframe_t *execute_data_to_stackframe(zend_execute_data *execute_data, int capture_variables)
{
stackdriver_debugger_stackframe_t *stackframe;
zend_op_array *op_array;
zend_string *funcname;
if (!execute_data->func || !ZEND_USER_CODE(execute_data->func->common.type)) {
return NULL;
}
stackframe = (stackdriver_debugger_stackframe_t *)emalloc(sizeof(stackdriver_debugger_stackframe_t));
init_stackframe(stackframe);
op_array = &execute_data->func->op_array;
funcname = op_array->function_name;
stackframe->function = NULL;
if (funcname != NULL) {
stackframe->function = zend_string_copy(funcname);
}
stackframe->filename = zend_string_copy(op_array->filename);
stackframe->lineno = execute_data->opline->lineno;
if (capture_variables == 1) {
capture_locals(execute_data, stackframe);
}
return stackframe;
}
/**
* Registers a snapshot for recording. We store the snapshot configuration in a
* request global HashTable by file which is consulted during file compilation.
*/
int register_snapshot(zend_string *snapshot_id, zend_string *filename,
zend_long lineno, zend_string *condition, HashTable *expressions,
zval *callback, zend_long max_stack_eval_depth)
{
HashTable *snapshots;
stackdriver_debugger_snapshot_t *snapshot;
snapshot = emalloc(sizeof(stackdriver_debugger_snapshot_t));
init_snapshot(snapshot);
if (snapshot_id == NULL) {
snapshot->id = generate_breakpoint_id();
} else {
snapshot->id = zend_string_copy(snapshot_id);
}
snapshot->filename = zend_string_copy(filename);
snapshot->lineno = lineno;
snapshot->max_stack_eval_depth = max_stack_eval_depth;
if (condition != NULL && ZSTR_LEN(condition) > 0) {
if (valid_debugger_statement(condition) != SUCCESS) {
destroy_snapshot(snapshot);
return FAILURE;
}
snapshot->condition = zend_string_copy(condition);
}
if (expressions != NULL) {
zval *expression;
ZEND_HASH_FOREACH_VAL(expressions, expression) {
if (valid_debugger_statement(Z_STR_P(expression)) != SUCCESS) {
destroy_snapshot(snapshot);
return FAILURE;
}
zend_hash_next_index_insert(snapshot->expressions, expression);
} ZEND_HASH_FOREACH_END();
}
if (callback != NULL) {
ZVAL_COPY(&snapshot->callback, callback);
}
snapshots = zend_hash_find_ptr(STACKDRIVER_DEBUGGER_G(snapshots_by_file), filename);
if (snapshots == NULL) {
ALLOC_HASHTABLE(snapshots);
zend_hash_init(snapshots, 4, NULL, ZVAL_PTR_DTOR, 0);
zend_hash_update_ptr(STACKDRIVER_DEBUGGER_G(snapshots_by_file), filename, snapshots);
}
zend_hash_next_index_insert_ptr(snapshots, snapshot);
zend_hash_update_ptr(STACKDRIVER_DEBUGGER_G(snapshots_by_id), snapshot->id, snapshot);
return SUCCESS;
}
/**
* Capture the full execution state into the provided snapshot
*/
static void capture_execution_state(zend_execute_data *execute_data, stackdriver_debugger_snapshot_t *snapshot)
{
zend_execute_data *ptr = execute_data;
stackdriver_debugger_stackframe_t *stackframe;
int i = 0;
while (ptr) {
if (snapshot->max_stack_eval_depth == 0 || i < snapshot->max_stack_eval_depth) {
stackframe = execute_data_to_stackframe(ptr, 1);
} else {
stackframe = execute_data_to_stackframe(ptr, 0);
}
if (stackframe != NULL) {
zend_hash_next_index_insert_ptr(snapshot->stackframes, stackframe);
i++;
}
ptr = ptr->prev_execute_data;
}
}
/**
* Evaluate each provided expression and capture the result into the
* provided snapshot data struct.
*/
static void capture_expressions(zend_execute_data *execute_data, stackdriver_debugger_snapshot_t *snapshot)
{
zval *expression;
ZEND_HASH_FOREACH_VAL(snapshot->expressions, expression) {
zval retval;
if (zend_eval_string(Z_STRVAL_P(expression), &retval, "expression evaluation") != SUCCESS ||
EG(exception) != NULL) {
zend_clear_exception();
ZVAL_STRING(&retval, "ERROR IN EXPRESSION");
}
zend_hash_add(snapshot->evaluated_expressions, Z_STR_P(expression), &retval);
} ZEND_HASH_FOREACH_END();
}
static int handle_snapshot_callback(zval *callback, stackdriver_debugger_snapshot_t *snapshot)
{
zval zsnapshot, callback_result;
snapshot_to_zval(&zsnapshot, snapshot);
int call_result = call_user_function(EG(function_table), NULL, callback, &callback_result, 1, &zsnapshot);
zval_ptr_dtor_nogc(&zsnapshot);
zval_ptr_dtor_nogc(&callback_result);
return call_result;
}
/**
* Evaluate the provided snapshot in the provided execution scope.
*/
void evaluate_snapshot(zend_execute_data *execute_data, stackdriver_debugger_snapshot_t *snapshot)
{
if (snapshot->fulfilled) {
return;
}
snapshot->fulfilled = 1;
/* collect locals at each level of the backtrace */
capture_execution_state(execute_data, snapshot);
/* evaluate and collect expressions */
capture_expressions(execute_data, snapshot);
/* record as collected */
if (Z_TYPE(snapshot->callback) != IS_NULL) {
if (handle_snapshot_callback(&snapshot->callback, snapshot) != SUCCESS) {
php_error_docref(NULL, E_WARNING, "Error running snapshot callback.");
}
if (EG(exception) != NULL) {
zend_clear_exception();
php_error_docref(NULL, E_WARNING, "Error running snapshot callback.");
}
} else {
zend_hash_update_ptr(STACKDRIVER_DEBUGGER_G(collected_snapshots_by_id), snapshot->id, snapshot);
}
}
/**
* Fetch the list of collected snapshots and return as an array of data
*/
void list_snapshots(zval *return_value)
{
stackdriver_debugger_snapshot_t *snapshot;
ZEND_HASH_FOREACH_PTR(STACKDRIVER_DEBUGGER_G(collected_snapshots_by_id), snapshot) {
zval zsnapshot;
snapshot_to_zval(&zsnapshot, snapshot);
add_next_index_zval(return_value, &zsnapshot);
} ZEND_HASH_FOREACH_END();
}
/**
* Destructor for cleaning up a zval pointer which contains a manually
* emalloc'ed snapshot pointer. This should efree all manually emalloc'ed data
* within the snapshot and also call the zval's normal destructor as well.
*/
static void snapshot_dtor(zval *zv)
{
stackdriver_debugger_snapshot_t *snapshot = (stackdriver_debugger_snapshot_t *)Z_PTR_P(zv);
destroy_snapshot(snapshot);
ZVAL_PTR_DTOR(zv);
}
static void snapshots_by_file_dtor(zval *zv)
{
HashTable *ht = (HashTable *)Z_PTR_P(zv);
zend_hash_destroy(ht);
FREE_HASHTABLE(ht);
ZVAL_PTR_DTOR(zv);
}
/**
* Request initialization lifecycle hook. Initializes request global variables.
*/
int stackdriver_debugger_snapshot_rinit(TSRMLS_D)
{
ALLOC_HASHTABLE(STACKDRIVER_DEBUGGER_G(snapshots_by_id));
zend_hash_init(STACKDRIVER_DEBUGGER_G(snapshots_by_id), 16, NULL, snapshot_dtor, 0);
ALLOC_HASHTABLE(STACKDRIVER_DEBUGGER_G(snapshots_by_file));
zend_hash_init(STACKDRIVER_DEBUGGER_G(snapshots_by_file), 16, NULL, snapshots_by_file_dtor, 0);
ALLOC_HASHTABLE(STACKDRIVER_DEBUGGER_G(collected_snapshots_by_id));
zend_hash_init(STACKDRIVER_DEBUGGER_G(collected_snapshots_by_id), 16, NULL, ZVAL_PTR_DTOR, 0);
return SUCCESS;
}
/**
* Request shutdown lifecycle hook. Destroys request global variables.
*/
int stackdriver_debugger_snapshot_rshutdown(TSRMLS_D)
{
zend_hash_destroy(STACKDRIVER_DEBUGGER_G(collected_snapshots_by_id));
FREE_HASHTABLE(STACKDRIVER_DEBUGGER_G(collected_snapshots_by_id));
zend_hash_destroy(STACKDRIVER_DEBUGGER_G(snapshots_by_file));
FREE_HASHTABLE(STACKDRIVER_DEBUGGER_G(snapshots_by_file));
zend_hash_destroy(STACKDRIVER_DEBUGGER_G(snapshots_by_id));
FREE_HASHTABLE(STACKDRIVER_DEBUGGER_G(snapshots_by_id));
return SUCCESS;
}