Add kernel trace data structure test
[lttng-tools.git] / tests / test_kernel_data_trace.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
28#include "ltt-sessiond/trace.h"
29#include "utils.h"
30
31/* This path will NEVER be created in this test */
32#define PATH1 "/tmp/.test-junk-lttng"
33
34/* For lttngerr.h */
35int opt_quiet = 1;
36int opt_verbose = 0;
37
38static const char alphanum[] =
39 "0123456789"
40 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
41 "abcdefghijklmnopqrstuvwxyz";
42
43static struct ltt_kernel_session *kern;
44
45/*
46 * Return random string of 10 characters.
47 */
48static char *get_random_string(void)
49{
50 int i;
51 char *str = malloc(11);
52
53 for (i = 0; i < 10; i++) {
54 str[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
55 }
56
57 str[10] = '\0';
58
59 return str;
60}
61
62static void create_one_kernel_session(void)
63{
64 printf("Create kernel session: ");
65 kern = trace_create_kernel_session();
66 assert(kern != NULL);
67 PRINT_OK();
68
69 printf("Validating kernel session: ");
70 assert(kern->fd == 0);
71 assert(kern->metadata_stream_fd == 0);
72 assert(kern->kconsumer_fds_sent == 0);
73 assert(kern->channel_count == 0);
74 assert(kern->stream_count_global == 0);
75 assert(kern->metadata == NULL);
76 PRINT_OK();
77
78 /* Init list in order to avoid sefaults from cds_list_del */
79 trace_destroy_kernel_session(kern);
80}
81
82static void create_kernel_metadata(void)
83{
84 assert(kern != NULL);
85
86 printf("Create kernel metadata: ");
87 kern->metadata = trace_create_kernel_metadata(PATH1);
88 assert(kern->metadata != NULL);
89 PRINT_OK();
90
91 printf("Validating kernel session metadata: ");
92 assert(kern->metadata->fd == 0);
93 assert(strlen(kern->metadata->pathname));
94 assert(kern->metadata->conf != NULL);
95 assert(kern->metadata->conf->attr.overwrite
96 == DEFAULT_CHANNEL_OVERWRITE);
97 assert(kern->metadata->conf->attr.subbuf_size
98 == DEFAULT_CHANNEL_SUBBUF_SIZE);
99 assert(kern->metadata->conf->attr.num_subbuf
100 == DEFAULT_CHANNEL_SUBBUF_NUM);
101 assert(kern->metadata->conf->attr.switch_timer_interval
102 == DEFAULT_CHANNEL_SWITCH_TIMER);
103 assert(kern->metadata->conf->attr.read_timer_interval
104 == DEFAULT_CHANNEL_READ_TIMER);
105 assert(kern->metadata->conf->attr.output
106 == DEFAULT_KERNEL_CHANNEL_OUTPUT);
107 PRINT_OK();
108
109 trace_destroy_kernel_metadata(kern->metadata);
110}
111
112static void create_kernel_channel(void)
113{
114 struct ltt_kernel_channel *chan;
115 struct lttng_channel attr;
116
117 printf("Creating kernel channel: ");
118 chan = trace_create_kernel_channel(&attr, PATH1);
119 assert(chan != NULL);
120 PRINT_OK();
121
122 printf("Validating kernel channel: ");
123 assert(chan->fd == 0);
124 assert(chan->enabled == 1);
125 assert(strcmp(PATH1, chan->pathname) == 0);
126 assert(chan->stream_count == 0);
127 assert(chan->ctx == NULL);
128 assert(chan->channel->attr.overwrite == attr.attr.overwrite);
129 PRINT_OK();
130
131 /* Init list in order to avoid sefaults from cds_list_del */
132 CDS_INIT_LIST_HEAD(&chan->list);
133 trace_destroy_kernel_channel(chan);
134}
135
136static void create_kernel_event(void)
137{
138 struct ltt_kernel_event *event;
139 struct lttng_event ev;
140
141 strncpy(ev.name, get_random_string(), LTTNG_SYM_NAME_LEN);
142 ev.type = LTTNG_EVENT_TRACEPOINT;
143
144 printf("Creating kernel event: ");
145 event = trace_create_kernel_event(&ev);
146 assert(event != NULL);
147 PRINT_OK();
148
149 printf("Validating kernel event: ");
150 assert(event->fd == 0);
151 assert(event->enabled == 1);
152 assert(event->ctx == NULL);
153 assert(event->event->instrumentation == LTTNG_KERNEL_TRACEPOINT);
154 assert(strlen(event->event->name));
155 PRINT_OK();
156
157 /* Init list in order to avoid sefaults from cds_list_del */
158 CDS_INIT_LIST_HEAD(&event->list);
159 trace_destroy_kernel_event(event);
160}
161
162static void create_kernel_stream(void)
163{
164 struct ltt_kernel_stream *stream;
165
166 printf("Creating kernel stream: ");
167 stream = trace_create_kernel_stream();
168 assert(stream != NULL);
169 PRINT_OK();
170
171 printf("Validating kernel stream: ");
172 assert(stream->fd == 0);
173 assert(stream->pathname == NULL);
174 assert(stream->state == 0);
175 PRINT_OK();
176
177 /* Init list in order to avoid sefaults from cds_list_del */
178 CDS_INIT_LIST_HEAD(&stream->list);
179 trace_destroy_kernel_stream(stream);
180}
181
182int main(int argc, char **argv)
183{
184 printf("\nTesting kernel data structures:\n-----------\n");
185
186 create_one_kernel_session();
187
188 create_kernel_metadata();
189 create_kernel_channel();
190
191
192 create_kernel_event();
193
194 create_kernel_stream();
195
196 /* Success */
197 return 0;
198}
This page took 0.051524 seconds and 4 git commands to generate.