fix: all functions have declarations (-Wmissing-prototypes -Wold-style-definition)
[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 #include <lttng/ust-events.h>
32 #include <lttng/ringbuffer-context.h>
33 /* Internal header. */
34 #include <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(struct lttng_ust_ctx_field *field, size_t offset)
46 {
47 int sel = test_count % _NR_LTTNG_UST_DYNAMIC_TYPES;
48 size_t size = 0;
49
50 size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(char));
51 size += sizeof(char); /* tag */
52 switch (sel) {
53 case LTTNG_UST_DYNAMIC_TYPE_NONE:
54 break;
55 case LTTNG_UST_DYNAMIC_TYPE_S8:
56 size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(int8_t));
57 size += sizeof(int8_t); /* variant */
58 break;
59 case LTTNG_UST_DYNAMIC_TYPE_S16:
60 size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(int16_t));
61 size += sizeof(int16_t); /* variant */
62 break;
63 case LTTNG_UST_DYNAMIC_TYPE_S32:
64 size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(int32_t));
65 size += sizeof(int32_t); /* variant */
66 break;
67 case LTTNG_UST_DYNAMIC_TYPE_S64:
68 size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(int64_t));
69 size += sizeof(int64_t); /* variant */
70 break;
71 case LTTNG_UST_DYNAMIC_TYPE_U8:
72 size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(uint8_t));
73 size += sizeof(uint8_t); /* variant */
74 break;
75 case LTTNG_UST_DYNAMIC_TYPE_U16:
76 size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(uint16_t));
77 size += sizeof(uint16_t); /* variant */
78 break;
79 case LTTNG_UST_DYNAMIC_TYPE_U32:
80 size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(uint32_t));
81 size += sizeof(uint32_t); /* variant */
82 break;
83 case LTTNG_UST_DYNAMIC_TYPE_U64:
84 size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(uint64_t));
85 size += sizeof(uint64_t); /* variant */
86 break;
87 case LTTNG_UST_DYNAMIC_TYPE_FLOAT:
88 size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(float));
89 size += sizeof(float); /* variant */
90 break;
91 case LTTNG_UST_DYNAMIC_TYPE_DOUBLE:
92 size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(double));
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
105 static
106 void test_record(struct lttng_ust_ctx_field *field,
107 struct lttng_ust_lib_ring_buffer_ctx *ctx,
108 struct lttng_ust_channel_buffer *lttng_chan_buf)
109 {
110 int sel = test_count % _NR_LTTNG_UST_DYNAMIC_TYPES;
111 char sel_char = (char) sel;
112
113 lttng_chan_buf->ops->event_write(ctx, &sel_char, sizeof(sel_char), lttng_ust_rb_alignof(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 lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v));
122 break;
123 }
124 case LTTNG_UST_DYNAMIC_TYPE_S16:
125 {
126 int16_t v = -16;
127
128 lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v));
129 break;
130 }
131 case LTTNG_UST_DYNAMIC_TYPE_S32:
132 {
133 int32_t v = -32;
134
135 lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v));
136 break;
137 }
138 case LTTNG_UST_DYNAMIC_TYPE_S64:
139 {
140 int64_t v = -64;
141
142 lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v));
143 break;
144 }
145 case LTTNG_UST_DYNAMIC_TYPE_U8:
146 {
147 uint8_t v = 8;
148
149 lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v));
150 break;
151 }
152 case LTTNG_UST_DYNAMIC_TYPE_U16:
153 {
154 uint16_t v = 16;
155
156 lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v));
157 break;
158 }
159 case LTTNG_UST_DYNAMIC_TYPE_U32:
160 {
161 uint32_t v = 32;
162
163 lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v));
164 break;
165 }
166 case LTTNG_UST_DYNAMIC_TYPE_U64:
167 {
168 uint64_t v = 64;
169
170 lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v));
171 break;
172 }
173 case LTTNG_UST_DYNAMIC_TYPE_FLOAT:
174 {
175 float f = 22322.0;
176
177 lttng_chan_buf->ops->event_write(ctx, &f, sizeof(f), lttng_ust_rb_alignof(f));
178 break;
179 }
180 case LTTNG_UST_DYNAMIC_TYPE_DOUBLE:
181 {
182 double d = 2.0;
183
184 lttng_chan_buf->ops->event_write(ctx, &d, sizeof(d), lttng_ust_rb_alignof(d));
185 break;
186 }
187 case LTTNG_UST_DYNAMIC_TYPE_STRING:
188 {
189 const char *str = "teststr";
190 lttng_chan_buf->ops->event_write(ctx, str, strlen(str) + 1, 1);
191 break;
192 }
193 default:
194 abort();
195 }
196 }
197
198 static
199 void test_get_value(struct lttng_ust_ctx_field *field,
200 struct lttng_ust_ctx_value *value)
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:
221 value->u.u64 = 8;
222 break;
223 case LTTNG_UST_DYNAMIC_TYPE_U16:
224 value->u.u64 = 16;
225 break;
226 case LTTNG_UST_DYNAMIC_TYPE_U32:
227 value->u.u64 = 32;
228 break;
229 case LTTNG_UST_DYNAMIC_TYPE_U64:
230 value->u.u64 = 64;
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
246 struct lttng_ust_context_provider myprovider = {
247 .struct_size = sizeof(struct lttng_ust_context_provider),
248 .name = "$app.myprovider",
249 .get_size = test_get_size,
250 .record = test_record,
251 .get_value = test_get_value,
252 };
253
254 static
255 void inthandler(int sig)
256 {
257 printf("in SIGUSR1 handler\n");
258 tracepoint(ust_tests_hello, tptest_sighandler);
259 }
260
261 static
262 int init_int_handler(void)
263 {
264 int result;
265 struct sigaction act;
266
267 memset(&act, 0, sizeof(act));
268 result = sigemptyset(&act.sa_mask);
269 if (result == -1) {
270 perror("sigemptyset");
271 return -1;
272 }
273
274 act.sa_handler = inthandler;
275 act.sa_flags = SA_RESTART;
276
277 /* Only defer ourselves. Also, try to restart interrupted
278 * syscalls to disturb the traced program as little as possible.
279 */
280 result = sigaction(SIGUSR1, &act, NULL);
281 if (result == -1) {
282 perror("sigaction");
283 return -1;
284 }
285
286 return 0;
287 }
288
289 int main(int argc, char **argv)
290 {
291 int i, netint;
292 long values[] = { 1, 2, 3 };
293 char text[10] = "test";
294 double dbl = 2.0;
295 float flt = 2222.0;
296 int delay = 0;
297 bool mybool = 123; /* should print "1" */
298
299 init_int_handler();
300
301 if (argc == 2)
302 delay = atoi(argv[1]);
303
304 if (lttng_ust_context_provider_register(&myprovider))
305 abort();
306
307 fprintf(stderr, "Hello, World!\n");
308
309 sleep(delay);
310
311 fprintf(stderr, "Tracing... ");
312 for (i = 0; i < 1000000; i++) {
313 netint = htonl(i);
314 tracepoint(ust_tests_hello, tptest, i, netint, values,
315 text, strlen(text), dbl, flt, mybool);
316 test_inc_count();
317 //usleep(100000);
318 }
319 lttng_ust_context_provider_unregister(&myprovider);
320 fprintf(stderr, " done.\n");
321 return 0;
322 }
This page took 0.036646 seconds and 4 git commands to generate.