xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.base/stap-probe.c (revision 22ebeae4b2252475e0ebe332f69734639cb946ea)
1 /* This testcase is part of GDB, the GNU debugger.
2 
3    Copyright 2012-2023 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 "../lib/attributes.h"
19 
20 #if USE_SEMAPHORES
21 
22 #define _SDT_HAS_SEMAPHORES
23 __extension__ unsigned short test_user_semaphore __attribute__ ((unused)) __attribute__ ((section (".probes")));
24 #define TEST test_user_semaphore
25 
26 __extension__ unsigned short test_two_semaphore __attribute__ ((unused)) __attribute__ ((section (".probes")));
27 #define TEST2 test_two_semaphore
28 
29 __extension__ unsigned short test_m4_semaphore __attribute__ ((unused)) __attribute__ ((section (".probes")));
30 
31 __extension__ unsigned short test_pstr_semaphore __attribute__ ((unused)) __attribute__ ((section (".probes")));
32 
33 __extension__ unsigned short test_ps_semaphore __attribute__ ((unused)) __attribute__ ((section (".probes")));
34 
35 __extension__ unsigned short test_xmmreg_semaphore __attribute__ ((unused)) __attribute__ ((section (".probes")));
36 #else
37 
38 int relocation_marker __attribute__ ((unused));
39 
40 #define TEST 1
41 #define TEST2 1
42 
43 #endif
44 
45 #include <sys/sdt.h>
46 
47 /* We only support SystemTap and only the v3 form.  */
48 #if _SDT_NOTE_TYPE != 3
49 #error "not using SystemTap v3 probes"
50 #endif
51 
52 struct funcs
53 {
54   int val;
55 
56   const char *(*ps) (int);
57 };
58 
59 static void
60 m1 (void)
61 {
62   /* m1 and m2 are equivalent, but because of some compiler
63      optimizations we have to make each of them unique.  This is why
64      we have this dummy variable here.  */
65   volatile int dummy = 0;
66 
67   if (TEST2)
68     STAP_PROBE1 (test, two, dummy);
69 }
70 
71 static void
72 m2 (void)
73 {
74   if (TEST2)
75     STAP_PROBE (test, two);
76 }
77 
78 static int
79 f (int x)
80 {
81   if (TEST)
82     STAP_PROBE1 (test, user, x);
83   return x+5;
84 }
85 
86 static const char *
87 pstr (int val)
88 {
89   const char *a = "This is a test message.";
90   const char *b = "This is another test message.";
91 
92   STAP_PROBE3 (test, ps, a, b, val);
93 
94   return val == 0 ? a : b;
95 }
96 
97 #ifdef __SSE2__
98 static const char * __attribute__((noinline))
99 use_xmm_reg (int val)
100 {
101   volatile register int val_in_reg asm ("xmm0") = val;
102 
103   STAP_PROBE1 (test, xmmreg, val_in_reg);
104 
105   return val == 0 ? "xxx" : "yyy";
106 }
107 #else
108 static const char * __attribute__((noinline)) ATTRIBUTE_NOCLONE
109 use_xmm_reg (int val)
110 {
111   /* Nothing.  */
112 }
113 #endif /* __SSE2__ */
114 
115 static void
116 m4 (const struct funcs *fs, int v)
117 {
118   STAP_PROBE3 (test, m4, fs->val, fs->ps (v), v);
119 }
120 
121 int
122 main()
123 {
124   struct funcs fs;
125 
126   fs.val = 42;
127   fs.ps = pstr;
128 
129   f (f (23));
130   m1 ();
131   m2 ();
132 
133   m4 (&fs, 0);
134   m4 (&fs, 1);
135 
136   use_xmm_reg (0x1234);
137 
138   return 0; /* last break here */
139 }
140