e887f99aebbc244110a0329ef09391b9ae935b62
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / watchpoint.c
1 #include <stdio.h>
2 #include <unistd.h>
3 /*
4 * Since using watchpoints can be very slow, we have to take some pains to
5 * ensure that we don't run too long with them enabled or we run the risk
6 * of having the test timeout. To help avoid this, we insert some marker
7 * functions in the execution stream so we can set breakpoints at known
8 * locations, without worrying about invalidating line numbers by changing
9 * this file. We use null bodied functions are markers since gdb does
10 * not support breakpoints at labeled text points at this time.
11 *
12 * One place we need is a marker for when we start executing our tests
13 * instructions rather than any process startup code, so we insert one
14 * right after entering main(). Another is right before we finish, before
15 * we start executing any process termination code.
16 *
17 * Another problem we have to guard against, at least for the test
18 * suite, is that we need to ensure that the line that causes the
19 * watchpoint to be hit is still the current line when gdb notices
20 * the hit. Depending upon the specific code generated by the compiler,
21 * the instruction after the one that triggers the hit may be part of
22 * the same line or part of the next line. Thus we ensure that there
23 * are always some instructions to execute on the same line after the
24 * code that should trigger the hit.
25 */
26
27 int count = -1;
28 int ival1 = -1;
29 int ival2 = -1;
30 int ival3 = -1;
31 int ival4 = -1;
32 int ival5 = -1;
33 char buf[30] = "testtesttesttesttesttesttestte";
34 struct foo
35 {
36 int val;
37 };
38 struct foo struct1, struct2, *ptr1, *ptr2;
39
40 int doread = 0;
41
42 char *global_ptr;
43 char **global_ptr_ptr;
44
45 struct foo2
46 {
47 int val[2];
48 };
49 struct foo2 foo2;
50
51 struct foo4
52 {
53 int val[4];
54 };
55 struct foo4 foo4;
56
57 void marker1 ()
58 {
59 }
60
61 void marker2 ()
62 {
63 }
64
65 void marker4 ()
66 {
67 }
68
69 void marker5 ()
70 {
71 }
72
73 void marker6 ()
74 {
75 }
76
77 #ifdef PROTOTYPES
78 void recurser (int x)
79 #else
80 void recurser (x) int x;
81 #endif
82 {
83 int local_x = 0;
84
85 if (x > 0)
86 recurser (x-1);
87 local_x = x;
88 }
89
90 void
91 func2 ()
92 {
93 int local_a = 0;
94 static int static_b;
95
96 /* func2 breakpoint here */
97 ival5++;
98 local_a = ival5;
99 static_b = local_a;
100 }
101
102 void
103 func3 ()
104 {
105 int x;
106 int y;
107
108 x = 0;
109 x = 1; /* second x assignment */
110 y = 1;
111 y = 2;
112 buf[26] = 3;
113 }
114
115 int
116 func1 ()
117 {
118 /* The point of this is that we will set a breakpoint at this call.
119
120 Then, if DECR_PC_AFTER_BREAK equals the size of a function call
121 instruction (true on a sun3 if this is gcc-compiled--FIXME we
122 should use asm() to make it work for any compiler, present or
123 future), then we will end up branching to the location just after
124 the breakpoint. And we better not confuse that with hitting the
125 breakpoint. */
126 func2 ();
127 return 73;
128 }
129
130 void
131 func4 ()
132 {
133 buf[0] = 3;
134 global_ptr = buf;
135 buf[0] = 7;
136 buf[1] = 5;
137 global_ptr_ptr = &global_ptr;
138 buf[0] = 9;
139 global_ptr++;
140 }
141
142 void
143 func5 ()
144 {
145 int val = 0, val2 = 23;
146 int *x = &val;
147
148 /* func5 breakpoint here */
149 x = &val2;
150 val = 27;
151 }
152
153 void
154 func6 (void)
155 {
156 /* func6 breakpoint here */
157 foo2.val[1] = 0;
158 foo2.val[1] = 11;
159 }
160
161 void
162 func7 (void)
163 {
164 /* func7 breakpoint here */
165 foo4.val[3] = 0;
166 foo4.val[3] = 33;
167 }
168
169 int main ()
170 {
171 struct1.val = 1;
172 struct2.val = 2;
173 ptr1 = &struct1;
174 ptr2 = &struct2;
175 marker1 ();
176 func1 ();
177 for (count = 0; count < 4; count++) {
178 ival1 = count;
179 ival3 = count; ival4 = count;
180 }
181 ival1 = count; /* Outside loop */
182 ival2 = count;
183 ival3 = count; ival4 = count;
184 marker2 ();
185 if (doread)
186 {
187 static char msg[] = "type stuff for buf now:";
188 write (1, msg, sizeof (msg) - 1);
189 read (0, &buf[0], 5);
190 }
191 marker4 ();
192
193 /* We have a watchpoint on ptr1->val. It should be triggered if
194 ptr1's value changes. */
195 ptr1 = ptr2;
196
197 /* This should not trigger the watchpoint. If it does, then we
198 used the wrong value chain to re-insert the watchpoints or we
199 are not evaluating the watchpoint expression correctly. */
200 struct1.val = 5;
201 marker5 ();
202
203 /* We have a watchpoint on ptr1->val. It should be triggered if
204 ptr1's value changes. */
205 ptr1 = ptr2;
206
207 /* This should not trigger the watchpoint. If it does, then we
208 used the wrong value chain to re-insert the watchpoints or we
209 are not evaluating the watchpoint expression correctly. */
210 struct1.val = 5;
211 marker5 ();
212
213 /* We're going to watch locals of func2, to see that out-of-scope
214 watchpoints are detected and properly deleted.
215 */
216 marker6 ();
217
218 /* This invocation is used for watches of a single
219 local variable. */
220 func2 ();
221
222 /* This invocation is used for watches of an expression
223 involving a local variable. */
224 func2 ();
225
226 /* This invocation is used for watches of a static
227 (non-stack-based) local variable. */
228 func2 ();
229
230 /* This invocation is used for watches of a local variable
231 when recursion happens.
232 */
233 marker6 ();
234 recurser (2);
235
236 /* This invocation is used for watches of a local variable with explicitly
237 specified scope when recursion happens.
238 */
239 marker6 ();
240 recurser (2);
241
242 marker6 ();
243
244 func3 ();
245
246 func4 ();
247
248 func5 ();
249
250 func6 ();
251
252 func7 ();
253
254 return 0;
255 }
This page took 0.038154 seconds and 3 git commands to generate.