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