| 1 | /* |
| 2 | * Copyright (C) 2011 EfficiOS Inc. |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <common/compat/errno.hpp> |
| 9 | #include <common/defaults.hpp> |
| 10 | |
| 11 | #include <lttng/lttng.h> |
| 12 | #include <lttng/ust-sigbus.h> |
| 13 | |
| 14 | #include <bin/lttng-sessiond/lttng-ust-abi.hpp> |
| 15 | #include <bin/lttng-sessiond/notification-thread.hpp> |
| 16 | #include <bin/lttng-sessiond/trace-ust.hpp> |
| 17 | #include <bin/lttng-sessiond/ust-app.hpp> |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | #include <tap/tap.h> |
| 22 | #include <time.h> |
| 23 | #include <unistd.h> |
| 24 | #include <urcu.h> |
| 25 | |
| 26 | /* This path will NEVER be created in this test */ |
| 27 | #define PATH1 "/tmp/.test-junk-lttng" |
| 28 | |
| 29 | #define RANDOM_STRING_LEN 11 |
| 30 | |
| 31 | /* Number of TAP tests in this file */ |
| 32 | #define NUM_TESTS 16 |
| 33 | |
| 34 | LTTNG_EXPORT DEFINE_LTTNG_UST_SIGBUS_STATE(); |
| 35 | |
| 36 | static const char alphanum[] = "0123456789" |
| 37 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 38 | "abcdefghijklmnopqrstuvwxyz"; |
| 39 | static char random_string[RANDOM_STRING_LEN]; |
| 40 | |
| 41 | /* |
| 42 | * Return random string of 10 characters. |
| 43 | * Not thread-safe. |
| 44 | */ |
| 45 | static char *get_random_string(void) |
| 46 | { |
| 47 | int i; |
| 48 | |
| 49 | for (i = 0; i < RANDOM_STRING_LEN - 1; i++) { |
| 50 | random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)]; |
| 51 | } |
| 52 | |
| 53 | random_string[RANDOM_STRING_LEN - 1] = '\0'; |
| 54 | |
| 55 | return random_string; |
| 56 | } |
| 57 | |
| 58 | static void test_create_one_ust_session(void) |
| 59 | { |
| 60 | struct ltt_ust_session *usess = trace_ust_create_session(42); |
| 61 | |
| 62 | ok(usess != NULL, "Create UST session"); |
| 63 | |
| 64 | if (!usess) { |
| 65 | skip(1, "UST session is null"); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | ok(usess->id == 42 && usess->active == 0 && usess->domain_global.channels != NULL && |
| 70 | usess->uid == 0 && usess->gid == 0, |
| 71 | "Validate UST session"); |
| 72 | |
| 73 | trace_ust_destroy_session(usess); |
| 74 | trace_ust_free_session(usess); |
| 75 | } |
| 76 | |
| 77 | static void test_create_ust_channel(void) |
| 78 | { |
| 79 | struct ltt_ust_channel *uchan; |
| 80 | struct lttng_channel attr; |
| 81 | struct lttng_channel_extended extended; |
| 82 | |
| 83 | memset(&attr, 0, sizeof(attr)); |
| 84 | memset(&extended, 0, sizeof(extended)); |
| 85 | attr.attr.extended.ptr = &extended; |
| 86 | |
| 87 | ok(lttng_strncpy(attr.name, "channel0", sizeof(attr.name)) == 0, |
| 88 | "Validate channel name length"); |
| 89 | uchan = trace_ust_create_channel(&attr, LTTNG_DOMAIN_UST); |
| 90 | ok(uchan != NULL, "Create UST channel"); |
| 91 | |
| 92 | if (!uchan) { |
| 93 | skip(1, "UST channel is null"); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | ok(uchan->enabled == 0 && strncmp(uchan->name, "channel0", 8) == 0 && |
| 98 | uchan->name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] == '\0' && uchan->ctx != NULL && |
| 99 | uchan->events != NULL && uchan->attr.overwrite == attr.attr.overwrite, |
| 100 | "Validate UST channel"); |
| 101 | |
| 102 | trace_ust_destroy_channel(uchan); |
| 103 | } |
| 104 | |
| 105 | static void test_create_ust_event(void) |
| 106 | { |
| 107 | struct ltt_ust_event *event; |
| 108 | struct lttng_event ev; |
| 109 | enum lttng_error_code ret; |
| 110 | |
| 111 | memset(&ev, 0, sizeof(ev)); |
| 112 | ok(lttng_strncpy(ev.name, get_random_string(), LTTNG_SYMBOL_NAME_LEN) == 0, |
| 113 | "Validate string length"); |
| 114 | ev.type = LTTNG_EVENT_TRACEPOINT; |
| 115 | ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; |
| 116 | |
| 117 | ret = trace_ust_create_event(&ev, NULL, NULL, NULL, false, &event); |
| 118 | |
| 119 | ok(ret == LTTNG_OK, "Create UST event"); |
| 120 | |
| 121 | if (!event) { |
| 122 | skip(1, "UST event is null"); |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | ok(event->enabled == 0 && event->attr.instrumentation == LTTNG_UST_ABI_TRACEPOINT && |
| 127 | strcmp(event->attr.name, ev.name) == 0 && |
| 128 | event->attr.name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] == '\0', |
| 129 | "Validate UST event"); |
| 130 | |
| 131 | trace_ust_destroy_event(event); |
| 132 | } |
| 133 | |
| 134 | static void test_create_ust_event_exclusion(void) |
| 135 | { |
| 136 | int copy_ret; |
| 137 | enum lttng_error_code ret; |
| 138 | struct ltt_ust_event *event; |
| 139 | struct lttng_event ev; |
| 140 | char *name; |
| 141 | char *random_name; |
| 142 | struct lttng_event_exclusion *exclusion = NULL; |
| 143 | struct lttng_event_exclusion *exclusion_copy = NULL; |
| 144 | const int exclusion_count = 2; |
| 145 | |
| 146 | memset(&ev, 0, sizeof(ev)); |
| 147 | |
| 148 | /* make a wildcarded event name */ |
| 149 | name = get_random_string(); |
| 150 | name[strlen(name) - 1] = '*'; |
| 151 | ok(lttng_strncpy(ev.name, name, LTTNG_SYMBOL_NAME_LEN) == 0, "Validate string length"); |
| 152 | |
| 153 | ev.type = LTTNG_EVENT_TRACEPOINT; |
| 154 | ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; |
| 155 | |
| 156 | /* set up an exclusion set */ |
| 157 | exclusion = zmalloc<lttng_event_exclusion>(sizeof(*exclusion) + |
| 158 | LTTNG_SYMBOL_NAME_LEN * exclusion_count); |
| 159 | ok(exclusion != NULL, "Create UST exclusion"); |
| 160 | if (!exclusion) { |
| 161 | skip(4, "zmalloc failed"); |
| 162 | goto end; |
| 163 | } |
| 164 | |
| 165 | exclusion->count = exclusion_count; |
| 166 | random_name = get_random_string(); |
| 167 | strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0), |
| 168 | random_name, |
| 169 | LTTNG_SYMBOL_NAME_LEN - 1); |
| 170 | strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1), |
| 171 | random_name, |
| 172 | LTTNG_SYMBOL_NAME_LEN - 1); |
| 173 | |
| 174 | ret = trace_ust_create_event(&ev, NULL, NULL, exclusion, false, &event); |
| 175 | exclusion = NULL; |
| 176 | |
| 177 | ok(ret != LTTNG_OK, "Create UST event with identical exclusion names fails"); |
| 178 | |
| 179 | exclusion = zmalloc<lttng_event_exclusion>(sizeof(*exclusion) + |
| 180 | LTTNG_SYMBOL_NAME_LEN * exclusion_count); |
| 181 | ok(exclusion != NULL, "Create UST exclusion"); |
| 182 | if (!exclusion) { |
| 183 | skip(2, "zmalloc failed"); |
| 184 | goto end; |
| 185 | } |
| 186 | |
| 187 | exclusion_copy = zmalloc<lttng_event_exclusion>(sizeof(*exclusion) + |
| 188 | LTTNG_SYMBOL_NAME_LEN * exclusion_count); |
| 189 | if (!exclusion_copy) { |
| 190 | skip(2, "zmalloc failed"); |
| 191 | goto end; |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * We are giving ownership of the exclusion struct to the |
| 196 | * trace_ust_create_event() function. Make a copy of the exclusion struct |
| 197 | * so we can compare it later. |
| 198 | */ |
| 199 | |
| 200 | exclusion->count = exclusion_count; |
| 201 | copy_ret = lttng_strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0), |
| 202 | get_random_string(), |
| 203 | sizeof(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0))); |
| 204 | LTTNG_ASSERT(copy_ret == 0); |
| 205 | copy_ret = lttng_strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1), |
| 206 | get_random_string(), |
| 207 | sizeof(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1))); |
| 208 | LTTNG_ASSERT(copy_ret == 0); |
| 209 | |
| 210 | exclusion_copy->count = exclusion_count; |
| 211 | copy_ret = lttng_strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy, 0), |
| 212 | LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0), |
| 213 | sizeof(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy, 0))); |
| 214 | LTTNG_ASSERT(copy_ret == 0); |
| 215 | copy_ret = lttng_strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy, 1), |
| 216 | LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1), |
| 217 | sizeof(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy, 1))); |
| 218 | LTTNG_ASSERT(copy_ret == 0); |
| 219 | |
| 220 | ret = trace_ust_create_event(&ev, NULL, NULL, exclusion, false, &event); |
| 221 | exclusion = NULL; |
| 222 | ok(ret == LTTNG_OK, "Create UST event with different exclusion names"); |
| 223 | |
| 224 | if (!event) { |
| 225 | skip(1, "UST event with exclusion is null"); |
| 226 | goto end; |
| 227 | } |
| 228 | |
| 229 | ok(event->enabled == 0 && event->attr.instrumentation == LTTNG_UST_ABI_TRACEPOINT && |
| 230 | strcmp(event->attr.name, ev.name) == 0 && event->exclusion != NULL && |
| 231 | event->exclusion->count == exclusion_count && |
| 232 | !memcmp(event->exclusion->names, |
| 233 | exclusion_copy->names, |
| 234 | LTTNG_SYMBOL_NAME_LEN * exclusion_count) && |
| 235 | event->attr.name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] == '\0', |
| 236 | "Validate UST event and exclusion"); |
| 237 | |
| 238 | trace_ust_destroy_event(event); |
| 239 | end: |
| 240 | free(exclusion); |
| 241 | free(exclusion_copy); |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | static void test_create_ust_context(void) |
| 246 | { |
| 247 | struct lttng_event_context ectx; |
| 248 | struct ltt_ust_context *uctx; |
| 249 | |
| 250 | ectx.ctx = LTTNG_EVENT_CONTEXT_VTID; |
| 251 | |
| 252 | uctx = trace_ust_create_context(&ectx); |
| 253 | ok(uctx != NULL, "Create UST context"); |
| 254 | |
| 255 | if (uctx) { |
| 256 | ok((int) uctx->ctx.ctx == LTTNG_UST_ABI_CONTEXT_VTID, "Validate UST context"); |
| 257 | } else { |
| 258 | skip(1, "Skipping UST context validation as creation failed"); |
| 259 | } |
| 260 | free(uctx); |
| 261 | } |
| 262 | |
| 263 | int main(void) |
| 264 | { |
| 265 | plan_tests(NUM_TESTS); |
| 266 | |
| 267 | diag("UST data structures unit test"); |
| 268 | |
| 269 | rcu_register_thread(); |
| 270 | |
| 271 | test_create_one_ust_session(); |
| 272 | test_create_ust_channel(); |
| 273 | test_create_ust_event(); |
| 274 | test_create_ust_context(); |
| 275 | test_create_ust_event_exclusion(); |
| 276 | |
| 277 | rcu_unregister_thread(); |
| 278 | |
| 279 | return exit_status(); |
| 280 | } |