Tests: Fix: Use '.logfile' instead of '.log' for test app output
[lttng-tools.git] / tests / unit / test_kernel_data.cpp
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #include <common/compat/errno.hpp>
9 #include <common/defaults.hpp>
10
11 #include <bin/lttng-sessiond/trace-kernel.hpp>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <tap/tap.h>
16 #include <time.h>
17 #include <unistd.h>
18
19 #define RANDOM_STRING_LEN 11
20
21 /* Number of TAP tests in this file */
22 #define NUM_TESTS 11
23
24 #ifdef HAVE_LIBLTTNG_UST_CTL
25 #include <lttng/lttng-export.h>
26 #include <lttng/ust-sigbus.h>
27 LTTNG_EXPORT DEFINE_LTTNG_UST_SIGBUS_STATE();
28 #endif
29
30 static const char alphanum[] = "0123456789"
31 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
32 "abcdefghijklmnopqrstuvwxyz";
33
34 static struct ltt_kernel_session *kern;
35 static char random_string[RANDOM_STRING_LEN];
36
37 /*
38 * Return random string of 10 characters.
39 * Not thread-safe.
40 */
41 static char *get_random_string()
42 {
43 int i;
44
45 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
46 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
47 }
48
49 random_string[RANDOM_STRING_LEN - 1] = '\0';
50
51 return random_string;
52 }
53
54 static void test_create_one_kernel_session()
55 {
56 kern = trace_kernel_create_session();
57 ok(kern != nullptr, "Create kernel session");
58
59 if (!kern) {
60 skip(1, "Kernel session is null");
61 return;
62 }
63 ok(kern->fd == -1 && kern->metadata_stream_fd == -1 && kern->consumer_fds_sent == 0 &&
64 kern->channel_count == 0 && kern->stream_count_global == 0 &&
65 kern->metadata == nullptr,
66 "Validate kernel session");
67 }
68
69 static void test_create_kernel_metadata()
70 {
71 LTTNG_ASSERT(kern != nullptr);
72
73 kern->metadata = trace_kernel_create_metadata();
74 ok(kern->metadata != nullptr, "Create kernel metadata");
75
76 ok(kern->metadata->fd == -1 && kern->metadata->conf != nullptr &&
77 kern->metadata->conf->attr.overwrite == DEFAULT_METADATA_OVERWRITE &&
78 kern->metadata->conf->attr.subbuf_size == default_get_metadata_subbuf_size() &&
79 kern->metadata->conf->attr.num_subbuf == DEFAULT_METADATA_SUBBUF_NUM &&
80 kern->metadata->conf->attr.switch_timer_interval ==
81 DEFAULT_METADATA_SWITCH_TIMER &&
82 kern->metadata->conf->attr.read_timer_interval == DEFAULT_METADATA_READ_TIMER &&
83 kern->metadata->conf->attr.output == LTTNG_EVENT_MMAP,
84 "Validate kernel session metadata");
85
86 trace_kernel_destroy_metadata(kern->metadata);
87 }
88
89 static void test_create_kernel_channel()
90 {
91 struct ltt_kernel_channel *chan;
92 struct lttng_channel attr;
93 struct lttng_channel_extended extended;
94
95 memset(&attr, 0, sizeof(attr));
96 memset(&extended, 0, sizeof(extended));
97 attr.attr.extended.ptr = &extended;
98
99 chan = trace_kernel_create_channel(&attr);
100 ok(chan != nullptr, "Create kernel channel");
101
102 if (!chan) {
103 skip(1, "Channel is null");
104 return;
105 }
106
107 ok(chan->fd == -1 && chan->enabled && chan->stream_count == 0 &&
108 chan->channel->attr.overwrite == attr.attr.overwrite,
109 "Validate kernel channel");
110
111 /* Init list in order to avoid sefaults from cds_list_del */
112 CDS_INIT_LIST_HEAD(&chan->list);
113 trace_kernel_destroy_channel(chan);
114 }
115
116 static void test_create_kernel_event()
117 {
118 enum lttng_error_code ret;
119 struct ltt_kernel_event *event;
120 struct lttng_event ev;
121
122 memset(&ev, 0, sizeof(ev));
123 ok(!lttng_strncpy(ev.name, get_random_string(), RANDOM_STRING_LEN),
124 "Validate string length");
125 ev.type = LTTNG_EVENT_TRACEPOINT;
126 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
127
128 ret = trace_kernel_create_event(&ev, nullptr, nullptr, &event);
129 ok(ret == LTTNG_OK, "Create kernel event");
130
131 if (!event) {
132 skip(1, "Event is null");
133 return;
134 }
135
136 ok(event->fd == -1 && event->enabled &&
137 event->event->instrumentation == LTTNG_KERNEL_ABI_TRACEPOINT &&
138 strlen(event->event->name),
139 "Validate kernel event");
140
141 /* Init list in order to avoid sefaults from cds_list_del */
142 CDS_INIT_LIST_HEAD(&event->list);
143 trace_kernel_destroy_event(event);
144 }
145
146 static void test_create_kernel_stream()
147 {
148 struct ltt_kernel_stream *stream;
149
150 stream = trace_kernel_create_stream("stream1", 0);
151 ok(stream != nullptr, "Create kernel stream");
152
153 if (!stream) {
154 skip(1, "Stream is null");
155 return;
156 }
157
158 ok(stream->fd == -1 && stream->state == 0, "Validate kernel stream");
159
160 /* Init list in order to avoid sefaults from cds_list_del */
161 CDS_INIT_LIST_HEAD(&stream->list);
162 trace_kernel_destroy_stream(stream);
163 }
164
165 int main()
166 {
167 plan_tests(NUM_TESTS);
168
169 diag("Kernel data structure unit test");
170
171 test_create_one_kernel_session();
172 test_create_kernel_metadata();
173 test_create_kernel_channel();
174 test_create_kernel_event();
175 test_create_kernel_stream();
176
177 return exit_status();
178 }
This page took 0.032199 seconds and 4 git commands to generate.