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