gdb/doc/
[external/binutils.git] / gdb / testsuite / gdb.reverse / watch-reverse.c
1 /* This testcase is part of GDB, the GNU debugger.
2
3    Copyright 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #include <stdio.h>
19 #include <unistd.h>
20 /*
21  *      Since using watchpoints can be very slow, we have to take some pains to
22  *      ensure that we don't run too long with them enabled or we run the risk
23  *      of having the test timeout.  To help avoid this, we insert some marker
24  *      functions in the execution stream so we can set breakpoints at known
25  *      locations, without worrying about invalidating line numbers by changing
26  *      this file.  We use null bodied functions are markers since gdb does
27  *      not support breakpoints at labeled text points at this time.
28  *
29  *      One place we need is a marker for when we start executing our tests
30  *      instructions rather than any process startup code, so we insert one
31  *      right after entering main().  Another is right before we finish, before
32  *      we start executing any process termination code.
33  *
34  *      Another problem we have to guard against, at least for the test
35  *      suite, is that we need to ensure that the line that causes the
36  *      watchpoint to be hit is still the current line when gdb notices
37  *      the hit.  Depending upon the specific code generated by the compiler,
38  *      the instruction after the one that triggers the hit may be part of
39  *      the same line or part of the next line.  Thus we ensure that there
40  *      are always some instructions to execute on the same line after the
41  *      code that should trigger the hit.
42  */
43
44 int count = -1;
45 int ival1 = -1;
46 int ival2 = -1;
47 int ival3 = -1;
48 int ival4 = -1;
49 int ival5 = -1;
50 char buf[10];
51 struct foo
52 {
53   int val;
54 };
55 struct foo struct1, struct2, *ptr1, *ptr2;
56
57 int doread = 0;
58
59 char *global_ptr;
60
61 void marker1 ()
62 {
63 }
64
65 void marker2 ()
66 {
67 }
68
69 void marker4 ()
70 {
71 }
72
73 void marker5 ()
74 {
75 }
76
77 void marker6 ()
78 {
79 }
80
81 #ifdef PROTOTYPES
82 void recurser (int  x)
83 #else
84 void recurser (x) int  x;
85 #endif
86 {
87   int  local_x;
88
89   if (x > 0)
90     recurser (x-1);
91   local_x = x;
92 }
93
94 void
95 func2 ()
96 {
97   int  local_a;
98   static int  static_b;
99
100   ival5++;
101   local_a = ival5;
102   static_b = local_a;
103 }
104
105 void
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 }
116
117 int
118 func1 ()
119 {
120   /* The point of this is that we will set a breakpoint at this call.
121
122      Then, if DECR_PC_AFTER_BREAK equals the size of a function call
123      instruction (true on a sun3 if this is gcc-compiled--FIXME we
124      should use asm() to make it work for any compiler, present or
125      future), then we will end up branching to the location just after
126      the breakpoint.  And we better not confuse that with hitting the
127      breakpoint.  */
128   func2 ();
129   return 73;
130 }
131
132 void
133 func4 ()
134 {
135   buf[0] = 3;
136   global_ptr = buf;
137   buf[0] = 7;
138 }
139
140 int main ()
141 {
142   struct1.val = 1;
143   struct2.val = 2;
144   ptr1 = &struct1;
145   ptr2 = &struct2;
146   marker1 ();
147   func1 ();
148   for (count = 0; count < 4; count++) {
149     ival1 = count;
150     ival3 = count; ival4 = count;
151   }
152   ival1 = count; /* Outside loop */
153   ival2 = count;
154   ival3 = count; ival4 = count;
155   marker2 ();
156   if (doread)
157     {
158       static char msg[] = "type stuff for buf now:";
159       write (1, msg, sizeof (msg) - 1);
160       read (0, &buf[0], 5);
161     }
162   marker4 ();
163
164   /* We have a watchpoint on ptr1->val.  It should be triggered if
165      ptr1's value changes.  */
166   ptr1 = ptr2;
167
168   /* This should not trigger the watchpoint.  If it does, then we
169      used the wrong value chain to re-insert the watchpoints or we
170      are not evaluating the watchpoint expression correctly.  */
171   struct1.val = 5;
172   marker5 ();
173
174   /* We have a watchpoint on ptr1->val.  It should be triggered if
175      ptr1's value changes.  */
176   ptr1 = ptr2;
177
178   /* This should not trigger the watchpoint.  If it does, then we
179      used the wrong value chain to re-insert the watchpoints or we
180      are not evaluating the watchpoint expression correctly.  */
181   struct1.val = 5;
182   marker5 ();
183
184   /* We're going to watch locals of func2, to see that out-of-scope
185      watchpoints are detected and properly deleted.
186      */
187   marker6 ();
188
189   /* This invocation is used for watches of a single
190      local variable. */
191   func2 ();
192
193   /* This invocation is used for watches of an expression
194      involving a local variable. */
195   func2 ();
196
197   /* This invocation is used for watches of a static
198      (non-stack-based) local variable. */
199   func2 ();
200
201   /* This invocation is used for watches of a local variable
202      when recursion happens.
203      */
204   marker6 ();
205   recurser (2);
206
207   marker6 ();
208
209   func3 ();
210
211   func4 ();
212
213   return 0;
214 } /* end of main */
215