85064846ab60ca449ac17045df97c12e0ee2566b
[lttng-ust.git] / tests / test-app-ctx / hello.c
1 /*
2 * Copyright (C) 2009 Pierre-Marc Fournier
3 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; version 2.1 of
8 * the License.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include <stdio.h>
21 #include <unistd.h>
22 #include <sys/mman.h>
23 #include <stdarg.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <signal.h>
28 #include <string.h>
29 /*
30 * Work-around inet.h missing struct mmsghdr forward declaration, with
31 * triggers a warning when system files warnings are enabled.
32 */
33 struct mmsghdr;
34 #include <arpa/inet.h>
35 #include <stddef.h>
36 #include <stdlib.h>
37 #include <stdbool.h>
38
39 #define TRACEPOINT_DEFINE
40 #include "ust_tests_hello.h"
41
42 /* Internal header. */
43 #include <lttng/ust-events.h>
44 #include <lttng/ringbuffer-config.h>
45 #include <lttng/ust-context-provider.h>
46
47 static __thread unsigned int test_count;
48
49 void test_inc_count(void)
50 {
51 test_count++;
52 }
53
54 static
55 size_t test_get_size(struct lttng_ctx_field *field, size_t offset)
56 {
57 int sel = test_count % _NR_LTTNG_UST_DYNAMIC_TYPES;
58 size_t size = 0;
59
60 size += lib_ring_buffer_align(offset, lttng_alignof(char));
61 size += sizeof(char); /* tag */
62 switch (sel) {
63 case LTTNG_UST_DYNAMIC_TYPE_NONE:
64 break;
65 case LTTNG_UST_DYNAMIC_TYPE_S8:
66 size += lib_ring_buffer_align(offset, lttng_alignof(int8_t));
67 size += sizeof(int8_t); /* variant */
68 break;
69 case LTTNG_UST_DYNAMIC_TYPE_S16:
70 size += lib_ring_buffer_align(offset, lttng_alignof(int16_t));
71 size += sizeof(int16_t); /* variant */
72 break;
73 case LTTNG_UST_DYNAMIC_TYPE_S32:
74 size += lib_ring_buffer_align(offset, lttng_alignof(int32_t));
75 size += sizeof(int32_t); /* variant */
76 break;
77 case LTTNG_UST_DYNAMIC_TYPE_S64:
78 size += lib_ring_buffer_align(offset, lttng_alignof(int64_t));
79 size += sizeof(int64_t); /* variant */
80 break;
81 case LTTNG_UST_DYNAMIC_TYPE_U8:
82 size += lib_ring_buffer_align(offset, lttng_alignof(uint8_t));
83 size += sizeof(uint8_t); /* variant */
84 break;
85 case LTTNG_UST_DYNAMIC_TYPE_U16:
86 size += lib_ring_buffer_align(offset, lttng_alignof(uint16_t));
87 size += sizeof(uint16_t); /* variant */
88 break;
89 case LTTNG_UST_DYNAMIC_TYPE_U32:
90 size += lib_ring_buffer_align(offset, lttng_alignof(uint32_t));
91 size += sizeof(uint32_t); /* variant */
92 break;
93 case LTTNG_UST_DYNAMIC_TYPE_U64:
94 size += lib_ring_buffer_align(offset, lttng_alignof(uint64_t));
95 size += sizeof(uint64_t); /* variant */
96 break;
97 case LTTNG_UST_DYNAMIC_TYPE_FLOAT:
98 size += lib_ring_buffer_align(offset, lttng_alignof(float));
99 size += sizeof(float); /* variant */
100 break;
101 case LTTNG_UST_DYNAMIC_TYPE_DOUBLE:
102 size += lib_ring_buffer_align(offset, lttng_alignof(double));
103 size += sizeof(double); /* variant */
104 break;
105 case LTTNG_UST_DYNAMIC_TYPE_STRING:
106 size += strlen("teststr") + 1;
107 break;
108 default:
109 abort();
110 }
111
112 return size;
113 }
114
115 static
116 void test_record(struct lttng_ctx_field *field,
117 struct lttng_ust_lib_ring_buffer_ctx *ctx,
118 struct lttng_channel *chan)
119 {
120 int sel = test_count % _NR_LTTNG_UST_DYNAMIC_TYPES;
121 char sel_char = (char) sel;
122
123 lib_ring_buffer_align_ctx(ctx, lttng_alignof(char));
124 chan->ops->event_write(ctx, &sel_char, sizeof(sel_char));
125 switch (sel) {
126 case LTTNG_UST_DYNAMIC_TYPE_NONE:
127 break;
128 case LTTNG_UST_DYNAMIC_TYPE_S8:
129 {
130 int8_t v = -8;
131
132 lib_ring_buffer_align_ctx(ctx, lttng_alignof(v));
133 chan->ops->event_write(ctx, &v, sizeof(v));
134 break;
135 }
136 case LTTNG_UST_DYNAMIC_TYPE_S16:
137 {
138 int16_t v = -16;
139
140 lib_ring_buffer_align_ctx(ctx, lttng_alignof(v));
141 chan->ops->event_write(ctx, &v, sizeof(v));
142 break;
143 }
144 case LTTNG_UST_DYNAMIC_TYPE_S32:
145 {
146 int32_t v = -32;
147
148 lib_ring_buffer_align_ctx(ctx, lttng_alignof(v));
149 chan->ops->event_write(ctx, &v, sizeof(v));
150 break;
151 }
152 case LTTNG_UST_DYNAMIC_TYPE_S64:
153 {
154 int64_t v = -64;
155
156 lib_ring_buffer_align_ctx(ctx, lttng_alignof(v));
157 chan->ops->event_write(ctx, &v, sizeof(v));
158 break;
159 }
160 case LTTNG_UST_DYNAMIC_TYPE_U8:
161 {
162 uint8_t v = 8;
163
164 lib_ring_buffer_align_ctx(ctx, lttng_alignof(v));
165 chan->ops->event_write(ctx, &v, sizeof(v));
166 break;
167 }
168 case LTTNG_UST_DYNAMIC_TYPE_U16:
169 {
170 uint16_t v = 16;
171
172 lib_ring_buffer_align_ctx(ctx, lttng_alignof(v));
173 chan->ops->event_write(ctx, &v, sizeof(v));
174 break;
175 }
176 case LTTNG_UST_DYNAMIC_TYPE_U32:
177 {
178 uint32_t v = 32;
179
180 lib_ring_buffer_align_ctx(ctx, lttng_alignof(v));
181 chan->ops->event_write(ctx, &v, sizeof(v));
182 break;
183 }
184 case LTTNG_UST_DYNAMIC_TYPE_U64:
185 {
186 uint64_t v = 64;
187
188 lib_ring_buffer_align_ctx(ctx, lttng_alignof(v));
189 chan->ops->event_write(ctx, &v, sizeof(v));
190 break;
191 }
192 case LTTNG_UST_DYNAMIC_TYPE_FLOAT:
193 {
194 float f = 22322.0;
195
196 lib_ring_buffer_align_ctx(ctx, lttng_alignof(f));
197 chan->ops->event_write(ctx, &f, sizeof(f));
198 break;
199 }
200 case LTTNG_UST_DYNAMIC_TYPE_DOUBLE:
201 {
202 double d = 2.0;
203
204 lib_ring_buffer_align_ctx(ctx, lttng_alignof(d));
205 chan->ops->event_write(ctx, &d, sizeof(d));
206 break;
207 }
208 case LTTNG_UST_DYNAMIC_TYPE_STRING:
209 {
210 const char *str = "teststr";
211 chan->ops->event_write(ctx, str, strlen(str) + 1);
212 break;
213 }
214 default:
215 abort();
216 }
217 }
218
219 static
220 void test_get_value(struct lttng_ctx_field *field,
221 struct lttng_ctx_value *value)
222 {
223 int sel = test_count % _NR_LTTNG_UST_DYNAMIC_TYPES;
224
225 value->sel = sel;
226 switch (sel) {
227 case LTTNG_UST_DYNAMIC_TYPE_NONE:
228 break;
229 case LTTNG_UST_DYNAMIC_TYPE_S8:
230 value->u.s64 = -8;
231 break;
232 case LTTNG_UST_DYNAMIC_TYPE_S16:
233 value->u.s64 = -16;
234 break;
235 case LTTNG_UST_DYNAMIC_TYPE_S32:
236 value->u.s64 = -32;
237 break;
238 case LTTNG_UST_DYNAMIC_TYPE_S64:
239 value->u.s64 = -64;
240 break;
241 case LTTNG_UST_DYNAMIC_TYPE_U8:
242 value->u.s64 = 8;
243 break;
244 case LTTNG_UST_DYNAMIC_TYPE_U16:
245 value->u.s64 = 16;
246 break;
247 case LTTNG_UST_DYNAMIC_TYPE_U32:
248 value->u.s64 = 32;
249 break;
250 case LTTNG_UST_DYNAMIC_TYPE_U64:
251 value->u.s64 = 64;
252 break;
253 case LTTNG_UST_DYNAMIC_TYPE_FLOAT:
254 value->u.d = 22322.0;
255 break;
256 case LTTNG_UST_DYNAMIC_TYPE_DOUBLE:
257 value->u.d = 2.0;
258 break;
259 case LTTNG_UST_DYNAMIC_TYPE_STRING:
260 value->u.str = "teststr";
261 break;
262 default:
263 abort();
264 }
265 }
266
267 struct lttng_ust_context_provider myprovider = {
268 .name = "$app.myprovider",
269 .get_size = test_get_size,
270 .record = test_record,
271 .get_value = test_get_value,
272 };
273
274 void inthandler(int sig)
275 {
276 printf("in SIGUSR1 handler\n");
277 tracepoint(ust_tests_hello, tptest_sighandler);
278 }
279
280 int init_int_handler(void)
281 {
282 int result;
283 struct sigaction act;
284
285 memset(&act, 0, sizeof(act));
286 result = sigemptyset(&act.sa_mask);
287 if (result == -1) {
288 perror("sigemptyset");
289 return -1;
290 }
291
292 act.sa_handler = inthandler;
293 act.sa_flags = SA_RESTART;
294
295 /* Only defer ourselves. Also, try to restart interrupted
296 * syscalls to disturb the traced program as little as possible.
297 */
298 result = sigaction(SIGUSR1, &act, NULL);
299 if (result == -1) {
300 perror("sigaction");
301 return -1;
302 }
303
304 return 0;
305 }
306
307 void test_inc_count(void);
308
309 int main(int argc, char **argv)
310 {
311 int i, netint;
312 long values[] = { 1, 2, 3 };
313 char text[10] = "test";
314 double dbl = 2.0;
315 float flt = 2222.0;
316 int delay = 0;
317 bool mybool = 123; /* should print "1" */
318
319 init_int_handler();
320
321 if (argc == 2)
322 delay = atoi(argv[1]);
323
324 if (lttng_ust_context_provider_register(&myprovider))
325 abort();
326
327 fprintf(stderr, "Hello, World!\n");
328
329 sleep(delay);
330
331 fprintf(stderr, "Tracing... ");
332 for (i = 0; i < 1000000; i++) {
333 netint = htonl(i);
334 tracepoint(ust_tests_hello, tptest, i, netint, values,
335 text, strlen(text), dbl, flt, mybool);
336 test_inc_count();
337 //usleep(100000);
338 }
339 lttng_ust_context_provider_unregister(&myprovider);
340 fprintf(stderr, " done.\n");
341 return 0;
342 }
This page took 0.034311 seconds and 3 git commands to generate.