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