236f507bb0033b8933749d19ee8a4892754ce79a
[lttng-tools.git] / tests / unit / test_ust_data.c
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
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>
26
27 #include <lttng/lttng.h>
28 #include <bin/lttng-sessiond/lttng-ust-abi.h>
29 #include <common/defaults.h>
30 #include <bin/lttng-sessiond/trace-ust.h>
31 #include <bin/lttng-sessiond/ust-app.h>
32
33 #include <tap/tap.h>
34
35 /* This path will NEVER be created in this test */
36 #define PATH1 "/tmp/.test-junk-lttng"
37
38 #define RANDOM_STRING_LEN 11
39
40 /* Number of TAP tests in this file */
41 #define NUM_TESTS 16
42
43 /* For error.h */
44 int lttng_opt_quiet = 1;
45 int lttng_opt_verbose;
46 int lttng_opt_mi;
47
48 int ust_consumerd32_fd;
49 int ust_consumerd64_fd;
50
51 /* Global variable required by sessiond objects being linked-in */
52 struct lttng_ht *agent_apps_ht_by_sock;
53
54 static const char alphanum[] =
55 "0123456789"
56 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
57 "abcdefghijklmnopqrstuvwxyz";
58 static char random_string[RANDOM_STRING_LEN];
59
60 static struct ltt_ust_session *usess;
61 static struct lttng_domain dom;
62
63 /*
64 * Return random string of 10 characters.
65 * Not thread-safe.
66 */
67 static char *get_random_string(void)
68 {
69 int i;
70
71 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
72 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
73 }
74
75 random_string[RANDOM_STRING_LEN - 1] = '\0';
76
77 return random_string;
78 }
79
80 static void test_create_one_ust_session(void)
81 {
82 dom.type = LTTNG_DOMAIN_UST;
83
84 usess = trace_ust_create_session(42);
85 ok(usess != NULL, "Create UST session");
86
87 ok(usess->id == 42 &&
88 usess->active == 0 &&
89 usess->domain_global.channels != NULL &&
90 usess->uid == 0 &&
91 usess->gid == 0,
92 "Validate UST session");
93
94 trace_ust_destroy_session(usess);
95 }
96
97 static void test_create_ust_channel(void)
98 {
99 struct ltt_ust_channel *uchan;
100 struct lttng_channel attr;
101
102 memset(&attr, 0, sizeof(attr));
103
104 ok(lttng_strncpy(attr.name, "channel0", sizeof(attr.name)) == 0,
105 "Validate channel name length");
106 uchan = trace_ust_create_channel(&attr, LTTNG_DOMAIN_UST);
107 ok(uchan != NULL, "Create UST channel");
108
109 ok(uchan->enabled == 0 &&
110 strncmp(uchan->name, "channel0", 8) == 0 &&
111 uchan->name[LTTNG_UST_SYM_NAME_LEN - 1] == '\0' &&
112 uchan->ctx != NULL &&
113 uchan->events != NULL &&
114 uchan->attr.overwrite == attr.attr.overwrite,
115 "Validate UST channel");
116
117 trace_ust_destroy_channel(uchan);
118 }
119
120 static void test_create_ust_event(void)
121 {
122 struct ltt_ust_event *event;
123 struct lttng_event ev;
124
125 memset(&ev, 0, sizeof(ev));
126 ok(lttng_strncpy(ev.name, get_random_string(),
127 LTTNG_SYMBOL_NAME_LEN) == 0,
128 "Validate string length");
129 ev.type = LTTNG_EVENT_TRACEPOINT;
130 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
131
132 event = trace_ust_create_event(&ev, NULL, NULL, NULL, false);
133
134 ok(event != NULL, "Create UST event");
135
136 ok(event->enabled == 0 &&
137 event->attr.instrumentation == LTTNG_UST_TRACEPOINT &&
138 strcmp(event->attr.name, ev.name) == 0 &&
139 event->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] == '\0',
140 "Validate UST event");
141
142 trace_ust_destroy_event(event);
143 }
144
145 static void test_create_ust_event_exclusion(void)
146 {
147 struct ltt_ust_event *event;
148 struct lttng_event ev;
149 char *name;
150 char *random_name;
151 struct lttng_event_exclusion *exclusion;
152 const int exclusion_count = 2;
153
154 memset(&ev, 0, sizeof(ev));
155
156 /* make a wildcarded event name */
157 name = get_random_string();
158 name[strlen(name) - 1] = '*';
159 ok(lttng_strncpy(ev.name, name, LTTNG_SYMBOL_NAME_LEN) == 0,
160 "Validate string length");
161
162 ev.type = LTTNG_EVENT_TRACEPOINT;
163 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
164
165 /* set up an exclusion set */
166 exclusion = zmalloc(sizeof(*exclusion) +
167 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
168 if (!exclusion) {
169 PERROR("zmalloc");
170 }
171
172 ok(exclusion != NULL, "Create UST exclusion");
173
174 exclusion->count = exclusion_count;
175 random_name = get_random_string();
176 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0), random_name,
177 LTTNG_SYMBOL_NAME_LEN);
178 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1), random_name,
179 LTTNG_SYMBOL_NAME_LEN);
180
181 event = trace_ust_create_event(&ev, NULL, NULL, exclusion, false);
182
183 ok(!event, "Create UST event with identical exclusion names fails");
184
185 exclusion = zmalloc(sizeof(*exclusion) +
186 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
187 if (!exclusion) {
188 PERROR("zmalloc");
189 }
190
191 ok(exclusion != NULL, "Create UST exclusion");
192
193 exclusion->count = exclusion_count;
194 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 0),
195 get_random_string(), LTTNG_SYMBOL_NAME_LEN);
196 strncpy(LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, 1),
197 get_random_string(), LTTNG_SYMBOL_NAME_LEN);
198
199 event = trace_ust_create_event(&ev, NULL, NULL, exclusion, false);
200 assert(event != NULL);
201
202 ok(event != NULL, "Create UST event with different exclusion names");
203
204 ok(event->enabled == 0 &&
205 event->attr.instrumentation == LTTNG_UST_TRACEPOINT &&
206 strcmp(event->attr.name, ev.name) == 0 &&
207 event->exclusion != NULL &&
208 event->exclusion->count == exclusion_count &&
209 !memcmp(event->exclusion->names, exclusion->names,
210 LTTNG_SYMBOL_NAME_LEN * exclusion_count) &&
211 event->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] == '\0',
212 "Validate UST event and exclusion");
213
214 trace_ust_destroy_event(event);
215 }
216
217
218 static void test_create_ust_context(void)
219 {
220 struct lttng_event_context ectx;
221 struct ltt_ust_context *uctx;
222
223 ectx.ctx = LTTNG_EVENT_CONTEXT_VTID;
224
225 uctx = trace_ust_create_context(&ectx);
226 ok(uctx != NULL, "Create UST context");
227
228 ok((int) uctx->ctx.ctx == LTTNG_UST_CONTEXT_VTID,
229 "Validate UST context");
230 free(uctx);
231 }
232
233 int main(int argc, char **argv)
234 {
235 plan_tests(NUM_TESTS);
236
237 diag("UST data structures unit test");
238
239 test_create_one_ust_session();
240 test_create_ust_channel();
241 test_create_ust_event();
242 test_create_ust_context();
243 test_create_ust_event_exclusion();
244
245 return exit_status();
246 }
This page took 0.033197 seconds and 3 git commands to generate.