723f179f4809c9e8d13d8f1718eb1650ee4583ae
[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)), size_t offset)
46 {
47 int sel = test_count % _NR_LTTNG_UST_DYNAMIC_TYPES;
48 size_t size = 0;
49
50 size += lttng_ust_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_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_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_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_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_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_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_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_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_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_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(void *priv __attribute__((unused)),
107 struct lttng_ust_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(void *priv __attribute__((unused)),
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 static char myprovider_name[] = "$app.myprovider";
247 struct lttng_ust_context_provider myprovider = {
248 .struct_size = sizeof(struct lttng_ust_context_provider),
249 .name = myprovider_name,
250 .get_size = test_get_size,
251 .record = test_record,
252 .get_value = test_get_value,
253 };
254
255 static
256 void inthandler(int sig __attribute__((unused)))
257 {
258 printf("in SIGUSR1 handler\n");
259 lttng_ust_tracepoint(ust_tests_hello, tptest_sighandler);
260 }
261
262 static
263 int 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
290 int main(int argc, char **argv)
291 {
292 struct lttng_ust_registered_context_provider *reg_provider;
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
306 reg_provider = lttng_ust_context_provider_register(&myprovider);
307 if (!reg_provider)
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 lttng_ust_tracepoint(ust_tests_hello, tptest, i, netint, values,
318 text, strlen(text), dbl, flt, mybool);
319 test_inc_count();
320 //usleep(100000);
321 }
322 lttng_ust_context_provider_unregister(reg_provider);
323 fprintf(stderr, " done.\n");
324 return 0;
325 }
This page took 0.035436 seconds and 3 git commands to generate.