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