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