xref: /PHP-8.3/Zend/zend_vm_trace_handlers.h (revision 90b7bde6)
1 /*
2    +----------------------------------------------------------------------+
3    | Zend Engine                                                          |
4    +----------------------------------------------------------------------+
5    | Copyright (c) Zend Technologies Ltd. (http://www.zend.com)           |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 2.00 of the Zend license,     |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.zend.com/license/2_00.txt.                                |
11    | If you did not receive a copy of the Zend license and are unable to  |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@zend.com so we can mail you a copy immediately.              |
14    +----------------------------------------------------------------------+
15    | Authors: Dmitry Stogov <dmitry@php.net>                              |
16    +----------------------------------------------------------------------+
17 */
18 
19 #include "zend_sort.h"
20 
21 #define VM_TRACE(op)     zend_vm_trace(#op, sizeof(#op)-1);
22 #define VM_TRACE_START() zend_vm_trace_init();
23 #define VM_TRACE_END()   zend_vm_trace_finish();
24 
25 static HashTable vm_trace_ht;
26 
zend_vm_trace(const char * op,size_t op_len)27 static void zend_vm_trace(const char *op, size_t op_len)
28 {
29 	static const char *last = NULL;
30 	static size_t last_len = 0;
31 	char buf[256];
32 	size_t len;
33 	zval tmp, *zv;
34 
35 	if (EXPECTED(last)) {
36 		len = last_len + 1 + op_len;
37 		memcpy(buf, last, last_len);
38 		buf[last_len] = ' ';
39 		memcpy(buf + last_len + 1, op, op_len + 1);
40 		zv = zend_hash_str_find(&vm_trace_ht, buf, len);
41 		if (EXPECTED(zv)) {
42 			if (EXPECTED(Z_LVAL_P(zv) < ZEND_LONG_MAX)) {
43 				Z_LVAL_P(zv)++;
44 			}
45 		} else {
46 			ZVAL_LONG(&tmp, 1);
47 			zend_hash_str_add_new(&vm_trace_ht, buf, len, &tmp);
48 		}
49 	}
50 	last = op;
51 	last_len = op_len;
52 }
53 
zend_vm_trace_compare(const Bucket * p1,const Bucket * p2)54 static int zend_vm_trace_compare(const Bucket *p1, const Bucket *p2)
55 {
56 	if (Z_LVAL(p1->val) < Z_LVAL(p2->val)) {
57 		return 1;
58 	} else if (Z_LVAL(p1->val) > Z_LVAL(p2->val)) {
59 		return -1;
60 	} else {
61 		return 0;
62 	}
63 }
64 
zend_vm_trace_finish(void)65 static void zend_vm_trace_finish(void)
66 {
67 	zend_string *key;
68 	zval *val;
69 	FILE *f;
70 
71 	f = fopen("zend_vm_trace.log", "w+");
72 	if (f) {
73 		zend_hash_sort(&vm_trace_ht, (compare_func_t)zend_vm_trace_compare, 0);
74 		ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(&vm_trace_ht, key, val) {
75 			fprintf(f, "%s "ZEND_LONG_FMT"\n", ZSTR_VAL(key), Z_LVAL_P(val));
76 		} ZEND_HASH_FOREACH_END();
77 		fclose(f);
78 	}
79 	zend_hash_destroy(&vm_trace_ht);
80 }
81 
zend_vm_trace_init(void)82 static void zend_vm_trace_init(void)
83 {
84 	FILE *f;
85 
86 	zend_hash_init(&vm_trace_ht, 0, NULL, NULL, 1);
87 	f = fopen("zend_vm_trace.log", "r");
88 	if (f) {
89 		char buf[256];
90 		size_t len;
91 		zval tmp;
92 
93 		while (!feof(f)) {
94 			if (fgets(buf, sizeof(buf)-1, f)) {
95 				len = strlen(buf);
96 				while (len > 0 && buf[len-1] <= ' ') {
97 					len--;
98 					buf[len] = 0;
99 				}
100 				while (len > 0 && buf[len-1] >= '0' && buf[len-1] <= '9') {
101 					len--;
102 				}
103 				if (len > 1) {
104 					buf[len-1] = 0;
105 					ZVAL_LONG(&tmp, ZEND_STRTOL(buf + len, NULL, 10));
106 					zend_hash_str_add(&vm_trace_ht, buf, len - 1, &tmp);
107 				}
108 			}
109 		}
110 		fclose(f);
111 	}
112 }
113