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