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