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