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