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