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