xref: /PHP-8.3/Zend/zend_vm_trace_map.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_vm_handlers.h"
20 #include "zend_sort.h"
21 
22 #define GEN_MAP(n, name) do { \
23 		ZVAL_LONG(&tmp, (zend_long)(uintptr_t)zend_opcode_handlers[n]); \
24 		zend_hash_str_add(&vm_trace_ht, #name, sizeof(#name) - 1, &tmp); \
25 	} while (0);
26 
27 #define VM_TRACE_START() do { \
28 		zval tmp; \
29 		zend_hash_init(&vm_trace_ht, 0, NULL, NULL, 1); \
30 		VM_HANDLERS(GEN_MAP) \
31 		zend_vm_trace_init(); \
32 	} while (0)
33 
34 #ifdef _WIN64
35 # define ADDR_FMT "%016I64x"
36 #elif SIZEOF_SIZE_T == 4
37 # define ADDR_FMT "%08zx"
38 #elif SIZEOF_SIZE_T == 8
39 # define ADDR_FMT "%016zx"
40 #else
41 # error "Unknown SIZEOF_SIZE_T"
42 #endif
43 
44 static HashTable vm_trace_ht;
45 
zend_vm_trace_compare(const Bucket * p1,const Bucket * p2)46 static int zend_vm_trace_compare(const Bucket *p1, const Bucket *p2)
47 {
48 	if (Z_LVAL(p1->val) > Z_LVAL(p2->val)) {
49 		return 1;
50 	} else if (Z_LVAL(p1->val) < Z_LVAL(p2->val)) {
51 		return -1;
52 	} else {
53 		return 0;
54 	}
55 }
56 
zend_vm_trace_init(void)57 static void zend_vm_trace_init(void)
58 {
59 	FILE *f;
60 	zend_string *key, *prev_key;
61 	zval *val;
62 	zend_long prev_addr;
63 
64 	f = fopen("zend_vm.map", "w+");
65 	if (f) {
66 		zend_hash_sort(&vm_trace_ht, (bucket_compare_func_t)zend_vm_trace_compare, 0);
67 		prev_key = NULL;
68 		ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(&vm_trace_ht, key, val) {
69 			if (prev_key) {
70 				fprintf(f, ADDR_FMT" "ADDR_FMT" t %s\n", prev_addr, Z_LVAL_P(val) - prev_addr, ZSTR_VAL(prev_key));
71 			}
72 			prev_key  = key;
73 			prev_addr = Z_LVAL_P(val);
74 		} ZEND_HASH_FOREACH_END();
75 		if (prev_key) {
76 			fprintf(f, ADDR_FMT" "ADDR_FMT" t %s\n", prev_addr, 0, ZSTR_VAL(prev_key));
77 		}
78 		fclose(f);
79 	}
80 	zend_hash_destroy(&vm_trace_ht);
81 }
82