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