| 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 TRACEPOINT_DEFINE |
| 29 | #include "ust_tests_hello.h" |
| 30 | |
| 31 | /* Internal header. */ |
| 32 | #include <lttng/ust-events.h> |
| 33 | #include <lttng/ringbuffer-config.h> |
| 34 | #include <ust-context-provider.h> |
| 35 | |
| 36 | static __thread unsigned int test_count; |
| 37 | |
| 38 | void test_inc_count(void) |
| 39 | { |
| 40 | test_count++; |
| 41 | } |
| 42 | |
| 43 | static |
| 44 | size_t test_get_size(struct lttng_ust_ctx_field *field, size_t offset) |
| 45 | { |
| 46 | int sel = test_count % _NR_LTTNG_UST_DYNAMIC_TYPES; |
| 47 | size_t size = 0; |
| 48 | |
| 49 | size += lib_ring_buffer_align(offset, lttng_alignof(char)); |
| 50 | size += sizeof(char); /* tag */ |
| 51 | switch (sel) { |
| 52 | case LTTNG_UST_DYNAMIC_TYPE_NONE: |
| 53 | break; |
| 54 | case LTTNG_UST_DYNAMIC_TYPE_S8: |
| 55 | size += lib_ring_buffer_align(offset, lttng_alignof(int8_t)); |
| 56 | size += sizeof(int8_t); /* variant */ |
| 57 | break; |
| 58 | case LTTNG_UST_DYNAMIC_TYPE_S16: |
| 59 | size += lib_ring_buffer_align(offset, lttng_alignof(int16_t)); |
| 60 | size += sizeof(int16_t); /* variant */ |
| 61 | break; |
| 62 | case LTTNG_UST_DYNAMIC_TYPE_S32: |
| 63 | size += lib_ring_buffer_align(offset, lttng_alignof(int32_t)); |
| 64 | size += sizeof(int32_t); /* variant */ |
| 65 | break; |
| 66 | case LTTNG_UST_DYNAMIC_TYPE_S64: |
| 67 | size += lib_ring_buffer_align(offset, lttng_alignof(int64_t)); |
| 68 | size += sizeof(int64_t); /* variant */ |
| 69 | break; |
| 70 | case LTTNG_UST_DYNAMIC_TYPE_U8: |
| 71 | size += lib_ring_buffer_align(offset, lttng_alignof(uint8_t)); |
| 72 | size += sizeof(uint8_t); /* variant */ |
| 73 | break; |
| 74 | case LTTNG_UST_DYNAMIC_TYPE_U16: |
| 75 | size += lib_ring_buffer_align(offset, lttng_alignof(uint16_t)); |
| 76 | size += sizeof(uint16_t); /* variant */ |
| 77 | break; |
| 78 | case LTTNG_UST_DYNAMIC_TYPE_U32: |
| 79 | size += lib_ring_buffer_align(offset, lttng_alignof(uint32_t)); |
| 80 | size += sizeof(uint32_t); /* variant */ |
| 81 | break; |
| 82 | case LTTNG_UST_DYNAMIC_TYPE_U64: |
| 83 | size += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
| 84 | size += sizeof(uint64_t); /* variant */ |
| 85 | break; |
| 86 | case LTTNG_UST_DYNAMIC_TYPE_FLOAT: |
| 87 | size += lib_ring_buffer_align(offset, lttng_alignof(float)); |
| 88 | size += sizeof(float); /* variant */ |
| 89 | break; |
| 90 | case LTTNG_UST_DYNAMIC_TYPE_DOUBLE: |
| 91 | size += lib_ring_buffer_align(offset, lttng_alignof(double)); |
| 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 | |
| 104 | static |
| 105 | void test_record(struct lttng_ust_ctx_field *field, |
| 106 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
| 107 | struct lttng_channel *chan) |
| 108 | { |
| 109 | int sel = test_count % _NR_LTTNG_UST_DYNAMIC_TYPES; |
| 110 | char sel_char = (char) sel; |
| 111 | |
| 112 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(char)); |
| 113 | chan->ops->event_write(ctx, &sel_char, sizeof(sel_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 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(v)); |
| 122 | chan->ops->event_write(ctx, &v, sizeof(v)); |
| 123 | break; |
| 124 | } |
| 125 | case LTTNG_UST_DYNAMIC_TYPE_S16: |
| 126 | { |
| 127 | int16_t v = -16; |
| 128 | |
| 129 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(v)); |
| 130 | chan->ops->event_write(ctx, &v, sizeof(v)); |
| 131 | break; |
| 132 | } |
| 133 | case LTTNG_UST_DYNAMIC_TYPE_S32: |
| 134 | { |
| 135 | int32_t v = -32; |
| 136 | |
| 137 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(v)); |
| 138 | chan->ops->event_write(ctx, &v, sizeof(v)); |
| 139 | break; |
| 140 | } |
| 141 | case LTTNG_UST_DYNAMIC_TYPE_S64: |
| 142 | { |
| 143 | int64_t v = -64; |
| 144 | |
| 145 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(v)); |
| 146 | chan->ops->event_write(ctx, &v, sizeof(v)); |
| 147 | break; |
| 148 | } |
| 149 | case LTTNG_UST_DYNAMIC_TYPE_U8: |
| 150 | { |
| 151 | uint8_t v = 8; |
| 152 | |
| 153 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(v)); |
| 154 | chan->ops->event_write(ctx, &v, sizeof(v)); |
| 155 | break; |
| 156 | } |
| 157 | case LTTNG_UST_DYNAMIC_TYPE_U16: |
| 158 | { |
| 159 | uint16_t v = 16; |
| 160 | |
| 161 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(v)); |
| 162 | chan->ops->event_write(ctx, &v, sizeof(v)); |
| 163 | break; |
| 164 | } |
| 165 | case LTTNG_UST_DYNAMIC_TYPE_U32: |
| 166 | { |
| 167 | uint32_t v = 32; |
| 168 | |
| 169 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(v)); |
| 170 | chan->ops->event_write(ctx, &v, sizeof(v)); |
| 171 | break; |
| 172 | } |
| 173 | case LTTNG_UST_DYNAMIC_TYPE_U64: |
| 174 | { |
| 175 | uint64_t v = 64; |
| 176 | |
| 177 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(v)); |
| 178 | chan->ops->event_write(ctx, &v, sizeof(v)); |
| 179 | break; |
| 180 | } |
| 181 | case LTTNG_UST_DYNAMIC_TYPE_FLOAT: |
| 182 | { |
| 183 | float f = 22322.0; |
| 184 | |
| 185 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(f)); |
| 186 | chan->ops->event_write(ctx, &f, sizeof(f)); |
| 187 | break; |
| 188 | } |
| 189 | case LTTNG_UST_DYNAMIC_TYPE_DOUBLE: |
| 190 | { |
| 191 | double d = 2.0; |
| 192 | |
| 193 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(d)); |
| 194 | chan->ops->event_write(ctx, &d, sizeof(d)); |
| 195 | break; |
| 196 | } |
| 197 | case LTTNG_UST_DYNAMIC_TYPE_STRING: |
| 198 | { |
| 199 | const char *str = "teststr"; |
| 200 | chan->ops->event_write(ctx, str, strlen(str) + 1); |
| 201 | break; |
| 202 | } |
| 203 | default: |
| 204 | abort(); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | static |
| 209 | void test_get_value(struct lttng_ust_ctx_field *field, |
| 210 | struct lttng_ust_ctx_value *value) |
| 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: |
| 231 | value->u.u64 = 8; |
| 232 | break; |
| 233 | case LTTNG_UST_DYNAMIC_TYPE_U16: |
| 234 | value->u.u64 = 16; |
| 235 | break; |
| 236 | case LTTNG_UST_DYNAMIC_TYPE_U32: |
| 237 | value->u.u64 = 32; |
| 238 | break; |
| 239 | case LTTNG_UST_DYNAMIC_TYPE_U64: |
| 240 | value->u.u64 = 64; |
| 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 | |
| 256 | struct lttng_ust_context_provider myprovider = { |
| 257 | .struct_size = sizeof(struct lttng_ust_context_provider), |
| 258 | .name = "$app.myprovider", |
| 259 | .get_size = test_get_size, |
| 260 | .record = test_record, |
| 261 | .get_value = test_get_value, |
| 262 | }; |
| 263 | |
| 264 | void inthandler(int sig) |
| 265 | { |
| 266 | printf("in SIGUSR1 handler\n"); |
| 267 | tracepoint(ust_tests_hello, tptest_sighandler); |
| 268 | } |
| 269 | |
| 270 | int 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 | |
| 297 | void test_inc_count(void); |
| 298 | |
| 299 | int 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 | } |