cleanup: function attribute 'always_inline'
[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
MD
37
38void test_inc_count(void)
39{
40 test_count++;
41}
42
43static
daacdbfc 44size_t test_get_size(struct lttng_ust_ctx_field *field, size_t offset)
53569322
MD
45{
46 int sel = test_count % _NR_LTTNG_UST_DYNAMIC_TYPES;
47 size_t size = 0;
48
dc325c1d 49 size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(char));
53569322
MD
50 size += sizeof(char); /* tag */
51 switch (sel) {
52 case LTTNG_UST_DYNAMIC_TYPE_NONE:
53 break;
54 case LTTNG_UST_DYNAMIC_TYPE_S8:
dc325c1d 55 size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(int8_t));
53569322
MD
56 size += sizeof(int8_t); /* variant */
57 break;
58 case LTTNG_UST_DYNAMIC_TYPE_S16:
dc325c1d 59 size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(int16_t));
53569322
MD
60 size += sizeof(int16_t); /* variant */
61 break;
62 case LTTNG_UST_DYNAMIC_TYPE_S32:
dc325c1d 63 size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(int32_t));
53569322
MD
64 size += sizeof(int32_t); /* variant */
65 break;
66 case LTTNG_UST_DYNAMIC_TYPE_S64:
dc325c1d 67 size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(int64_t));
53569322
MD
68 size += sizeof(int64_t); /* variant */
69 break;
70 case LTTNG_UST_DYNAMIC_TYPE_U8:
dc325c1d 71 size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(uint8_t));
53569322
MD
72 size += sizeof(uint8_t); /* variant */
73 break;
74 case LTTNG_UST_DYNAMIC_TYPE_U16:
dc325c1d 75 size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(uint16_t));
53569322
MD
76 size += sizeof(uint16_t); /* variant */
77 break;
78 case LTTNG_UST_DYNAMIC_TYPE_U32:
dc325c1d 79 size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(uint32_t));
53569322
MD
80 size += sizeof(uint32_t); /* variant */
81 break;
82 case LTTNG_UST_DYNAMIC_TYPE_U64:
dc325c1d 83 size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(uint64_t));
53569322
MD
84 size += sizeof(uint64_t); /* variant */
85 break;
86 case LTTNG_UST_DYNAMIC_TYPE_FLOAT:
dc325c1d 87 size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(float));
53569322
MD
88 size += sizeof(float); /* variant */
89 break;
90 case LTTNG_UST_DYNAMIC_TYPE_DOUBLE:
dc325c1d 91 size += lttng_ust_lib_ring_buffer_align(offset, lttng_ust_rb_alignof(double));
53569322
MD
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
104static
daacdbfc 105void test_record(struct lttng_ust_ctx_field *field,
53569322 106 struct lttng_ust_lib_ring_buffer_ctx *ctx,
e7bc0ef6 107 struct lttng_ust_channel_buffer *lttng_chan_buf)
53569322
MD
108{
109 int sel = test_count % _NR_LTTNG_UST_DYNAMIC_TYPES;
110 char sel_char = (char) sel;
111
8936b6c0 112 lttng_chan_buf->ops->event_write(ctx, &sel_char, sizeof(sel_char), lttng_ust_rb_alignof(char));
53569322
MD
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
8936b6c0 120 lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v));
53569322
MD
121 break;
122 }
123 case LTTNG_UST_DYNAMIC_TYPE_S16:
124 {
125 int16_t v = -16;
126
8936b6c0 127 lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v));
53569322
MD
128 break;
129 }
130 case LTTNG_UST_DYNAMIC_TYPE_S32:
131 {
132 int32_t v = -32;
133
8936b6c0 134 lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v));
53569322
MD
135 break;
136 }
137 case LTTNG_UST_DYNAMIC_TYPE_S64:
138 {
139 int64_t v = -64;
140
8936b6c0 141 lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v));
53569322
MD
142 break;
143 }
144 case LTTNG_UST_DYNAMIC_TYPE_U8:
145 {
146 uint8_t v = 8;
147
8936b6c0 148 lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v));
53569322
MD
149 break;
150 }
151 case LTTNG_UST_DYNAMIC_TYPE_U16:
152 {
153 uint16_t v = 16;
154
8936b6c0 155 lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v));
53569322
MD
156 break;
157 }
158 case LTTNG_UST_DYNAMIC_TYPE_U32:
159 {
160 uint32_t v = 32;
161
8936b6c0 162 lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v));
53569322
MD
163 break;
164 }
165 case LTTNG_UST_DYNAMIC_TYPE_U64:
166 {
167 uint64_t v = 64;
168
8936b6c0 169 lttng_chan_buf->ops->event_write(ctx, &v, sizeof(v), lttng_ust_rb_alignof(v));
53569322
MD
170 break;
171 }
172 case LTTNG_UST_DYNAMIC_TYPE_FLOAT:
173 {
174 float f = 22322.0;
175
8936b6c0 176 lttng_chan_buf->ops->event_write(ctx, &f, sizeof(f), lttng_ust_rb_alignof(f));
53569322
MD
177 break;
178 }
179 case LTTNG_UST_DYNAMIC_TYPE_DOUBLE:
180 {
181 double d = 2.0;
182
8936b6c0 183 lttng_chan_buf->ops->event_write(ctx, &d, sizeof(d), lttng_ust_rb_alignof(d));
53569322
MD
184 break;
185 }
186 case LTTNG_UST_DYNAMIC_TYPE_STRING:
187 {
188 const char *str = "teststr";
8936b6c0 189 lttng_chan_buf->ops->event_write(ctx, str, strlen(str) + 1, 1);
53569322
MD
190 break;
191 }
192 default:
193 abort();
194 }
195}
196
197static
daacdbfc
MD
198void test_get_value(struct lttng_ust_ctx_field *field,
199 struct lttng_ust_ctx_value *value)
53569322
MD
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:
86133caf 220 value->u.u64 = 8;
53569322
MD
221 break;
222 case LTTNG_UST_DYNAMIC_TYPE_U16:
86133caf 223 value->u.u64 = 16;
53569322
MD
224 break;
225 case LTTNG_UST_DYNAMIC_TYPE_U32:
86133caf 226 value->u.u64 = 32;
53569322
MD
227 break;
228 case LTTNG_UST_DYNAMIC_TYPE_U64:
86133caf 229 value->u.u64 = 64;
53569322
MD
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
245struct lttng_ust_context_provider myprovider = {
daacdbfc 246 .struct_size = sizeof(struct lttng_ust_context_provider),
53569322
MD
247 .name = "$app.myprovider",
248 .get_size = test_get_size,
249 .record = test_record,
250 .get_value = test_get_value,
251};
252
253void inthandler(int sig)
254{
255 printf("in SIGUSR1 handler\n");
256 tracepoint(ust_tests_hello, tptest_sighandler);
257}
258
259int 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
286void test_inc_count(void);
287
288int 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.03857 seconds and 4 git commands to generate.