| 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 | static const char alphanum[] = |
| 43 | "0123456789" |
| 44 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 45 | "abcdefghijklmnopqrstuvwxyz"; |
| 46 | |
| 47 | static struct ltt_kernel_session *kern; |
| 48 | static char random_string[RANDOM_STRING_LEN]; |
| 49 | |
| 50 | /* |
| 51 | * Return random string of 10 characters. |
| 52 | * Not thread-safe. |
| 53 | */ |
| 54 | static char *get_random_string(void) |
| 55 | { |
| 56 | int i; |
| 57 | |
| 58 | for (i = 0; i < RANDOM_STRING_LEN - 1; i++) { |
| 59 | random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)]; |
| 60 | } |
| 61 | |
| 62 | random_string[RANDOM_STRING_LEN - 1] = '\0'; |
| 63 | |
| 64 | return random_string; |
| 65 | } |
| 66 | |
| 67 | static void test_create_one_kernel_session(void) |
| 68 | { |
| 69 | kern = trace_kernel_create_session(); |
| 70 | ok(kern != NULL, "Create kernel session"); |
| 71 | |
| 72 | if (!kern) { |
| 73 | skip(1, "Kernel session is null"); |
| 74 | return; |
| 75 | } |
| 76 | ok(kern->fd == -1 && |
| 77 | kern->metadata_stream_fd == -1 && |
| 78 | kern->consumer_fds_sent == 0 && |
| 79 | kern->channel_count == 0 && |
| 80 | kern->stream_count_global == 0 && |
| 81 | kern->metadata == NULL, |
| 82 | "Validate kernel session"); |
| 83 | } |
| 84 | |
| 85 | static void test_create_kernel_metadata(void) |
| 86 | { |
| 87 | assert(kern != NULL); |
| 88 | |
| 89 | kern->metadata = trace_kernel_create_metadata(); |
| 90 | ok(kern->metadata != NULL, "Create kernel metadata"); |
| 91 | |
| 92 | ok(kern->metadata->fd == -1 && |
| 93 | kern->metadata->conf != NULL && |
| 94 | kern->metadata->conf->attr.overwrite |
| 95 | == DEFAULT_CHANNEL_OVERWRITE && |
| 96 | kern->metadata->conf->attr.subbuf_size |
| 97 | == default_get_metadata_subbuf_size() && |
| 98 | kern->metadata->conf->attr.num_subbuf |
| 99 | == DEFAULT_METADATA_SUBBUF_NUM && |
| 100 | kern->metadata->conf->attr.switch_timer_interval |
| 101 | == DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER && |
| 102 | kern->metadata->conf->attr.read_timer_interval |
| 103 | == DEFAULT_KERNEL_CHANNEL_READ_TIMER && |
| 104 | kern->metadata->conf->attr.output |
| 105 | == DEFAULT_KERNEL_CHANNEL_OUTPUT, |
| 106 | "Validate kernel session metadata"); |
| 107 | |
| 108 | trace_kernel_destroy_metadata(kern->metadata); |
| 109 | } |
| 110 | |
| 111 | static void test_create_kernel_channel(void) |
| 112 | { |
| 113 | struct ltt_kernel_channel *chan; |
| 114 | struct lttng_channel attr; |
| 115 | struct lttng_channel_extended extended; |
| 116 | |
| 117 | memset(&attr, 0, sizeof(attr)); |
| 118 | memset(&extended, 0, sizeof(extended)); |
| 119 | attr.attr.extended.ptr = &extended; |
| 120 | |
| 121 | chan = trace_kernel_create_channel(&attr); |
| 122 | ok(chan != NULL, "Create kernel channel"); |
| 123 | |
| 124 | if (!chan) { |
| 125 | skip(1, "Channel is null"); |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | ok(chan->fd == -1 && |
| 130 | chan->enabled == 1 && |
| 131 | chan->stream_count == 0 && |
| 132 | chan->channel->attr.overwrite == attr.attr.overwrite, |
| 133 | "Validate kernel channel"); |
| 134 | |
| 135 | /* Init list in order to avoid sefaults from cds_list_del */ |
| 136 | CDS_INIT_LIST_HEAD(&chan->list); |
| 137 | trace_kernel_destroy_channel(chan); |
| 138 | } |
| 139 | |
| 140 | static void test_create_kernel_event(void) |
| 141 | { |
| 142 | enum lttng_error_code ret; |
| 143 | struct ltt_kernel_event *event; |
| 144 | struct lttng_event ev; |
| 145 | |
| 146 | memset(&ev, 0, sizeof(ev)); |
| 147 | ok(!lttng_strncpy(ev.name, get_random_string(), |
| 148 | LTTNG_KERNEL_SYM_NAME_LEN), |
| 149 | "Validate string length"); |
| 150 | ev.type = LTTNG_EVENT_TRACEPOINT; |
| 151 | ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; |
| 152 | |
| 153 | ret = trace_kernel_create_event(&ev, NULL, NULL, &event); |
| 154 | ok(ret == LTTNG_OK, "Create kernel event"); |
| 155 | |
| 156 | if (!event) { |
| 157 | skip(1, "Event is null"); |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | ok(event->fd == -1 && |
| 162 | event->enabled == 1 && |
| 163 | event->event->instrumentation == LTTNG_KERNEL_TRACEPOINT && |
| 164 | strlen(event->event->name), |
| 165 | "Validate kernel event"); |
| 166 | |
| 167 | /* Init list in order to avoid sefaults from cds_list_del */ |
| 168 | CDS_INIT_LIST_HEAD(&event->list); |
| 169 | trace_kernel_destroy_event(event); |
| 170 | } |
| 171 | |
| 172 | static void test_create_kernel_stream(void) |
| 173 | { |
| 174 | struct ltt_kernel_stream *stream; |
| 175 | |
| 176 | stream = trace_kernel_create_stream("stream1", 0); |
| 177 | ok(stream != NULL, "Create kernel stream"); |
| 178 | |
| 179 | if (!stream) { |
| 180 | skip(1, "Stream is null"); |
| 181 | return; |
| 182 | } |
| 183 | |
| 184 | ok(stream->fd == -1 && |
| 185 | stream->state == 0, |
| 186 | "Validate kernel stream"); |
| 187 | |
| 188 | /* Init list in order to avoid sefaults from cds_list_del */ |
| 189 | CDS_INIT_LIST_HEAD(&stream->list); |
| 190 | trace_kernel_destroy_stream(stream); |
| 191 | } |
| 192 | |
| 193 | int main(int argc, char **argv) |
| 194 | { |
| 195 | plan_tests(NUM_TESTS); |
| 196 | |
| 197 | diag("Kernel data structure unit test"); |
| 198 | |
| 199 | test_create_one_kernel_session(); |
| 200 | test_create_kernel_metadata(); |
| 201 | test_create_kernel_channel(); |
| 202 | test_create_kernel_event(); |
| 203 | test_create_kernel_stream(); |
| 204 | |
| 205 | /* Success */ |
| 206 | return 0; |
| 207 | } |