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