Tests: Convert the session unit test output to TAP
[lttng-tools.git] / tests / unit / test_kernel_data.c
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
28 #include <bin/lttng-sessiond/trace-kernel.h>
29 #include <common/defaults.h>
30
31 #include "utils.h"
32
33 /* This path will NEVER be created in this test */
34 #define PATH1 "/tmp/.test-junk-lttng"
35
36 #define RANDOM_STRING_LEN 11
37
38 /* For lttngerr.h */
39 int lttng_opt_quiet = 1;
40 int lttng_opt_verbose;
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 create_one_kernel_session(void)
68 {
69 printf("Create kernel session: ");
70 kern = trace_kernel_create_session(PATH1);
71 assert(kern != NULL);
72 PRINT_OK();
73
74 printf("Validating kernel session: ");
75 assert(kern->fd == -1);
76 assert(kern->metadata_stream_fd == -1);
77 assert(kern->consumer_fds_sent == 0);
78 assert(kern->channel_count == 0);
79 assert(kern->stream_count_global == 0);
80 assert(kern->metadata == NULL);
81 PRINT_OK();
82
83 /* Init list in order to avoid sefaults from cds_list_del */
84 trace_kernel_destroy_session(kern);
85 }
86
87 static void create_kernel_metadata(void)
88 {
89 assert(kern != NULL);
90
91 printf("Create kernel metadata: ");
92 kern->metadata = trace_kernel_create_metadata();
93 assert(kern->metadata != NULL);
94 PRINT_OK();
95
96 printf("Validating kernel session metadata: ");
97 assert(kern->metadata->fd == -1);
98 assert(kern->metadata->conf != NULL);
99 assert(kern->metadata->conf->attr.overwrite
100 == DEFAULT_CHANNEL_OVERWRITE);
101 assert(kern->metadata->conf->attr.subbuf_size
102 == default_get_metadata_subbuf_size());
103 assert(kern->metadata->conf->attr.num_subbuf
104 == DEFAULT_METADATA_SUBBUF_NUM);
105 assert(kern->metadata->conf->attr.switch_timer_interval
106 == DEFAULT_CHANNEL_SWITCH_TIMER);
107 assert(kern->metadata->conf->attr.read_timer_interval
108 == DEFAULT_CHANNEL_READ_TIMER);
109 assert(kern->metadata->conf->attr.output
110 == DEFAULT_KERNEL_CHANNEL_OUTPUT);
111 PRINT_OK();
112
113 trace_kernel_destroy_metadata(kern->metadata);
114 }
115
116 static void create_kernel_channel(void)
117 {
118 struct ltt_kernel_channel *chan;
119 struct lttng_channel attr;
120
121 memset(&attr, 0, sizeof(attr));
122
123 printf("Creating kernel channel: ");
124 chan = trace_kernel_create_channel(&attr);
125 assert(chan != NULL);
126 PRINT_OK();
127
128 printf("Validating kernel channel: ");
129 assert(chan->fd == -1);
130 assert(chan->enabled == 1);
131 assert(chan->stream_count == 0);
132 assert(chan->ctx == NULL);
133 assert(chan->channel->attr.overwrite == attr.attr.overwrite);
134 PRINT_OK();
135
136 /* Init list in order to avoid sefaults from cds_list_del */
137 CDS_INIT_LIST_HEAD(&chan->list);
138 trace_kernel_destroy_channel(chan);
139 }
140
141 static void create_kernel_event(void)
142 {
143 struct ltt_kernel_event *event;
144 struct lttng_event ev;
145
146 memset(&ev, 0, sizeof(ev));
147 strncpy(ev.name, get_random_string(), LTTNG_KERNEL_SYM_NAME_LEN);
148 ev.type = LTTNG_EVENT_TRACEPOINT;
149 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
150
151 printf("Creating kernel event: ");
152 event = trace_kernel_create_event(&ev);
153 assert(event != NULL);
154 PRINT_OK();
155
156 printf("Validating kernel event: ");
157 assert(event->fd == -1);
158 assert(event->enabled == 1);
159 assert(event->event->instrumentation == LTTNG_KERNEL_TRACEPOINT);
160 assert(strlen(event->event->name));
161 PRINT_OK();
162
163 /* Init list in order to avoid sefaults from cds_list_del */
164 CDS_INIT_LIST_HEAD(&event->list);
165 trace_kernel_destroy_event(event);
166 }
167
168 static void create_kernel_stream(void)
169 {
170 struct ltt_kernel_stream *stream;
171
172 printf("Creating kernel stream: ");
173 stream = trace_kernel_create_stream("stream1", 0);
174 assert(stream != NULL);
175 PRINT_OK();
176
177 printf("Validating kernel stream: ");
178 assert(stream->fd == -1);
179 assert(stream->state == 0);
180 PRINT_OK();
181
182 /* Init list in order to avoid sefaults from cds_list_del */
183 CDS_INIT_LIST_HEAD(&stream->list);
184 trace_kernel_destroy_stream(stream);
185 }
186
187 int main(int argc, char **argv)
188 {
189 printf("\nTesting kernel data structures:\n-----------\n");
190
191 create_one_kernel_session();
192
193 create_kernel_metadata();
194 create_kernel_channel();
195
196
197 create_kernel_event();
198
199 create_kernel_stream();
200
201 /* Success */
202 return 0;
203 }
This page took 0.048113 seconds and 4 git commands to generate.