2011-12-13 Pedro Alves <pedro@codesourcery.com>
[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;
84
85 if (x > 0)
86 recurser (x-1);
87 local_x = x;
88 }
89
90 void
91 func2 ()
92 {
93 int local_a;
94 static int static_b;
95
96 ival5++;
97 local_a = ival5;
98 static_b = local_a;
99 }
100
101 void
102 func3 ()
103 {
104 int x;
105 int y;
106
107 x = 0;
108 x = 1; /* second x assignment */
109 y = 1;
110 y = 2;
111 buf[26] = 3;
112 }
113
114 int
115 func1 ()
116 {
117 /* The point of this is that we will set a breakpoint at this call.
118
119 Then, if DECR_PC_AFTER_BREAK equals the size of a function call
120 instruction (true on a sun3 if this is gcc-compiled--FIXME we
121 should use asm() to make it work for any compiler, present or
122 future), then we will end up branching to the location just after
123 the breakpoint. And we better not confuse that with hitting the
124 breakpoint. */
125 func2 ();
126 return 73;
127 }
128
129 void
130 func4 ()
131 {
132 buf[0] = 3;
133 global_ptr = buf;
134 buf[0] = 7;
135 buf[1] = 5;
136 global_ptr_ptr = &global_ptr;
137 buf[0] = 9;
138 global_ptr++;
139 }
140
141 void
142 func5 ()
143 {
144 int val = 0, val2 = 23;
145 int *x = &val;
146
147 /* func5 breakpoint here */
148 x = &val2;
149 val = 27;
150 }
151
152 void
153 func6 (void)
154 {
155 /* func6 breakpoint here */
156 foo2.val[1] = 0;
157 foo2.val[1] = 11;
158 }
159
160 void
161 func7 (void)
162 {
163 /* func7 breakpoint here */
164 foo4.val[3] = 0;
165 foo4.val[3] = 33;
166 }
167
168 int main ()
169 {
170 #ifdef usestubs
171 set_debug_traps();
172 breakpoint();
173 #endif
174 struct1.val = 1;
175 struct2.val = 2;
176 ptr1 = &struct1;
177 ptr2 = &struct2;
178 marker1 ();
179 func1 ();
180 for (count = 0; count < 4; count++) {
181 ival1 = count;
182 ival3 = count; ival4 = count;
183 }
184 ival1 = count; /* Outside loop */
185 ival2 = count;
186 ival3 = count; ival4 = count;
187 marker2 ();
188 if (doread)
189 {
190 static char msg[] = "type stuff for buf now:";
191 write (1, msg, sizeof (msg) - 1);
192 read (0, &buf[0], 5);
193 }
194 marker4 ();
195
196 /* We have a watchpoint on ptr1->val. It should be triggered if
197 ptr1's value changes. */
198 ptr1 = ptr2;
199
200 /* This should not trigger the watchpoint. If it does, then we
201 used the wrong value chain to re-insert the watchpoints or we
202 are not evaluating the watchpoint expression correctly. */
203 struct1.val = 5;
204 marker5 ();
205
206 /* We have a watchpoint on ptr1->val. It should be triggered if
207 ptr1's value changes. */
208 ptr1 = ptr2;
209
210 /* This should not trigger the watchpoint. If it does, then we
211 used the wrong value chain to re-insert the watchpoints or we
212 are not evaluating the watchpoint expression correctly. */
213 struct1.val = 5;
214 marker5 ();
215
216 /* We're going to watch locals of func2, to see that out-of-scope
217 watchpoints are detected and properly deleted.
218 */
219 marker6 ();
220
221 /* This invocation is used for watches of a single
222 local variable. */
223 func2 ();
224
225 /* This invocation is used for watches of an expression
226 involving a local variable. */
227 func2 ();
228
229 /* This invocation is used for watches of a static
230 (non-stack-based) local variable. */
231 func2 ();
232
233 /* This invocation is used for watches of a local variable
234 when recursion happens.
235 */
236 marker6 ();
237 recurser (2);
238
239 marker6 ();
240
241 func3 ();
242
243 func4 ();
244
245 func5 ();
246
247 func6 ();
248
249 func7 ();
250
251 return 0;
252 }
This page took 0.034476 seconds and 4 git commands to generate.