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