OSDN Git Service

88c110f273e2ed6d55259e7e9703b9cb6c8fe4ff
[pf3gnuchains/pf3gnuchains4x.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;
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   struct1.val = 1;
171   struct2.val = 2;
172   ptr1 = &struct1;
173   ptr2 = &struct2;
174   marker1 ();
175   func1 ();
176   for (count = 0; count < 4; count++) {
177     ival1 = count;
178     ival3 = count; ival4 = count;
179   }
180   ival1 = count; /* Outside loop */
181   ival2 = count;
182   ival3 = count; ival4 = count;
183   marker2 ();
184   if (doread)
185     {
186       static char msg[] = "type stuff for buf now:";
187       write (1, msg, sizeof (msg) - 1);
188       read (0, &buf[0], 5);
189     }
190   marker4 ();
191
192   /* We have a watchpoint on ptr1->val.  It should be triggered if
193      ptr1's value changes.  */
194   ptr1 = ptr2;
195
196   /* This should not trigger the watchpoint.  If it does, then we
197      used the wrong value chain to re-insert the watchpoints or we
198      are not evaluating the watchpoint expression correctly.  */
199   struct1.val = 5;
200   marker5 ();
201
202   /* We have a watchpoint on ptr1->val.  It should be triggered if
203      ptr1's value changes.  */
204   ptr1 = ptr2;
205
206   /* This should not trigger the watchpoint.  If it does, then we
207      used the wrong value chain to re-insert the watchpoints or we
208      are not evaluating the watchpoint expression correctly.  */
209   struct1.val = 5;
210   marker5 ();
211
212   /* We're going to watch locals of func2, to see that out-of-scope
213      watchpoints are detected and properly deleted.
214      */
215   marker6 ();
216
217   /* This invocation is used for watches of a single
218      local variable. */
219   func2 ();
220
221   /* This invocation is used for watches of an expression
222      involving a local variable. */
223   func2 ();
224
225   /* This invocation is used for watches of a static
226      (non-stack-based) local variable. */
227   func2 ();
228
229   /* This invocation is used for watches of a local variable
230      when recursion happens.
231      */
232   marker6 ();
233   recurser (2);
234
235   /* This invocation is used for watches of a local variable with explicitly
236      specified scope when recursion happens.
237      */
238   marker6 ();
239   recurser (2);
240
241   marker6 ();
242
243   func3 ();
244
245   func4 ();
246
247   func5 ();
248
249   func6 ();
250
251   func7 ();
252
253   return 0;
254 }