xref: /openbsd-src/gnu/usr.bin/binutils/gdb/testsuite/gdb.base/watchpoint.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 #include <stdio.h>
2 /*
3  *	Since using watchpoints can be very slow, we have to take some pains to
4  *	ensure that we don't run too long with them enabled or we run the risk
5  *	of having the test timeout.  To help avoid this, we insert some marker
6  *	functions in the execution stream so we can set breakpoints at known
7  *	locations, without worrying about invalidating line numbers by changing
8  *	this file.  We use null bodied functions are markers since gdb does
9  *	not support breakpoints at labeled text points at this time.
10  *
11  *	One place we need is a marker for when we start executing our tests
12  *	instructions rather than any process startup code, so we insert one
13  *	right after entering main().  Another is right before we finish, before
14  *	we start executing any process termination code.
15  *
16  *	Another problem we have to guard against, at least for the test
17  *	suite, is that we need to ensure that the line that causes the
18  *	watchpoint to be hit is still the current line when gdb notices
19  *	the hit.  Depending upon the specific code generated by the compiler,
20  *	the instruction after the one that triggers the hit may be part of
21  *	the same line or part of the next line.  Thus we ensure that there
22  *	are always some instructions to execute on the same line after the
23  *	code that should trigger the hit.
24  */
25 
26 int count = -1;
27 int ival1 = -1;
28 int ival2 = -1;
29 int ival3 = -1;
30 int ival4 = -1;
31 char buf[10];
32 struct foo
33 {
34   int val;
35 };
36 struct foo struct1, struct2, *ptr1, *ptr2;
37 
38 int doread = 0;
39 
40 void marker1 ()
41 {
42 }
43 
44 void marker2 ()
45 {
46 }
47 
48 void marker4 ()
49 {
50 }
51 
52 void marker5 ()
53 {
54 }
55 
56 void
57 func2 ()
58 {
59 }
60 
61 int
62 func1 ()
63 {
64   /* The point of this is that we will set a breakpoint at this call.
65 
66      Then, if DECR_PC_AFTER_BREAK equals the size of a function call
67      instruction (true on a sun3 if this is gcc-compiled--FIXME we
68      should use asm() to make it work for any compiler, present or
69      future), then we will end up branching to the location just after
70      the breakpoint.  And we better not confuse that with hitting the
71      breakpoint.  */
72   func2 ();
73   return 73;
74 }
75 
76 int main ()
77 {
78   struct1.val = 1;
79   struct2.val = 2;
80   ptr1 = &struct1;
81   ptr2 = &struct2;
82   marker1 ();
83   func1 ();
84   for (count = 0; count < 4; count++) {
85     ival1 = count;
86     ival3 = count; ival4 = count;
87   }
88   ival1 = count; /* Outside loop */
89   ival2 = count;
90   ival3 = count; ival4 = count;
91   marker2 ();
92   if (doread)
93     {
94       static char msg[] = "type stuff for buf now:";
95       write (1, msg, sizeof (msg) - 1);
96       read (0, &buf[0], 5);
97     }
98   marker4 ();
99 
100   /* We have a watchpoint on ptr1->val.  It should be triggered if
101      ptr1's value changes.  */
102   ptr1 = ptr2;
103 
104   /* This should not trigger the watchpoint.  If it does, then we
105      used the wrong value chain to re-insert the watchpoints or we
106      are not evaluating the watchpoint expression correctly.  */
107   struct1.val = 5;
108   marker5 ();
109 
110   /* We have a watchpoint on ptr1->val.  It should be triggered if
111      ptr1's value changes.  */
112   ptr1 = ptr2;
113 
114   /* This should not trigger the watchpoint.  If it does, then we
115      used the wrong value chain to re-insert the watchpoints or we
116      are not evaluating the watchpoint expression correctly.  */
117   struct1.val = 5;
118   marker5 ();
119   return 0;
120 }
121