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