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 Bucket *last;
110 zend_string *last_str;
111 zend_ulong last_idx;
112
113 HashTable *ht;
114 size_t data_size;
115 HashTable watches; /* contains phpdbg_watch_element */
116 } phpdbg_watch_ht_info;
117
118 void phpdbg_setup_watchpoints(void);
119 void phpdbg_destroy_watchpoints(void);
120 void phpdbg_purge_watchpoint_tree(void);
121
122 #ifndef _WIN32
123 int phpdbg_watchpoint_segfault_handler(siginfo_t *info, void *context);
124 #else
125 int phpdbg_watchpoint_segfault_handler(void *addr);
126 #endif
127
128 void phpdbg_create_addr_watchpoint(void *addr, size_t size, phpdbg_watchpoint_t *watch);
129 void phpdbg_create_zval_watchpoint(zval *zv, phpdbg_watchpoint_t *watch);
130
131 int phpdbg_delete_var_watchpoint(char *input, size_t len);
132 int phpdbg_create_var_watchpoint(char *input, size_t len);
133
134 int phpdbg_print_changed_zvals(void);
135
136 void phpdbg_list_watchpoints(void);
137
138 void phpdbg_watch_efree(void *ptr);
139
140
141 static long phpdbg_pagesize;
142
phpdbg_get_page_boundary(void * addr)143 static zend_always_inline void *phpdbg_get_page_boundary(void *addr) {
144 return (void *) ((size_t) addr & ~(phpdbg_pagesize - 1));
145 }
146
phpdbg_get_total_page_size(void * addr,size_t size)147 static zend_always_inline size_t phpdbg_get_total_page_size(void *addr, size_t size) {
148 return (size_t) phpdbg_get_page_boundary((void *) ((size_t) addr + size - 1)) - (size_t) phpdbg_get_page_boundary(addr) + phpdbg_pagesize;
149 }
150
151 #endif
152