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