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