2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * SPDX-License-Identifier: GPL-2.0-only
15 #include <common/compat/errno.h>
16 #include <bin/lttng-sessiond/trace-kernel.h>
17 #include <common/defaults.h>
21 #define RANDOM_STRING_LEN 11
23 /* Number of TAP tests in this file */
27 int lttng_opt_quiet
= 1;
28 int lttng_opt_verbose
;
31 static const char alphanum
[] =
33 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
34 "abcdefghijklmnopqrstuvwxyz";
36 static struct ltt_kernel_session
*kern
;
37 static char random_string
[RANDOM_STRING_LEN
];
40 * Return random string of 10 characters.
43 static char *get_random_string(void)
47 for (i
= 0; i
< RANDOM_STRING_LEN
- 1; i
++) {
48 random_string
[i
] = alphanum
[rand() % (sizeof(alphanum
) - 1)];
51 random_string
[RANDOM_STRING_LEN
- 1] = '\0';
56 static void test_create_one_kernel_session(void)
58 kern
= trace_kernel_create_session();
59 ok(kern
!= NULL
, "Create kernel session");
62 skip(1, "Kernel session is null");
66 kern
->metadata_stream_fd
== -1 &&
67 kern
->consumer_fds_sent
== 0 &&
68 kern
->channel_count
== 0 &&
69 kern
->stream_count_global
== 0 &&
70 kern
->metadata
== NULL
,
71 "Validate kernel session");
74 static void test_create_kernel_metadata(void)
78 kern
->metadata
= trace_kernel_create_metadata();
79 ok(kern
->metadata
!= NULL
, "Create kernel metadata");
81 ok(kern
->metadata
->fd
== -1 &&
82 kern
->metadata
->conf
!= NULL
&&
83 kern
->metadata
->conf
->attr
.overwrite
84 == DEFAULT_METADATA_OVERWRITE
&&
85 kern
->metadata
->conf
->attr
.subbuf_size
86 == default_get_metadata_subbuf_size() &&
87 kern
->metadata
->conf
->attr
.num_subbuf
88 == DEFAULT_METADATA_SUBBUF_NUM
&&
89 kern
->metadata
->conf
->attr
.switch_timer_interval
90 == DEFAULT_METADATA_SWITCH_TIMER
&&
91 kern
->metadata
->conf
->attr
.read_timer_interval
92 == DEFAULT_METADATA_READ_TIMER
&&
93 kern
->metadata
->conf
->attr
.output
95 "Validate kernel session metadata");
97 trace_kernel_destroy_metadata(kern
->metadata
);
100 static void test_create_kernel_channel(void)
102 struct ltt_kernel_channel
*chan
;
103 struct lttng_channel attr
;
104 struct lttng_channel_extended extended
;
106 memset(&attr
, 0, sizeof(attr
));
107 memset(&extended
, 0, sizeof(extended
));
108 attr
.attr
.extended
.ptr
= &extended
;
110 chan
= trace_kernel_create_channel(&attr
);
111 ok(chan
!= NULL
, "Create kernel channel");
114 skip(1, "Channel is null");
119 chan
->enabled
== 1 &&
120 chan
->stream_count
== 0 &&
121 chan
->channel
->attr
.overwrite
== attr
.attr
.overwrite
,
122 "Validate kernel channel");
124 /* Init list in order to avoid sefaults from cds_list_del */
125 CDS_INIT_LIST_HEAD(&chan
->list
);
126 trace_kernel_destroy_channel(chan
);
129 static void test_create_kernel_event(void)
131 enum lttng_error_code ret
;
132 struct ltt_kernel_event
*event
;
133 struct lttng_event ev
;
135 memset(&ev
, 0, sizeof(ev
));
136 ok(!lttng_strncpy(ev
.name
, get_random_string(),
138 "Validate string length");
139 ev
.type
= LTTNG_EVENT_TRACEPOINT
;
140 ev
.loglevel_type
= LTTNG_EVENT_LOGLEVEL_ALL
;
142 ret
= trace_kernel_create_event(&ev
, NULL
, NULL
, &event
);
143 ok(ret
== LTTNG_OK
, "Create kernel event");
146 skip(1, "Event is null");
150 ok(event
->fd
== -1 &&
151 event
->enabled
== 1 &&
152 event
->event
->instrumentation
== LTTNG_KERNEL_TRACEPOINT
&&
153 strlen(event
->event
->name
),
154 "Validate kernel event");
156 /* Init list in order to avoid sefaults from cds_list_del */
157 CDS_INIT_LIST_HEAD(&event
->list
);
158 trace_kernel_destroy_event(event
);
161 static void test_create_kernel_stream(void)
163 struct ltt_kernel_stream
*stream
;
165 stream
= trace_kernel_create_stream("stream1", 0);
166 ok(stream
!= NULL
, "Create kernel stream");
169 skip(1, "Stream is null");
173 ok(stream
->fd
== -1 &&
175 "Validate kernel stream");
177 /* Init list in order to avoid sefaults from cds_list_del */
178 CDS_INIT_LIST_HEAD(&stream
->list
);
179 trace_kernel_destroy_stream(stream
);
182 int main(int argc
, char **argv
)
184 plan_tests(NUM_TESTS
);
186 diag("Kernel data structure unit test");
188 test_create_one_kernel_session();
189 test_create_kernel_metadata();
190 test_create_kernel_channel();
191 test_create_kernel_event();
192 test_create_kernel_stream();
194 return exit_status();