Rotate command
[lttng-tools.git] / tests / unit / test_ust_data.c
CommitLineData
d3e8f6bb
DG
1/*
2 * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
d3e8f6bb
DG
19#include <assert.h>
20#include <errno.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <unistd.h>
25#include <time.h>
8273250b 26#include <urcu.h>
d3e8f6bb 27
10a8a223
DG
28#include <lttng/lttng.h>
29#include <bin/lttng-sessiond/lttng-ust-abi.h>
990570ed 30#include <common/defaults.h>
10a8a223 31#include <bin/lttng-sessiond/trace-ust.h>
7972aab2 32#include <bin/lttng-sessiond/ust-app.h>
83b45089 33#include <bin/lttng-sessiond/notification-thread.h>
10a8a223 34
657270a4
CB
35#include <tap/tap.h>
36
d3e8f6bb
DG
37/* This path will NEVER be created in this test */
38#define PATH1 "/tmp/.test-junk-lttng"
39
98612240
MD
40#define RANDOM_STRING_LEN 11
41
657270a4 42/* Number of TAP tests in this file */
1cda50f2 43#define NUM_TESTS 16
657270a4 44
ad7c9c18 45/* For error.h */
97e19046
DG
46int lttng_opt_quiet = 1;
47int lttng_opt_verbose;
c7e35b03 48int lttng_opt_mi;
d3e8f6bb 49
7972aab2
DG
50int ust_consumerd32_fd;
51int ust_consumerd64_fd;
52
83b45089 53/* Global variables required by sessiond objects being linked-in */
7c1d2758 54struct lttng_ht *agent_apps_ht_by_sock;
83b45089 55struct notification_thread_handle *notification_thread_handle;
7c1d2758 56
d3e8f6bb
DG
57static const char alphanum[] =
58 "0123456789"
59 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
60 "abcdefghijklmnopqrstuvwxyz";
98612240 61static char random_string[RANDOM_STRING_LEN];
d3e8f6bb
DG
62
63static struct ltt_ust_session *usess;
64static struct lttng_domain dom;
65
5c408ad8
JD
66/*
67 * Stub to prevent an undefined reference in this test without having to link
68 * the entire tree because of a cascade of dependencies. This is not used,
69 * it is just there to prevent GCC from complaining.
70 */
71int rotate_add_channel_pending(uint64_t key, enum lttng_domain_type domain,
72 struct ltt_session *session)
73{
74 ERR("Stub called instead of the real function");
75 abort();
76 return -1;
77}
78
d3e8f6bb
DG
79/*
80 * Return random string of 10 characters.
98612240 81 * Not thread-safe.
d3e8f6bb
DG
82 */
83static char *get_random_string(void)
84{
85 int i;
d3e8f6bb 86
98612240
MD
87 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
88 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
d3e8f6bb
DG
89 }
90
98612240 91 random_string[RANDOM_STRING_LEN - 1] = '\0';
d3e8f6bb 92
98612240 93 return random_string;
d3e8f6bb
DG
94}
95
657270a4 96static void test_create_one_ust_session(void)
d3e8f6bb 97{
d3e8f6bb
DG
98 dom.type = LTTNG_DOMAIN_UST;
99
dec56f6c 100 usess = trace_ust_create_session(42);
657270a4
CB
101 ok(usess != NULL, "Create UST session");
102
67b2f51c
JR
103 if (!usess) {
104 skip(1, "UST session is null");
105 return;
106 }
107
657270a4 108 ok(usess->id == 42 &&
14fb1ebe 109 usess->active == 0 &&
657270a4 110 usess->domain_global.channels != NULL &&
657270a4
CB
111 usess->uid == 0 &&
112 usess->gid == 0,
113 "Validate UST session");
d3e8f6bb
DG
114
115 trace_ust_destroy_session(usess);
116}
117
657270a4 118static void test_create_ust_channel(void)
d3e8f6bb
DG
119{
120 struct ltt_ust_channel *uchan;
121 struct lttng_channel attr;
82b4ebce 122 struct lttng_channel_extended extended;
d3e8f6bb 123
441c16a7 124 memset(&attr, 0, sizeof(attr));
82b4ebce
JG
125 memset(&extended, 0, sizeof(extended));
126 attr.attr.extended.ptr = &extended;
441c16a7 127
1b0eb865
MD
128 ok(lttng_strncpy(attr.name, "channel0", sizeof(attr.name)) == 0,
129 "Validate channel name length");
51755dc8 130 uchan = trace_ust_create_channel(&attr, LTTNG_DOMAIN_UST);
657270a4
CB
131 ok(uchan != NULL, "Create UST channel");
132
67b2f51c
JR
133 if (!usess) {
134 skip(1, "UST session is null");
135 return;
136 }
137
657270a4 138 ok(uchan->enabled == 0 &&
657270a4
CB
139 strncmp(uchan->name, "channel0", 8) == 0 &&
140 uchan->name[LTTNG_UST_SYM_NAME_LEN - 1] == '\0' &&
141 uchan->ctx != NULL &&
142 uchan->events != NULL &&
143 uchan->attr.overwrite == attr.attr.overwrite,
144 "Validate UST channel");
d3e8f6bb
DG
145
146 trace_ust_destroy_channel(uchan);
147}
148
657270a4 149static void test_create_ust_event(void)
d3e8f6bb
DG
150{
151 struct ltt_ust_event *event;
152 struct lttng_event ev;
153
441c16a7 154 memset(&ev, 0, sizeof(ev));
a84aca51
MD
155 ok(lttng_strncpy(ev.name, get_random_string(),
156 LTTNG_SYMBOL_NAME_LEN) == 0,
157 "Validate string length");
d3e8f6bb 158 ev.type = LTTNG_EVENT_TRACEPOINT;
441c16a7 159 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
d3e8f6bb 160
88f06f15 161 event = trace_ust_create_event(&ev, NULL, NULL, NULL, false);
d3e8f6bb 162
657270a4
CB
163 ok(event != NULL, "Create UST event");
164
67b2f51c
JR
165 if (!event) {
166 skip(1, "UST event is null");
167 return;
168 }
169
657270a4
CB
170 ok(event->enabled == 0 &&
171 event->attr.instrumentation == LTTNG_UST_TRACEPOINT &&
172 strcmp(event->attr.name, ev.name) == 0 &&
173 event->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] == '\0',
174 "Validate UST event");
d3e8f6bb
DG
175
176 trace_ust_destroy_event(event);
177}
178
41d7b959
JI
179static void test_create_ust_event_exclusion(void)
180{
181 struct ltt_ust_event *event;
182 struct lttng_event ev;
183 char *name;
88329be5 184 char *random_name;
41d7b959 185 struct lttng_event_exclusion *exclusion;
88329be5 186 const int exclusion_count = 2;
41d7b959
JI
187
188 memset(&ev, 0, sizeof(ev));
189
190 /* make a wildcarded event name */
191 name = get_random_string();
192 name[strlen(name) - 1] = '*';
61a046d9
MD
193 ok(lttng_strncpy(ev.name, name, LTTNG_SYMBOL_NAME_LEN) == 0,
194 "Validate string length");
41d7b959
JI
195
196 ev.type = LTTNG_EVENT_TRACEPOINT;
197 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
198
199 /* set up an exclusion set */
88329be5
PP
200 exclusion = zmalloc(sizeof(*exclusion) +
201 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
3111dcc4 202 ok(exclusion != NULL, "Create UST exclusion");
c710ece7 203 if (!exclusion) {
67b2f51c
JR
204 skip(4, "zmalloc failed");
205 goto end;
c710ece7
MD
206 }
207
88329be5
PP
208 exclusion->count = exclusion_count;
209 random_name = get_random_string();
210 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0), random_name,
211 LTTNG_SYMBOL_NAME_LEN);
212 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1), random_name,
213 LTTNG_SYMBOL_NAME_LEN);
41d7b959 214
88f06f15 215 event = trace_ust_create_event(&ev, NULL, NULL, exclusion, false);
3111dcc4 216 exclusion = NULL;
41d7b959 217
88329be5
PP
218 ok(!event, "Create UST event with identical exclusion names fails");
219
220 exclusion = zmalloc(sizeof(*exclusion) +
221 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
3111dcc4 222 ok(exclusion != NULL, "Create UST exclusion");
88329be5 223 if (!exclusion) {
67b2f51c
JR
224 skip(2, "zmalloc failed");
225 goto end;
88329be5
PP
226 }
227
228 exclusion->count = exclusion_count;
229 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0),
230 get_random_string(), LTTNG_SYMBOL_NAME_LEN);
231 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1),
232 get_random_string(), LTTNG_SYMBOL_NAME_LEN);
233
234 event = trace_ust_create_event(&ev, NULL, NULL, exclusion, false);
88329be5 235 ok(event != NULL, "Create UST event with different exclusion names");
41d7b959 236
67b2f51c
JR
237 if (!event) {
238 skip(1, "UST event with exclusion is null");
239 goto end;
240 }
241
41d7b959
JI
242 ok(event->enabled == 0 &&
243 event->attr.instrumentation == LTTNG_UST_TRACEPOINT &&
244 strcmp(event->attr.name, ev.name) == 0 &&
245 event->exclusion != NULL &&
88329be5
PP
246 event->exclusion->count == exclusion_count &&
247 !memcmp(event->exclusion->names, exclusion->names,
248 LTTNG_SYMBOL_NAME_LEN * exclusion_count) &&
41d7b959
JI
249 event->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] == '\0',
250 "Validate UST event and exclusion");
251
41d7b959 252 trace_ust_destroy_event(event);
67b2f51c
JR
253end:
254 return;
41d7b959
JI
255}
256
257
657270a4 258static void test_create_ust_context(void)
d3e8f6bb 259{
e38021f8 260 struct lttng_event_context ectx;
d3e8f6bb
DG
261 struct ltt_ust_context *uctx;
262
e38021f8
DG
263 ectx.ctx = LTTNG_EVENT_CONTEXT_VTID;
264
e38021f8 265 uctx = trace_ust_create_context(&ectx);
657270a4 266 ok(uctx != NULL, "Create UST context");
d3e8f6bb 267
657270a4
CB
268 ok((int) uctx->ctx.ctx == LTTNG_UST_CONTEXT_VTID,
269 "Validate UST context");
f949b23e 270 free(uctx);
d3e8f6bb
DG
271}
272
273int main(int argc, char **argv)
274{
657270a4 275 plan_tests(NUM_TESTS);
d3e8f6bb 276
e3bef725
CB
277 diag("UST data structures unit test");
278
8273250b
JR
279 rcu_register_thread();
280
657270a4 281 test_create_one_ust_session();
657270a4
CB
282 test_create_ust_channel();
283 test_create_ust_event();
284 test_create_ust_context();
41d7b959 285 test_create_ust_event_exclusion();
d3e8f6bb 286
8273250b
JR
287 rcu_unregister_thread();
288
657270a4 289 return exit_status();
d3e8f6bb 290}
This page took 0.05217 seconds and 4 git commands to generate.