php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
zend_vm_trace_handlers.h
Go to the documentation of this file.
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
25static HashTable vm_trace_ht;
26
27static 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)) {
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
54static 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
65static void zend_vm_trace_finish(void)
66{
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);
77 fclose(f);
78 }
79 zend_hash_destroy(&vm_trace_ht);
80}
81
82static 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}
size_t len
Definition apprentice.c:174
fprintf($stream, string $format, mixed ... $values)
feof($stream)
fclose($stream)
fopen(string $filename, string $mode, bool $use_include_path=false, $context=null)
fgets($stream, ?int $length=null)
zval * zv
Definition ffi.c:3975
memcpy(ptr1, ptr2, size)
zval * val
Definition ffi.c:4262
zend_ffi_ctype_name_buf buf
Definition ffi.c:4685
#define NULL
Definition gdcache.h:45
unsigned char key[REFLECTION_KEY_LEN]
#define zend_hash_str_add(...)
Definition phpdbg.h:77
zval val
Definition zend_types.h:381
struct _zval_struct zval
strlen(string $string)
ZEND_API void ZEND_FASTCALL zend_hash_destroy(HashTable *ht)
Definition zend_hash.c:1727
ZEND_API zval *ZEND_FASTCALL zend_hash_str_find(const HashTable *ht, const char *str, size_t len)
Definition zend_hash.c:2689
ZEND_API zval *ZEND_FASTCALL zend_hash_str_add_new(HashTable *ht, const char *str, size_t len, zval *pData)
Definition zend_hash.c:1052
#define zend_hash_init(ht, nSize, pHashFunction, pDestructor, persistent)
Definition zend_hash.h:108
#define ZEND_HASH_FOREACH_END()
Definition zend_hash.h:1086
#define ZEND_HASH_MAP_FOREACH_STR_KEY_VAL(ht, _key, _val)
Definition zend_hash.h:1374
#define ZEND_STRTOL(s0, s1, base)
Definition zend_long.h:85
#define ZEND_LONG_FMT
Definition zend_long.h:87
#define ZEND_LONG_MAX
Definition zend_long.h:45
struct _zend_string zend_string
int last
#define EXPECTED(condition)
#define ZSTR_VAL(zstr)
Definition zend_string.h:68
int(* compare_func_t)(const void *, const void *)
Definition zend_types.h:104
#define ZVAL_LONG(z, l)
struct _zend_array HashTable
Definition zend_types.h:386
struct _Bucket Bucket
#define Z_LVAL_P(zval_p)
Definition zend_types.h:966
#define Z_LVAL(zval)
Definition zend_types.h:965