Fix: Handle SIGBUS in sessiond and consumerd
[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
881fc67f
MD
24#include <lttng/ust-sigbus.h>
25
657270a4
CB
26#include <tap/tap.h>
27
d3e8f6bb
DG
28/* This path will NEVER be created in this test */
29#define PATH1 "/tmp/.test-junk-lttng"
30
98612240
MD
31#define RANDOM_STRING_LEN 11
32
657270a4 33/* Number of TAP tests in this file */
1cda50f2 34#define NUM_TESTS 16
657270a4 35
881fc67f
MD
36DEFINE_LTTNG_UST_SIGBUS_STATE();
37
ad7c9c18 38/* For error.h */
97e19046
DG
39int lttng_opt_quiet = 1;
40int lttng_opt_verbose;
c7e35b03 41int lttng_opt_mi;
d3e8f6bb
DG
42
43static const char alphanum[] =
44 "0123456789"
45 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
46 "abcdefghijklmnopqrstuvwxyz";
98612240 47static char random_string[RANDOM_STRING_LEN];
d3e8f6bb 48
d3e8f6bb
DG
49/*
50 * Return random string of 10 characters.
98612240 51 * Not thread-safe.
d3e8f6bb
DG
52 */
53static char *get_random_string(void)
54{
55 int i;
d3e8f6bb 56
98612240
MD
57 for (i = 0; i < RANDOM_STRING_LEN - 1; i++) {
58 random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)];
d3e8f6bb
DG
59 }
60
98612240 61 random_string[RANDOM_STRING_LEN - 1] = '\0';
d3e8f6bb 62
98612240 63 return random_string;
d3e8f6bb
DG
64}
65
657270a4 66static void test_create_one_ust_session(void)
d3e8f6bb 67{
30b66335
JG
68 struct ltt_ust_session *usess =
69 trace_ust_create_session(42);
d3e8f6bb 70
657270a4
CB
71 ok(usess != NULL, "Create UST session");
72
67b2f51c
JR
73 if (!usess) {
74 skip(1, "UST session is null");
75 return;
76 }
77
657270a4 78 ok(usess->id == 42 &&
14fb1ebe 79 usess->active == 0 &&
657270a4 80 usess->domain_global.channels != NULL &&
657270a4
CB
81 usess->uid == 0 &&
82 usess->gid == 0,
83 "Validate UST session");
d3e8f6bb
DG
84
85 trace_ust_destroy_session(usess);
86}
87
657270a4 88static void test_create_ust_channel(void)
d3e8f6bb
DG
89{
90 struct ltt_ust_channel *uchan;
91 struct lttng_channel attr;
82b4ebce 92 struct lttng_channel_extended extended;
d3e8f6bb 93
441c16a7 94 memset(&attr, 0, sizeof(attr));
82b4ebce
JG
95 memset(&extended, 0, sizeof(extended));
96 attr.attr.extended.ptr = &extended;
441c16a7 97
1b0eb865
MD
98 ok(lttng_strncpy(attr.name, "channel0", sizeof(attr.name)) == 0,
99 "Validate channel name length");
51755dc8 100 uchan = trace_ust_create_channel(&attr, LTTNG_DOMAIN_UST);
657270a4
CB
101 ok(uchan != NULL, "Create UST channel");
102
30b66335
JG
103 if (!uchan) {
104 skip(1, "UST channel is null");
67b2f51c
JR
105 return;
106 }
107
657270a4 108 ok(uchan->enabled == 0 &&
657270a4 109 strncmp(uchan->name, "channel0", 8) == 0 &&
fc4b93fa 110 uchan->name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] == '\0' &&
657270a4
CB
111 uchan->ctx != NULL &&
112 uchan->events != NULL &&
113 uchan->attr.overwrite == attr.attr.overwrite,
114 "Validate UST channel");
d3e8f6bb
DG
115
116 trace_ust_destroy_channel(uchan);
117}
118
657270a4 119static void test_create_ust_event(void)
d3e8f6bb
DG
120{
121 struct ltt_ust_event *event;
122 struct lttng_event ev;
39687410 123 enum lttng_error_code ret;
d3e8f6bb 124
441c16a7 125 memset(&ev, 0, sizeof(ev));
a84aca51
MD
126 ok(lttng_strncpy(ev.name, get_random_string(),
127 LTTNG_SYMBOL_NAME_LEN) == 0,
128 "Validate string length");
d3e8f6bb 129 ev.type = LTTNG_EVENT_TRACEPOINT;
441c16a7 130 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
d3e8f6bb 131
39687410 132 ret = trace_ust_create_event(&ev, NULL, NULL, NULL, false, &event);
d3e8f6bb 133
39687410 134 ok(ret == LTTNG_OK, "Create UST event");
657270a4 135
67b2f51c
JR
136 if (!event) {
137 skip(1, "UST event is null");
138 return;
139 }
140
657270a4 141 ok(event->enabled == 0 &&
fc4b93fa 142 event->attr.instrumentation == LTTNG_UST_ABI_TRACEPOINT &&
657270a4 143 strcmp(event->attr.name, ev.name) == 0 &&
fc4b93fa 144 event->attr.name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] == '\0',
657270a4 145 "Validate UST event");
d3e8f6bb
DG
146
147 trace_ust_destroy_event(event);
148}
149
41d7b959
JI
150static void test_create_ust_event_exclusion(void)
151{
39687410 152 enum lttng_error_code ret;
41d7b959
JI
153 struct ltt_ust_event *event;
154 struct lttng_event ev;
155 char *name;
88329be5 156 char *random_name;
e196f4f4 157 struct lttng_event_exclusion *exclusion = NULL;
4b6816b6 158 struct lttng_event_exclusion *exclusion_copy = NULL;
88329be5 159 const int exclusion_count = 2;
41d7b959
JI
160
161 memset(&ev, 0, sizeof(ev));
162
163 /* make a wildcarded event name */
164 name = get_random_string();
165 name[strlen(name) - 1] = '*';
61a046d9
MD
166 ok(lttng_strncpy(ev.name, name, LTTNG_SYMBOL_NAME_LEN) == 0,
167 "Validate string length");
41d7b959
JI
168
169 ev.type = LTTNG_EVENT_TRACEPOINT;
170 ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL;
171
172 /* set up an exclusion set */
88329be5
PP
173 exclusion = zmalloc(sizeof(*exclusion) +
174 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
3111dcc4 175 ok(exclusion != NULL, "Create UST exclusion");
c710ece7 176 if (!exclusion) {
67b2f51c
JR
177 skip(4, "zmalloc failed");
178 goto end;
c710ece7
MD
179 }
180
88329be5
PP
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);
41d7b959 187
39687410 188 ret = trace_ust_create_event(&ev, NULL, NULL, exclusion, false, &event);
3111dcc4 189 exclusion = NULL;
41d7b959 190
39687410 191 ok(ret != LTTNG_OK, "Create UST event with identical exclusion names fails");
88329be5
PP
192
193 exclusion = zmalloc(sizeof(*exclusion) +
194 LTTNG_SYMBOL_NAME_LEN * exclusion_count);
3111dcc4 195 ok(exclusion != NULL, "Create UST exclusion");
88329be5 196 if (!exclusion) {
67b2f51c
JR
197 skip(2, "zmalloc failed");
198 goto end;
88329be5
PP
199 }
200
4b6816b6
FD
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
88329be5
PP
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
4b6816b6
FD
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
39687410 226 ret = trace_ust_create_event(&ev, NULL, NULL, exclusion, false, &event);
e196f4f4 227 exclusion = NULL;
39687410 228 ok(ret == LTTNG_OK, "Create UST event with different exclusion names");
41d7b959 229
67b2f51c
JR
230 if (!event) {
231 skip(1, "UST event with exclusion is null");
232 goto end;
233 }
234
41d7b959 235 ok(event->enabled == 0 &&
fc4b93fa 236 event->attr.instrumentation == LTTNG_UST_ABI_TRACEPOINT &&
881fa57f
JG
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) &&
fc4b93fa 242 event->attr.name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] == '\0',
881fa57f 243 "Validate UST event and exclusion");
41d7b959 244
41d7b959 245 trace_ust_destroy_event(event);
67b2f51c 246end:
e196f4f4 247 free(exclusion);
4b6816b6 248 free(exclusion_copy);
67b2f51c 249 return;
41d7b959
JI
250}
251
252
657270a4 253static void test_create_ust_context(void)
d3e8f6bb 254{
e38021f8 255 struct lttng_event_context ectx;
d3e8f6bb
DG
256 struct ltt_ust_context *uctx;
257
e38021f8
DG
258 ectx.ctx = LTTNG_EVENT_CONTEXT_VTID;
259
e38021f8 260 uctx = trace_ust_create_context(&ectx);
657270a4 261 ok(uctx != NULL, "Create UST context");
d3e8f6bb 262
77f5299f 263 if (uctx) {
fc4b93fa 264 ok((int) uctx->ctx.ctx == LTTNG_UST_ABI_CONTEXT_VTID,
77f5299f
JG
265 "Validate UST context");
266 } else {
267 skip(1, "Skipping UST context validation as creation failed");
268 }
f949b23e 269 free(uctx);
d3e8f6bb
DG
270}
271
272int main(int argc, char **argv)
273{
657270a4 274 plan_tests(NUM_TESTS);
d3e8f6bb 275
e3bef725
CB
276 diag("UST data structures unit test");
277
8273250b
JR
278 rcu_register_thread();
279
657270a4 280 test_create_one_ust_session();
657270a4
CB
281 test_create_ust_channel();
282 test_create_ust_event();
283 test_create_ust_context();
41d7b959 284 test_create_ust_event_exclusion();
d3e8f6bb 285
8273250b
JR
286 rcu_unregister_thread();
287
657270a4 288 return exit_status();
d3e8f6bb 289}
This page took 0.05805 seconds and 4 git commands to generate.