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