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