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