fix: namespacing of 'tp_rcu_read_lock'
[lttng-ust.git] / tests / compile / test-app-ctx / hello.c
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2009 Pierre-Marc Fournier
5 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 */
7
8 #include <fcntl.h>
9 #include <signal.h>
10 #include <stdarg.h>
11 #include <stdint.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <sys/mman.h>
15 #include <sys/stat.h>
16 #include <sys/types.h>
17 #include <unistd.h>
18 /*
19 * Work-around inet.h missing struct mmsghdr forward declaration, with
20 * triggers a warning when system files warnings are enabled.
21 */
22 struct mmsghdr;
23 #include <arpa/inet.h>
24 #include <stddef.h>
25 #include <stdlib.h>
26 #include <stdbool.h>
27
28 #define LTTNG_UST_TRACEPOINT_DEFINE
29 #include "ust_tests_hello.h"
30
31 #include <lttng/ust-events.h>
32 #include <lttng/ust-ringbuffer-context.h>
33 /* Internal header. */
34 #include <common/ust-context-provider.h>
35
36 static __thread unsigned int test_count;
37
38 static
39 void test_inc_count(void)
40 {
41 test_count++;
42 }
43
44 static
45 size_t test_get_size(void *priv __attribute__((unused)),
46 struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)),
47 size_t offset)
48 {
49 int sel = test_count % _NR_LTTNG_UST_DYNAMIC_TYPES;
50 size_t size = 0;
51
52 size += lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(char));
53 size += sizeof(char); /* tag */
54 switch (sel) {
55 case LTTNG_UST_DYNAMIC_TYPE_NONE:
56 break;
57 case LTTNG_UST_DYNAMIC_TYPE_S8:
58 size += lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(int8_t));
59 size += sizeof(int8_t); /* variant */
60 break;
61 case LTTNG_UST_DYNAMIC_TYPE_S16:
62 size += lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(int16_t));
63 size += sizeof(int16_t); /* variant */
64 break;
65 case LTTNG_UST_DYNAMIC_TYPE_S32:
66 size += lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(int32_t));
67 size += sizeof(int32_t); /* variant */
68 break;
69 case LTTNG_UST_DYNAMIC_TYPE_S64:
70 size += lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(int64_t));
71 size += sizeof(int64_t); /* variant */
72 break;
73 case LTTNG_UST_DYNAMIC_TYPE_U8:
74 size += lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(uint8_t));
75 size += sizeof(uint8_t); /* variant */
76 break;
77 case LTTNG_UST_DYNAMIC_TYPE_U16:
78 size += lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(uint16_t));
79 size += sizeof(uint16_t); /* variant */
80 break;
81 case LTTNG_UST_DYNAMIC_TYPE_U32:
82 size += lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(uint32_t));
83 size += sizeof(uint32_t); /* variant */
84 break;
85 case LTTNG_UST_DYNAMIC_TYPE_U64:
86 size += lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(uint64_t));
87 size += sizeof(uint64_t); /* variant */
88 break;
89 case LTTNG_UST_DYNAMIC_TYPE_FLOAT:
90 size += lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(float));
91 size += sizeof(float); /* variant */
92 break;
93 case LTTNG_UST_DYNAMIC_TYPE_DOUBLE:
94 size += lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(double));
95 size += sizeof(double); /* variant */
96 break;
97 case LTTNG_UST_DYNAMIC_TYPE_STRING:
98 size += strlen("teststr") + 1;
99 break;
100 default:
101 abort();
102 }
103
104 return size;
105 }
106
107 static
108 void test_record(void *priv __attribute__((unused)),
109 struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)),
110 struct lttng_ust_ring_buffer_ctx *ctx,
111 struct lttng_ust_channel_buffer *lttng_chan_buf)
112 {
113 int sel = test_count % _NR_LTTNG_UST_DYNAMIC_TYPES;
114 char sel_char = (char) sel;
115
116 lttng_chan_buf->ops->event_write(ctx, &sel_char, sizeof(sel_char), lttng_ust_rb_alignof(char));
117 switch (sel) {
118 case LTTNG_UST_DYNAMIC_TYPE_NONE:
119 break;
120 case LTTNG_UST_DYNAMIC_TYPE_S8:
121 {
122 int8_t v = -8;
123
124 lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v));
125 break;
126 }
127 case LTTNG_UST_DYNAMIC_TYPE_S16:
128 {
129 int16_t v = -16;
130
131 lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v));
132 break;
133 }
134 case LTTNG_UST_DYNAMIC_TYPE_S32:
135 {
136 int32_t v = -32;
137
138 lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v));
139 break;
140 }
141 case LTTNG_UST_DYNAMIC_TYPE_S64:
142 {
143 int64_t v = -64;
144
145 lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v));
146 break;
147 }
148 case LTTNG_UST_DYNAMIC_TYPE_U8:
149 {
150 uint8_t v = 8;
151
152 lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v));
153 break;
154 }
155 case LTTNG_UST_DYNAMIC_TYPE_U16:
156 {
157 uint16_t v = 16;
158
159 lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v));
160 break;
161 }
162 case LTTNG_UST_DYNAMIC_TYPE_U32:
163 {
164 uint32_t v = 32;
165
166 lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v));
167 break;
168 }
169 case LTTNG_UST_DYNAMIC_TYPE_U64:
170 {
171 uint64_t v = 64;
172
173 lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v));
174 break;
175 }
176 case LTTNG_UST_DYNAMIC_TYPE_FLOAT:
177 {
178 float f = 22322.0;
179
180 lttng_chan_buf->ops->event_write(ctx, &f, sizeof(f), lttng_ust_rb_alignof(f));
181 break;
182 }
183 case LTTNG_UST_DYNAMIC_TYPE_DOUBLE:
184 {
185 double d = 2.0;
186
187 lttng_chan_buf->ops->event_write(ctx, &d, sizeof(d), lttng_ust_rb_alignof(d));
188 break;
189 }
190 case LTTNG_UST_DYNAMIC_TYPE_STRING:
191 {
192 const char *str = "teststr";
193 lttng_chan_buf->ops->event_write(ctx, str, strlen(str) + 1, 1);
194 break;
195 }
196 default:
197 abort();
198 }
199 }
200
201 static
202 void test_get_value(void *priv __attribute__((unused)),
203 struct lttng_ust_probe_ctx *probe_ctx __attribute__((unused)),
204 struct lttng_ust_ctx_value *value)
205 {
206 int sel = test_count % _NR_LTTNG_UST_DYNAMIC_TYPES;
207
208 value->sel = sel;
209 switch (sel) {
210 case LTTNG_UST_DYNAMIC_TYPE_NONE:
211 break;
212 case LTTNG_UST_DYNAMIC_TYPE_S8:
213 value->u.s64 = -8;
214 break;
215 case LTTNG_UST_DYNAMIC_TYPE_S16:
216 value->u.s64 = -16;
217 break;
218 case LTTNG_UST_DYNAMIC_TYPE_S32:
219 value->u.s64 = -32;
220 break;
221 case LTTNG_UST_DYNAMIC_TYPE_S64:
222 value->u.s64 = -64;
223 break;
224 case LTTNG_UST_DYNAMIC_TYPE_U8:
225 value->u.u64 = 8;
226 break;
227 case LTTNG_UST_DYNAMIC_TYPE_U16:
228 value->u.u64 = 16;
229 break;
230 case LTTNG_UST_DYNAMIC_TYPE_U32:
231 value->u.u64 = 32;
232 break;
233 case LTTNG_UST_DYNAMIC_TYPE_U64:
234 value->u.u64 = 64;
235 break;
236 case LTTNG_UST_DYNAMIC_TYPE_FLOAT:
237 value->u.d = 22322.0;
238 break;
239 case LTTNG_UST_DYNAMIC_TYPE_DOUBLE:
240 value->u.d = 2.0;
241 break;
242 case LTTNG_UST_DYNAMIC_TYPE_STRING:
243 value->u.str = "teststr";
244 break;
245 default:
246 abort();
247 }
248 }
249
250 static char myprovider_name[] = "$app.myprovider";
251 struct lttng_ust_context_provider myprovider = {
252 .struct_size = sizeof(struct lttng_ust_context_provider),
253 .name = myprovider_name,
254 .get_size = test_get_size,
255 .record = test_record,
256 .get_value = test_get_value,
257 };
258
259 static
260 void inthandler(int sig __attribute__((unused)))
261 {
262 printf("in SIGUSR1 handler\n");
263 lttng_ust_tracepoint(ust_tests_hello, tptest_sighandler);
264 }
265
266 static
267 int init_int_handler(void)
268 {
269 int result;
270 struct sigaction act;
271
272 memset(&act, 0, sizeof(act));
273 result = sigemptyset(&act.sa_mask);
274 if (result == -1) {
275 perror("sigemptyset");
276 return -1;
277 }
278
279 act.sa_handler = inthandler;
280 act.sa_flags = SA_RESTART;
281
282 /* Only defer ourselves. Also, try to restart interrupted
283 * syscalls to disturb the traced program as little as possible.
284 */
285 result = sigaction(SIGUSR1, &act, NULL);
286 if (result == -1) {
287 perror("sigaction");
288 return -1;
289 }
290
291 return 0;
292 }
293
294 int main(int argc, char **argv)
295 {
296 struct lttng_ust_registered_context_provider *reg_provider;
297 int i, netint;
298 long values[] = { 1, 2, 3 };
299 char text[10] = "test";
300 double dbl = 2.0;
301 float flt = 2222.0;
302 int delay = 0;
303 bool mybool = 123; /* should print "1" */
304
305 init_int_handler();
306
307 if (argc == 2)
308 delay = atoi(argv[1]);
309
310 reg_provider = lttng_ust_context_provider_register(&myprovider);
311 if (!reg_provider)
312 abort();
313
314 fprintf(stderr, "Hello, World!\n");
315
316 sleep(delay);
317
318 fprintf(stderr, "Tracing... ");
319 for (i = 0; i < 1000000; i++) {
320 netint = htonl(i);
321 lttng_ust_tracepoint(ust_tests_hello, tptest, i, netint, values,
322 text, strlen(text), dbl, flt, mybool);
323 test_inc_count();
324 //usleep(100000);
325 }
326 lttng_ust_context_provider_unregister(reg_provider);
327 fprintf(stderr, " done.\n");
328 return 0;
329 }
This page took 0.035848 seconds and 4 git commands to generate.