2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include <common/compat/errno.hpp>
9 #include <common/defaults.hpp>
11 #include <bin/lttng-sessiond/trace-kernel.hpp>
19 #define RANDOM_STRING_LEN 11
21 /* Number of TAP tests in this file */
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();
30 static const char alphanum
[] = "0123456789"
31 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
32 "abcdefghijklmnopqrstuvwxyz";
34 static struct ltt_kernel_session
*kern
;
35 static char random_string
[RANDOM_STRING_LEN
];
38 * Return random string of 10 characters.
41 static char *get_random_string()
45 for (i
= 0; i
< RANDOM_STRING_LEN
- 1; i
++) {
46 random_string
[i
] = alphanum
[rand() % (sizeof(alphanum
) - 1)];
49 random_string
[RANDOM_STRING_LEN
- 1] = '\0';
54 static void test_create_one_kernel_session()
56 kern
= trace_kernel_create_session();
57 ok(kern
!= nullptr, "Create kernel session");
60 skip(1, "Kernel session is null");
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");
69 static void test_create_kernel_metadata()
71 LTTNG_ASSERT(kern
!= nullptr);
73 kern
->metadata
= trace_kernel_create_metadata();
74 ok(kern
->metadata
!= nullptr, "Create kernel metadata");
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");
86 trace_kernel_destroy_metadata(kern
->metadata
);
89 static void test_create_kernel_channel()
91 struct ltt_kernel_channel
*chan
;
92 struct lttng_channel attr
;
93 struct lttng_channel_extended extended
;
95 memset(&attr
, 0, sizeof(attr
));
96 memset(&extended
, 0, sizeof(extended
));
97 attr
.attr
.extended
.ptr
= &extended
;
99 chan
= trace_kernel_create_channel(&attr
);
100 ok(chan
!= nullptr, "Create kernel channel");
103 skip(1, "Channel is null");
107 ok(chan
->fd
== -1 && chan
->enabled
&& chan
->stream_count
== 0 &&
108 chan
->channel
->attr
.overwrite
== attr
.attr
.overwrite
,
109 "Validate kernel channel");
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
);
116 static void test_create_kernel_event()
118 enum lttng_error_code ret
;
119 struct ltt_kernel_event
*event
;
120 struct lttng_event ev
;
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
;
128 ret
= trace_kernel_create_event(&ev
, nullptr, nullptr, &event
);
129 ok(ret
== LTTNG_OK
, "Create kernel event");
132 skip(1, "Event is null");
136 ok(event
->fd
== -1 && event
->enabled
&&
137 event
->event
->instrumentation
== LTTNG_KERNEL_ABI_TRACEPOINT
&&
138 strlen(event
->event
->name
),
139 "Validate kernel event");
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
);
146 static void test_create_kernel_stream()
148 struct ltt_kernel_stream
*stream
;
150 stream
= trace_kernel_create_stream("stream1", 0);
151 ok(stream
!= nullptr, "Create kernel stream");
154 skip(1, "Stream is null");
158 ok(stream
->fd
== -1 && stream
->state
== 0, "Validate kernel stream");
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
);
167 plan_tests(NUM_TESTS
);
169 diag("Kernel data structure unit test");
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();
177 return exit_status();