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