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