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