Commit | Line | Data |
---|---|---|
d3e8f6bb DG |
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 | ||
10a8a223 DG |
28 | #include <lttng/lttng.h> |
29 | #include <bin/lttng-sessiond/lttng-ust-abi.h> | |
990570ed | 30 | #include <common/defaults.h> |
10a8a223 DG |
31 | #include <bin/lttng-sessiond/trace-ust.h> |
32 | ||
d3e8f6bb DG |
33 | #include "utils.h" |
34 | ||
35 | /* This path will NEVER be created in this test */ | |
36 | #define PATH1 "/tmp/.test-junk-lttng" | |
37 | ||
98612240 MD |
38 | #define RANDOM_STRING_LEN 11 |
39 | ||
d3e8f6bb | 40 | /* For lttngerr.h */ |
97e19046 DG |
41 | int lttng_opt_quiet = 1; |
42 | int lttng_opt_verbose; | |
d3e8f6bb DG |
43 | |
44 | static const char alphanum[] = | |
45 | "0123456789" | |
46 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
47 | "abcdefghijklmnopqrstuvwxyz"; | |
98612240 | 48 | static char random_string[RANDOM_STRING_LEN]; |
d3e8f6bb DG |
49 | |
50 | static struct ltt_ust_session *usess; | |
51 | static struct lttng_domain dom; | |
52 | ||
53 | /* | |
54 | * Return random string of 10 characters. | |
98612240 | 55 | * Not thread-safe. |
d3e8f6bb DG |
56 | */ |
57 | static char *get_random_string(void) | |
58 | { | |
59 | int i; | |
d3e8f6bb | 60 | |
98612240 MD |
61 | for (i = 0; i < RANDOM_STRING_LEN - 1; i++) { |
62 | random_string[i] = alphanum[rand() % (sizeof(alphanum) - 1)]; | |
d3e8f6bb DG |
63 | } |
64 | ||
98612240 | 65 | random_string[RANDOM_STRING_LEN - 1] = '\0'; |
d3e8f6bb | 66 | |
98612240 | 67 | return random_string; |
d3e8f6bb DG |
68 | } |
69 | ||
70 | static void create_one_ust_session(void) | |
71 | { | |
72 | printf("Create UST session: "); | |
73 | ||
74 | dom.type = LTTNG_DOMAIN_UST; | |
75 | ||
fdd9eb17 | 76 | usess = trace_ust_create_session(PATH1, 42); |
d3e8f6bb DG |
77 | assert(usess != NULL); |
78 | PRINT_OK(); | |
79 | ||
80 | printf("Validating UST session: "); | |
81 | assert(usess->id == 42); | |
82 | assert(usess->start_trace == 0); | |
83 | assert(usess->domain_global.channels != NULL); | |
84 | assert(usess->domain_pid != NULL); | |
85 | assert(usess->domain_exec != NULL); | |
86 | assert(usess->uid == 0); | |
87 | assert(usess->gid == 0); | |
88 | PRINT_OK(); | |
89 | ||
90 | trace_ust_destroy_session(usess); | |
91 | } | |
92 | ||
93 | static void create_ust_metadata(void) | |
94 | { | |
95 | struct ltt_ust_metadata *metadata; | |
96 | ||
97 | assert(usess != NULL); | |
98 | ||
99 | printf("Create UST metadata: "); | |
100 | metadata = trace_ust_create_metadata(PATH1); | |
101 | assert(metadata != NULL); | |
102 | PRINT_OK(); | |
103 | ||
104 | printf("Validating UST session metadata: "); | |
105 | assert(metadata->handle == -1); | |
106 | assert(strlen(metadata->pathname)); | |
107 | assert(metadata->attr.overwrite | |
108 | == DEFAULT_CHANNEL_OVERWRITE); | |
109 | assert(metadata->attr.subbuf_size | |
3e230f92 | 110 | == default_get_metadata_subbuf_size()); |
d3e8f6bb DG |
111 | assert(metadata->attr.num_subbuf |
112 | == DEFAULT_METADATA_SUBBUF_NUM); | |
113 | assert(metadata->attr.switch_timer_interval | |
114 | == DEFAULT_CHANNEL_SWITCH_TIMER); | |
115 | assert(metadata->attr.read_timer_interval | |
116 | == DEFAULT_CHANNEL_READ_TIMER); | |
117 | assert(metadata->attr.output == LTTNG_UST_MMAP); | |
118 | PRINT_OK(); | |
119 | ||
120 | trace_ust_destroy_metadata(metadata); | |
121 | } | |
122 | ||
123 | static void create_ust_channel(void) | |
124 | { | |
125 | struct ltt_ust_channel *uchan; | |
126 | struct lttng_channel attr; | |
127 | ||
441c16a7 MD |
128 | memset(&attr, 0, sizeof(attr)); |
129 | ||
d3e8f6bb DG |
130 | strncpy(attr.name, "channel0", 8); |
131 | ||
132 | printf("Creating UST channel: "); | |
133 | uchan = trace_ust_create_channel(&attr, PATH1); | |
134 | assert(uchan != NULL); | |
135 | PRINT_OK(); | |
136 | ||
137 | printf("Validating UST channel: "); | |
138 | assert(uchan->enabled == 0); | |
139 | assert(strcmp(PATH1, uchan->pathname) == 0); | |
140 | assert(strncmp(uchan->name, "channel0", 8) == 0); | |
141 | assert(uchan->name[LTTNG_UST_SYM_NAME_LEN - 1] == '\0'); | |
142 | assert(uchan->ctx != NULL); | |
143 | assert(uchan->events != NULL); | |
144 | assert(uchan->attr.overwrite == attr.attr.overwrite); | |
145 | PRINT_OK(); | |
146 | ||
147 | trace_ust_destroy_channel(uchan); | |
148 | } | |
149 | ||
150 | static void create_ust_event(void) | |
151 | { | |
152 | struct ltt_ust_event *event; | |
153 | struct lttng_event ev; | |
154 | ||
441c16a7 | 155 | memset(&ev, 0, sizeof(ev)); |
d3e8f6bb DG |
156 | strncpy(ev.name, get_random_string(), LTTNG_SYMBOL_NAME_LEN); |
157 | ev.type = LTTNG_EVENT_TRACEPOINT; | |
441c16a7 | 158 | ev.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; |
d3e8f6bb DG |
159 | |
160 | printf("Creating UST event: "); | |
025faf73 | 161 | event = trace_ust_create_event(&ev, NULL); |
d3e8f6bb DG |
162 | assert(event != NULL); |
163 | PRINT_OK(); | |
164 | ||
165 | printf("Validating UST event: "); | |
166 | assert(event->enabled == 0); | |
d3e8f6bb DG |
167 | assert(event->attr.instrumentation == LTTNG_UST_TRACEPOINT); |
168 | assert(strcmp(event->attr.name, ev.name) == 0); | |
169 | assert(event->attr.name[LTTNG_UST_SYM_NAME_LEN - 1] == '\0'); | |
170 | PRINT_OK(); | |
171 | ||
172 | trace_ust_destroy_event(event); | |
173 | } | |
174 | ||
175 | static void create_ust_context(void) | |
176 | { | |
e38021f8 | 177 | struct lttng_event_context ectx; |
d3e8f6bb DG |
178 | struct ltt_ust_context *uctx; |
179 | ||
e38021f8 DG |
180 | ectx.ctx = LTTNG_EVENT_CONTEXT_VTID; |
181 | ||
d3e8f6bb | 182 | printf("Creating UST context: "); |
e38021f8 | 183 | uctx = trace_ust_create_context(&ectx); |
d3e8f6bb DG |
184 | assert(uctx != NULL); |
185 | PRINT_OK(); | |
186 | ||
187 | printf("Validating UST context: "); | |
e38021f8 | 188 | assert((int) uctx->ctx.ctx == LTTNG_UST_CONTEXT_VTID); |
d3e8f6bb DG |
189 | PRINT_OK(); |
190 | } | |
191 | ||
192 | int main(int argc, char **argv) | |
193 | { | |
194 | printf("\nTesting UST data structures:\n-----------\n"); | |
195 | ||
196 | create_one_ust_session(); | |
197 | create_ust_metadata(); | |
198 | create_ust_channel(); | |
199 | create_ust_event(); | |
200 | create_ust_context(); | |
201 | ||
202 | /* Success */ | |
203 | return 0; | |
204 | } |