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