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