fix: warning '-Wstringop-truncation' with GCC 11.2
[lttng-tools.git] / tests / unit / test_ust_data.cpp
CommitLineData
d3e8f6bb 1/*
21cf9b6b 2 * Copyright (C) 2011 EfficiOS Inc.
d3e8f6bb 3 *
9d16b343 4 * SPDX-License-Identifier: GPL-2.0-only
d3e8f6bb 5 *
d3e8f6bb
DG
6 */
7
d3e8f6bb
DG
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11#include <unistd.h>
12#include <time.h>
8273250b 13#include <urcu.h>
d3e8f6bb 14
10a8a223
DG
15#include <lttng/lttng.h>
16#include <bin/lttng-sessiond/lttng-ust-abi.h>
990570ed 17#include <common/defaults.h>
edf4b93e 18#include <common/compat/errno.h>
10a8a223 19#include <bin/lttng-sessiond/trace-ust.h>
7972aab2 20#include <bin/lttng-sessiond/ust-app.h>
83b45089 21#include <bin/lttng-sessiond/notification-thread.h>
10a8a223 22
881fc67f
MD
23#include <lttng/ust-sigbus.h>
24
657270a4
CB
25#include <tap/tap.h>
26
d3e8f6bb
DG
27/* This path will NEVER be created in this test */
28#define PATH1 "/tmp/.test-junk-lttng"
29
98612240
MD
30#define RANDOM_STRING_LEN 11
31
657270a4 32/* Number of TAP tests in this file */
1cda50f2 33#define NUM_TESTS 16
657270a4 34
4bd69c5f 35LTTNG_EXPORT DEFINE_LTTNG_UST_SIGBUS_STATE();
881fc67f 36
d3e8f6bb
DG
37static const char alphanum[] =
38 "0123456789"
39 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
40 "abcdefghijklmnopqrstuvwxyz";
98612240 41static char random_string[RANDOM_STRING_LEN];
d3e8f6bb 42
d3e8f6bb
DG
43/*
44 * Return random string of 10 characters.
98612240 45 * Not thread-safe.
d3e8f6bb
DG
46 */
47static char *get_random_string(void)
48{
49 int i;
d3e8f6bb 50
98612240
MD
51 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
52 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
d3e8f6bb
DG
53 }
54
98612240 55 random_string[RANDOM_STRING_LEN - 1] = '\0';
d3e8f6bb 56
98612240 57 return random_string;
d3e8f6bb
DG
58}
59
657270a4 60static void test_create_one_ust_session(void)
d3e8f6bb 61{
30b66335
JG
62 struct ltt_ust_session *usess =
63 trace_ust_create_session(42);
d3e8f6bb 64
657270a4
CB
65 ok(usess != NULL, "Create UST session");
66
67b2f51c
JR
67 if (!usess) {
68 skip(1, "UST session is null");
69 return;
70 }
71
657270a4 72 ok(usess->id == 42 &&
14fb1ebe 73 usess->active == 0 &&
657270a4 74 usess->domain_global.channels != NULL &&
657270a4
CB
75 usess->uid == 0 &&
76 usess->gid == 0,
77 "Validate UST session");
d3e8f6bb
DG
78
79 trace_ust_destroy_session(usess);
80}
81
657270a4 82static void test_create_ust_channel(void)
d3e8f6bb
DG
83{
84 struct ltt_ust_channel *uchan;
85 struct lttng_channel attr;
82b4ebce 86 struct lttng_channel_extended extended;
d3e8f6bb 87
441c16a7 88 memset(&attr, 0, sizeof(attr));
82b4ebce
JG
89 memset(&extended, 0, sizeof(extended));
90 attr.attr.extended.ptr = &extended;
441c16a7 91
1b0eb865
MD
92 ok(lttng_strncpy(attr.name, "channel0", sizeof(attr.name)) == 0,
93 "Validate channel name length");
51755dc8 94 uchan = trace_ust_create_channel(&attr, LTTNG_DOMAIN_UST);
657270a4
CB
95 ok(uchan != NULL, "Create UST channel");
96
30b66335
JG
97 if (!uchan) {
98 skip(1, "UST channel is null");
67b2f51c
JR
99 return;
100 }
101
657270a4 102 ok(uchan->enabled == 0 &&
657270a4 103 strncmp(uchan->name, "channel0", 8) == 0 &&
fc4b93fa 104 uchan->name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] == '\0' &&
657270a4
CB
105 uchan->ctx != NULL &&
106 uchan->events != NULL &&
107 uchan->attr.overwrite == attr.attr.overwrite,
108 "Validate UST channel");
d3e8f6bb
DG
109
110 trace_ust_destroy_channel(uchan);
111}
112
657270a4 113static void test_create_ust_event(void)
d3e8f6bb
DG
114{
115 struct ltt_ust_event *event;
116 struct lttng_event ev;
39687410 117 enum lttng_error_code ret;
d3e8f6bb 118
441c16a7 119 memset(&ev, 0, sizeof(ev));
a84aca51
MD
120 ok(lttng_strncpy(ev.name, get_random_string(),
121 LTTNG_SYMBOL_NAME_LEN) == 0,
122 "Validate string length");
d3e8f6bb 123 ev.type = LTTNG_EVENT_TRACEPOINT;
441c16a7 124 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
d3e8f6bb 125
39687410 126 ret = trace_ust_create_event(&ev, NULL, NULL, NULL, false, &event);
d3e8f6bb 127
39687410 128 ok(ret == LTTNG_OK, "Create UST event");
657270a4 129
67b2f51c
JR
130 if (!event) {
131 skip(1, "UST event is null");
132 return;
133 }
134
657270a4 135 ok(event->enabled == 0 &&
fc4b93fa 136 event->attr.instrumentation == LTTNG_UST_ABI_TRACEPOINT &&
657270a4 137 strcmp(event->attr.name, ev.name) == 0 &&
fc4b93fa 138 event->attr.name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] == '\0',
657270a4 139 "Validate UST event");
d3e8f6bb
DG
140
141 trace_ust_destroy_event(event);
142}
143
41d7b959
JI
144static void test_create_ust_event_exclusion(void)
145{
39687410 146 enum lttng_error_code ret;
41d7b959
JI
147 struct ltt_ust_event *event;
148 struct lttng_event ev;
149 char *name;
88329be5 150 char *random_name;
e196f4f4 151 struct lttng_event_exclusion *exclusion = NULL;
4b6816b6 152 struct lttng_event_exclusion *exclusion_copy = NULL;
88329be5 153 const int exclusion_count = 2;
41d7b959
JI
154
155 memset(&ev, 0, sizeof(ev));
156
157 /* make a wildcarded event name */
158 name = get_random_string();
159 name[strlen(name) - 1] = '*';
61a046d9
MD
160 ok(lttng_strncpy(ev.name, name, LTTNG_SYMBOL_NAME_LEN) == 0,
161 "Validate string length");
41d7b959
JI
162
163 ev.type = LTTNG_EVENT_TRACEPOINT;
164 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
165
166 /* set up an exclusion set */
7966af57 167 exclusion = (lttng_event_exclusion *) zmalloc(sizeof(*exclusion) +
88329be5 168 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
3111dcc4 169 ok(exclusion != NULL, "Create UST exclusion");
c710ece7 170 if (!exclusion) {
67b2f51c
JR
171 skip(4, "zmalloc failed");
172 goto end;
c710ece7
MD
173 }
174
88329be5
PP
175 exclusion->count = exclusion_count;
176 random_name = get_random_string();
177 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0), random_name,
b43eb2f6 178 LTTNG_SYMBOL_NAME_LEN - 1);
88329be5 179 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1), random_name,
b43eb2f6 180 LTTNG_SYMBOL_NAME_LEN - 1);
41d7b959 181
39687410 182 ret = trace_ust_create_event(&ev, NULL, NULL, exclusion, false, &event);
3111dcc4 183 exclusion = NULL;
41d7b959 184
39687410 185 ok(ret != LTTNG_OK, "Create UST event with identical exclusion names fails");
88329be5 186
7966af57 187 exclusion = (lttng_event_exclusion *) zmalloc(sizeof(*exclusion) +
88329be5 188 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
3111dcc4 189 ok(exclusion != NULL, "Create UST exclusion");
88329be5 190 if (!exclusion) {
67b2f51c
JR
191 skip(2, "zmalloc failed");
192 goto end;
88329be5
PP
193 }
194
7966af57 195 exclusion_copy = (lttng_event_exclusion *) zmalloc(sizeof(*exclusion) +
4b6816b6
FD
196 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
197 if (!exclusion_copy) {
198 skip(2, "zmalloc failed");
199 goto end;
200 }
201
202 /*
203 * We are giving ownership of the exclusion struct to the
204 * trace_ust_create_event() function. Make a copy of the exclusion struct
205 * so we can compare it later.
206 */
207
88329be5
PP
208 exclusion->count = exclusion_count;
209 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0),
b43eb2f6 210 get_random_string(), LTTNG_SYMBOL_NAME_LEN - 1);
88329be5 211 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1),
b43eb2f6 212 get_random_string(), LTTNG_SYMBOL_NAME_LEN - 1);
88329be5 213
4b6816b6
FD
214 exclusion_copy->count = exclusion_count;
215 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy, 0),
216 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0), LTTNG_SYMBOL_NAME_LEN);
217 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion_copy, 1),
218 LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1), LTTNG_SYMBOL_NAME_LEN);
219
39687410 220 ret = trace_ust_create_event(&ev, NULL, NULL, exclusion, false, &event);
e196f4f4 221 exclusion = NULL;
39687410 222 ok(ret == LTTNG_OK, "Create UST event with different exclusion names");
41d7b959 223
67b2f51c
JR
224 if (!event) {
225 skip(1, "UST event with exclusion is null");
226 goto end;
227 }
228
41d7b959 229 ok(event->enabled == 0 &&
fc4b93fa 230 event->attr.instrumentation == LTTNG_UST_ABI_TRACEPOINT &&
881fa57f
JG
231 strcmp(event->attr.name, ev.name) == 0 &&
232 event->exclusion != NULL &&
233 event->exclusion->count == exclusion_count &&
234 !memcmp(event->exclusion->names, exclusion_copy->names,
235 LTTNG_SYMBOL_NAME_LEN * exclusion_count) &&
fc4b93fa 236 event->attr.name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] == '\0',
881fa57f 237 "Validate UST event and exclusion");
41d7b959 238
41d7b959 239 trace_ust_destroy_event(event);
67b2f51c 240end:
e196f4f4 241 free(exclusion);
4b6816b6 242 free(exclusion_copy);
67b2f51c 243 return;
41d7b959
JI
244}
245
246
657270a4 247static void test_create_ust_context(void)
d3e8f6bb 248{
e38021f8 249 struct lttng_event_context ectx;
d3e8f6bb
DG
250 struct ltt_ust_context *uctx;
251
e38021f8
DG
252 ectx.ctx = LTTNG_EVENT_CONTEXT_VTID;
253
e38021f8 254 uctx = trace_ust_create_context(&ectx);
657270a4 255 ok(uctx != NULL, "Create UST context");
d3e8f6bb 256
77f5299f 257 if (uctx) {
fc4b93fa 258 ok((int) uctx->ctx.ctx == LTTNG_UST_ABI_CONTEXT_VTID,
77f5299f
JG
259 "Validate UST context");
260 } else {
261 skip(1, "Skipping UST context validation as creation failed");
262 }
f949b23e 263 free(uctx);
d3e8f6bb
DG
264}
265
266int main(int argc, char **argv)
267{
657270a4 268 plan_tests(NUM_TESTS);
d3e8f6bb 269
e3bef725
CB
270 diag("UST data structures unit test");
271
8273250b
JR
272 rcu_register_thread();
273
657270a4 274 test_create_one_ust_session();
657270a4
CB
275 test_create_ust_channel();
276 test_create_ust_event();
277 test_create_ust_context();
41d7b959 278 test_create_ust_event_exclusion();
d3e8f6bb 279
8273250b
JR
280 rcu_unregister_thread();
281
657270a4 282 return exit_status();
d3e8f6bb 283}
This page took 0.06714 seconds and 4 git commands to generate.