xref: /netbsd-src/external/gpl3/gdb/dist/gdb/testsuite/gdb.base/watchpoint.c (revision 4b169a6ba595ae283ca507b26b15fdff40495b1c)
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[31] = "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 struct foo5
58 {
59   struct { int x; } *p;
60 };
61 
62 struct foo5 *null_ptr;
63 
marker1()64 void marker1 ()
65 {
66 }
67 
marker2()68 void marker2 ()
69 {
70 }
71 
marker4()72 void marker4 ()
73 {
74 }
75 
marker5()76 void marker5 ()
77 {
78 }
79 
marker6()80 void marker6 ()
81 {
82 }
83 
recurser(int x)84 void recurser (int  x)
85 {
86   int  local_x = 0;
87 
88   if (x > 0)
89     recurser (x-1);
90   local_x = x;
91 }
92 
93 void
func2()94 func2 ()
95 {
96   int  local_a = 0;
97   static int  static_b;
98 
99   /* func2 breakpoint here */
100   ival5++;
101   local_a = ival5;
102   static_b = local_a;
103 }
104 
105 void
func3()106 func3 ()
107 {
108   int x;
109   int y;
110 
111   x = 0;
112   x = 1;				/* second x assignment */
113   y = 1;
114   y = 2;
115   buf[26] = 3;
116 }
117 
118 int
func1()119 func1 ()
120 {
121   /* The point of this is that we will set a breakpoint at this call.
122 
123      Then, if DECR_PC_AFTER_BREAK equals the size of a function call
124      instruction (true on a sun3 if this is gcc-compiled--FIXME we
125      should use asm() to make it work for any compiler, present or
126      future), then we will end up branching to the location just after
127      the breakpoint.  And we better not confuse that with hitting the
128      breakpoint.  */
129   func2 ();
130   return 73;
131 }
132 
133 void
func4()134 func4 ()
135 {
136   buf[0] = 3;
137   global_ptr = buf;
138   buf[0] = 7;
139   buf[1] = 5;
140   global_ptr_ptr = &global_ptr;
141   buf[0] = 9;
142   global_ptr++;
143 }
144 
145 void
func5()146 func5 ()
147 {
148   int val = 0, val2 = 23;
149   int *x = &val;
150 
151   /* func5 breakpoint here */
152   x = &val2;
153   val = 27;
154 }
155 
156 void
func6(void)157 func6 (void)
158 {
159   /* func6 breakpoint here */
160   foo2.val[1] = 0;
161   foo2.val[1] = 11;
162 }
163 
164 void
func7(void)165 func7 (void)
166 {
167   /* func7 breakpoint here */
168   foo4.val[3] = 0;
169   foo4.val[3] = 33;
170 }
171 
main()172 int main ()
173 {
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   /* This invocation is used for watches of a local variable with explicitly
240      specified scope when recursion happens.
241      */
242   marker6 ();
243   recurser (2);
244 
245   marker6 ();
246 
247   func3 ();
248 
249   func4 ();
250 
251   func5 ();
252 
253   func6 ();
254 
255   func7 ();
256 
257   return 0;
258 }
259