xref: /PHP-7.4/sapi/phpdbg/phpdbg_watch.h (revision 8483a21f)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) The PHP Group                                          |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP 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.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Felipe Pena <felipe@php.net>                                |
16    | Authors: Joe Watkins <joe.watkins@live.co.uk>                        |
17    | Authors: Bob Weinand <bwoebi@php.net>                                |
18    +----------------------------------------------------------------------+
19 */
20 
21 #ifndef PHPDBG_WATCH_H
22 #define PHPDBG_WATCH_H
23 
24 #include "phpdbg_cmd.h"
25 
26 #ifdef _WIN32
27 #	include "phpdbg_win.h"
28 #endif
29 
30 #define PHPDBG_WATCH(name) PHPDBG_COMMAND(watch_##name)
31 
32 /**
33  * Printer Forward Declarations
34  */
35 PHPDBG_WATCH(array);
36 PHPDBG_WATCH(delete);
37 PHPDBG_WATCH(recursive);
38 
39 extern const phpdbg_command_t phpdbg_watch_commands[];
40 
41 /* Watchpoint functions/typedefs */
42 
43 /* References are managed through their parent zval *, being a simple WATCH_ON_ZVAL and eventually WATCH_ON_REFCOUNTED */
44 typedef enum {
45 	WATCH_ON_ZVAL,
46 	WATCH_ON_HASHTABLE,
47 	WATCH_ON_REFCOUNTED,
48 	WATCH_ON_STR,
49 	WATCH_ON_HASHDATA,
50 	WATCH_ON_BUCKET,
51 } phpdbg_watchtype;
52 
53 
54 #define PHPDBG_WATCH_SIMPLE     0x01
55 #define PHPDBG_WATCH_RECURSIVE  0x02
56 #define PHPDBG_WATCH_ARRAY      0x04
57 #define PHPDBG_WATCH_OBJECT     0x08
58 #define PHPDBG_WATCH_NORMAL     (PHPDBG_WATCH_SIMPLE | PHPDBG_WATCH_RECURSIVE)
59 #define PHPDBG_WATCH_IMPLICIT   0x10
60 #define PHPDBG_WATCH_RECURSIVE_ROOT 0x20
61 
62 typedef struct _phpdbg_watch_collision phpdbg_watch_collision;
63 
64 typedef struct _phpdbg_watchpoint_t {
65 	union {
66 		zval *zv;
67 		zend_refcounted *ref;
68 		Bucket *bucket;
69 		void *ptr;
70 	} addr;
71 	size_t size;
72 	phpdbg_watchtype type;
73 	zend_refcounted *ref; /* key to fetch the collision on parents */
74 	HashTable elements;
75 	phpdbg_watch_collision *coll; /* only present on *children* */
76 	union {
77 		zval zv;
78 		Bucket bucket;
79 		zend_refcounted ref;
80 		HashTable ht;
81 		zend_string *str;
82 	} backup;
83 } phpdbg_watchpoint_t;
84 
85 struct _phpdbg_watch_collision {
86 	phpdbg_watchpoint_t ref;
87 	phpdbg_watchpoint_t reference;
88 	HashTable parents;
89 };
90 
91 typedef struct _phpdbg_watch_element {
92 	uint32_t id;
93 	phpdbg_watchpoint_t *watch;
94 	char flags;
95 	struct _phpdbg_watch_element *child; /* always set for implicit watches */
96 	struct _phpdbg_watch_element *parent;
97 	HashTable child_container; /* children of this watch element for recursive array elements */
98 	HashTable *parent_container; /* container of the value */
99 	zend_string *name_in_parent;
100 	zend_string *str;
101 	union {
102 		zval zv;
103 		zend_refcounted ref;
104 		HashTable ht;
105 	} backup; /* backup for when watchpoint gets dissociated */
106 } phpdbg_watch_element;
107 
108 typedef struct {
109 	/* to watch rehashes (yes, this is not *perfect*, but good enough for everything in PHP...) */
110 	phpdbg_watchpoint_t hash_watch; /* must be first element */
111 	Bucket *last;
112 	zend_string *last_str;
113 	zend_ulong last_idx;
114 
115 	HashTable *ht;
116 	size_t data_size;
117 	HashTable watches; /* contains phpdbg_watch_element */
118 } phpdbg_watch_ht_info;
119 
120 void phpdbg_setup_watchpoints(void);
121 void phpdbg_destroy_watchpoints(void);
122 void phpdbg_purge_watchpoint_tree(void);
123 
124 #ifndef _WIN32
125 int phpdbg_watchpoint_segfault_handler(siginfo_t *info, void *context);
126 #else
127 int phpdbg_watchpoint_segfault_handler(void *addr);
128 #endif
129 
130 void phpdbg_create_addr_watchpoint(void *addr, size_t size, phpdbg_watchpoint_t *watch);
131 void phpdbg_create_zval_watchpoint(zval *zv, phpdbg_watchpoint_t *watch);
132 
133 int phpdbg_delete_var_watchpoint(char *input, size_t len);
134 int phpdbg_create_var_watchpoint(char *input, size_t len);
135 
136 int phpdbg_print_changed_zvals(void);
137 
138 void phpdbg_list_watchpoints(void);
139 
140 void phpdbg_watch_efree(void *ptr);
141 
142 
143 static long phpdbg_pagesize;
144 
phpdbg_get_page_boundary(void * addr)145 static zend_always_inline void *phpdbg_get_page_boundary(void *addr) {
146 	return (void *) ((size_t) addr & ~(phpdbg_pagesize - 1));
147 }
148 
phpdbg_get_total_page_size(void * addr,size_t size)149 static zend_always_inline size_t phpdbg_get_total_page_size(void *addr, size_t size) {
150 	return (size_t) phpdbg_get_page_boundary((void *) ((size_t) addr + size - 1)) - (size_t) phpdbg_get_page_boundary(addr) + phpdbg_pagesize;
151 }
152 
153 #endif
154