-
Notifications
You must be signed in to change notification settings - Fork 78
/
pinpoint_php.cpp
1283 lines (1136 loc) · 38.3 KB
/
pinpoint_php.cpp
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
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
////////////////////////////////////////////////////////////////////////////////
// Copyright 2019 NAVER Corp
//
// 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.
////////////////////////////////////////////////////////////////////////////////
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2017 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: |
+----------------------------------------------------------------------+
*/
/* $Id$ */
// clang-format off
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <iostream>
#include <string>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <stdio.h>
#include <cinttypes>
#include "zend_API.h"
#include "zend_string.h"
#include "zend_types.h"
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "common.h"
#include "php_pinpoint_php.h"
// clang-format on
#ifdef COMPILE_DL_PINPOINT_PHP
#ifdef ZTS
#if PHP_VERSION_ID >= 70000 && PHP_VERSION_ID < 80000
ZEND_TSRMLS_CACHE_DEFINE()
#else
#include "TSRM.h"
#endif
#endif
ZEND_GET_MODULE(pinpoint_php)
#endif
ZEND_DECLARE_MODULE_GLOBALS(pinpoint_php);
//static void pinpoint_log(char *msg);
// clang-format off
/* {{{ PHP_INI
*/
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("pinpoint_php.SendSpanTimeOutMs", "0", PHP_INI_ALL,
OnUpdateLong, w_timeout_ms, zend_pinpoint_php_globals,
pinpoint_php_globals)
STD_PHP_INI_ENTRY("pinpoint_php.CollectorHost", "unix:/tmp/collector.sock",
PHP_INI_ALL, OnUpdateString, co_host,
zend_pinpoint_php_globals, pinpoint_php_globals)
STD_PHP_INI_ENTRY("pinpoint_php.UnitTest", "no", PHP_INI_ALL, OnUpdateBool,
utest_flag, zend_pinpoint_php_globals, pinpoint_php_globals)
STD_PHP_INI_ENTRY("pinpoint_php.TraceLimit", "-1", PHP_INI_ALL, OnUpdateLong,
tracelimit, zend_pinpoint_php_globals, pinpoint_php_globals)
STD_PHP_INI_ENTRY("pinpoint_php.DebugReport", "no", PHP_INI_ALL, OnUpdateBool,
debug_report, zend_pinpoint_php_globals, pinpoint_php_globals)
PHP_INI_END()
/* }}} */
ZEND_BEGIN_ARG_INFO_EX(arginfo_add_id_key_value, 0, 0, 2)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, value)
ZEND_ARG_INFO(0, nodeid)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_add_id_key_value_flag, 0, 0, 2)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, value)
ZEND_ARG_INFO(0, nodeid)
ZEND_ARG_INFO(0, flag)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_add_join_cb_cb_cb, 0, 0, 4)
ZEND_ARG_INFO(0, joinable)
ZEND_ARG_INFO(0, onBefore)
ZEND_ARG_INFO(0, onEnd)
ZEND_ARG_INFO(0, onException)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_add_msg_filename_lineno_id, 0, 0, 2)
ZEND_ARG_INFO(0, msg)
ZEND_ARG_INFO(0, filename)
ZEND_ARG_INFO(0, lineno)
ZEND_ARG_INFO(0, nodeid)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_add_timestamp, 0, 0, 0)
ZEND_ARG_INFO(0, timestamp)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_add_arg_index, 0, 0, 0)
ZEND_ARG_INFO(0, index)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_add_id, 0, 0, 0)
ZEND_ARG_INFO(0, nodeid)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO_EX(arginfo_add_id_value, 0, 0, 1)
ZEND_ARG_INFO(0, key)
ZEND_ARG_INFO(0, nodeid)
ZEND_END_ARG_INFO()
// php5 needs (0,0,0)
ZEND_BEGIN_ARG_INFO_EX(arginfo_none, 0, 0, 0)
ZEND_END_ARG_INFO()
/* {{{ pinpioint_php_functions[]
*
* Every user visible function must have an entry in pinpioint_php_functions[].
*/
const zend_function_entry pinpoint_php_functions[] = {
PHP_FE(_pinpoint_start_trace, arginfo_add_id)
PHP_FE(_pinpoint_end_trace, arginfo_add_id)
PHP_FE(_pinpoint_unique_id, arginfo_none)
PHP_FE(pinpoint_get_this, arginfo_none)
PHP_FE(pinpoint_get_caller_arg,arginfo_add_arg_index)
PHP_FE(pinpoint_status, arginfo_none)
// PHP__FE(pinpoint_get_func_ref_args, arginfo_none)
PHP_FE(_pinpoint_drop_trace, arginfo_add_id)
PHP_FE(_pinpoint_start_time, arginfo_none)
PHP_FE(_pinpoint_set_context, arginfo_add_id_key_value)
PHP_FE(_pinpoint_get_context, arginfo_add_id_value)
PHP_FE(_pinpoint_trace_limit, arginfo_add_timestamp)
PHP_FE(_pinpoint_mark_as_error, arginfo_add_msg_filename_lineno_id)
PHP_FE(_pinpoint_add_clue, arginfo_add_id_key_value_flag)
PHP_FE(_pinpoint_add_clues, arginfo_add_id_key_value_flag)
PHP_FE(_pinpoint_join_cut,arginfo_add_join_cb_cb_cb)
PHP_FE_END /* Must be the last line in pinpioint_php_functions[] */
};
/* }}} */
// clang-format on
/* {{{ pinpioint_php_module_entry
*/
zend_module_entry pinpoint_php_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"pinpoint_php",
pinpoint_php_functions,
PHP_MINIT(pinpoint_php),
PHP_MSHUTDOWN(pinpoint_php),
PHP_RINIT(pinpoint_php), /* Replace with NULL if there's nothing to do at
request start */
PHP_RSHUTDOWN(pinpoint_php), /* Replace with NULL if there's nothing to do
at request end */
PHP_MINFO(pinpoint_php),
#if ZEND_MODULE_API_NO >= 20010901
PHP_PINPOINT_PHP_VERSION,
#endif
STANDARD_MODULE_PROPERTIES};
/* }}} */
#if PHP_VERSION_ID >= 80100
void (*old_error_cb)(int type, zend_string *error_filename,
const uint32_t error_lineno, zend_string *message);
#elif PHP_VERSION_ID >= 80000
void (*old_error_cb)(int type, const char *error_filename,
const uint32_t error_lineno, zend_string *message);
#else
void (*old_error_cb)(int type, const char *error_filename,
const uint32_t error_lineno, const char *format,
va_list args);
#endif
#define safe_free(x) \
if ((x)) { \
free((x)); \
(x) = NULL; \
}
PHP_FUNCTION(_pinpoint_drop_trace) {
NodeID id = E_ROOT_NODE;
#if PHP_VERSION_ID < 70000
size_t _id = -1;
zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &_id);
#else
zend_long _id = -1;
zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &_id);
#endif
if (_id == -1) {
id = pinpoint_get_per_thread_id();
} else {
id = (NodeID)_id;
}
change_trace_status(id, E_TRACE_BLOCK);
RETURN_TRUE;
}
PHP_FUNCTION(pinpoint_get_this) {
if (EX(prev_execute_data) && EX(prev_execute_data)->prev_execute_data) {
zval *self = &EX(prev_execute_data)->prev_execute_data->This;
if (zval_get_type(self) != IS_UNDEF) {
RETURN_ZVAL(self, 1, 0);
}
}
pp_trace("pinpoint_get_this: not support, no this");
RETURN_FALSE;
}
PHP_FUNCTION(_pinpoint_set_context) {
std::string key;
zval *zvalue;
#if PHP_VERSION_ID < 70000
char *zkey = NULL;
int zkey_len;
size_t _id = -1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz|l", &zkey, &zkey_len,
&zvalue, &_id) == FAILURE) {
zend_error(E_ERROR, "key/value required");
return;
}
key = std::string(zkey, zkey_len);
#else
zend_long _id = -1;
zend_string *zkey;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sz|l", &zkey, &zvalue, &_id) ==
FAILURE) {
zend_error(E_ERROR, "key/value required");
return;
}
key = std::string(zkey->val, zkey->len);
#endif
if (_id == -1) {
_id = pinpoint_get_per_thread_id();
}
switch (Z_TYPE_P(zvalue)) {
// case IS_LONG:
// pinpoint_set_context_long(_id,key.c_str(),Z_LVAL_P(zvalue));
// break;
case IS_STRING: {
std::string value(Z_STRVAL_P(zvalue), Z_STRLEN_P(zvalue));
pinpoint_set_context_key((NodeID)_id, key.c_str(), value.c_str());
} break;
default:
zend_error(E_WARNING, "value only support string");
return;
}
RETURN_TRUE;
}
// ref from ZEND_FUNCTION(func_get_arg)
PHP_FUNCTION(pinpoint_get_caller_arg) {
uint32_t arg_count, first_extra_arg;
zval *arg;
zend_long requested_offset;
zend_execute_data *ex;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &requested_offset) ==
FAILURE) {
return;
}
if (requested_offset < 0) {
zend_error(
E_WARNING,
"pinpoint_get_caller_arg(): The argument number should be >= 0");
RETURN_FALSE;
}
// changes
// ex = EX(prev_execute_data)
ex = EX(prev_execute_data)->prev_execute_data;
if (ZEND_CALL_INFO(ex) & ZEND_CALL_CODE) {
zend_error(E_WARNING, "pinpoint_get_caller_arg(): Called from the global "
"scope - no function context");
RETURN_FALSE;
}
#if PHP_MAJOR_VERSION == 8 && PHP_MINOR_VERSION >= 2
if (zend_forbid_dynamic_call() == FAILURE) {
RETURN_THROWS();
}
#else
if (zend_forbid_dynamic_call("pinpoint_get_caller_arg()") == FAILURE) {
RETURN_FALSE;
}
#endif
arg_count = ZEND_CALL_NUM_ARGS(ex);
if ((zend_ulong)requested_offset >= arg_count) {
zend_error(E_WARNING,
"pinpoint_get_caller_arg(): Argument "
"%" PRId64 " not passed to function",
requested_offset);
RETURN_FALSE;
}
// ZEND_LONG_FMT
first_extra_arg = ex->func->op_array.num_args;
if ((zend_ulong)requested_offset >= first_extra_arg &&
(ZEND_CALL_NUM_ARGS(ex) > first_extra_arg)) {
arg = ZEND_CALL_VAR_NUM(ex, ex->func->op_array.last_var +
ex->func->op_array.T) +
(requested_offset - first_extra_arg);
} else {
arg = ZEND_CALL_ARG(ex, requested_offset + 1);
}
if (EXPECTED(!Z_ISUNDEF_P(arg))) {
#if PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION <= 2
ZVAL_DEREF(arg);
ZVAL_COPY(return_value, arg);
#else
ZVAL_COPY_DEREF(return_value, arg);
#endif
}
}
PHP_FUNCTION(_pinpoint_get_context) {
std::string key;
#if PHP_VERSION_ID < 70000
long _id = -1;
char *zkey = NULL;
size_t zkey_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &zkey, &zkey_len,
&_id) == FAILURE) {
zend_error(E_ERROR, "key/value required");
return;
}
key = std::string(zkey, zkey_len);
#else
zend_string *zkey;
zend_long _id = -1;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|l", &zkey, &_id) == FAILURE) {
zend_error(E_ERROR, "key/value required");
return;
}
key = std::string(zkey->val, zkey->len);
#endif
if (_id == -1) {
_id = pinpoint_get_per_thread_id();
}
char value[1024] = {0};
int len = pinpoint_get_context_key((NodeID)_id, key.c_str(), value, 1024);
if (len > 0) {
#if PHP_VERSION_ID < 70000
RETURN_STRINGL(value, len, 1);
#else
RETURN_STRINGL(value, len);
#endif
} else {
RETURN_FALSE;
}
}
PHP_FUNCTION(_pinpoint_start_time) {
RETURN_DOUBLE(static_cast<double>(pinpoint_start_time()));
}
PHP_FUNCTION(_pinpoint_start_trace) {
NodeID id = E_ROOT_NODE, cur_id = E_ROOT_NODE;
#if PHP_VERSION_ID < 70000
long _id = -1;
zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &_id);
#else
zend_long _id = -1;
zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &_id);
#endif
if (_id == -1) {
id = pinpoint_get_per_thread_id();
cur_id = pinpoint_start_trace(id);
pinpoint_update_per_thread_id(cur_id);
RETURN_LONG((long)cur_id);
} else {
id = (NodeID)_id;
cur_id = pinpoint_start_trace(id);
RETURN_LONG((long)cur_id);
}
}
#if PHP_VERSION_ID >= 80100
void apm_error_cb(int type, zend_string *_error_filename,
const uint32_t error_lineno, zend_string *_message) {
char *error_filename = _error_filename->val;
char *msg = _message->val;
#elif PHP_VERSION_ID >= 80000
void apm_error_cb(int type, const char *_error_filename,
const uint32_t error_lineno, zend_string *_message) {
char *msg = _message->val;
const char *error_filename = _error_filename;
#else
void apm_error_cb(int type, const char *error_filename,
const uint32_t error_lineno, const char *format,
va_list args) {
char *msg;
va_list args_copy;
#if PHP_VERSION_ID < 80000
TSRMLS_FETCH();
#endif
va_copy(args_copy, args);
vspprintf(&msg, 0, format, args_copy);
va_end(args_copy);
#endif
if (!(EG(error_reporting) & type)) {
#if PHP_VERSION_ID < 80000
efree(msg);
#endif
return;
}
catch_error(pinpoint_get_per_thread_id(), msg, error_filename, error_lineno);
pp_trace("apm_error_cb called");
#if PHP_VERSION_ID < 80000
efree(msg);
/// call origin cb
old_error_cb(type, error_filename, error_lineno, format, args);
#else
old_error_cb(type, _error_filename, error_lineno, _message);
#endif
}
PHP_FUNCTION(_pinpoint_end_trace) {
NodeID id = E_ROOT_NODE, cur_id = E_ROOT_NODE;
#if PHP_VERSION_ID < 70000
long _id = -1;
zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &_id);
#else
zend_long _id = -1;
zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &_id);
#endif
if (_id == -1) {
id = pinpoint_get_per_thread_id();
cur_id = pinpoint_end_trace(id);
pinpoint_update_per_thread_id(cur_id);
RETURN_LONG((long)cur_id);
} else {
id = (NodeID)_id;
cur_id = pinpoint_end_trace(id);
RETURN_LONG((long)cur_id);
}
}
PHP_FUNCTION(pinpoint_status) { show_status(); }
PHP_FUNCTION(_pinpoint_add_clue) {
std::string key;
std::string value;
#if PHP_VERSION_ID < 70000
char *zkey = NULL, *zvalue = NULL;
int zkey_len, value_len;
long _id = -1;
long _flag = E_LOC_CURRENT;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|ll", &zkey,
&zkey_len, &zvalue, &value_len, &_id,
&_flag) == FAILURE) {
zend_error(E_ERROR, "pinpoint_add_clue() expects (int, string).");
return;
}
key = std::string(zkey, zkey_len);
value = std::string(zvalue, value_len);
#else
zend_string *zkey = nullptr;
zend_string *zvalue = nullptr;
zend_long _id = -1;
zend_long _flag = E_LOC_CURRENT;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS|ll", &zkey, &zvalue, &_id,
&_flag) == FAILURE) {
zend_error(E_ERROR, "pinpoint_add_clue() expects (int, string).");
return;
}
key = std::string(zkey->val, zkey->len);
value = std::string(zvalue->val, zvalue->len);
#endif
NodeID Id = (_id == -1) ? (pinpoint_get_per_thread_id()) : ((NodeID)_id);
pinpoint_add_clue(Id, key.c_str(), value.c_str(), (E_NODE_LOC)_flag);
}
PHP_FUNCTION(_pinpoint_unique_id) {
RETURN_DOUBLE(static_cast<double>(generate_unique_id()));
}
PHP_FUNCTION(_pinpoint_mark_as_error) {
std::string msg;
std::string fileName;
#if PHP_VERSION_ID < 70000
char *zkey = NULL, *zvalue = NULL;
int zkey_len, value_len;
long _lineno = 0;
long _id = -1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|ll", &zkey,
&zkey_len, &zvalue, &value_len, &_lineno,
&_id) == FAILURE) {
zend_error(E_ERROR,
"pinpoint_mark_as_error() expects (string,string,int,int).");
return;
}
msg = std::string(zkey, zkey_len);
fileName = std::string(zvalue, value_len);
#else
zend_string *zkey;
zend_string *zvalue;
zend_long _lineno = 0;
zend_long _id = -1;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS|ll", &zkey, &zvalue, &_lineno,
&_id) == FAILURE) {
zend_error(E_ERROR,
"pinpoint_mark_as_error() expects (string,string,int,int).");
return;
}
msg = std::string(zkey->val, zkey->len);
fileName = std::string(zvalue->val, zvalue->len);
#endif
NodeID id = (_id == -1) ? (pinpoint_get_per_thread_id()) : ((NodeID)_id);
catch_error(id, msg.c_str(), fileName.c_str(), _lineno);
}
PHP_FUNCTION(_pinpoint_add_clues) {
std::string key;
std::string value;
#if PHP_VERSION_ID < 70000
long _id = -1;
long _flag = E_LOC_CURRENT;
char *zkey = NULL, *zvalue = NULL;
size_t zkey_len, value_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|ll", &zkey,
&zkey_len, &zvalue, &value_len, &_id,
&_flag) == FAILURE) {
zend_error(E_ERROR, "pinpoint_add_clues() expects (int, string).");
return;
}
key = std::string(zkey, zkey_len);
value = std::string(zvalue, value_len);
#else
zend_string *zkey;
zend_string *zvalue;
zend_long _id = -1;
zend_long _flag = E_LOC_CURRENT;
int ret = zend_parse_parameters(ZEND_NUM_ARGS(), "SS|ll", &zkey, &zvalue,
&_id, &_flag);
if (ret == FAILURE) {
zend_error(E_ERROR, "pinpoint_add_clues() expects (int, string).");
return;
}
key = std::string(zkey->val, zkey->len);
value = std::string(zvalue->val, zvalue->len);
#endif
NodeID id = (_id == -1) ? (pinpoint_get_per_thread_id()) : ((NodeID)_id);
pinpoint_add_clues(id, key.c_str(), value.c_str(), (E_NODE_LOC)_flag);
}
static inline zend_string *merge_pp_style_name(zend_string *scope,
zend_string *func) {
#define MAX_CLASS_METHOD_SIZE 128
char buf[MAX_CLASS_METHOD_SIZE] = {0};
if (scope) {
int size = snprintf(buf, MAX_CLASS_METHOD_SIZE, "%s::%s", ZSTR_VAL(scope),
ZSTR_VAL(func));
zend_string *name = zend_string_init(buf, size, 0);
zend_string *lower_name = zend_string_tolower(name);
zend_string_release(name);
return lower_name;
} else {
return zend_string_tolower(func);
}
}
#if (PHP_MAJOR_VERSION == 8 && PHP_MINOR_VERSION >= 2)
// ref from php-8.2.19/ext/standard/var.c:137
static zval *zend_array_index(zval *ar, uint32_t index) {
HashTable *__ht = Z_ARRVAL_P(ar);
uint32_t _idx = 0;
uint32_t _count = __ht->nNumUsed - _idx;
size_t _size = ZEND_HASH_ELEMENT_SIZE(__ht);
zval *__z = ZEND_HASH_ELEMENT_EX(__ht, _idx, _size);
zval *ret_zval = nullptr;
for (; _count > 0 && _idx < index; _count--) {
ret_zval = __z;
if (HT_IS_PACKED(__ht)) {
__z++;
} else {
Bucket *_p = (Bucket *)__z;
__z = &(_p + 1)->val;
}
_idx++;
}
return ret_zval;
}
#else
// ref from php-8.1.25/ext/standard/var.c:137
static zval *zend_array_index(zval *ar, int index) {
HashTable *__ht = Z_ARRVAL_P(ar);
Bucket *_p = __ht->arData;
Bucket *_end = _p + __ht->nNumUsed;
zval *val;
for (int i = 0; i < index && _p != _end; _p++, i++) {
val = &_p->val;
}
return val;
}
#endif
static inline pp_interceptor_v_t *find_interceptor(zend_string *func_name) {
return (pp_interceptor_v_t *)zend_hash_str_find_ptr(
PPG(interceptors), ZSTR_VAL(func_name), ZSTR_LEN(func_name));
}
static zend_string *
get_pp_style_function_name(zend_execute_data *execute_data) {
zend_function *func = execute_data->func;
zend_object *object = (Z_TYPE(execute_data->This) == IS_OBJECT)
? Z_OBJ(execute_data->This)
: NULL;
#if PHP_MAJOR_VERSION == 8
zend_string *function_name = NULL;
if (func) {
function_name = func->common.function_name;
}
#else
zend_string *function_name =
(func->common.scope && func->common.scope->trait_aliases)
? zend_resolve_method_name((object ? object->ce : func->common.scope),
func)
: func->common.function_name;
#endif
if (object) {
zend_string *scope_name;
if (func->common.scope) {
scope_name = func->common.scope->name;
#if PHP_MAJOR_VERSION == 7 && PHP_MINOR_VERSION <= 2
} else if (object->handlers->get_class_name ==
std_object_handlers.get_class_name) {
#else
} else if (object->handlers->get_class_name == zend_std_get_class_name) {
#endif
scope_name = object->ce->name;
} else {
scope_name = object->handlers->get_class_name(object);
}
return merge_pp_style_name(scope_name, function_name);
} else if (func->common.scope) {
zend_string *scope = func->common.scope->name;
return merge_pp_style_name(scope, function_name);
} else {
return merge_pp_style_name(nullptr, function_name);
}
}
static void call_callback_function(zval *callback, zval *params,
uint32_t params_count, int free_params,
zval *needs_retval = nullptr) {
// ref SAPI.c:139
int error;
zend_fcall_info fcall_info;
char *callback_error = NULL;
zval default_retval;
zval *retval = &default_retval;
if (needs_retval) {
retval = needs_retval;
}
zend_fcall_info_cache fcall_cache;
if (zend_fcall_info_init(callback, 0, &fcall_info, &fcall_cache, NULL,
&callback_error) == SUCCESS) {
fcall_info.retval = retval;
fcall_info.param_count = params_count;
fcall_info.params = params;
fcall_info.object = NULL;
#if PHP_MAJOR_VERSION == 7
fcall_info.no_separation = 0;
#endif
// fcall_cache.initialized = 1;
// fcall_cache.function_handler = EG(autoload_func);
fcall_cache.called_scope = NULL;
fcall_cache.object = NULL;
error = zend_call_function(&fcall_info, &fcall_cache);
if (error == FAILURE) {
goto callback_failed;
} else {
if (!needs_retval) {
zval_ptr_dtor(retval);
}
}
} else {
callback_failed:
php_error_docref(NULL, E_WARNING,
"Could not call the pinpoint_join_cut callback");
}
if (callback_error) {
efree(callback_error);
}
if (free_params) {
zend_fcall_info_args_clear(&fcall_info, 1);
}
}
static void get_shadow_copy_current_parameters(int param_count,
zval *argument_array) {
// ref zend_API.c:46
// _zend_get_parameters_array_ex
zval *param_ptr;
param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1);
while (param_count-- > 0) {
ZVAL_COPY(argument_array, param_ptr);
argument_array++;
param_ptr++;
}
}
static void replace_ex_caller_parameters(zval *argv) {
if (zval_get_type(argv) != IS_ARRAY) {
pp_trace("replace_ex_caller_parameters return value must be `an array`");
return;
}
uint32_t size = zend_array_count(Z_ARRVAL_P(argv));
pp_trace("argv size:%d", size);
uint32_t param_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
if (size != param_count) {
pp_trace(
"error: replace_ex_caller_parameters return `size` does not matched");
return;
}
uint32_t i = 0;
zval *ex_param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1);
// check old and new
while (i < size) {
zval *val = zend_array_index(argv, i + 1);
if (Z_TYPE_P(ex_param_ptr) != Z_TYPE_P(val)) {
pp_trace("error: replace_ex_caller_parameters return `type` does not "
"matched "
"expected:%d give:%d",
Z_TYPE_P(ex_param_ptr), Z_TYPE_P(val));
return;
}
i++;
ex_param_ptr++;
}
i = 0;
ex_param_ptr = ZEND_CALL_ARG(EG(current_execute_data), 1);
while (i < size) {
zval *val = zend_array_index(argv, i + 1);
if (Z_TYPE_P(val) == IS_ARRAY) {
pp_trace("args[%d] type:%d in:%u va:%u", i, zval_get_type(val),
Z_REFCOUNT_P(val), Z_REFCOUNT_P(ex_param_ptr));
}
zval_ptr_dtor(ex_param_ptr);
ZVAL_COPY(ex_param_ptr, val);
i++;
ex_param_ptr++;
}
}
static void call_interceptor_before(pp_interceptor_v_t *interceptor) {
uint32_t param_count = ZEND_CALL_NUM_ARGS(EG(current_execute_data));
zval *params = (zval *)safe_emalloc(param_count, sizeof(zval), 0);
get_shadow_copy_current_parameters(param_count, params);
zval retval;
call_callback_function(&interceptor->before, params, param_count, 1, &retval);
pp_trace(" call_interceptor_before:%s return type(%d) zval",
ZSTR_VAL(interceptor->name), zval_get_type(&retval));
replace_ex_caller_parameters(&retval);
zval_ptr_dtor(&retval);
}
static void call_interceptor_end(pp_interceptor_v_t *interceptor,
zval *return_value) {
uint32_t param_count = 1;
zval *params = (zval *)safe_emalloc(param_count, sizeof(zval), 0);
ZVAL_COPY(params, return_value);
call_callback_function(&interceptor->end, params, param_count, 1);
pp_trace("call_interceptor_end: %s ", ZSTR_VAL(interceptor->name));
}
static void call_interceptor_exception(pp_interceptor_v_t *interceptor) {
zend_object *exception = EG(exception);
pp_trace(" exception: %p ", exception);
}
ZEND_NAMED_FUNCTION(pinpoint_interceptor_handler_entry) {
// 1. get function/method name
// ref zend_builtin_functions.c:2278
zend_string *function_name = get_pp_style_function_name(execute_data);
pp_trace("pinpoint_interceptor_handler_entry: handle func/method:%s",
ZSTR_VAL(function_name));
pp_interceptor_v_t *interceptor = find_interceptor(function_name);
zend_string_release(function_name);
if (interceptor == nullptr) {
pp_trace(" MUST be a bug 🐞🐞🐞 !!! please send us "
"email@dl_cd_pinpoint@navercorp.com");
return;
}
// 2. call before
// 3. call origin
// 4. call end
// 5. call exception if catch
zend_try {
call_interceptor_before(interceptor);
interceptor->origin(INTERNAL_FUNCTION_PARAM_PASSTHRU);
call_interceptor_end(interceptor, return_value);
}
zend_catch {
call_interceptor_exception(interceptor);
call_interceptor_end(interceptor, return_value);
}
zend_end_try();
}
static pp_interceptor_v_t *make_interceptor(zend_string *name, zval *before,
zval *end, zval *exception,
zend_function *origin_func) {
pp_interceptor_v_t *interceptor =
(pp_interceptor_v_t *)malloc(sizeof(pp_interceptor_v_t));
// bzero(interceptor, sizeof(*interceptor));
memset(interceptor, 0, sizeof(*interceptor));
interceptor->name = zend_string_dup(name, 0);
interceptor->origin = origin_func->internal_function.handler;
interceptor->origin_func = origin_func;
ZVAL_DUP(&interceptor->before, before);
ZVAL_DUP(&interceptor->end, end);
ZVAL_DUP(&interceptor->exception, exception);
return interceptor;
}
static inline void free_callback(zval *val) {
if (Z_TYPE_P(val) != IS_UNDEF) {
zval_ptr_dtor(val);
}
}
static void free_interceptor(pp_interceptor_v_t *interceptor) {
pp_trace("start free interceptor: %s", ZSTR_VAL(interceptor->name));
zend_function *origin_func = interceptor->origin_func;
origin_func->internal_function.handler = interceptor->origin;
zend_string_release(interceptor->name);
free_callback(&interceptor->before);
free_callback(&interceptor->end);
free_callback(&interceptor->exception);
free(interceptor);
}
static void add_function_interceptor(zend_string *name, zval *before, zval *end,
zval *exception) {
zend_function *func = (zend_function *)zend_hash_str_find_ptr(
CG(function_table), ZSTR_VAL(name), ZSTR_LEN(name));
if (func != NULL &&
func->internal_function.handler == pinpoint_interceptor_handler_entry) {
pp_trace("function `%s` interceptor already added", ZSTR_VAL(name));
} else if (func != NULL) {
pp_interceptor_v_t *interceptor =
make_interceptor(name, before, end, exception, func);
// insert into hash
if (!zend_hash_add_ptr(PPG(interceptors), name, interceptor)) {
free_interceptor(interceptor);
pp_trace("added interceptor on `function`: %s failed. reason: already "
"exist ",
ZSTR_VAL(name));
return;
}
func->internal_function.handler = pinpoint_interceptor_handler_entry;
pp_trace("added interceptor on `function`: %s success", ZSTR_VAL(name));
} else {
pp_trace("not found function: %s", ZSTR_VAL(name));
}
}
static void add_class_method_interceptor(zend_string *cls_name,
zend_string *method, zval *before,
zval *end, zval *exception) {
zend_class_entry *module = (zend_class_entry *)zend_hash_str_find_ptr(
CG(class_table), ZSTR_VAL(cls_name), ZSTR_LEN(cls_name));
if (module != NULL) {
zend_function *original = (zend_function *)zend_hash_str_find_ptr(
&module->function_table, ZSTR_VAL(method), ZSTR_LEN(method));
if (original != NULL) {
// overwrite with plugins eg: pinpoint_pdo_exec
#define MAX_CLASS_METHOD_SIZE 128
char buf[MAX_CLASS_METHOD_SIZE] = {0};
int size = snprintf(buf, MAX_CLASS_METHOD_SIZE, "%s::%s",
ZSTR_VAL(cls_name), ZSTR_VAL(method));
zend_string *name = zend_string_init(buf, size, 0);
pp_interceptor_v_t *interceptor =
make_interceptor(name, before, end, exception, original);
if (!zend_hash_add_ptr(PPG(interceptors), name, interceptor)) {
free_interceptor(interceptor);
pp_trace("added interceptor on `module`: %s failed. reason: already "
"exist ",
ZSTR_VAL(name));
zend_string_release(name);
return;
}
original->internal_function.handler = pinpoint_interceptor_handler_entry;
pp_trace("added interceptor on `module`: %s success", ZSTR_VAL(name));
zend_string_release(name);
} else {
pp_trace("add interceptor on `module`: %s::%s failed: no such method",
ZSTR_VAL(cls_name), ZSTR_VAL(method));
}
} else {
pp_trace("add interceptor on `module`: %s failed: no such module",
ZSTR_VAL(cls_name));
// debug CG(class_table)
// zend_class_entry *ce;
// void *val;
// ZEND_HASH_FOREACH_PTR(CG(class_table), val) {
// zend_class_entry *ce = (zend_class_entry *)val;
// pp_trace("key:%s name: %s", ZSTR_VAL(_p->key), ZSTR_VAL(ce->name));
// // if (ce->type == ZEND_INTERNAL_CLASS &&
// // ce->default_static_members_count > 0) {
// // class_cleanup_handlers[--class_count] = ce;
// // }
// }
// ZEND_HASH_FOREACH_END();
}
}
static void add_interceptor(zval *joinable, zval *before, zval *end,
zval *exception) {
uint32_t join_type = zend_hash_num_elements(Z_ARRVAL_P(joinable));
switch (join_type) {
case 2: {
zval *v = zend_array_index(joinable, 1);
zend_string *module = zend_string_tolower(Z_STR_P(v));
v = zend_array_index(joinable, 2);
zend_string *method = zend_string_tolower(Z_STR_P(v));
pp_trace("try to interceptor module(class)/function=%s:%s",
ZSTR_VAL(module), ZSTR_VAL(method));