c49d7ce549795000753b029e3256b4aeaafd2f0d
[lttng-tools.git] / tests / tools / 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/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_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, PATH1);
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->ctx == NULL);
160 assert(event->event->instrumentation == LTTNG_KERNEL_TRACEPOINT);
161 assert(strlen(event->event->name));
162 PRINT_OK();
163
164 /* Init list in order to avoid sefaults from cds_list_del */
165 CDS_INIT_LIST_HEAD(&event->list);
166 trace_kernel_destroy_event(event);
167 }
168
169 static void create_kernel_stream(void)
170 {
171 struct ltt_kernel_stream *stream;
172
173 printf("Creating kernel stream: ");
174 stream = trace_kernel_create_stream("stream1", 0);
175 assert(stream != NULL);
176 PRINT_OK();
177
178 printf("Validating kernel stream: ");
179 assert(stream->fd == -1);
180 assert(stream->state == 0);
181 PRINT_OK();
182
183 /* Init list in order to avoid sefaults from cds_list_del */
184 CDS_INIT_LIST_HEAD(&stream->list);
185 trace_kernel_destroy_stream(stream);
186 }
187
188 int main(int argc, char **argv)
189 {
190 printf("\nTesting kernel data structures:\n-----------\n");
191
192 create_one_kernel_session();
193
194 create_kernel_metadata();
195 create_kernel_channel();
196
197
198 create_kernel_event();
199
200 create_kernel_stream();
201
202 /* Success */
203 return 0;
204 }
This page took 0.032622 seconds and 3 git commands to generate.