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