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