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