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