| 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 | |
| 27 | #include <bin/lttng-sessiond/trace-kernel.h> |
| 28 | #include <common/defaults.h> |
| 29 | |
| 30 | #include <tap/tap.h> |
| 31 | |
| 32 | #define RANDOM_STRING_LEN 11 |
| 33 | |
| 34 | /* Number of TAP tests in this file */ |
| 35 | #define NUM_TESTS 11 |
| 36 | |
| 37 | /* For error.h */ |
| 38 | int lttng_opt_quiet = 1; |
| 39 | int lttng_opt_verbose; |
| 40 | int lttng_opt_mi; |
| 41 | |
| 42 | int ust_consumerd32_fd; |
| 43 | int ust_consumerd64_fd; |
| 44 | |
| 45 | static const char alphanum[] = |
| 46 | "0123456789" |
| 47 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 48 | "abcdefghijklmnopqrstuvwxyz"; |
| 49 | |
| 50 | static struct ltt_kernel_session *kern; |
| 51 | static char random_string[RANDOM_STRING_LEN]; |
| 52 | |
| 53 | /* |
| 54 | * Return random string of 10 characters. |
| 55 | * Not thread-safe. |
| 56 | */ |
| 57 | static char *get_random_string(void) |
| 58 | { |
| 59 | int i; |
| 60 | |
| 61 | for (i = 0; i < RANDOM_STRING_LEN - 1; i++) { |
| 62 | random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)]; |
| 63 | } |
| 64 | |
| 65 | random_string[RANDOM_STRING_LEN - 1] = '\0'; |
| 66 | |
| 67 | return random_string; |
| 68 | } |
| 69 | |
| 70 | static void test_create_one_kernel_session(void) |
| 71 | { |
| 72 | kern = trace_kernel_create_session(); |
| 73 | ok(kern != NULL, "Create kernel session"); |
| 74 | |
| 75 | ok(kern->fd == -1 && |
| 76 | kern->metadata_stream_fd == -1 && |
| 77 | kern->consumer_fds_sent == 0 && |
| 78 | kern->channel_count == 0 && |
| 79 | kern->stream_count_global == 0 && |
| 80 | kern->metadata == NULL, |
| 81 | "Validate kernel session"); |
| 82 | } |
| 83 | |
| 84 | static void test_create_kernel_metadata(void) |
| 85 | { |
| 86 | assert(kern != NULL); |
| 87 | |
| 88 | kern->metadata = trace_kernel_create_metadata(); |
| 89 | ok(kern->metadata != NULL, "Create kernel metadata"); |
| 90 | |
| 91 | ok(kern->metadata->fd == -1 && |
| 92 | kern->metadata->conf != NULL && |
| 93 | kern->metadata->conf->attr.overwrite |
| 94 | == DEFAULT_CHANNEL_OVERWRITE && |
| 95 | kern->metadata->conf->attr.subbuf_size |
| 96 | == default_get_metadata_subbuf_size() && |
| 97 | kern->metadata->conf->attr.num_subbuf |
| 98 | == DEFAULT_METADATA_SUBBUF_NUM && |
| 99 | kern->metadata->conf->attr.switch_timer_interval |
| 100 | == DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER && |
| 101 | kern->metadata->conf->attr.read_timer_interval |
| 102 | == DEFAULT_KERNEL_CHANNEL_READ_TIMER && |
| 103 | kern->metadata->conf->attr.output |
| 104 | == DEFAULT_KERNEL_CHANNEL_OUTPUT, |
| 105 | "Validate kernel session metadata"); |
| 106 | |
| 107 | trace_kernel_destroy_metadata(kern->metadata); |
| 108 | } |
| 109 | |
| 110 | static void test_create_kernel_channel(void) |
| 111 | { |
| 112 | struct ltt_kernel_channel *chan; |
| 113 | struct lttng_channel attr; |
| 114 | |
| 115 | memset(&attr, 0, sizeof(attr)); |
| 116 | |
| 117 | chan = trace_kernel_create_channel(&attr); |
| 118 | ok(chan != NULL, "Create kernel channel"); |
| 119 | |
| 120 | ok(chan->fd == -1 && |
| 121 | chan->enabled == 1 && |
| 122 | chan->stream_count == 0 && |
| 123 | chan->channel->attr.overwrite == attr.attr.overwrite, |
| 124 | "Validate kernel channel"); |
| 125 | |
| 126 | /* Init list in order to avoid sefaults from cds_list_del */ |
| 127 | CDS_INIT_LIST_HEAD(&chan->list); |
| 128 | trace_kernel_destroy_channel(chan); |
| 129 | } |
| 130 | |
| 131 | static void test_create_kernel_event(void) |
| 132 | { |
| 133 | struct ltt_kernel_event *event; |
| 134 | struct lttng_event ev; |
| 135 | |
| 136 | memset(&ev, 0, sizeof(ev)); |
| 137 | ok(lttng_strncpy(ev.name, get_random_string(), |
| 138 | LTTNG_KERNEL_SYM_NAME_LEN), |
| 139 | "Validate string length"); |
| 140 | ev.type = LTTNG_EVENT_TRACEPOINT; |
| 141 | ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; |
| 142 | |
| 143 | event = trace_kernel_create_event(&ev, NULL, NULL); |
| 144 | ok(event != NULL, "Create kernel event"); |
| 145 | |
| 146 | ok(event->fd == -1 && |
| 147 | event->enabled == 1 && |
| 148 | event->event->instrumentation == LTTNG_KERNEL_TRACEPOINT && |
| 149 | strlen(event->event->name), |
| 150 | "Validate kernel event"); |
| 151 | |
| 152 | /* Init list in order to avoid sefaults from cds_list_del */ |
| 153 | CDS_INIT_LIST_HEAD(&event->list); |
| 154 | trace_kernel_destroy_event(event); |
| 155 | } |
| 156 | |
| 157 | static void test_create_kernel_stream(void) |
| 158 | { |
| 159 | struct ltt_kernel_stream *stream; |
| 160 | |
| 161 | stream = trace_kernel_create_stream("stream1", 0); |
| 162 | ok(stream != NULL, "Create kernel stream"); |
| 163 | |
| 164 | ok(stream->fd == -1 && |
| 165 | stream->state == 0, |
| 166 | "Validate kernel stream"); |
| 167 | |
| 168 | /* Init list in order to avoid sefaults from cds_list_del */ |
| 169 | CDS_INIT_LIST_HEAD(&stream->list); |
| 170 | trace_kernel_destroy_stream(stream); |
| 171 | } |
| 172 | |
| 173 | int main(int argc, char **argv) |
| 174 | { |
| 175 | plan_tests(NUM_TESTS); |
| 176 | |
| 177 | diag("Kernel data structure unit test"); |
| 178 | |
| 179 | test_create_one_kernel_session(); |
| 180 | test_create_kernel_metadata(); |
| 181 | test_create_kernel_channel(); |
| 182 | test_create_kernel_event(); |
| 183 | test_create_kernel_stream(); |
| 184 | |
| 185 | /* Success */ |
| 186 | return 0; |
| 187 | } |