Introduce emission site for syscall event rule
[lttng-tools.git] / tests / unit / test_kernel_data.c
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 8#include <assert.h>
897b8e23
DG
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <unistd.h>
13#include <time.h>
14
edf4b93e 15#include <common/compat/errno.h>
10a8a223 16#include <bin/lttng-sessiond/trace-kernel.h>
990570ed 17#include <common/defaults.h>
10a8a223 18
23aaa19e 19#include <tap/tap.h>
897b8e23 20
98612240
MD
21#define RANDOM_STRING_LEN 11
22
23aaa19e 23/* Number of TAP tests in this file */
1c0733db 24#define NUM_TESTS 11
23aaa19e 25
ad7c9c18 26/* For error.h */
97e19046
DG
27int lttng_opt_quiet = 1;
28int lttng_opt_verbose;
c7e35b03 29int lttng_opt_mi;
7972aab2 30
897b8e23
DG
31static const char alphanum[] =
32 "0123456789"
33 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
34 "abcdefghijklmnopqrstuvwxyz";
35
36static struct ltt_kernel_session *kern;
98612240 37static char random_string[RANDOM_STRING_LEN];
897b8e23
DG
38
39/*
40 * Return random string of 10 characters.
98612240 41 * Not thread-safe.
897b8e23
DG
42 */
43static char *get_random_string(void)
44{
45 int i;
897b8e23 46
98612240
MD
47 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
48 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
897b8e23
DG
49 }
50
98612240 51 random_string[RANDOM_STRING_LEN - 1] = '\0';
897b8e23 52
98612240 53 return random_string;
897b8e23
DG
54}
55
23aaa19e 56static void test_create_one_kernel_session(void)
897b8e23 57{
dec56f6c 58 kern = trace_kernel_create_session();
23aaa19e 59 ok(kern != NULL, "Create kernel session");
897b8e23 60
84a7eb73
JR
61 if (!kern) {
62 skip(1, "Kernel session is null");
63 return;
64 }
23aaa19e
CB
65 ok(kern->fd == -1 &&
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");
897b8e23
DG
72}
73
23aaa19e 74static void test_create_kernel_metadata(void)
897b8e23
DG
75{
76 assert(kern != NULL);
77
a4b92340 78 kern->metadata = trace_kernel_create_metadata();
23aaa19e
CB
79 ok(kern->metadata != NULL, "Create kernel metadata");
80
81 ok(kern->metadata->fd == -1 &&
82 kern->metadata->conf != NULL &&
83 kern->metadata->conf->attr.overwrite
d42266a4 84 == DEFAULT_METADATA_OVERWRITE &&
23aaa19e
CB
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
d42266a4 90 == DEFAULT_METADATA_SWITCH_TIMER &&
23aaa19e 91 kern->metadata->conf->attr.read_timer_interval
d42266a4 92 == DEFAULT_METADATA_READ_TIMER &&
23aaa19e 93 kern->metadata->conf->attr.output
d42266a4 94 == LTTNG_EVENT_MMAP,
23aaa19e 95 "Validate kernel session metadata");
897b8e23 96
3f43a221 97 trace_kernel_destroy_metadata(kern->metadata);
897b8e23
DG
98}
99
23aaa19e 100static void test_create_kernel_channel(void)
897b8e23
DG
101{
102 struct ltt_kernel_channel *chan;
103 struct lttng_channel attr;
63aaa3dc 104 struct lttng_channel_extended extended;
897b8e23 105
441c16a7 106 memset(&attr, 0, sizeof(attr));
63aaa3dc
JG
107 memset(&extended, 0, sizeof(extended));
108 attr.attr.extended.ptr = &extended;
441c16a7 109
fdd9eb17 110 chan = trace_kernel_create_channel(&attr);
23aaa19e 111 ok(chan != NULL, "Create kernel channel");
897b8e23 112
84a7eb73
JR
113 if (!chan) {
114 skip(1, "Channel is null");
115 return;
116 }
117
23aaa19e
CB
118 ok(chan->fd == -1 &&
119 chan->enabled == 1 &&
120 chan->stream_count == 0 &&
23aaa19e
CB
121 chan->channel->attr.overwrite == attr.attr.overwrite,
122 "Validate kernel channel");
897b8e23
DG
123
124 /* Init list in order to avoid sefaults from cds_list_del */
125 CDS_INIT_LIST_HEAD(&chan->list);
3f43a221 126 trace_kernel_destroy_channel(chan);
897b8e23
DG
127}
128
23aaa19e 129static void test_create_kernel_event(void)
897b8e23 130{
71a3bb01 131 enum lttng_error_code ret;
897b8e23
DG
132 struct ltt_kernel_event *event;
133 struct lttng_event ev;
134
441c16a7 135 memset(&ev, 0, sizeof(ev));
72265d8a 136 ok(!lttng_strncpy(ev.name, get_random_string(),
5961b0b5 137 RANDOM_STRING_LEN),
1c0733db 138 "Validate string length");
897b8e23 139 ev.type = LTTNG_EVENT_TRACEPOINT;
441c16a7 140 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
897b8e23 141
71a3bb01
FD
142 ret = trace_kernel_create_event(&ev, NULL, NULL, &event);
143 ok(ret == LTTNG_OK, "Create kernel event");
897b8e23 144
84a7eb73
JR
145 if (!event) {
146 skip(1, "Event is null");
147 return;
148 }
149
23aaa19e
CB
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");
897b8e23
DG
155
156 /* Init list in order to avoid sefaults from cds_list_del */
157 CDS_INIT_LIST_HEAD(&event->list);
3f43a221 158 trace_kernel_destroy_event(event);
897b8e23
DG
159}
160
23aaa19e 161static void test_create_kernel_stream(void)
897b8e23
DG
162{
163 struct ltt_kernel_stream *stream;
164
00e2e675 165 stream = trace_kernel_create_stream("stream1", 0);
23aaa19e 166 ok(stream != NULL, "Create kernel stream");
897b8e23 167
84a7eb73
JR
168 if (!stream) {
169 skip(1, "Stream is null");
170 return;
171 }
172
23aaa19e
CB
173 ok(stream->fd == -1 &&
174 stream->state == 0,
175 "Validate kernel stream");
897b8e23
DG
176
177 /* Init list in order to avoid sefaults from cds_list_del */
178 CDS_INIT_LIST_HEAD(&stream->list);
3f43a221 179 trace_kernel_destroy_stream(stream);
897b8e23
DG
180}
181
182int main(int argc, char **argv)
183{
23aaa19e 184 plan_tests(NUM_TESTS);
897b8e23 185
e3bef725
CB
186 diag("Kernel data structure unit test");
187
23aaa19e
CB
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();
897b8e23 193
674c3e2c 194 return exit_status();
897b8e23 195}
This page took 0.053754 seconds and 4 git commands to generate.