Tests: Convert the session unit test output to TAP
[lttng-tools.git] / tests / unit / test_kernel_data.c
CommitLineData
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
897b8e23
DG
31#include "utils.h"
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
897b8e23 38/* For lttngerr.h */
97e19046
DG
39int lttng_opt_quiet = 1;
40int lttng_opt_verbose;
897b8e23
DG
41
42static const char alphanum[] =
43 "0123456789"
44 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
45 "abcdefghijklmnopqrstuvwxyz";
46
47static struct ltt_kernel_session *kern;
98612240 48static char random_string[RANDOM_STRING_LEN];
897b8e23
DG
49
50/*
51 * Return random string of 10 characters.
98612240 52 * Not thread-safe.
897b8e23
DG
53 */
54static char *get_random_string(void)
55{
56 int i;
897b8e23 57
98612240
MD
58 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
59 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
897b8e23
DG
60 }
61
98612240 62 random_string[RANDOM_STRING_LEN - 1] = '\0';
897b8e23 63
98612240 64 return random_string;
897b8e23
DG
65}
66
67static void create_one_kernel_session(void)
68{
69 printf("Create kernel session: ");
f9815039 70 kern = trace_kernel_create_session(PATH1);
897b8e23
DG
71 assert(kern != NULL);
72 PRINT_OK();
73
74 printf("Validating kernel session: ");
03550b58
MD
75 assert(kern->fd == -1);
76 assert(kern->metadata_stream_fd == -1);
3bd1e081 77 assert(kern->consumer_fds_sent == 0);
897b8e23
DG
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 */
3f43a221 84 trace_kernel_destroy_session(kern);
897b8e23
DG
85}
86
87static void create_kernel_metadata(void)
88{
89 assert(kern != NULL);
90
91 printf("Create kernel metadata: ");
a4b92340 92 kern->metadata = trace_kernel_create_metadata();
897b8e23
DG
93 assert(kern->metadata != NULL);
94 PRINT_OK();
95
96 printf("Validating kernel session metadata: ");
03550b58 97 assert(kern->metadata->fd == -1);
897b8e23
DG
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
3e230f92 102 == default_get_metadata_subbuf_size());
897b8e23 103 assert(kern->metadata->conf->attr.num_subbuf
b389abbe 104 == DEFAULT_METADATA_SUBBUF_NUM);
897b8e23
DG
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
3f43a221 113 trace_kernel_destroy_metadata(kern->metadata);
897b8e23
DG
114}
115
116static void create_kernel_channel(void)
117{
118 struct ltt_kernel_channel *chan;
119 struct lttng_channel attr;
120
441c16a7
MD
121 memset(&attr, 0, sizeof(attr));
122
897b8e23 123 printf("Creating kernel channel: ");
fdd9eb17 124 chan = trace_kernel_create_channel(&attr);
897b8e23
DG
125 assert(chan != NULL);
126 PRINT_OK();
127
128 printf("Validating kernel channel: ");
03550b58 129 assert(chan->fd == -1);
897b8e23 130 assert(chan->enabled == 1);
897b8e23
DG
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);
3f43a221 138 trace_kernel_destroy_channel(chan);
897b8e23
DG
139}
140
141static void create_kernel_event(void)
142{
143 struct ltt_kernel_event *event;
144 struct lttng_event ev;
145
441c16a7 146 memset(&ev, 0, sizeof(ev));
dbbb3ec5 147 strncpy(ev.name, get_random_string(), LTTNG_KERNEL_SYM_NAME_LEN);
897b8e23 148 ev.type = LTTNG_EVENT_TRACEPOINT;
441c16a7 149 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
897b8e23
DG
150
151 printf("Creating kernel event: ");
3f43a221 152 event = trace_kernel_create_event(&ev);
897b8e23
DG
153 assert(event != NULL);
154 PRINT_OK();
155
156 printf("Validating kernel event: ");
03550b58 157 assert(event->fd == -1);
897b8e23 158 assert(event->enabled == 1);
897b8e23
DG
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);
3f43a221 165 trace_kernel_destroy_event(event);
897b8e23
DG
166}
167
168static void create_kernel_stream(void)
169{
170 struct ltt_kernel_stream *stream;
171
172 printf("Creating kernel stream: ");
00e2e675 173 stream = trace_kernel_create_stream("stream1", 0);
897b8e23
DG
174 assert(stream != NULL);
175 PRINT_OK();
176
177 printf("Validating kernel stream: ");
03550b58 178 assert(stream->fd == -1);
897b8e23
DG
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);
3f43a221 184 trace_kernel_destroy_stream(stream);
897b8e23
DG
185}
186
187int 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.062693 seconds and 4 git commands to generate.