gdb
[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
44 void marker1 ()
45 {
46 }
47
48 void marker2 ()
49 {
50 }
51
52 void marker4 ()
53 {
54 }
55
56 void marker5 ()
57 {
58 }
59
60 void marker6 ()
61 {
62 }
63
64 #ifdef PROTOTYPES
65 void recurser (int x)
66 #else
67 void recurser (x) int x;
68 #endif
69 {
70 int local_x;
71
72 if (x > 0)
73 recurser (x-1);
74 local_x = x;
75 }
76
77 void
78 func2 ()
79 {
80 int local_a;
81 static int static_b;
82
83 ival5++;
84 local_a = ival5;
85 static_b = local_a;
86 }
87
88 void
89 func3 ()
90 {
91 int x;
92 int y;
93
94 x = 0;
95 x = 1; /* second x assignment */
96 y = 1;
97 y = 2;
98 buf[26] = 3;
99 }
100
101 int
102 func1 ()
103 {
104 /* The point of this is that we will set a breakpoint at this call.
105
106 Then, if DECR_PC_AFTER_BREAK equals the size of a function call
107 instruction (true on a sun3 if this is gcc-compiled--FIXME we
108 should use asm() to make it work for any compiler, present or
109 future), then we will end up branching to the location just after
110 the breakpoint. And we better not confuse that with hitting the
111 breakpoint. */
112 func2 ();
113 return 73;
114 }
115
116 void
117 func4 ()
118 {
119 buf[0] = 3;
120 global_ptr = buf;
121 buf[0] = 7;
122 }
123
124 int main ()
125 {
126 #ifdef usestubs
127 set_debug_traps();
128 breakpoint();
129 #endif
130 struct1.val = 1;
131 struct2.val = 2;
132 ptr1 = &struct1;
133 ptr2 = &struct2;
134 marker1 ();
135 func1 ();
136 for (count = 0; count < 4; count++) {
137 ival1 = count;
138 ival3 = count; ival4 = count;
139 }
140 ival1 = count; /* Outside loop */
141 ival2 = count;
142 ival3 = count; ival4 = count;
143 marker2 ();
144 if (doread)
145 {
146 static char msg[] = "type stuff for buf now:";
147 write (1, msg, sizeof (msg) - 1);
148 read (0, &buf[0], 5);
149 }
150 marker4 ();
151
152 /* We have a watchpoint on ptr1->val. It should be triggered if
153 ptr1's value changes. */
154 ptr1 = ptr2;
155
156 /* This should not trigger the watchpoint. If it does, then we
157 used the wrong value chain to re-insert the watchpoints or we
158 are not evaluating the watchpoint expression correctly. */
159 struct1.val = 5;
160 marker5 ();
161
162 /* We have a watchpoint on ptr1->val. It should be triggered if
163 ptr1's value changes. */
164 ptr1 = ptr2;
165
166 /* This should not trigger the watchpoint. If it does, then we
167 used the wrong value chain to re-insert the watchpoints or we
168 are not evaluating the watchpoint expression correctly. */
169 struct1.val = 5;
170 marker5 ();
171
172 /* We're going to watch locals of func2, to see that out-of-scope
173 watchpoints are detected and properly deleted.
174 */
175 marker6 ();
176
177 /* This invocation is used for watches of a single
178 local variable. */
179 func2 ();
180
181 /* This invocation is used for watches of an expression
182 involving a local variable. */
183 func2 ();
184
185 /* This invocation is used for watches of a static
186 (non-stack-based) local variable. */
187 func2 ();
188
189 /* This invocation is used for watches of a local variable
190 when recursion happens.
191 */
192 marker6 ();
193 recurser (2);
194
195 marker6 ();
196
197 func3 ();
198
199 func4 ();
200
201 return 0;
202 }
This page took 0.03461 seconds and 4 git commands to generate.