* gdb.base/call-signal-resume.exp, gdb.base/unwindonsignal.exp: Skip
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / watchpoint.c
CommitLineData
c906108c 1#include <stdio.h>
085dd6e6 2#include <unistd.h>
c906108c
SS
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
27int count = -1;
28int ival1 = -1;
29int ival2 = -1;
30int ival3 = -1;
31int ival4 = -1;
085dd6e6 32int ival5 = -1;
218d2fc6 33char buf[30] = "testtesttesttesttesttesttestte";
c906108c
SS
34struct foo
35{
36 int val;
37};
38struct foo struct1, struct2, *ptr1, *ptr2;
39
40int doread = 0;
41
fa4727a6
DJ
42char *global_ptr;
43
c906108c
SS
44void marker1 ()
45{
46}
47
48void marker2 ()
49{
50}
51
52void marker4 ()
53{
54}
55
56void marker5 ()
57{
58}
59
085dd6e6
JM
60void marker6 ()
61{
62}
63
64#ifdef PROTOTYPES
65void recurser (int x)
66#else
67void 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
c906108c
SS
77void
78func2 ()
79{
085dd6e6
JM
80 int local_a;
81 static int static_b;
82
83 ival5++;
84 local_a = ival5;
85 static_b = local_a;
c906108c
SS
86}
87
293e9a31
DC
88void
89func3 ()
90{
91 int x;
92 int y;
93
94 x = 0;
95 x = 1; /* second x assignment */
96 y = 1;
97 y = 2;
218d2fc6 98 buf[26] = 3;
293e9a31
DC
99}
100
c906108c
SS
101int
102func1 ()
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
fa4727a6
DJ
116void
117func4 ()
118{
119 buf[0] = 3;
120 global_ptr = buf;
121 buf[0] = 7;
122}
123
c906108c
SS
124int 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 ();
085dd6e6
JM
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 ();
293e9a31
DC
196
197 func3 ();
198
fa4727a6
DJ
199 func4 ();
200
c906108c
SS
201 return 0;
202}
This page took 0.928127 seconds and 4 git commands to generate.