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