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