Commit | Line | Data |
---|---|---|
2f77fc4b | 1 | /* |
ab5be9fa MJ |
2 | * Copyright (C) 2012 David Goulet <dgoulet@efficios.com> |
3 | * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
2f77fc4b | 4 | * |
ab5be9fa | 5 | * SPDX-License-Identifier: GPL-2.0-only |
2f77fc4b | 6 | * |
2f77fc4b DG |
7 | */ |
8 | ||
159b042f JG |
9 | #include "bin/lttng-sessiond/tracker.h" |
10 | #include "lttng/lttng-error.h" | |
11 | #include "lttng/tracker.h" | |
6c1c0768 | 12 | #define _LGPL_SOURCE |
2f77fc4b | 13 | #include <assert.h> |
6dc3064a | 14 | #include <inttypes.h> |
2f77fc4b DG |
15 | #include <urcu/list.h> |
16 | #include <urcu/uatomic.h> | |
a503e1ef | 17 | #include <sys/stat.h> |
d2956687 | 18 | #include <stdio.h> |
2f77fc4b DG |
19 | |
20 | #include <common/defaults.h> | |
21 | #include <common/common.h> | |
22 | #include <common/sessiond-comm/sessiond-comm.h> | |
23 | #include <common/relayd/relayd.h> | |
10a50311 | 24 | #include <common/utils.h> |
f5436bfc | 25 | #include <common/compat/string.h> |
93ec662e | 26 | #include <common/kernel-ctl/kernel-ctl.h> |
b0880ae5 JG |
27 | #include <common/dynamic-buffer.h> |
28 | #include <common/buffer-view.h> | |
82b69413 | 29 | #include <common/trace-chunk.h> |
3e3665b8 | 30 | #include <lttng/location-internal.h> |
b0880ae5 JG |
31 | #include <lttng/trigger/trigger-internal.h> |
32 | #include <lttng/condition/condition.h> | |
33 | #include <lttng/action/action.h> | |
6e21424e | 34 | #include <lttng/channel.h> |
b0880ae5 | 35 | #include <lttng/channel-internal.h> |
5c408ad8 | 36 | #include <lttng/rotate-internal.h> |
17dd1232 | 37 | #include <lttng/location-internal.h> |
b178f53e | 38 | #include <lttng/session-internal.h> |
3c02e545 | 39 | #include <lttng/userspace-probe-internal.h> |
b178f53e | 40 | #include <lttng/session-descriptor-internal.h> |
9f449915 | 41 | #include <common/string-utils/string-utils.h> |
2f77fc4b DG |
42 | |
43 | #include "channel.h" | |
44 | #include "consumer.h" | |
45 | #include "event.h" | |
8782cc74 | 46 | #include "health-sessiond.h" |
2f77fc4b DG |
47 | #include "kernel.h" |
48 | #include "kernel-consumer.h" | |
49 | #include "lttng-sessiond.h" | |
50 | #include "utils.h" | |
0dbc2034 | 51 | #include "lttng-syscall.h" |
7c1d2758 | 52 | #include "agent.h" |
93ec662e | 53 | #include "buffer-registry.h" |
b0880ae5 JG |
54 | #include "notification-thread.h" |
55 | #include "notification-thread-commands.h" | |
5c408ad8 JD |
56 | #include "rotate.h" |
57 | #include "rotation-thread.h" | |
8e319828 | 58 | #include "timer.h" |
f28f9e44 | 59 | #include "agent-thread.h" |
2f77fc4b DG |
60 | |
61 | #include "cmd.h" | |
62 | ||
a503e1ef JG |
63 | /* Sleep for 100ms between each check for the shm path's deletion. */ |
64 | #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000 | |
65 | ||
3e3665b8 JG |
66 | struct cmd_destroy_session_reply_context { |
67 | int reply_sock_fd; | |
68 | bool implicit_rotation_on_destroy; | |
3285a971 JG |
69 | /* |
70 | * Indicates whether or not an error occurred while launching the | |
71 | * destruction of a session. | |
72 | */ | |
73 | enum lttng_error_code destruction_status; | |
3e3665b8 JG |
74 | }; |
75 | ||
a503e1ef JG |
76 | static enum lttng_error_code wait_on_path(void *path); |
77 | ||
78 | /* | |
79 | * Command completion handler that is used by the destroy command | |
80 | * when a session that has a non-default shm_path is being destroyed. | |
81 | * | |
82 | * See comment in cmd_destroy_session() for the rationale. | |
83 | */ | |
84 | static struct destroy_completion_handler { | |
85 | struct cmd_completion_handler handler; | |
86 | char shm_path[member_sizeof(struct ltt_session, shm_path)]; | |
87 | } destroy_completion_handler = { | |
88 | .handler = { | |
89 | .run = wait_on_path, | |
90 | .data = destroy_completion_handler.shm_path | |
91 | }, | |
92 | .shm_path = { 0 }, | |
93 | }; | |
94 | ||
95 | static struct cmd_completion_handler *current_completion_handler; | |
96 | ||
2f77fc4b DG |
97 | /* |
98 | * Used to keep a unique index for each relayd socket created where this value | |
99 | * is associated with streams on the consumer so it can match the right relayd | |
d88aee68 DG |
100 | * to send to. It must be accessed with the relayd_net_seq_idx_lock |
101 | * held. | |
2f77fc4b | 102 | */ |
d88aee68 DG |
103 | static pthread_mutex_t relayd_net_seq_idx_lock = PTHREAD_MUTEX_INITIALIZER; |
104 | static uint64_t relayd_net_seq_idx; | |
2f77fc4b | 105 | |
7076b56e JG |
106 | static int validate_ust_event_name(const char *); |
107 | static int cmd_enable_event_internal(struct ltt_session *session, | |
df4f5a87 | 108 | const struct lttng_domain *domain, |
7076b56e JG |
109 | char *channel_name, struct lttng_event *event, |
110 | char *filter_expression, | |
111 | struct lttng_filter_bytecode *filter, | |
112 | struct lttng_event_exclusion *exclusion, | |
113 | int wpipe); | |
114 | ||
2f77fc4b DG |
115 | /* |
116 | * Create a session path used by list_lttng_sessions for the case that the | |
117 | * session consumer is on the network. | |
118 | */ | |
119 | static int build_network_session_path(char *dst, size_t size, | |
120 | struct ltt_session *session) | |
121 | { | |
122 | int ret, kdata_port, udata_port; | |
123 | struct lttng_uri *kuri = NULL, *uuri = NULL, *uri = NULL; | |
124 | char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX]; | |
125 | ||
126 | assert(session); | |
127 | assert(dst); | |
128 | ||
129 | memset(tmp_urls, 0, sizeof(tmp_urls)); | |
130 | memset(tmp_uurl, 0, sizeof(tmp_uurl)); | |
131 | ||
132 | kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT; | |
133 | ||
134 | if (session->kernel_session && session->kernel_session->consumer) { | |
135 | kuri = &session->kernel_session->consumer->dst.net.control; | |
136 | kdata_port = session->kernel_session->consumer->dst.net.data.port; | |
137 | } | |
138 | ||
139 | if (session->ust_session && session->ust_session->consumer) { | |
140 | uuri = &session->ust_session->consumer->dst.net.control; | |
141 | udata_port = session->ust_session->consumer->dst.net.data.port; | |
142 | } | |
143 | ||
144 | if (uuri == NULL && kuri == NULL) { | |
145 | uri = &session->consumer->dst.net.control; | |
146 | kdata_port = session->consumer->dst.net.data.port; | |
147 | } else if (kuri && uuri) { | |
148 | ret = uri_compare(kuri, uuri); | |
149 | if (ret) { | |
150 | /* Not Equal */ | |
151 | uri = kuri; | |
152 | /* Build uuri URL string */ | |
153 | ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl)); | |
154 | if (ret < 0) { | |
155 | goto error; | |
156 | } | |
157 | } else { | |
158 | uri = kuri; | |
159 | } | |
160 | } else if (kuri && uuri == NULL) { | |
161 | uri = kuri; | |
162 | } else if (uuri && kuri == NULL) { | |
163 | uri = uuri; | |
164 | } | |
165 | ||
166 | ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls)); | |
167 | if (ret < 0) { | |
168 | goto error; | |
169 | } | |
170 | ||
9aa9f900 DG |
171 | /* |
172 | * Do we have a UST url set. If yes, this means we have both kernel and UST | |
173 | * to print. | |
174 | */ | |
9d035200 | 175 | if (*tmp_uurl != '\0') { |
2f77fc4b DG |
176 | ret = snprintf(dst, size, "[K]: %s [data: %d] -- [U]: %s [data: %d]", |
177 | tmp_urls, kdata_port, tmp_uurl, udata_port); | |
178 | } else { | |
9aa9f900 | 179 | int dport; |
bef08707 | 180 | if (kuri || (!kuri && !uuri)) { |
9aa9f900 DG |
181 | dport = kdata_port; |
182 | } else { | |
183 | /* No kernel URI, use the UST port. */ | |
184 | dport = udata_port; | |
185 | } | |
186 | ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, dport); | |
2f77fc4b DG |
187 | } |
188 | ||
189 | error: | |
190 | return ret; | |
191 | } | |
192 | ||
fb83fe64 JD |
193 | /* |
194 | * Get run-time attributes if the session has been started (discarded events, | |
195 | * lost packets). | |
196 | */ | |
197 | static int get_kernel_runtime_stats(struct ltt_session *session, | |
198 | struct ltt_kernel_channel *kchan, uint64_t *discarded_events, | |
199 | uint64_t *lost_packets) | |
200 | { | |
201 | int ret; | |
202 | ||
203 | if (!session->has_been_started) { | |
204 | ret = 0; | |
205 | *discarded_events = 0; | |
206 | *lost_packets = 0; | |
207 | goto end; | |
208 | } | |
209 | ||
e1f3997a | 210 | ret = consumer_get_discarded_events(session->id, kchan->key, |
fb83fe64 JD |
211 | session->kernel_session->consumer, |
212 | discarded_events); | |
213 | if (ret < 0) { | |
214 | goto end; | |
215 | } | |
216 | ||
e1f3997a | 217 | ret = consumer_get_lost_packets(session->id, kchan->key, |
fb83fe64 JD |
218 | session->kernel_session->consumer, |
219 | lost_packets); | |
220 | if (ret < 0) { | |
221 | goto end; | |
222 | } | |
223 | ||
224 | end: | |
225 | return ret; | |
226 | } | |
227 | ||
228 | /* | |
229 | * Get run-time attributes if the session has been started (discarded events, | |
230 | * lost packets). | |
231 | */ | |
232 | static int get_ust_runtime_stats(struct ltt_session *session, | |
233 | struct ltt_ust_channel *uchan, uint64_t *discarded_events, | |
234 | uint64_t *lost_packets) | |
235 | { | |
236 | int ret; | |
237 | struct ltt_ust_session *usess; | |
238 | ||
a91c5803 JG |
239 | if (!discarded_events || !lost_packets) { |
240 | ret = -1; | |
241 | goto end; | |
242 | } | |
243 | ||
fb83fe64 | 244 | usess = session->ust_session; |
a905c624 JG |
245 | assert(discarded_events); |
246 | assert(lost_packets); | |
fb83fe64 JD |
247 | |
248 | if (!usess || !session->has_been_started) { | |
249 | *discarded_events = 0; | |
250 | *lost_packets = 0; | |
251 | ret = 0; | |
252 | goto end; | |
253 | } | |
254 | ||
255 | if (usess->buffer_type == LTTNG_BUFFER_PER_UID) { | |
256 | ret = ust_app_uid_get_channel_runtime_stats(usess->id, | |
257 | &usess->buffer_reg_uid_list, | |
258 | usess->consumer, uchan->id, | |
259 | uchan->attr.overwrite, | |
260 | discarded_events, | |
261 | lost_packets); | |
262 | } else if (usess->buffer_type == LTTNG_BUFFER_PER_PID) { | |
263 | ret = ust_app_pid_get_channel_runtime_stats(usess, | |
264 | uchan, usess->consumer, | |
265 | uchan->attr.overwrite, | |
266 | discarded_events, | |
267 | lost_packets); | |
268 | if (ret < 0) { | |
269 | goto end; | |
270 | } | |
271 | *discarded_events += uchan->per_pid_closed_app_discarded; | |
272 | *lost_packets += uchan->per_pid_closed_app_lost; | |
273 | } else { | |
274 | ERR("Unsupported buffer type"); | |
967bc289 | 275 | assert(0); |
fb83fe64 JD |
276 | ret = -1; |
277 | goto end; | |
278 | } | |
279 | ||
280 | end: | |
281 | return ret; | |
282 | } | |
283 | ||
2f77fc4b DG |
284 | /* |
285 | * Fill lttng_channel array of all channels. | |
286 | */ | |
d449df4a | 287 | static ssize_t list_lttng_channels(enum lttng_domain_type domain, |
fb83fe64 | 288 | struct ltt_session *session, struct lttng_channel *channels, |
cf0bcb51 | 289 | struct lttng_channel_extended *chan_exts) |
2f77fc4b | 290 | { |
d449df4a | 291 | int i = 0, ret = 0; |
2f77fc4b DG |
292 | struct ltt_kernel_channel *kchan; |
293 | ||
294 | DBG("Listing channels for session %s", session->name); | |
295 | ||
296 | switch (domain) { | |
297 | case LTTNG_DOMAIN_KERNEL: | |
298 | /* Kernel channels */ | |
299 | if (session->kernel_session != NULL) { | |
300 | cds_list_for_each_entry(kchan, | |
301 | &session->kernel_session->channel_list.head, list) { | |
fb83fe64 | 302 | uint64_t discarded_events, lost_packets; |
cf0bcb51 JG |
303 | struct lttng_channel_extended *extended; |
304 | ||
305 | extended = (struct lttng_channel_extended *) | |
306 | kchan->channel->attr.extended.ptr; | |
fb83fe64 JD |
307 | |
308 | ret = get_kernel_runtime_stats(session, kchan, | |
309 | &discarded_events, &lost_packets); | |
310 | if (ret < 0) { | |
311 | goto end; | |
312 | } | |
2f77fc4b DG |
313 | /* Copy lttng_channel struct to array */ |
314 | memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel)); | |
315 | channels[i].enabled = kchan->enabled; | |
fb83fe64 JD |
316 | chan_exts[i].discarded_events = |
317 | discarded_events; | |
318 | chan_exts[i].lost_packets = lost_packets; | |
cf0bcb51 JG |
319 | chan_exts[i].monitor_timer_interval = |
320 | extended->monitor_timer_interval; | |
491d1539 | 321 | chan_exts[i].blocking_timeout = 0; |
2f77fc4b DG |
322 | i++; |
323 | } | |
324 | } | |
325 | break; | |
326 | case LTTNG_DOMAIN_UST: | |
327 | { | |
328 | struct lttng_ht_iter iter; | |
329 | struct ltt_ust_channel *uchan; | |
330 | ||
e7fe706f | 331 | rcu_read_lock(); |
2f77fc4b DG |
332 | cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht, |
333 | &iter.iter, uchan, node.node) { | |
a91c5803 | 334 | uint64_t discarded_events = 0, lost_packets = 0; |
fb83fe64 | 335 | |
8ee609c8 MD |
336 | if (lttng_strncpy(channels[i].name, uchan->name, |
337 | LTTNG_SYMBOL_NAME_LEN)) { | |
338 | break; | |
339 | } | |
2f77fc4b DG |
340 | channels[i].attr.overwrite = uchan->attr.overwrite; |
341 | channels[i].attr.subbuf_size = uchan->attr.subbuf_size; | |
342 | channels[i].attr.num_subbuf = uchan->attr.num_subbuf; | |
343 | channels[i].attr.switch_timer_interval = | |
344 | uchan->attr.switch_timer_interval; | |
345 | channels[i].attr.read_timer_interval = | |
346 | uchan->attr.read_timer_interval; | |
347 | channels[i].enabled = uchan->enabled; | |
2f785fe7 DG |
348 | channels[i].attr.tracefile_size = uchan->tracefile_size; |
349 | channels[i].attr.tracefile_count = uchan->tracefile_count; | |
5e4435a4 JG |
350 | |
351 | /* | |
352 | * Map enum lttng_ust_output to enum lttng_event_output. | |
353 | */ | |
2f77fc4b DG |
354 | switch (uchan->attr.output) { |
355 | case LTTNG_UST_MMAP: | |
2f77fc4b DG |
356 | channels[i].attr.output = LTTNG_EVENT_MMAP; |
357 | break; | |
5e4435a4 JG |
358 | default: |
359 | /* | |
360 | * LTTNG_UST_MMAP is the only supported UST | |
361 | * output mode. | |
362 | */ | |
363 | assert(0); | |
364 | break; | |
2f77fc4b | 365 | } |
fb83fe64 | 366 | |
d449df4a MD |
367 | chan_exts[i].monitor_timer_interval = |
368 | uchan->monitor_timer_interval; | |
491d1539 MD |
369 | chan_exts[i].blocking_timeout = |
370 | uchan->attr.u.s.blocking_timeout; | |
d449df4a | 371 | |
fb83fe64 JD |
372 | ret = get_ust_runtime_stats(session, uchan, |
373 | &discarded_events, &lost_packets); | |
374 | if (ret < 0) { | |
375 | break; | |
376 | } | |
377 | chan_exts[i].discarded_events = discarded_events; | |
378 | chan_exts[i].lost_packets = lost_packets; | |
2f77fc4b DG |
379 | i++; |
380 | } | |
e7fe706f | 381 | rcu_read_unlock(); |
2f77fc4b DG |
382 | break; |
383 | } | |
384 | default: | |
385 | break; | |
386 | } | |
fb83fe64 JD |
387 | |
388 | end: | |
d449df4a MD |
389 | if (ret < 0) { |
390 | return -LTTNG_ERR_FATAL; | |
391 | } else { | |
392 | return LTTNG_OK; | |
393 | } | |
2f77fc4b DG |
394 | } |
395 | ||
3c02e545 FD |
396 | static int increment_extended_len(const char *filter_expression, |
397 | struct lttng_event_exclusion *exclusion, | |
398 | const struct lttng_userspace_probe_location *probe_location, | |
399 | size_t *extended_len) | |
b4e3ceb9 | 400 | { |
3c02e545 FD |
401 | int ret = 0; |
402 | ||
b4e3ceb9 PP |
403 | *extended_len += sizeof(struct lttcomm_event_extended_header); |
404 | ||
405 | if (filter_expression) { | |
406 | *extended_len += strlen(filter_expression) + 1; | |
407 | } | |
795d57ce PP |
408 | |
409 | if (exclusion) { | |
410 | *extended_len += exclusion->count * LTTNG_SYMBOL_NAME_LEN; | |
411 | } | |
3c02e545 FD |
412 | |
413 | if (probe_location) { | |
414 | ret = lttng_userspace_probe_location_serialize(probe_location, | |
415 | NULL, NULL); | |
416 | if (ret < 0) { | |
417 | goto end; | |
418 | } | |
419 | *extended_len += ret; | |
420 | } | |
421 | ret = 0; | |
422 | end: | |
423 | return ret; | |
b4e3ceb9 PP |
424 | } |
425 | ||
3c02e545 FD |
426 | static int append_extended_info(const char *filter_expression, |
427 | struct lttng_event_exclusion *exclusion, | |
428 | struct lttng_userspace_probe_location *probe_location, | |
429 | void **extended_at) | |
b4e3ceb9 | 430 | { |
3c02e545 | 431 | int ret = 0; |
b4e3ceb9 | 432 | size_t filter_len = 0; |
795d57ce | 433 | size_t nb_exclusions = 0; |
3c02e545 FD |
434 | size_t userspace_probe_location_len = 0; |
435 | struct lttng_dynamic_buffer location_buffer; | |
436 | struct lttcomm_event_extended_header extended_header; | |
b4e3ceb9 PP |
437 | |
438 | if (filter_expression) { | |
439 | filter_len = strlen(filter_expression) + 1; | |
440 | } | |
441 | ||
795d57ce PP |
442 | if (exclusion) { |
443 | nb_exclusions = exclusion->count; | |
444 | } | |
445 | ||
3c02e545 FD |
446 | if (probe_location) { |
447 | lttng_dynamic_buffer_init(&location_buffer); | |
448 | ret = lttng_userspace_probe_location_serialize(probe_location, | |
449 | &location_buffer, NULL); | |
450 | if (ret < 0) { | |
451 | ret = -1; | |
452 | goto end; | |
453 | } | |
454 | userspace_probe_location_len = location_buffer.size; | |
455 | } | |
456 | ||
795d57ce | 457 | /* Set header fields */ |
b4e3ceb9 | 458 | extended_header.filter_len = filter_len; |
795d57ce | 459 | extended_header.nb_exclusions = nb_exclusions; |
3c02e545 | 460 | extended_header.userspace_probe_location_len = userspace_probe_location_len; |
795d57ce PP |
461 | |
462 | /* Copy header */ | |
b4e3ceb9 PP |
463 | memcpy(*extended_at, &extended_header, sizeof(extended_header)); |
464 | *extended_at += sizeof(extended_header); | |
795d57ce PP |
465 | |
466 | /* Copy filter string */ | |
467 | if (filter_expression) { | |
468 | memcpy(*extended_at, filter_expression, filter_len); | |
469 | *extended_at += filter_len; | |
470 | } | |
471 | ||
472 | /* Copy exclusion names */ | |
473 | if (exclusion) { | |
474 | size_t len = nb_exclusions * LTTNG_SYMBOL_NAME_LEN; | |
475 | ||
476 | memcpy(*extended_at, &exclusion->names, len); | |
477 | *extended_at += len; | |
478 | } | |
3c02e545 FD |
479 | |
480 | if (probe_location) { | |
481 | memcpy(*extended_at, location_buffer.data, location_buffer.size); | |
482 | *extended_at += location_buffer.size; | |
483 | lttng_dynamic_buffer_reset(&location_buffer); | |
484 | } | |
485 | ret = 0; | |
486 | end: | |
487 | return ret; | |
b4e3ceb9 PP |
488 | } |
489 | ||
3c6a091f | 490 | /* |
022d91ba | 491 | * Create a list of agent domain events. |
3c6a091f DG |
492 | * |
493 | * Return number of events in list on success or else a negative value. | |
494 | */ | |
022d91ba | 495 | static int list_lttng_agent_events(struct agent *agt, |
b4e3ceb9 | 496 | struct lttng_event **events, size_t *total_size) |
3c6a091f DG |
497 | { |
498 | int i = 0, ret = 0; | |
499 | unsigned int nb_event = 0; | |
022d91ba | 500 | struct agent_event *event; |
47e52862 | 501 | struct lttng_event *tmp_events = NULL; |
3c6a091f | 502 | struct lttng_ht_iter iter; |
b4e3ceb9 PP |
503 | size_t extended_len = 0; |
504 | void *extended_at; | |
3c6a091f | 505 | |
022d91ba | 506 | assert(agt); |
3c6a091f DG |
507 | assert(events); |
508 | ||
022d91ba | 509 | DBG3("Listing agent events"); |
3c6a091f | 510 | |
e5bbf678 | 511 | rcu_read_lock(); |
022d91ba | 512 | nb_event = lttng_ht_get_count(agt->events); |
e5bbf678 | 513 | rcu_read_unlock(); |
3c6a091f DG |
514 | if (nb_event == 0) { |
515 | ret = nb_event; | |
b4e3ceb9 | 516 | *total_size = 0; |
3c6a091f DG |
517 | goto error; |
518 | } | |
519 | ||
b4e3ceb9 PP |
520 | /* Compute required extended infos size */ |
521 | extended_len = nb_event * sizeof(struct lttcomm_event_extended_header); | |
522 | ||
523 | /* | |
524 | * This is only valid because the commands which add events are | |
525 | * processed in the same thread as the listing. | |
526 | */ | |
527 | rcu_read_lock(); | |
528 | cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) { | |
3c02e545 | 529 | ret = increment_extended_len(event->filter_expression, NULL, NULL, |
795d57ce | 530 | &extended_len); |
3c02e545 FD |
531 | if (ret) { |
532 | DBG("Error computing the length of extended info message"); | |
533 | ret = -LTTNG_ERR_FATAL; | |
534 | goto error; | |
535 | } | |
b4e3ceb9 PP |
536 | } |
537 | rcu_read_unlock(); | |
538 | ||
539 | *total_size = nb_event * sizeof(*tmp_events) + extended_len; | |
540 | tmp_events = zmalloc(*total_size); | |
3c6a091f | 541 | if (!tmp_events) { |
022d91ba | 542 | PERROR("zmalloc agent events session"); |
3c6a091f DG |
543 | ret = -LTTNG_ERR_FATAL; |
544 | goto error; | |
545 | } | |
546 | ||
b4e3ceb9 PP |
547 | extended_at = ((uint8_t *) tmp_events) + |
548 | nb_event * sizeof(struct lttng_event); | |
549 | ||
3c6a091f | 550 | rcu_read_lock(); |
022d91ba | 551 | cds_lfht_for_each_entry(agt->events->ht, &iter.iter, event, node.node) { |
3c6a091f DG |
552 | strncpy(tmp_events[i].name, event->name, sizeof(tmp_events[i].name)); |
553 | tmp_events[i].name[sizeof(tmp_events[i].name) - 1] = '\0'; | |
554 | tmp_events[i].enabled = event->enabled; | |
2106efa0 | 555 | tmp_events[i].loglevel = event->loglevel_value; |
ff94328f | 556 | tmp_events[i].loglevel_type = event->loglevel_type; |
3c6a091f | 557 | i++; |
b4e3ceb9 PP |
558 | |
559 | /* Append extended info */ | |
3c02e545 | 560 | ret = append_extended_info(event->filter_expression, NULL, NULL, |
795d57ce | 561 | &extended_at); |
3c02e545 FD |
562 | if (ret) { |
563 | DBG("Error appending extended info message"); | |
564 | ret = -LTTNG_ERR_FATAL; | |
565 | goto error; | |
566 | } | |
3c6a091f | 567 | } |
3c6a091f DG |
568 | |
569 | *events = tmp_events; | |
570 | ret = nb_event; | |
3c02e545 | 571 | assert(nb_event == i); |
3c6a091f | 572 | |
47e52862 | 573 | end: |
3c02e545 | 574 | rcu_read_unlock(); |
3c6a091f | 575 | return ret; |
47e52862 JG |
576 | error: |
577 | free(tmp_events); | |
578 | goto end; | |
3c6a091f DG |
579 | } |
580 | ||
2f77fc4b DG |
581 | /* |
582 | * Create a list of ust global domain events. | |
583 | */ | |
584 | static int list_lttng_ust_global_events(char *channel_name, | |
b4e3ceb9 PP |
585 | struct ltt_ust_domain_global *ust_global, |
586 | struct lttng_event **events, size_t *total_size) | |
2f77fc4b DG |
587 | { |
588 | int i = 0, ret = 0; | |
589 | unsigned int nb_event = 0; | |
590 | struct lttng_ht_iter iter; | |
591 | struct lttng_ht_node_str *node; | |
592 | struct ltt_ust_channel *uchan; | |
593 | struct ltt_ust_event *uevent; | |
594 | struct lttng_event *tmp; | |
b4e3ceb9 PP |
595 | size_t extended_len = 0; |
596 | void *extended_at; | |
2f77fc4b DG |
597 | |
598 | DBG("Listing UST global events for channel %s", channel_name); | |
599 | ||
600 | rcu_read_lock(); | |
601 | ||
602 | lttng_ht_lookup(ust_global->channels, (void *)channel_name, &iter); | |
603 | node = lttng_ht_iter_get_node_str(&iter); | |
604 | if (node == NULL) { | |
f73fabfd | 605 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
d31d3e8c | 606 | goto end; |
2f77fc4b DG |
607 | } |
608 | ||
609 | uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node); | |
610 | ||
3c442be3 | 611 | nb_event = lttng_ht_get_count(uchan->events); |
2f77fc4b DG |
612 | if (nb_event == 0) { |
613 | ret = nb_event; | |
b4e3ceb9 | 614 | *total_size = 0; |
d31d3e8c | 615 | goto end; |
2f77fc4b DG |
616 | } |
617 | ||
618 | DBG3("Listing UST global %d events", nb_event); | |
619 | ||
b4e3ceb9 PP |
620 | /* Compute required extended infos size */ |
621 | cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) { | |
622 | if (uevent->internal) { | |
623 | nb_event--; | |
624 | continue; | |
625 | } | |
626 | ||
3c02e545 FD |
627 | ret = increment_extended_len(uevent->filter_expression, |
628 | uevent->exclusion, NULL, &extended_len); | |
629 | if (ret) { | |
630 | DBG("Error computing the length of extended info message"); | |
631 | ret = -LTTNG_ERR_FATAL; | |
632 | goto end; | |
633 | } | |
b4e3ceb9 | 634 | } |
d31d3e8c PP |
635 | if (nb_event == 0) { |
636 | /* All events are internal, skip. */ | |
637 | ret = 0; | |
638 | *total_size = 0; | |
639 | goto end; | |
640 | } | |
b4e3ceb9 PP |
641 | |
642 | *total_size = nb_event * sizeof(struct lttng_event) + extended_len; | |
643 | tmp = zmalloc(*total_size); | |
2f77fc4b | 644 | if (tmp == NULL) { |
b12c38df JG |
645 | ret = -LTTNG_ERR_FATAL; |
646 | goto end; | |
2f77fc4b DG |
647 | } |
648 | ||
b4e3ceb9 PP |
649 | extended_at = ((uint8_t *) tmp) + nb_event * sizeof(struct lttng_event); |
650 | ||
2f77fc4b | 651 | cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) { |
3c442be3 JG |
652 | if (uevent->internal) { |
653 | /* This event should remain hidden from clients */ | |
3c442be3 JG |
654 | continue; |
655 | } | |
2f77fc4b DG |
656 | strncpy(tmp[i].name, uevent->attr.name, LTTNG_SYMBOL_NAME_LEN); |
657 | tmp[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
658 | tmp[i].enabled = uevent->enabled; | |
659 | ||
660 | switch (uevent->attr.instrumentation) { | |
661 | case LTTNG_UST_TRACEPOINT: | |
662 | tmp[i].type = LTTNG_EVENT_TRACEPOINT; | |
663 | break; | |
664 | case LTTNG_UST_PROBE: | |
665 | tmp[i].type = LTTNG_EVENT_PROBE; | |
666 | break; | |
667 | case LTTNG_UST_FUNCTION: | |
668 | tmp[i].type = LTTNG_EVENT_FUNCTION; | |
669 | break; | |
670 | } | |
671 | ||
672 | tmp[i].loglevel = uevent->attr.loglevel; | |
673 | switch (uevent->attr.loglevel_type) { | |
674 | case LTTNG_UST_LOGLEVEL_ALL: | |
675 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; | |
676 | break; | |
677 | case LTTNG_UST_LOGLEVEL_RANGE: | |
678 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE; | |
679 | break; | |
680 | case LTTNG_UST_LOGLEVEL_SINGLE: | |
681 | tmp[i].loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE; | |
682 | break; | |
683 | } | |
684 | if (uevent->filter) { | |
685 | tmp[i].filter = 1; | |
686 | } | |
4634f12e JI |
687 | if (uevent->exclusion) { |
688 | tmp[i].exclusion = 1; | |
689 | } | |
2f77fc4b | 690 | i++; |
b4e3ceb9 PP |
691 | |
692 | /* Append extended info */ | |
3c02e545 FD |
693 | ret = append_extended_info(uevent->filter_expression, |
694 | uevent->exclusion, NULL, &extended_at); | |
695 | if (ret) { | |
696 | DBG("Error appending extended info message"); | |
697 | ret = -LTTNG_ERR_FATAL; | |
698 | goto end; | |
699 | } | |
2f77fc4b DG |
700 | } |
701 | ||
702 | ret = nb_event; | |
703 | *events = tmp; | |
d31d3e8c | 704 | end: |
2f77fc4b DG |
705 | rcu_read_unlock(); |
706 | return ret; | |
707 | } | |
708 | ||
709 | /* | |
710 | * Fill lttng_event array of all kernel events in the channel. | |
711 | */ | |
712 | static int list_lttng_kernel_events(char *channel_name, | |
b4e3ceb9 PP |
713 | struct ltt_kernel_session *kernel_session, |
714 | struct lttng_event **events, size_t *total_size) | |
2f77fc4b DG |
715 | { |
716 | int i = 0, ret; | |
717 | unsigned int nb_event; | |
718 | struct ltt_kernel_event *event; | |
719 | struct ltt_kernel_channel *kchan; | |
b4e3ceb9 PP |
720 | size_t extended_len = 0; |
721 | void *extended_at; | |
2f77fc4b DG |
722 | |
723 | kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session); | |
724 | if (kchan == NULL) { | |
f73fabfd | 725 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
2f77fc4b DG |
726 | goto error; |
727 | } | |
728 | ||
729 | nb_event = kchan->event_count; | |
730 | ||
731 | DBG("Listing events for channel %s", kchan->channel->name); | |
732 | ||
733 | if (nb_event == 0) { | |
b4e3ceb9 | 734 | *total_size = 0; |
834978fd | 735 | *events = NULL; |
db906c12 | 736 | goto end; |
2f77fc4b DG |
737 | } |
738 | ||
b4e3ceb9 PP |
739 | /* Compute required extended infos size */ |
740 | cds_list_for_each_entry(event, &kchan->events_list.head, list) { | |
3c02e545 FD |
741 | ret = increment_extended_len(event->filter_expression, NULL, |
742 | event->userspace_probe_location, | |
795d57ce | 743 | &extended_len); |
3c02e545 FD |
744 | if (ret) { |
745 | DBG("Error computing the length of extended info message"); | |
746 | ret = -LTTNG_ERR_FATAL; | |
747 | goto error; | |
748 | } | |
b4e3ceb9 PP |
749 | } |
750 | ||
751 | *total_size = nb_event * sizeof(struct lttng_event) + extended_len; | |
752 | *events = zmalloc(*total_size); | |
2f77fc4b | 753 | if (*events == NULL) { |
3c02e545 | 754 | ret = -LTTNG_ERR_FATAL; |
2f77fc4b DG |
755 | goto error; |
756 | } | |
757 | ||
ab4aa612 | 758 | extended_at = ((void *) *events) + |
b4e3ceb9 PP |
759 | nb_event * sizeof(struct lttng_event); |
760 | ||
2f77fc4b DG |
761 | /* Kernel channels */ |
762 | cds_list_for_each_entry(event, &kchan->events_list.head , list) { | |
763 | strncpy((*events)[i].name, event->event->name, LTTNG_SYMBOL_NAME_LEN); | |
764 | (*events)[i].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0'; | |
765 | (*events)[i].enabled = event->enabled; | |
00f992f2 JG |
766 | (*events)[i].filter = |
767 | (unsigned char) !!event->filter_expression; | |
2f77fc4b DG |
768 | |
769 | switch (event->event->instrumentation) { | |
770 | case LTTNG_KERNEL_TRACEPOINT: | |
771 | (*events)[i].type = LTTNG_EVENT_TRACEPOINT; | |
772 | break; | |
2f77fc4b | 773 | case LTTNG_KERNEL_KRETPROBE: |
1896972b DG |
774 | (*events)[i].type = LTTNG_EVENT_FUNCTION; |
775 | memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe, | |
776 | sizeof(struct lttng_kernel_kprobe)); | |
777 | break; | |
778 | case LTTNG_KERNEL_KPROBE: | |
2f77fc4b DG |
779 | (*events)[i].type = LTTNG_EVENT_PROBE; |
780 | memcpy(&(*events)[i].attr.probe, &event->event->u.kprobe, | |
781 | sizeof(struct lttng_kernel_kprobe)); | |
782 | break; | |
b955b4d4 FD |
783 | case LTTNG_KERNEL_UPROBE: |
784 | (*events)[i].type = LTTNG_EVENT_USERSPACE_PROBE; | |
785 | break; | |
2f77fc4b DG |
786 | case LTTNG_KERNEL_FUNCTION: |
787 | (*events)[i].type = LTTNG_EVENT_FUNCTION; | |
788 | memcpy(&((*events)[i].attr.ftrace), &event->event->u.ftrace, | |
789 | sizeof(struct lttng_kernel_function)); | |
790 | break; | |
791 | case LTTNG_KERNEL_NOOP: | |
792 | (*events)[i].type = LTTNG_EVENT_NOOP; | |
793 | break; | |
794 | case LTTNG_KERNEL_SYSCALL: | |
795 | (*events)[i].type = LTTNG_EVENT_SYSCALL; | |
796 | break; | |
797 | case LTTNG_KERNEL_ALL: | |
1ab8c2ad FD |
798 | /* fall-through. */ |
799 | default: | |
2f77fc4b DG |
800 | assert(0); |
801 | break; | |
802 | } | |
803 | i++; | |
b4e3ceb9 PP |
804 | |
805 | /* Append extended info */ | |
3c02e545 FD |
806 | ret = append_extended_info(event->filter_expression, NULL, |
807 | event->userspace_probe_location, &extended_at); | |
808 | if (ret) { | |
809 | DBG("Error appending extended info message"); | |
810 | ret = -LTTNG_ERR_FATAL; | |
811 | goto error; | |
812 | } | |
2f77fc4b DG |
813 | } |
814 | ||
db906c12 | 815 | end: |
2f77fc4b DG |
816 | return nb_event; |
817 | ||
818 | error: | |
f73fabfd DG |
819 | /* Negate the error code to differentiate the size from an error */ |
820 | return -ret; | |
2f77fc4b DG |
821 | } |
822 | ||
823 | /* | |
824 | * Add URI so the consumer output object. Set the correct path depending on the | |
825 | * domain adding the default trace directory. | |
826 | */ | |
b178f53e JG |
827 | static enum lttng_error_code add_uri_to_consumer( |
828 | const struct ltt_session *session, | |
829 | struct consumer_output *consumer, | |
830 | struct lttng_uri *uri, enum lttng_domain_type domain) | |
2f77fc4b | 831 | { |
b178f53e JG |
832 | int ret; |
833 | enum lttng_error_code ret_code = LTTNG_OK; | |
2f77fc4b DG |
834 | |
835 | assert(uri); | |
836 | ||
837 | if (consumer == NULL) { | |
838 | DBG("No consumer detected. Don't add URI. Stopping."); | |
b178f53e | 839 | ret_code = LTTNG_ERR_NO_CONSUMER; |
2f77fc4b DG |
840 | goto error; |
841 | } | |
842 | ||
843 | switch (domain) { | |
844 | case LTTNG_DOMAIN_KERNEL: | |
b178f53e JG |
845 | ret = lttng_strncpy(consumer->domain_subdir, |
846 | DEFAULT_KERNEL_TRACE_DIR, | |
847 | sizeof(consumer->domain_subdir)); | |
2f77fc4b DG |
848 | break; |
849 | case LTTNG_DOMAIN_UST: | |
b178f53e JG |
850 | ret = lttng_strncpy(consumer->domain_subdir, |
851 | DEFAULT_UST_TRACE_DIR, | |
852 | sizeof(consumer->domain_subdir)); | |
2f77fc4b DG |
853 | break; |
854 | default: | |
855 | /* | |
b178f53e JG |
856 | * This case is possible is we try to add the URI to the global |
857 | * tracing session consumer object which in this case there is | |
858 | * no subdir. | |
2f77fc4b | 859 | */ |
b178f53e JG |
860 | memset(consumer->domain_subdir, 0, |
861 | sizeof(consumer->domain_subdir)); | |
862 | ret = 0; | |
863 | } | |
864 | if (ret) { | |
865 | ERR("Failed to initialize consumer output domain subdirectory"); | |
866 | ret_code = LTTNG_ERR_FATAL; | |
867 | goto error; | |
2f77fc4b DG |
868 | } |
869 | ||
870 | switch (uri->dtype) { | |
871 | case LTTNG_DST_IPV4: | |
872 | case LTTNG_DST_IPV6: | |
873 | DBG2("Setting network URI to consumer"); | |
874 | ||
df75acac DG |
875 | if (consumer->type == CONSUMER_DST_NET) { |
876 | if ((uri->stype == LTTNG_STREAM_CONTROL && | |
785d2d0d DG |
877 | consumer->dst.net.control_isset) || |
878 | (uri->stype == LTTNG_STREAM_DATA && | |
879 | consumer->dst.net.data_isset)) { | |
b178f53e | 880 | ret_code = LTTNG_ERR_URL_EXIST; |
df75acac DG |
881 | goto error; |
882 | } | |
883 | } else { | |
b178f53e | 884 | memset(&consumer->dst, 0, sizeof(consumer->dst)); |
785d2d0d DG |
885 | } |
886 | ||
2f77fc4b | 887 | /* Set URI into consumer output object */ |
b178f53e | 888 | ret = consumer_set_network_uri(session, consumer, uri); |
2f77fc4b | 889 | if (ret < 0) { |
b178f53e | 890 | ret_code = -ret; |
2f77fc4b DG |
891 | goto error; |
892 | } else if (ret == 1) { | |
893 | /* | |
894 | * URI was the same in the consumer so we do not append the subdir | |
895 | * again so to not duplicate output dir. | |
896 | */ | |
b178f53e | 897 | ret_code = LTTNG_OK; |
2f77fc4b DG |
898 | goto error; |
899 | } | |
2f77fc4b DG |
900 | break; |
901 | case LTTNG_DST_PATH: | |
b178f53e JG |
902 | if (*uri->dst.path != '/' || strstr(uri->dst.path, "../")) { |
903 | ret_code = LTTNG_ERR_INVALID; | |
9ac05d92 MD |
904 | goto error; |
905 | } | |
b178f53e JG |
906 | DBG2("Setting trace directory path from URI to %s", |
907 | uri->dst.path); | |
908 | memset(&consumer->dst, 0, sizeof(consumer->dst)); | |
909 | ||
910 | ret = lttng_strncpy(consumer->dst.session_root_path, | |
911 | uri->dst.path, | |
912 | sizeof(consumer->dst.session_root_path)); | |
4df41cad JG |
913 | if (ret) { |
914 | ret_code = LTTNG_ERR_FATAL; | |
915 | goto error; | |
916 | } | |
2f77fc4b DG |
917 | consumer->type = CONSUMER_DST_LOCAL; |
918 | break; | |
919 | } | |
920 | ||
b178f53e | 921 | ret_code = LTTNG_OK; |
2f77fc4b | 922 | error: |
b178f53e | 923 | return ret_code; |
2f77fc4b DG |
924 | } |
925 | ||
926 | /* | |
927 | * Init tracing by creating trace directory and sending fds kernel consumer. | |
928 | */ | |
929 | static int init_kernel_tracing(struct ltt_kernel_session *session) | |
930 | { | |
931 | int ret = 0; | |
932 | struct lttng_ht_iter iter; | |
933 | struct consumer_socket *socket; | |
934 | ||
935 | assert(session); | |
936 | ||
e7fe706f DG |
937 | rcu_read_lock(); |
938 | ||
2f77fc4b DG |
939 | if (session->consumer_fds_sent == 0 && session->consumer != NULL) { |
940 | cds_lfht_for_each_entry(session->consumer->socks->ht, &iter.iter, | |
941 | socket, node.node) { | |
2f77fc4b | 942 | pthread_mutex_lock(socket->lock); |
f50f23d9 | 943 | ret = kernel_consumer_send_session(socket, session); |
2f77fc4b DG |
944 | pthread_mutex_unlock(socket->lock); |
945 | if (ret < 0) { | |
f73fabfd | 946 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
2f77fc4b DG |
947 | goto error; |
948 | } | |
949 | } | |
950 | } | |
951 | ||
952 | error: | |
e7fe706f | 953 | rcu_read_unlock(); |
2f77fc4b DG |
954 | return ret; |
955 | } | |
956 | ||
957 | /* | |
958 | * Create a socket to the relayd using the URI. | |
959 | * | |
960 | * On success, the relayd_sock pointer is set to the created socket. | |
9a654598 | 961 | * Else, it remains untouched and an LTTng error code is returned. |
2f77fc4b | 962 | */ |
9a654598 | 963 | static enum lttng_error_code create_connect_relayd(struct lttng_uri *uri, |
b31610f2 JD |
964 | struct lttcomm_relayd_sock **relayd_sock, |
965 | struct consumer_output *consumer) | |
2f77fc4b DG |
966 | { |
967 | int ret; | |
9a654598 | 968 | enum lttng_error_code status = LTTNG_OK; |
6151a90f | 969 | struct lttcomm_relayd_sock *rsock; |
2f77fc4b | 970 | |
6151a90f JD |
971 | rsock = lttcomm_alloc_relayd_sock(uri, RELAYD_VERSION_COMM_MAJOR, |
972 | RELAYD_VERSION_COMM_MINOR); | |
973 | if (!rsock) { | |
9a654598 | 974 | status = LTTNG_ERR_FATAL; |
2f77fc4b DG |
975 | goto error; |
976 | } | |
977 | ||
ffe60014 DG |
978 | /* |
979 | * Connect to relayd so we can proceed with a session creation. This call | |
980 | * can possibly block for an arbitrary amount of time to set the health | |
981 | * state to be in poll execution. | |
982 | */ | |
983 | health_poll_entry(); | |
6151a90f | 984 | ret = relayd_connect(rsock); |
ffe60014 | 985 | health_poll_exit(); |
2f77fc4b DG |
986 | if (ret < 0) { |
987 | ERR("Unable to reach lttng-relayd"); | |
9a654598 | 988 | status = LTTNG_ERR_RELAYD_CONNECT_FAIL; |
2f77fc4b DG |
989 | goto free_sock; |
990 | } | |
991 | ||
992 | /* Create socket for control stream. */ | |
993 | if (uri->stype == LTTNG_STREAM_CONTROL) { | |
eacb7b6f MD |
994 | uint64_t result_flags; |
995 | ||
2f77fc4b DG |
996 | DBG3("Creating relayd stream socket from URI"); |
997 | ||
998 | /* Check relayd version */ | |
6151a90f | 999 | ret = relayd_version_check(rsock); |
67d5aa28 | 1000 | if (ret == LTTNG_ERR_RELAYD_VERSION_FAIL) { |
9a654598 | 1001 | status = LTTNG_ERR_RELAYD_VERSION_FAIL; |
67d5aa28 JD |
1002 | goto close_sock; |
1003 | } else if (ret < 0) { | |
1004 | ERR("Unable to reach lttng-relayd"); | |
9a654598 | 1005 | status = LTTNG_ERR_RELAYD_CONNECT_FAIL; |
2f77fc4b DG |
1006 | goto close_sock; |
1007 | } | |
b31610f2 JD |
1008 | consumer->relay_major_version = rsock->major; |
1009 | consumer->relay_minor_version = rsock->minor; | |
eacb7b6f MD |
1010 | ret = relayd_get_configuration(rsock, 0, |
1011 | &result_flags); | |
1012 | if (ret < 0) { | |
1013 | ERR("Unable to get relayd configuration"); | |
1014 | status = LTTNG_ERR_RELAYD_CONNECT_FAIL; | |
1015 | goto close_sock; | |
1016 | } | |
1017 | if (result_flags & LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED) { | |
1018 | consumer->relay_allows_clear = true; | |
1019 | } | |
2f77fc4b DG |
1020 | } else if (uri->stype == LTTNG_STREAM_DATA) { |
1021 | DBG3("Creating relayd data socket from URI"); | |
1022 | } else { | |
1023 | /* Command is not valid */ | |
1024 | ERR("Relayd invalid stream type: %d", uri->stype); | |
9a654598 | 1025 | status = LTTNG_ERR_INVALID; |
2f77fc4b DG |
1026 | goto close_sock; |
1027 | } | |
1028 | ||
6151a90f | 1029 | *relayd_sock = rsock; |
2f77fc4b | 1030 | |
9a654598 | 1031 | return status; |
2f77fc4b DG |
1032 | |
1033 | close_sock: | |
6151a90f JD |
1034 | /* The returned value is not useful since we are on an error path. */ |
1035 | (void) relayd_close(rsock); | |
2f77fc4b | 1036 | free_sock: |
6151a90f | 1037 | free(rsock); |
2f77fc4b | 1038 | error: |
9a654598 | 1039 | return status; |
2f77fc4b DG |
1040 | } |
1041 | ||
1042 | /* | |
1043 | * Connect to the relayd using URI and send the socket to the right consumer. | |
43fade62 JG |
1044 | * |
1045 | * The consumer socket lock must be held by the caller. | |
9a654598 JG |
1046 | * |
1047 | * Returns LTTNG_OK on success or an LTTng error code on failure. | |
2f77fc4b | 1048 | */ |
9a654598 JG |
1049 | static enum lttng_error_code send_consumer_relayd_socket( |
1050 | unsigned int session_id, | |
3044f922 | 1051 | struct lttng_uri *relayd_uri, |
56a37563 | 1052 | struct consumer_output *consumer, |
d3e2ba59 | 1053 | struct consumer_socket *consumer_sock, |
fb9a95c4 | 1054 | const char *session_name, const char *hostname, |
6fa5fe7c | 1055 | const char *base_path, int session_live_timer, |
db1da059 | 1056 | const uint64_t *current_chunk_id, |
46ef2188 MD |
1057 | time_t session_creation_time, |
1058 | bool session_name_contains_creation_time) | |
2f77fc4b DG |
1059 | { |
1060 | int ret; | |
6151a90f | 1061 | struct lttcomm_relayd_sock *rsock = NULL; |
9a654598 | 1062 | enum lttng_error_code status; |
2f77fc4b | 1063 | |
ffe60014 | 1064 | /* Connect to relayd and make version check if uri is the control. */ |
9a654598 JG |
1065 | status = create_connect_relayd(relayd_uri, &rsock, consumer); |
1066 | if (status != LTTNG_OK) { | |
9e218353 | 1067 | goto relayd_comm_error; |
ffe60014 | 1068 | } |
6151a90f | 1069 | assert(rsock); |
ffe60014 | 1070 | |
2f77fc4b | 1071 | /* Set the network sequence index if not set. */ |
d88aee68 DG |
1072 | if (consumer->net_seq_index == (uint64_t) -1ULL) { |
1073 | pthread_mutex_lock(&relayd_net_seq_idx_lock); | |
2f77fc4b DG |
1074 | /* |
1075 | * Increment net_seq_idx because we are about to transfer the | |
1076 | * new relayd socket to the consumer. | |
d88aee68 | 1077 | * Assign unique key so the consumer can match streams. |
2f77fc4b | 1078 | */ |
d88aee68 DG |
1079 | consumer->net_seq_index = ++relayd_net_seq_idx; |
1080 | pthread_mutex_unlock(&relayd_net_seq_idx_lock); | |
2f77fc4b DG |
1081 | } |
1082 | ||
2f77fc4b | 1083 | /* Send relayd socket to consumer. */ |
6151a90f | 1084 | ret = consumer_send_relayd_socket(consumer_sock, rsock, consumer, |
d3e2ba59 | 1085 | relayd_uri->stype, session_id, |
6fa5fe7c MD |
1086 | session_name, hostname, base_path, |
1087 | session_live_timer, current_chunk_id, | |
46ef2188 | 1088 | session_creation_time, session_name_contains_creation_time); |
2f77fc4b | 1089 | if (ret < 0) { |
9a654598 | 1090 | status = LTTNG_ERR_ENABLE_CONSUMER_FAIL; |
2f77fc4b DG |
1091 | goto close_sock; |
1092 | } | |
1093 | ||
c890b720 DG |
1094 | /* Flag that the corresponding socket was sent. */ |
1095 | if (relayd_uri->stype == LTTNG_STREAM_CONTROL) { | |
ffe60014 | 1096 | consumer_sock->control_sock_sent = 1; |
c890b720 | 1097 | } else if (relayd_uri->stype == LTTNG_STREAM_DATA) { |
ffe60014 | 1098 | consumer_sock->data_sock_sent = 1; |
c890b720 DG |
1099 | } |
1100 | ||
2f77fc4b DG |
1101 | /* |
1102 | * Close socket which was dup on the consumer side. The session daemon does | |
1103 | * NOT keep track of the relayd socket(s) once transfer to the consumer. | |
1104 | */ | |
1105 | ||
1106 | close_sock: | |
9a654598 | 1107 | if (status != LTTNG_OK) { |
ffe60014 | 1108 | /* |
d9078d0c DG |
1109 | * The consumer output for this session should not be used anymore |
1110 | * since the relayd connection failed thus making any tracing or/and | |
1111 | * streaming not usable. | |
ffe60014 | 1112 | */ |
d9078d0c | 1113 | consumer->enabled = 0; |
ffe60014 | 1114 | } |
9e218353 JR |
1115 | (void) relayd_close(rsock); |
1116 | free(rsock); | |
1117 | ||
1118 | relayd_comm_error: | |
9a654598 | 1119 | return status; |
2f77fc4b DG |
1120 | } |
1121 | ||
1122 | /* | |
1123 | * Send both relayd sockets to a specific consumer and domain. This is a | |
1124 | * helper function to facilitate sending the information to the consumer for a | |
1125 | * session. | |
43fade62 JG |
1126 | * |
1127 | * The consumer socket lock must be held by the caller. | |
9a654598 JG |
1128 | * |
1129 | * Returns LTTNG_OK, or an LTTng error code on failure. | |
2f77fc4b | 1130 | */ |
9a654598 JG |
1131 | static enum lttng_error_code send_consumer_relayd_sockets( |
1132 | enum lttng_domain_type domain, | |
56a37563 | 1133 | unsigned int session_id, struct consumer_output *consumer, |
fb9a95c4 | 1134 | struct consumer_socket *sock, const char *session_name, |
6fa5fe7c | 1135 | const char *hostname, const char *base_path, int session_live_timer, |
46ef2188 MD |
1136 | const uint64_t *current_chunk_id, time_t session_creation_time, |
1137 | bool session_name_contains_creation_time) | |
2f77fc4b | 1138 | { |
9a654598 | 1139 | enum lttng_error_code status = LTTNG_OK; |
2f77fc4b | 1140 | |
2f77fc4b | 1141 | assert(consumer); |
6dc3064a | 1142 | assert(sock); |
2f77fc4b | 1143 | |
2f77fc4b | 1144 | /* Sending control relayd socket. */ |
ffe60014 | 1145 | if (!sock->control_sock_sent) { |
9a654598 | 1146 | status = send_consumer_relayd_socket(session_id, |
d3e2ba59 | 1147 | &consumer->dst.net.control, consumer, sock, |
6fa5fe7c | 1148 | session_name, hostname, base_path, session_live_timer, |
46ef2188 MD |
1149 | current_chunk_id, session_creation_time, |
1150 | session_name_contains_creation_time); | |
9a654598 | 1151 | if (status != LTTNG_OK) { |
c890b720 DG |
1152 | goto error; |
1153 | } | |
2f77fc4b DG |
1154 | } |
1155 | ||
1156 | /* Sending data relayd socket. */ | |
ffe60014 | 1157 | if (!sock->data_sock_sent) { |
9a654598 | 1158 | status = send_consumer_relayd_socket(session_id, |
d3e2ba59 | 1159 | &consumer->dst.net.data, consumer, sock, |
6fa5fe7c | 1160 | session_name, hostname, base_path, session_live_timer, |
46ef2188 MD |
1161 | current_chunk_id, session_creation_time, |
1162 | session_name_contains_creation_time); | |
9a654598 | 1163 | if (status != LTTNG_OK) { |
c890b720 DG |
1164 | goto error; |
1165 | } | |
2f77fc4b DG |
1166 | } |
1167 | ||
2f77fc4b | 1168 | error: |
9a654598 | 1169 | return status; |
2f77fc4b DG |
1170 | } |
1171 | ||
1172 | /* | |
1173 | * Setup relayd connections for a tracing session. First creates the socket to | |
1174 | * the relayd and send them to the right domain consumer. Consumer type MUST be | |
1175 | * network. | |
1176 | */ | |
ffe60014 | 1177 | int cmd_setup_relayd(struct ltt_session *session) |
2f77fc4b | 1178 | { |
f73fabfd | 1179 | int ret = LTTNG_OK; |
2f77fc4b DG |
1180 | struct ltt_ust_session *usess; |
1181 | struct ltt_kernel_session *ksess; | |
1182 | struct consumer_socket *socket; | |
1183 | struct lttng_ht_iter iter; | |
1e791a74 | 1184 | LTTNG_OPTIONAL(uint64_t) current_chunk_id = {}; |
2f77fc4b | 1185 | |
1e791a74 | 1186 | assert(session); |
2f77fc4b DG |
1187 | |
1188 | usess = session->ust_session; | |
1189 | ksess = session->kernel_session; | |
1190 | ||
785d2d0d | 1191 | DBG("Setting relayd for session %s", session->name); |
2f77fc4b | 1192 | |
aa997ea3 | 1193 | rcu_read_lock(); |
1e791a74 JG |
1194 | if (session->current_trace_chunk) { |
1195 | enum lttng_trace_chunk_status status = lttng_trace_chunk_get_id( | |
1196 | session->current_trace_chunk, ¤t_chunk_id.value); | |
1197 | ||
1198 | if (status == LTTNG_TRACE_CHUNK_STATUS_OK) { | |
1199 | current_chunk_id.is_set = true; | |
1200 | } else { | |
1201 | ERR("Failed to get current trace chunk id"); | |
1202 | ret = LTTNG_ERR_UNK; | |
1203 | goto error; | |
1204 | } | |
1205 | } | |
1206 | ||
2f77fc4b DG |
1207 | if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET |
1208 | && usess->consumer->enabled) { | |
1209 | /* For each consumer socket, send relayd sockets */ | |
1210 | cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter, | |
1211 | socket, node.node) { | |
2f77fc4b | 1212 | pthread_mutex_lock(socket->lock); |
6dc3064a | 1213 | ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_UST, session->id, |
d3e2ba59 JD |
1214 | usess->consumer, socket, |
1215 | session->name, session->hostname, | |
6fa5fe7c | 1216 | session->base_path, |
1e791a74 | 1217 | session->live_timer, |
db1da059 | 1218 | current_chunk_id.is_set ? ¤t_chunk_id.value : NULL, |
46ef2188 MD |
1219 | session->creation_time, |
1220 | session->name_contains_creation_time); | |
2f77fc4b | 1221 | pthread_mutex_unlock(socket->lock); |
f73fabfd | 1222 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1223 | goto error; |
1224 | } | |
6dc3064a DG |
1225 | /* Session is now ready for network streaming. */ |
1226 | session->net_handle = 1; | |
2f77fc4b | 1227 | } |
b31610f2 JD |
1228 | session->consumer->relay_major_version = |
1229 | usess->consumer->relay_major_version; | |
1230 | session->consumer->relay_minor_version = | |
1231 | usess->consumer->relay_minor_version; | |
eacb7b6f MD |
1232 | session->consumer->relay_allows_clear = |
1233 | usess->consumer->relay_allows_clear; | |
2f77fc4b DG |
1234 | } |
1235 | ||
1236 | if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET | |
1237 | && ksess->consumer->enabled) { | |
1238 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter, | |
1239 | socket, node.node) { | |
2f77fc4b | 1240 | pthread_mutex_lock(socket->lock); |
6dc3064a | 1241 | ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL, session->id, |
d3e2ba59 JD |
1242 | ksess->consumer, socket, |
1243 | session->name, session->hostname, | |
6fa5fe7c | 1244 | session->base_path, |
1e791a74 | 1245 | session->live_timer, |
db1da059 | 1246 | current_chunk_id.is_set ? ¤t_chunk_id.value : NULL, |
46ef2188 MD |
1247 | session->creation_time, |
1248 | session->name_contains_creation_time); | |
2f77fc4b | 1249 | pthread_mutex_unlock(socket->lock); |
f73fabfd | 1250 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1251 | goto error; |
1252 | } | |
6dc3064a DG |
1253 | /* Session is now ready for network streaming. */ |
1254 | session->net_handle = 1; | |
2f77fc4b | 1255 | } |
b31610f2 JD |
1256 | session->consumer->relay_major_version = |
1257 | ksess->consumer->relay_major_version; | |
1258 | session->consumer->relay_minor_version = | |
1259 | ksess->consumer->relay_minor_version; | |
eacb7b6f MD |
1260 | session->consumer->relay_allows_clear = |
1261 | ksess->consumer->relay_allows_clear; | |
2f77fc4b DG |
1262 | } |
1263 | ||
1264 | error: | |
e7fe706f | 1265 | rcu_read_unlock(); |
2f77fc4b DG |
1266 | return ret; |
1267 | } | |
1268 | ||
9b6c7ec5 DG |
1269 | /* |
1270 | * Start a kernel session by opening all necessary streams. | |
1271 | */ | |
4dbe1875 | 1272 | int start_kernel_session(struct ltt_kernel_session *ksess) |
9b6c7ec5 DG |
1273 | { |
1274 | int ret; | |
1275 | struct ltt_kernel_channel *kchan; | |
1276 | ||
1277 | /* Open kernel metadata */ | |
07b86b52 | 1278 | if (ksess->metadata == NULL && ksess->output_traces) { |
9b6c7ec5 DG |
1279 | ret = kernel_open_metadata(ksess); |
1280 | if (ret < 0) { | |
1281 | ret = LTTNG_ERR_KERN_META_FAIL; | |
1282 | goto error; | |
1283 | } | |
1284 | } | |
1285 | ||
1286 | /* Open kernel metadata stream */ | |
07b86b52 | 1287 | if (ksess->metadata && ksess->metadata_stream_fd < 0) { |
9b6c7ec5 DG |
1288 | ret = kernel_open_metadata_stream(ksess); |
1289 | if (ret < 0) { | |
1290 | ERR("Kernel create metadata stream failed"); | |
1291 | ret = LTTNG_ERR_KERN_STREAM_FAIL; | |
1292 | goto error; | |
1293 | } | |
1294 | } | |
1295 | ||
1296 | /* For each channel */ | |
1297 | cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) { | |
1298 | if (kchan->stream_count == 0) { | |
1299 | ret = kernel_open_channel_stream(kchan); | |
1300 | if (ret < 0) { | |
1301 | ret = LTTNG_ERR_KERN_STREAM_FAIL; | |
1302 | goto error; | |
1303 | } | |
1304 | /* Update the stream global counter */ | |
1305 | ksess->stream_count_global += ret; | |
1306 | } | |
1307 | } | |
1308 | ||
1309 | /* Setup kernel consumer socket and send fds to it */ | |
1310 | ret = init_kernel_tracing(ksess); | |
e43c41c5 | 1311 | if (ret != 0) { |
9b6c7ec5 DG |
1312 | ret = LTTNG_ERR_KERN_START_FAIL; |
1313 | goto error; | |
1314 | } | |
1315 | ||
1316 | /* This start the kernel tracing */ | |
1317 | ret = kernel_start_session(ksess); | |
1318 | if (ret < 0) { | |
1319 | ret = LTTNG_ERR_KERN_START_FAIL; | |
1320 | goto error; | |
1321 | } | |
1322 | ||
1323 | /* Quiescent wait after starting trace */ | |
7d268848 | 1324 | kernel_wait_quiescent(); |
9b6c7ec5 | 1325 | |
14fb1ebe | 1326 | ksess->active = 1; |
9b6c7ec5 DG |
1327 | |
1328 | ret = LTTNG_OK; | |
1329 | ||
1330 | error: | |
1331 | return ret; | |
1332 | } | |
1333 | ||
4dbe1875 MD |
1334 | int stop_kernel_session(struct ltt_kernel_session *ksess) |
1335 | { | |
1336 | struct ltt_kernel_channel *kchan; | |
1337 | bool error_occurred = false; | |
1338 | int ret; | |
1339 | ||
1340 | if (!ksess || !ksess->active) { | |
1341 | return LTTNG_OK; | |
1342 | } | |
1343 | DBG("Stopping kernel tracing"); | |
1344 | ||
1345 | ret = kernel_stop_session(ksess); | |
1346 | if (ret < 0) { | |
1347 | ret = LTTNG_ERR_KERN_STOP_FAIL; | |
1348 | goto error; | |
1349 | } | |
1350 | ||
1351 | kernel_wait_quiescent(); | |
1352 | ||
1353 | /* Flush metadata after stopping (if exists) */ | |
1354 | if (ksess->metadata_stream_fd >= 0) { | |
1355 | ret = kernel_metadata_flush_buffer(ksess->metadata_stream_fd); | |
1356 | if (ret < 0) { | |
1357 | ERR("Kernel metadata flush failed"); | |
1358 | error_occurred = true; | |
1359 | } | |
1360 | } | |
1361 | ||
1362 | /* Flush all buffers after stopping */ | |
1363 | cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) { | |
1364 | ret = kernel_flush_buffer(kchan); | |
1365 | if (ret < 0) { | |
1366 | ERR("Kernel flush buffer error"); | |
1367 | error_occurred = true; | |
1368 | } | |
1369 | } | |
1370 | ||
1371 | ksess->active = 0; | |
1372 | if (error_occurred) { | |
1373 | ret = LTTNG_ERR_UNK; | |
1374 | } else { | |
1375 | ret = LTTNG_OK; | |
1376 | } | |
1377 | error: | |
1378 | return ret; | |
1379 | } | |
1380 | ||
2f77fc4b DG |
1381 | /* |
1382 | * Command LTTNG_DISABLE_CHANNEL processed by the client thread. | |
1383 | */ | |
56a37563 JG |
1384 | int cmd_disable_channel(struct ltt_session *session, |
1385 | enum lttng_domain_type domain, char *channel_name) | |
2f77fc4b DG |
1386 | { |
1387 | int ret; | |
1388 | struct ltt_ust_session *usess; | |
1389 | ||
1390 | usess = session->ust_session; | |
1391 | ||
2223c96f DG |
1392 | rcu_read_lock(); |
1393 | ||
2f77fc4b DG |
1394 | switch (domain) { |
1395 | case LTTNG_DOMAIN_KERNEL: | |
1396 | { | |
1397 | ret = channel_kernel_disable(session->kernel_session, | |
1398 | channel_name); | |
f73fabfd | 1399 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1400 | goto error; |
1401 | } | |
1402 | ||
7d268848 | 1403 | kernel_wait_quiescent(); |
2f77fc4b DG |
1404 | break; |
1405 | } | |
1406 | case LTTNG_DOMAIN_UST: | |
1407 | { | |
1408 | struct ltt_ust_channel *uchan; | |
1409 | struct lttng_ht *chan_ht; | |
1410 | ||
1411 | chan_ht = usess->domain_global.channels; | |
1412 | ||
1413 | uchan = trace_ust_find_channel_by_name(chan_ht, channel_name); | |
1414 | if (uchan == NULL) { | |
f73fabfd | 1415 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
2f77fc4b DG |
1416 | goto error; |
1417 | } | |
1418 | ||
7972aab2 | 1419 | ret = channel_ust_disable(usess, uchan); |
f73fabfd | 1420 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1421 | goto error; |
1422 | } | |
1423 | break; | |
1424 | } | |
2f77fc4b | 1425 | default: |
f73fabfd | 1426 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
2f77fc4b DG |
1427 | goto error; |
1428 | } | |
1429 | ||
f73fabfd | 1430 | ret = LTTNG_OK; |
2f77fc4b DG |
1431 | |
1432 | error: | |
2223c96f | 1433 | rcu_read_unlock(); |
2f77fc4b DG |
1434 | return ret; |
1435 | } | |
1436 | ||
1437 | /* | |
1438 | * Command LTTNG_ENABLE_CHANNEL processed by the client thread. | |
1439 | * | |
1440 | * The wpipe arguments is used as a notifier for the kernel thread. | |
1441 | */ | |
1442 | int cmd_enable_channel(struct ltt_session *session, | |
df4f5a87 | 1443 | const struct lttng_domain *domain, const struct lttng_channel *_attr, int wpipe) |
2f77fc4b DG |
1444 | { |
1445 | int ret; | |
1446 | struct ltt_ust_session *usess = session->ust_session; | |
1447 | struct lttng_ht *chan_ht; | |
1f345e94 | 1448 | size_t len; |
df4f5a87 | 1449 | struct lttng_channel attr; |
2f77fc4b DG |
1450 | |
1451 | assert(session); | |
df4f5a87 | 1452 | assert(_attr); |
7972aab2 | 1453 | assert(domain); |
2f77fc4b | 1454 | |
df4f5a87 JG |
1455 | attr = *_attr; |
1456 | len = lttng_strnlen(attr.name, sizeof(attr.name)); | |
1f345e94 PP |
1457 | |
1458 | /* Validate channel name */ | |
df4f5a87 JG |
1459 | if (attr.name[0] == '.' || |
1460 | memchr(attr.name, '/', len) != NULL) { | |
1f345e94 PP |
1461 | ret = LTTNG_ERR_INVALID_CHANNEL_NAME; |
1462 | goto end; | |
1463 | } | |
1464 | ||
df4f5a87 | 1465 | DBG("Enabling channel %s for session %s", attr.name, session->name); |
2f77fc4b | 1466 | |
03b4fdcf DG |
1467 | rcu_read_lock(); |
1468 | ||
ecc48a90 JD |
1469 | /* |
1470 | * Don't try to enable a channel if the session has been started at | |
1471 | * some point in time before. The tracer does not allow it. | |
1472 | */ | |
8382cf6f | 1473 | if (session->has_been_started) { |
ecc48a90 JD |
1474 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
1475 | goto error; | |
1476 | } | |
1477 | ||
1478 | /* | |
1479 | * If the session is a live session, remove the switch timer, the | |
1480 | * live timer does the same thing but sends also synchronisation | |
1481 | * beacons for inactive streams. | |
1482 | */ | |
1483 | if (session->live_timer > 0) { | |
df4f5a87 JG |
1484 | attr.attr.live_timer_interval = session->live_timer; |
1485 | attr.attr.switch_timer_interval = 0; | |
ecc48a90 JD |
1486 | } |
1487 | ||
6e21424e JR |
1488 | /* Check for feature support */ |
1489 | switch (domain->type) { | |
1490 | case LTTNG_DOMAIN_KERNEL: | |
1491 | { | |
7d268848 | 1492 | if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) { |
6e21424e JR |
1493 | /* Sampling position of buffer is not supported */ |
1494 | WARN("Kernel tracer does not support buffer monitoring. " | |
1495 | "Setting the monitor interval timer to 0 " | |
1496 | "(disabled) for channel '%s' of session '%s'", | |
df4f5a87 JG |
1497 | attr.name, session->name); |
1498 | lttng_channel_set_monitor_timer_interval(&attr, 0); | |
6e21424e JR |
1499 | } |
1500 | break; | |
1501 | } | |
1502 | case LTTNG_DOMAIN_UST: | |
f28f9e44 | 1503 | break; |
6e21424e JR |
1504 | case LTTNG_DOMAIN_JUL: |
1505 | case LTTNG_DOMAIN_LOG4J: | |
1506 | case LTTNG_DOMAIN_PYTHON: | |
f28f9e44 JG |
1507 | if (!agent_tracing_is_enabled()) { |
1508 | DBG("Attempted to enable a channel in an agent domain but the agent thread is not running"); | |
1509 | ret = LTTNG_ERR_AGENT_TRACING_DISABLED; | |
1510 | goto error; | |
1511 | } | |
6e21424e JR |
1512 | break; |
1513 | default: | |
1514 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; | |
1515 | goto error; | |
1516 | } | |
1517 | ||
7972aab2 | 1518 | switch (domain->type) { |
2f77fc4b DG |
1519 | case LTTNG_DOMAIN_KERNEL: |
1520 | { | |
1521 | struct ltt_kernel_channel *kchan; | |
1522 | ||
df4f5a87 | 1523 | kchan = trace_kernel_get_channel_by_name(attr.name, |
2f77fc4b DG |
1524 | session->kernel_session); |
1525 | if (kchan == NULL) { | |
54213acc JG |
1526 | if (session->snapshot.nb_output > 0 || |
1527 | session->snapshot_mode) { | |
1528 | /* Enforce mmap output for snapshot sessions. */ | |
df4f5a87 | 1529 | attr.attr.output = LTTNG_EVENT_MMAP; |
54213acc | 1530 | } |
df4f5a87 JG |
1531 | ret = channel_kernel_create(session->kernel_session, &attr, wpipe); |
1532 | if (attr.name[0] != '\0') { | |
85076754 MD |
1533 | session->kernel_session->has_non_default_channel = 1; |
1534 | } | |
2f77fc4b DG |
1535 | } else { |
1536 | ret = channel_kernel_enable(session->kernel_session, kchan); | |
1537 | } | |
1538 | ||
f73fabfd | 1539 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1540 | goto error; |
1541 | } | |
1542 | ||
7d268848 | 1543 | kernel_wait_quiescent(); |
2f77fc4b DG |
1544 | break; |
1545 | } | |
1546 | case LTTNG_DOMAIN_UST: | |
9232818f JG |
1547 | case LTTNG_DOMAIN_JUL: |
1548 | case LTTNG_DOMAIN_LOG4J: | |
1549 | case LTTNG_DOMAIN_PYTHON: | |
2f77fc4b DG |
1550 | { |
1551 | struct ltt_ust_channel *uchan; | |
1552 | ||
9232818f JG |
1553 | /* |
1554 | * FIXME | |
1555 | * | |
1556 | * Current agent implementation limitations force us to allow | |
1557 | * only one channel at once in "agent" subdomains. Each | |
1558 | * subdomain has a default channel name which must be strictly | |
1559 | * adhered to. | |
1560 | */ | |
1561 | if (domain->type == LTTNG_DOMAIN_JUL) { | |
df4f5a87 | 1562 | if (strncmp(attr.name, DEFAULT_JUL_CHANNEL_NAME, |
9232818f JG |
1563 | LTTNG_SYMBOL_NAME_LEN)) { |
1564 | ret = LTTNG_ERR_INVALID_CHANNEL_NAME; | |
1565 | goto error; | |
1566 | } | |
1567 | } else if (domain->type == LTTNG_DOMAIN_LOG4J) { | |
df4f5a87 | 1568 | if (strncmp(attr.name, DEFAULT_LOG4J_CHANNEL_NAME, |
9232818f JG |
1569 | LTTNG_SYMBOL_NAME_LEN)) { |
1570 | ret = LTTNG_ERR_INVALID_CHANNEL_NAME; | |
1571 | goto error; | |
1572 | } | |
1573 | } else if (domain->type == LTTNG_DOMAIN_PYTHON) { | |
df4f5a87 | 1574 | if (strncmp(attr.name, DEFAULT_PYTHON_CHANNEL_NAME, |
9232818f JG |
1575 | LTTNG_SYMBOL_NAME_LEN)) { |
1576 | ret = LTTNG_ERR_INVALID_CHANNEL_NAME; | |
1577 | goto error; | |
1578 | } | |
1579 | } | |
1580 | ||
2f77fc4b DG |
1581 | chan_ht = usess->domain_global.channels; |
1582 | ||
df4f5a87 | 1583 | uchan = trace_ust_find_channel_by_name(chan_ht, attr.name); |
2f77fc4b | 1584 | if (uchan == NULL) { |
df4f5a87 JG |
1585 | ret = channel_ust_create(usess, &attr, domain->buf_type); |
1586 | if (attr.name[0] != '\0') { | |
85076754 MD |
1587 | usess->has_non_default_channel = 1; |
1588 | } | |
2f77fc4b | 1589 | } else { |
7972aab2 | 1590 | ret = channel_ust_enable(usess, uchan); |
2f77fc4b DG |
1591 | } |
1592 | break; | |
1593 | } | |
2f77fc4b | 1594 | default: |
f73fabfd | 1595 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
2f77fc4b DG |
1596 | goto error; |
1597 | } | |
1598 | ||
df4f5a87 | 1599 | if (ret == LTTNG_OK && attr.attr.output != LTTNG_EVENT_MMAP) { |
54213acc JG |
1600 | session->has_non_mmap_channel = true; |
1601 | } | |
2f77fc4b | 1602 | error: |
2223c96f | 1603 | rcu_read_unlock(); |
1f345e94 | 1604 | end: |
2f77fc4b DG |
1605 | return ret; |
1606 | } | |
1607 | ||
159b042f JG |
1608 | enum lttng_error_code cmd_process_attr_tracker_get_tracking_policy( |
1609 | struct ltt_session *session, | |
1610 | enum lttng_domain_type domain, | |
1611 | enum lttng_process_attr process_attr, | |
1612 | enum lttng_tracking_policy *policy) | |
1613 | { | |
1614 | enum lttng_error_code ret_code = LTTNG_OK; | |
1615 | const struct process_attr_tracker *tracker; | |
1616 | ||
1617 | switch (domain) { | |
1618 | case LTTNG_DOMAIN_KERNEL: | |
1619 | if (!session->kernel_session) { | |
1620 | ret_code = LTTNG_ERR_INVALID; | |
1621 | goto end; | |
1622 | } | |
1623 | tracker = kernel_get_process_attr_tracker( | |
1624 | session->kernel_session, process_attr); | |
1625 | break; | |
1626 | case LTTNG_DOMAIN_UST: | |
1627 | if (!session->ust_session) { | |
1628 | ret_code = LTTNG_ERR_INVALID; | |
1629 | goto end; | |
1630 | } | |
1631 | tracker = trace_ust_get_process_attr_tracker( | |
1632 | session->ust_session, process_attr); | |
1633 | break; | |
1634 | default: | |
1635 | ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN; | |
1636 | goto end; | |
1637 | } | |
1638 | if (tracker) { | |
1639 | *policy = process_attr_tracker_get_tracking_policy(tracker); | |
1640 | } else { | |
1641 | ret_code = LTTNG_ERR_INVALID; | |
1642 | } | |
1643 | end: | |
1644 | return ret_code; | |
1645 | } | |
1646 | ||
1647 | enum lttng_error_code cmd_process_attr_tracker_set_tracking_policy( | |
1648 | struct ltt_session *session, | |
1649 | enum lttng_domain_type domain, | |
1650 | enum lttng_process_attr process_attr, | |
1651 | enum lttng_tracking_policy policy) | |
1652 | { | |
1653 | enum lttng_error_code ret_code = LTTNG_OK; | |
1654 | ||
1655 | switch (policy) { | |
1656 | case LTTNG_TRACKING_POLICY_INCLUDE_SET: | |
1657 | case LTTNG_TRACKING_POLICY_EXCLUDE_ALL: | |
1658 | case LTTNG_TRACKING_POLICY_INCLUDE_ALL: | |
1659 | break; | |
1660 | default: | |
1661 | ret_code = LTTNG_ERR_INVALID; | |
1662 | goto end; | |
1663 | } | |
1664 | ||
1665 | switch (domain) { | |
1666 | case LTTNG_DOMAIN_KERNEL: | |
1667 | if (!session->kernel_session) { | |
1668 | ret_code = LTTNG_ERR_INVALID; | |
1669 | goto end; | |
1670 | } | |
1671 | ret_code = kernel_process_attr_tracker_set_tracking_policy( | |
1672 | session->kernel_session, process_attr, policy); | |
1673 | break; | |
1674 | case LTTNG_DOMAIN_UST: | |
1675 | if (!session->ust_session) { | |
1676 | ret_code = LTTNG_ERR_INVALID; | |
1677 | goto end; | |
1678 | } | |
1679 | ret_code = trace_ust_process_attr_tracker_set_tracking_policy( | |
1680 | session->ust_session, process_attr, policy); | |
1681 | break; | |
1682 | default: | |
1683 | ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN; | |
1684 | break; | |
1685 | } | |
1686 | end: | |
1687 | return ret_code; | |
1688 | } | |
1689 | ||
1690 | enum lttng_error_code cmd_process_attr_tracker_inclusion_set_add_value( | |
1691 | struct ltt_session *session, | |
1692 | enum lttng_domain_type domain, | |
1693 | enum lttng_process_attr process_attr, | |
1694 | const struct process_attr_value *value) | |
1695 | { | |
1696 | enum lttng_error_code ret_code = LTTNG_OK; | |
1697 | ||
1698 | switch (domain) { | |
1699 | case LTTNG_DOMAIN_KERNEL: | |
1700 | if (!session->kernel_session) { | |
1701 | ret_code = LTTNG_ERR_INVALID; | |
1702 | goto end; | |
1703 | } | |
1704 | ret_code = kernel_process_attr_tracker_inclusion_set_add_value( | |
1705 | session->kernel_session, process_attr, value); | |
1706 | break; | |
1707 | case LTTNG_DOMAIN_UST: | |
1708 | if (!session->ust_session) { | |
1709 | ret_code = LTTNG_ERR_INVALID; | |
1710 | goto end; | |
1711 | } | |
1712 | ret_code = trace_ust_process_attr_tracker_inclusion_set_add_value( | |
1713 | session->ust_session, process_attr, value); | |
1714 | break; | |
1715 | default: | |
1716 | ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN; | |
1717 | break; | |
1718 | } | |
1719 | end: | |
1720 | return ret_code; | |
1721 | } | |
1722 | ||
1723 | enum lttng_error_code cmd_process_attr_tracker_inclusion_set_remove_value( | |
1724 | struct ltt_session *session, | |
1725 | enum lttng_domain_type domain, | |
1726 | enum lttng_process_attr process_attr, | |
1727 | const struct process_attr_value *value) | |
1728 | { | |
1729 | enum lttng_error_code ret_code = LTTNG_OK; | |
1730 | ||
1731 | switch (domain) { | |
1732 | case LTTNG_DOMAIN_KERNEL: | |
1733 | if (!session->kernel_session) { | |
1734 | ret_code = LTTNG_ERR_INVALID; | |
1735 | goto end; | |
1736 | } | |
1737 | ret_code = kernel_process_attr_tracker_inclusion_set_remove_value( | |
1738 | session->kernel_session, process_attr, value); | |
1739 | break; | |
1740 | case LTTNG_DOMAIN_UST: | |
1741 | if (!session->ust_session) { | |
1742 | ret_code = LTTNG_ERR_INVALID; | |
1743 | goto end; | |
1744 | } | |
1745 | ret_code = trace_ust_process_attr_tracker_inclusion_set_remove_value( | |
1746 | session->ust_session, process_attr, value); | |
1747 | break; | |
1748 | default: | |
1749 | ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN; | |
1750 | break; | |
1751 | } | |
1752 | end: | |
1753 | return ret_code; | |
1754 | } | |
1755 | ||
1756 | enum lttng_error_code cmd_process_attr_tracker_get_inclusion_set( | |
1757 | struct ltt_session *session, | |
1758 | enum lttng_domain_type domain, | |
1759 | enum lttng_process_attr process_attr, | |
1760 | struct lttng_process_attr_values **values) | |
1761 | { | |
1762 | enum lttng_error_code ret_code = LTTNG_OK; | |
1763 | const struct process_attr_tracker *tracker; | |
1764 | enum process_attr_tracker_status status; | |
1765 | ||
1766 | switch (domain) { | |
1767 | case LTTNG_DOMAIN_KERNEL: | |
1768 | if (!session->kernel_session) { | |
1769 | ret_code = LTTNG_ERR_INVALID; | |
1770 | goto end; | |
1771 | } | |
1772 | tracker = kernel_get_process_attr_tracker( | |
1773 | session->kernel_session, process_attr); | |
1774 | break; | |
1775 | case LTTNG_DOMAIN_UST: | |
1776 | if (!session->ust_session) { | |
1777 | ret_code = LTTNG_ERR_INVALID; | |
1778 | goto end; | |
1779 | } | |
1780 | tracker = trace_ust_get_process_attr_tracker( | |
1781 | session->ust_session, process_attr); | |
1782 | break; | |
1783 | default: | |
1784 | ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN; | |
1785 | goto end; | |
1786 | } | |
1787 | ||
1788 | if (!tracker) { | |
1789 | ret_code = LTTNG_ERR_INVALID; | |
1790 | goto end; | |
1791 | } | |
1792 | ||
1793 | status = process_attr_tracker_get_inclusion_set(tracker, values); | |
1794 | switch (status) { | |
1795 | case PROCESS_ATTR_TRACKER_STATUS_OK: | |
1796 | ret_code = LTTNG_OK; | |
1797 | break; | |
1798 | case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY: | |
1799 | ret_code = LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY; | |
1800 | break; | |
1801 | case PROCESS_ATTR_TRACKER_STATUS_ERROR: | |
1802 | ret_code = LTTNG_ERR_NOMEM; | |
1803 | break; | |
1804 | default: | |
1805 | ret_code = LTTNG_ERR_UNK; | |
1806 | break; | |
1807 | } | |
1808 | ||
1809 | end: | |
1810 | return ret_code; | |
1811 | } | |
1812 | ||
2f77fc4b DG |
1813 | /* |
1814 | * Command LTTNG_DISABLE_EVENT processed by the client thread. | |
1815 | */ | |
56a37563 | 1816 | int cmd_disable_event(struct ltt_session *session, |
df4f5a87 JG |
1817 | enum lttng_domain_type domain, const char *channel_name, |
1818 | const struct lttng_event *event) | |
2f77fc4b DG |
1819 | { |
1820 | int ret; | |
df4f5a87 | 1821 | const char *event_name; |
6e911cad | 1822 | |
18a720cd MD |
1823 | DBG("Disable event command for event \'%s\'", event->name); |
1824 | ||
6e911cad MD |
1825 | event_name = event->name; |
1826 | ||
9b7431cf JG |
1827 | /* Error out on unhandled search criteria */ |
1828 | if (event->loglevel_type || event->loglevel != -1 || event->enabled | |
6e911cad | 1829 | || event->pid || event->filter || event->exclusion) { |
7076b56e JG |
1830 | ret = LTTNG_ERR_UNK; |
1831 | goto error; | |
6e911cad | 1832 | } |
2f77fc4b | 1833 | |
2223c96f DG |
1834 | rcu_read_lock(); |
1835 | ||
2f77fc4b DG |
1836 | switch (domain) { |
1837 | case LTTNG_DOMAIN_KERNEL: | |
1838 | { | |
1839 | struct ltt_kernel_channel *kchan; | |
1840 | struct ltt_kernel_session *ksess; | |
1841 | ||
1842 | ksess = session->kernel_session; | |
1843 | ||
85076754 MD |
1844 | /* |
1845 | * If a non-default channel has been created in the | |
1846 | * session, explicitely require that -c chan_name needs | |
1847 | * to be provided. | |
1848 | */ | |
1849 | if (ksess->has_non_default_channel && channel_name[0] == '\0') { | |
1850 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
7076b56e | 1851 | goto error_unlock; |
85076754 MD |
1852 | } |
1853 | ||
2f77fc4b DG |
1854 | kchan = trace_kernel_get_channel_by_name(channel_name, ksess); |
1855 | if (kchan == NULL) { | |
f73fabfd | 1856 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
7076b56e | 1857 | goto error_unlock; |
2f77fc4b DG |
1858 | } |
1859 | ||
6e911cad MD |
1860 | switch (event->type) { |
1861 | case LTTNG_EVENT_ALL: | |
9550ee81 | 1862 | case LTTNG_EVENT_TRACEPOINT: |
d0ae4ea8 | 1863 | case LTTNG_EVENT_SYSCALL: |
9550ee81 JR |
1864 | case LTTNG_EVENT_PROBE: |
1865 | case LTTNG_EVENT_FUNCTION: | |
1866 | case LTTNG_EVENT_FUNCTION_ENTRY:/* fall-through */ | |
1867 | if (event_name[0] == '\0') { | |
1868 | ret = event_kernel_disable_event(kchan, | |
1869 | NULL, event->type); | |
29c62722 | 1870 | } else { |
d0ae4ea8 | 1871 | ret = event_kernel_disable_event(kchan, |
9550ee81 | 1872 | event_name, event->type); |
29c62722 | 1873 | } |
6e911cad | 1874 | if (ret != LTTNG_OK) { |
7076b56e | 1875 | goto error_unlock; |
6e911cad MD |
1876 | } |
1877 | break; | |
6e911cad MD |
1878 | default: |
1879 | ret = LTTNG_ERR_UNK; | |
7076b56e | 1880 | goto error_unlock; |
2f77fc4b DG |
1881 | } |
1882 | ||
7d268848 | 1883 | kernel_wait_quiescent(); |
2f77fc4b DG |
1884 | break; |
1885 | } | |
1886 | case LTTNG_DOMAIN_UST: | |
1887 | { | |
1888 | struct ltt_ust_channel *uchan; | |
1889 | struct ltt_ust_session *usess; | |
1890 | ||
1891 | usess = session->ust_session; | |
1892 | ||
7076b56e JG |
1893 | if (validate_ust_event_name(event_name)) { |
1894 | ret = LTTNG_ERR_INVALID_EVENT_NAME; | |
1895 | goto error_unlock; | |
1896 | } | |
1897 | ||
85076754 MD |
1898 | /* |
1899 | * If a non-default channel has been created in the | |
9550ee81 | 1900 | * session, explicitly require that -c chan_name needs |
85076754 MD |
1901 | * to be provided. |
1902 | */ | |
1903 | if (usess->has_non_default_channel && channel_name[0] == '\0') { | |
1904 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
7076b56e | 1905 | goto error_unlock; |
85076754 MD |
1906 | } |
1907 | ||
2f77fc4b DG |
1908 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, |
1909 | channel_name); | |
1910 | if (uchan == NULL) { | |
f73fabfd | 1911 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
7076b56e | 1912 | goto error_unlock; |
2f77fc4b DG |
1913 | } |
1914 | ||
6e911cad MD |
1915 | switch (event->type) { |
1916 | case LTTNG_EVENT_ALL: | |
b3639870 JR |
1917 | /* |
1918 | * An empty event name means that everything | |
1919 | * should be disabled. | |
1920 | */ | |
1921 | if (event->name[0] == '\0') { | |
1922 | ret = event_ust_disable_all_tracepoints(usess, uchan); | |
77d536b2 JR |
1923 | } else { |
1924 | ret = event_ust_disable_tracepoint(usess, uchan, | |
1925 | event_name); | |
1926 | } | |
6e911cad | 1927 | if (ret != LTTNG_OK) { |
7076b56e | 1928 | goto error_unlock; |
6e911cad MD |
1929 | } |
1930 | break; | |
1931 | default: | |
1932 | ret = LTTNG_ERR_UNK; | |
7076b56e | 1933 | goto error_unlock; |
2f77fc4b DG |
1934 | } |
1935 | ||
1936 | DBG3("Disable UST event %s in channel %s completed", event_name, | |
1937 | channel_name); | |
1938 | break; | |
1939 | } | |
5cdb6027 | 1940 | case LTTNG_DOMAIN_LOG4J: |
f20baf8e | 1941 | case LTTNG_DOMAIN_JUL: |
0e115563 | 1942 | case LTTNG_DOMAIN_PYTHON: |
f20baf8e | 1943 | { |
fefd409b | 1944 | struct agent *agt; |
f20baf8e DG |
1945 | struct ltt_ust_session *usess = session->ust_session; |
1946 | ||
1947 | assert(usess); | |
1948 | ||
6e911cad MD |
1949 | switch (event->type) { |
1950 | case LTTNG_EVENT_ALL: | |
1951 | break; | |
1952 | default: | |
1953 | ret = LTTNG_ERR_UNK; | |
7076b56e | 1954 | goto error_unlock; |
6e911cad MD |
1955 | } |
1956 | ||
5cdb6027 | 1957 | agt = trace_ust_find_agent(usess, domain); |
fefd409b DG |
1958 | if (!agt) { |
1959 | ret = -LTTNG_ERR_UST_EVENT_NOT_FOUND; | |
7076b56e | 1960 | goto error_unlock; |
fefd409b | 1961 | } |
b3639870 JR |
1962 | /* |
1963 | * An empty event name means that everything | |
1964 | * should be disabled. | |
1965 | */ | |
1966 | if (event->name[0] == '\0') { | |
18a720cd MD |
1967 | ret = event_agent_disable_all(usess, agt); |
1968 | } else { | |
1969 | ret = event_agent_disable(usess, agt, event_name); | |
1970 | } | |
f20baf8e | 1971 | if (ret != LTTNG_OK) { |
7076b56e | 1972 | goto error_unlock; |
f20baf8e DG |
1973 | } |
1974 | ||
1975 | break; | |
1976 | } | |
2f77fc4b | 1977 | default: |
f73fabfd | 1978 | ret = LTTNG_ERR_UND; |
7076b56e | 1979 | goto error_unlock; |
2f77fc4b DG |
1980 | } |
1981 | ||
f73fabfd | 1982 | ret = LTTNG_OK; |
2f77fc4b | 1983 | |
7076b56e | 1984 | error_unlock: |
2223c96f | 1985 | rcu_read_unlock(); |
7076b56e | 1986 | error: |
2f77fc4b DG |
1987 | return ret; |
1988 | } | |
1989 | ||
2f77fc4b DG |
1990 | /* |
1991 | * Command LTTNG_ADD_CONTEXT processed by the client thread. | |
1992 | */ | |
56a37563 | 1993 | int cmd_add_context(struct ltt_session *session, enum lttng_domain_type domain, |
df4f5a87 | 1994 | char *channel_name, const struct lttng_event_context *ctx, int kwpipe) |
2f77fc4b | 1995 | { |
d5979e4a | 1996 | int ret, chan_kern_created = 0, chan_ust_created = 0; |
bdf64013 JG |
1997 | char *app_ctx_provider_name = NULL, *app_ctx_name = NULL; |
1998 | ||
9a699f7b JR |
1999 | /* |
2000 | * Don't try to add a context if the session has been started at | |
2001 | * some point in time before. The tracer does not allow it and would | |
2002 | * result in a corrupted trace. | |
2003 | */ | |
2004 | if (session->has_been_started) { | |
2005 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; | |
2006 | goto end; | |
2007 | } | |
2008 | ||
bdf64013 JG |
2009 | if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) { |
2010 | app_ctx_provider_name = ctx->u.app_ctx.provider_name; | |
2011 | app_ctx_name = ctx->u.app_ctx.ctx_name; | |
2012 | } | |
2f77fc4b DG |
2013 | |
2014 | switch (domain) { | |
2015 | case LTTNG_DOMAIN_KERNEL: | |
979e618e DG |
2016 | assert(session->kernel_session); |
2017 | ||
2018 | if (session->kernel_session->channel_count == 0) { | |
2019 | /* Create default channel */ | |
2020 | ret = channel_kernel_create(session->kernel_session, NULL, kwpipe); | |
2021 | if (ret != LTTNG_OK) { | |
2022 | goto error; | |
2023 | } | |
d5979e4a | 2024 | chan_kern_created = 1; |
979e618e | 2025 | } |
2f77fc4b | 2026 | /* Add kernel context to kernel tracer */ |
601d5acf | 2027 | ret = context_kernel_add(session->kernel_session, ctx, channel_name); |
f73fabfd | 2028 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
2029 | goto error; |
2030 | } | |
2031 | break; | |
bdf64013 JG |
2032 | case LTTNG_DOMAIN_JUL: |
2033 | case LTTNG_DOMAIN_LOG4J: | |
2034 | { | |
2035 | /* | |
2036 | * Validate channel name. | |
2037 | * If no channel name is given and the domain is JUL or LOG4J, | |
2038 | * set it to the appropriate domain-specific channel name. If | |
2039 | * a name is provided but does not match the expexted channel | |
2040 | * name, return an error. | |
2041 | */ | |
2042 | if (domain == LTTNG_DOMAIN_JUL && *channel_name && | |
2043 | strcmp(channel_name, | |
2044 | DEFAULT_JUL_CHANNEL_NAME)) { | |
2045 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; | |
2046 | goto error; | |
2047 | } else if (domain == LTTNG_DOMAIN_LOG4J && *channel_name && | |
2048 | strcmp(channel_name, | |
2049 | DEFAULT_LOG4J_CHANNEL_NAME)) { | |
2050 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; | |
2051 | goto error; | |
2052 | } | |
a93b3916 | 2053 | /* break is _not_ missing here. */ |
bdf64013 | 2054 | } |
2f77fc4b DG |
2055 | case LTTNG_DOMAIN_UST: |
2056 | { | |
2057 | struct ltt_ust_session *usess = session->ust_session; | |
85076754 MD |
2058 | unsigned int chan_count; |
2059 | ||
2f77fc4b DG |
2060 | assert(usess); |
2061 | ||
85076754 | 2062 | chan_count = lttng_ht_get_count(usess->domain_global.channels); |
979e618e DG |
2063 | if (chan_count == 0) { |
2064 | struct lttng_channel *attr; | |
2065 | /* Create default channel */ | |
0a9c6494 | 2066 | attr = channel_new_default_attr(domain, usess->buffer_type); |
979e618e DG |
2067 | if (attr == NULL) { |
2068 | ret = LTTNG_ERR_FATAL; | |
2069 | goto error; | |
2070 | } | |
2071 | ||
7972aab2 | 2072 | ret = channel_ust_create(usess, attr, usess->buffer_type); |
979e618e DG |
2073 | if (ret != LTTNG_OK) { |
2074 | free(attr); | |
2075 | goto error; | |
2076 | } | |
cf0bcb51 | 2077 | channel_attr_destroy(attr); |
d5979e4a | 2078 | chan_ust_created = 1; |
979e618e DG |
2079 | } |
2080 | ||
601d5acf | 2081 | ret = context_ust_add(usess, domain, ctx, channel_name); |
bdf64013 JG |
2082 | free(app_ctx_provider_name); |
2083 | free(app_ctx_name); | |
2084 | app_ctx_name = NULL; | |
2085 | app_ctx_provider_name = NULL; | |
f73fabfd | 2086 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
2087 | goto error; |
2088 | } | |
2089 | break; | |
2090 | } | |
2f77fc4b | 2091 | default: |
f73fabfd | 2092 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
2093 | goto error; |
2094 | } | |
2095 | ||
bdf64013 JG |
2096 | ret = LTTNG_OK; |
2097 | goto end; | |
2f77fc4b DG |
2098 | |
2099 | error: | |
d5979e4a DG |
2100 | if (chan_kern_created) { |
2101 | struct ltt_kernel_channel *kchan = | |
2102 | trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME, | |
2103 | session->kernel_session); | |
2104 | /* Created previously, this should NOT fail. */ | |
2105 | assert(kchan); | |
2106 | kernel_destroy_channel(kchan); | |
2107 | } | |
2108 | ||
2109 | if (chan_ust_created) { | |
2110 | struct ltt_ust_channel *uchan = | |
2111 | trace_ust_find_channel_by_name( | |
2112 | session->ust_session->domain_global.channels, | |
2113 | DEFAULT_CHANNEL_NAME); | |
2114 | /* Created previously, this should NOT fail. */ | |
2115 | assert(uchan); | |
2116 | /* Remove from the channel list of the session. */ | |
2117 | trace_ust_delete_channel(session->ust_session->domain_global.channels, | |
2118 | uchan); | |
2119 | trace_ust_destroy_channel(uchan); | |
2120 | } | |
bdf64013 JG |
2121 | end: |
2122 | free(app_ctx_provider_name); | |
2123 | free(app_ctx_name); | |
2f77fc4b DG |
2124 | return ret; |
2125 | } | |
2126 | ||
dac8e046 JG |
2127 | static inline bool name_starts_with(const char *name, const char *prefix) |
2128 | { | |
2129 | const size_t max_cmp_len = min(strlen(prefix), LTTNG_SYMBOL_NAME_LEN); | |
2130 | ||
2131 | return !strncmp(name, prefix, max_cmp_len); | |
2132 | } | |
2133 | ||
2134 | /* Perform userspace-specific event name validation */ | |
2135 | static int validate_ust_event_name(const char *name) | |
2136 | { | |
2137 | int ret = 0; | |
2138 | ||
2139 | if (!name) { | |
2140 | ret = -1; | |
2141 | goto end; | |
2142 | } | |
2143 | ||
2144 | /* | |
2145 | * Check name against all internal UST event component namespaces used | |
2146 | * by the agents. | |
2147 | */ | |
2148 | if (name_starts_with(name, DEFAULT_JUL_EVENT_COMPONENT) || | |
2149 | name_starts_with(name, DEFAULT_LOG4J_EVENT_COMPONENT) || | |
2150 | name_starts_with(name, DEFAULT_PYTHON_EVENT_COMPONENT)) { | |
2151 | ret = -1; | |
2152 | } | |
2153 | ||
2154 | end: | |
2155 | return ret; | |
2156 | } | |
88f06f15 | 2157 | |
2f77fc4b | 2158 | /* |
88f06f15 JG |
2159 | * Internal version of cmd_enable_event() with a supplemental |
2160 | * "internal_event" flag which is used to enable internal events which should | |
2161 | * be hidden from clients. Such events are used in the agent implementation to | |
2162 | * enable the events through which all "agent" events are funeled. | |
2f77fc4b | 2163 | */ |
88f06f15 | 2164 | static int _cmd_enable_event(struct ltt_session *session, |
df4f5a87 | 2165 | const struct lttng_domain *domain, |
025faf73 | 2166 | char *channel_name, struct lttng_event *event, |
6b453b5e | 2167 | char *filter_expression, |
db8f1377 JI |
2168 | struct lttng_filter_bytecode *filter, |
2169 | struct lttng_event_exclusion *exclusion, | |
88f06f15 | 2170 | int wpipe, bool internal_event) |
2f77fc4b | 2171 | { |
9f449915 | 2172 | int ret = 0, channel_created = 0; |
cfedea03 | 2173 | struct lttng_channel *attr = NULL; |
2f77fc4b DG |
2174 | |
2175 | assert(session); | |
2176 | assert(event); | |
2177 | assert(channel_name); | |
2178 | ||
2a385866 JG |
2179 | /* If we have a filter, we must have its filter expression */ |
2180 | assert(!(!!filter_expression ^ !!filter)); | |
2181 | ||
9f449915 PP |
2182 | /* Normalize event name as a globbing pattern */ |
2183 | strutils_normalize_star_glob_pattern(event->name); | |
18a720cd | 2184 | |
9f449915 PP |
2185 | /* Normalize exclusion names as globbing patterns */ |
2186 | if (exclusion) { | |
2187 | size_t i; | |
f5ac4bd7 | 2188 | |
9f449915 PP |
2189 | for (i = 0; i < exclusion->count; i++) { |
2190 | char *name = LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, i); | |
2191 | ||
2192 | strutils_normalize_star_glob_pattern(name); | |
2193 | } | |
930a2e99 JG |
2194 | } |
2195 | ||
9f449915 PP |
2196 | DBG("Enable event command for event \'%s\'", event->name); |
2197 | ||
2198 | rcu_read_lock(); | |
2199 | ||
7972aab2 | 2200 | switch (domain->type) { |
2f77fc4b DG |
2201 | case LTTNG_DOMAIN_KERNEL: |
2202 | { | |
2203 | struct ltt_kernel_channel *kchan; | |
2204 | ||
85076754 MD |
2205 | /* |
2206 | * If a non-default channel has been created in the | |
2207 | * session, explicitely require that -c chan_name needs | |
2208 | * to be provided. | |
2209 | */ | |
2210 | if (session->kernel_session->has_non_default_channel | |
2211 | && channel_name[0] == '\0') { | |
2212 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
2213 | goto error; | |
2214 | } | |
2215 | ||
2f77fc4b DG |
2216 | kchan = trace_kernel_get_channel_by_name(channel_name, |
2217 | session->kernel_session); | |
2218 | if (kchan == NULL) { | |
0a9c6494 DG |
2219 | attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL, |
2220 | LTTNG_BUFFER_GLOBAL); | |
2f77fc4b | 2221 | if (attr == NULL) { |
f73fabfd | 2222 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
2223 | goto error; |
2224 | } | |
04c17253 MD |
2225 | if (lttng_strncpy(attr->name, channel_name, |
2226 | sizeof(attr->name))) { | |
2227 | ret = LTTNG_ERR_INVALID; | |
04c17253 MD |
2228 | goto error; |
2229 | } | |
2f77fc4b DG |
2230 | |
2231 | ret = cmd_enable_channel(session, domain, attr, wpipe); | |
f73fabfd | 2232 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
2233 | goto error; |
2234 | } | |
e5f5db7f | 2235 | channel_created = 1; |
2f77fc4b DG |
2236 | } |
2237 | ||
2238 | /* Get the newly created kernel channel pointer */ | |
2239 | kchan = trace_kernel_get_channel_by_name(channel_name, | |
2240 | session->kernel_session); | |
2241 | if (kchan == NULL) { | |
2242 | /* This sould not happen... */ | |
f73fabfd | 2243 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
2244 | goto error; |
2245 | } | |
2246 | ||
6e911cad MD |
2247 | switch (event->type) { |
2248 | case LTTNG_EVENT_ALL: | |
29c62722 | 2249 | { |
00a62084 MD |
2250 | char *filter_expression_a = NULL; |
2251 | struct lttng_filter_bytecode *filter_a = NULL; | |
2252 | ||
2253 | /* | |
2254 | * We need to duplicate filter_expression and filter, | |
2255 | * because ownership is passed to first enable | |
2256 | * event. | |
2257 | */ | |
2258 | if (filter_expression) { | |
2259 | filter_expression_a = strdup(filter_expression); | |
2260 | if (!filter_expression_a) { | |
2261 | ret = LTTNG_ERR_FATAL; | |
2262 | goto error; | |
2263 | } | |
2264 | } | |
2265 | if (filter) { | |
2266 | filter_a = zmalloc(sizeof(*filter_a) + filter->len); | |
2267 | if (!filter_a) { | |
2268 | free(filter_expression_a); | |
2269 | ret = LTTNG_ERR_FATAL; | |
2270 | goto error; | |
2271 | } | |
2272 | memcpy(filter_a, filter, sizeof(*filter_a) + filter->len); | |
2273 | } | |
29c62722 | 2274 | event->type = LTTNG_EVENT_TRACEPOINT; /* Hack */ |
00a62084 MD |
2275 | ret = event_kernel_enable_event(kchan, event, |
2276 | filter_expression, filter); | |
a969e101 MD |
2277 | /* We have passed ownership */ |
2278 | filter_expression = NULL; | |
2279 | filter = NULL; | |
29c62722 MD |
2280 | if (ret != LTTNG_OK) { |
2281 | if (channel_created) { | |
2282 | /* Let's not leak a useless channel. */ | |
2283 | kernel_destroy_channel(kchan); | |
2284 | } | |
00a62084 MD |
2285 | free(filter_expression_a); |
2286 | free(filter_a); | |
29c62722 MD |
2287 | goto error; |
2288 | } | |
2289 | event->type = LTTNG_EVENT_SYSCALL; /* Hack */ | |
00a62084 MD |
2290 | ret = event_kernel_enable_event(kchan, event, |
2291 | filter_expression_a, filter_a); | |
60d21fa2 AB |
2292 | /* We have passed ownership */ |
2293 | filter_expression_a = NULL; | |
2294 | filter_a = NULL; | |
29c62722 MD |
2295 | if (ret != LTTNG_OK) { |
2296 | goto error; | |
2297 | } | |
2298 | break; | |
2299 | } | |
6e6ef3d7 | 2300 | case LTTNG_EVENT_PROBE: |
dcabc190 | 2301 | case LTTNG_EVENT_USERSPACE_PROBE: |
6e6ef3d7 DG |
2302 | case LTTNG_EVENT_FUNCTION: |
2303 | case LTTNG_EVENT_FUNCTION_ENTRY: | |
6e911cad | 2304 | case LTTNG_EVENT_TRACEPOINT: |
00a62084 MD |
2305 | ret = event_kernel_enable_event(kchan, event, |
2306 | filter_expression, filter); | |
a969e101 MD |
2307 | /* We have passed ownership */ |
2308 | filter_expression = NULL; | |
2309 | filter = NULL; | |
6e911cad MD |
2310 | if (ret != LTTNG_OK) { |
2311 | if (channel_created) { | |
2312 | /* Let's not leak a useless channel. */ | |
2313 | kernel_destroy_channel(kchan); | |
2314 | } | |
2315 | goto error; | |
e5f5db7f | 2316 | } |
6e911cad MD |
2317 | break; |
2318 | case LTTNG_EVENT_SYSCALL: | |
00a62084 MD |
2319 | ret = event_kernel_enable_event(kchan, event, |
2320 | filter_expression, filter); | |
a969e101 MD |
2321 | /* We have passed ownership */ |
2322 | filter_expression = NULL; | |
2323 | filter = NULL; | |
e2b957af MD |
2324 | if (ret != LTTNG_OK) { |
2325 | goto error; | |
2326 | } | |
6e911cad MD |
2327 | break; |
2328 | default: | |
2329 | ret = LTTNG_ERR_UNK; | |
2f77fc4b DG |
2330 | goto error; |
2331 | } | |
2332 | ||
7d268848 | 2333 | kernel_wait_quiescent(); |
2f77fc4b DG |
2334 | break; |
2335 | } | |
2336 | case LTTNG_DOMAIN_UST: | |
2337 | { | |
2338 | struct ltt_ust_channel *uchan; | |
2339 | struct ltt_ust_session *usess = session->ust_session; | |
2340 | ||
2341 | assert(usess); | |
2342 | ||
85076754 MD |
2343 | /* |
2344 | * If a non-default channel has been created in the | |
2345 | * session, explicitely require that -c chan_name needs | |
2346 | * to be provided. | |
2347 | */ | |
2348 | if (usess->has_non_default_channel && channel_name[0] == '\0') { | |
2349 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
2350 | goto error; | |
2351 | } | |
2352 | ||
2f77fc4b DG |
2353 | /* Get channel from global UST domain */ |
2354 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, | |
2355 | channel_name); | |
2356 | if (uchan == NULL) { | |
2357 | /* Create default channel */ | |
0a9c6494 DG |
2358 | attr = channel_new_default_attr(LTTNG_DOMAIN_UST, |
2359 | usess->buffer_type); | |
2f77fc4b | 2360 | if (attr == NULL) { |
f73fabfd | 2361 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
2362 | goto error; |
2363 | } | |
04c17253 MD |
2364 | if (lttng_strncpy(attr->name, channel_name, |
2365 | sizeof(attr->name))) { | |
2366 | ret = LTTNG_ERR_INVALID; | |
04c17253 MD |
2367 | goto error; |
2368 | } | |
2f77fc4b DG |
2369 | |
2370 | ret = cmd_enable_channel(session, domain, attr, wpipe); | |
f73fabfd | 2371 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
2372 | goto error; |
2373 | } | |
2f77fc4b DG |
2374 | |
2375 | /* Get the newly created channel reference back */ | |
2376 | uchan = trace_ust_find_channel_by_name( | |
2377 | usess->domain_global.channels, channel_name); | |
2378 | assert(uchan); | |
2379 | } | |
2380 | ||
141feb8c JG |
2381 | if (uchan->domain != LTTNG_DOMAIN_UST && !internal_event) { |
2382 | /* | |
2383 | * Don't allow users to add UST events to channels which | |
2384 | * are assigned to a userspace subdomain (JUL, Log4J, | |
2385 | * Python, etc.). | |
2386 | */ | |
2387 | ret = LTTNG_ERR_INVALID_CHANNEL_DOMAIN; | |
2388 | goto error; | |
2389 | } | |
2390 | ||
dac8e046 JG |
2391 | if (!internal_event) { |
2392 | /* | |
2393 | * Ensure the event name is not reserved for internal | |
2394 | * use. | |
2395 | */ | |
2396 | ret = validate_ust_event_name(event->name); | |
2397 | if (ret) { | |
2398 | WARN("Userspace event name %s failed validation.", | |
bbcab087 | 2399 | event->name); |
dac8e046 JG |
2400 | ret = LTTNG_ERR_INVALID_EVENT_NAME; |
2401 | goto error; | |
2402 | } | |
2403 | } | |
2404 | ||
2f77fc4b | 2405 | /* At this point, the session and channel exist on the tracer */ |
6b453b5e | 2406 | ret = event_ust_enable_tracepoint(usess, uchan, event, |
88f06f15 JG |
2407 | filter_expression, filter, exclusion, |
2408 | internal_event); | |
49d21f93 MD |
2409 | /* We have passed ownership */ |
2410 | filter_expression = NULL; | |
2411 | filter = NULL; | |
2412 | exclusion = NULL; | |
94382e15 JG |
2413 | if (ret == LTTNG_ERR_UST_EVENT_ENABLED) { |
2414 | goto already_enabled; | |
2415 | } else if (ret != LTTNG_OK) { | |
2f77fc4b DG |
2416 | goto error; |
2417 | } | |
2418 | break; | |
2419 | } | |
5cdb6027 | 2420 | case LTTNG_DOMAIN_LOG4J: |
f20baf8e | 2421 | case LTTNG_DOMAIN_JUL: |
0e115563 | 2422 | case LTTNG_DOMAIN_PYTHON: |
f20baf8e | 2423 | { |
da6c3a50 | 2424 | const char *default_event_name, *default_chan_name; |
fefd409b | 2425 | struct agent *agt; |
f20baf8e DG |
2426 | struct lttng_event uevent; |
2427 | struct lttng_domain tmp_dom; | |
2428 | struct ltt_ust_session *usess = session->ust_session; | |
2429 | ||
2430 | assert(usess); | |
2431 | ||
f28f9e44 JG |
2432 | if (!agent_tracing_is_enabled()) { |
2433 | DBG("Attempted to enable an event in an agent domain but the agent thread is not running"); | |
2434 | ret = LTTNG_ERR_AGENT_TRACING_DISABLED; | |
2435 | goto error; | |
2436 | } | |
2437 | ||
5cdb6027 | 2438 | agt = trace_ust_find_agent(usess, domain->type); |
fefd409b | 2439 | if (!agt) { |
5cdb6027 | 2440 | agt = agent_create(domain->type); |
fefd409b | 2441 | if (!agt) { |
e5b3c48c | 2442 | ret = LTTNG_ERR_NOMEM; |
fefd409b DG |
2443 | goto error; |
2444 | } | |
2445 | agent_add(agt, usess->agents); | |
2446 | } | |
2447 | ||
022d91ba | 2448 | /* Create the default tracepoint. */ |
996de3c7 | 2449 | memset(&uevent, 0, sizeof(uevent)); |
f20baf8e DG |
2450 | uevent.type = LTTNG_EVENT_TRACEPOINT; |
2451 | uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; | |
51755dc8 JG |
2452 | default_event_name = event_get_default_agent_ust_name( |
2453 | domain->type); | |
da6c3a50 | 2454 | if (!default_event_name) { |
e5b3c48c | 2455 | ret = LTTNG_ERR_FATAL; |
da6c3a50 | 2456 | goto error; |
f43f95a9 | 2457 | } |
da6c3a50 | 2458 | strncpy(uevent.name, default_event_name, sizeof(uevent.name)); |
f20baf8e DG |
2459 | uevent.name[sizeof(uevent.name) - 1] = '\0'; |
2460 | ||
2461 | /* | |
2462 | * The domain type is changed because we are about to enable the | |
2463 | * default channel and event for the JUL domain that are hardcoded. | |
2464 | * This happens in the UST domain. | |
2465 | */ | |
2466 | memcpy(&tmp_dom, domain, sizeof(tmp_dom)); | |
2467 | tmp_dom.type = LTTNG_DOMAIN_UST; | |
2468 | ||
0e115563 DG |
2469 | switch (domain->type) { |
2470 | case LTTNG_DOMAIN_LOG4J: | |
da6c3a50 | 2471 | default_chan_name = DEFAULT_LOG4J_CHANNEL_NAME; |
0e115563 DG |
2472 | break; |
2473 | case LTTNG_DOMAIN_JUL: | |
da6c3a50 | 2474 | default_chan_name = DEFAULT_JUL_CHANNEL_NAME; |
0e115563 DG |
2475 | break; |
2476 | case LTTNG_DOMAIN_PYTHON: | |
2477 | default_chan_name = DEFAULT_PYTHON_CHANNEL_NAME; | |
2478 | break; | |
2479 | default: | |
e98a44b0 | 2480 | /* The switch/case we are in makes this impossible */ |
0e115563 | 2481 | assert(0); |
da6c3a50 DG |
2482 | } |
2483 | ||
971da06a | 2484 | { |
8404118c | 2485 | char *filter_expression_copy = NULL; |
51755dc8 | 2486 | struct lttng_filter_bytecode *filter_copy = NULL; |
971da06a JG |
2487 | |
2488 | if (filter) { | |
51755dc8 JG |
2489 | const size_t filter_size = sizeof( |
2490 | struct lttng_filter_bytecode) | |
2491 | + filter->len; | |
2492 | ||
2493 | filter_copy = zmalloc(filter_size); | |
971da06a | 2494 | if (!filter_copy) { |
018096a4 | 2495 | ret = LTTNG_ERR_NOMEM; |
b742e3e2 | 2496 | goto error; |
971da06a | 2497 | } |
51755dc8 | 2498 | memcpy(filter_copy, filter, filter_size); |
971da06a | 2499 | |
8404118c JG |
2500 | filter_expression_copy = |
2501 | strdup(filter_expression); | |
2502 | if (!filter_expression) { | |
2503 | ret = LTTNG_ERR_NOMEM; | |
51755dc8 JG |
2504 | } |
2505 | ||
2506 | if (!filter_expression_copy || !filter_copy) { | |
2507 | free(filter_expression_copy); | |
2508 | free(filter_copy); | |
2509 | goto error; | |
8404118c | 2510 | } |
971da06a JG |
2511 | } |
2512 | ||
88f06f15 | 2513 | ret = cmd_enable_event_internal(session, &tmp_dom, |
971da06a | 2514 | (char *) default_chan_name, |
8404118c JG |
2515 | &uevent, filter_expression_copy, |
2516 | filter_copy, NULL, wpipe); | |
971da06a JG |
2517 | } |
2518 | ||
94382e15 JG |
2519 | if (ret == LTTNG_ERR_UST_EVENT_ENABLED) { |
2520 | goto already_enabled; | |
2521 | } else if (ret != LTTNG_OK) { | |
f20baf8e DG |
2522 | goto error; |
2523 | } | |
2524 | ||
2525 | /* The wild card * means that everything should be enabled. */ | |
2526 | if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) { | |
8404118c JG |
2527 | ret = event_agent_enable_all(usess, agt, event, filter, |
2528 | filter_expression); | |
f20baf8e | 2529 | } else { |
8404118c JG |
2530 | ret = event_agent_enable(usess, agt, event, filter, |
2531 | filter_expression); | |
f20baf8e | 2532 | } |
51755dc8 JG |
2533 | filter = NULL; |
2534 | filter_expression = NULL; | |
f20baf8e DG |
2535 | if (ret != LTTNG_OK) { |
2536 | goto error; | |
2537 | } | |
2538 | ||
2539 | break; | |
2540 | } | |
2f77fc4b | 2541 | default: |
f73fabfd | 2542 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
2543 | goto error; |
2544 | } | |
2545 | ||
f73fabfd | 2546 | ret = LTTNG_OK; |
2f77fc4b | 2547 | |
94382e15 | 2548 | already_enabled: |
2f77fc4b | 2549 | error: |
49d21f93 MD |
2550 | free(filter_expression); |
2551 | free(filter); | |
2552 | free(exclusion); | |
cf0bcb51 | 2553 | channel_attr_destroy(attr); |
2223c96f | 2554 | rcu_read_unlock(); |
2f77fc4b DG |
2555 | return ret; |
2556 | } | |
2557 | ||
88f06f15 JG |
2558 | /* |
2559 | * Command LTTNG_ENABLE_EVENT processed by the client thread. | |
2560 | * We own filter, exclusion, and filter_expression. | |
2561 | */ | |
df4f5a87 JG |
2562 | int cmd_enable_event(struct ltt_session *session, |
2563 | const struct lttng_domain *domain, | |
88f06f15 JG |
2564 | char *channel_name, struct lttng_event *event, |
2565 | char *filter_expression, | |
2566 | struct lttng_filter_bytecode *filter, | |
2567 | struct lttng_event_exclusion *exclusion, | |
2568 | int wpipe) | |
2569 | { | |
2570 | return _cmd_enable_event(session, domain, channel_name, event, | |
2571 | filter_expression, filter, exclusion, wpipe, false); | |
2572 | } | |
2573 | ||
2574 | /* | |
2575 | * Enable an event which is internal to LTTng. An internal should | |
2576 | * never be made visible to clients and are immune to checks such as | |
2577 | * reserved names. | |
2578 | */ | |
2579 | static int cmd_enable_event_internal(struct ltt_session *session, | |
df4f5a87 | 2580 | const struct lttng_domain *domain, |
88f06f15 JG |
2581 | char *channel_name, struct lttng_event *event, |
2582 | char *filter_expression, | |
2583 | struct lttng_filter_bytecode *filter, | |
2584 | struct lttng_event_exclusion *exclusion, | |
2585 | int wpipe) | |
2586 | { | |
2587 | return _cmd_enable_event(session, domain, channel_name, event, | |
2588 | filter_expression, filter, exclusion, wpipe, true); | |
2589 | } | |
2590 | ||
2f77fc4b DG |
2591 | /* |
2592 | * Command LTTNG_LIST_TRACEPOINTS processed by the client thread. | |
2593 | */ | |
56a37563 JG |
2594 | ssize_t cmd_list_tracepoints(enum lttng_domain_type domain, |
2595 | struct lttng_event **events) | |
2f77fc4b DG |
2596 | { |
2597 | int ret; | |
2598 | ssize_t nb_events = 0; | |
2599 | ||
2600 | switch (domain) { | |
2601 | case LTTNG_DOMAIN_KERNEL: | |
7d268848 | 2602 | nb_events = kernel_list_events(events); |
2f77fc4b | 2603 | if (nb_events < 0) { |
f73fabfd | 2604 | ret = LTTNG_ERR_KERN_LIST_FAIL; |
2f77fc4b DG |
2605 | goto error; |
2606 | } | |
2607 | break; | |
2608 | case LTTNG_DOMAIN_UST: | |
2609 | nb_events = ust_app_list_events(events); | |
2610 | if (nb_events < 0) { | |
f73fabfd | 2611 | ret = LTTNG_ERR_UST_LIST_FAIL; |
2f77fc4b DG |
2612 | goto error; |
2613 | } | |
2614 | break; | |
5cdb6027 | 2615 | case LTTNG_DOMAIN_LOG4J: |
3c6a091f | 2616 | case LTTNG_DOMAIN_JUL: |
0e115563 | 2617 | case LTTNG_DOMAIN_PYTHON: |
f60140a1 | 2618 | nb_events = agent_list_events(events, domain); |
3c6a091f DG |
2619 | if (nb_events < 0) { |
2620 | ret = LTTNG_ERR_UST_LIST_FAIL; | |
2621 | goto error; | |
2622 | } | |
2623 | break; | |
2f77fc4b | 2624 | default: |
f73fabfd | 2625 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
2626 | goto error; |
2627 | } | |
2628 | ||
2629 | return nb_events; | |
2630 | ||
2631 | error: | |
2632 | /* Return negative value to differentiate return code */ | |
2633 | return -ret; | |
2634 | } | |
2635 | ||
2636 | /* | |
2637 | * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread. | |
2638 | */ | |
56a37563 | 2639 | ssize_t cmd_list_tracepoint_fields(enum lttng_domain_type domain, |
2f77fc4b DG |
2640 | struct lttng_event_field **fields) |
2641 | { | |
2642 | int ret; | |
2643 | ssize_t nb_fields = 0; | |
2644 | ||
2645 | switch (domain) { | |
2646 | case LTTNG_DOMAIN_UST: | |
2647 | nb_fields = ust_app_list_event_fields(fields); | |
2648 | if (nb_fields < 0) { | |
f73fabfd | 2649 | ret = LTTNG_ERR_UST_LIST_FAIL; |
2f77fc4b DG |
2650 | goto error; |
2651 | } | |
2652 | break; | |
2653 | case LTTNG_DOMAIN_KERNEL: | |
2654 | default: /* fall-through */ | |
f73fabfd | 2655 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
2656 | goto error; |
2657 | } | |
2658 | ||
2659 | return nb_fields; | |
2660 | ||
2661 | error: | |
2662 | /* Return negative value to differentiate return code */ | |
2663 | return -ret; | |
2664 | } | |
2665 | ||
834978fd DG |
2666 | ssize_t cmd_list_syscalls(struct lttng_event **events) |
2667 | { | |
2668 | return syscall_table_list(events); | |
2669 | } | |
2670 | ||
2f77fc4b DG |
2671 | /* |
2672 | * Command LTTNG_START_TRACE processed by the client thread. | |
a9ad0c8f MD |
2673 | * |
2674 | * Called with session mutex held. | |
2f77fc4b DG |
2675 | */ |
2676 | int cmd_start_trace(struct ltt_session *session) | |
2677 | { | |
82b69413 | 2678 | enum lttng_error_code ret; |
cde3e505 | 2679 | unsigned long nb_chan = 0; |
2f77fc4b DG |
2680 | struct ltt_kernel_session *ksession; |
2681 | struct ltt_ust_session *usess; | |
1f496244 JG |
2682 | const bool session_rotated_after_last_stop = |
2683 | session->rotated_after_last_stop; | |
b02f5986 MD |
2684 | const bool session_cleared_after_last_stop = |
2685 | session->cleared_after_last_stop; | |
2f77fc4b DG |
2686 | |
2687 | assert(session); | |
2688 | ||
2689 | /* Ease our life a bit ;) */ | |
2690 | ksession = session->kernel_session; | |
2691 | usess = session->ust_session; | |
2692 | ||
8382cf6f DG |
2693 | /* Is the session already started? */ |
2694 | if (session->active) { | |
f73fabfd | 2695 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
7a24ece3 JR |
2696 | /* Perform nothing */ |
2697 | goto end; | |
2f77fc4b DG |
2698 | } |
2699 | ||
1f496244 JG |
2700 | if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING && |
2701 | !session->current_trace_chunk) { | |
2702 | /* | |
2703 | * A rotation was launched while the session was stopped and | |
2704 | * it has not been completed yet. It is not possible to start | |
2705 | * the session since starting the session here would require a | |
2706 | * rotation from "NULL" to a new trace chunk. That rotation | |
2707 | * would overlap with the ongoing rotation, which is not | |
2708 | * supported. | |
2709 | */ | |
2710 | WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing", | |
2711 | session->name); | |
2712 | ret = LTTNG_ERR_ROTATION_PENDING; | |
2713 | goto error; | |
2714 | } | |
2715 | ||
cde3e505 DG |
2716 | /* |
2717 | * Starting a session without channel is useless since after that it's not | |
2718 | * possible to enable channel thus inform the client. | |
2719 | */ | |
2720 | if (usess && usess->domain_global.channels) { | |
2721 | nb_chan += lttng_ht_get_count(usess->domain_global.channels); | |
2722 | } | |
2723 | if (ksession) { | |
2724 | nb_chan += ksession->channel_count; | |
2725 | } | |
2726 | if (!nb_chan) { | |
2727 | ret = LTTNG_ERR_NO_CHANNEL; | |
2728 | goto error; | |
2729 | } | |
2730 | ||
1f496244 JG |
2731 | session->active = 1; |
2732 | session->rotated_after_last_stop = false; | |
b02f5986 | 2733 | session->cleared_after_last_stop = false; |
070b6a86 | 2734 | if (session->output_traces && !session->current_trace_chunk) { |
1f496244 JG |
2735 | if (!session->has_been_started) { |
2736 | struct lttng_trace_chunk *trace_chunk; | |
2737 | ||
2738 | DBG("Creating initial trace chunk of session \"%s\"", | |
2739 | session->name); | |
2740 | trace_chunk = session_create_new_trace_chunk( | |
2741 | session, NULL, NULL, NULL); | |
2742 | if (!trace_chunk) { | |
2743 | ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
2744 | goto error; | |
2745 | } | |
2746 | assert(!session->current_trace_chunk); | |
2747 | ret = session_set_trace_chunk(session, trace_chunk, | |
2748 | NULL); | |
2749 | lttng_trace_chunk_put(trace_chunk); | |
2750 | if (ret) { | |
2751 | ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER; | |
2752 | goto error; | |
2753 | } | |
2754 | } else { | |
2755 | DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk", | |
2756 | session->name); | |
2757 | /* | |
2758 | * Rotate existing streams into the new chunk. | |
2759 | * This is a "quiet" rotation has no client has | |
2760 | * explicitly requested this operation. | |
2761 | * | |
2762 | * There is also no need to wait for the rotation | |
2763 | * to complete as it will happen immediately. No data | |
2764 | * was produced as the session was stopped, so the | |
2765 | * rotation should happen on reception of the command. | |
2766 | */ | |
343defc2 MD |
2767 | ret = cmd_rotate_session(session, NULL, true, |
2768 | LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION); | |
1f496244 JG |
2769 | if (ret != LTTNG_OK) { |
2770 | goto error; | |
2771 | } | |
5c408ad8 | 2772 | } |
c996624c JD |
2773 | } |
2774 | ||
2f77fc4b DG |
2775 | /* Kernel tracing */ |
2776 | if (ksession != NULL) { | |
c996624c | 2777 | DBG("Start kernel tracing session %s", session->name); |
7d268848 | 2778 | ret = start_kernel_session(ksession); |
9b6c7ec5 | 2779 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
2780 | goto error; |
2781 | } | |
2f77fc4b DG |
2782 | } |
2783 | ||
2784 | /* Flag session that trace should start automatically */ | |
2785 | if (usess) { | |
82b69413 JG |
2786 | int int_ret = ust_app_start_trace_all(usess); |
2787 | ||
2788 | if (int_ret < 0) { | |
f73fabfd | 2789 | ret = LTTNG_ERR_UST_START_FAIL; |
2f77fc4b DG |
2790 | goto error; |
2791 | } | |
2792 | } | |
2793 | ||
5c408ad8 JD |
2794 | /* |
2795 | * Clear the flag that indicates that a rotation was done while the | |
2796 | * session was stopped. | |
2797 | */ | |
2798 | session->rotated_after_last_stop = false; | |
2799 | ||
259c2674 | 2800 | if (session->rotate_timer_period) { |
82b69413 JG |
2801 | int int_ret = timer_session_rotation_schedule_timer_start( |
2802 | session, session->rotate_timer_period); | |
2803 | ||
2804 | if (int_ret < 0) { | |
259c2674 JD |
2805 | ERR("Failed to enable rotate timer"); |
2806 | ret = LTTNG_ERR_UNK; | |
2807 | goto error; | |
2808 | } | |
2809 | } | |
2810 | ||
f73fabfd | 2811 | ret = LTTNG_OK; |
2f77fc4b DG |
2812 | |
2813 | error: | |
1f496244 JG |
2814 | if (ret == LTTNG_OK) { |
2815 | /* Flag this after a successful start. */ | |
2816 | session->has_been_started |= 1; | |
2817 | } else { | |
2818 | session->active = 0; | |
2819 | /* Restore initial state on error. */ | |
2820 | session->rotated_after_last_stop = | |
2821 | session_rotated_after_last_stop; | |
b02f5986 MD |
2822 | session->cleared_after_last_stop = |
2823 | session_cleared_after_last_stop; | |
1f496244 | 2824 | } |
7a24ece3 | 2825 | end: |
2f77fc4b DG |
2826 | return ret; |
2827 | } | |
2828 | ||
2829 | /* | |
2830 | * Command LTTNG_STOP_TRACE processed by the client thread. | |
2831 | */ | |
2832 | int cmd_stop_trace(struct ltt_session *session) | |
2833 | { | |
2834 | int ret; | |
2f77fc4b DG |
2835 | struct ltt_kernel_session *ksession; |
2836 | struct ltt_ust_session *usess; | |
2837 | ||
2838 | assert(session); | |
2839 | ||
4dbe1875 | 2840 | DBG("Begin stop session \"%s\" (id %" PRIu64 ")", session->name, session->id); |
2f77fc4b DG |
2841 | /* Short cut */ |
2842 | ksession = session->kernel_session; | |
2843 | usess = session->ust_session; | |
2844 | ||
8382cf6f DG |
2845 | /* Session is not active. Skip everythong and inform the client. */ |
2846 | if (!session->active) { | |
f73fabfd | 2847 | ret = LTTNG_ERR_TRACE_ALREADY_STOPPED; |
2f77fc4b DG |
2848 | goto error; |
2849 | } | |
2850 | ||
4dbe1875 MD |
2851 | ret = stop_kernel_session(ksession); |
2852 | if (ret != LTTNG_OK) { | |
2853 | goto error; | |
2f77fc4b DG |
2854 | } |
2855 | ||
14fb1ebe | 2856 | if (usess && usess->active) { |
2f77fc4b DG |
2857 | ret = ust_app_stop_trace_all(usess); |
2858 | if (ret < 0) { | |
f73fabfd | 2859 | ret = LTTNG_ERR_UST_STOP_FAIL; |
2f77fc4b DG |
2860 | goto error; |
2861 | } | |
2862 | } | |
2863 | ||
4dbe1875 MD |
2864 | DBG("Completed stop session \"%s\" (id %" PRIu64 ")", session->name, |
2865 | session->id); | |
8382cf6f DG |
2866 | /* Flag inactive after a successful stop. */ |
2867 | session->active = 0; | |
4dbe1875 | 2868 | ret = LTTNG_OK; |
2f77fc4b DG |
2869 | |
2870 | error: | |
2871 | return ret; | |
2872 | } | |
2873 | ||
2874 | /* | |
433f5ba9 JR |
2875 | * Set the base_path of the session only if subdir of a control uris is set. |
2876 | * Return LTTNG_OK on success, otherwise LTTNG_ERR_*. | |
2f77fc4b | 2877 | */ |
433f5ba9 JR |
2878 | static int set_session_base_path_from_uris(struct ltt_session *session, |
2879 | size_t nb_uri, | |
bda32d56 | 2880 | struct lttng_uri *uris) |
2f77fc4b | 2881 | { |
433f5ba9 JR |
2882 | int ret; |
2883 | size_t i; | |
2f77fc4b | 2884 | |
e3876bf0 JR |
2885 | for (i = 0; i < nb_uri; i++) { |
2886 | if (uris[i].stype != LTTNG_STREAM_CONTROL || | |
2887 | uris[i].subdir[0] == '\0') { | |
2888 | /* Not interested in these URIs */ | |
2889 | continue; | |
2890 | } | |
2891 | ||
2892 | if (session->base_path != NULL) { | |
2893 | free(session->base_path); | |
2894 | session->base_path = NULL; | |
2895 | } | |
2896 | ||
2897 | /* Set session base_path */ | |
2898 | session->base_path = strdup(uris[i].subdir); | |
2899 | if (!session->base_path) { | |
433f5ba9 JR |
2900 | PERROR("Failed to copy base path \"%s\" to session \"%s\"", |
2901 | uris[i].subdir, session->name); | |
2902 | ret = LTTNG_ERR_NOMEM; | |
e3876bf0 JR |
2903 | goto error; |
2904 | } | |
433f5ba9 JR |
2905 | DBG2("Setting base path \"%s\" for session \"%s\"", |
2906 | session->base_path, session->name); | |
2907 | } | |
2908 | ret = LTTNG_OK; | |
2909 | error: | |
2910 | return ret; | |
2911 | } | |
2912 | ||
2913 | /* | |
2914 | * Command LTTNG_SET_CONSUMER_URI processed by the client thread. | |
2915 | */ | |
2916 | int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri, | |
2917 | struct lttng_uri *uris) | |
2918 | { | |
2919 | int ret, i; | |
2920 | struct ltt_kernel_session *ksess = session->kernel_session; | |
2921 | struct ltt_ust_session *usess = session->ust_session; | |
2922 | ||
2923 | assert(session); | |
2924 | assert(uris); | |
2925 | assert(nb_uri > 0); | |
2926 | ||
2927 | /* Can't set consumer URI if the session is active. */ | |
2928 | if (session->active) { | |
2929 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; | |
2930 | goto error; | |
2931 | } | |
2932 | ||
2933 | /* | |
2934 | * Set the session base path if any. This is done inside | |
2935 | * cmd_set_consumer_uri to preserve backward compatibility of the | |
2936 | * previous session creation api vs the session descriptor api. | |
2937 | */ | |
2938 | ret = set_session_base_path_from_uris(session, nb_uri, uris); | |
2939 | if (ret != LTTNG_OK) { | |
2940 | goto error; | |
e3876bf0 JR |
2941 | } |
2942 | ||
bda32d56 | 2943 | /* Set the "global" consumer URIs */ |
2f77fc4b | 2944 | for (i = 0; i < nb_uri; i++) { |
e3876bf0 JR |
2945 | ret = add_uri_to_consumer(session, session->consumer, &uris[i], |
2946 | LTTNG_DOMAIN_NONE); | |
a74934ba | 2947 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
2948 | goto error; |
2949 | } | |
2f77fc4b DG |
2950 | } |
2951 | ||
bda32d56 JG |
2952 | /* Set UST session URIs */ |
2953 | if (session->ust_session) { | |
2954 | for (i = 0; i < nb_uri; i++) { | |
b178f53e | 2955 | ret = add_uri_to_consumer(session, |
bda32d56 | 2956 | session->ust_session->consumer, |
b178f53e | 2957 | &uris[i], LTTNG_DOMAIN_UST); |
bda32d56 JG |
2958 | if (ret != LTTNG_OK) { |
2959 | goto error; | |
2960 | } | |
2961 | } | |
2962 | } | |
2963 | ||
2964 | /* Set kernel session URIs */ | |
2965 | if (session->kernel_session) { | |
2966 | for (i = 0; i < nb_uri; i++) { | |
b178f53e | 2967 | ret = add_uri_to_consumer(session, |
bda32d56 | 2968 | session->kernel_session->consumer, |
b178f53e | 2969 | &uris[i], LTTNG_DOMAIN_KERNEL); |
bda32d56 JG |
2970 | if (ret != LTTNG_OK) { |
2971 | goto error; | |
2972 | } | |
2973 | } | |
2974 | } | |
2975 | ||
7ab70fe0 DG |
2976 | /* |
2977 | * Make sure to set the session in output mode after we set URI since a | |
2978 | * session can be created without URL (thus flagged in no output mode). | |
2979 | */ | |
2980 | session->output_traces = 1; | |
2981 | if (ksess) { | |
2982 | ksess->output_traces = 1; | |
bda32d56 JG |
2983 | } |
2984 | ||
2985 | if (usess) { | |
7ab70fe0 DG |
2986 | usess->output_traces = 1; |
2987 | } | |
2988 | ||
2f77fc4b | 2989 | /* All good! */ |
f73fabfd | 2990 | ret = LTTNG_OK; |
2f77fc4b DG |
2991 | |
2992 | error: | |
2993 | return ret; | |
2994 | } | |
2995 | ||
b178f53e JG |
2996 | static |
2997 | enum lttng_error_code set_session_output_from_descriptor( | |
2998 | struct ltt_session *session, | |
2999 | const struct lttng_session_descriptor *descriptor) | |
2f77fc4b DG |
3000 | { |
3001 | int ret; | |
b178f53e JG |
3002 | enum lttng_error_code ret_code = LTTNG_OK; |
3003 | enum lttng_session_descriptor_type session_type = | |
3004 | lttng_session_descriptor_get_type(descriptor); | |
3005 | enum lttng_session_descriptor_output_type output_type = | |
3006 | lttng_session_descriptor_get_output_type(descriptor); | |
3007 | struct lttng_uri uris[2] = {}; | |
3008 | size_t uri_count = 0; | |
3009 | ||
3010 | switch (output_type) { | |
3011 | case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE: | |
3012 | goto end; | |
3013 | case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL: | |
3014 | lttng_session_descriptor_get_local_output_uri(descriptor, | |
3015 | &uris[0]); | |
3016 | uri_count = 1; | |
3017 | break; | |
3018 | case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK: | |
3019 | lttng_session_descriptor_get_network_output_uris(descriptor, | |
3020 | &uris[0], &uris[1]); | |
3021 | uri_count = 2; | |
3022 | break; | |
3023 | default: | |
3024 | ret_code = LTTNG_ERR_INVALID; | |
e32d7f27 | 3025 | goto end; |
2f77fc4b DG |
3026 | } |
3027 | ||
b178f53e JG |
3028 | switch (session_type) { |
3029 | case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT: | |
3030 | { | |
3031 | struct snapshot_output *new_output = NULL; | |
3032 | ||
3033 | new_output = snapshot_output_alloc(); | |
3034 | if (!new_output) { | |
3035 | ret_code = LTTNG_ERR_NOMEM; | |
3036 | goto end; | |
3037 | } | |
3038 | ||
3039 | ret = snapshot_output_init_with_uri(session, | |
3040 | DEFAULT_SNAPSHOT_MAX_SIZE, | |
3041 | NULL, uris, uri_count, session->consumer, | |
3042 | new_output, &session->snapshot); | |
3043 | if (ret < 0) { | |
3044 | ret_code = (ret == -ENOMEM) ? | |
3045 | LTTNG_ERR_NOMEM : LTTNG_ERR_INVALID; | |
3046 | snapshot_output_destroy(new_output); | |
3047 | goto end; | |
3048 | } | |
3049 | snapshot_add_output(&session->snapshot, new_output); | |
3050 | break; | |
3051 | } | |
3052 | case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR: | |
3053 | case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE: | |
3054 | { | |
3055 | ret_code = cmd_set_consumer_uri(session, uri_count, uris); | |
3056 | break; | |
3057 | } | |
3058 | default: | |
3059 | ret_code = LTTNG_ERR_INVALID; | |
e32d7f27 | 3060 | goto end; |
2f77fc4b | 3061 | } |
b178f53e JG |
3062 | end: |
3063 | return ret_code; | |
3064 | } | |
3065 | ||
3066 | static | |
3067 | enum lttng_error_code cmd_create_session_from_descriptor( | |
3068 | struct lttng_session_descriptor *descriptor, | |
3069 | const lttng_sock_cred *creds, | |
3070 | const char *home_path) | |
3071 | { | |
3072 | int ret; | |
3073 | enum lttng_error_code ret_code; | |
3074 | const char *session_name; | |
3075 | struct ltt_session *new_session = NULL; | |
3076 | enum lttng_session_descriptor_status descriptor_status; | |
2f77fc4b | 3077 | |
e32d7f27 | 3078 | session_lock_list(); |
b178f53e JG |
3079 | if (home_path) { |
3080 | if (*home_path != '/') { | |
3081 | ERR("Home path provided by client is not absolute"); | |
3082 | ret_code = LTTNG_ERR_INVALID; | |
3083 | goto end; | |
3084 | } | |
3085 | } | |
2f77fc4b | 3086 | |
b178f53e JG |
3087 | descriptor_status = lttng_session_descriptor_get_session_name( |
3088 | descriptor, &session_name); | |
3089 | switch (descriptor_status) { | |
3090 | case LTTNG_SESSION_DESCRIPTOR_STATUS_OK: | |
3091 | break; | |
3092 | case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET: | |
3093 | session_name = NULL; | |
3094 | break; | |
3095 | default: | |
3096 | ret_code = LTTNG_ERR_INVALID; | |
3097 | goto end; | |
3098 | } | |
e3876bf0 | 3099 | |
b178f53e | 3100 | ret_code = session_create(session_name, creds->uid, creds->gid, |
e3876bf0 | 3101 | &new_session); |
b178f53e | 3102 | if (ret_code != LTTNG_OK) { |
e32d7f27 | 3103 | goto end; |
2f77fc4b DG |
3104 | } |
3105 | ||
b178f53e JG |
3106 | if (!session_name) { |
3107 | ret = lttng_session_descriptor_set_session_name(descriptor, | |
3108 | new_session->name); | |
3109 | if (ret) { | |
3110 | ret_code = LTTNG_ERR_SESSION_FAIL; | |
3111 | goto end; | |
3112 | } | |
3113 | } | |
3114 | ||
3115 | if (!lttng_session_descriptor_is_output_destination_initialized( | |
3116 | descriptor)) { | |
3117 | /* | |
3118 | * Only include the session's creation time in the output | |
3119 | * destination if the name of the session itself was | |
3120 | * not auto-generated. | |
3121 | */ | |
3122 | ret_code = lttng_session_descriptor_set_default_output( | |
3123 | descriptor, | |
3124 | session_name ? &new_session->creation_time : NULL, | |
3125 | home_path); | |
3126 | if (ret_code != LTTNG_OK) { | |
e32d7f27 | 3127 | goto end; |
2bba9e53 | 3128 | } |
2bba9e53 | 3129 | } else { |
b178f53e JG |
3130 | new_session->has_user_specified_directory = |
3131 | lttng_session_descriptor_has_output_directory( | |
3132 | descriptor); | |
2f77fc4b DG |
3133 | } |
3134 | ||
b178f53e JG |
3135 | switch (lttng_session_descriptor_get_type(descriptor)) { |
3136 | case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT: | |
3137 | new_session->snapshot_mode = 1; | |
3138 | break; | |
3139 | case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE: | |
3140 | new_session->live_timer = | |
3141 | lttng_session_descriptor_live_get_timer_interval( | |
3142 | descriptor); | |
3143 | break; | |
3144 | default: | |
3145 | break; | |
3146 | } | |
2f77fc4b | 3147 | |
b178f53e JG |
3148 | ret_code = set_session_output_from_descriptor(new_session, descriptor); |
3149 | if (ret_code != LTTNG_OK) { | |
3150 | goto end; | |
3151 | } | |
3152 | new_session->consumer->enabled = 1; | |
3153 | ret_code = LTTNG_OK; | |
e32d7f27 | 3154 | end: |
b178f53e JG |
3155 | /* Release reference provided by the session_create function. */ |
3156 | session_put(new_session); | |
3157 | if (ret_code != LTTNG_OK && new_session) { | |
3158 | /* Release the global reference on error. */ | |
3159 | session_destroy(new_session); | |
e32d7f27 | 3160 | } |
b178f53e JG |
3161 | session_unlock_list(); |
3162 | return ret_code; | |
2f77fc4b DG |
3163 | } |
3164 | ||
b178f53e JG |
3165 | enum lttng_error_code cmd_create_session(struct command_ctx *cmd_ctx, int sock, |
3166 | struct lttng_session_descriptor **return_descriptor) | |
27babd3a DG |
3167 | { |
3168 | int ret; | |
b178f53e JG |
3169 | size_t payload_size; |
3170 | struct lttng_dynamic_buffer payload; | |
3171 | struct lttng_buffer_view home_dir_view; | |
3172 | struct lttng_buffer_view session_descriptor_view; | |
3173 | struct lttng_session_descriptor *session_descriptor = NULL; | |
3174 | enum lttng_error_code ret_code; | |
3175 | ||
3176 | lttng_dynamic_buffer_init(&payload); | |
3177 | if (cmd_ctx->lsm->u.create_session.home_dir_size >= | |
3178 | LTTNG_PATH_MAX) { | |
3179 | ret_code = LTTNG_ERR_INVALID; | |
3180 | goto error; | |
27babd3a | 3181 | } |
b178f53e JG |
3182 | if (cmd_ctx->lsm->u.create_session.session_descriptor_size > |
3183 | LTTNG_SESSION_DESCRIPTOR_MAX_LEN) { | |
3184 | ret_code = LTTNG_ERR_INVALID; | |
3185 | goto error; | |
27babd3a DG |
3186 | } |
3187 | ||
b178f53e JG |
3188 | payload_size = cmd_ctx->lsm->u.create_session.home_dir_size + |
3189 | cmd_ctx->lsm->u.create_session.session_descriptor_size; | |
3190 | ret = lttng_dynamic_buffer_set_size(&payload, payload_size); | |
3191 | if (ret) { | |
3192 | ret_code = LTTNG_ERR_NOMEM; | |
3193 | goto error; | |
27babd3a DG |
3194 | } |
3195 | ||
b178f53e JG |
3196 | ret = lttcomm_recv_unix_sock(sock, payload.data, payload.size); |
3197 | if (ret <= 0) { | |
3198 | ERR("Reception of session descriptor failed, aborting."); | |
3199 | ret_code = LTTNG_ERR_SESSION_FAIL; | |
3200 | goto error; | |
27babd3a DG |
3201 | } |
3202 | ||
b178f53e JG |
3203 | home_dir_view = lttng_buffer_view_from_dynamic_buffer( |
3204 | &payload, | |
3205 | 0, | |
3206 | cmd_ctx->lsm->u.create_session.home_dir_size); | |
3207 | session_descriptor_view = lttng_buffer_view_from_dynamic_buffer( | |
3208 | &payload, | |
3209 | cmd_ctx->lsm->u.create_session.home_dir_size, | |
3210 | cmd_ctx->lsm->u.create_session.session_descriptor_size); | |
27babd3a | 3211 | |
b178f53e JG |
3212 | ret = lttng_session_descriptor_create_from_buffer( |
3213 | &session_descriptor_view, &session_descriptor); | |
3214 | if (ret < 0) { | |
3215 | ERR("Failed to create session descriptor from payload of \"create session\" command"); | |
3216 | ret_code = LTTNG_ERR_INVALID; | |
3217 | goto error; | |
3218 | } | |
27babd3a | 3219 | |
b178f53e JG |
3220 | /* |
3221 | * Sets the descriptor's auto-generated properties (name, output) if | |
3222 | * needed. | |
3223 | */ | |
3224 | ret_code = cmd_create_session_from_descriptor(session_descriptor, | |
3225 | &cmd_ctx->creds, | |
3226 | home_dir_view.size ? home_dir_view.data : NULL); | |
3227 | if (ret_code != LTTNG_OK) { | |
3228 | goto error; | |
e32d7f27 | 3229 | } |
b178f53e JG |
3230 | |
3231 | ret_code = LTTNG_OK; | |
3232 | *return_descriptor = session_descriptor; | |
3233 | session_descriptor = NULL; | |
3234 | error: | |
3235 | lttng_dynamic_buffer_reset(&payload); | |
3236 | lttng_session_descriptor_destroy(session_descriptor); | |
3237 | return ret_code; | |
27babd3a DG |
3238 | } |
3239 | ||
3e3665b8 JG |
3240 | static |
3241 | void cmd_destroy_session_reply(const struct ltt_session *session, | |
3242 | void *_reply_context) | |
3243 | { | |
3244 | int ret; | |
3245 | ssize_t comm_ret; | |
3246 | const struct cmd_destroy_session_reply_context *reply_context = | |
3247 | _reply_context; | |
3248 | struct lttng_dynamic_buffer payload; | |
3249 | struct lttcomm_session_destroy_command_header cmd_header; | |
3250 | struct lttng_trace_archive_location *location = NULL; | |
3251 | struct lttcomm_lttng_msg llm = { | |
3252 | .cmd_type = LTTNG_DESTROY_SESSION, | |
3285a971 | 3253 | .ret_code = reply_context->destruction_status, |
3e3665b8 JG |
3254 | .pid = UINT32_MAX, |
3255 | .cmd_header_size = | |
3256 | sizeof(struct lttcomm_session_destroy_command_header), | |
3257 | .data_size = 0, | |
3258 | }; | |
3259 | size_t payload_size_before_location; | |
3260 | ||
3261 | lttng_dynamic_buffer_init(&payload); | |
3262 | ||
3263 | ret = lttng_dynamic_buffer_append(&payload, &llm, sizeof(llm)); | |
3264 | if (ret) { | |
3265 | ERR("Failed to append session destruction message"); | |
3266 | goto error; | |
3267 | } | |
3268 | ||
3269 | cmd_header.rotation_state = | |
3270 | (int32_t) (reply_context->implicit_rotation_on_destroy ? | |
3271 | session->rotation_state : | |
3272 | LTTNG_ROTATION_STATE_NO_ROTATION); | |
3273 | ret = lttng_dynamic_buffer_append(&payload, &cmd_header, | |
3274 | sizeof(cmd_header)); | |
3275 | if (ret) { | |
3276 | ERR("Failed to append session destruction command header"); | |
3277 | goto error; | |
3278 | } | |
3279 | ||
3280 | if (!reply_context->implicit_rotation_on_destroy) { | |
3281 | DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply", | |
3282 | session->name); | |
3283 | goto send_reply; | |
3284 | } | |
3285 | if (session->rotation_state != LTTNG_ROTATION_STATE_COMPLETED) { | |
3286 | DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply", | |
3287 | session->name); | |
3288 | goto send_reply; | |
3289 | } | |
3290 | ||
3291 | location = session_get_trace_archive_location(session); | |
3292 | if (!location) { | |
3293 | ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"", | |
3294 | session->name); | |
3295 | goto error; | |
3296 | } | |
3297 | ||
3298 | payload_size_before_location = payload.size; | |
3299 | comm_ret = lttng_trace_archive_location_serialize(location, | |
3300 | &payload); | |
3301 | if (comm_ret < 0) { | |
3302 | ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"", | |
3303 | session->name); | |
3304 | goto error; | |
3305 | } | |
3306 | /* Update the message to indicate the location's length. */ | |
3307 | ((struct lttcomm_lttng_msg *) payload.data)->data_size = | |
3308 | payload.size - payload_size_before_location; | |
3309 | send_reply: | |
3310 | comm_ret = lttcomm_send_unix_sock(reply_context->reply_sock_fd, | |
3311 | payload.data, payload.size); | |
3312 | if (comm_ret != (ssize_t) payload.size) { | |
3313 | ERR("Failed to send result of the destruction of session \"%s\" to client", | |
3314 | session->name); | |
3315 | } | |
3316 | error: | |
3317 | ret = close(reply_context->reply_sock_fd); | |
3318 | if (ret) { | |
3319 | PERROR("Failed to close client socket in deferred session destroy reply"); | |
3320 | } | |
3321 | lttng_dynamic_buffer_reset(&payload); | |
3322 | free(_reply_context); | |
3323 | } | |
3324 | ||
2f77fc4b DG |
3325 | /* |
3326 | * Command LTTNG_DESTROY_SESSION processed by the client thread. | |
a9ad0c8f MD |
3327 | * |
3328 | * Called with session lock held. | |
2f77fc4b | 3329 | */ |
e32d7f27 | 3330 | int cmd_destroy_session(struct ltt_session *session, |
3e3665b8 JG |
3331 | struct notification_thread_handle *notification_thread_handle, |
3332 | int *sock_fd) | |
2f77fc4b DG |
3333 | { |
3334 | int ret; | |
3285a971 | 3335 | enum lttng_error_code destruction_last_error = LTTNG_OK; |
3e3665b8 JG |
3336 | struct cmd_destroy_session_reply_context *reply_context = NULL; |
3337 | ||
3338 | if (sock_fd) { | |
3339 | reply_context = zmalloc(sizeof(*reply_context)); | |
3340 | if (!reply_context) { | |
3341 | ret = LTTNG_ERR_NOMEM; | |
3342 | goto end; | |
3343 | } | |
3344 | reply_context->reply_sock_fd = *sock_fd; | |
3345 | } | |
2f77fc4b DG |
3346 | |
3347 | /* Safety net */ | |
3348 | assert(session); | |
3349 | ||
3e3665b8 JG |
3350 | DBG("Begin destroy session %s (id %" PRIu64 ")", session->name, |
3351 | session->id); | |
3352 | if (session->active) { | |
3353 | DBG("Session \"%s\" is active, attempting to stop it before destroying it", | |
3354 | session->name); | |
3355 | ret = cmd_stop_trace(session); | |
3356 | if (ret != LTTNG_OK && ret != LTTNG_ERR_TRACE_ALREADY_STOPPED) { | |
3357 | /* Carry on with the destruction of the session. */ | |
3358 | ERR("Failed to stop session \"%s\" as part of its destruction: %s", | |
3359 | session->name, lttng_strerror(-ret)); | |
3285a971 | 3360 | destruction_last_error = ret; |
3e3665b8 JG |
3361 | } |
3362 | } | |
5c408ad8 | 3363 | |
92816cc3 JG |
3364 | if (session->rotation_schedule_timer_enabled) { |
3365 | if (timer_session_rotation_schedule_timer_stop( | |
3366 | session)) { | |
3367 | ERR("Failed to stop the \"rotation schedule\" timer of session %s", | |
3368 | session->name); | |
3285a971 | 3369 | destruction_last_error = LTTNG_ERR_TIMER_STOP_ERROR; |
92816cc3 | 3370 | } |
259c2674 JD |
3371 | } |
3372 | ||
90936dcf JD |
3373 | if (session->rotate_size) { |
3374 | unsubscribe_session_consumed_size_rotation(session, notification_thread_handle); | |
3375 | session->rotate_size = 0; | |
3376 | } | |
3377 | ||
a7ceb342 | 3378 | if (session->rotated && session->current_trace_chunk && session->output_traces) { |
b5893d8e JG |
3379 | /* |
3380 | * Perform a last rotation on destruction if rotations have | |
3381 | * occurred during the session's lifetime. | |
3382 | */ | |
343defc2 MD |
3383 | ret = cmd_rotate_session(session, NULL, false, |
3384 | LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED); | |
d2956687 JG |
3385 | if (ret != LTTNG_OK) { |
3386 | ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s", | |
3387 | session->name, lttng_strerror(-ret)); | |
3285a971 | 3388 | destruction_last_error = -ret; |
124473a3 | 3389 | } |
3e3665b8 JG |
3390 | if (reply_context) { |
3391 | reply_context->implicit_rotation_on_destroy = true; | |
3392 | } | |
070b6a86 | 3393 | } else if (session->has_been_started && session->current_trace_chunk) { |
7fdbed1c JG |
3394 | /* |
3395 | * The user has not triggered a session rotation. However, to | |
3396 | * ensure all data has been consumed, the session is rotated | |
3397 | * to a 'null' trace chunk before it is destroyed. | |
3398 | * | |
3399 | * This is a "quiet" rotation meaning that no notification is | |
3400 | * emitted and no renaming of the current trace chunk takes | |
3401 | * place. | |
3402 | */ | |
343defc2 MD |
3403 | ret = cmd_rotate_session(session, NULL, true, |
3404 | LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION); | |
53fb6336 JG |
3405 | /* |
3406 | * Rotation operations may not be supported by the kernel | |
3407 | * tracer. Hence, do not consider this implicit rotation as | |
3408 | * a session destruction error. The library has already stopped | |
3409 | * the session and waited for pending data; there is nothing | |
3410 | * left to do but complete the destruction of the session. | |
3411 | */ | |
3412 | if (ret != LTTNG_OK && | |
3413 | ret != -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL) { | |
7fdbed1c | 3414 | ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s", |
53fb6336 | 3415 | session->name, lttng_strerror(ret)); |
3285a971 | 3416 | destruction_last_error = -ret; |
7fdbed1c JG |
3417 | } |
3418 | } | |
5c408ad8 | 3419 | |
a503e1ef JG |
3420 | if (session->shm_path[0]) { |
3421 | /* | |
3422 | * When a session is created with an explicit shm_path, | |
3423 | * the consumer daemon will create its shared memory files | |
3424 | * at that location and will *not* unlink them. This is normal | |
3425 | * as the intention of that feature is to make it possible | |
3426 | * to retrieve the content of those files should a crash occur. | |
3427 | * | |
3428 | * To ensure the content of those files can be used, the | |
3429 | * sessiond daemon will replicate the content of the metadata | |
3430 | * cache in a metadata file. | |
3431 | * | |
3432 | * On clean-up, it is expected that the consumer daemon will | |
3433 | * unlink the shared memory files and that the session daemon | |
3434 | * will unlink the metadata file. Then, the session's directory | |
3435 | * in the shm path can be removed. | |
3436 | * | |
3437 | * Unfortunately, a flaw in the design of the sessiond's and | |
3438 | * consumerd's tear down of channels makes it impossible to | |
3439 | * determine when the sessiond _and_ the consumerd have both | |
3440 | * destroyed their representation of a channel. For one, the | |
3441 | * unlinking, close, and rmdir happen in deferred 'call_rcu' | |
3442 | * callbacks in both daemons. | |
3443 | * | |
3444 | * However, it is also impossible for the sessiond to know when | |
3445 | * the consumer daemon is done destroying its channel(s) since | |
3446 | * it occurs as a reaction to the closing of the channel's file | |
3447 | * descriptor. There is no resulting communication initiated | |
3448 | * from the consumerd to the sessiond to confirm that the | |
3449 | * operation is completed (and was successful). | |
3450 | * | |
3451 | * Until this is all fixed, the session daemon checks for the | |
3452 | * removal of the session's shm path which makes it possible | |
3453 | * to safely advertise a session as having been destroyed. | |
3454 | * | |
3455 | * Prior to this fix, it was not possible to reliably save | |
3456 | * a session making use of the --shm-path option, destroy it, | |
3457 | * and load it again. This is because the creation of the | |
3458 | * session would fail upon seeing the session's shm path | |
3459 | * already in existence. | |
3460 | * | |
3461 | * Note that none of the error paths in the check for the | |
3462 | * directory's existence return an error. This is normal | |
3463 | * as there isn't much that can be done. The session will | |
3464 | * be destroyed properly, except that we can't offer the | |
3465 | * guarantee that the same session can be re-created. | |
3466 | */ | |
3467 | current_completion_handler = &destroy_completion_handler.handler; | |
3468 | ret = lttng_strncpy(destroy_completion_handler.shm_path, | |
3469 | session->shm_path, | |
3470 | sizeof(destroy_completion_handler.shm_path)); | |
3471 | assert(!ret); | |
3472 | } | |
e32d7f27 JG |
3473 | |
3474 | /* | |
3475 | * The session is destroyed. However, note that the command context | |
3476 | * still holds a reference to the session, thus delaying its destruction | |
3477 | * _at least_ up to the point when that reference is released. | |
3478 | */ | |
3479 | session_destroy(session); | |
3e3665b8 | 3480 | if (reply_context) { |
3285a971 | 3481 | reply_context->destruction_status = destruction_last_error; |
3e3665b8 JG |
3482 | ret = session_add_destroy_notifier(session, |
3483 | cmd_destroy_session_reply, | |
3484 | (void *) reply_context); | |
3485 | if (ret) { | |
3486 | ret = LTTNG_ERR_FATAL; | |
3487 | goto end; | |
3488 | } else { | |
3489 | *sock_fd = -1; | |
3490 | } | |
3491 | } | |
3492 | ret = LTTNG_OK; | |
3493 | end: | |
2f77fc4b DG |
3494 | return ret; |
3495 | } | |
3496 | ||
2f77fc4b DG |
3497 | /* |
3498 | * Command LTTNG_REGISTER_CONSUMER processed by the client thread. | |
3499 | */ | |
56a37563 JG |
3500 | int cmd_register_consumer(struct ltt_session *session, |
3501 | enum lttng_domain_type domain, const char *sock_path, | |
3502 | struct consumer_data *cdata) | |
2f77fc4b DG |
3503 | { |
3504 | int ret, sock; | |
dd81b457 | 3505 | struct consumer_socket *socket = NULL; |
2f77fc4b DG |
3506 | |
3507 | assert(session); | |
3508 | assert(cdata); | |
3509 | assert(sock_path); | |
3510 | ||
3511 | switch (domain) { | |
3512 | case LTTNG_DOMAIN_KERNEL: | |
3513 | { | |
3514 | struct ltt_kernel_session *ksess = session->kernel_session; | |
3515 | ||
3516 | assert(ksess); | |
3517 | ||
3518 | /* Can't register a consumer if there is already one */ | |
3519 | if (ksess->consumer_fds_sent != 0) { | |
f73fabfd | 3520 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
2f77fc4b DG |
3521 | goto error; |
3522 | } | |
3523 | ||
3524 | sock = lttcomm_connect_unix_sock(sock_path); | |
3525 | if (sock < 0) { | |
f73fabfd | 3526 | ret = LTTNG_ERR_CONNECT_FAIL; |
2f77fc4b DG |
3527 | goto error; |
3528 | } | |
4ce514c4 | 3529 | cdata->cmd_sock = sock; |
2f77fc4b | 3530 | |
4ce514c4 | 3531 | socket = consumer_allocate_socket(&cdata->cmd_sock); |
2f77fc4b | 3532 | if (socket == NULL) { |
f66c074c DG |
3533 | ret = close(sock); |
3534 | if (ret < 0) { | |
3535 | PERROR("close register consumer"); | |
3536 | } | |
4ce514c4 | 3537 | cdata->cmd_sock = -1; |
f73fabfd | 3538 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
3539 | goto error; |
3540 | } | |
3541 | ||
3542 | socket->lock = zmalloc(sizeof(pthread_mutex_t)); | |
3543 | if (socket->lock == NULL) { | |
3544 | PERROR("zmalloc pthread mutex"); | |
f73fabfd | 3545 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
3546 | goto error; |
3547 | } | |
3548 | pthread_mutex_init(socket->lock, NULL); | |
3549 | socket->registered = 1; | |
3550 | ||
3551 | rcu_read_lock(); | |
3552 | consumer_add_socket(socket, ksess->consumer); | |
3553 | rcu_read_unlock(); | |
3554 | ||
3555 | pthread_mutex_lock(&cdata->pid_mutex); | |
3556 | cdata->pid = -1; | |
3557 | pthread_mutex_unlock(&cdata->pid_mutex); | |
3558 | ||
3559 | break; | |
3560 | } | |
3561 | default: | |
3562 | /* TODO: Userspace tracing */ | |
f73fabfd | 3563 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
3564 | goto error; |
3565 | } | |
3566 | ||
dd81b457 | 3567 | return LTTNG_OK; |
2f77fc4b DG |
3568 | |
3569 | error: | |
dd81b457 DG |
3570 | if (socket) { |
3571 | consumer_destroy_socket(socket); | |
3572 | } | |
2f77fc4b DG |
3573 | return ret; |
3574 | } | |
3575 | ||
3576 | /* | |
3577 | * Command LTTNG_LIST_DOMAINS processed by the client thread. | |
3578 | */ | |
3579 | ssize_t cmd_list_domains(struct ltt_session *session, | |
3580 | struct lttng_domain **domains) | |
3581 | { | |
3582 | int ret, index = 0; | |
3583 | ssize_t nb_dom = 0; | |
fefd409b DG |
3584 | struct agent *agt; |
3585 | struct lttng_ht_iter iter; | |
2f77fc4b DG |
3586 | |
3587 | if (session->kernel_session != NULL) { | |
3588 | DBG3("Listing domains found kernel domain"); | |
3589 | nb_dom++; | |
3590 | } | |
3591 | ||
3592 | if (session->ust_session != NULL) { | |
3593 | DBG3("Listing domains found UST global domain"); | |
3594 | nb_dom++; | |
3c6a091f | 3595 | |
e0a74f01 | 3596 | rcu_read_lock(); |
fefd409b DG |
3597 | cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter, |
3598 | agt, node.node) { | |
3599 | if (agt->being_used) { | |
3600 | nb_dom++; | |
3601 | } | |
3c6a091f | 3602 | } |
e0a74f01 | 3603 | rcu_read_unlock(); |
2f77fc4b DG |
3604 | } |
3605 | ||
fa64dfb4 JG |
3606 | if (!nb_dom) { |
3607 | goto end; | |
3608 | } | |
3609 | ||
2f77fc4b DG |
3610 | *domains = zmalloc(nb_dom * sizeof(struct lttng_domain)); |
3611 | if (*domains == NULL) { | |
f73fabfd | 3612 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
3613 | goto error; |
3614 | } | |
3615 | ||
3616 | if (session->kernel_session != NULL) { | |
3617 | (*domains)[index].type = LTTNG_DOMAIN_KERNEL; | |
b5edb9e8 PP |
3618 | |
3619 | /* Kernel session buffer type is always GLOBAL */ | |
3620 | (*domains)[index].buf_type = LTTNG_BUFFER_GLOBAL; | |
3621 | ||
2f77fc4b DG |
3622 | index++; |
3623 | } | |
3624 | ||
3625 | if (session->ust_session != NULL) { | |
3626 | (*domains)[index].type = LTTNG_DOMAIN_UST; | |
88c5f0d8 | 3627 | (*domains)[index].buf_type = session->ust_session->buffer_type; |
2f77fc4b | 3628 | index++; |
3c6a091f | 3629 | |
e0a74f01 | 3630 | rcu_read_lock(); |
fefd409b DG |
3631 | cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter, |
3632 | agt, node.node) { | |
3633 | if (agt->being_used) { | |
3634 | (*domains)[index].type = agt->domain; | |
3635 | (*domains)[index].buf_type = session->ust_session->buffer_type; | |
3636 | index++; | |
3637 | } | |
3c6a091f | 3638 | } |
e0a74f01 | 3639 | rcu_read_unlock(); |
2f77fc4b | 3640 | } |
fa64dfb4 | 3641 | end: |
2f77fc4b DG |
3642 | return nb_dom; |
3643 | ||
3644 | error: | |
f73fabfd DG |
3645 | /* Return negative value to differentiate return code */ |
3646 | return -ret; | |
2f77fc4b DG |
3647 | } |
3648 | ||
3649 | ||
3650 | /* | |
3651 | * Command LTTNG_LIST_CHANNELS processed by the client thread. | |
3652 | */ | |
56a37563 JG |
3653 | ssize_t cmd_list_channels(enum lttng_domain_type domain, |
3654 | struct ltt_session *session, struct lttng_channel **channels) | |
2f77fc4b | 3655 | { |
53e367f9 | 3656 | ssize_t nb_chan = 0, payload_size = 0, ret; |
2f77fc4b DG |
3657 | |
3658 | switch (domain) { | |
3659 | case LTTNG_DOMAIN_KERNEL: | |
3660 | if (session->kernel_session != NULL) { | |
3661 | nb_chan = session->kernel_session->channel_count; | |
3662 | } | |
3663 | DBG3("Number of kernel channels %zd", nb_chan); | |
c7d620a2 | 3664 | if (nb_chan <= 0) { |
53e367f9 JG |
3665 | ret = -LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
3666 | goto end; | |
c7d620a2 | 3667 | } |
2f77fc4b DG |
3668 | break; |
3669 | case LTTNG_DOMAIN_UST: | |
3670 | if (session->ust_session != NULL) { | |
7ffc31a2 | 3671 | rcu_read_lock(); |
2f77fc4b | 3672 | nb_chan = lttng_ht_get_count( |
7ffc31a2 JG |
3673 | session->ust_session->domain_global.channels); |
3674 | rcu_read_unlock(); | |
2f77fc4b DG |
3675 | } |
3676 | DBG3("Number of UST global channels %zd", nb_chan); | |
ade7ce52 | 3677 | if (nb_chan < 0) { |
53e367f9 JG |
3678 | ret = -LTTNG_ERR_UST_CHAN_NOT_FOUND; |
3679 | goto end; | |
c7d620a2 | 3680 | } |
2f77fc4b DG |
3681 | break; |
3682 | default: | |
53e367f9 JG |
3683 | ret = -LTTNG_ERR_UND; |
3684 | goto end; | |
2f77fc4b DG |
3685 | } |
3686 | ||
3687 | if (nb_chan > 0) { | |
53e367f9 | 3688 | const size_t channel_size = sizeof(struct lttng_channel) + |
cf0bcb51 JG |
3689 | sizeof(struct lttng_channel_extended); |
3690 | struct lttng_channel_extended *channel_exts; | |
53e367f9 JG |
3691 | |
3692 | payload_size = nb_chan * channel_size; | |
3693 | *channels = zmalloc(payload_size); | |
2f77fc4b | 3694 | if (*channels == NULL) { |
53e367f9 JG |
3695 | ret = -LTTNG_ERR_FATAL; |
3696 | goto end; | |
2f77fc4b DG |
3697 | } |
3698 | ||
53e367f9 JG |
3699 | channel_exts = ((void *) *channels) + |
3700 | (nb_chan * sizeof(struct lttng_channel)); | |
d449df4a MD |
3701 | ret = list_lttng_channels(domain, session, *channels, channel_exts); |
3702 | if (ret != LTTNG_OK) { | |
3703 | free(*channels); | |
3704 | *channels = NULL; | |
3705 | goto end; | |
3706 | } | |
53e367f9 JG |
3707 | } else { |
3708 | *channels = NULL; | |
2f77fc4b DG |
3709 | } |
3710 | ||
53e367f9 JG |
3711 | ret = payload_size; |
3712 | end: | |
3713 | return ret; | |
2f77fc4b DG |
3714 | } |
3715 | ||
3716 | /* | |
3717 | * Command LTTNG_LIST_EVENTS processed by the client thread. | |
3718 | */ | |
56a37563 JG |
3719 | ssize_t cmd_list_events(enum lttng_domain_type domain, |
3720 | struct ltt_session *session, char *channel_name, | |
b4e3ceb9 | 3721 | struct lttng_event **events, size_t *total_size) |
2f77fc4b DG |
3722 | { |
3723 | int ret = 0; | |
3724 | ssize_t nb_event = 0; | |
3725 | ||
3726 | switch (domain) { | |
3727 | case LTTNG_DOMAIN_KERNEL: | |
3728 | if (session->kernel_session != NULL) { | |
3729 | nb_event = list_lttng_kernel_events(channel_name, | |
b4e3ceb9 PP |
3730 | session->kernel_session, events, |
3731 | total_size); | |
2f77fc4b DG |
3732 | } |
3733 | break; | |
3734 | case LTTNG_DOMAIN_UST: | |
3735 | { | |
3736 | if (session->ust_session != NULL) { | |
3737 | nb_event = list_lttng_ust_global_events(channel_name, | |
b4e3ceb9 PP |
3738 | &session->ust_session->domain_global, events, |
3739 | total_size); | |
2f77fc4b DG |
3740 | } |
3741 | break; | |
3742 | } | |
5cdb6027 | 3743 | case LTTNG_DOMAIN_LOG4J: |
3c6a091f | 3744 | case LTTNG_DOMAIN_JUL: |
0e115563 | 3745 | case LTTNG_DOMAIN_PYTHON: |
3c6a091f | 3746 | if (session->ust_session) { |
fefd409b DG |
3747 | struct lttng_ht_iter iter; |
3748 | struct agent *agt; | |
3749 | ||
b11feea5 | 3750 | rcu_read_lock(); |
fefd409b DG |
3751 | cds_lfht_for_each_entry(session->ust_session->agents->ht, |
3752 | &iter.iter, agt, node.node) { | |
1dfd9906 JG |
3753 | if (agt->domain == domain) { |
3754 | nb_event = list_lttng_agent_events( | |
b4e3ceb9 PP |
3755 | agt, events, |
3756 | total_size); | |
1dfd9906 JG |
3757 | break; |
3758 | } | |
fefd409b | 3759 | } |
b11feea5 | 3760 | rcu_read_unlock(); |
3c6a091f DG |
3761 | } |
3762 | break; | |
2f77fc4b | 3763 | default: |
f73fabfd | 3764 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
3765 | goto error; |
3766 | } | |
3767 | ||
f73fabfd | 3768 | return nb_event; |
2f77fc4b DG |
3769 | |
3770 | error: | |
f73fabfd DG |
3771 | /* Return negative value to differentiate return code */ |
3772 | return -ret; | |
2f77fc4b DG |
3773 | } |
3774 | ||
3775 | /* | |
3776 | * Using the session list, filled a lttng_session array to send back to the | |
3777 | * client for session listing. | |
3778 | * | |
3779 | * The session list lock MUST be acquired before calling this function. Use | |
3780 | * session_lock_list() and session_unlock_list(). | |
3781 | */ | |
b178f53e JG |
3782 | void cmd_list_lttng_sessions(struct lttng_session *sessions, |
3783 | size_t session_count, uid_t uid, gid_t gid) | |
2f77fc4b DG |
3784 | { |
3785 | int ret; | |
3786 | unsigned int i = 0; | |
3787 | struct ltt_session *session; | |
3788 | struct ltt_session_list *list = session_get_list(); | |
b178f53e JG |
3789 | struct lttng_session_extended *extended = |
3790 | (typeof(extended)) (&sessions[session_count]); | |
2f77fc4b DG |
3791 | |
3792 | DBG("Getting all available session for UID %d GID %d", | |
3793 | uid, gid); | |
3794 | /* | |
3795 | * Iterate over session list and append data after the control struct in | |
3796 | * the buffer. | |
3797 | */ | |
3798 | cds_list_for_each_entry(session, &list->head, list) { | |
e32d7f27 JG |
3799 | if (!session_get(session)) { |
3800 | continue; | |
3801 | } | |
2f77fc4b DG |
3802 | /* |
3803 | * Only list the sessions the user can control. | |
3804 | */ | |
e32d7f27 JG |
3805 | if (!session_access_ok(session, uid, gid) || |
3806 | session->destroyed) { | |
3807 | session_put(session); | |
2f77fc4b DG |
3808 | continue; |
3809 | } | |
3810 | ||
3811 | struct ltt_kernel_session *ksess = session->kernel_session; | |
3812 | struct ltt_ust_session *usess = session->ust_session; | |
3813 | ||
3814 | if (session->consumer->type == CONSUMER_DST_NET || | |
3815 | (ksess && ksess->consumer->type == CONSUMER_DST_NET) || | |
3816 | (usess && usess->consumer->type == CONSUMER_DST_NET)) { | |
3817 | ret = build_network_session_path(sessions[i].path, | |
dec56f6c | 3818 | sizeof(sessions[i].path), session); |
2f77fc4b | 3819 | } else { |
dec56f6c | 3820 | ret = snprintf(sessions[i].path, sizeof(sessions[i].path), "%s", |
366a9222 | 3821 | session->consumer->dst.session_root_path); |
2f77fc4b DG |
3822 | } |
3823 | if (ret < 0) { | |
3824 | PERROR("snprintf session path"); | |
e32d7f27 | 3825 | session_put(session); |
2f77fc4b DG |
3826 | continue; |
3827 | } | |
3828 | ||
3829 | strncpy(sessions[i].name, session->name, NAME_MAX); | |
3830 | sessions[i].name[NAME_MAX - 1] = '\0'; | |
8382cf6f | 3831 | sessions[i].enabled = session->active; |
2cbf8fed | 3832 | sessions[i].snapshot_mode = session->snapshot_mode; |
8960e9cd | 3833 | sessions[i].live_timer_interval = session->live_timer; |
b178f53e JG |
3834 | extended[i].creation_time.value = (uint64_t) session->creation_time; |
3835 | extended[i].creation_time.is_set = 1; | |
2f77fc4b | 3836 | i++; |
e32d7f27 | 3837 | session_put(session); |
2f77fc4b DG |
3838 | } |
3839 | } | |
3840 | ||
806e2684 | 3841 | /* |
6d805429 | 3842 | * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning |
d3f14b8a | 3843 | * ready for trace analysis (or any kind of reader) or else 1 for pending data. |
806e2684 | 3844 | */ |
6d805429 | 3845 | int cmd_data_pending(struct ltt_session *session) |
806e2684 DG |
3846 | { |
3847 | int ret; | |
3848 | struct ltt_kernel_session *ksess = session->kernel_session; | |
3849 | struct ltt_ust_session *usess = session->ust_session; | |
3850 | ||
3851 | assert(session); | |
3852 | ||
5c408ad8 JD |
3853 | DBG("Data pending for session %s", session->name); |
3854 | ||
806e2684 | 3855 | /* Session MUST be stopped to ask for data availability. */ |
8382cf6f | 3856 | if (session->active) { |
806e2684 DG |
3857 | ret = LTTNG_ERR_SESSION_STARTED; |
3858 | goto error; | |
3a89d11a DG |
3859 | } else { |
3860 | /* | |
3861 | * If stopped, just make sure we've started before else the above call | |
3862 | * will always send that there is data pending. | |
3863 | * | |
3864 | * The consumer assumes that when the data pending command is received, | |
3865 | * the trace has been started before or else no output data is written | |
3866 | * by the streams which is a condition for data pending. So, this is | |
3867 | * *VERY* important that we don't ask the consumer before a start | |
3868 | * trace. | |
3869 | */ | |
8382cf6f | 3870 | if (!session->has_been_started) { |
3a89d11a DG |
3871 | ret = 0; |
3872 | goto error; | |
3873 | } | |
806e2684 DG |
3874 | } |
3875 | ||
92816cc3 JG |
3876 | /* A rotation is still pending, we have to wait. */ |
3877 | if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) { | |
5c408ad8 JD |
3878 | DBG("Rotate still pending for session %s", session->name); |
3879 | ret = 1; | |
3880 | goto error; | |
3881 | } | |
3882 | ||
806e2684 | 3883 | if (ksess && ksess->consumer) { |
6d805429 DG |
3884 | ret = consumer_is_data_pending(ksess->id, ksess->consumer); |
3885 | if (ret == 1) { | |
806e2684 DG |
3886 | /* Data is still being extracted for the kernel. */ |
3887 | goto error; | |
3888 | } | |
3889 | } | |
3890 | ||
3891 | if (usess && usess->consumer) { | |
6d805429 DG |
3892 | ret = consumer_is_data_pending(usess->id, usess->consumer); |
3893 | if (ret == 1) { | |
806e2684 DG |
3894 | /* Data is still being extracted for the kernel. */ |
3895 | goto error; | |
3896 | } | |
3897 | } | |
3898 | ||
3899 | /* Data is ready to be read by a viewer */ | |
6d805429 | 3900 | ret = 0; |
806e2684 DG |
3901 | |
3902 | error: | |
3903 | return ret; | |
3904 | } | |
3905 | ||
6dc3064a DG |
3906 | /* |
3907 | * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library. | |
3908 | * | |
3909 | * Return LTTNG_OK on success or else a LTTNG_ERR code. | |
3910 | */ | |
3911 | int cmd_snapshot_add_output(struct ltt_session *session, | |
df4f5a87 | 3912 | const struct lttng_snapshot_output *output, uint32_t *id) |
6dc3064a DG |
3913 | { |
3914 | int ret; | |
3915 | struct snapshot_output *new_output; | |
3916 | ||
3917 | assert(session); | |
3918 | assert(output); | |
3919 | ||
3920 | DBG("Cmd snapshot add output for session %s", session->name); | |
3921 | ||
3922 | /* | |
903ef685 | 3923 | * Can't create an output if the session is not set in no-output mode. |
6dc3064a DG |
3924 | */ |
3925 | if (session->output_traces) { | |
903ef685 | 3926 | ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
6dc3064a DG |
3927 | goto error; |
3928 | } | |
3929 | ||
54213acc JG |
3930 | if (session->has_non_mmap_channel) { |
3931 | ret = LTTNG_ERR_SNAPSHOT_UNSUPPORTED; | |
3932 | goto error; | |
3933 | } | |
3934 | ||
6dc3064a DG |
3935 | /* Only one output is allowed until we have the "tee" feature. */ |
3936 | if (session->snapshot.nb_output == 1) { | |
3937 | ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST; | |
3938 | goto error; | |
3939 | } | |
3940 | ||
3941 | new_output = snapshot_output_alloc(); | |
3942 | if (!new_output) { | |
3943 | ret = LTTNG_ERR_NOMEM; | |
3944 | goto error; | |
3945 | } | |
3946 | ||
b178f53e | 3947 | ret = snapshot_output_init(session, output->max_size, output->name, |
6dc3064a DG |
3948 | output->ctrl_url, output->data_url, session->consumer, new_output, |
3949 | &session->snapshot); | |
3950 | if (ret < 0) { | |
3951 | if (ret == -ENOMEM) { | |
3952 | ret = LTTNG_ERR_NOMEM; | |
3953 | } else { | |
3954 | ret = LTTNG_ERR_INVALID; | |
3955 | } | |
3956 | goto free_error; | |
3957 | } | |
3958 | ||
6dc3064a DG |
3959 | rcu_read_lock(); |
3960 | snapshot_add_output(&session->snapshot, new_output); | |
3961 | if (id) { | |
3962 | *id = new_output->id; | |
3963 | } | |
3964 | rcu_read_unlock(); | |
3965 | ||
3966 | return LTTNG_OK; | |
3967 | ||
3968 | free_error: | |
3969 | snapshot_output_destroy(new_output); | |
3970 | error: | |
3971 | return ret; | |
3972 | } | |
3973 | ||
3974 | /* | |
3975 | * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl. | |
3976 | * | |
3977 | * Return LTTNG_OK on success or else a LTTNG_ERR code. | |
3978 | */ | |
3979 | int cmd_snapshot_del_output(struct ltt_session *session, | |
df4f5a87 | 3980 | const struct lttng_snapshot_output *output) |
6dc3064a DG |
3981 | { |
3982 | int ret; | |
eb240553 | 3983 | struct snapshot_output *sout = NULL; |
6dc3064a DG |
3984 | |
3985 | assert(session); | |
3986 | assert(output); | |
3987 | ||
6dc3064a DG |
3988 | rcu_read_lock(); |
3989 | ||
3990 | /* | |
d3f14b8a MD |
3991 | * Permission denied to create an output if the session is not |
3992 | * set in no output mode. | |
6dc3064a DG |
3993 | */ |
3994 | if (session->output_traces) { | |
903ef685 | 3995 | ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
6dc3064a DG |
3996 | goto error; |
3997 | } | |
3998 | ||
eb240553 DG |
3999 | if (output->id) { |
4000 | DBG("Cmd snapshot del output id %" PRIu32 " for session %s", output->id, | |
4001 | session->name); | |
4002 | sout = snapshot_find_output_by_id(output->id, &session->snapshot); | |
4003 | } else if (*output->name != '\0') { | |
4004 | DBG("Cmd snapshot del output name %s for session %s", output->name, | |
4005 | session->name); | |
4006 | sout = snapshot_find_output_by_name(output->name, &session->snapshot); | |
4007 | } | |
6dc3064a DG |
4008 | if (!sout) { |
4009 | ret = LTTNG_ERR_INVALID; | |
4010 | goto error; | |
4011 | } | |
4012 | ||
4013 | snapshot_delete_output(&session->snapshot, sout); | |
4014 | snapshot_output_destroy(sout); | |
4015 | ret = LTTNG_OK; | |
4016 | ||
4017 | error: | |
4018 | rcu_read_unlock(); | |
4019 | return ret; | |
4020 | } | |
4021 | ||
4022 | /* | |
4023 | * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl. | |
4024 | * | |
4025 | * If no output is available, outputs is untouched and 0 is returned. | |
4026 | * | |
4027 | * Return the size of the newly allocated outputs or a negative LTTNG_ERR code. | |
4028 | */ | |
4029 | ssize_t cmd_snapshot_list_outputs(struct ltt_session *session, | |
4030 | struct lttng_snapshot_output **outputs) | |
4031 | { | |
4032 | int ret, idx = 0; | |
b223ca94 | 4033 | struct lttng_snapshot_output *list = NULL; |
6dc3064a DG |
4034 | struct lttng_ht_iter iter; |
4035 | struct snapshot_output *output; | |
4036 | ||
4037 | assert(session); | |
4038 | assert(outputs); | |
4039 | ||
4040 | DBG("Cmd snapshot list outputs for session %s", session->name); | |
4041 | ||
4042 | /* | |
d3f14b8a MD |
4043 | * Permission denied to create an output if the session is not |
4044 | * set in no output mode. | |
6dc3064a DG |
4045 | */ |
4046 | if (session->output_traces) { | |
903ef685 JG |
4047 | ret = -LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
4048 | goto end; | |
6dc3064a DG |
4049 | } |
4050 | ||
4051 | if (session->snapshot.nb_output == 0) { | |
4052 | ret = 0; | |
903ef685 | 4053 | goto end; |
6dc3064a DG |
4054 | } |
4055 | ||
4056 | list = zmalloc(session->snapshot.nb_output * sizeof(*list)); | |
4057 | if (!list) { | |
b223ca94 | 4058 | ret = -LTTNG_ERR_NOMEM; |
903ef685 | 4059 | goto end; |
6dc3064a DG |
4060 | } |
4061 | ||
4062 | /* Copy list from session to the new list object. */ | |
b223ca94 | 4063 | rcu_read_lock(); |
6dc3064a DG |
4064 | cds_lfht_for_each_entry(session->snapshot.output_ht->ht, &iter.iter, |
4065 | output, node.node) { | |
4066 | assert(output->consumer); | |
4067 | list[idx].id = output->id; | |
4068 | list[idx].max_size = output->max_size; | |
6ce22875 MD |
4069 | if (lttng_strncpy(list[idx].name, output->name, |
4070 | sizeof(list[idx].name))) { | |
4071 | ret = -LTTNG_ERR_INVALID; | |
903ef685 | 4072 | goto error; |
6ce22875 | 4073 | } |
6dc3064a | 4074 | if (output->consumer->type == CONSUMER_DST_LOCAL) { |
6ce22875 | 4075 | if (lttng_strncpy(list[idx].ctrl_url, |
366a9222 | 4076 | output->consumer->dst.session_root_path, |
6ce22875 MD |
4077 | sizeof(list[idx].ctrl_url))) { |
4078 | ret = -LTTNG_ERR_INVALID; | |
903ef685 | 4079 | goto error; |
6ce22875 | 4080 | } |
6dc3064a DG |
4081 | } else { |
4082 | /* Control URI. */ | |
4083 | ret = uri_to_str_url(&output->consumer->dst.net.control, | |
4084 | list[idx].ctrl_url, sizeof(list[idx].ctrl_url)); | |
4085 | if (ret < 0) { | |
b223ca94 | 4086 | ret = -LTTNG_ERR_NOMEM; |
903ef685 | 4087 | goto error; |
6dc3064a DG |
4088 | } |
4089 | ||
4090 | /* Data URI. */ | |
4091 | ret = uri_to_str_url(&output->consumer->dst.net.data, | |
4092 | list[idx].data_url, sizeof(list[idx].data_url)); | |
4093 | if (ret < 0) { | |
b223ca94 | 4094 | ret = -LTTNG_ERR_NOMEM; |
903ef685 | 4095 | goto error; |
6dc3064a DG |
4096 | } |
4097 | } | |
4098 | idx++; | |
4099 | } | |
4100 | ||
4101 | *outputs = list; | |
b223ca94 JG |
4102 | list = NULL; |
4103 | ret = session->snapshot.nb_output; | |
6dc3064a | 4104 | error: |
903ef685 | 4105 | rcu_read_unlock(); |
b223ca94 | 4106 | free(list); |
903ef685 | 4107 | end: |
b223ca94 | 4108 | return ret; |
6dc3064a DG |
4109 | } |
4110 | ||
93ec662e JD |
4111 | /* |
4112 | * Check if we can regenerate the metadata for this session. | |
4113 | * Only kernel, UST per-uid and non-live sessions are supported. | |
4114 | * | |
4115 | * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise. | |
4116 | */ | |
4117 | static | |
eded6438 | 4118 | int check_regenerate_metadata_support(struct ltt_session *session) |
93ec662e JD |
4119 | { |
4120 | int ret; | |
4121 | ||
4122 | assert(session); | |
4123 | ||
4124 | if (session->live_timer != 0) { | |
4125 | ret = LTTNG_ERR_LIVE_SESSION; | |
4126 | goto end; | |
4127 | } | |
4128 | if (!session->active) { | |
4129 | ret = LTTNG_ERR_SESSION_NOT_STARTED; | |
4130 | goto end; | |
4131 | } | |
4132 | if (session->ust_session) { | |
4133 | switch (session->ust_session->buffer_type) { | |
4134 | case LTTNG_BUFFER_PER_UID: | |
4135 | break; | |
4136 | case LTTNG_BUFFER_PER_PID: | |
4137 | ret = LTTNG_ERR_PER_PID_SESSION; | |
4138 | goto end; | |
4139 | default: | |
4140 | assert(0); | |
4141 | ret = LTTNG_ERR_UNK; | |
4142 | goto end; | |
4143 | } | |
4144 | } | |
4145 | if (session->consumer->type == CONSUMER_DST_NET && | |
4146 | session->consumer->relay_minor_version < 8) { | |
4147 | ret = LTTNG_ERR_RELAYD_VERSION_FAIL; | |
4148 | goto end; | |
4149 | } | |
4150 | ret = 0; | |
4151 | ||
4152 | end: | |
4153 | return ret; | |
4154 | } | |
4155 | ||
7802bae3 LL |
4156 | static |
4157 | int clear_metadata_file(int fd) | |
4158 | { | |
4159 | int ret; | |
a5df8828 | 4160 | off_t lseek_ret; |
7802bae3 | 4161 | |
a5df8828 GL |
4162 | lseek_ret = lseek(fd, 0, SEEK_SET); |
4163 | if (lseek_ret < 0) { | |
7802bae3 | 4164 | PERROR("lseek"); |
a5df8828 | 4165 | ret = -1; |
7802bae3 LL |
4166 | goto end; |
4167 | } | |
4168 | ||
4169 | ret = ftruncate(fd, 0); | |
4170 | if (ret < 0) { | |
4171 | PERROR("ftruncate"); | |
4172 | goto end; | |
4173 | } | |
4174 | ||
4175 | end: | |
4176 | return ret; | |
4177 | } | |
4178 | ||
93ec662e | 4179 | static |
eded6438 | 4180 | int ust_regenerate_metadata(struct ltt_ust_session *usess) |
93ec662e JD |
4181 | { |
4182 | int ret = 0; | |
4183 | struct buffer_reg_uid *uid_reg = NULL; | |
4184 | struct buffer_reg_session *session_reg = NULL; | |
4185 | ||
4186 | rcu_read_lock(); | |
4187 | cds_list_for_each_entry(uid_reg, &usess->buffer_reg_uid_list, lnode) { | |
4188 | struct ust_registry_session *registry; | |
4189 | struct ust_registry_channel *chan; | |
4190 | struct lttng_ht_iter iter_chan; | |
4191 | ||
4192 | session_reg = uid_reg->registry; | |
4193 | registry = session_reg->reg.ust; | |
4194 | ||
4195 | pthread_mutex_lock(®istry->lock); | |
4196 | registry->metadata_len_sent = 0; | |
4197 | memset(registry->metadata, 0, registry->metadata_alloc_len); | |
4198 | registry->metadata_len = 0; | |
4199 | registry->metadata_version++; | |
7802bae3 LL |
4200 | if (registry->metadata_fd > 0) { |
4201 | /* Clear the metadata file's content. */ | |
4202 | ret = clear_metadata_file(registry->metadata_fd); | |
4203 | if (ret) { | |
4204 | pthread_mutex_unlock(®istry->lock); | |
4205 | goto end; | |
4206 | } | |
4207 | } | |
4208 | ||
93ec662e JD |
4209 | ret = ust_metadata_session_statedump(registry, NULL, |
4210 | registry->major, registry->minor); | |
4211 | if (ret) { | |
4212 | pthread_mutex_unlock(®istry->lock); | |
4213 | ERR("Failed to generate session metadata (err = %d)", | |
4214 | ret); | |
4215 | goto end; | |
4216 | } | |
4217 | cds_lfht_for_each_entry(registry->channels->ht, &iter_chan.iter, | |
4218 | chan, node.node) { | |
4219 | struct ust_registry_event *event; | |
4220 | struct lttng_ht_iter iter_event; | |
4221 | ||
4222 | ret = ust_metadata_channel_statedump(registry, chan); | |
4223 | if (ret) { | |
4224 | pthread_mutex_unlock(®istry->lock); | |
4225 | ERR("Failed to generate channel metadata " | |
4226 | "(err = %d)", ret); | |
4227 | goto end; | |
4228 | } | |
4229 | cds_lfht_for_each_entry(chan->ht->ht, &iter_event.iter, | |
4230 | event, node.node) { | |
4231 | ret = ust_metadata_event_statedump(registry, | |
4232 | chan, event); | |
4233 | if (ret) { | |
4234 | pthread_mutex_unlock(®istry->lock); | |
4235 | ERR("Failed to generate event metadata " | |
4236 | "(err = %d)", ret); | |
4237 | goto end; | |
4238 | } | |
4239 | } | |
4240 | } | |
4241 | pthread_mutex_unlock(®istry->lock); | |
4242 | } | |
4243 | ||
4244 | end: | |
4245 | rcu_read_unlock(); | |
4246 | return ret; | |
4247 | } | |
4248 | ||
4249 | /* | |
eded6438 | 4250 | * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library. |
93ec662e JD |
4251 | * |
4252 | * Ask the consumer to truncate the existing metadata file(s) and | |
4253 | * then regenerate the metadata. Live and per-pid sessions are not | |
4254 | * supported and return an error. | |
4255 | * | |
4256 | * Return 0 on success or else a LTTNG_ERR code. | |
4257 | */ | |
eded6438 | 4258 | int cmd_regenerate_metadata(struct ltt_session *session) |
93ec662e JD |
4259 | { |
4260 | int ret; | |
4261 | ||
4262 | assert(session); | |
4263 | ||
eded6438 | 4264 | ret = check_regenerate_metadata_support(session); |
93ec662e JD |
4265 | if (ret) { |
4266 | goto end; | |
4267 | } | |
4268 | ||
4269 | if (session->kernel_session) { | |
eded6438 | 4270 | ret = kernctl_session_regenerate_metadata( |
93ec662e JD |
4271 | session->kernel_session->fd); |
4272 | if (ret < 0) { | |
4273 | ERR("Failed to regenerate the kernel metadata"); | |
4274 | goto end; | |
4275 | } | |
4276 | } | |
4277 | ||
4278 | if (session->ust_session) { | |
eded6438 | 4279 | ret = ust_regenerate_metadata(session->ust_session); |
93ec662e JD |
4280 | if (ret < 0) { |
4281 | ERR("Failed to regenerate the UST metadata"); | |
4282 | goto end; | |
4283 | } | |
4284 | } | |
4285 | DBG("Cmd metadata regenerate for session %s", session->name); | |
4286 | ret = LTTNG_OK; | |
4287 | ||
4288 | end: | |
4289 | return ret; | |
4290 | } | |
4291 | ||
c2561365 JD |
4292 | /* |
4293 | * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library. | |
4294 | * | |
4295 | * Ask the tracer to regenerate a new statedump. | |
4296 | * | |
4297 | * Return 0 on success or else a LTTNG_ERR code. | |
4298 | */ | |
4299 | int cmd_regenerate_statedump(struct ltt_session *session) | |
4300 | { | |
4301 | int ret; | |
4302 | ||
4303 | assert(session); | |
4304 | ||
4305 | if (!session->active) { | |
4306 | ret = LTTNG_ERR_SESSION_NOT_STARTED; | |
4307 | goto end; | |
4308 | } | |
c2561365 JD |
4309 | |
4310 | if (session->kernel_session) { | |
4311 | ret = kernctl_session_regenerate_statedump( | |
4312 | session->kernel_session->fd); | |
4313 | /* | |
4314 | * Currently, the statedump in kernel can only fail if out | |
4315 | * of memory. | |
4316 | */ | |
4317 | if (ret < 0) { | |
4318 | if (ret == -ENOMEM) { | |
4319 | ret = LTTNG_ERR_REGEN_STATEDUMP_NOMEM; | |
4320 | } else { | |
4321 | ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL; | |
4322 | } | |
4323 | ERR("Failed to regenerate the kernel statedump"); | |
4324 | goto end; | |
4325 | } | |
4326 | } | |
4327 | ||
4328 | if (session->ust_session) { | |
4329 | ret = ust_app_regenerate_statedump_all(session->ust_session); | |
4330 | /* | |
4331 | * Currently, the statedump in UST always returns 0. | |
4332 | */ | |
4333 | if (ret < 0) { | |
4334 | ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL; | |
4335 | ERR("Failed to regenerate the UST statedump"); | |
4336 | goto end; | |
4337 | } | |
4338 | } | |
4339 | DBG("Cmd regenerate statedump for session %s", session->name); | |
4340 | ret = LTTNG_OK; | |
4341 | ||
4342 | end: | |
4343 | return ret; | |
4344 | } | |
4345 | ||
b0880ae5 JG |
4346 | int cmd_register_trigger(struct command_ctx *cmd_ctx, int sock, |
4347 | struct notification_thread_handle *notification_thread) | |
4348 | { | |
4349 | int ret; | |
4350 | size_t trigger_len; | |
4351 | ssize_t sock_recv_len; | |
4352 | struct lttng_trigger *trigger = NULL; | |
4353 | struct lttng_buffer_view view; | |
4354 | struct lttng_dynamic_buffer trigger_buffer; | |
4355 | ||
4356 | lttng_dynamic_buffer_init(&trigger_buffer); | |
4357 | trigger_len = (size_t) cmd_ctx->lsm->u.trigger.length; | |
4358 | ret = lttng_dynamic_buffer_set_size(&trigger_buffer, trigger_len); | |
4359 | if (ret) { | |
4360 | ret = LTTNG_ERR_NOMEM; | |
4361 | goto end; | |
4362 | } | |
4363 | ||
4364 | sock_recv_len = lttcomm_recv_unix_sock(sock, trigger_buffer.data, | |
4365 | trigger_len); | |
4366 | if (sock_recv_len < 0 || sock_recv_len != trigger_len) { | |
4367 | ERR("Failed to receive \"register trigger\" command payload"); | |
4368 | /* TODO: should this be a new error enum ? */ | |
4369 | ret = LTTNG_ERR_INVALID_TRIGGER; | |
4370 | goto end; | |
4371 | } | |
4372 | ||
4373 | view = lttng_buffer_view_from_dynamic_buffer(&trigger_buffer, 0, -1); | |
4374 | if (lttng_trigger_create_from_buffer(&view, &trigger) != | |
4375 | trigger_len) { | |
4376 | ERR("Invalid trigger payload received in \"register trigger\" command"); | |
4377 | ret = LTTNG_ERR_INVALID_TRIGGER; | |
4378 | goto end; | |
4379 | } | |
4380 | ||
4381 | ret = notification_thread_command_register_trigger(notification_thread, | |
4382 | trigger); | |
587f9fd0 JG |
4383 | /* Ownership of trigger was transferred. */ |
4384 | trigger = NULL; | |
b0880ae5 | 4385 | end: |
587f9fd0 | 4386 | lttng_trigger_destroy(trigger); |
b0880ae5 JG |
4387 | lttng_dynamic_buffer_reset(&trigger_buffer); |
4388 | return ret; | |
4389 | } | |
4390 | ||
4391 | int cmd_unregister_trigger(struct command_ctx *cmd_ctx, int sock, | |
4392 | struct notification_thread_handle *notification_thread) | |
4393 | { | |
4394 | int ret; | |
4395 | size_t trigger_len; | |
4396 | ssize_t sock_recv_len; | |
4397 | struct lttng_trigger *trigger = NULL; | |
4398 | struct lttng_buffer_view view; | |
4399 | struct lttng_dynamic_buffer trigger_buffer; | |
4400 | ||
4401 | lttng_dynamic_buffer_init(&trigger_buffer); | |
4402 | trigger_len = (size_t) cmd_ctx->lsm->u.trigger.length; | |
4403 | ret = lttng_dynamic_buffer_set_size(&trigger_buffer, trigger_len); | |
4404 | if (ret) { | |
4405 | ret = LTTNG_ERR_NOMEM; | |
4406 | goto end; | |
4407 | } | |
4408 | ||
4409 | sock_recv_len = lttcomm_recv_unix_sock(sock, trigger_buffer.data, | |
4410 | trigger_len); | |
4411 | if (sock_recv_len < 0 || sock_recv_len != trigger_len) { | |
4412 | ERR("Failed to receive \"unregister trigger\" command payload"); | |
4413 | /* TODO: should this be a new error enum ? */ | |
4414 | ret = LTTNG_ERR_INVALID_TRIGGER; | |
4415 | goto end; | |
4416 | } | |
4417 | ||
4418 | view = lttng_buffer_view_from_dynamic_buffer(&trigger_buffer, 0, -1); | |
4419 | if (lttng_trigger_create_from_buffer(&view, &trigger) != | |
4420 | trigger_len) { | |
4421 | ERR("Invalid trigger payload received in \"unregister trigger\" command"); | |
4422 | ret = LTTNG_ERR_INVALID_TRIGGER; | |
4423 | goto end; | |
4424 | } | |
4425 | ||
4426 | ret = notification_thread_command_unregister_trigger(notification_thread, | |
4427 | trigger); | |
4428 | end: | |
587f9fd0 | 4429 | lttng_trigger_destroy(trigger); |
b0880ae5 JG |
4430 | lttng_dynamic_buffer_reset(&trigger_buffer); |
4431 | return ret; | |
4432 | } | |
4433 | ||
6dc3064a DG |
4434 | /* |
4435 | * Send relayd sockets from snapshot output to consumer. Ignore request if the | |
4436 | * snapshot output is *not* set with a remote destination. | |
4437 | * | |
9a654598 | 4438 | * Return LTTNG_OK on success or a LTTNG_ERR code. |
6dc3064a | 4439 | */ |
9a654598 | 4440 | static enum lttng_error_code set_relayd_for_snapshot( |
348a81dc | 4441 | struct consumer_output *output, |
fb9a95c4 | 4442 | const struct ltt_session *session) |
6dc3064a | 4443 | { |
9a654598 | 4444 | enum lttng_error_code status = LTTNG_OK; |
6dc3064a DG |
4445 | struct lttng_ht_iter iter; |
4446 | struct consumer_socket *socket; | |
1e791a74 | 4447 | LTTNG_OPTIONAL(uint64_t) current_chunk_id = {}; |
6fa5fe7c | 4448 | const char *base_path; |
6dc3064a | 4449 | |
348a81dc | 4450 | assert(output); |
6dc3064a DG |
4451 | assert(session); |
4452 | ||
4453 | DBG2("Set relayd object from snapshot output"); | |
4454 | ||
1e791a74 | 4455 | if (session->current_trace_chunk) { |
348a81dc JG |
4456 | enum lttng_trace_chunk_status chunk_status = |
4457 | lttng_trace_chunk_get_id( | |
4458 | session->current_trace_chunk, | |
4459 | ¤t_chunk_id.value); | |
1e791a74 | 4460 | |
348a81dc | 4461 | if (chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK) { |
1e791a74 JG |
4462 | current_chunk_id.is_set = true; |
4463 | } else { | |
4464 | ERR("Failed to get current trace chunk id"); | |
4465 | status = LTTNG_ERR_UNK; | |
4466 | goto error; | |
4467 | } | |
4468 | } | |
4469 | ||
6dc3064a | 4470 | /* Ignore if snapshot consumer output is not network. */ |
348a81dc | 4471 | if (output->type != CONSUMER_DST_NET) { |
6dc3064a DG |
4472 | goto error; |
4473 | } | |
4474 | ||
6fa5fe7c MD |
4475 | /* |
4476 | * The snapshot record URI base path overrides the session | |
4477 | * base path. | |
4478 | */ | |
4479 | if (output->dst.net.control.subdir[0] != '\0') { | |
4480 | base_path = output->dst.net.control.subdir; | |
4481 | } else { | |
4482 | base_path = session->base_path; | |
4483 | } | |
4484 | ||
6dc3064a DG |
4485 | /* |
4486 | * For each consumer socket, create and send the relayd object of the | |
4487 | * snapshot output. | |
4488 | */ | |
4489 | rcu_read_lock(); | |
348a81dc | 4490 | cds_lfht_for_each_entry(output->socks->ht, &iter.iter, |
5eecee74 | 4491 | socket, node.node) { |
ecd0f96d | 4492 | pthread_mutex_lock(socket->lock); |
9a654598 | 4493 | status = send_consumer_relayd_sockets(0, session->id, |
348a81dc | 4494 | output, socket, |
d3e2ba59 | 4495 | session->name, session->hostname, |
6fa5fe7c | 4496 | base_path, |
1e791a74 | 4497 | session->live_timer, |
db1da059 | 4498 | current_chunk_id.is_set ? ¤t_chunk_id.value : NULL, |
46ef2188 MD |
4499 | session->creation_time, |
4500 | session->name_contains_creation_time); | |
ecd0f96d | 4501 | pthread_mutex_unlock(socket->lock); |
9a654598 | 4502 | if (status != LTTNG_OK) { |
6dc3064a DG |
4503 | rcu_read_unlock(); |
4504 | goto error; | |
4505 | } | |
4506 | } | |
4507 | rcu_read_unlock(); | |
4508 | ||
4509 | error: | |
9a654598 | 4510 | return status; |
6dc3064a DG |
4511 | } |
4512 | ||
4513 | /* | |
4514 | * Record a kernel snapshot. | |
4515 | * | |
fac41e72 | 4516 | * Return LTTNG_OK on success or a LTTNG_ERR code. |
6dc3064a | 4517 | */ |
fb9a95c4 JG |
4518 | static enum lttng_error_code record_kernel_snapshot( |
4519 | struct ltt_kernel_session *ksess, | |
348a81dc | 4520 | const struct consumer_output *output, |
fb9a95c4 | 4521 | const struct ltt_session *session, |
d07ceecd | 4522 | int wait, uint64_t nb_packets_per_stream) |
6dc3064a | 4523 | { |
9a654598 | 4524 | enum lttng_error_code status; |
6dc3064a DG |
4525 | |
4526 | assert(ksess); | |
4527 | assert(output); | |
4528 | assert(session); | |
4529 | ||
348a81dc JG |
4530 | status = kernel_snapshot_record( |
4531 | ksess, output, wait, nb_packets_per_stream); | |
9a654598 | 4532 | return status; |
6dc3064a DG |
4533 | } |
4534 | ||
4535 | /* | |
4536 | * Record a UST snapshot. | |
4537 | * | |
9a654598 | 4538 | * Returns LTTNG_OK on success or a LTTNG_ERR error code. |
6dc3064a | 4539 | */ |
9a654598 | 4540 | static enum lttng_error_code record_ust_snapshot(struct ltt_ust_session *usess, |
348a81dc JG |
4541 | const struct consumer_output *output, |
4542 | const struct ltt_session *session, | |
4543 | int wait, uint64_t nb_packets_per_stream) | |
6dc3064a | 4544 | { |
9a654598 | 4545 | enum lttng_error_code status; |
6dc3064a DG |
4546 | |
4547 | assert(usess); | |
4548 | assert(output); | |
4549 | assert(session); | |
4550 | ||
348a81dc JG |
4551 | status = ust_app_snapshot_record( |
4552 | usess, output, wait, nb_packets_per_stream); | |
9a654598 | 4553 | return status; |
6dc3064a DG |
4554 | } |
4555 | ||
d07ceecd | 4556 | static |
fb9a95c4 JG |
4557 | uint64_t get_session_size_one_more_packet_per_stream( |
4558 | const struct ltt_session *session, uint64_t cur_nr_packets) | |
68808f4e | 4559 | { |
d07ceecd | 4560 | uint64_t tot_size = 0; |
68808f4e DG |
4561 | |
4562 | if (session->kernel_session) { | |
4563 | struct ltt_kernel_channel *chan; | |
fb9a95c4 JG |
4564 | const struct ltt_kernel_session *ksess = |
4565 | session->kernel_session; | |
68808f4e | 4566 | |
68808f4e | 4567 | cds_list_for_each_entry(chan, &ksess->channel_list.head, list) { |
d07ceecd MD |
4568 | if (cur_nr_packets >= chan->channel->attr.num_subbuf) { |
4569 | /* | |
4570 | * Don't take channel into account if we | |
4571 | * already grab all its packets. | |
4572 | */ | |
4573 | continue; | |
68808f4e | 4574 | } |
d07ceecd MD |
4575 | tot_size += chan->channel->attr.subbuf_size |
4576 | * chan->stream_count; | |
68808f4e DG |
4577 | } |
4578 | } | |
4579 | ||
4580 | if (session->ust_session) { | |
fb9a95c4 | 4581 | const struct ltt_ust_session *usess = session->ust_session; |
68808f4e | 4582 | |
d07ceecd MD |
4583 | tot_size += ust_app_get_size_one_more_packet_per_stream(usess, |
4584 | cur_nr_packets); | |
68808f4e DG |
4585 | } |
4586 | ||
d07ceecd | 4587 | return tot_size; |
68808f4e DG |
4588 | } |
4589 | ||
5c786ded | 4590 | /* |
d07ceecd MD |
4591 | * Calculate the number of packets we can grab from each stream that |
4592 | * fits within the overall snapshot max size. | |
4593 | * | |
4594 | * Returns -1 on error, 0 means infinite number of packets, else > 0 is | |
4595 | * the number of packets per stream. | |
4596 | * | |
4597 | * TODO: this approach is not perfect: we consider the worse case | |
4598 | * (packet filling the sub-buffers) as an upper bound, but we could do | |
4599 | * better if we do this calculation while we actually grab the packet | |
4600 | * content: we would know how much padding we don't actually store into | |
4601 | * the file. | |
4602 | * | |
4603 | * This algorithm is currently bounded by the number of packets per | |
4604 | * stream. | |
4605 | * | |
4606 | * Since we call this algorithm before actually grabbing the data, it's | |
4607 | * an approximation: for instance, applications could appear/disappear | |
4608 | * in between this call and actually grabbing data. | |
5c786ded | 4609 | */ |
d07ceecd | 4610 | static |
fb9a95c4 JG |
4611 | int64_t get_session_nb_packets_per_stream(const struct ltt_session *session, |
4612 | uint64_t max_size) | |
5c786ded | 4613 | { |
d07ceecd MD |
4614 | int64_t size_left; |
4615 | uint64_t cur_nb_packets = 0; | |
5c786ded | 4616 | |
d07ceecd MD |
4617 | if (!max_size) { |
4618 | return 0; /* Infinite */ | |
5c786ded JD |
4619 | } |
4620 | ||
d07ceecd MD |
4621 | size_left = max_size; |
4622 | for (;;) { | |
4623 | uint64_t one_more_packet_tot_size; | |
5c786ded | 4624 | |
fb9a95c4 JG |
4625 | one_more_packet_tot_size = get_session_size_one_more_packet_per_stream( |
4626 | session, cur_nb_packets); | |
d07ceecd MD |
4627 | if (!one_more_packet_tot_size) { |
4628 | /* We are already grabbing all packets. */ | |
4629 | break; | |
4630 | } | |
4631 | size_left -= one_more_packet_tot_size; | |
4632 | if (size_left < 0) { | |
4633 | break; | |
4634 | } | |
4635 | cur_nb_packets++; | |
5c786ded | 4636 | } |
aecf2da5 | 4637 | if (!cur_nb_packets && size_left != max_size) { |
d07ceecd MD |
4638 | /* Not enough room to grab one packet of each stream, error. */ |
4639 | return -1; | |
4640 | } | |
4641 | return cur_nb_packets; | |
5c786ded JD |
4642 | } |
4643 | ||
fb9a95c4 | 4644 | static |
d2956687 | 4645 | enum lttng_error_code snapshot_record(struct ltt_session *session, |
fb9a95c4 JG |
4646 | const struct snapshot_output *snapshot_output, int wait) |
4647 | { | |
4648 | int64_t nb_packets_per_stream; | |
d2956687 | 4649 | char snapshot_chunk_name[LTTNG_NAME_MAX]; |
348a81dc JG |
4650 | int ret; |
4651 | enum lttng_error_code ret_code = LTTNG_OK; | |
d2956687 | 4652 | struct lttng_trace_chunk *snapshot_trace_chunk; |
348a81dc JG |
4653 | struct consumer_output *original_ust_consumer_output = NULL; |
4654 | struct consumer_output *original_kernel_consumer_output = NULL; | |
4655 | struct consumer_output *snapshot_ust_consumer_output = NULL; | |
4656 | struct consumer_output *snapshot_kernel_consumer_output = NULL; | |
d2956687 | 4657 | |
348a81dc | 4658 | ret = snprintf(snapshot_chunk_name, sizeof(snapshot_chunk_name), |
d2956687 JG |
4659 | "%s-%s-%" PRIu64, |
4660 | snapshot_output->name, | |
4661 | snapshot_output->datetime, | |
4662 | snapshot_output->nb_snapshot); | |
348a81dc | 4663 | if (ret < 0 || ret >= sizeof(snapshot_chunk_name)) { |
d2956687 | 4664 | ERR("Failed to format snapshot name"); |
348a81dc JG |
4665 | ret_code = LTTNG_ERR_INVALID; |
4666 | goto error; | |
d2956687 JG |
4667 | } |
4668 | DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"", | |
4669 | snapshot_output->name, session->name, | |
4670 | snapshot_chunk_name); | |
348a81dc JG |
4671 | if (!session->kernel_session && !session->ust_session) { |
4672 | ERR("Failed to record snapshot as no channels exist"); | |
4673 | ret_code = LTTNG_ERR_NO_CHANNEL; | |
4674 | goto error; | |
4675 | } | |
4676 | ||
4677 | if (session->kernel_session) { | |
4678 | original_kernel_consumer_output = | |
4679 | session->kernel_session->consumer; | |
4680 | snapshot_kernel_consumer_output = | |
4681 | consumer_copy_output(snapshot_output->consumer); | |
3b967712 MD |
4682 | strcpy(snapshot_kernel_consumer_output->chunk_path, |
4683 | snapshot_chunk_name); | |
348a81dc JG |
4684 | ret = consumer_copy_sockets(snapshot_kernel_consumer_output, |
4685 | original_kernel_consumer_output); | |
4686 | if (ret < 0) { | |
4687 | ERR("Failed to copy consumer sockets from snapshot output configuration"); | |
4688 | ret_code = LTTNG_ERR_NOMEM; | |
4689 | goto error; | |
4690 | } | |
4691 | ret_code = set_relayd_for_snapshot( | |
4692 | snapshot_kernel_consumer_output, session); | |
4693 | if (ret_code != LTTNG_OK) { | |
4694 | ERR("Failed to setup relay daemon for kernel tracer snapshot"); | |
4695 | goto error; | |
4696 | } | |
4697 | session->kernel_session->consumer = | |
4698 | snapshot_kernel_consumer_output; | |
4699 | } | |
4700 | if (session->ust_session) { | |
4701 | original_ust_consumer_output = session->ust_session->consumer; | |
4702 | snapshot_ust_consumer_output = | |
4703 | consumer_copy_output(snapshot_output->consumer); | |
3b967712 MD |
4704 | strcpy(snapshot_ust_consumer_output->chunk_path, |
4705 | snapshot_chunk_name); | |
348a81dc JG |
4706 | ret = consumer_copy_sockets(snapshot_ust_consumer_output, |
4707 | original_ust_consumer_output); | |
4708 | if (ret < 0) { | |
4709 | ERR("Failed to copy consumer sockets from snapshot output configuration"); | |
4710 | ret_code = LTTNG_ERR_NOMEM; | |
4711 | goto error; | |
4712 | } | |
4713 | ret_code = set_relayd_for_snapshot( | |
4714 | snapshot_ust_consumer_output, session); | |
4715 | if (ret_code != LTTNG_OK) { | |
4716 | ERR("Failed to setup relay daemon for userspace tracer snapshot"); | |
4717 | goto error; | |
4718 | } | |
4719 | session->ust_session->consumer = | |
4720 | snapshot_ust_consumer_output; | |
4721 | } | |
4722 | ||
d2956687 | 4723 | snapshot_trace_chunk = session_create_new_trace_chunk(session, |
348a81dc JG |
4724 | snapshot_kernel_consumer_output ?: |
4725 | snapshot_ust_consumer_output, | |
4726 | consumer_output_get_base_path( | |
4727 | snapshot_output->consumer), | |
d2956687 JG |
4728 | snapshot_chunk_name); |
4729 | if (!snapshot_trace_chunk) { | |
348a81dc JG |
4730 | ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"", |
4731 | session->name); | |
4732 | ret_code = LTTNG_ERR_CREATE_DIR_FAIL; | |
4733 | goto error; | |
d2956687 JG |
4734 | } |
4735 | assert(!session->current_trace_chunk); | |
4736 | ret = session_set_trace_chunk(session, snapshot_trace_chunk, NULL); | |
4737 | lttng_trace_chunk_put(snapshot_trace_chunk); | |
4738 | snapshot_trace_chunk = NULL; | |
4739 | if (ret) { | |
348a81dc JG |
4740 | ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"", |
4741 | session->name); | |
4742 | ret_code = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER; | |
4743 | goto error; | |
d2956687 | 4744 | } |
fb9a95c4 JG |
4745 | |
4746 | nb_packets_per_stream = get_session_nb_packets_per_stream(session, | |
4747 | snapshot_output->max_size); | |
4748 | if (nb_packets_per_stream < 0) { | |
348a81dc | 4749 | ret_code = LTTNG_ERR_MAX_SIZE_INVALID; |
5151d412 | 4750 | goto error_close_trace_chunk; |
fb9a95c4 JG |
4751 | } |
4752 | ||
4753 | if (session->kernel_session) { | |
348a81dc JG |
4754 | ret_code = record_kernel_snapshot(session->kernel_session, |
4755 | snapshot_kernel_consumer_output, session, | |
fb9a95c4 | 4756 | wait, nb_packets_per_stream); |
348a81dc | 4757 | if (ret_code != LTTNG_OK) { |
5151d412 | 4758 | goto error_close_trace_chunk; |
fb9a95c4 JG |
4759 | } |
4760 | } | |
4761 | ||
4762 | if (session->ust_session) { | |
348a81dc JG |
4763 | ret_code = record_ust_snapshot(session->ust_session, |
4764 | snapshot_ust_consumer_output, session, | |
fb9a95c4 | 4765 | wait, nb_packets_per_stream); |
348a81dc | 4766 | if (ret_code != LTTNG_OK) { |
5151d412 | 4767 | goto error_close_trace_chunk; |
fb9a95c4 JG |
4768 | } |
4769 | } | |
d2956687 | 4770 | |
5151d412 | 4771 | error_close_trace_chunk: |
dbfee52c MD |
4772 | if (session_set_trace_chunk(session, NULL, &snapshot_trace_chunk)) { |
4773 | ERR("Failed to release the current trace chunk of session \"%s\"", | |
4774 | session->name); | |
4775 | ret_code = LTTNG_ERR_UNK; | |
4776 | } | |
4777 | ||
4778 | if (session_close_trace_chunk(session, snapshot_trace_chunk, | |
343defc2 | 4779 | LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION, NULL)) { |
d2956687 JG |
4780 | /* |
4781 | * Don't goto end; make sure the chunk is closed for the session | |
4782 | * to allow future snapshots. | |
4783 | */ | |
4784 | ERR("Failed to close snapshot trace chunk of session \"%s\"", | |
4785 | session->name); | |
348a81dc | 4786 | ret_code = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER; |
d2956687 | 4787 | } |
348a81dc JG |
4788 | error: |
4789 | if (original_ust_consumer_output) { | |
4790 | session->ust_session->consumer = original_ust_consumer_output; | |
4791 | } | |
4792 | if (original_kernel_consumer_output) { | |
4793 | session->kernel_session->consumer = | |
4794 | original_kernel_consumer_output; | |
4795 | } | |
4796 | consumer_output_put(snapshot_ust_consumer_output); | |
4797 | consumer_output_put(snapshot_kernel_consumer_output); | |
4798 | return ret_code; | |
fb9a95c4 JG |
4799 | } |
4800 | ||
6dc3064a DG |
4801 | /* |
4802 | * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl. | |
4803 | * | |
4804 | * The wait parameter is ignored so this call always wait for the snapshot to | |
4805 | * complete before returning. | |
4806 | * | |
4807 | * Return LTTNG_OK on success or else a LTTNG_ERR code. | |
4808 | */ | |
4809 | int cmd_snapshot_record(struct ltt_session *session, | |
df4f5a87 | 4810 | const struct lttng_snapshot_output *output, int wait) |
6dc3064a | 4811 | { |
9a654598 JG |
4812 | enum lttng_error_code cmd_ret = LTTNG_OK; |
4813 | int ret; | |
00e1dfc4 | 4814 | unsigned int snapshot_success = 0; |
10ba83fe | 4815 | char datetime[16]; |
2abe7969 | 4816 | struct snapshot_output *tmp_output = NULL; |
6dc3064a DG |
4817 | |
4818 | assert(session); | |
ba45d9f0 | 4819 | assert(output); |
6dc3064a DG |
4820 | |
4821 | DBG("Cmd snapshot record for session %s", session->name); | |
4822 | ||
10ba83fe JR |
4823 | /* Get the datetime for the snapshot output directory. */ |
4824 | ret = utils_get_current_time_str("%Y%m%d-%H%M%S", datetime, | |
4825 | sizeof(datetime)); | |
4826 | if (!ret) { | |
9a654598 | 4827 | cmd_ret = LTTNG_ERR_INVALID; |
10ba83fe JR |
4828 | goto error; |
4829 | } | |
4830 | ||
6dc3064a | 4831 | /* |
d3f14b8a MD |
4832 | * Permission denied to create an output if the session is not |
4833 | * set in no output mode. | |
6dc3064a DG |
4834 | */ |
4835 | if (session->output_traces) { | |
9a654598 | 4836 | cmd_ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
6dc3064a DG |
4837 | goto error; |
4838 | } | |
4839 | ||
4840 | /* The session needs to be started at least once. */ | |
8382cf6f | 4841 | if (!session->has_been_started) { |
9a654598 | 4842 | cmd_ret = LTTNG_ERR_START_SESSION_ONCE; |
6dc3064a DG |
4843 | goto error; |
4844 | } | |
4845 | ||
4846 | /* Use temporary output for the session. */ | |
ba45d9f0 | 4847 | if (*output->ctrl_url != '\0') { |
2abe7969 JG |
4848 | tmp_output = snapshot_output_alloc(); |
4849 | if (!tmp_output) { | |
4850 | cmd_ret = LTTNG_ERR_NOMEM; | |
4851 | goto error; | |
4852 | } | |
4853 | ||
b178f53e JG |
4854 | ret = snapshot_output_init(session, output->max_size, |
4855 | output->name, | |
4856 | output->ctrl_url, output->data_url, | |
4857 | session->consumer, | |
2abe7969 | 4858 | tmp_output, NULL); |
6dc3064a DG |
4859 | if (ret < 0) { |
4860 | if (ret == -ENOMEM) { | |
9a654598 | 4861 | cmd_ret = LTTNG_ERR_NOMEM; |
6dc3064a | 4862 | } else { |
9a654598 | 4863 | cmd_ret = LTTNG_ERR_INVALID; |
6dc3064a DG |
4864 | } |
4865 | goto error; | |
4866 | } | |
1bfe7328 | 4867 | /* Use the global session count for the temporary snapshot. */ |
2abe7969 | 4868 | tmp_output->nb_snapshot = session->snapshot.nb_snapshot; |
10ba83fe JR |
4869 | |
4870 | /* Use the global datetime */ | |
2abe7969 JG |
4871 | memcpy(tmp_output->datetime, datetime, sizeof(datetime)); |
4872 | cmd_ret = snapshot_record(session, tmp_output, wait); | |
fb9a95c4 | 4873 | if (cmd_ret != LTTNG_OK) { |
804c90a8 JR |
4874 | goto error; |
4875 | } | |
804c90a8 JR |
4876 | snapshot_success = 1; |
4877 | } else { | |
4878 | struct snapshot_output *sout; | |
4879 | struct lttng_ht_iter iter; | |
68808f4e | 4880 | |
804c90a8 JR |
4881 | rcu_read_lock(); |
4882 | cds_lfht_for_each_entry(session->snapshot.output_ht->ht, | |
4883 | &iter.iter, sout, node.node) { | |
2abe7969 JG |
4884 | struct snapshot_output output_copy; |
4885 | ||
804c90a8 | 4886 | /* |
2abe7969 JG |
4887 | * Make a local copy of the output and override output |
4888 | * parameters with those provided as part of the | |
4889 | * command. | |
804c90a8 | 4890 | */ |
2abe7969 | 4891 | memcpy(&output_copy, sout, sizeof(output_copy)); |
1bfe7328 | 4892 | |
804c90a8 | 4893 | if (output->max_size != (uint64_t) -1ULL) { |
2abe7969 | 4894 | output_copy.max_size = output->max_size; |
6dc3064a | 4895 | } |
d07ceecd | 4896 | |
2abe7969 JG |
4897 | output_copy.nb_snapshot = session->snapshot.nb_snapshot; |
4898 | memcpy(output_copy.datetime, datetime, | |
4899 | sizeof(datetime)); | |
6dc3064a | 4900 | |
804c90a8 JR |
4901 | /* Use temporary name. */ |
4902 | if (*output->name != '\0') { | |
2abe7969 JG |
4903 | if (lttng_strncpy(output_copy.name, |
4904 | output->name, | |
4905 | sizeof(output_copy.name))) { | |
9a654598 | 4906 | cmd_ret = LTTNG_ERR_INVALID; |
cf3e357d MD |
4907 | rcu_read_unlock(); |
4908 | goto error; | |
4909 | } | |
804c90a8 | 4910 | } |
e1986656 | 4911 | |
2abe7969 | 4912 | cmd_ret = snapshot_record(session, &output_copy, wait); |
fb9a95c4 JG |
4913 | if (cmd_ret != LTTNG_OK) { |
4914 | rcu_read_unlock(); | |
4915 | goto error; | |
6dc3064a | 4916 | } |
804c90a8 | 4917 | snapshot_success = 1; |
6dc3064a | 4918 | } |
804c90a8 | 4919 | rcu_read_unlock(); |
6dc3064a DG |
4920 | } |
4921 | ||
1bfe7328 DG |
4922 | if (snapshot_success) { |
4923 | session->snapshot.nb_snapshot++; | |
b67578cb | 4924 | } else { |
9a654598 | 4925 | cmd_ret = LTTNG_ERR_SNAPSHOT_FAIL; |
1bfe7328 DG |
4926 | } |
4927 | ||
6dc3064a | 4928 | error: |
2abe7969 JG |
4929 | if (tmp_output) { |
4930 | snapshot_output_destroy(tmp_output); | |
4931 | } | |
9a654598 | 4932 | return cmd_ret; |
6dc3064a DG |
4933 | } |
4934 | ||
d7ba1388 MD |
4935 | /* |
4936 | * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread. | |
4937 | */ | |
4938 | int cmd_set_session_shm_path(struct ltt_session *session, | |
4939 | const char *shm_path) | |
4940 | { | |
4941 | /* Safety net */ | |
4942 | assert(session); | |
4943 | ||
4944 | /* | |
4945 | * Can only set shm path before session is started. | |
4946 | */ | |
4947 | if (session->has_been_started) { | |
4948 | return LTTNG_ERR_SESSION_STARTED; | |
4949 | } | |
4950 | ||
4951 | strncpy(session->shm_path, shm_path, | |
4952 | sizeof(session->shm_path)); | |
4953 | session->shm_path[sizeof(session->shm_path) - 1] = '\0'; | |
4954 | ||
4955 | return 0; | |
4956 | } | |
4957 | ||
5c408ad8 JD |
4958 | /* |
4959 | * Command LTTNG_ROTATE_SESSION from the lttng-ctl library. | |
4960 | * | |
4961 | * Ask the consumer to rotate the session output directory. | |
4962 | * The session lock must be held. | |
4963 | * | |
d5a1b7aa | 4964 | * Returns LTTNG_OK on success or else a negative LTTng error code. |
5c408ad8 JD |
4965 | */ |
4966 | int cmd_rotate_session(struct ltt_session *session, | |
7fdbed1c | 4967 | struct lttng_rotate_session_return *rotate_return, |
343defc2 MD |
4968 | bool quiet_rotation, |
4969 | enum lttng_trace_chunk_command_type command) | |
5c408ad8 JD |
4970 | { |
4971 | int ret; | |
d2956687 | 4972 | uint64_t ongoing_rotation_chunk_id; |
d5a1b7aa | 4973 | enum lttng_error_code cmd_ret = LTTNG_OK; |
d2956687 JG |
4974 | struct lttng_trace_chunk *chunk_being_archived = NULL; |
4975 | struct lttng_trace_chunk *new_trace_chunk = NULL; | |
4976 | enum lttng_trace_chunk_status chunk_status; | |
3156892b JG |
4977 | bool failed_to_rotate = false; |
4978 | enum lttng_error_code rotation_fail_code = LTTNG_OK; | |
5c408ad8 JD |
4979 | |
4980 | assert(session); | |
4981 | ||
4982 | if (!session->has_been_started) { | |
d5a1b7aa | 4983 | cmd_ret = LTTNG_ERR_START_SESSION_ONCE; |
d68c9a04 | 4984 | goto end; |
5c408ad8 JD |
4985 | } |
4986 | ||
d48d65e1 MD |
4987 | /* |
4988 | * Explicit rotation is not supported for live sessions. | |
4989 | * However, live sessions can perform a quiet rotation on | |
4990 | * destroy. | |
4991 | * Rotation is not supported for snapshot traces (no output). | |
4992 | */ | |
4993 | if ((!quiet_rotation && session->live_timer) || | |
4994 | !session->output_traces) { | |
d5a1b7aa | 4995 | cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE; |
d68c9a04 | 4996 | goto end; |
5c408ad8 JD |
4997 | } |
4998 | ||
d2956687 | 4999 | /* Unsupported feature in lttng-relayd before 2.11. */ |
070b6a86 | 5000 | if (!quiet_rotation && session->consumer->type == CONSUMER_DST_NET && |
5c408ad8 JD |
5001 | (session->consumer->relay_major_version == 2 && |
5002 | session->consumer->relay_minor_version < 11)) { | |
d5a1b7aa | 5003 | cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY; |
d68c9a04 | 5004 | goto end; |
5c408ad8 JD |
5005 | } |
5006 | ||
a40a503f MD |
5007 | /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */ |
5008 | if (session->kernel_session && !kernel_supports_ring_buffer_packet_sequence_number()) { | |
5009 | cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL; | |
5010 | goto end; | |
5011 | } | |
5012 | ||
92816cc3 | 5013 | if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) { |
92816cc3 JG |
5014 | DBG("Refusing to launch a rotation; a rotation is already in progress for session %s", |
5015 | session->name); | |
d5a1b7aa | 5016 | cmd_ret = LTTNG_ERR_ROTATION_PENDING; |
d68c9a04 | 5017 | goto end; |
5c408ad8 JD |
5018 | } |
5019 | ||
5020 | /* | |
5021 | * After a stop, we only allow one rotation to occur, the other ones are | |
5022 | * useless until a new start. | |
5023 | */ | |
5024 | if (session->rotated_after_last_stop) { | |
5025 | DBG("Session \"%s\" was already rotated after stop, refusing rotation", | |
5026 | session->name); | |
d5a1b7aa | 5027 | cmd_ret = LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP; |
d68c9a04 | 5028 | goto end; |
5c408ad8 | 5029 | } |
b02f5986 MD |
5030 | |
5031 | /* | |
5032 | * After a stop followed by a clear, disallow following rotations a they would | |
5033 | * generate empty chunks. | |
5034 | */ | |
5035 | if (session->cleared_after_last_stop) { | |
5036 | DBG("Session \"%s\" was already cleared after stop, refusing rotation", | |
5037 | session->name); | |
5038 | cmd_ret = LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR; | |
5039 | goto end; | |
5040 | } | |
5041 | ||
d2956687 | 5042 | if (session->active) { |
348a81dc | 5043 | new_trace_chunk = session_create_new_trace_chunk(session, NULL, |
d2956687 JG |
5044 | NULL, NULL); |
5045 | if (!new_trace_chunk) { | |
5046 | cmd_ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
5047 | goto error; | |
5c408ad8 | 5048 | } |
d2956687 | 5049 | } |
2961f09e | 5050 | |
3156892b JG |
5051 | /* |
5052 | * The current trace chunk becomes the chunk being archived. | |
5053 | * | |
5054 | * After this point, "chunk_being_archived" must absolutely | |
5055 | * be closed on the consumer(s), otherwise it will never be | |
5056 | * cleaned-up, which will result in a leak. | |
5057 | */ | |
d2956687 JG |
5058 | ret = session_set_trace_chunk(session, new_trace_chunk, |
5059 | &chunk_being_archived); | |
5060 | if (ret) { | |
5061 | cmd_ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER; | |
b178f53e JG |
5062 | goto error; |
5063 | } | |
5064 | ||
5c408ad8 | 5065 | if (session->kernel_session) { |
d5a1b7aa JG |
5066 | cmd_ret = kernel_rotate_session(session); |
5067 | if (cmd_ret != LTTNG_OK) { | |
3156892b JG |
5068 | failed_to_rotate = true; |
5069 | rotation_fail_code = cmd_ret; | |
5c408ad8 JD |
5070 | } |
5071 | } | |
5072 | if (session->ust_session) { | |
d5a1b7aa JG |
5073 | cmd_ret = ust_app_rotate_session(session); |
5074 | if (cmd_ret != LTTNG_OK) { | |
3156892b JG |
5075 | failed_to_rotate = true; |
5076 | rotation_fail_code = cmd_ret; | |
5c408ad8 | 5077 | } |
92816cc3 | 5078 | } |
17dd1232 | 5079 | |
3b61d9ee JG |
5080 | if (!session->active) { |
5081 | session->rotated_after_last_stop = true; | |
5082 | } | |
5083 | ||
5084 | if (!chunk_being_archived) { | |
5085 | DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check", | |
5086 | session->name); | |
5087 | if (failed_to_rotate) { | |
5088 | cmd_ret = rotation_fail_code; | |
5089 | goto error; | |
5090 | } | |
5091 | cmd_ret = LTTNG_OK; | |
5092 | goto end; | |
5093 | } | |
5094 | ||
5095 | session->rotation_state = LTTNG_ROTATION_STATE_ONGOING; | |
5096 | chunk_status = lttng_trace_chunk_get_id(chunk_being_archived, | |
5097 | &ongoing_rotation_chunk_id); | |
5098 | assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK); | |
5099 | ||
bbc4768c | 5100 | ret = session_close_trace_chunk(session, chunk_being_archived, |
343defc2 | 5101 | command, session->last_chunk_path); |
d2956687 JG |
5102 | if (ret) { |
5103 | cmd_ret = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER; | |
5104 | goto error; | |
5105 | } | |
5106 | ||
3156892b JG |
5107 | if (failed_to_rotate) { |
5108 | cmd_ret = rotation_fail_code; | |
5109 | goto error; | |
5110 | } | |
5111 | ||
7fdbed1c | 5112 | session->quiet_rotation = quiet_rotation; |
92816cc3 JG |
5113 | ret = timer_session_rotation_pending_check_start(session, |
5114 | DEFAULT_ROTATE_PENDING_TIMER); | |
5115 | if (ret) { | |
d5a1b7aa | 5116 | cmd_ret = LTTNG_ERR_UNK; |
2961f09e | 5117 | goto error; |
5c408ad8 JD |
5118 | } |
5119 | ||
5c408ad8 | 5120 | if (rotate_return) { |
d2956687 | 5121 | rotate_return->rotation_id = ongoing_rotation_chunk_id; |
5c408ad8 JD |
5122 | } |
5123 | ||
d2956687 JG |
5124 | session->chunk_being_archived = chunk_being_archived; |
5125 | chunk_being_archived = NULL; | |
7fdbed1c JG |
5126 | if (!quiet_rotation) { |
5127 | ret = notification_thread_command_session_rotation_ongoing( | |
5128 | notification_thread_handle, | |
5129 | session->name, session->uid, session->gid, | |
5130 | ongoing_rotation_chunk_id); | |
5131 | if (ret != LTTNG_OK) { | |
5132 | ERR("Failed to notify notification thread that a session rotation is ongoing for session %s", | |
5133 | session->name); | |
5134 | cmd_ret = ret; | |
5135 | } | |
2961f09e JG |
5136 | } |
5137 | ||
92816cc3 | 5138 | DBG("Cmd rotate session %s, archive_id %" PRIu64 " sent", |
d2956687 | 5139 | session->name, ongoing_rotation_chunk_id); |
5c408ad8 | 5140 | end: |
d2956687 JG |
5141 | lttng_trace_chunk_put(new_trace_chunk); |
5142 | lttng_trace_chunk_put(chunk_being_archived); | |
d5a1b7aa | 5143 | ret = (cmd_ret == LTTNG_OK) ? cmd_ret : -((int) cmd_ret); |
5c408ad8 | 5144 | return ret; |
2961f09e | 5145 | error: |
2961f09e | 5146 | if (session_reset_rotation_state(session, |
d2956687 | 5147 | LTTNG_ROTATION_STATE_ERROR)) { |
2961f09e JG |
5148 | ERR("Failed to reset rotation state of session \"%s\"", |
5149 | session->name); | |
5150 | } | |
5151 | goto end; | |
5c408ad8 JD |
5152 | } |
5153 | ||
5154 | /* | |
d68c9a04 | 5155 | * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library. |
5c408ad8 JD |
5156 | * |
5157 | * Check if the session has finished its rotation. | |
5158 | * | |
d2956687 | 5159 | * Return LTTNG_OK on success or else an LTTNG_ERR code. |
5c408ad8 | 5160 | */ |
d68c9a04 JD |
5161 | int cmd_rotate_get_info(struct ltt_session *session, |
5162 | struct lttng_rotation_get_info_return *info_return, | |
5163 | uint64_t rotation_id) | |
5c408ad8 | 5164 | { |
d2956687 JG |
5165 | enum lttng_error_code cmd_ret = LTTNG_OK; |
5166 | enum lttng_rotation_state rotation_state; | |
5c408ad8 | 5167 | |
d68c9a04 | 5168 | DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64, session->name, |
d2956687 | 5169 | session->most_recent_chunk_id.value); |
5c408ad8 | 5170 | |
d2956687 JG |
5171 | if (session->chunk_being_archived) { |
5172 | enum lttng_trace_chunk_status chunk_status; | |
5173 | uint64_t chunk_id; | |
5174 | ||
5175 | chunk_status = lttng_trace_chunk_get_id( | |
5176 | session->chunk_being_archived, | |
5177 | &chunk_id); | |
5178 | assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK); | |
5179 | ||
5180 | rotation_state = rotation_id == chunk_id ? | |
5181 | LTTNG_ROTATION_STATE_ONGOING : | |
5182 | LTTNG_ROTATION_STATE_EXPIRED; | |
5183 | } else { | |
5184 | if (session->last_archived_chunk_id.is_set && | |
5185 | rotation_id != session->last_archived_chunk_id.value) { | |
5186 | rotation_state = LTTNG_ROTATION_STATE_EXPIRED; | |
5187 | } else { | |
5188 | rotation_state = session->rotation_state; | |
5189 | } | |
5c408ad8 JD |
5190 | } |
5191 | ||
d2956687 JG |
5192 | switch (rotation_state) { |
5193 | case LTTNG_ROTATION_STATE_NO_ROTATION: | |
83ed9e90 | 5194 | DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"", |
d2956687 JG |
5195 | session->name); |
5196 | goto end; | |
5197 | case LTTNG_ROTATION_STATE_EXPIRED: | |
5198 | DBG("Reporting that the rotation state of rotation id %" PRIu64 " of session \"%s\" has expired", | |
5199 | rotation_id, session->name); | |
5200 | break; | |
d68c9a04 | 5201 | case LTTNG_ROTATION_STATE_ONGOING: |
d2956687 | 5202 | DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is still pending", |
d68c9a04 JD |
5203 | rotation_id, session->name); |
5204 | break; | |
5205 | case LTTNG_ROTATION_STATE_COMPLETED: | |
dd73d57b | 5206 | { |
d2956687 JG |
5207 | int fmt_ret; |
5208 | char *chunk_path; | |
dd73d57b JG |
5209 | char *current_tracing_path_reply; |
5210 | size_t current_tracing_path_reply_len; | |
5211 | ||
d2956687 JG |
5212 | DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is completed", |
5213 | rotation_id, session->name); | |
5214 | ||
dd73d57b JG |
5215 | switch (session_get_consumer_destination_type(session)) { |
5216 | case CONSUMER_DST_LOCAL: | |
5217 | current_tracing_path_reply = | |
5218 | info_return->location.local.absolute_path; | |
5219 | current_tracing_path_reply_len = | |
5220 | sizeof(info_return->location.local.absolute_path); | |
5221 | info_return->location_type = | |
05f8afa9 | 5222 | (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL; |
ecd1a12f MD |
5223 | fmt_ret = asprintf(&chunk_path, |
5224 | "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY "/%s", | |
5225 | session_get_base_path(session), | |
5226 | session->last_archived_chunk_name); | |
5227 | if (fmt_ret == -1) { | |
5228 | PERROR("Failed to format the path of the last archived trace chunk"); | |
5229 | info_return->status = LTTNG_ROTATION_STATUS_ERROR; | |
5230 | cmd_ret = LTTNG_ERR_UNK; | |
5231 | goto end; | |
5232 | } | |
dd73d57b JG |
5233 | break; |
5234 | case CONSUMER_DST_NET: | |
09cfbe47 JG |
5235 | { |
5236 | uint16_t ctrl_port, data_port; | |
5237 | ||
dd73d57b JG |
5238 | current_tracing_path_reply = |
5239 | info_return->location.relay.relative_path; | |
5240 | current_tracing_path_reply_len = | |
5241 | sizeof(info_return->location.relay.relative_path); | |
5242 | /* Currently the only supported relay protocol. */ | |
5243 | info_return->location.relay.protocol = | |
05f8afa9 | 5244 | (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP; |
dd73d57b | 5245 | |
d2956687 | 5246 | fmt_ret = lttng_strncpy(info_return->location.relay.host, |
dd73d57b JG |
5247 | session_get_net_consumer_hostname(session), |
5248 | sizeof(info_return->location.relay.host)); | |
d2956687 JG |
5249 | if (fmt_ret) { |
5250 | ERR("Failed to copy host name to rotate_get_info reply"); | |
dd73d57b | 5251 | info_return->status = LTTNG_ROTATION_STATUS_ERROR; |
d2956687 | 5252 | cmd_ret = LTTNG_ERR_SET_URL; |
dd73d57b JG |
5253 | goto end; |
5254 | } | |
5255 | ||
09cfbe47 JG |
5256 | session_get_net_consumer_ports(session, &ctrl_port, &data_port); |
5257 | info_return->location.relay.ports.control = ctrl_port; | |
5258 | info_return->location.relay.ports.data = data_port; | |
dd73d57b | 5259 | info_return->location_type = |
05f8afa9 | 5260 | (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY; |
ecd1a12f MD |
5261 | chunk_path = strdup(session->last_chunk_path); |
5262 | if (!chunk_path) { | |
5263 | ERR("Failed to allocate the path of the last archived trace chunk"); | |
5264 | info_return->status = LTTNG_ROTATION_STATUS_ERROR; | |
5265 | cmd_ret = LTTNG_ERR_UNK; | |
5266 | goto end; | |
5267 | } | |
dd73d57b | 5268 | break; |
09cfbe47 | 5269 | } |
dd73d57b JG |
5270 | default: |
5271 | abort(); | |
5272 | } | |
d2956687 JG |
5273 | |
5274 | fmt_ret = lttng_strncpy(current_tracing_path_reply, | |
5275 | chunk_path, current_tracing_path_reply_len); | |
5276 | free(chunk_path); | |
5277 | if (fmt_ret) { | |
5278 | ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply"); | |
d68c9a04 | 5279 | info_return->status = LTTNG_ROTATION_STATUS_ERROR; |
d2956687 | 5280 | cmd_ret = LTTNG_ERR_UNK; |
5c408ad8 JD |
5281 | goto end; |
5282 | } | |
dd73d57b | 5283 | |
d68c9a04 | 5284 | break; |
dd73d57b | 5285 | } |
d68c9a04 | 5286 | case LTTNG_ROTATION_STATE_ERROR: |
d2956687 | 5287 | DBG("Reporting that an error occurred during rotation %" PRIu64 " of session \"%s\"", |
d68c9a04 JD |
5288 | rotation_id, session->name); |
5289 | break; | |
5290 | default: | |
5291 | abort(); | |
5c408ad8 JD |
5292 | } |
5293 | ||
d2956687 | 5294 | cmd_ret = LTTNG_OK; |
5c408ad8 | 5295 | end: |
d2956687 JG |
5296 | info_return->status = (int32_t) rotation_state; |
5297 | return cmd_ret; | |
5c408ad8 JD |
5298 | } |
5299 | ||
259c2674 JD |
5300 | /* |
5301 | * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library. | |
5302 | * | |
5303 | * Configure the automatic rotation parameters. | |
66ea93b1 JG |
5304 | * 'activate' to true means activate the rotation schedule type with 'new_value'. |
5305 | * 'activate' to false means deactivate the rotation schedule and validate that | |
5306 | * 'new_value' has the same value as the currently active value. | |
259c2674 | 5307 | * |
66ea93b1 | 5308 | * Return 0 on success or else a positive LTTNG_ERR code. |
259c2674 JD |
5309 | */ |
5310 | int cmd_rotation_set_schedule(struct ltt_session *session, | |
66ea93b1 JG |
5311 | bool activate, enum lttng_rotation_schedule_type schedule_type, |
5312 | uint64_t new_value, | |
90936dcf | 5313 | struct notification_thread_handle *notification_thread_handle) |
259c2674 JD |
5314 | { |
5315 | int ret; | |
66ea93b1 | 5316 | uint64_t *parameter_value; |
259c2674 JD |
5317 | |
5318 | assert(session); | |
5319 | ||
5320 | DBG("Cmd rotate set schedule session %s", session->name); | |
5321 | ||
92fe5ca1 | 5322 | if (session->live_timer || !session->output_traces) { |
66ea93b1 | 5323 | DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session"); |
259c2674 JD |
5324 | ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE; |
5325 | goto end; | |
5326 | } | |
5327 | ||
66ea93b1 JG |
5328 | switch (schedule_type) { |
5329 | case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD: | |
5330 | parameter_value = &session->rotate_size; | |
5331 | break; | |
5332 | case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC: | |
5333 | parameter_value = &session->rotate_timer_period; | |
5334 | if (new_value >= UINT_MAX) { | |
5335 | DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64 " > %u (UINT_MAX)", | |
5336 | new_value, UINT_MAX); | |
5337 | ret = LTTNG_ERR_INVALID; | |
5338 | goto end; | |
5339 | } | |
5340 | break; | |
5341 | default: | |
5342 | WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type"); | |
5343 | ret = LTTNG_ERR_INVALID; | |
259c2674 | 5344 | goto end; |
66ea93b1 JG |
5345 | } |
5346 | ||
5347 | /* Improper use of the API. */ | |
5348 | if (new_value == -1ULL) { | |
5349 | WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1"); | |
5350 | ret = LTTNG_ERR_INVALID; | |
259c2674 JD |
5351 | goto end; |
5352 | } | |
5353 | ||
66ea93b1 JG |
5354 | /* |
5355 | * As indicated in struct ltt_session's comments, a value of == 0 means | |
5356 | * this schedule rotation type is not in use. | |
5357 | * | |
5358 | * Reject the command if we were asked to activate a schedule that was | |
5359 | * already active. | |
5360 | */ | |
5361 | if (activate && *parameter_value != 0) { | |
5362 | DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active"); | |
5363 | ret = LTTNG_ERR_ROTATION_SCHEDULE_SET; | |
90936dcf | 5364 | goto end; |
66ea93b1 JG |
5365 | } |
5366 | ||
5367 | /* | |
5368 | * Reject the command if we were asked to deactivate a schedule that was | |
5369 | * not active. | |
5370 | */ | |
5371 | if (!activate && *parameter_value == 0) { | |
5372 | DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive"); | |
5373 | ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET; | |
90936dcf JD |
5374 | goto end; |
5375 | } | |
5376 | ||
66ea93b1 JG |
5377 | /* |
5378 | * Reject the command if we were asked to deactivate a schedule that | |
5379 | * doesn't exist. | |
5380 | */ | |
5381 | if (!activate && *parameter_value != new_value) { | |
5382 | DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided"); | |
5383 | ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET; | |
5384 | goto end; | |
5385 | } | |
259c2674 | 5386 | |
66ea93b1 JG |
5387 | *parameter_value = activate ? new_value : 0; |
5388 | ||
5389 | switch (schedule_type) { | |
5390 | case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC: | |
5391 | if (activate && session->active) { | |
5392 | /* | |
5393 | * Only start the timer if the session is active, | |
5394 | * otherwise it will be started when the session starts. | |
5395 | */ | |
92816cc3 JG |
5396 | ret = timer_session_rotation_schedule_timer_start( |
5397 | session, new_value); | |
259c2674 | 5398 | if (ret) { |
66ea93b1 | 5399 | ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command"); |
259c2674 JD |
5400 | ret = LTTNG_ERR_UNK; |
5401 | goto end; | |
5402 | } | |
66ea93b1 | 5403 | } else { |
92816cc3 JG |
5404 | ret = timer_session_rotation_schedule_timer_stop( |
5405 | session); | |
66ea93b1 JG |
5406 | if (ret) { |
5407 | ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command"); | |
5408 | ret = LTTNG_ERR_UNK; | |
f3ce6946 | 5409 | goto end; |
66ea93b1 | 5410 | } |
259c2674 | 5411 | } |
66ea93b1 JG |
5412 | break; |
5413 | case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD: | |
5414 | if (activate) { | |
5415 | ret = subscribe_session_consumed_size_rotation(session, | |
5416 | new_value, notification_thread_handle); | |
90936dcf | 5417 | if (ret) { |
66ea93b1 | 5418 | ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command"); |
90936dcf JD |
5419 | ret = LTTNG_ERR_UNK; |
5420 | goto end; | |
5421 | } | |
90936dcf | 5422 | } else { |
66ea93b1 JG |
5423 | ret = unsubscribe_session_consumed_size_rotation(session, |
5424 | notification_thread_handle); | |
90936dcf | 5425 | if (ret) { |
66ea93b1 | 5426 | ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command"); |
90936dcf JD |
5427 | ret = LTTNG_ERR_UNK; |
5428 | goto end; | |
5429 | } | |
66ea93b1 | 5430 | |
90936dcf | 5431 | } |
66ea93b1 JG |
5432 | break; |
5433 | default: | |
5434 | /* Would have been caught before. */ | |
5435 | abort(); | |
90936dcf JD |
5436 | } |
5437 | ||
259c2674 JD |
5438 | ret = LTTNG_OK; |
5439 | ||
5440 | goto end; | |
5441 | ||
5442 | end: | |
5443 | return ret; | |
5444 | } | |
5445 | ||
a503e1ef JG |
5446 | /* Wait for a given path to be removed before continuing. */ |
5447 | static enum lttng_error_code wait_on_path(void *path_data) | |
5448 | { | |
5449 | const char *shm_path = path_data; | |
5450 | ||
5451 | DBG("Waiting for the shm path at %s to be removed before completing session destruction", | |
5452 | shm_path); | |
5453 | while (true) { | |
5454 | int ret; | |
5455 | struct stat st; | |
5456 | ||
5457 | ret = stat(shm_path, &st); | |
5458 | if (ret) { | |
5459 | if (errno != ENOENT) { | |
5460 | PERROR("stat() returned an error while checking for the existence of the shm path"); | |
5461 | } else { | |
5462 | DBG("shm path no longer exists, completing the destruction of session"); | |
5463 | } | |
5464 | break; | |
5465 | } else { | |
5466 | if (!S_ISDIR(st.st_mode)) { | |
5467 | ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal", | |
5468 | shm_path); | |
5469 | break; | |
5470 | } | |
5471 | } | |
5472 | usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US); | |
5473 | } | |
5474 | return LTTNG_OK; | |
5475 | } | |
5476 | ||
5477 | /* | |
5478 | * Returns a pointer to a handler to run on completion of a command. | |
5479 | * Returns NULL if no handler has to be run for the last command executed. | |
5480 | */ | |
5481 | const struct cmd_completion_handler *cmd_pop_completion_handler(void) | |
5482 | { | |
5483 | struct cmd_completion_handler *handler = current_completion_handler; | |
5484 | ||
5485 | current_completion_handler = NULL; | |
5486 | return handler; | |
5487 | } | |
5488 | ||
2f77fc4b DG |
5489 | /* |
5490 | * Init command subsystem. | |
5491 | */ | |
5492 | void cmd_init(void) | |
5493 | { | |
5494 | /* | |
d88aee68 DG |
5495 | * Set network sequence index to 1 for streams to match a relayd |
5496 | * socket on the consumer side. | |
2f77fc4b | 5497 | */ |
d88aee68 DG |
5498 | pthread_mutex_lock(&relayd_net_seq_idx_lock); |
5499 | relayd_net_seq_idx = 1; | |
5500 | pthread_mutex_unlock(&relayd_net_seq_idx_lock); | |
2f77fc4b DG |
5501 | |
5502 | DBG("Command subsystem initialized"); | |
5503 | } |