Move to kernel style SPDX license identifiers
[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 TRACEPOINT_DEFINE
29 #include "ust_tests_hello.h"
30
31 /* Internal header. */
32 #include <lttng/ust-events.h>
33 #include <lttng/ringbuffer-config.h>
34 #include <lttng/ust-context-provider.h>
35
36 static __thread unsigned int test_count;
37
38 void test_inc_count(void)
39 {
40 test_count++;
41 }
42
43 static
44 size_t test_get_size(struct lttng_ctx_field *field, size_t offset)
45 {
46 int sel = test_count % _NR_LTTNG_UST_DYNAMIC_TYPES;
47 size_t size = 0;
48
49 size += lib_ring_buffer_align(offset, lttng_alignof(char));
50 size += sizeof(char); /* tag */
51 switch (sel) {
52 case LTTNG_UST_DYNAMIC_TYPE_NONE:
53 break;
54 case LTTNG_UST_DYNAMIC_TYPE_S8:
55 size += lib_ring_buffer_align(offset, lttng_alignof(int8_t));
56 size += sizeof(int8_t); /* variant */
57 break;
58 case LTTNG_UST_DYNAMIC_TYPE_S16:
59 size += lib_ring_buffer_align(offset, lttng_alignof(int16_t));
60 size += sizeof(int16_t); /* variant */
61 break;
62 case LTTNG_UST_DYNAMIC_TYPE_S32:
63 size += lib_ring_buffer_align(offset, lttng_alignof(int32_t));
64 size += sizeof(int32_t); /* variant */
65 break;
66 case LTTNG_UST_DYNAMIC_TYPE_S64:
67 size += lib_ring_buffer_align(offset, lttng_alignof(int64_t));
68 size += sizeof(int64_t); /* variant */
69 break;
70 case LTTNG_UST_DYNAMIC_TYPE_U8:
71 size += lib_ring_buffer_align(offset, lttng_alignof(uint8_t));
72 size += sizeof(uint8_t); /* variant */
73 break;
74 case LTTNG_UST_DYNAMIC_TYPE_U16:
75 size += lib_ring_buffer_align(offset, lttng_alignof(uint16_t));
76 size += sizeof(uint16_t); /* variant */
77 break;
78 case LTTNG_UST_DYNAMIC_TYPE_U32:
79 size += lib_ring_buffer_align(offset, lttng_alignof(uint32_t));
80 size += sizeof(uint32_t); /* variant */
81 break;
82 case LTTNG_UST_DYNAMIC_TYPE_U64:
83 size += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
84 size += sizeof(uint64_t); /* variant */
85 break;
86 case LTTNG_UST_DYNAMIC_TYPE_FLOAT:
87 size += lib_ring_buffer_align(offset, lttng_alignof(float));
88 size += sizeof(float); /* variant */
89 break;
90 case LTTNG_UST_DYNAMIC_TYPE_DOUBLE:
91 size += lib_ring_buffer_align(offset, lttng_alignof(double));
92 size += sizeof(double); /* variant */
93 break;
94 case LTTNG_UST_DYNAMIC_TYPE_STRING:
95 size += strlen("teststr") + 1;
96 break;
97 default:
98 abort();
99 }
100
101 return size;
102 }
103
104 static
105 void test_record(struct lttng_ctx_field *field,
106 struct lttng_ust_lib_ring_buffer_ctx *ctx,
107 struct lttng_channel *chan)
108 {
109 int sel = test_count % _NR_LTTNG_UST_DYNAMIC_TYPES;
110 char sel_char = (char) sel;
111
112 lib_ring_buffer_align_ctx(ctx, lttng_alignof(char));
113 chan->ops->event_write(ctx, &sel_char, sizeof(sel_char));
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
121 lib_ring_buffer_align_ctx(ctx, lttng_alignof(v));
122 chan->ops->event_write(ctx, &v, sizeof(v));
123 break;
124 }
125 case LTTNG_UST_DYNAMIC_TYPE_S16:
126 {
127 int16_t v = -16;
128
129 lib_ring_buffer_align_ctx(ctx, lttng_alignof(v));
130 chan->ops->event_write(ctx, &v, sizeof(v));
131 break;
132 }
133 case LTTNG_UST_DYNAMIC_TYPE_S32:
134 {
135 int32_t v = -32;
136
137 lib_ring_buffer_align_ctx(ctx, lttng_alignof(v));
138 chan->ops->event_write(ctx, &v, sizeof(v));
139 break;
140 }
141 case LTTNG_UST_DYNAMIC_TYPE_S64:
142 {
143 int64_t v = -64;
144
145 lib_ring_buffer_align_ctx(ctx, lttng_alignof(v));
146 chan->ops->event_write(ctx, &v, sizeof(v));
147 break;
148 }
149 case LTTNG_UST_DYNAMIC_TYPE_U8:
150 {
151 uint8_t v = 8;
152
153 lib_ring_buffer_align_ctx(ctx, lttng_alignof(v));
154 chan->ops->event_write(ctx, &v, sizeof(v));
155 break;
156 }
157 case LTTNG_UST_DYNAMIC_TYPE_U16:
158 {
159 uint16_t v = 16;
160
161 lib_ring_buffer_align_ctx(ctx, lttng_alignof(v));
162 chan->ops->event_write(ctx, &v, sizeof(v));
163 break;
164 }
165 case LTTNG_UST_DYNAMIC_TYPE_U32:
166 {
167 uint32_t v = 32;
168
169 lib_ring_buffer_align_ctx(ctx, lttng_alignof(v));
170 chan->ops->event_write(ctx, &v, sizeof(v));
171 break;
172 }
173 case LTTNG_UST_DYNAMIC_TYPE_U64:
174 {
175 uint64_t v = 64;
176
177 lib_ring_buffer_align_ctx(ctx, lttng_alignof(v));
178 chan->ops->event_write(ctx, &v, sizeof(v));
179 break;
180 }
181 case LTTNG_UST_DYNAMIC_TYPE_FLOAT:
182 {
183 float f = 22322.0;
184
185 lib_ring_buffer_align_ctx(ctx, lttng_alignof(f));
186 chan->ops->event_write(ctx, &f, sizeof(f));
187 break;
188 }
189 case LTTNG_UST_DYNAMIC_TYPE_DOUBLE:
190 {
191 double d = 2.0;
192
193 lib_ring_buffer_align_ctx(ctx, lttng_alignof(d));
194 chan->ops->event_write(ctx, &d, sizeof(d));
195 break;
196 }
197 case LTTNG_UST_DYNAMIC_TYPE_STRING:
198 {
199 const char *str = "teststr";
200 chan->ops->event_write(ctx, str, strlen(str) + 1);
201 break;
202 }
203 default:
204 abort();
205 }
206 }
207
208 static
209 void test_get_value(struct lttng_ctx_field *field,
210 struct lttng_ctx_value *value)
211 {
212 int sel = test_count % _NR_LTTNG_UST_DYNAMIC_TYPES;
213
214 value->sel = sel;
215 switch (sel) {
216 case LTTNG_UST_DYNAMIC_TYPE_NONE:
217 break;
218 case LTTNG_UST_DYNAMIC_TYPE_S8:
219 value->u.s64 = -8;
220 break;
221 case LTTNG_UST_DYNAMIC_TYPE_S16:
222 value->u.s64 = -16;
223 break;
224 case LTTNG_UST_DYNAMIC_TYPE_S32:
225 value->u.s64 = -32;
226 break;
227 case LTTNG_UST_DYNAMIC_TYPE_S64:
228 value->u.s64 = -64;
229 break;
230 case LTTNG_UST_DYNAMIC_TYPE_U8:
231 value->u.u64 = 8;
232 break;
233 case LTTNG_UST_DYNAMIC_TYPE_U16:
234 value->u.u64 = 16;
235 break;
236 case LTTNG_UST_DYNAMIC_TYPE_U32:
237 value->u.u64 = 32;
238 break;
239 case LTTNG_UST_DYNAMIC_TYPE_U64:
240 value->u.u64 = 64;
241 break;
242 case LTTNG_UST_DYNAMIC_TYPE_FLOAT:
243 value->u.d = 22322.0;
244 break;
245 case LTTNG_UST_DYNAMIC_TYPE_DOUBLE:
246 value->u.d = 2.0;
247 break;
248 case LTTNG_UST_DYNAMIC_TYPE_STRING:
249 value->u.str = "teststr";
250 break;
251 default:
252 abort();
253 }
254 }
255
256 struct lttng_ust_context_provider myprovider = {
257 .name = "$app.myprovider",
258 .get_size = test_get_size,
259 .record = test_record,
260 .get_value = test_get_value,
261 };
262
263 void inthandler(int sig)
264 {
265 printf("in SIGUSR1 handler\n");
266 tracepoint(ust_tests_hello, tptest_sighandler);
267 }
268
269 int init_int_handler(void)
270 {
271 int result;
272 struct sigaction act;
273
274 memset(&act, 0, sizeof(act));
275 result = sigemptyset(&act.sa_mask);
276 if (result == -1) {
277 perror("sigemptyset");
278 return -1;
279 }
280
281 act.sa_handler = inthandler;
282 act.sa_flags = SA_RESTART;
283
284 /* Only defer ourselves. Also, try to restart interrupted
285 * syscalls to disturb the traced program as little as possible.
286 */
287 result = sigaction(SIGUSR1, &act, NULL);
288 if (result == -1) {
289 perror("sigaction");
290 return -1;
291 }
292
293 return 0;
294 }
295
296 void test_inc_count(void);
297
298 int main(int argc, char **argv)
299 {
300 int i, netint;
301 long values[] = { 1, 2, 3 };
302 char text[10] = "test";
303 double dbl = 2.0;
304 float flt = 2222.0;
305 int delay = 0;
306 bool mybool = 123; /* should print "1" */
307
308 init_int_handler();
309
310 if (argc == 2)
311 delay = atoi(argv[1]);
312
313 if (lttng_ust_context_provider_register(&myprovider))
314 abort();
315
316 fprintf(stderr, "Hello, World!\n");
317
318 sleep(delay);
319
320 fprintf(stderr, "Tracing... ");
321 for (i = 0; i < 1000000; i++) {
322 netint = htonl(i);
323 tracepoint(ust_tests_hello, tptest, i, netint, values,
324 text, strlen(text), dbl, flt, mybool);
325 test_inc_count();
326 //usleep(100000);
327 }
328 lttng_ust_context_provider_unregister(&myprovider);
329 fprintf(stderr, " done.\n");
330 return 0;
331 }
This page took 0.039526 seconds and 4 git commands to generate.