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