Commit | Line | Data |
---|---|---|
2f77fc4b | 1 | /* |
ab5be9fa MJ |
2 | * Copyright (C) 2012 David Goulet <dgoulet@efficios.com> |
3 | * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
2f77fc4b | 4 | * |
ab5be9fa | 5 | * SPDX-License-Identifier: GPL-2.0-only |
2f77fc4b | 6 | * |
2f77fc4b DG |
7 | */ |
8 | ||
6c1c0768 | 9 | #define _LGPL_SOURCE |
2f77fc4b | 10 | #include <assert.h> |
6dc3064a | 11 | #include <inttypes.h> |
2f77fc4b DG |
12 | #include <urcu/list.h> |
13 | #include <urcu/uatomic.h> | |
a503e1ef | 14 | #include <sys/stat.h> |
d2956687 | 15 | #include <stdio.h> |
2f77fc4b DG |
16 | |
17 | #include <common/defaults.h> | |
18 | #include <common/common.h> | |
c3e68e71 | 19 | #include <common/error.h> |
2f77fc4b DG |
20 | #include <common/sessiond-comm/sessiond-comm.h> |
21 | #include <common/relayd/relayd.h> | |
10a50311 | 22 | #include <common/utils.h> |
f5436bfc | 23 | #include <common/compat/string.h> |
93ec662e | 24 | #include <common/kernel-ctl/kernel-ctl.h> |
b0880ae5 JG |
25 | #include <common/dynamic-buffer.h> |
26 | #include <common/buffer-view.h> | |
9e620ea7 JG |
27 | #include <common/payload.h> |
28 | #include <common/payload-view.h> | |
82b69413 | 29 | #include <common/trace-chunk.h> |
3e3665b8 | 30 | #include <lttng/location-internal.h> |
b0880ae5 JG |
31 | #include <lttng/trigger/trigger-internal.h> |
32 | #include <lttng/condition/condition.h> | |
70670472 | 33 | #include <lttng/condition/condition-internal.h> |
c3e68e71 JR |
34 | #include <lttng/condition/on-event.h> |
35 | #include <lttng/condition/on-event-internal.h> | |
70670472 JR |
36 | #include <lttng/event-rule/event-rule.h> |
37 | #include <lttng/event-rule/event-rule-internal.h> | |
b0880ae5 | 38 | #include <lttng/action/action.h> |
c3e68e71 JR |
39 | #include <lttng/action/action-internal.h> |
40 | #include <lttng/action/group-internal.h> | |
41 | #include <lttng/action/incr-value-internal.h> | |
6e21424e | 42 | #include <lttng/channel.h> |
b0880ae5 | 43 | #include <lttng/channel-internal.h> |
c3e68e71 JR |
44 | #include "lttng/domain.h" |
45 | #include "lttng/map/map.h" | |
46 | #include <lttng/map/map-internal.h> | |
5c408ad8 | 47 | #include <lttng/rotate-internal.h> |
17dd1232 | 48 | #include <lttng/location-internal.h> |
b178f53e | 49 | #include <lttng/session-internal.h> |
3c02e545 | 50 | #include <lttng/userspace-probe-internal.h> |
b178f53e | 51 | #include <lttng/session-descriptor-internal.h> |
47aa1ca6 JG |
52 | #include <lttng/lttng-error.h> |
53 | #include <lttng/tracker.h> | |
9f449915 | 54 | #include <common/string-utils/string-utils.h> |
2f77fc4b DG |
55 | |
56 | #include "channel.h" | |
57 | #include "consumer.h" | |
58 | #include "event.h" | |
8782cc74 | 59 | #include "health-sessiond.h" |
2f77fc4b DG |
60 | #include "kernel.h" |
61 | #include "kernel-consumer.h" | |
62 | #include "lttng-sessiond.h" | |
63 | #include "utils.h" | |
0dbc2034 | 64 | #include "lttng-syscall.h" |
7c1d2758 | 65 | #include "agent.h" |
93ec662e | 66 | #include "buffer-registry.h" |
c3e68e71 | 67 | #include "map.h" |
b0880ae5 JG |
68 | #include "notification-thread.h" |
69 | #include "notification-thread-commands.h" | |
5c408ad8 JD |
70 | #include "rotate.h" |
71 | #include "rotation-thread.h" | |
8e319828 | 72 | #include "timer.h" |
f28f9e44 | 73 | #include "agent-thread.h" |
c3e68e71 | 74 | #include "session.h" |
47aa1ca6 | 75 | #include "tracker.h" |
2f77fc4b DG |
76 | |
77 | #include "cmd.h" | |
78 | ||
a503e1ef JG |
79 | /* Sleep for 100ms between each check for the shm path's deletion. */ |
80 | #define SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US 100000 | |
81 | ||
3e3665b8 JG |
82 | struct cmd_destroy_session_reply_context { |
83 | int reply_sock_fd; | |
84 | bool implicit_rotation_on_destroy; | |
3285a971 JG |
85 | /* |
86 | * Indicates whether or not an error occurred while launching the | |
87 | * destruction of a session. | |
88 | */ | |
89 | enum lttng_error_code destruction_status; | |
3e3665b8 JG |
90 | }; |
91 | ||
a503e1ef JG |
92 | static enum lttng_error_code wait_on_path(void *path); |
93 | ||
94 | /* | |
95 | * Command completion handler that is used by the destroy command | |
96 | * when a session that has a non-default shm_path is being destroyed. | |
97 | * | |
98 | * See comment in cmd_destroy_session() for the rationale. | |
99 | */ | |
100 | static struct destroy_completion_handler { | |
101 | struct cmd_completion_handler handler; | |
102 | char shm_path[member_sizeof(struct ltt_session, shm_path)]; | |
103 | } destroy_completion_handler = { | |
104 | .handler = { | |
105 | .run = wait_on_path, | |
106 | .data = destroy_completion_handler.shm_path | |
107 | }, | |
108 | .shm_path = { 0 }, | |
109 | }; | |
110 | ||
111 | static struct cmd_completion_handler *current_completion_handler; | |
112 | ||
2f77fc4b DG |
113 | /* |
114 | * Used to keep a unique index for each relayd socket created where this value | |
115 | * is associated with streams on the consumer so it can match the right relayd | |
d88aee68 DG |
116 | * to send to. It must be accessed with the relayd_net_seq_idx_lock |
117 | * held. | |
2f77fc4b | 118 | */ |
d88aee68 DG |
119 | static pthread_mutex_t relayd_net_seq_idx_lock = PTHREAD_MUTEX_INITIALIZER; |
120 | static uint64_t relayd_net_seq_idx; | |
2f77fc4b | 121 | |
7076b56e JG |
122 | static int validate_ust_event_name(const char *); |
123 | static int cmd_enable_event_internal(struct ltt_session *session, | |
df4f5a87 | 124 | const struct lttng_domain *domain, |
7076b56e JG |
125 | char *channel_name, struct lttng_event *event, |
126 | char *filter_expression, | |
2b00d462 | 127 | struct lttng_bytecode *filter, |
7076b56e JG |
128 | struct lttng_event_exclusion *exclusion, |
129 | int wpipe); | |
130 | ||
2f77fc4b DG |
131 | /* |
132 | * Create a session path used by list_lttng_sessions for the case that the | |
133 | * session consumer is on the network. | |
134 | */ | |
135 | static int build_network_session_path(char *dst, size_t size, | |
136 | struct ltt_session *session) | |
137 | { | |
138 | int ret, kdata_port, udata_port; | |
139 | struct lttng_uri *kuri = NULL, *uuri = NULL, *uri = NULL; | |
140 | char tmp_uurl[PATH_MAX], tmp_urls[PATH_MAX]; | |
141 | ||
142 | assert(session); | |
143 | assert(dst); | |
144 | ||
145 | memset(tmp_urls, 0, sizeof(tmp_urls)); | |
146 | memset(tmp_uurl, 0, sizeof(tmp_uurl)); | |
147 | ||
148 | kdata_port = udata_port = DEFAULT_NETWORK_DATA_PORT; | |
149 | ||
150 | if (session->kernel_session && session->kernel_session->consumer) { | |
151 | kuri = &session->kernel_session->consumer->dst.net.control; | |
152 | kdata_port = session->kernel_session->consumer->dst.net.data.port; | |
153 | } | |
154 | ||
155 | if (session->ust_session && session->ust_session->consumer) { | |
156 | uuri = &session->ust_session->consumer->dst.net.control; | |
157 | udata_port = session->ust_session->consumer->dst.net.data.port; | |
158 | } | |
159 | ||
160 | if (uuri == NULL && kuri == NULL) { | |
161 | uri = &session->consumer->dst.net.control; | |
162 | kdata_port = session->consumer->dst.net.data.port; | |
163 | } else if (kuri && uuri) { | |
164 | ret = uri_compare(kuri, uuri); | |
165 | if (ret) { | |
166 | /* Not Equal */ | |
167 | uri = kuri; | |
168 | /* Build uuri URL string */ | |
169 | ret = uri_to_str_url(uuri, tmp_uurl, sizeof(tmp_uurl)); | |
170 | if (ret < 0) { | |
171 | goto error; | |
172 | } | |
173 | } else { | |
174 | uri = kuri; | |
175 | } | |
176 | } else if (kuri && uuri == NULL) { | |
177 | uri = kuri; | |
178 | } else if (uuri && kuri == NULL) { | |
179 | uri = uuri; | |
180 | } | |
181 | ||
182 | ret = uri_to_str_url(uri, tmp_urls, sizeof(tmp_urls)); | |
183 | if (ret < 0) { | |
184 | goto error; | |
185 | } | |
186 | ||
9aa9f900 DG |
187 | /* |
188 | * Do we have a UST url set. If yes, this means we have both kernel and UST | |
189 | * to print. | |
190 | */ | |
9d035200 | 191 | if (*tmp_uurl != '\0') { |
2f77fc4b DG |
192 | ret = snprintf(dst, size, "[K]: %s [data: %d] -- [U]: %s [data: %d]", |
193 | tmp_urls, kdata_port, tmp_uurl, udata_port); | |
194 | } else { | |
9aa9f900 | 195 | int dport; |
bef08707 | 196 | if (kuri || (!kuri && !uuri)) { |
9aa9f900 DG |
197 | dport = kdata_port; |
198 | } else { | |
199 | /* No kernel URI, use the UST port. */ | |
200 | dport = udata_port; | |
201 | } | |
202 | ret = snprintf(dst, size, "%s [data: %d]", tmp_urls, dport); | |
2f77fc4b DG |
203 | } |
204 | ||
205 | error: | |
206 | return ret; | |
207 | } | |
208 | ||
fb83fe64 JD |
209 | /* |
210 | * Get run-time attributes if the session has been started (discarded events, | |
211 | * lost packets). | |
212 | */ | |
213 | static int get_kernel_runtime_stats(struct ltt_session *session, | |
214 | struct ltt_kernel_channel *kchan, uint64_t *discarded_events, | |
215 | uint64_t *lost_packets) | |
216 | { | |
217 | int ret; | |
218 | ||
219 | if (!session->has_been_started) { | |
220 | ret = 0; | |
221 | *discarded_events = 0; | |
222 | *lost_packets = 0; | |
223 | goto end; | |
224 | } | |
225 | ||
e1f3997a | 226 | ret = consumer_get_discarded_events(session->id, kchan->key, |
fb83fe64 JD |
227 | session->kernel_session->consumer, |
228 | discarded_events); | |
229 | if (ret < 0) { | |
230 | goto end; | |
231 | } | |
232 | ||
e1f3997a | 233 | ret = consumer_get_lost_packets(session->id, kchan->key, |
fb83fe64 JD |
234 | session->kernel_session->consumer, |
235 | lost_packets); | |
236 | if (ret < 0) { | |
237 | goto end; | |
238 | } | |
239 | ||
240 | end: | |
241 | return ret; | |
242 | } | |
243 | ||
244 | /* | |
245 | * Get run-time attributes if the session has been started (discarded events, | |
246 | * lost packets). | |
247 | */ | |
248 | static int get_ust_runtime_stats(struct ltt_session *session, | |
249 | struct ltt_ust_channel *uchan, uint64_t *discarded_events, | |
250 | uint64_t *lost_packets) | |
251 | { | |
252 | int ret; | |
253 | struct ltt_ust_session *usess; | |
254 | ||
a91c5803 JG |
255 | if (!discarded_events || !lost_packets) { |
256 | ret = -1; | |
257 | goto end; | |
258 | } | |
259 | ||
fb83fe64 | 260 | usess = session->ust_session; |
a905c624 JG |
261 | assert(discarded_events); |
262 | assert(lost_packets); | |
fb83fe64 JD |
263 | |
264 | if (!usess || !session->has_been_started) { | |
265 | *discarded_events = 0; | |
266 | *lost_packets = 0; | |
267 | ret = 0; | |
268 | goto end; | |
269 | } | |
270 | ||
271 | if (usess->buffer_type == LTTNG_BUFFER_PER_UID) { | |
272 | ret = ust_app_uid_get_channel_runtime_stats(usess->id, | |
273 | &usess->buffer_reg_uid_list, | |
274 | usess->consumer, uchan->id, | |
275 | uchan->attr.overwrite, | |
276 | discarded_events, | |
277 | lost_packets); | |
278 | } else if (usess->buffer_type == LTTNG_BUFFER_PER_PID) { | |
279 | ret = ust_app_pid_get_channel_runtime_stats(usess, | |
280 | uchan, usess->consumer, | |
281 | uchan->attr.overwrite, | |
282 | discarded_events, | |
283 | lost_packets); | |
284 | if (ret < 0) { | |
285 | goto end; | |
286 | } | |
287 | *discarded_events += uchan->per_pid_closed_app_discarded; | |
288 | *lost_packets += uchan->per_pid_closed_app_lost; | |
289 | } else { | |
290 | ERR("Unsupported buffer type"); | |
967bc289 | 291 | assert(0); |
fb83fe64 JD |
292 | ret = -1; |
293 | goto end; | |
294 | } | |
295 | ||
296 | end: | |
297 | return ret; | |
298 | } | |
299 | ||
c3e68e71 JR |
300 | enum tracer_executed_action_state { |
301 | TRACER_EXECUTED_ACTION_STATE_REGISTER, | |
302 | TRACER_EXECUTED_ACTION_STATE_UNREGISTER | |
303 | }; | |
304 | ||
305 | static | |
306 | enum lttng_error_code sync_incr_value_action_ust( | |
307 | struct ltt_session *session, | |
308 | const struct lttng_condition *condition, | |
309 | const char *map_name, | |
310 | uint64_t tracer_token, | |
311 | struct lttng_map_key *key, | |
312 | enum tracer_executed_action_state state) | |
313 | { | |
314 | enum lttng_error_code ret_code; | |
315 | const struct lttng_event_rule *event_rule; | |
316 | enum lttng_condition_status cond_status; | |
317 | enum lttng_event_rule_status er_status; | |
318 | struct ltt_ust_session *usess = session->ust_session; | |
319 | const char *pattern; | |
320 | struct ltt_ust_map *map; | |
321 | char *filter_expression; | |
322 | struct lttng_bytecode *filter; | |
323 | struct lttng_event_exclusion *exclusion; | |
324 | enum lttng_event_rule_generate_exclusions_status | |
325 | generate_exclusion_status; | |
326 | ||
327 | DBG("Syncing UST incr-value action for session '%s', map '%s'", | |
328 | session->name, map_name); | |
329 | if (!usess) { | |
330 | DBG("No UST session"); | |
331 | ret_code = LTTNG_OK; | |
332 | goto end; | |
333 | } | |
334 | ||
335 | assert(usess->domain_global.maps); | |
336 | ||
337 | map = trace_ust_find_map_by_name(usess->domain_global.maps, map_name); | |
338 | if (!map) { | |
339 | DBG("UST map \"%s\" not found", map_name); | |
340 | ret_code = LTTNG_OK; | |
341 | goto end; | |
342 | } | |
343 | ||
344 | cond_status = lttng_condition_on_event_get_rule(condition, &event_rule); | |
345 | if (cond_status != LTTNG_CONDITION_STATUS_OK) { | |
346 | ERR("Error getting on-event condition event-rule"); | |
347 | ret_code = LTTNG_ERR_INVALID_MAP; | |
348 | goto end; | |
349 | } | |
350 | ||
351 | er_status = lttng_event_rule_tracepoint_get_pattern(event_rule, &pattern); | |
352 | if (er_status != LTTNG_EVENT_RULE_STATUS_OK) { | |
353 | /* At this point, this is a fatal error. */ | |
354 | abort(); | |
355 | } | |
356 | ||
357 | /* | |
358 | * FIXME: frdeso, reuse the event notifier functions and approach to | |
359 | * create the event. | |
360 | */ | |
361 | filter_expression = (char *) lttng_event_rule_get_filter(event_rule); | |
362 | filter = (struct lttng_bytecode *)lttng_event_rule_get_filter_bytecode( | |
363 | event_rule); | |
364 | generate_exclusion_status = lttng_event_rule_generate_exclusions( | |
365 | event_rule, &exclusion); | |
366 | if (generate_exclusion_status == LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_ERROR) { | |
367 | ERR("Error generating the exclusion"); | |
368 | ret_code = LTTNG_ERR_EXCLUSION_INVAL; | |
369 | goto end; | |
370 | } | |
371 | ||
372 | switch (state) { | |
373 | case TRACER_EXECUTED_ACTION_STATE_REGISTER: | |
374 | ret_code = map_event_ust_enable_tracepoint(usess, | |
375 | map, tracer_token, (char *) pattern, key, | |
376 | LTTNG_EVENT_TRACEPOINT, LTTNG_EVENT_LOGLEVEL_ALL, | |
377 | 0, filter_expression, filter, exclusion, false); | |
378 | if (ret_code == LTTNG_ERR_UST_EVENT_ENABLED) { | |
379 | ret_code = LTTNG_OK; | |
380 | } else if (ret_code != LTTNG_OK) { | |
381 | ERR("Enabling UST map event"); | |
382 | goto end; | |
383 | } | |
384 | break; | |
385 | case TRACER_EXECUTED_ACTION_STATE_UNREGISTER: | |
386 | ret_code = map_event_ust_disable_tracepoint(usess, | |
387 | map, tracer_token, (char *) pattern, key, | |
388 | LTTNG_EVENT_TRACEPOINT, LTTNG_EVENT_LOGLEVEL_ALL, | |
389 | 0, filter_expression, filter, exclusion, false); | |
390 | if (ret_code == LTTNG_ERR_UST_EVENT_ENABLED) { | |
391 | ret_code = LTTNG_OK; | |
392 | } else if (ret_code != LTTNG_OK) { | |
393 | ERR("Enabling UST map event"); | |
394 | goto end; | |
395 | } | |
396 | break; | |
397 | default: | |
398 | abort(); | |
399 | } | |
400 | ||
401 | ret_code = LTTNG_OK; | |
402 | ||
403 | end: | |
404 | return ret_code; | |
405 | } | |
406 | ||
407 | static | |
408 | enum lttng_error_code sync_incr_value_action_kernel( | |
409 | struct ltt_session *session, | |
410 | const struct lttng_credentials *creds, | |
411 | const struct lttng_condition *condition, | |
412 | const char *map_name, | |
413 | uint64_t tracer_token, | |
414 | struct lttng_map_key *key, | |
415 | enum tracer_executed_action_state state) | |
416 | { | |
417 | enum lttng_error_code ret_code; | |
418 | const struct lttng_event_rule *event_rule; | |
419 | enum lttng_condition_status cond_status; | |
420 | struct ltt_kernel_map *kmap; | |
421 | struct ltt_kernel_session *ksess = session->kernel_session; | |
422 | ||
423 | DBG("Syncing kernel incr-value action for session '%s', map '%s'", | |
424 | session->name, map_name); | |
425 | ||
426 | if (!ksess) { | |
427 | DBG("No kernel session"); | |
428 | ret_code = LTTNG_OK; | |
429 | goto end; | |
430 | } | |
431 | ||
432 | kmap = trace_kernel_get_map_by_name(map_name, ksess); | |
433 | if (!kmap) { | |
434 | DBG("Kernel map \"%s\" not found", map_name); | |
435 | ret_code = LTTNG_OK; | |
436 | goto end; | |
437 | } | |
438 | ||
439 | cond_status = lttng_condition_on_event_get_rule(condition, &event_rule); | |
440 | if (cond_status != LTTNG_CONDITION_STATUS_OK) { | |
441 | ret_code = LTTNG_ERR_INVALID_MAP; | |
442 | ERR("Error getting on-event condition event-rule"); | |
443 | goto end; | |
444 | } | |
445 | ||
446 | switch (state) { | |
447 | case TRACER_EXECUTED_ACTION_STATE_REGISTER: | |
448 | ret_code = map_event_kernel_enable_event(kmap, creds, tracer_token, | |
449 | event_rule, key); | |
450 | if(ret_code != LTTNG_OK) { | |
451 | ERR("Error enabling event counter to the kernel tracer"); | |
452 | goto end; | |
453 | } | |
454 | break; | |
455 | case TRACER_EXECUTED_ACTION_STATE_UNREGISTER: | |
456 | ret_code = map_event_kernel_disable_event(kmap, tracer_token); | |
457 | if(ret_code != LTTNG_OK) { | |
458 | ERR("Error disabling event counter to the kernel tracer"); | |
459 | goto end; | |
460 | } | |
461 | break; | |
462 | } | |
463 | ||
464 | end: | |
465 | return ret_code; | |
466 | } | |
467 | ||
468 | static | |
469 | enum lttng_error_code sync_incr_value_action( | |
470 | const struct lttng_credentials *creds, | |
471 | const struct lttng_condition *condition, | |
472 | const struct lttng_action *action, | |
473 | enum tracer_executed_action_state state) | |
474 | { | |
475 | enum lttng_error_code ret; | |
476 | const char *session_name, *map_name; | |
477 | enum lttng_action_status action_status; | |
478 | enum lttng_condition_status cond_status; | |
479 | const struct lttng_event_rule *event_rule; | |
480 | struct ltt_session *session = NULL; | |
481 | struct lttng_map_key *key; | |
482 | uint64_t action_tracer_token; | |
483 | ||
484 | action_status = lttng_action_incr_value_get_map_name(action, | |
485 | &map_name); | |
486 | if (action_status != LTTNG_ACTION_STATUS_OK) { | |
487 | ERR("Map name not set for incr-value action"); | |
488 | ret = LTTNG_ERR_INVALID_MAP; | |
489 | goto end; | |
490 | } | |
491 | ||
492 | action_status = lttng_action_incr_value_get_session_name(action, | |
493 | &session_name); | |
494 | if (action_status != LTTNG_ACTION_STATUS_OK) { | |
495 | ERR("Session name not set for incr-value action"); | |
496 | ret = LTTNG_ERR_INVALID_MAP; | |
497 | goto end; | |
498 | } | |
499 | ||
500 | action_status = lttng_action_incr_value_borrow_key_mutable(action, &key); | |
501 | if (action_status != LTTNG_ACTION_STATUS_OK) { | |
502 | ERR("Key not set for incr-value action"); | |
503 | ret = LTTNG_ERR_INVALID_MAP; | |
504 | goto end; | |
505 | } | |
506 | ||
507 | /* Returns a refcounted reference */ | |
508 | session = session_find_by_name(session_name); | |
509 | if(!session) { | |
510 | DBG("Session not found for incr-value action: session-name=%s", | |
511 | session_name); | |
512 | ret = LTTNG_OK; | |
513 | goto end; | |
514 | } | |
515 | ||
516 | cond_status = lttng_condition_on_event_get_rule(condition, &event_rule); | |
517 | if (cond_status != LTTNG_CONDITION_STATUS_OK) { | |
518 | ERR("Error getting on-event condition event-rule"); | |
519 | ret = LTTNG_ERR_INVALID_MAP; | |
520 | session_put(session); | |
521 | goto end; | |
522 | } | |
523 | ||
524 | action_tracer_token = lttng_action_incr_value_get_tracer_token(action); | |
525 | ||
526 | switch (lttng_event_rule_get_domain_type(event_rule)) { | |
527 | case LTTNG_DOMAIN_UST: | |
528 | ret = sync_incr_value_action_ust(session, condition, | |
529 | map_name, action_tracer_token, key, state); | |
530 | if (ret == LTTNG_ERR_UST_EVENT_EXIST) { | |
531 | DBG("Incr-value action already registered"); | |
532 | ret = LTTNG_OK; | |
533 | } | |
534 | break; | |
535 | case LTTNG_DOMAIN_KERNEL: | |
536 | ret = sync_incr_value_action_kernel(session, creds, condition, | |
537 | map_name, action_tracer_token, key, state); | |
538 | if (ret == LTTNG_ERR_KERN_EVENT_EXIST) { | |
539 | DBG("Incr-value action already registered"); | |
540 | ret = LTTNG_OK; | |
541 | } | |
542 | break; | |
543 | default: | |
544 | abort(); | |
545 | } | |
546 | ||
547 | goto end; | |
548 | end: | |
549 | if (session) { | |
550 | session_put(session); | |
551 | } | |
552 | return ret; | |
553 | } | |
554 | ||
555 | static | |
556 | enum lttng_error_code sync_one_tracer_executed_action( | |
557 | const struct lttng_credentials *creds, | |
558 | const struct lttng_condition *condition, | |
559 | const struct lttng_action *action, | |
560 | enum tracer_executed_action_state state) | |
561 | { | |
562 | enum lttng_action_type action_type; | |
563 | enum lttng_error_code ret; | |
564 | ||
565 | action_type = lttng_action_get_type(action); | |
566 | assert(action_type != LTTNG_ACTION_TYPE_GROUP); | |
567 | ||
568 | switch (action_type) { | |
569 | case LTTNG_ACTION_TYPE_INCREMENT_VALUE: | |
570 | DBG("Action type \"%s\" is a tracer executed action.", | |
571 | lttng_action_type_string(action_type)); | |
572 | ||
573 | ret = sync_incr_value_action(creds, condition, action, state); | |
574 | if (ret != LTTNG_OK) { | |
575 | ERR("Error syncing increment value action to the tracer"); | |
576 | } | |
577 | break; | |
578 | default: | |
579 | DBG("Action type \"%s\" is not a tracer executed action.", | |
580 | lttng_action_type_string(action_type)); | |
581 | ret = LTTNG_OK; | |
582 | goto end; | |
583 | } | |
584 | ||
585 | end: | |
586 | return ret; | |
587 | } | |
588 | ||
589 | static | |
590 | enum lttng_error_code sync_all_tracer_executed_actions( | |
591 | const struct lttng_trigger *trigger, | |
592 | const struct lttng_credentials *cmd_creds, | |
593 | enum tracer_executed_action_state state) | |
594 | { | |
595 | enum lttng_error_code ret; | |
596 | unsigned int i, count; | |
597 | enum lttng_action_status action_status; | |
598 | enum lttng_action_type action_type; | |
599 | const struct lttng_action *action; | |
600 | const struct lttng_condition *condition; | |
601 | ||
602 | condition = lttng_trigger_get_const_condition(trigger); | |
603 | action = lttng_trigger_get_const_action(trigger); | |
604 | ||
605 | action_type = lttng_action_get_type(action); | |
606 | ||
607 | DBG("Iterating over all actions of trigger \"%s\" to sync any tracer executed actions", | |
608 | trigger->name); | |
609 | ||
610 | if (action_type != LTTNG_ACTION_TYPE_GROUP) { | |
611 | ret = sync_one_tracer_executed_action(cmd_creds, condition, action, | |
612 | state); | |
613 | } else { | |
614 | action_status = lttng_action_group_get_count(action, &count); | |
615 | assert(action_status == LTTNG_ACTION_STATUS_OK); | |
616 | ||
617 | for (i = 0; i < count; i++) { | |
618 | const struct lttng_action *inner_action = | |
619 | lttng_action_group_get_at_index(action, i); | |
620 | ||
621 | ret = sync_one_tracer_executed_action(cmd_creds, condition, | |
622 | inner_action, state); | |
623 | if (ret != LTTNG_OK) { | |
624 | ERR("Error syncing tracer executed action"); | |
625 | goto end; | |
626 | } | |
627 | } | |
628 | } | |
629 | ||
630 | end: | |
631 | return ret; | |
632 | } | |
633 | ||
2f77fc4b DG |
634 | /* |
635 | * Fill lttng_channel array of all channels. | |
636 | */ | |
d449df4a | 637 | static ssize_t list_lttng_channels(enum lttng_domain_type domain, |
fb83fe64 | 638 | struct ltt_session *session, struct lttng_channel *channels, |
cf0bcb51 | 639 | struct lttng_channel_extended *chan_exts) |
2f77fc4b | 640 | { |
d449df4a | 641 | int i = 0, ret = 0; |
2f77fc4b DG |
642 | struct ltt_kernel_channel *kchan; |
643 | ||
644 | DBG("Listing channels for session %s", session->name); | |
645 | ||
646 | switch (domain) { | |
647 | case LTTNG_DOMAIN_KERNEL: | |
648 | /* Kernel channels */ | |
649 | if (session->kernel_session != NULL) { | |
650 | cds_list_for_each_entry(kchan, | |
651 | &session->kernel_session->channel_list.head, list) { | |
fb83fe64 | 652 | uint64_t discarded_events, lost_packets; |
cf0bcb51 JG |
653 | struct lttng_channel_extended *extended; |
654 | ||
655 | extended = (struct lttng_channel_extended *) | |
656 | kchan->channel->attr.extended.ptr; | |
fb83fe64 JD |
657 | |
658 | ret = get_kernel_runtime_stats(session, kchan, | |
659 | &discarded_events, &lost_packets); | |
660 | if (ret < 0) { | |
661 | goto end; | |
662 | } | |
2f77fc4b DG |
663 | /* Copy lttng_channel struct to array */ |
664 | memcpy(&channels[i], kchan->channel, sizeof(struct lttng_channel)); | |
665 | channels[i].enabled = kchan->enabled; | |
fb83fe64 JD |
666 | chan_exts[i].discarded_events = |
667 | discarded_events; | |
668 | chan_exts[i].lost_packets = lost_packets; | |
cf0bcb51 JG |
669 | chan_exts[i].monitor_timer_interval = |
670 | extended->monitor_timer_interval; | |
491d1539 | 671 | chan_exts[i].blocking_timeout = 0; |
2f77fc4b DG |
672 | i++; |
673 | } | |
674 | } | |
675 | break; | |
676 | case LTTNG_DOMAIN_UST: | |
677 | { | |
678 | struct lttng_ht_iter iter; | |
679 | struct ltt_ust_channel *uchan; | |
680 | ||
e7fe706f | 681 | rcu_read_lock(); |
2f77fc4b DG |
682 | cds_lfht_for_each_entry(session->ust_session->domain_global.channels->ht, |
683 | &iter.iter, uchan, node.node) { | |
a91c5803 | 684 | uint64_t discarded_events = 0, lost_packets = 0; |
fb83fe64 | 685 | |
8ee609c8 MD |
686 | if (lttng_strncpy(channels[i].name, uchan->name, |
687 | LTTNG_SYMBOL_NAME_LEN)) { | |
688 | break; | |
689 | } | |
2f77fc4b DG |
690 | channels[i].attr.overwrite = uchan->attr.overwrite; |
691 | channels[i].attr.subbuf_size = uchan->attr.subbuf_size; | |
692 | channels[i].attr.num_subbuf = uchan->attr.num_subbuf; | |
693 | channels[i].attr.switch_timer_interval = | |
694 | uchan->attr.switch_timer_interval; | |
695 | channels[i].attr.read_timer_interval = | |
696 | uchan->attr.read_timer_interval; | |
697 | channels[i].enabled = uchan->enabled; | |
2f785fe7 DG |
698 | channels[i].attr.tracefile_size = uchan->tracefile_size; |
699 | channels[i].attr.tracefile_count = uchan->tracefile_count; | |
5e4435a4 JG |
700 | |
701 | /* | |
702 | * Map enum lttng_ust_output to enum lttng_event_output. | |
703 | */ | |
2f77fc4b DG |
704 | switch (uchan->attr.output) { |
705 | case LTTNG_UST_MMAP: | |
2f77fc4b DG |
706 | channels[i].attr.output = LTTNG_EVENT_MMAP; |
707 | break; | |
5e4435a4 JG |
708 | default: |
709 | /* | |
710 | * LTTNG_UST_MMAP is the only supported UST | |
711 | * output mode. | |
712 | */ | |
713 | assert(0); | |
714 | break; | |
2f77fc4b | 715 | } |
fb83fe64 | 716 | |
d449df4a MD |
717 | chan_exts[i].monitor_timer_interval = |
718 | uchan->monitor_timer_interval; | |
491d1539 MD |
719 | chan_exts[i].blocking_timeout = |
720 | uchan->attr.u.s.blocking_timeout; | |
d449df4a | 721 | |
fb83fe64 JD |
722 | ret = get_ust_runtime_stats(session, uchan, |
723 | &discarded_events, &lost_packets); | |
724 | if (ret < 0) { | |
725 | break; | |
726 | } | |
727 | chan_exts[i].discarded_events = discarded_events; | |
728 | chan_exts[i].lost_packets = lost_packets; | |
2f77fc4b DG |
729 | i++; |
730 | } | |
e7fe706f | 731 | rcu_read_unlock(); |
2f77fc4b DG |
732 | break; |
733 | } | |
734 | default: | |
735 | break; | |
736 | } | |
fb83fe64 JD |
737 | |
738 | end: | |
d449df4a MD |
739 | if (ret < 0) { |
740 | return -LTTNG_ERR_FATAL; | |
741 | } else { | |
742 | return LTTNG_OK; | |
743 | } | |
2f77fc4b DG |
744 | } |
745 | ||
3c02e545 FD |
746 | static int append_extended_info(const char *filter_expression, |
747 | struct lttng_event_exclusion *exclusion, | |
748 | struct lttng_userspace_probe_location *probe_location, | |
e368fb43 | 749 | struct lttng_payload *payload) |
b4e3ceb9 | 750 | { |
3c02e545 | 751 | int ret = 0; |
b4e3ceb9 | 752 | size_t filter_len = 0; |
795d57ce | 753 | size_t nb_exclusions = 0; |
3c02e545 | 754 | size_t userspace_probe_location_len = 0; |
e368fb43 JG |
755 | struct lttcomm_event_extended_header extended_header = {}; |
756 | struct lttcomm_event_extended_header *p_extended_header; | |
757 | const size_t original_payload_size = payload->buffer.size; | |
758 | ||
759 | ret = lttng_dynamic_buffer_append(&payload->buffer, &extended_header, | |
760 | sizeof(extended_header)); | |
761 | if (ret) { | |
762 | goto end; | |
763 | } | |
b4e3ceb9 PP |
764 | |
765 | if (filter_expression) { | |
766 | filter_len = strlen(filter_expression) + 1; | |
e368fb43 JG |
767 | ret = lttng_dynamic_buffer_append(&payload->buffer, |
768 | filter_expression, filter_len); | |
769 | if (ret) { | |
770 | goto end; | |
771 | } | |
b4e3ceb9 PP |
772 | } |
773 | ||
795d57ce | 774 | if (exclusion) { |
e368fb43 JG |
775 | const size_t len = exclusion->count * LTTNG_SYMBOL_NAME_LEN; |
776 | ||
795d57ce | 777 | nb_exclusions = exclusion->count; |
e368fb43 JG |
778 | |
779 | ret = lttng_dynamic_buffer_append( | |
780 | &payload->buffer, &exclusion->names, len); | |
781 | if (ret) { | |
782 | goto end; | |
783 | } | |
795d57ce PP |
784 | } |
785 | ||
3c02e545 | 786 | if (probe_location) { |
e368fb43 JG |
787 | const size_t size_before_probe = payload->buffer.size; |
788 | ||
3c02e545 | 789 | ret = lttng_userspace_probe_location_serialize(probe_location, |
e368fb43 | 790 | payload); |
3c02e545 FD |
791 | if (ret < 0) { |
792 | ret = -1; | |
793 | goto end; | |
794 | } | |
795d57ce | 795 | |
e368fb43 JG |
796 | userspace_probe_location_len = |
797 | payload->buffer.size - size_before_probe; | |
795d57ce PP |
798 | } |
799 | ||
e368fb43 JG |
800 | /* Set header fields */ |
801 | p_extended_header = (struct lttcomm_event_extended_header *) | |
802 | (payload->buffer.data + original_payload_size); | |
795d57ce | 803 | |
e368fb43 JG |
804 | p_extended_header->filter_len = filter_len; |
805 | p_extended_header->nb_exclusions = nb_exclusions; | |
806 | p_extended_header->userspace_probe_location_len = | |
807 | userspace_probe_location_len; | |
3c02e545 | 808 | |
3c02e545 FD |
809 | ret = 0; |
810 | end: | |
811 | return ret; | |
b4e3ceb9 PP |
812 | } |
813 | ||
3c6a091f | 814 | /* |
022d91ba | 815 | * Create a list of agent domain events. |
3c6a091f DG |
816 | * |
817 | * Return number of events in list on success or else a negative value. | |
818 | */ | |
022d91ba | 819 | static int list_lttng_agent_events(struct agent *agt, |
e368fb43 | 820 | struct lttng_payload *payload) |
3c6a091f | 821 | { |
e368fb43 JG |
822 | int nb_events = 0, ret = 0; |
823 | const struct agent_event *agent_event; | |
3c6a091f DG |
824 | struct lttng_ht_iter iter; |
825 | ||
022d91ba | 826 | assert(agt); |
3c6a091f | 827 | |
022d91ba | 828 | DBG3("Listing agent events"); |
3c6a091f | 829 | |
e5bbf678 | 830 | rcu_read_lock(); |
e368fb43 JG |
831 | cds_lfht_for_each_entry ( |
832 | agt->events->ht, &iter.iter, agent_event, node.node) { | |
833 | struct lttng_event event = { | |
44760c20 | 834 | .enabled = AGENT_EVENT_IS_ENABLED(agent_event), |
e368fb43 JG |
835 | .loglevel = agent_event->loglevel_value, |
836 | .loglevel_type = agent_event->loglevel_type, | |
837 | }; | |
838 | ||
43ed1485 JG |
839 | ret = lttng_strncpy(event.name, agent_event->name, sizeof(event.name)); |
840 | if (ret) { | |
841 | /* Internal error, invalid name. */ | |
842 | ERR("Invalid event name while listing agent events: '%s' exceeds the maximal allowed length of %zu bytes", | |
843 | agent_event->name, sizeof(event.name)); | |
844 | ret = -LTTNG_ERR_UNK; | |
845 | goto end; | |
846 | } | |
e368fb43 JG |
847 | |
848 | ret = lttng_dynamic_buffer_append( | |
849 | &payload->buffer, &event, sizeof(event)); | |
3c02e545 | 850 | if (ret) { |
e368fb43 JG |
851 | ERR("Failed to append event to payload"); |
852 | ret = -LTTNG_ERR_NOMEM; | |
853 | goto end; | |
3c02e545 | 854 | } |
b4e3ceb9 | 855 | |
e368fb43 | 856 | nb_events++; |
3c6a091f DG |
857 | } |
858 | ||
e368fb43 JG |
859 | cds_lfht_for_each_entry ( |
860 | agt->events->ht, &iter.iter, agent_event, node.node) { | |
861 | /* Append extended info. */ | |
862 | ret = append_extended_info(agent_event->filter_expression, NULL, | |
863 | NULL, payload); | |
3c02e545 | 864 | if (ret) { |
e368fb43 JG |
865 | ERR("Failed to append extended event info to payload"); |
866 | ret = -LTTNG_ERR_NOMEM; | |
867 | goto end; | |
3c02e545 | 868 | } |
3c6a091f | 869 | } |
3c6a091f | 870 | |
e368fb43 | 871 | ret = nb_events; |
47e52862 | 872 | end: |
3c02e545 | 873 | rcu_read_unlock(); |
3c6a091f DG |
874 | return ret; |
875 | } | |
876 | ||
2f77fc4b DG |
877 | /* |
878 | * Create a list of ust global domain events. | |
879 | */ | |
880 | static int list_lttng_ust_global_events(char *channel_name, | |
b4e3ceb9 | 881 | struct ltt_ust_domain_global *ust_global, |
e368fb43 | 882 | struct lttng_payload *payload) |
2f77fc4b | 883 | { |
e368fb43 JG |
884 | int ret = 0; |
885 | unsigned int nb_events = 0; | |
2f77fc4b | 886 | struct lttng_ht_iter iter; |
e368fb43 JG |
887 | const struct lttng_ht_node_str *node; |
888 | const struct ltt_ust_channel *uchan; | |
889 | const struct ltt_ust_event *uevent; | |
2f77fc4b DG |
890 | |
891 | DBG("Listing UST global events for channel %s", channel_name); | |
892 | ||
893 | rcu_read_lock(); | |
894 | ||
e368fb43 | 895 | lttng_ht_lookup(ust_global->channels, (void *) channel_name, &iter); |
2f77fc4b DG |
896 | node = lttng_ht_iter_get_node_str(&iter); |
897 | if (node == NULL) { | |
f73fabfd | 898 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
d31d3e8c | 899 | goto end; |
2f77fc4b DG |
900 | } |
901 | ||
902 | uchan = caa_container_of(&node->node, struct ltt_ust_channel, node.node); | |
903 | ||
e368fb43 | 904 | DBG3("Listing UST global events"); |
2f77fc4b | 905 | |
b4e3ceb9 | 906 | cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) { |
e368fb43 JG |
907 | struct lttng_event event = {}; |
908 | ||
b4e3ceb9 | 909 | if (uevent->internal) { |
b4e3ceb9 PP |
910 | continue; |
911 | } | |
912 | ||
43ed1485 JG |
913 | ret = lttng_strncpy(event.name, uevent->attr.name, sizeof(event.name)); |
914 | if (ret) { | |
915 | /* Internal error, invalid name. */ | |
916 | ERR("Invalid event name while listing user space tracer events: '%s' exceeds the maximal allowed length of %zu bytes", | |
917 | uevent->attr.name, sizeof(event.name)); | |
918 | ret = -LTTNG_ERR_UNK; | |
919 | goto end; | |
920 | } | |
2f77fc4b | 921 | |
e368fb43 | 922 | event.enabled = uevent->enabled; |
2f77fc4b DG |
923 | |
924 | switch (uevent->attr.instrumentation) { | |
925 | case LTTNG_UST_TRACEPOINT: | |
e368fb43 | 926 | event.type = LTTNG_EVENT_TRACEPOINT; |
2f77fc4b DG |
927 | break; |
928 | case LTTNG_UST_PROBE: | |
e368fb43 | 929 | event.type = LTTNG_EVENT_PROBE; |
2f77fc4b DG |
930 | break; |
931 | case LTTNG_UST_FUNCTION: | |
e368fb43 | 932 | event.type = LTTNG_EVENT_FUNCTION; |
2f77fc4b DG |
933 | break; |
934 | } | |
935 | ||
e368fb43 | 936 | event.loglevel = uevent->attr.loglevel; |
2f77fc4b DG |
937 | switch (uevent->attr.loglevel_type) { |
938 | case LTTNG_UST_LOGLEVEL_ALL: | |
e368fb43 | 939 | event.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; |
2f77fc4b DG |
940 | break; |
941 | case LTTNG_UST_LOGLEVEL_RANGE: | |
e368fb43 | 942 | event.loglevel_type = LTTNG_EVENT_LOGLEVEL_RANGE; |
2f77fc4b DG |
943 | break; |
944 | case LTTNG_UST_LOGLEVEL_SINGLE: | |
e368fb43 | 945 | event.loglevel_type = LTTNG_EVENT_LOGLEVEL_SINGLE; |
2f77fc4b DG |
946 | break; |
947 | } | |
e368fb43 | 948 | |
2f77fc4b | 949 | if (uevent->filter) { |
e368fb43 | 950 | event.filter = 1; |
2f77fc4b | 951 | } |
e368fb43 | 952 | |
4634f12e | 953 | if (uevent->exclusion) { |
e368fb43 JG |
954 | event.exclusion = 1; |
955 | } | |
956 | ||
957 | ret = lttng_dynamic_buffer_append(&payload->buffer, &event, sizeof(event)); | |
958 | if (ret) { | |
959 | ERR("Failed to append event to payload"); | |
960 | ret = -LTTNG_ERR_NOMEM; | |
961 | goto end; | |
4634f12e | 962 | } |
b4e3ceb9 | 963 | |
e368fb43 JG |
964 | nb_events++; |
965 | } | |
966 | ||
967 | cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) { | |
968 | /* Append extended info. */ | |
3c02e545 | 969 | ret = append_extended_info(uevent->filter_expression, |
e368fb43 | 970 | uevent->exclusion, NULL, payload); |
3c02e545 | 971 | if (ret) { |
e368fb43 | 972 | ERR("Failed to append extended event info to payload"); |
3c02e545 FD |
973 | ret = -LTTNG_ERR_FATAL; |
974 | goto end; | |
975 | } | |
2f77fc4b DG |
976 | } |
977 | ||
e368fb43 | 978 | ret = nb_events; |
d31d3e8c | 979 | end: |
2f77fc4b DG |
980 | rcu_read_unlock(); |
981 | return ret; | |
982 | } | |
983 | ||
984 | /* | |
985 | * Fill lttng_event array of all kernel events in the channel. | |
986 | */ | |
987 | static int list_lttng_kernel_events(char *channel_name, | |
b4e3ceb9 | 988 | struct ltt_kernel_session *kernel_session, |
e368fb43 | 989 | struct lttng_payload *payload) |
2f77fc4b | 990 | { |
e368fb43 | 991 | int ret; |
2f77fc4b | 992 | unsigned int nb_event; |
e368fb43 JG |
993 | const struct ltt_kernel_event *kevent; |
994 | const struct ltt_kernel_channel *kchan; | |
2f77fc4b DG |
995 | |
996 | kchan = trace_kernel_get_channel_by_name(channel_name, kernel_session); | |
997 | if (kchan == NULL) { | |
f73fabfd | 998 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
2f77fc4b DG |
999 | goto error; |
1000 | } | |
1001 | ||
1002 | nb_event = kchan->event_count; | |
1003 | ||
1004 | DBG("Listing events for channel %s", kchan->channel->name); | |
1005 | ||
e368fb43 JG |
1006 | /* Kernel channels */ |
1007 | cds_list_for_each_entry(kevent, &kchan->events_list.head , list) { | |
1008 | struct lttng_event event = {}; | |
2f77fc4b | 1009 | |
43ed1485 JG |
1010 | ret = lttng_strncpy(event.name, kevent->event->name, sizeof(event.name)); |
1011 | if (ret) { | |
1012 | /* Internal error, invalid name. */ | |
1013 | ERR("Invalid event name while listing kernel events: '%s' exceeds the maximal allowed length of %zu bytes", | |
1014 | kevent->event->name, | |
1015 | sizeof(event.name)); | |
1016 | ret = -LTTNG_ERR_UNK; | |
1017 | goto end; | |
1018 | } | |
1019 | ||
e368fb43 JG |
1020 | event.enabled = kevent->enabled; |
1021 | event.filter = (unsigned char) !!kevent->filter_expression; | |
b4e3ceb9 | 1022 | |
e368fb43 | 1023 | switch (kevent->event->instrumentation) { |
2f77fc4b | 1024 | case LTTNG_KERNEL_TRACEPOINT: |
e368fb43 | 1025 | event.type = LTTNG_EVENT_TRACEPOINT; |
2f77fc4b | 1026 | break; |
2f77fc4b | 1027 | case LTTNG_KERNEL_KRETPROBE: |
e368fb43 JG |
1028 | event.type = LTTNG_EVENT_FUNCTION; |
1029 | memcpy(&event.attr.probe, &kevent->event->u.kprobe, | |
1896972b DG |
1030 | sizeof(struct lttng_kernel_kprobe)); |
1031 | break; | |
1032 | case LTTNG_KERNEL_KPROBE: | |
e368fb43 JG |
1033 | event.type = LTTNG_EVENT_PROBE; |
1034 | memcpy(&event.attr.probe, &kevent->event->u.kprobe, | |
2f77fc4b DG |
1035 | sizeof(struct lttng_kernel_kprobe)); |
1036 | break; | |
b955b4d4 | 1037 | case LTTNG_KERNEL_UPROBE: |
e368fb43 | 1038 | event.type = LTTNG_EVENT_USERSPACE_PROBE; |
b955b4d4 | 1039 | break; |
2f77fc4b | 1040 | case LTTNG_KERNEL_FUNCTION: |
e368fb43 JG |
1041 | event.type = LTTNG_EVENT_FUNCTION; |
1042 | memcpy(&event.attr.ftrace, &kevent->event->u.ftrace, | |
2f77fc4b DG |
1043 | sizeof(struct lttng_kernel_function)); |
1044 | break; | |
1045 | case LTTNG_KERNEL_NOOP: | |
e368fb43 | 1046 | event.type = LTTNG_EVENT_NOOP; |
2f77fc4b DG |
1047 | break; |
1048 | case LTTNG_KERNEL_SYSCALL: | |
e368fb43 | 1049 | event.type = LTTNG_EVENT_SYSCALL; |
2f77fc4b DG |
1050 | break; |
1051 | case LTTNG_KERNEL_ALL: | |
1ab8c2ad FD |
1052 | /* fall-through. */ |
1053 | default: | |
2f77fc4b DG |
1054 | assert(0); |
1055 | break; | |
1056 | } | |
b4e3ceb9 | 1057 | |
e368fb43 JG |
1058 | ret = lttng_dynamic_buffer_append( |
1059 | &payload->buffer, &event, sizeof(event)); | |
1060 | if (ret) { | |
1061 | ERR("Failed to append event to payload"); | |
1062 | ret = -LTTNG_ERR_NOMEM; | |
1063 | goto end; | |
1064 | } | |
1065 | } | |
1066 | ||
1067 | cds_list_for_each_entry(kevent, &kchan->events_list.head , list) { | |
1068 | /* Append extended info. */ | |
1069 | ret = append_extended_info(kevent->filter_expression, NULL, | |
1070 | kevent->userspace_probe_location, payload); | |
3c02e545 FD |
1071 | if (ret) { |
1072 | DBG("Error appending extended info message"); | |
1073 | ret = -LTTNG_ERR_FATAL; | |
1074 | goto error; | |
1075 | } | |
2f77fc4b DG |
1076 | } |
1077 | ||
db906c12 | 1078 | end: |
2f77fc4b | 1079 | return nb_event; |
2f77fc4b | 1080 | error: |
6314496f | 1081 | return ret; |
2f77fc4b DG |
1082 | } |
1083 | ||
1084 | /* | |
1085 | * Add URI so the consumer output object. Set the correct path depending on the | |
1086 | * domain adding the default trace directory. | |
1087 | */ | |
b178f53e JG |
1088 | static enum lttng_error_code add_uri_to_consumer( |
1089 | const struct ltt_session *session, | |
1090 | struct consumer_output *consumer, | |
1091 | struct lttng_uri *uri, enum lttng_domain_type domain) | |
2f77fc4b | 1092 | { |
b178f53e JG |
1093 | int ret; |
1094 | enum lttng_error_code ret_code = LTTNG_OK; | |
2f77fc4b DG |
1095 | |
1096 | assert(uri); | |
1097 | ||
1098 | if (consumer == NULL) { | |
1099 | DBG("No consumer detected. Don't add URI. Stopping."); | |
b178f53e | 1100 | ret_code = LTTNG_ERR_NO_CONSUMER; |
2f77fc4b DG |
1101 | goto error; |
1102 | } | |
1103 | ||
1104 | switch (domain) { | |
1105 | case LTTNG_DOMAIN_KERNEL: | |
b178f53e JG |
1106 | ret = lttng_strncpy(consumer->domain_subdir, |
1107 | DEFAULT_KERNEL_TRACE_DIR, | |
1108 | sizeof(consumer->domain_subdir)); | |
2f77fc4b DG |
1109 | break; |
1110 | case LTTNG_DOMAIN_UST: | |
b178f53e JG |
1111 | ret = lttng_strncpy(consumer->domain_subdir, |
1112 | DEFAULT_UST_TRACE_DIR, | |
1113 | sizeof(consumer->domain_subdir)); | |
2f77fc4b DG |
1114 | break; |
1115 | default: | |
1116 | /* | |
b178f53e JG |
1117 | * This case is possible is we try to add the URI to the global |
1118 | * tracing session consumer object which in this case there is | |
1119 | * no subdir. | |
2f77fc4b | 1120 | */ |
b178f53e JG |
1121 | memset(consumer->domain_subdir, 0, |
1122 | sizeof(consumer->domain_subdir)); | |
1123 | ret = 0; | |
1124 | } | |
1125 | if (ret) { | |
1126 | ERR("Failed to initialize consumer output domain subdirectory"); | |
1127 | ret_code = LTTNG_ERR_FATAL; | |
1128 | goto error; | |
2f77fc4b DG |
1129 | } |
1130 | ||
1131 | switch (uri->dtype) { | |
1132 | case LTTNG_DST_IPV4: | |
1133 | case LTTNG_DST_IPV6: | |
1134 | DBG2("Setting network URI to consumer"); | |
1135 | ||
df75acac DG |
1136 | if (consumer->type == CONSUMER_DST_NET) { |
1137 | if ((uri->stype == LTTNG_STREAM_CONTROL && | |
785d2d0d DG |
1138 | consumer->dst.net.control_isset) || |
1139 | (uri->stype == LTTNG_STREAM_DATA && | |
1140 | consumer->dst.net.data_isset)) { | |
b178f53e | 1141 | ret_code = LTTNG_ERR_URL_EXIST; |
df75acac DG |
1142 | goto error; |
1143 | } | |
1144 | } else { | |
b178f53e | 1145 | memset(&consumer->dst, 0, sizeof(consumer->dst)); |
785d2d0d DG |
1146 | } |
1147 | ||
2f77fc4b | 1148 | /* Set URI into consumer output object */ |
b178f53e | 1149 | ret = consumer_set_network_uri(session, consumer, uri); |
2f77fc4b | 1150 | if (ret < 0) { |
b178f53e | 1151 | ret_code = -ret; |
2f77fc4b DG |
1152 | goto error; |
1153 | } else if (ret == 1) { | |
1154 | /* | |
1155 | * URI was the same in the consumer so we do not append the subdir | |
1156 | * again so to not duplicate output dir. | |
1157 | */ | |
b178f53e | 1158 | ret_code = LTTNG_OK; |
2f77fc4b DG |
1159 | goto error; |
1160 | } | |
2f77fc4b DG |
1161 | break; |
1162 | case LTTNG_DST_PATH: | |
b178f53e JG |
1163 | if (*uri->dst.path != '/' || strstr(uri->dst.path, "../")) { |
1164 | ret_code = LTTNG_ERR_INVALID; | |
9ac05d92 MD |
1165 | goto error; |
1166 | } | |
b178f53e JG |
1167 | DBG2("Setting trace directory path from URI to %s", |
1168 | uri->dst.path); | |
1169 | memset(&consumer->dst, 0, sizeof(consumer->dst)); | |
1170 | ||
1171 | ret = lttng_strncpy(consumer->dst.session_root_path, | |
1172 | uri->dst.path, | |
1173 | sizeof(consumer->dst.session_root_path)); | |
4df41cad JG |
1174 | if (ret) { |
1175 | ret_code = LTTNG_ERR_FATAL; | |
1176 | goto error; | |
1177 | } | |
2f77fc4b DG |
1178 | consumer->type = CONSUMER_DST_LOCAL; |
1179 | break; | |
1180 | } | |
1181 | ||
b178f53e | 1182 | ret_code = LTTNG_OK; |
2f77fc4b | 1183 | error: |
b178f53e | 1184 | return ret_code; |
2f77fc4b DG |
1185 | } |
1186 | ||
1187 | /* | |
1188 | * Init tracing by creating trace directory and sending fds kernel consumer. | |
1189 | */ | |
1190 | static int init_kernel_tracing(struct ltt_kernel_session *session) | |
1191 | { | |
1192 | int ret = 0; | |
1193 | struct lttng_ht_iter iter; | |
1194 | struct consumer_socket *socket; | |
1195 | ||
1196 | assert(session); | |
1197 | ||
e7fe706f DG |
1198 | rcu_read_lock(); |
1199 | ||
2f77fc4b DG |
1200 | if (session->consumer_fds_sent == 0 && session->consumer != NULL) { |
1201 | cds_lfht_for_each_entry(session->consumer->socks->ht, &iter.iter, | |
1202 | socket, node.node) { | |
2f77fc4b | 1203 | pthread_mutex_lock(socket->lock); |
f50f23d9 | 1204 | ret = kernel_consumer_send_session(socket, session); |
2f77fc4b DG |
1205 | pthread_mutex_unlock(socket->lock); |
1206 | if (ret < 0) { | |
f73fabfd | 1207 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
2f77fc4b DG |
1208 | goto error; |
1209 | } | |
1210 | } | |
1211 | } | |
1212 | ||
1213 | error: | |
e7fe706f | 1214 | rcu_read_unlock(); |
2f77fc4b DG |
1215 | return ret; |
1216 | } | |
1217 | ||
1218 | /* | |
1219 | * Create a socket to the relayd using the URI. | |
1220 | * | |
1221 | * On success, the relayd_sock pointer is set to the created socket. | |
9a654598 | 1222 | * Else, it remains untouched and an LTTng error code is returned. |
2f77fc4b | 1223 | */ |
9a654598 | 1224 | static enum lttng_error_code create_connect_relayd(struct lttng_uri *uri, |
b31610f2 JD |
1225 | struct lttcomm_relayd_sock **relayd_sock, |
1226 | struct consumer_output *consumer) | |
2f77fc4b DG |
1227 | { |
1228 | int ret; | |
9a654598 | 1229 | enum lttng_error_code status = LTTNG_OK; |
6151a90f | 1230 | struct lttcomm_relayd_sock *rsock; |
2f77fc4b | 1231 | |
6151a90f JD |
1232 | rsock = lttcomm_alloc_relayd_sock(uri, RELAYD_VERSION_COMM_MAJOR, |
1233 | RELAYD_VERSION_COMM_MINOR); | |
1234 | if (!rsock) { | |
9a654598 | 1235 | status = LTTNG_ERR_FATAL; |
2f77fc4b DG |
1236 | goto error; |
1237 | } | |
1238 | ||
ffe60014 DG |
1239 | /* |
1240 | * Connect to relayd so we can proceed with a session creation. This call | |
1241 | * can possibly block for an arbitrary amount of time to set the health | |
1242 | * state to be in poll execution. | |
1243 | */ | |
1244 | health_poll_entry(); | |
6151a90f | 1245 | ret = relayd_connect(rsock); |
ffe60014 | 1246 | health_poll_exit(); |
2f77fc4b DG |
1247 | if (ret < 0) { |
1248 | ERR("Unable to reach lttng-relayd"); | |
9a654598 | 1249 | status = LTTNG_ERR_RELAYD_CONNECT_FAIL; |
2f77fc4b DG |
1250 | goto free_sock; |
1251 | } | |
1252 | ||
1253 | /* Create socket for control stream. */ | |
1254 | if (uri->stype == LTTNG_STREAM_CONTROL) { | |
eacb7b6f MD |
1255 | uint64_t result_flags; |
1256 | ||
2f77fc4b DG |
1257 | DBG3("Creating relayd stream socket from URI"); |
1258 | ||
1259 | /* Check relayd version */ | |
6151a90f | 1260 | ret = relayd_version_check(rsock); |
67d5aa28 | 1261 | if (ret == LTTNG_ERR_RELAYD_VERSION_FAIL) { |
9a654598 | 1262 | status = LTTNG_ERR_RELAYD_VERSION_FAIL; |
67d5aa28 JD |
1263 | goto close_sock; |
1264 | } else if (ret < 0) { | |
1265 | ERR("Unable to reach lttng-relayd"); | |
9a654598 | 1266 | status = LTTNG_ERR_RELAYD_CONNECT_FAIL; |
2f77fc4b DG |
1267 | goto close_sock; |
1268 | } | |
b31610f2 JD |
1269 | consumer->relay_major_version = rsock->major; |
1270 | consumer->relay_minor_version = rsock->minor; | |
eacb7b6f MD |
1271 | ret = relayd_get_configuration(rsock, 0, |
1272 | &result_flags); | |
1273 | if (ret < 0) { | |
1274 | ERR("Unable to get relayd configuration"); | |
1275 | status = LTTNG_ERR_RELAYD_CONNECT_FAIL; | |
1276 | goto close_sock; | |
1277 | } | |
1278 | if (result_flags & LTTCOMM_RELAYD_CONFIGURATION_FLAG_CLEAR_ALLOWED) { | |
1279 | consumer->relay_allows_clear = true; | |
1280 | } | |
2f77fc4b DG |
1281 | } else if (uri->stype == LTTNG_STREAM_DATA) { |
1282 | DBG3("Creating relayd data socket from URI"); | |
1283 | } else { | |
1284 | /* Command is not valid */ | |
1285 | ERR("Relayd invalid stream type: %d", uri->stype); | |
9a654598 | 1286 | status = LTTNG_ERR_INVALID; |
2f77fc4b DG |
1287 | goto close_sock; |
1288 | } | |
1289 | ||
6151a90f | 1290 | *relayd_sock = rsock; |
2f77fc4b | 1291 | |
9a654598 | 1292 | return status; |
2f77fc4b DG |
1293 | |
1294 | close_sock: | |
6151a90f JD |
1295 | /* The returned value is not useful since we are on an error path. */ |
1296 | (void) relayd_close(rsock); | |
2f77fc4b | 1297 | free_sock: |
6151a90f | 1298 | free(rsock); |
2f77fc4b | 1299 | error: |
9a654598 | 1300 | return status; |
2f77fc4b DG |
1301 | } |
1302 | ||
1303 | /* | |
1304 | * Connect to the relayd using URI and send the socket to the right consumer. | |
43fade62 JG |
1305 | * |
1306 | * The consumer socket lock must be held by the caller. | |
9a654598 JG |
1307 | * |
1308 | * Returns LTTNG_OK on success or an LTTng error code on failure. | |
2f77fc4b | 1309 | */ |
9a654598 JG |
1310 | static enum lttng_error_code send_consumer_relayd_socket( |
1311 | unsigned int session_id, | |
3044f922 | 1312 | struct lttng_uri *relayd_uri, |
56a37563 | 1313 | struct consumer_output *consumer, |
d3e2ba59 | 1314 | struct consumer_socket *consumer_sock, |
fb9a95c4 | 1315 | const char *session_name, const char *hostname, |
6fa5fe7c | 1316 | const char *base_path, int session_live_timer, |
0e270a1e | 1317 | const uint64_t *current_chunk_id, |
46ef2188 MD |
1318 | time_t session_creation_time, |
1319 | bool session_name_contains_creation_time) | |
2f77fc4b DG |
1320 | { |
1321 | int ret; | |
6151a90f | 1322 | struct lttcomm_relayd_sock *rsock = NULL; |
9a654598 | 1323 | enum lttng_error_code status; |
2f77fc4b | 1324 | |
ffe60014 | 1325 | /* Connect to relayd and make version check if uri is the control. */ |
9a654598 JG |
1326 | status = create_connect_relayd(relayd_uri, &rsock, consumer); |
1327 | if (status != LTTNG_OK) { | |
9e218353 | 1328 | goto relayd_comm_error; |
ffe60014 | 1329 | } |
6151a90f | 1330 | assert(rsock); |
ffe60014 | 1331 | |
2f77fc4b | 1332 | /* Set the network sequence index if not set. */ |
d88aee68 DG |
1333 | if (consumer->net_seq_index == (uint64_t) -1ULL) { |
1334 | pthread_mutex_lock(&relayd_net_seq_idx_lock); | |
2f77fc4b DG |
1335 | /* |
1336 | * Increment net_seq_idx because we are about to transfer the | |
1337 | * new relayd socket to the consumer. | |
d88aee68 | 1338 | * Assign unique key so the consumer can match streams. |
2f77fc4b | 1339 | */ |
d88aee68 DG |
1340 | consumer->net_seq_index = ++relayd_net_seq_idx; |
1341 | pthread_mutex_unlock(&relayd_net_seq_idx_lock); | |
2f77fc4b DG |
1342 | } |
1343 | ||
2f77fc4b | 1344 | /* Send relayd socket to consumer. */ |
6151a90f | 1345 | ret = consumer_send_relayd_socket(consumer_sock, rsock, consumer, |
d3e2ba59 | 1346 | relayd_uri->stype, session_id, |
6fa5fe7c MD |
1347 | session_name, hostname, base_path, |
1348 | session_live_timer, current_chunk_id, | |
46ef2188 | 1349 | session_creation_time, session_name_contains_creation_time); |
2f77fc4b | 1350 | if (ret < 0) { |
9a654598 | 1351 | status = LTTNG_ERR_ENABLE_CONSUMER_FAIL; |
2f77fc4b DG |
1352 | goto close_sock; |
1353 | } | |
1354 | ||
c890b720 DG |
1355 | /* Flag that the corresponding socket was sent. */ |
1356 | if (relayd_uri->stype == LTTNG_STREAM_CONTROL) { | |
ffe60014 | 1357 | consumer_sock->control_sock_sent = 1; |
c890b720 | 1358 | } else if (relayd_uri->stype == LTTNG_STREAM_DATA) { |
ffe60014 | 1359 | consumer_sock->data_sock_sent = 1; |
c890b720 DG |
1360 | } |
1361 | ||
2f77fc4b DG |
1362 | /* |
1363 | * Close socket which was dup on the consumer side. The session daemon does | |
1364 | * NOT keep track of the relayd socket(s) once transfer to the consumer. | |
1365 | */ | |
1366 | ||
1367 | close_sock: | |
9a654598 | 1368 | if (status != LTTNG_OK) { |
ffe60014 | 1369 | /* |
d9078d0c DG |
1370 | * The consumer output for this session should not be used anymore |
1371 | * since the relayd connection failed thus making any tracing or/and | |
1372 | * streaming not usable. | |
ffe60014 | 1373 | */ |
d9078d0c | 1374 | consumer->enabled = 0; |
ffe60014 | 1375 | } |
9e218353 JR |
1376 | (void) relayd_close(rsock); |
1377 | free(rsock); | |
1378 | ||
1379 | relayd_comm_error: | |
9a654598 | 1380 | return status; |
2f77fc4b DG |
1381 | } |
1382 | ||
1383 | /* | |
1384 | * Send both relayd sockets to a specific consumer and domain. This is a | |
1385 | * helper function to facilitate sending the information to the consumer for a | |
1386 | * session. | |
43fade62 JG |
1387 | * |
1388 | * The consumer socket lock must be held by the caller. | |
9a654598 JG |
1389 | * |
1390 | * Returns LTTNG_OK, or an LTTng error code on failure. | |
2f77fc4b | 1391 | */ |
9a654598 JG |
1392 | static enum lttng_error_code send_consumer_relayd_sockets( |
1393 | enum lttng_domain_type domain, | |
56a37563 | 1394 | unsigned int session_id, struct consumer_output *consumer, |
fb9a95c4 | 1395 | struct consumer_socket *sock, const char *session_name, |
6fa5fe7c | 1396 | const char *hostname, const char *base_path, int session_live_timer, |
46ef2188 MD |
1397 | const uint64_t *current_chunk_id, time_t session_creation_time, |
1398 | bool session_name_contains_creation_time) | |
2f77fc4b | 1399 | { |
9a654598 | 1400 | enum lttng_error_code status = LTTNG_OK; |
2f77fc4b | 1401 | |
2f77fc4b | 1402 | assert(consumer); |
6dc3064a | 1403 | assert(sock); |
2f77fc4b | 1404 | |
2f77fc4b | 1405 | /* Sending control relayd socket. */ |
ffe60014 | 1406 | if (!sock->control_sock_sent) { |
9a654598 | 1407 | status = send_consumer_relayd_socket(session_id, |
d3e2ba59 | 1408 | &consumer->dst.net.control, consumer, sock, |
6fa5fe7c | 1409 | session_name, hostname, base_path, session_live_timer, |
46ef2188 MD |
1410 | current_chunk_id, session_creation_time, |
1411 | session_name_contains_creation_time); | |
9a654598 | 1412 | if (status != LTTNG_OK) { |
c890b720 DG |
1413 | goto error; |
1414 | } | |
2f77fc4b DG |
1415 | } |
1416 | ||
1417 | /* Sending data relayd socket. */ | |
ffe60014 | 1418 | if (!sock->data_sock_sent) { |
9a654598 | 1419 | status = send_consumer_relayd_socket(session_id, |
d3e2ba59 | 1420 | &consumer->dst.net.data, consumer, sock, |
6fa5fe7c | 1421 | session_name, hostname, base_path, session_live_timer, |
46ef2188 MD |
1422 | current_chunk_id, session_creation_time, |
1423 | session_name_contains_creation_time); | |
9a654598 | 1424 | if (status != LTTNG_OK) { |
c890b720 DG |
1425 | goto error; |
1426 | } | |
2f77fc4b DG |
1427 | } |
1428 | ||
2f77fc4b | 1429 | error: |
9a654598 | 1430 | return status; |
2f77fc4b DG |
1431 | } |
1432 | ||
1433 | /* | |
1434 | * Setup relayd connections for a tracing session. First creates the socket to | |
1435 | * the relayd and send them to the right domain consumer. Consumer type MUST be | |
1436 | * network. | |
1437 | */ | |
ffe60014 | 1438 | int cmd_setup_relayd(struct ltt_session *session) |
2f77fc4b | 1439 | { |
f73fabfd | 1440 | int ret = LTTNG_OK; |
2f77fc4b DG |
1441 | struct ltt_ust_session *usess; |
1442 | struct ltt_kernel_session *ksess; | |
1443 | struct consumer_socket *socket; | |
1444 | struct lttng_ht_iter iter; | |
0e270a1e | 1445 | LTTNG_OPTIONAL(uint64_t) current_chunk_id = {}; |
2f77fc4b | 1446 | |
0e270a1e | 1447 | assert(session); |
2f77fc4b DG |
1448 | |
1449 | usess = session->ust_session; | |
1450 | ksess = session->kernel_session; | |
1451 | ||
785d2d0d | 1452 | DBG("Setting relayd for session %s", session->name); |
2f77fc4b | 1453 | |
aa997ea3 | 1454 | rcu_read_lock(); |
1e791a74 JG |
1455 | if (session->current_trace_chunk) { |
1456 | enum lttng_trace_chunk_status status = lttng_trace_chunk_get_id( | |
1457 | session->current_trace_chunk, ¤t_chunk_id.value); | |
1458 | ||
1459 | if (status == LTTNG_TRACE_CHUNK_STATUS_OK) { | |
1460 | current_chunk_id.is_set = true; | |
1461 | } else { | |
1462 | ERR("Failed to get current trace chunk id"); | |
1463 | ret = LTTNG_ERR_UNK; | |
1464 | goto error; | |
1465 | } | |
1466 | } | |
1467 | ||
2f77fc4b DG |
1468 | if (usess && usess->consumer && usess->consumer->type == CONSUMER_DST_NET |
1469 | && usess->consumer->enabled) { | |
1470 | /* For each consumer socket, send relayd sockets */ | |
1471 | cds_lfht_for_each_entry(usess->consumer->socks->ht, &iter.iter, | |
1472 | socket, node.node) { | |
2f77fc4b | 1473 | pthread_mutex_lock(socket->lock); |
6dc3064a | 1474 | ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_UST, session->id, |
d3e2ba59 JD |
1475 | usess->consumer, socket, |
1476 | session->name, session->hostname, | |
6fa5fe7c | 1477 | session->base_path, |
1e791a74 | 1478 | session->live_timer, |
db1da059 | 1479 | current_chunk_id.is_set ? ¤t_chunk_id.value : NULL, |
46ef2188 MD |
1480 | session->creation_time, |
1481 | session->name_contains_creation_time); | |
2f77fc4b | 1482 | pthread_mutex_unlock(socket->lock); |
f73fabfd | 1483 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1484 | goto error; |
1485 | } | |
6dc3064a DG |
1486 | /* Session is now ready for network streaming. */ |
1487 | session->net_handle = 1; | |
2f77fc4b | 1488 | } |
b31610f2 JD |
1489 | session->consumer->relay_major_version = |
1490 | usess->consumer->relay_major_version; | |
1491 | session->consumer->relay_minor_version = | |
1492 | usess->consumer->relay_minor_version; | |
eacb7b6f MD |
1493 | session->consumer->relay_allows_clear = |
1494 | usess->consumer->relay_allows_clear; | |
2f77fc4b DG |
1495 | } |
1496 | ||
1497 | if (ksess && ksess->consumer && ksess->consumer->type == CONSUMER_DST_NET | |
1498 | && ksess->consumer->enabled) { | |
1499 | cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter, | |
1500 | socket, node.node) { | |
2f77fc4b | 1501 | pthread_mutex_lock(socket->lock); |
6dc3064a | 1502 | ret = send_consumer_relayd_sockets(LTTNG_DOMAIN_KERNEL, session->id, |
d3e2ba59 JD |
1503 | ksess->consumer, socket, |
1504 | session->name, session->hostname, | |
6fa5fe7c | 1505 | session->base_path, |
1e791a74 | 1506 | session->live_timer, |
db1da059 | 1507 | current_chunk_id.is_set ? ¤t_chunk_id.value : NULL, |
46ef2188 MD |
1508 | session->creation_time, |
1509 | session->name_contains_creation_time); | |
2f77fc4b | 1510 | pthread_mutex_unlock(socket->lock); |
f73fabfd | 1511 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1512 | goto error; |
1513 | } | |
6dc3064a DG |
1514 | /* Session is now ready for network streaming. */ |
1515 | session->net_handle = 1; | |
2f77fc4b | 1516 | } |
b31610f2 JD |
1517 | session->consumer->relay_major_version = |
1518 | ksess->consumer->relay_major_version; | |
1519 | session->consumer->relay_minor_version = | |
1520 | ksess->consumer->relay_minor_version; | |
eacb7b6f MD |
1521 | session->consumer->relay_allows_clear = |
1522 | ksess->consumer->relay_allows_clear; | |
2f77fc4b DG |
1523 | } |
1524 | ||
1525 | error: | |
e7fe706f | 1526 | rcu_read_unlock(); |
2f77fc4b DG |
1527 | return ret; |
1528 | } | |
1529 | ||
9b6c7ec5 DG |
1530 | /* |
1531 | * Start a kernel session by opening all necessary streams. | |
1532 | */ | |
4dbe1875 | 1533 | int start_kernel_session(struct ltt_kernel_session *ksess) |
9b6c7ec5 DG |
1534 | { |
1535 | int ret; | |
1536 | struct ltt_kernel_channel *kchan; | |
1537 | ||
1538 | /* Open kernel metadata */ | |
07b86b52 | 1539 | if (ksess->metadata == NULL && ksess->output_traces) { |
9b6c7ec5 DG |
1540 | ret = kernel_open_metadata(ksess); |
1541 | if (ret < 0) { | |
1542 | ret = LTTNG_ERR_KERN_META_FAIL; | |
1543 | goto error; | |
1544 | } | |
1545 | } | |
1546 | ||
1547 | /* Open kernel metadata stream */ | |
07b86b52 | 1548 | if (ksess->metadata && ksess->metadata_stream_fd < 0) { |
9b6c7ec5 DG |
1549 | ret = kernel_open_metadata_stream(ksess); |
1550 | if (ret < 0) { | |
1551 | ERR("Kernel create metadata stream failed"); | |
1552 | ret = LTTNG_ERR_KERN_STREAM_FAIL; | |
1553 | goto error; | |
1554 | } | |
1555 | } | |
1556 | ||
1557 | /* For each channel */ | |
1558 | cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) { | |
1559 | if (kchan->stream_count == 0) { | |
1560 | ret = kernel_open_channel_stream(kchan); | |
1561 | if (ret < 0) { | |
1562 | ret = LTTNG_ERR_KERN_STREAM_FAIL; | |
1563 | goto error; | |
1564 | } | |
1565 | /* Update the stream global counter */ | |
1566 | ksess->stream_count_global += ret; | |
1567 | } | |
1568 | } | |
1569 | ||
1570 | /* Setup kernel consumer socket and send fds to it */ | |
1571 | ret = init_kernel_tracing(ksess); | |
e43c41c5 | 1572 | if (ret != 0) { |
9b6c7ec5 DG |
1573 | ret = LTTNG_ERR_KERN_START_FAIL; |
1574 | goto error; | |
1575 | } | |
1576 | ||
1577 | /* This start the kernel tracing */ | |
1578 | ret = kernel_start_session(ksess); | |
1579 | if (ret < 0) { | |
1580 | ret = LTTNG_ERR_KERN_START_FAIL; | |
1581 | goto error; | |
1582 | } | |
1583 | ||
1584 | /* Quiescent wait after starting trace */ | |
7d268848 | 1585 | kernel_wait_quiescent(); |
9b6c7ec5 | 1586 | |
14fb1ebe | 1587 | ksess->active = 1; |
9b6c7ec5 DG |
1588 | |
1589 | ret = LTTNG_OK; | |
1590 | ||
1591 | error: | |
1592 | return ret; | |
1593 | } | |
1594 | ||
4dbe1875 MD |
1595 | int stop_kernel_session(struct ltt_kernel_session *ksess) |
1596 | { | |
1597 | struct ltt_kernel_channel *kchan; | |
1598 | bool error_occurred = false; | |
1599 | int ret; | |
1600 | ||
1601 | if (!ksess || !ksess->active) { | |
1602 | return LTTNG_OK; | |
1603 | } | |
1604 | DBG("Stopping kernel tracing"); | |
1605 | ||
1606 | ret = kernel_stop_session(ksess); | |
1607 | if (ret < 0) { | |
1608 | ret = LTTNG_ERR_KERN_STOP_FAIL; | |
1609 | goto error; | |
1610 | } | |
1611 | ||
1612 | kernel_wait_quiescent(); | |
1613 | ||
1614 | /* Flush metadata after stopping (if exists) */ | |
1615 | if (ksess->metadata_stream_fd >= 0) { | |
1616 | ret = kernel_metadata_flush_buffer(ksess->metadata_stream_fd); | |
1617 | if (ret < 0) { | |
1618 | ERR("Kernel metadata flush failed"); | |
1619 | error_occurred = true; | |
1620 | } | |
1621 | } | |
1622 | ||
1623 | /* Flush all buffers after stopping */ | |
1624 | cds_list_for_each_entry(kchan, &ksess->channel_list.head, list) { | |
1625 | ret = kernel_flush_buffer(kchan); | |
1626 | if (ret < 0) { | |
1627 | ERR("Kernel flush buffer error"); | |
1628 | error_occurred = true; | |
1629 | } | |
1630 | } | |
1631 | ||
1632 | ksess->active = 0; | |
1633 | if (error_occurred) { | |
1634 | ret = LTTNG_ERR_UNK; | |
1635 | } else { | |
1636 | ret = LTTNG_OK; | |
1637 | } | |
1638 | error: | |
1639 | return ret; | |
1640 | } | |
1641 | ||
2f77fc4b DG |
1642 | /* |
1643 | * Command LTTNG_DISABLE_CHANNEL processed by the client thread. | |
1644 | */ | |
56a37563 JG |
1645 | int cmd_disable_channel(struct ltt_session *session, |
1646 | enum lttng_domain_type domain, char *channel_name) | |
2f77fc4b DG |
1647 | { |
1648 | int ret; | |
1649 | struct ltt_ust_session *usess; | |
1650 | ||
1651 | usess = session->ust_session; | |
1652 | ||
2223c96f DG |
1653 | rcu_read_lock(); |
1654 | ||
2f77fc4b DG |
1655 | switch (domain) { |
1656 | case LTTNG_DOMAIN_KERNEL: | |
1657 | { | |
1658 | ret = channel_kernel_disable(session->kernel_session, | |
1659 | channel_name); | |
f73fabfd | 1660 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1661 | goto error; |
1662 | } | |
1663 | ||
7d268848 | 1664 | kernel_wait_quiescent(); |
2f77fc4b DG |
1665 | break; |
1666 | } | |
1667 | case LTTNG_DOMAIN_UST: | |
1668 | { | |
1669 | struct ltt_ust_channel *uchan; | |
1670 | struct lttng_ht *chan_ht; | |
1671 | ||
1672 | chan_ht = usess->domain_global.channels; | |
1673 | ||
1674 | uchan = trace_ust_find_channel_by_name(chan_ht, channel_name); | |
1675 | if (uchan == NULL) { | |
f73fabfd | 1676 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
2f77fc4b DG |
1677 | goto error; |
1678 | } | |
1679 | ||
7972aab2 | 1680 | ret = channel_ust_disable(usess, uchan); |
f73fabfd | 1681 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1682 | goto error; |
1683 | } | |
1684 | break; | |
1685 | } | |
2f77fc4b | 1686 | default: |
f73fabfd | 1687 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
2f77fc4b DG |
1688 | goto error; |
1689 | } | |
1690 | ||
f73fabfd | 1691 | ret = LTTNG_OK; |
2f77fc4b DG |
1692 | |
1693 | error: | |
2223c96f | 1694 | rcu_read_unlock(); |
2f77fc4b DG |
1695 | return ret; |
1696 | } | |
1697 | ||
1698 | /* | |
1699 | * Command LTTNG_ENABLE_CHANNEL processed by the client thread. | |
1700 | * | |
1701 | * The wpipe arguments is used as a notifier for the kernel thread. | |
1702 | */ | |
1703 | int cmd_enable_channel(struct ltt_session *session, | |
df4f5a87 | 1704 | const struct lttng_domain *domain, const struct lttng_channel *_attr, int wpipe) |
2f77fc4b DG |
1705 | { |
1706 | int ret; | |
1707 | struct ltt_ust_session *usess = session->ust_session; | |
1708 | struct lttng_ht *chan_ht; | |
1f345e94 | 1709 | size_t len; |
df4f5a87 | 1710 | struct lttng_channel attr; |
2f77fc4b DG |
1711 | |
1712 | assert(session); | |
df4f5a87 | 1713 | assert(_attr); |
7972aab2 | 1714 | assert(domain); |
2f77fc4b | 1715 | |
df4f5a87 JG |
1716 | attr = *_attr; |
1717 | len = lttng_strnlen(attr.name, sizeof(attr.name)); | |
1f345e94 PP |
1718 | |
1719 | /* Validate channel name */ | |
df4f5a87 JG |
1720 | if (attr.name[0] == '.' || |
1721 | memchr(attr.name, '/', len) != NULL) { | |
1f345e94 PP |
1722 | ret = LTTNG_ERR_INVALID_CHANNEL_NAME; |
1723 | goto end; | |
1724 | } | |
1725 | ||
df4f5a87 | 1726 | DBG("Enabling channel %s for session %s", attr.name, session->name); |
2f77fc4b | 1727 | |
03b4fdcf DG |
1728 | rcu_read_lock(); |
1729 | ||
ecc48a90 JD |
1730 | /* |
1731 | * Don't try to enable a channel if the session has been started at | |
1732 | * some point in time before. The tracer does not allow it. | |
1733 | */ | |
8382cf6f | 1734 | if (session->has_been_started) { |
ecc48a90 JD |
1735 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
1736 | goto error; | |
1737 | } | |
1738 | ||
1739 | /* | |
1740 | * If the session is a live session, remove the switch timer, the | |
1741 | * live timer does the same thing but sends also synchronisation | |
1742 | * beacons for inactive streams. | |
1743 | */ | |
1744 | if (session->live_timer > 0) { | |
df4f5a87 JG |
1745 | attr.attr.live_timer_interval = session->live_timer; |
1746 | attr.attr.switch_timer_interval = 0; | |
ecc48a90 JD |
1747 | } |
1748 | ||
6e21424e JR |
1749 | /* Check for feature support */ |
1750 | switch (domain->type) { | |
1751 | case LTTNG_DOMAIN_KERNEL: | |
1752 | { | |
7d268848 | 1753 | if (kernel_supports_ring_buffer_snapshot_sample_positions() != 1) { |
6e21424e JR |
1754 | /* Sampling position of buffer is not supported */ |
1755 | WARN("Kernel tracer does not support buffer monitoring. " | |
1756 | "Setting the monitor interval timer to 0 " | |
1757 | "(disabled) for channel '%s' of session '%s'", | |
df4f5a87 JG |
1758 | attr.name, session->name); |
1759 | lttng_channel_set_monitor_timer_interval(&attr, 0); | |
6e21424e JR |
1760 | } |
1761 | break; | |
1762 | } | |
1763 | case LTTNG_DOMAIN_UST: | |
f28f9e44 | 1764 | break; |
6e21424e JR |
1765 | case LTTNG_DOMAIN_JUL: |
1766 | case LTTNG_DOMAIN_LOG4J: | |
1767 | case LTTNG_DOMAIN_PYTHON: | |
f28f9e44 JG |
1768 | if (!agent_tracing_is_enabled()) { |
1769 | DBG("Attempted to enable a channel in an agent domain but the agent thread is not running"); | |
1770 | ret = LTTNG_ERR_AGENT_TRACING_DISABLED; | |
1771 | goto error; | |
1772 | } | |
6e21424e JR |
1773 | break; |
1774 | default: | |
1775 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; | |
1776 | goto error; | |
1777 | } | |
1778 | ||
7972aab2 | 1779 | switch (domain->type) { |
2f77fc4b DG |
1780 | case LTTNG_DOMAIN_KERNEL: |
1781 | { | |
1782 | struct ltt_kernel_channel *kchan; | |
1783 | ||
df4f5a87 | 1784 | kchan = trace_kernel_get_channel_by_name(attr.name, |
2f77fc4b DG |
1785 | session->kernel_session); |
1786 | if (kchan == NULL) { | |
54213acc JG |
1787 | if (session->snapshot.nb_output > 0 || |
1788 | session->snapshot_mode) { | |
1789 | /* Enforce mmap output for snapshot sessions. */ | |
df4f5a87 | 1790 | attr.attr.output = LTTNG_EVENT_MMAP; |
54213acc | 1791 | } |
df4f5a87 JG |
1792 | ret = channel_kernel_create(session->kernel_session, &attr, wpipe); |
1793 | if (attr.name[0] != '\0') { | |
85076754 MD |
1794 | session->kernel_session->has_non_default_channel = 1; |
1795 | } | |
2f77fc4b DG |
1796 | } else { |
1797 | ret = channel_kernel_enable(session->kernel_session, kchan); | |
1798 | } | |
1799 | ||
f73fabfd | 1800 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
1801 | goto error; |
1802 | } | |
1803 | ||
7d268848 | 1804 | kernel_wait_quiescent(); |
2f77fc4b DG |
1805 | break; |
1806 | } | |
1807 | case LTTNG_DOMAIN_UST: | |
9232818f JG |
1808 | case LTTNG_DOMAIN_JUL: |
1809 | case LTTNG_DOMAIN_LOG4J: | |
1810 | case LTTNG_DOMAIN_PYTHON: | |
2f77fc4b DG |
1811 | { |
1812 | struct ltt_ust_channel *uchan; | |
1813 | ||
9232818f JG |
1814 | /* |
1815 | * FIXME | |
1816 | * | |
1817 | * Current agent implementation limitations force us to allow | |
1818 | * only one channel at once in "agent" subdomains. Each | |
1819 | * subdomain has a default channel name which must be strictly | |
1820 | * adhered to. | |
1821 | */ | |
1822 | if (domain->type == LTTNG_DOMAIN_JUL) { | |
df4f5a87 | 1823 | if (strncmp(attr.name, DEFAULT_JUL_CHANNEL_NAME, |
9232818f JG |
1824 | LTTNG_SYMBOL_NAME_LEN)) { |
1825 | ret = LTTNG_ERR_INVALID_CHANNEL_NAME; | |
1826 | goto error; | |
1827 | } | |
1828 | } else if (domain->type == LTTNG_DOMAIN_LOG4J) { | |
df4f5a87 | 1829 | if (strncmp(attr.name, DEFAULT_LOG4J_CHANNEL_NAME, |
9232818f JG |
1830 | LTTNG_SYMBOL_NAME_LEN)) { |
1831 | ret = LTTNG_ERR_INVALID_CHANNEL_NAME; | |
1832 | goto error; | |
1833 | } | |
1834 | } else if (domain->type == LTTNG_DOMAIN_PYTHON) { | |
df4f5a87 | 1835 | if (strncmp(attr.name, DEFAULT_PYTHON_CHANNEL_NAME, |
9232818f JG |
1836 | LTTNG_SYMBOL_NAME_LEN)) { |
1837 | ret = LTTNG_ERR_INVALID_CHANNEL_NAME; | |
1838 | goto error; | |
1839 | } | |
1840 | } | |
1841 | ||
2f77fc4b DG |
1842 | chan_ht = usess->domain_global.channels; |
1843 | ||
df4f5a87 | 1844 | uchan = trace_ust_find_channel_by_name(chan_ht, attr.name); |
2f77fc4b | 1845 | if (uchan == NULL) { |
df4f5a87 JG |
1846 | ret = channel_ust_create(usess, &attr, domain->buf_type); |
1847 | if (attr.name[0] != '\0') { | |
85076754 MD |
1848 | usess->has_non_default_channel = 1; |
1849 | } | |
2f77fc4b | 1850 | } else { |
7972aab2 | 1851 | ret = channel_ust_enable(usess, uchan); |
2f77fc4b DG |
1852 | } |
1853 | break; | |
1854 | } | |
2f77fc4b | 1855 | default: |
f73fabfd | 1856 | ret = LTTNG_ERR_UNKNOWN_DOMAIN; |
2f77fc4b DG |
1857 | goto error; |
1858 | } | |
1859 | ||
df4f5a87 | 1860 | if (ret == LTTNG_OK && attr.attr.output != LTTNG_EVENT_MMAP) { |
54213acc JG |
1861 | session->has_non_mmap_channel = true; |
1862 | } | |
2f77fc4b | 1863 | error: |
2223c96f | 1864 | rcu_read_unlock(); |
1f345e94 | 1865 | end: |
2f77fc4b DG |
1866 | return ret; |
1867 | } | |
1868 | ||
c3e68e71 JR |
1869 | enum lttng_error_code cmd_add_map(struct command_ctx *cmd_ctx, int sock) |
1870 | { | |
1871 | int ret; | |
1872 | enum lttng_error_code ret_code; | |
1873 | size_t map_len; | |
1874 | struct lttng_payload map_payload; | |
1875 | ssize_t sock_recv_len; | |
1876 | struct lttng_map *map = NULL; | |
1877 | const struct lttng_credentials cmd_creds = { | |
1878 | .uid = LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx->creds.uid), | |
1879 | .gid = LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx->creds.gid), | |
1880 | }; | |
1881 | ||
1882 | lttng_payload_init(&map_payload); | |
1883 | map_len = (size_t) cmd_ctx->lsm.u.add_map.length; | |
1884 | ret = lttng_dynamic_buffer_set_size( | |
1885 | &map_payload.buffer, map_len); | |
1886 | if (ret) { | |
1887 | ret_code = LTTNG_ERR_NOMEM; | |
1888 | goto end; | |
1889 | } | |
1890 | ||
1891 | sock_recv_len = lttcomm_recv_unix_sock( | |
1892 | sock, map_payload.buffer.data, map_len); | |
1893 | if (sock_recv_len < 0 || sock_recv_len != map_len) { | |
1894 | ERR("Failed to receive \"register map\" command payload"); | |
1895 | ret_code = LTTNG_ERR_INVALID_MAP; | |
1896 | goto end; | |
1897 | } | |
1898 | ||
1899 | /* Deserialize map. */ | |
1900 | { | |
1901 | struct lttng_payload_view view = | |
1902 | lttng_payload_view_from_payload( | |
1903 | &map_payload, 0, -1); | |
1904 | ||
1905 | if (lttng_map_create_from_payload(&view, &map) != map_len) { | |
1906 | ERR("Invalid map payload received in \"add map\" command"); | |
1907 | ret_code = LTTNG_ERR_INVALID_MAP; | |
1908 | goto end; | |
1909 | } | |
1910 | } | |
1911 | ||
1912 | switch (lttng_map_get_domain(map)) { | |
1913 | case LTTNG_DOMAIN_KERNEL: | |
1914 | ret_code = map_kernel_add(cmd_ctx->session->kernel_session, map); | |
1915 | if (ret_code != LTTNG_OK) { | |
1916 | ERR("Creating a new kernel map: %s", lttng_strerror(ret_code)); | |
1917 | goto end; | |
1918 | } | |
1919 | ||
1920 | ret_code = LTTNG_OK; | |
1921 | break; | |
1922 | case LTTNG_DOMAIN_UST: | |
1923 | ret = map_ust_add(cmd_ctx->session->ust_session, map); | |
1924 | if (ret) { | |
1925 | ERR("Creating a new UST map: %s", lttng_strerror(-ret)); | |
1926 | ret_code = ret; | |
1927 | goto end; | |
1928 | } | |
1929 | ||
1930 | ret_code = LTTNG_OK; | |
1931 | break; | |
1932 | default: | |
1933 | abort(); | |
1934 | } | |
1935 | ||
1936 | ||
1937 | { | |
1938 | struct lttng_triggers *triggers = NULL; | |
1939 | enum lttng_trigger_status t_status; | |
1940 | unsigned int count, i; | |
1941 | ||
1942 | /* | |
1943 | * FRDESO: beware of moving this code. This is currently not | |
1944 | * racy because this is executed by the client thread and the | |
1945 | * client thread is the thread registering new triggers. If | |
1946 | * this code is relocate special care must be taken. | |
1947 | */ | |
1948 | ret_code = notification_thread_command_list_triggers( | |
1949 | notification_thread_handle, 0, &triggers); | |
1950 | if (ret_code != LTTNG_OK) { | |
1951 | ret = -1; | |
1952 | goto end; | |
1953 | } | |
1954 | ||
1955 | assert(triggers); | |
1956 | ||
1957 | t_status = lttng_triggers_get_count(triggers, &count); | |
1958 | if (t_status != LTTNG_TRIGGER_STATUS_OK) { | |
1959 | ret = -1; | |
1960 | lttng_triggers_destroy(triggers); | |
1961 | goto end; | |
1962 | } | |
1963 | ||
1964 | for (i = 0; i < count; i++) { | |
1965 | const struct lttng_trigger *trigger; | |
1966 | ||
1967 | trigger = lttng_triggers_get_at_index(triggers, i); | |
1968 | assert(trigger); | |
1969 | ||
1970 | ret_code = sync_all_tracer_executed_actions(trigger, | |
1971 | &cmd_creds, TRACER_EXECUTED_ACTION_STATE_REGISTER); | |
1972 | assert(ret_code == LTTNG_OK); | |
1973 | } | |
1974 | ||
1975 | lttng_triggers_destroy(triggers); | |
1976 | } | |
1977 | ||
1978 | lttng_map_put(map); | |
1979 | ||
1980 | end: | |
1981 | lttng_payload_reset(&map_payload); | |
1982 | return ret_code; | |
1983 | } | |
1984 | ||
1985 | enum lttng_error_code cmd_enable_map(struct ltt_session *session, | |
1986 | enum lttng_domain_type domain, char *map_name) | |
1987 | { | |
1988 | struct ltt_ust_session *usess = session->ust_session; | |
1989 | enum lttng_error_code ret_code; | |
1990 | ||
1991 | DBG("Enabling map %s for session %s", map_name, session->name); | |
1992 | ||
1993 | rcu_read_lock(); | |
1994 | ||
1995 | switch (domain) { | |
1996 | case LTTNG_DOMAIN_KERNEL: | |
1997 | { | |
1998 | struct ltt_kernel_map *kmap; | |
1999 | struct ltt_kernel_session *ksess = session->kernel_session; | |
2000 | ||
2001 | kmap = trace_kernel_get_map_by_name(map_name, ksess); | |
2002 | if (kmap == NULL) { | |
2003 | ret_code = LTTNG_ERR_KERNEL_MAP_NOT_FOUND; | |
2004 | goto error; | |
2005 | } | |
2006 | ||
2007 | ret_code = map_kernel_enable(ksess, kmap); | |
2008 | if (ret_code != LTTNG_OK) { | |
2009 | goto error; | |
2010 | } | |
2011 | break; | |
2012 | } | |
2013 | case LTTNG_DOMAIN_UST: | |
2014 | { | |
2015 | struct ltt_ust_map *umap; | |
2016 | struct lttng_ht *map_ht; | |
2017 | ||
2018 | map_ht = usess->domain_global.maps; | |
2019 | ||
2020 | umap = trace_ust_find_map_by_name(map_ht, map_name); | |
2021 | if (umap == NULL) { | |
2022 | ret_code = LTTNG_ERR_UST_MAP_NOT_FOUND; | |
2023 | goto error; | |
2024 | } | |
2025 | ||
2026 | ret_code = map_ust_enable(usess, umap); | |
2027 | if (ret_code != LTTNG_OK) { | |
2028 | goto error; | |
2029 | } | |
2030 | break; | |
2031 | } | |
2032 | default: | |
2033 | abort(); | |
2034 | } | |
2035 | ||
2036 | ret_code = LTTNG_OK; | |
2037 | error: | |
2038 | rcu_read_unlock(); | |
2039 | return ret_code; | |
2040 | } | |
2041 | ||
2042 | enum lttng_error_code cmd_disable_map(struct ltt_session *session, | |
2043 | enum lttng_domain_type domain, char *map_name) | |
2044 | { | |
2045 | enum lttng_error_code ret_code; | |
2046 | ||
2047 | rcu_read_lock(); | |
2048 | ||
2049 | switch (domain) { | |
2050 | case LTTNG_DOMAIN_KERNEL: | |
2051 | { | |
2052 | struct ltt_kernel_map *kmap; | |
2053 | struct ltt_kernel_session *ksess = session->kernel_session; | |
2054 | ||
2055 | kmap = trace_kernel_get_map_by_name(map_name, ksess); | |
2056 | if (kmap == NULL) { | |
2057 | ret_code = LTTNG_ERR_KERNEL_MAP_NOT_FOUND; | |
2058 | goto error; | |
2059 | } | |
2060 | ||
2061 | ret_code = map_kernel_disable(ksess, kmap); | |
2062 | if (ret_code != LTTNG_OK) { | |
2063 | goto error; | |
2064 | } | |
2065 | break; | |
2066 | } | |
2067 | case LTTNG_DOMAIN_UST: | |
2068 | { | |
2069 | struct ltt_ust_map *umap; | |
2070 | struct lttng_ht *map_ht; | |
2071 | struct ltt_ust_session *usess = session->ust_session; | |
2072 | ||
2073 | assert(usess); | |
2074 | ||
2075 | map_ht = usess->domain_global.maps; | |
2076 | ||
2077 | umap = trace_ust_find_map_by_name(map_ht, map_name); | |
2078 | if (umap == NULL) { | |
2079 | ret_code = LTTNG_ERR_UST_MAP_NOT_FOUND; | |
2080 | goto error; | |
2081 | } | |
2082 | ||
2083 | ret_code = map_ust_disable(usess, umap); | |
2084 | if (ret_code != LTTNG_OK) { | |
2085 | goto error; | |
2086 | } | |
2087 | break; | |
2088 | } | |
2089 | default: | |
2090 | abort(); | |
2091 | } | |
2092 | ||
2093 | ret_code = LTTNG_OK; | |
2094 | ||
2095 | error: | |
2096 | rcu_read_unlock(); | |
2097 | return ret_code; | |
2098 | } | |
2099 | ||
159b042f JG |
2100 | enum lttng_error_code cmd_process_attr_tracker_get_tracking_policy( |
2101 | struct ltt_session *session, | |
2102 | enum lttng_domain_type domain, | |
2103 | enum lttng_process_attr process_attr, | |
2104 | enum lttng_tracking_policy *policy) | |
2105 | { | |
2106 | enum lttng_error_code ret_code = LTTNG_OK; | |
2107 | const struct process_attr_tracker *tracker; | |
2108 | ||
2109 | switch (domain) { | |
2110 | case LTTNG_DOMAIN_KERNEL: | |
2111 | if (!session->kernel_session) { | |
2112 | ret_code = LTTNG_ERR_INVALID; | |
2113 | goto end; | |
2114 | } | |
2115 | tracker = kernel_get_process_attr_tracker( | |
2116 | session->kernel_session, process_attr); | |
2117 | break; | |
2118 | case LTTNG_DOMAIN_UST: | |
2119 | if (!session->ust_session) { | |
2120 | ret_code = LTTNG_ERR_INVALID; | |
2121 | goto end; | |
2122 | } | |
2123 | tracker = trace_ust_get_process_attr_tracker( | |
2124 | session->ust_session, process_attr); | |
2125 | break; | |
2126 | default: | |
2127 | ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN; | |
2128 | goto end; | |
2129 | } | |
2130 | if (tracker) { | |
2131 | *policy = process_attr_tracker_get_tracking_policy(tracker); | |
2132 | } else { | |
2133 | ret_code = LTTNG_ERR_INVALID; | |
2134 | } | |
2135 | end: | |
2136 | return ret_code; | |
2137 | } | |
2138 | ||
2139 | enum lttng_error_code cmd_process_attr_tracker_set_tracking_policy( | |
2140 | struct ltt_session *session, | |
2141 | enum lttng_domain_type domain, | |
2142 | enum lttng_process_attr process_attr, | |
2143 | enum lttng_tracking_policy policy) | |
2144 | { | |
2145 | enum lttng_error_code ret_code = LTTNG_OK; | |
2146 | ||
2147 | switch (policy) { | |
2148 | case LTTNG_TRACKING_POLICY_INCLUDE_SET: | |
2149 | case LTTNG_TRACKING_POLICY_EXCLUDE_ALL: | |
2150 | case LTTNG_TRACKING_POLICY_INCLUDE_ALL: | |
2151 | break; | |
2152 | default: | |
2153 | ret_code = LTTNG_ERR_INVALID; | |
2154 | goto end; | |
2155 | } | |
2156 | ||
2157 | switch (domain) { | |
2158 | case LTTNG_DOMAIN_KERNEL: | |
2159 | if (!session->kernel_session) { | |
2160 | ret_code = LTTNG_ERR_INVALID; | |
2161 | goto end; | |
2162 | } | |
2163 | ret_code = kernel_process_attr_tracker_set_tracking_policy( | |
2164 | session->kernel_session, process_attr, policy); | |
2165 | break; | |
2166 | case LTTNG_DOMAIN_UST: | |
2167 | if (!session->ust_session) { | |
2168 | ret_code = LTTNG_ERR_INVALID; | |
2169 | goto end; | |
2170 | } | |
2171 | ret_code = trace_ust_process_attr_tracker_set_tracking_policy( | |
2172 | session->ust_session, process_attr, policy); | |
2173 | break; | |
2174 | default: | |
2175 | ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN; | |
2176 | break; | |
2177 | } | |
2178 | end: | |
2179 | return ret_code; | |
2180 | } | |
2181 | ||
2182 | enum lttng_error_code cmd_process_attr_tracker_inclusion_set_add_value( | |
2183 | struct ltt_session *session, | |
2184 | enum lttng_domain_type domain, | |
2185 | enum lttng_process_attr process_attr, | |
2186 | const struct process_attr_value *value) | |
2187 | { | |
2188 | enum lttng_error_code ret_code = LTTNG_OK; | |
2189 | ||
2190 | switch (domain) { | |
2191 | case LTTNG_DOMAIN_KERNEL: | |
2192 | if (!session->kernel_session) { | |
2193 | ret_code = LTTNG_ERR_INVALID; | |
2194 | goto end; | |
2195 | } | |
2196 | ret_code = kernel_process_attr_tracker_inclusion_set_add_value( | |
2197 | session->kernel_session, process_attr, value); | |
2198 | break; | |
2199 | case LTTNG_DOMAIN_UST: | |
2200 | if (!session->ust_session) { | |
2201 | ret_code = LTTNG_ERR_INVALID; | |
2202 | goto end; | |
2203 | } | |
2204 | ret_code = trace_ust_process_attr_tracker_inclusion_set_add_value( | |
2205 | session->ust_session, process_attr, value); | |
2206 | break; | |
2207 | default: | |
2208 | ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN; | |
2209 | break; | |
2210 | } | |
2211 | end: | |
2212 | return ret_code; | |
2213 | } | |
2214 | ||
2215 | enum lttng_error_code cmd_process_attr_tracker_inclusion_set_remove_value( | |
2216 | struct ltt_session *session, | |
2217 | enum lttng_domain_type domain, | |
2218 | enum lttng_process_attr process_attr, | |
2219 | const struct process_attr_value *value) | |
2220 | { | |
2221 | enum lttng_error_code ret_code = LTTNG_OK; | |
2222 | ||
2223 | switch (domain) { | |
2224 | case LTTNG_DOMAIN_KERNEL: | |
2225 | if (!session->kernel_session) { | |
2226 | ret_code = LTTNG_ERR_INVALID; | |
2227 | goto end; | |
2228 | } | |
2229 | ret_code = kernel_process_attr_tracker_inclusion_set_remove_value( | |
2230 | session->kernel_session, process_attr, value); | |
2231 | break; | |
2232 | case LTTNG_DOMAIN_UST: | |
2233 | if (!session->ust_session) { | |
2234 | ret_code = LTTNG_ERR_INVALID; | |
2235 | goto end; | |
2236 | } | |
2237 | ret_code = trace_ust_process_attr_tracker_inclusion_set_remove_value( | |
2238 | session->ust_session, process_attr, value); | |
2239 | break; | |
2240 | default: | |
2241 | ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN; | |
2242 | break; | |
2243 | } | |
2244 | end: | |
2245 | return ret_code; | |
2246 | } | |
2247 | ||
2248 | enum lttng_error_code cmd_process_attr_tracker_get_inclusion_set( | |
2249 | struct ltt_session *session, | |
2250 | enum lttng_domain_type domain, | |
2251 | enum lttng_process_attr process_attr, | |
2252 | struct lttng_process_attr_values **values) | |
2253 | { | |
2254 | enum lttng_error_code ret_code = LTTNG_OK; | |
2255 | const struct process_attr_tracker *tracker; | |
2256 | enum process_attr_tracker_status status; | |
2257 | ||
2258 | switch (domain) { | |
2259 | case LTTNG_DOMAIN_KERNEL: | |
2260 | if (!session->kernel_session) { | |
2261 | ret_code = LTTNG_ERR_INVALID; | |
2262 | goto end; | |
2263 | } | |
2264 | tracker = kernel_get_process_attr_tracker( | |
2265 | session->kernel_session, process_attr); | |
2266 | break; | |
2267 | case LTTNG_DOMAIN_UST: | |
2268 | if (!session->ust_session) { | |
2269 | ret_code = LTTNG_ERR_INVALID; | |
2270 | goto end; | |
2271 | } | |
2272 | tracker = trace_ust_get_process_attr_tracker( | |
2273 | session->ust_session, process_attr); | |
2274 | break; | |
2275 | default: | |
2276 | ret_code = LTTNG_ERR_UNSUPPORTED_DOMAIN; | |
2277 | goto end; | |
2278 | } | |
2279 | ||
2280 | if (!tracker) { | |
2281 | ret_code = LTTNG_ERR_INVALID; | |
2282 | goto end; | |
2283 | } | |
2284 | ||
2285 | status = process_attr_tracker_get_inclusion_set(tracker, values); | |
2286 | switch (status) { | |
2287 | case PROCESS_ATTR_TRACKER_STATUS_OK: | |
2288 | ret_code = LTTNG_OK; | |
2289 | break; | |
2290 | case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY: | |
2291 | ret_code = LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY; | |
2292 | break; | |
2293 | case PROCESS_ATTR_TRACKER_STATUS_ERROR: | |
2294 | ret_code = LTTNG_ERR_NOMEM; | |
2295 | break; | |
2296 | default: | |
2297 | ret_code = LTTNG_ERR_UNK; | |
2298 | break; | |
2299 | } | |
2300 | ||
2301 | end: | |
2302 | return ret_code; | |
2303 | } | |
2304 | ||
2f77fc4b DG |
2305 | /* |
2306 | * Command LTTNG_DISABLE_EVENT processed by the client thread. | |
2307 | */ | |
56a37563 | 2308 | int cmd_disable_event(struct ltt_session *session, |
df4f5a87 JG |
2309 | enum lttng_domain_type domain, const char *channel_name, |
2310 | const struct lttng_event *event) | |
2f77fc4b DG |
2311 | { |
2312 | int ret; | |
df4f5a87 | 2313 | const char *event_name; |
6e911cad | 2314 | |
18a720cd MD |
2315 | DBG("Disable event command for event \'%s\'", event->name); |
2316 | ||
6e911cad MD |
2317 | event_name = event->name; |
2318 | ||
9b7431cf JG |
2319 | /* Error out on unhandled search criteria */ |
2320 | if (event->loglevel_type || event->loglevel != -1 || event->enabled | |
6e911cad | 2321 | || event->pid || event->filter || event->exclusion) { |
7076b56e JG |
2322 | ret = LTTNG_ERR_UNK; |
2323 | goto error; | |
6e911cad | 2324 | } |
2f77fc4b | 2325 | |
2223c96f DG |
2326 | rcu_read_lock(); |
2327 | ||
2f77fc4b DG |
2328 | switch (domain) { |
2329 | case LTTNG_DOMAIN_KERNEL: | |
2330 | { | |
2331 | struct ltt_kernel_channel *kchan; | |
2332 | struct ltt_kernel_session *ksess; | |
2333 | ||
2334 | ksess = session->kernel_session; | |
2335 | ||
85076754 MD |
2336 | /* |
2337 | * If a non-default channel has been created in the | |
2338 | * session, explicitely require that -c chan_name needs | |
2339 | * to be provided. | |
2340 | */ | |
2341 | if (ksess->has_non_default_channel && channel_name[0] == '\0') { | |
2342 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
7076b56e | 2343 | goto error_unlock; |
85076754 MD |
2344 | } |
2345 | ||
2f77fc4b DG |
2346 | kchan = trace_kernel_get_channel_by_name(channel_name, ksess); |
2347 | if (kchan == NULL) { | |
f73fabfd | 2348 | ret = LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
7076b56e | 2349 | goto error_unlock; |
2f77fc4b DG |
2350 | } |
2351 | ||
6e911cad MD |
2352 | switch (event->type) { |
2353 | case LTTNG_EVENT_ALL: | |
9550ee81 | 2354 | case LTTNG_EVENT_TRACEPOINT: |
d0ae4ea8 | 2355 | case LTTNG_EVENT_SYSCALL: |
9550ee81 JR |
2356 | case LTTNG_EVENT_PROBE: |
2357 | case LTTNG_EVENT_FUNCTION: | |
2358 | case LTTNG_EVENT_FUNCTION_ENTRY:/* fall-through */ | |
2359 | if (event_name[0] == '\0') { | |
2360 | ret = event_kernel_disable_event(kchan, | |
2361 | NULL, event->type); | |
29c62722 | 2362 | } else { |
d0ae4ea8 | 2363 | ret = event_kernel_disable_event(kchan, |
9550ee81 | 2364 | event_name, event->type); |
29c62722 | 2365 | } |
6e911cad | 2366 | if (ret != LTTNG_OK) { |
7076b56e | 2367 | goto error_unlock; |
6e911cad MD |
2368 | } |
2369 | break; | |
6e911cad MD |
2370 | default: |
2371 | ret = LTTNG_ERR_UNK; | |
7076b56e | 2372 | goto error_unlock; |
2f77fc4b DG |
2373 | } |
2374 | ||
7d268848 | 2375 | kernel_wait_quiescent(); |
2f77fc4b DG |
2376 | break; |
2377 | } | |
2378 | case LTTNG_DOMAIN_UST: | |
2379 | { | |
2380 | struct ltt_ust_channel *uchan; | |
2381 | struct ltt_ust_session *usess; | |
2382 | ||
2383 | usess = session->ust_session; | |
2384 | ||
7076b56e JG |
2385 | if (validate_ust_event_name(event_name)) { |
2386 | ret = LTTNG_ERR_INVALID_EVENT_NAME; | |
2387 | goto error_unlock; | |
2388 | } | |
2389 | ||
85076754 MD |
2390 | /* |
2391 | * If a non-default channel has been created in the | |
9550ee81 | 2392 | * session, explicitly require that -c chan_name needs |
85076754 MD |
2393 | * to be provided. |
2394 | */ | |
2395 | if (usess->has_non_default_channel && channel_name[0] == '\0') { | |
2396 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
7076b56e | 2397 | goto error_unlock; |
85076754 MD |
2398 | } |
2399 | ||
2f77fc4b DG |
2400 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, |
2401 | channel_name); | |
2402 | if (uchan == NULL) { | |
f73fabfd | 2403 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; |
7076b56e | 2404 | goto error_unlock; |
2f77fc4b DG |
2405 | } |
2406 | ||
6e911cad MD |
2407 | switch (event->type) { |
2408 | case LTTNG_EVENT_ALL: | |
b3639870 JR |
2409 | /* |
2410 | * An empty event name means that everything | |
2411 | * should be disabled. | |
2412 | */ | |
2413 | if (event->name[0] == '\0') { | |
2414 | ret = event_ust_disable_all_tracepoints(usess, uchan); | |
77d536b2 JR |
2415 | } else { |
2416 | ret = event_ust_disable_tracepoint(usess, uchan, | |
2417 | event_name); | |
2418 | } | |
6e911cad | 2419 | if (ret != LTTNG_OK) { |
7076b56e | 2420 | goto error_unlock; |
6e911cad MD |
2421 | } |
2422 | break; | |
2423 | default: | |
2424 | ret = LTTNG_ERR_UNK; | |
7076b56e | 2425 | goto error_unlock; |
2f77fc4b DG |
2426 | } |
2427 | ||
2428 | DBG3("Disable UST event %s in channel %s completed", event_name, | |
2429 | channel_name); | |
2430 | break; | |
2431 | } | |
5cdb6027 | 2432 | case LTTNG_DOMAIN_LOG4J: |
f20baf8e | 2433 | case LTTNG_DOMAIN_JUL: |
0e115563 | 2434 | case LTTNG_DOMAIN_PYTHON: |
f20baf8e | 2435 | { |
fefd409b | 2436 | struct agent *agt; |
f20baf8e DG |
2437 | struct ltt_ust_session *usess = session->ust_session; |
2438 | ||
2439 | assert(usess); | |
2440 | ||
6e911cad MD |
2441 | switch (event->type) { |
2442 | case LTTNG_EVENT_ALL: | |
2443 | break; | |
2444 | default: | |
2445 | ret = LTTNG_ERR_UNK; | |
7076b56e | 2446 | goto error_unlock; |
6e911cad MD |
2447 | } |
2448 | ||
5cdb6027 | 2449 | agt = trace_ust_find_agent(usess, domain); |
fefd409b DG |
2450 | if (!agt) { |
2451 | ret = -LTTNG_ERR_UST_EVENT_NOT_FOUND; | |
7076b56e | 2452 | goto error_unlock; |
fefd409b | 2453 | } |
b3639870 JR |
2454 | /* |
2455 | * An empty event name means that everything | |
2456 | * should be disabled. | |
2457 | */ | |
2458 | if (event->name[0] == '\0') { | |
18a720cd MD |
2459 | ret = event_agent_disable_all(usess, agt); |
2460 | } else { | |
2461 | ret = event_agent_disable(usess, agt, event_name); | |
2462 | } | |
f20baf8e | 2463 | if (ret != LTTNG_OK) { |
7076b56e | 2464 | goto error_unlock; |
f20baf8e DG |
2465 | } |
2466 | ||
2467 | break; | |
2468 | } | |
2f77fc4b | 2469 | default: |
f73fabfd | 2470 | ret = LTTNG_ERR_UND; |
7076b56e | 2471 | goto error_unlock; |
2f77fc4b DG |
2472 | } |
2473 | ||
f73fabfd | 2474 | ret = LTTNG_OK; |
2f77fc4b | 2475 | |
7076b56e | 2476 | error_unlock: |
2223c96f | 2477 | rcu_read_unlock(); |
7076b56e | 2478 | error: |
2f77fc4b DG |
2479 | return ret; |
2480 | } | |
2481 | ||
2f77fc4b DG |
2482 | /* |
2483 | * Command LTTNG_ADD_CONTEXT processed by the client thread. | |
2484 | */ | |
56a37563 | 2485 | int cmd_add_context(struct ltt_session *session, enum lttng_domain_type domain, |
df4f5a87 | 2486 | char *channel_name, const struct lttng_event_context *ctx, int kwpipe) |
2f77fc4b | 2487 | { |
d5979e4a | 2488 | int ret, chan_kern_created = 0, chan_ust_created = 0; |
bdf64013 JG |
2489 | char *app_ctx_provider_name = NULL, *app_ctx_name = NULL; |
2490 | ||
9a699f7b JR |
2491 | /* |
2492 | * Don't try to add a context if the session has been started at | |
2493 | * some point in time before. The tracer does not allow it and would | |
2494 | * result in a corrupted trace. | |
2495 | */ | |
2496 | if (session->has_been_started) { | |
2497 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; | |
2498 | goto end; | |
2499 | } | |
2500 | ||
bdf64013 JG |
2501 | if (ctx->ctx == LTTNG_EVENT_CONTEXT_APP_CONTEXT) { |
2502 | app_ctx_provider_name = ctx->u.app_ctx.provider_name; | |
2503 | app_ctx_name = ctx->u.app_ctx.ctx_name; | |
2504 | } | |
2f77fc4b DG |
2505 | |
2506 | switch (domain) { | |
2507 | case LTTNG_DOMAIN_KERNEL: | |
979e618e DG |
2508 | assert(session->kernel_session); |
2509 | ||
2510 | if (session->kernel_session->channel_count == 0) { | |
2511 | /* Create default channel */ | |
2512 | ret = channel_kernel_create(session->kernel_session, NULL, kwpipe); | |
2513 | if (ret != LTTNG_OK) { | |
2514 | goto error; | |
2515 | } | |
d5979e4a | 2516 | chan_kern_created = 1; |
979e618e | 2517 | } |
2f77fc4b | 2518 | /* Add kernel context to kernel tracer */ |
601d5acf | 2519 | ret = context_kernel_add(session->kernel_session, ctx, channel_name); |
f73fabfd | 2520 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
2521 | goto error; |
2522 | } | |
2523 | break; | |
bdf64013 JG |
2524 | case LTTNG_DOMAIN_JUL: |
2525 | case LTTNG_DOMAIN_LOG4J: | |
2526 | { | |
2527 | /* | |
2528 | * Validate channel name. | |
2529 | * If no channel name is given and the domain is JUL or LOG4J, | |
2530 | * set it to the appropriate domain-specific channel name. If | |
2531 | * a name is provided but does not match the expexted channel | |
2532 | * name, return an error. | |
2533 | */ | |
2534 | if (domain == LTTNG_DOMAIN_JUL && *channel_name && | |
2535 | strcmp(channel_name, | |
2536 | DEFAULT_JUL_CHANNEL_NAME)) { | |
2537 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; | |
2538 | goto error; | |
2539 | } else if (domain == LTTNG_DOMAIN_LOG4J && *channel_name && | |
2540 | strcmp(channel_name, | |
2541 | DEFAULT_LOG4J_CHANNEL_NAME)) { | |
2542 | ret = LTTNG_ERR_UST_CHAN_NOT_FOUND; | |
2543 | goto error; | |
2544 | } | |
a93b3916 | 2545 | /* break is _not_ missing here. */ |
bdf64013 | 2546 | } |
2f77fc4b DG |
2547 | case LTTNG_DOMAIN_UST: |
2548 | { | |
2549 | struct ltt_ust_session *usess = session->ust_session; | |
85076754 MD |
2550 | unsigned int chan_count; |
2551 | ||
2f77fc4b DG |
2552 | assert(usess); |
2553 | ||
85076754 | 2554 | chan_count = lttng_ht_get_count(usess->domain_global.channels); |
979e618e DG |
2555 | if (chan_count == 0) { |
2556 | struct lttng_channel *attr; | |
2557 | /* Create default channel */ | |
0a9c6494 | 2558 | attr = channel_new_default_attr(domain, usess->buffer_type); |
979e618e DG |
2559 | if (attr == NULL) { |
2560 | ret = LTTNG_ERR_FATAL; | |
2561 | goto error; | |
2562 | } | |
2563 | ||
7972aab2 | 2564 | ret = channel_ust_create(usess, attr, usess->buffer_type); |
979e618e DG |
2565 | if (ret != LTTNG_OK) { |
2566 | free(attr); | |
2567 | goto error; | |
2568 | } | |
cf0bcb51 | 2569 | channel_attr_destroy(attr); |
d5979e4a | 2570 | chan_ust_created = 1; |
979e618e DG |
2571 | } |
2572 | ||
601d5acf | 2573 | ret = context_ust_add(usess, domain, ctx, channel_name); |
bdf64013 JG |
2574 | free(app_ctx_provider_name); |
2575 | free(app_ctx_name); | |
2576 | app_ctx_name = NULL; | |
2577 | app_ctx_provider_name = NULL; | |
f73fabfd | 2578 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
2579 | goto error; |
2580 | } | |
2581 | break; | |
2582 | } | |
2f77fc4b | 2583 | default: |
f73fabfd | 2584 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
2585 | goto error; |
2586 | } | |
2587 | ||
bdf64013 JG |
2588 | ret = LTTNG_OK; |
2589 | goto end; | |
2f77fc4b DG |
2590 | |
2591 | error: | |
d5979e4a DG |
2592 | if (chan_kern_created) { |
2593 | struct ltt_kernel_channel *kchan = | |
2594 | trace_kernel_get_channel_by_name(DEFAULT_CHANNEL_NAME, | |
2595 | session->kernel_session); | |
2596 | /* Created previously, this should NOT fail. */ | |
2597 | assert(kchan); | |
2598 | kernel_destroy_channel(kchan); | |
2599 | } | |
2600 | ||
2601 | if (chan_ust_created) { | |
2602 | struct ltt_ust_channel *uchan = | |
2603 | trace_ust_find_channel_by_name( | |
2604 | session->ust_session->domain_global.channels, | |
2605 | DEFAULT_CHANNEL_NAME); | |
2606 | /* Created previously, this should NOT fail. */ | |
2607 | assert(uchan); | |
2608 | /* Remove from the channel list of the session. */ | |
2609 | trace_ust_delete_channel(session->ust_session->domain_global.channels, | |
2610 | uchan); | |
2611 | trace_ust_destroy_channel(uchan); | |
2612 | } | |
bdf64013 JG |
2613 | end: |
2614 | free(app_ctx_provider_name); | |
2615 | free(app_ctx_name); | |
2f77fc4b DG |
2616 | return ret; |
2617 | } | |
2618 | ||
dac8e046 JG |
2619 | static inline bool name_starts_with(const char *name, const char *prefix) |
2620 | { | |
2621 | const size_t max_cmp_len = min(strlen(prefix), LTTNG_SYMBOL_NAME_LEN); | |
2622 | ||
2623 | return !strncmp(name, prefix, max_cmp_len); | |
2624 | } | |
2625 | ||
2626 | /* Perform userspace-specific event name validation */ | |
2627 | static int validate_ust_event_name(const char *name) | |
2628 | { | |
2629 | int ret = 0; | |
2630 | ||
2631 | if (!name) { | |
2632 | ret = -1; | |
2633 | goto end; | |
2634 | } | |
2635 | ||
2636 | /* | |
2637 | * Check name against all internal UST event component namespaces used | |
2638 | * by the agents. | |
2639 | */ | |
2640 | if (name_starts_with(name, DEFAULT_JUL_EVENT_COMPONENT) || | |
2641 | name_starts_with(name, DEFAULT_LOG4J_EVENT_COMPONENT) || | |
2642 | name_starts_with(name, DEFAULT_PYTHON_EVENT_COMPONENT)) { | |
2643 | ret = -1; | |
2644 | } | |
2645 | ||
2646 | end: | |
2647 | return ret; | |
2648 | } | |
88f06f15 | 2649 | |
2f77fc4b | 2650 | /* |
88f06f15 JG |
2651 | * Internal version of cmd_enable_event() with a supplemental |
2652 | * "internal_event" flag which is used to enable internal events which should | |
2653 | * be hidden from clients. Such events are used in the agent implementation to | |
2654 | * enable the events through which all "agent" events are funeled. | |
2f77fc4b | 2655 | */ |
88f06f15 | 2656 | static int _cmd_enable_event(struct ltt_session *session, |
df4f5a87 | 2657 | const struct lttng_domain *domain, |
025faf73 | 2658 | char *channel_name, struct lttng_event *event, |
6b453b5e | 2659 | char *filter_expression, |
2b00d462 | 2660 | struct lttng_bytecode *filter, |
db8f1377 | 2661 | struct lttng_event_exclusion *exclusion, |
88f06f15 | 2662 | int wpipe, bool internal_event) |
2f77fc4b | 2663 | { |
9f449915 | 2664 | int ret = 0, channel_created = 0; |
cfedea03 | 2665 | struct lttng_channel *attr = NULL; |
2f77fc4b DG |
2666 | |
2667 | assert(session); | |
2668 | assert(event); | |
2669 | assert(channel_name); | |
2670 | ||
2a385866 JG |
2671 | /* If we have a filter, we must have its filter expression */ |
2672 | assert(!(!!filter_expression ^ !!filter)); | |
2673 | ||
9f449915 PP |
2674 | /* Normalize event name as a globbing pattern */ |
2675 | strutils_normalize_star_glob_pattern(event->name); | |
18a720cd | 2676 | |
9f449915 PP |
2677 | /* Normalize exclusion names as globbing patterns */ |
2678 | if (exclusion) { | |
2679 | size_t i; | |
f5ac4bd7 | 2680 | |
9f449915 PP |
2681 | for (i = 0; i < exclusion->count; i++) { |
2682 | char *name = LTTNG_EVENT_EXCLUSION_NAME_AT(exclusion, i); | |
2683 | ||
2684 | strutils_normalize_star_glob_pattern(name); | |
2685 | } | |
930a2e99 JG |
2686 | } |
2687 | ||
9f449915 PP |
2688 | DBG("Enable event command for event \'%s\'", event->name); |
2689 | ||
2690 | rcu_read_lock(); | |
2691 | ||
7972aab2 | 2692 | switch (domain->type) { |
2f77fc4b DG |
2693 | case LTTNG_DOMAIN_KERNEL: |
2694 | { | |
2695 | struct ltt_kernel_channel *kchan; | |
2696 | ||
85076754 MD |
2697 | /* |
2698 | * If a non-default channel has been created in the | |
2699 | * session, explicitely require that -c chan_name needs | |
2700 | * to be provided. | |
2701 | */ | |
2702 | if (session->kernel_session->has_non_default_channel | |
2703 | && channel_name[0] == '\0') { | |
2704 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
2705 | goto error; | |
2706 | } | |
2707 | ||
2f77fc4b DG |
2708 | kchan = trace_kernel_get_channel_by_name(channel_name, |
2709 | session->kernel_session); | |
2710 | if (kchan == NULL) { | |
0a9c6494 DG |
2711 | attr = channel_new_default_attr(LTTNG_DOMAIN_KERNEL, |
2712 | LTTNG_BUFFER_GLOBAL); | |
2f77fc4b | 2713 | if (attr == NULL) { |
f73fabfd | 2714 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
2715 | goto error; |
2716 | } | |
04c17253 MD |
2717 | if (lttng_strncpy(attr->name, channel_name, |
2718 | sizeof(attr->name))) { | |
2719 | ret = LTTNG_ERR_INVALID; | |
04c17253 MD |
2720 | goto error; |
2721 | } | |
2f77fc4b DG |
2722 | |
2723 | ret = cmd_enable_channel(session, domain, attr, wpipe); | |
f73fabfd | 2724 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
2725 | goto error; |
2726 | } | |
e5f5db7f | 2727 | channel_created = 1; |
2f77fc4b DG |
2728 | } |
2729 | ||
2730 | /* Get the newly created kernel channel pointer */ | |
2731 | kchan = trace_kernel_get_channel_by_name(channel_name, | |
2732 | session->kernel_session); | |
2733 | if (kchan == NULL) { | |
2734 | /* This sould not happen... */ | |
f73fabfd | 2735 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
2736 | goto error; |
2737 | } | |
2738 | ||
6e911cad MD |
2739 | switch (event->type) { |
2740 | case LTTNG_EVENT_ALL: | |
29c62722 | 2741 | { |
00a62084 | 2742 | char *filter_expression_a = NULL; |
2b00d462 | 2743 | struct lttng_bytecode *filter_a = NULL; |
00a62084 MD |
2744 | |
2745 | /* | |
2746 | * We need to duplicate filter_expression and filter, | |
2747 | * because ownership is passed to first enable | |
2748 | * event. | |
2749 | */ | |
2750 | if (filter_expression) { | |
2751 | filter_expression_a = strdup(filter_expression); | |
2752 | if (!filter_expression_a) { | |
2753 | ret = LTTNG_ERR_FATAL; | |
2754 | goto error; | |
2755 | } | |
2756 | } | |
2757 | if (filter) { | |
2758 | filter_a = zmalloc(sizeof(*filter_a) + filter->len); | |
2759 | if (!filter_a) { | |
2760 | free(filter_expression_a); | |
2761 | ret = LTTNG_ERR_FATAL; | |
2762 | goto error; | |
2763 | } | |
2764 | memcpy(filter_a, filter, sizeof(*filter_a) + filter->len); | |
2765 | } | |
29c62722 | 2766 | event->type = LTTNG_EVENT_TRACEPOINT; /* Hack */ |
00a62084 MD |
2767 | ret = event_kernel_enable_event(kchan, event, |
2768 | filter_expression, filter); | |
a969e101 MD |
2769 | /* We have passed ownership */ |
2770 | filter_expression = NULL; | |
2771 | filter = NULL; | |
29c62722 MD |
2772 | if (ret != LTTNG_OK) { |
2773 | if (channel_created) { | |
2774 | /* Let's not leak a useless channel. */ | |
2775 | kernel_destroy_channel(kchan); | |
2776 | } | |
00a62084 MD |
2777 | free(filter_expression_a); |
2778 | free(filter_a); | |
29c62722 MD |
2779 | goto error; |
2780 | } | |
2781 | event->type = LTTNG_EVENT_SYSCALL; /* Hack */ | |
00a62084 MD |
2782 | ret = event_kernel_enable_event(kchan, event, |
2783 | filter_expression_a, filter_a); | |
60d21fa2 AB |
2784 | /* We have passed ownership */ |
2785 | filter_expression_a = NULL; | |
2786 | filter_a = NULL; | |
29c62722 MD |
2787 | if (ret != LTTNG_OK) { |
2788 | goto error; | |
2789 | } | |
2790 | break; | |
2791 | } | |
6e6ef3d7 | 2792 | case LTTNG_EVENT_PROBE: |
dcabc190 | 2793 | case LTTNG_EVENT_USERSPACE_PROBE: |
6e6ef3d7 DG |
2794 | case LTTNG_EVENT_FUNCTION: |
2795 | case LTTNG_EVENT_FUNCTION_ENTRY: | |
6e911cad | 2796 | case LTTNG_EVENT_TRACEPOINT: |
00a62084 MD |
2797 | ret = event_kernel_enable_event(kchan, event, |
2798 | filter_expression, filter); | |
a969e101 MD |
2799 | /* We have passed ownership */ |
2800 | filter_expression = NULL; | |
2801 | filter = NULL; | |
6e911cad MD |
2802 | if (ret != LTTNG_OK) { |
2803 | if (channel_created) { | |
2804 | /* Let's not leak a useless channel. */ | |
2805 | kernel_destroy_channel(kchan); | |
2806 | } | |
2807 | goto error; | |
e5f5db7f | 2808 | } |
6e911cad MD |
2809 | break; |
2810 | case LTTNG_EVENT_SYSCALL: | |
00a62084 MD |
2811 | ret = event_kernel_enable_event(kchan, event, |
2812 | filter_expression, filter); | |
a969e101 MD |
2813 | /* We have passed ownership */ |
2814 | filter_expression = NULL; | |
2815 | filter = NULL; | |
e2b957af MD |
2816 | if (ret != LTTNG_OK) { |
2817 | goto error; | |
2818 | } | |
6e911cad MD |
2819 | break; |
2820 | default: | |
2821 | ret = LTTNG_ERR_UNK; | |
2f77fc4b DG |
2822 | goto error; |
2823 | } | |
2824 | ||
7d268848 | 2825 | kernel_wait_quiescent(); |
2f77fc4b DG |
2826 | break; |
2827 | } | |
2828 | case LTTNG_DOMAIN_UST: | |
2829 | { | |
2830 | struct ltt_ust_channel *uchan; | |
2831 | struct ltt_ust_session *usess = session->ust_session; | |
2832 | ||
2833 | assert(usess); | |
2834 | ||
85076754 MD |
2835 | /* |
2836 | * If a non-default channel has been created in the | |
2837 | * session, explicitely require that -c chan_name needs | |
2838 | * to be provided. | |
2839 | */ | |
2840 | if (usess->has_non_default_channel && channel_name[0] == '\0') { | |
2841 | ret = LTTNG_ERR_NEED_CHANNEL_NAME; | |
2842 | goto error; | |
2843 | } | |
2844 | ||
2f77fc4b DG |
2845 | /* Get channel from global UST domain */ |
2846 | uchan = trace_ust_find_channel_by_name(usess->domain_global.channels, | |
2847 | channel_name); | |
2848 | if (uchan == NULL) { | |
2849 | /* Create default channel */ | |
0a9c6494 DG |
2850 | attr = channel_new_default_attr(LTTNG_DOMAIN_UST, |
2851 | usess->buffer_type); | |
2f77fc4b | 2852 | if (attr == NULL) { |
f73fabfd | 2853 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
2854 | goto error; |
2855 | } | |
04c17253 MD |
2856 | if (lttng_strncpy(attr->name, channel_name, |
2857 | sizeof(attr->name))) { | |
2858 | ret = LTTNG_ERR_INVALID; | |
04c17253 MD |
2859 | goto error; |
2860 | } | |
2f77fc4b DG |
2861 | |
2862 | ret = cmd_enable_channel(session, domain, attr, wpipe); | |
f73fabfd | 2863 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
2864 | goto error; |
2865 | } | |
2f77fc4b DG |
2866 | |
2867 | /* Get the newly created channel reference back */ | |
2868 | uchan = trace_ust_find_channel_by_name( | |
2869 | usess->domain_global.channels, channel_name); | |
2870 | assert(uchan); | |
2871 | } | |
2872 | ||
141feb8c JG |
2873 | if (uchan->domain != LTTNG_DOMAIN_UST && !internal_event) { |
2874 | /* | |
2875 | * Don't allow users to add UST events to channels which | |
2876 | * are assigned to a userspace subdomain (JUL, Log4J, | |
2877 | * Python, etc.). | |
2878 | */ | |
2879 | ret = LTTNG_ERR_INVALID_CHANNEL_DOMAIN; | |
2880 | goto error; | |
2881 | } | |
2882 | ||
dac8e046 JG |
2883 | if (!internal_event) { |
2884 | /* | |
2885 | * Ensure the event name is not reserved for internal | |
2886 | * use. | |
2887 | */ | |
2888 | ret = validate_ust_event_name(event->name); | |
2889 | if (ret) { | |
0e270a1e | 2890 | WARN("Userspace event name %s failed validation.", |
bbcab087 | 2891 | event->name); |
dac8e046 JG |
2892 | ret = LTTNG_ERR_INVALID_EVENT_NAME; |
2893 | goto error; | |
2894 | } | |
2895 | } | |
2896 | ||
2f77fc4b | 2897 | /* At this point, the session and channel exist on the tracer */ |
6b453b5e | 2898 | ret = event_ust_enable_tracepoint(usess, uchan, event, |
88f06f15 JG |
2899 | filter_expression, filter, exclusion, |
2900 | internal_event); | |
49d21f93 MD |
2901 | /* We have passed ownership */ |
2902 | filter_expression = NULL; | |
2903 | filter = NULL; | |
2904 | exclusion = NULL; | |
94382e15 JG |
2905 | if (ret == LTTNG_ERR_UST_EVENT_ENABLED) { |
2906 | goto already_enabled; | |
2907 | } else if (ret != LTTNG_OK) { | |
2f77fc4b DG |
2908 | goto error; |
2909 | } | |
2910 | break; | |
2911 | } | |
5cdb6027 | 2912 | case LTTNG_DOMAIN_LOG4J: |
f20baf8e | 2913 | case LTTNG_DOMAIN_JUL: |
0e115563 | 2914 | case LTTNG_DOMAIN_PYTHON: |
f20baf8e | 2915 | { |
da6c3a50 | 2916 | const char *default_event_name, *default_chan_name; |
fefd409b | 2917 | struct agent *agt; |
f20baf8e DG |
2918 | struct lttng_event uevent; |
2919 | struct lttng_domain tmp_dom; | |
2920 | struct ltt_ust_session *usess = session->ust_session; | |
2921 | ||
2922 | assert(usess); | |
2923 | ||
f28f9e44 JG |
2924 | if (!agent_tracing_is_enabled()) { |
2925 | DBG("Attempted to enable an event in an agent domain but the agent thread is not running"); | |
2926 | ret = LTTNG_ERR_AGENT_TRACING_DISABLED; | |
2927 | goto error; | |
2928 | } | |
2929 | ||
5cdb6027 | 2930 | agt = trace_ust_find_agent(usess, domain->type); |
fefd409b | 2931 | if (!agt) { |
5cdb6027 | 2932 | agt = agent_create(domain->type); |
fefd409b | 2933 | if (!agt) { |
e5b3c48c | 2934 | ret = LTTNG_ERR_NOMEM; |
fefd409b DG |
2935 | goto error; |
2936 | } | |
2937 | agent_add(agt, usess->agents); | |
2938 | } | |
2939 | ||
022d91ba | 2940 | /* Create the default tracepoint. */ |
996de3c7 | 2941 | memset(&uevent, 0, sizeof(uevent)); |
f20baf8e DG |
2942 | uevent.type = LTTNG_EVENT_TRACEPOINT; |
2943 | uevent.loglevel_type = LTTNG_EVENT_LOGLEVEL_ALL; | |
51755dc8 JG |
2944 | default_event_name = event_get_default_agent_ust_name( |
2945 | domain->type); | |
da6c3a50 | 2946 | if (!default_event_name) { |
e5b3c48c | 2947 | ret = LTTNG_ERR_FATAL; |
da6c3a50 | 2948 | goto error; |
f43f95a9 | 2949 | } |
da6c3a50 | 2950 | strncpy(uevent.name, default_event_name, sizeof(uevent.name)); |
f20baf8e DG |
2951 | uevent.name[sizeof(uevent.name) - 1] = '\0'; |
2952 | ||
2953 | /* | |
2954 | * The domain type is changed because we are about to enable the | |
2955 | * default channel and event for the JUL domain that are hardcoded. | |
2956 | * This happens in the UST domain. | |
2957 | */ | |
2958 | memcpy(&tmp_dom, domain, sizeof(tmp_dom)); | |
2959 | tmp_dom.type = LTTNG_DOMAIN_UST; | |
2960 | ||
0e115563 DG |
2961 | switch (domain->type) { |
2962 | case LTTNG_DOMAIN_LOG4J: | |
da6c3a50 | 2963 | default_chan_name = DEFAULT_LOG4J_CHANNEL_NAME; |
0e115563 DG |
2964 | break; |
2965 | case LTTNG_DOMAIN_JUL: | |
da6c3a50 | 2966 | default_chan_name = DEFAULT_JUL_CHANNEL_NAME; |
0e115563 DG |
2967 | break; |
2968 | case LTTNG_DOMAIN_PYTHON: | |
2969 | default_chan_name = DEFAULT_PYTHON_CHANNEL_NAME; | |
2970 | break; | |
2971 | default: | |
e98a44b0 | 2972 | /* The switch/case we are in makes this impossible */ |
0e115563 | 2973 | assert(0); |
da6c3a50 DG |
2974 | } |
2975 | ||
971da06a | 2976 | { |
8404118c | 2977 | char *filter_expression_copy = NULL; |
2b00d462 | 2978 | struct lttng_bytecode *filter_copy = NULL; |
971da06a JG |
2979 | |
2980 | if (filter) { | |
51755dc8 | 2981 | const size_t filter_size = sizeof( |
2b00d462 | 2982 | struct lttng_bytecode) |
51755dc8 JG |
2983 | + filter->len; |
2984 | ||
2985 | filter_copy = zmalloc(filter_size); | |
971da06a | 2986 | if (!filter_copy) { |
018096a4 | 2987 | ret = LTTNG_ERR_NOMEM; |
b742e3e2 | 2988 | goto error; |
971da06a | 2989 | } |
51755dc8 | 2990 | memcpy(filter_copy, filter, filter_size); |
971da06a | 2991 | |
8404118c JG |
2992 | filter_expression_copy = |
2993 | strdup(filter_expression); | |
2994 | if (!filter_expression) { | |
2995 | ret = LTTNG_ERR_NOMEM; | |
51755dc8 JG |
2996 | } |
2997 | ||
2998 | if (!filter_expression_copy || !filter_copy) { | |
2999 | free(filter_expression_copy); | |
3000 | free(filter_copy); | |
3001 | goto error; | |
8404118c | 3002 | } |
971da06a JG |
3003 | } |
3004 | ||
88f06f15 | 3005 | ret = cmd_enable_event_internal(session, &tmp_dom, |
971da06a | 3006 | (char *) default_chan_name, |
8404118c JG |
3007 | &uevent, filter_expression_copy, |
3008 | filter_copy, NULL, wpipe); | |
971da06a JG |
3009 | } |
3010 | ||
94382e15 JG |
3011 | if (ret == LTTNG_ERR_UST_EVENT_ENABLED) { |
3012 | goto already_enabled; | |
3013 | } else if (ret != LTTNG_OK) { | |
f20baf8e DG |
3014 | goto error; |
3015 | } | |
3016 | ||
3017 | /* The wild card * means that everything should be enabled. */ | |
3018 | if (strncmp(event->name, "*", 1) == 0 && strlen(event->name) == 1) { | |
8404118c JG |
3019 | ret = event_agent_enable_all(usess, agt, event, filter, |
3020 | filter_expression); | |
f20baf8e | 3021 | } else { |
8404118c JG |
3022 | ret = event_agent_enable(usess, agt, event, filter, |
3023 | filter_expression); | |
f20baf8e | 3024 | } |
51755dc8 JG |
3025 | filter = NULL; |
3026 | filter_expression = NULL; | |
f20baf8e DG |
3027 | if (ret != LTTNG_OK) { |
3028 | goto error; | |
3029 | } | |
3030 | ||
3031 | break; | |
3032 | } | |
2f77fc4b | 3033 | default: |
f73fabfd | 3034 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
3035 | goto error; |
3036 | } | |
3037 | ||
f73fabfd | 3038 | ret = LTTNG_OK; |
2f77fc4b | 3039 | |
94382e15 | 3040 | already_enabled: |
2f77fc4b | 3041 | error: |
49d21f93 MD |
3042 | free(filter_expression); |
3043 | free(filter); | |
3044 | free(exclusion); | |
cf0bcb51 | 3045 | channel_attr_destroy(attr); |
2223c96f | 3046 | rcu_read_unlock(); |
2f77fc4b DG |
3047 | return ret; |
3048 | } | |
3049 | ||
88f06f15 JG |
3050 | /* |
3051 | * Command LTTNG_ENABLE_EVENT processed by the client thread. | |
3052 | * We own filter, exclusion, and filter_expression. | |
3053 | */ | |
df4f5a87 JG |
3054 | int cmd_enable_event(struct ltt_session *session, |
3055 | const struct lttng_domain *domain, | |
88f06f15 JG |
3056 | char *channel_name, struct lttng_event *event, |
3057 | char *filter_expression, | |
2b00d462 | 3058 | struct lttng_bytecode *filter, |
88f06f15 JG |
3059 | struct lttng_event_exclusion *exclusion, |
3060 | int wpipe) | |
3061 | { | |
3062 | return _cmd_enable_event(session, domain, channel_name, event, | |
3063 | filter_expression, filter, exclusion, wpipe, false); | |
3064 | } | |
3065 | ||
3066 | /* | |
3067 | * Enable an event which is internal to LTTng. An internal should | |
3068 | * never be made visible to clients and are immune to checks such as | |
3069 | * reserved names. | |
3070 | */ | |
3071 | static int cmd_enable_event_internal(struct ltt_session *session, | |
df4f5a87 | 3072 | const struct lttng_domain *domain, |
88f06f15 JG |
3073 | char *channel_name, struct lttng_event *event, |
3074 | char *filter_expression, | |
2b00d462 | 3075 | struct lttng_bytecode *filter, |
88f06f15 JG |
3076 | struct lttng_event_exclusion *exclusion, |
3077 | int wpipe) | |
3078 | { | |
3079 | return _cmd_enable_event(session, domain, channel_name, event, | |
3080 | filter_expression, filter, exclusion, wpipe, true); | |
3081 | } | |
3082 | ||
2f77fc4b DG |
3083 | /* |
3084 | * Command LTTNG_LIST_TRACEPOINTS processed by the client thread. | |
3085 | */ | |
56a37563 JG |
3086 | ssize_t cmd_list_tracepoints(enum lttng_domain_type domain, |
3087 | struct lttng_event **events) | |
2f77fc4b DG |
3088 | { |
3089 | int ret; | |
3090 | ssize_t nb_events = 0; | |
3091 | ||
3092 | switch (domain) { | |
3093 | case LTTNG_DOMAIN_KERNEL: | |
7d268848 | 3094 | nb_events = kernel_list_events(events); |
2f77fc4b | 3095 | if (nb_events < 0) { |
f73fabfd | 3096 | ret = LTTNG_ERR_KERN_LIST_FAIL; |
2f77fc4b DG |
3097 | goto error; |
3098 | } | |
3099 | break; | |
3100 | case LTTNG_DOMAIN_UST: | |
3101 | nb_events = ust_app_list_events(events); | |
3102 | if (nb_events < 0) { | |
f73fabfd | 3103 | ret = LTTNG_ERR_UST_LIST_FAIL; |
2f77fc4b DG |
3104 | goto error; |
3105 | } | |
3106 | break; | |
5cdb6027 | 3107 | case LTTNG_DOMAIN_LOG4J: |
3c6a091f | 3108 | case LTTNG_DOMAIN_JUL: |
0e115563 | 3109 | case LTTNG_DOMAIN_PYTHON: |
f60140a1 | 3110 | nb_events = agent_list_events(events, domain); |
3c6a091f DG |
3111 | if (nb_events < 0) { |
3112 | ret = LTTNG_ERR_UST_LIST_FAIL; | |
3113 | goto error; | |
3114 | } | |
3115 | break; | |
2f77fc4b | 3116 | default: |
f73fabfd | 3117 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
3118 | goto error; |
3119 | } | |
3120 | ||
3121 | return nb_events; | |
3122 | ||
3123 | error: | |
3124 | /* Return negative value to differentiate return code */ | |
3125 | return -ret; | |
3126 | } | |
3127 | ||
3128 | /* | |
3129 | * Command LTTNG_LIST_TRACEPOINT_FIELDS processed by the client thread. | |
3130 | */ | |
56a37563 | 3131 | ssize_t cmd_list_tracepoint_fields(enum lttng_domain_type domain, |
2f77fc4b DG |
3132 | struct lttng_event_field **fields) |
3133 | { | |
3134 | int ret; | |
3135 | ssize_t nb_fields = 0; | |
3136 | ||
3137 | switch (domain) { | |
3138 | case LTTNG_DOMAIN_UST: | |
3139 | nb_fields = ust_app_list_event_fields(fields); | |
3140 | if (nb_fields < 0) { | |
f73fabfd | 3141 | ret = LTTNG_ERR_UST_LIST_FAIL; |
2f77fc4b DG |
3142 | goto error; |
3143 | } | |
3144 | break; | |
3145 | case LTTNG_DOMAIN_KERNEL: | |
3146 | default: /* fall-through */ | |
f73fabfd | 3147 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
3148 | goto error; |
3149 | } | |
3150 | ||
3151 | return nb_fields; | |
3152 | ||
3153 | error: | |
3154 | /* Return negative value to differentiate return code */ | |
3155 | return -ret; | |
3156 | } | |
3157 | ||
834978fd DG |
3158 | ssize_t cmd_list_syscalls(struct lttng_event **events) |
3159 | { | |
3160 | return syscall_table_list(events); | |
3161 | } | |
3162 | ||
2f77fc4b DG |
3163 | /* |
3164 | * Command LTTNG_START_TRACE processed by the client thread. | |
a9ad0c8f MD |
3165 | * |
3166 | * Called with session mutex held. | |
2f77fc4b DG |
3167 | */ |
3168 | int cmd_start_trace(struct ltt_session *session) | |
3169 | { | |
82b69413 | 3170 | enum lttng_error_code ret; |
c3e68e71 | 3171 | unsigned long nb_chan = 0, nb_map = 0; |
2f77fc4b DG |
3172 | struct ltt_kernel_session *ksession; |
3173 | struct ltt_ust_session *usess; | |
1f496244 JG |
3174 | const bool session_rotated_after_last_stop = |
3175 | session->rotated_after_last_stop; | |
b02f5986 MD |
3176 | const bool session_cleared_after_last_stop = |
3177 | session->cleared_after_last_stop; | |
2f77fc4b DG |
3178 | |
3179 | assert(session); | |
3180 | ||
3181 | /* Ease our life a bit ;) */ | |
3182 | ksession = session->kernel_session; | |
3183 | usess = session->ust_session; | |
3184 | ||
8382cf6f DG |
3185 | /* Is the session already started? */ |
3186 | if (session->active) { | |
f73fabfd | 3187 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; |
7a24ece3 JR |
3188 | /* Perform nothing */ |
3189 | goto end; | |
2f77fc4b DG |
3190 | } |
3191 | ||
1f496244 JG |
3192 | if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING && |
3193 | !session->current_trace_chunk) { | |
3194 | /* | |
3195 | * A rotation was launched while the session was stopped and | |
3196 | * it has not been completed yet. It is not possible to start | |
3197 | * the session since starting the session here would require a | |
3198 | * rotation from "NULL" to a new trace chunk. That rotation | |
3199 | * would overlap with the ongoing rotation, which is not | |
3200 | * supported. | |
3201 | */ | |
3202 | WARN("Refusing to start session \"%s\" as a rotation launched after the last \"stop\" is still ongoing", | |
3203 | session->name); | |
3204 | ret = LTTNG_ERR_ROTATION_PENDING; | |
3205 | goto error; | |
3206 | } | |
3207 | ||
cde3e505 DG |
3208 | /* |
3209 | * Starting a session without channel is useless since after that it's not | |
3210 | * possible to enable channel thus inform the client. | |
3211 | */ | |
3212 | if (usess && usess->domain_global.channels) { | |
3213 | nb_chan += lttng_ht_get_count(usess->domain_global.channels); | |
c3e68e71 | 3214 | nb_map += lttng_ht_get_count(usess->domain_global.maps); |
cde3e505 DG |
3215 | } |
3216 | if (ksession) { | |
3217 | nb_chan += ksession->channel_count; | |
c3e68e71 | 3218 | nb_map += ksession->map_count; |
cde3e505 | 3219 | } |
c3e68e71 | 3220 | if (!nb_chan && !nb_map) { |
cde3e505 DG |
3221 | ret = LTTNG_ERR_NO_CHANNEL; |
3222 | goto error; | |
3223 | } | |
3224 | ||
1f496244 JG |
3225 | session->active = 1; |
3226 | session->rotated_after_last_stop = false; | |
b02f5986 | 3227 | session->cleared_after_last_stop = false; |
070b6a86 | 3228 | if (session->output_traces && !session->current_trace_chunk) { |
1f496244 JG |
3229 | if (!session->has_been_started) { |
3230 | struct lttng_trace_chunk *trace_chunk; | |
3231 | ||
3232 | DBG("Creating initial trace chunk of session \"%s\"", | |
3233 | session->name); | |
3234 | trace_chunk = session_create_new_trace_chunk( | |
3235 | session, NULL, NULL, NULL); | |
3236 | if (!trace_chunk) { | |
3237 | ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
3238 | goto error; | |
3239 | } | |
3240 | assert(!session->current_trace_chunk); | |
3241 | ret = session_set_trace_chunk(session, trace_chunk, | |
3242 | NULL); | |
3243 | lttng_trace_chunk_put(trace_chunk); | |
3244 | if (ret) { | |
3245 | ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER; | |
3246 | goto error; | |
3247 | } | |
3248 | } else { | |
3249 | DBG("Rotating session \"%s\" from its current \"NULL\" trace chunk to a new chunk", | |
3250 | session->name); | |
3251 | /* | |
3252 | * Rotate existing streams into the new chunk. | |
3253 | * This is a "quiet" rotation has no client has | |
3254 | * explicitly requested this operation. | |
3255 | * | |
3256 | * There is also no need to wait for the rotation | |
3257 | * to complete as it will happen immediately. No data | |
3258 | * was produced as the session was stopped, so the | |
3259 | * rotation should happen on reception of the command. | |
3260 | */ | |
343defc2 MD |
3261 | ret = cmd_rotate_session(session, NULL, true, |
3262 | LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION); | |
1f496244 JG |
3263 | if (ret != LTTNG_OK) { |
3264 | goto error; | |
3265 | } | |
5c408ad8 | 3266 | } |
c996624c JD |
3267 | } |
3268 | ||
2f77fc4b DG |
3269 | /* Kernel tracing */ |
3270 | if (ksession != NULL) { | |
c996624c | 3271 | DBG("Start kernel tracing session %s", session->name); |
7d268848 | 3272 | ret = start_kernel_session(ksession); |
9b6c7ec5 | 3273 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
3274 | goto error; |
3275 | } | |
2f77fc4b DG |
3276 | } |
3277 | ||
3278 | /* Flag session that trace should start automatically */ | |
3279 | if (usess) { | |
82b69413 JG |
3280 | int int_ret = ust_app_start_trace_all(usess); |
3281 | ||
3282 | if (int_ret < 0) { | |
f73fabfd | 3283 | ret = LTTNG_ERR_UST_START_FAIL; |
2f77fc4b DG |
3284 | goto error; |
3285 | } | |
3286 | } | |
3287 | ||
04ed9e10 JG |
3288 | /* |
3289 | * Open a packet in every stream of the session to ensure that viewers | |
3290 | * can correctly identify the boundaries of the periods during which | |
3291 | * tracing was active for this session. | |
3292 | */ | |
3293 | ret = session_open_packets(session); | |
3294 | if (ret != LTTNG_OK) { | |
3295 | goto error; | |
3296 | } | |
3297 | ||
5c408ad8 JD |
3298 | /* |
3299 | * Clear the flag that indicates that a rotation was done while the | |
3300 | * session was stopped. | |
3301 | */ | |
3302 | session->rotated_after_last_stop = false; | |
3303 | ||
259c2674 | 3304 | if (session->rotate_timer_period) { |
82b69413 JG |
3305 | int int_ret = timer_session_rotation_schedule_timer_start( |
3306 | session, session->rotate_timer_period); | |
3307 | ||
3308 | if (int_ret < 0) { | |
259c2674 JD |
3309 | ERR("Failed to enable rotate timer"); |
3310 | ret = LTTNG_ERR_UNK; | |
3311 | goto error; | |
3312 | } | |
3313 | } | |
3314 | ||
f73fabfd | 3315 | ret = LTTNG_OK; |
2f77fc4b DG |
3316 | |
3317 | error: | |
1f496244 JG |
3318 | if (ret == LTTNG_OK) { |
3319 | /* Flag this after a successful start. */ | |
3320 | session->has_been_started |= 1; | |
3321 | } else { | |
3322 | session->active = 0; | |
3323 | /* Restore initial state on error. */ | |
3324 | session->rotated_after_last_stop = | |
3325 | session_rotated_after_last_stop; | |
b02f5986 MD |
3326 | session->cleared_after_last_stop = |
3327 | session_cleared_after_last_stop; | |
1f496244 | 3328 | } |
7a24ece3 | 3329 | end: |
2f77fc4b DG |
3330 | return ret; |
3331 | } | |
3332 | ||
3333 | /* | |
3334 | * Command LTTNG_STOP_TRACE processed by the client thread. | |
3335 | */ | |
3336 | int cmd_stop_trace(struct ltt_session *session) | |
3337 | { | |
3338 | int ret; | |
2f77fc4b DG |
3339 | struct ltt_kernel_session *ksession; |
3340 | struct ltt_ust_session *usess; | |
3341 | ||
3342 | assert(session); | |
3343 | ||
4dbe1875 | 3344 | DBG("Begin stop session \"%s\" (id %" PRIu64 ")", session->name, session->id); |
2f77fc4b DG |
3345 | /* Short cut */ |
3346 | ksession = session->kernel_session; | |
3347 | usess = session->ust_session; | |
3348 | ||
8382cf6f DG |
3349 | /* Session is not active. Skip everythong and inform the client. */ |
3350 | if (!session->active) { | |
f73fabfd | 3351 | ret = LTTNG_ERR_TRACE_ALREADY_STOPPED; |
2f77fc4b DG |
3352 | goto error; |
3353 | } | |
3354 | ||
4dbe1875 MD |
3355 | ret = stop_kernel_session(ksession); |
3356 | if (ret != LTTNG_OK) { | |
3357 | goto error; | |
2f77fc4b DG |
3358 | } |
3359 | ||
14fb1ebe | 3360 | if (usess && usess->active) { |
2f77fc4b DG |
3361 | ret = ust_app_stop_trace_all(usess); |
3362 | if (ret < 0) { | |
f73fabfd | 3363 | ret = LTTNG_ERR_UST_STOP_FAIL; |
2f77fc4b DG |
3364 | goto error; |
3365 | } | |
3366 | } | |
3367 | ||
4dbe1875 MD |
3368 | DBG("Completed stop session \"%s\" (id %" PRIu64 ")", session->name, |
3369 | session->id); | |
8382cf6f DG |
3370 | /* Flag inactive after a successful stop. */ |
3371 | session->active = 0; | |
4dbe1875 | 3372 | ret = LTTNG_OK; |
2f77fc4b DG |
3373 | |
3374 | error: | |
3375 | return ret; | |
3376 | } | |
3377 | ||
3378 | /* | |
433f5ba9 JR |
3379 | * Set the base_path of the session only if subdir of a control uris is set. |
3380 | * Return LTTNG_OK on success, otherwise LTTNG_ERR_*. | |
2f77fc4b | 3381 | */ |
433f5ba9 JR |
3382 | static int set_session_base_path_from_uris(struct ltt_session *session, |
3383 | size_t nb_uri, | |
bda32d56 | 3384 | struct lttng_uri *uris) |
2f77fc4b | 3385 | { |
433f5ba9 JR |
3386 | int ret; |
3387 | size_t i; | |
2f77fc4b | 3388 | |
e3876bf0 JR |
3389 | for (i = 0; i < nb_uri; i++) { |
3390 | if (uris[i].stype != LTTNG_STREAM_CONTROL || | |
3391 | uris[i].subdir[0] == '\0') { | |
3392 | /* Not interested in these URIs */ | |
3393 | continue; | |
3394 | } | |
3395 | ||
3396 | if (session->base_path != NULL) { | |
3397 | free(session->base_path); | |
3398 | session->base_path = NULL; | |
3399 | } | |
3400 | ||
3401 | /* Set session base_path */ | |
3402 | session->base_path = strdup(uris[i].subdir); | |
3403 | if (!session->base_path) { | |
433f5ba9 JR |
3404 | PERROR("Failed to copy base path \"%s\" to session \"%s\"", |
3405 | uris[i].subdir, session->name); | |
3406 | ret = LTTNG_ERR_NOMEM; | |
e3876bf0 JR |
3407 | goto error; |
3408 | } | |
433f5ba9 JR |
3409 | DBG2("Setting base path \"%s\" for session \"%s\"", |
3410 | session->base_path, session->name); | |
3411 | } | |
3412 | ret = LTTNG_OK; | |
3413 | error: | |
3414 | return ret; | |
3415 | } | |
3416 | ||
3417 | /* | |
3418 | * Command LTTNG_SET_CONSUMER_URI processed by the client thread. | |
3419 | */ | |
3420 | int cmd_set_consumer_uri(struct ltt_session *session, size_t nb_uri, | |
3421 | struct lttng_uri *uris) | |
3422 | { | |
3423 | int ret, i; | |
3424 | struct ltt_kernel_session *ksess = session->kernel_session; | |
3425 | struct ltt_ust_session *usess = session->ust_session; | |
3426 | ||
3427 | assert(session); | |
3428 | assert(uris); | |
3429 | assert(nb_uri > 0); | |
3430 | ||
3431 | /* Can't set consumer URI if the session is active. */ | |
3432 | if (session->active) { | |
3433 | ret = LTTNG_ERR_TRACE_ALREADY_STARTED; | |
3434 | goto error; | |
3435 | } | |
3436 | ||
3437 | /* | |
3438 | * Set the session base path if any. This is done inside | |
3439 | * cmd_set_consumer_uri to preserve backward compatibility of the | |
3440 | * previous session creation api vs the session descriptor api. | |
3441 | */ | |
3442 | ret = set_session_base_path_from_uris(session, nb_uri, uris); | |
3443 | if (ret != LTTNG_OK) { | |
3444 | goto error; | |
e3876bf0 JR |
3445 | } |
3446 | ||
bda32d56 | 3447 | /* Set the "global" consumer URIs */ |
2f77fc4b | 3448 | for (i = 0; i < nb_uri; i++) { |
e3876bf0 JR |
3449 | ret = add_uri_to_consumer(session, session->consumer, &uris[i], |
3450 | LTTNG_DOMAIN_NONE); | |
a74934ba | 3451 | if (ret != LTTNG_OK) { |
2f77fc4b DG |
3452 | goto error; |
3453 | } | |
2f77fc4b DG |
3454 | } |
3455 | ||
bda32d56 JG |
3456 | /* Set UST session URIs */ |
3457 | if (session->ust_session) { | |
3458 | for (i = 0; i < nb_uri; i++) { | |
b178f53e | 3459 | ret = add_uri_to_consumer(session, |
bda32d56 | 3460 | session->ust_session->consumer, |
b178f53e | 3461 | &uris[i], LTTNG_DOMAIN_UST); |
bda32d56 JG |
3462 | if (ret != LTTNG_OK) { |
3463 | goto error; | |
3464 | } | |
3465 | } | |
3466 | } | |
3467 | ||
3468 | /* Set kernel session URIs */ | |
3469 | if (session->kernel_session) { | |
3470 | for (i = 0; i < nb_uri; i++) { | |
b178f53e | 3471 | ret = add_uri_to_consumer(session, |
bda32d56 | 3472 | session->kernel_session->consumer, |
b178f53e | 3473 | &uris[i], LTTNG_DOMAIN_KERNEL); |
bda32d56 JG |
3474 | if (ret != LTTNG_OK) { |
3475 | goto error; | |
3476 | } | |
3477 | } | |
3478 | } | |
3479 | ||
7ab70fe0 DG |
3480 | /* |
3481 | * Make sure to set the session in output mode after we set URI since a | |
3482 | * session can be created without URL (thus flagged in no output mode). | |
3483 | */ | |
3484 | session->output_traces = 1; | |
3485 | if (ksess) { | |
3486 | ksess->output_traces = 1; | |
bda32d56 JG |
3487 | } |
3488 | ||
3489 | if (usess) { | |
7ab70fe0 DG |
3490 | usess->output_traces = 1; |
3491 | } | |
3492 | ||
2f77fc4b | 3493 | /* All good! */ |
f73fabfd | 3494 | ret = LTTNG_OK; |
2f77fc4b DG |
3495 | |
3496 | error: | |
3497 | return ret; | |
3498 | } | |
3499 | ||
b178f53e JG |
3500 | static |
3501 | enum lttng_error_code set_session_output_from_descriptor( | |
3502 | struct ltt_session *session, | |
3503 | const struct lttng_session_descriptor *descriptor) | |
2f77fc4b DG |
3504 | { |
3505 | int ret; | |
b178f53e JG |
3506 | enum lttng_error_code ret_code = LTTNG_OK; |
3507 | enum lttng_session_descriptor_type session_type = | |
3508 | lttng_session_descriptor_get_type(descriptor); | |
3509 | enum lttng_session_descriptor_output_type output_type = | |
3510 | lttng_session_descriptor_get_output_type(descriptor); | |
3511 | struct lttng_uri uris[2] = {}; | |
3512 | size_t uri_count = 0; | |
3513 | ||
3514 | switch (output_type) { | |
3515 | case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NONE: | |
3516 | goto end; | |
3517 | case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_LOCAL: | |
3518 | lttng_session_descriptor_get_local_output_uri(descriptor, | |
3519 | &uris[0]); | |
3520 | uri_count = 1; | |
3521 | break; | |
3522 | case LTTNG_SESSION_DESCRIPTOR_OUTPUT_TYPE_NETWORK: | |
3523 | lttng_session_descriptor_get_network_output_uris(descriptor, | |
3524 | &uris[0], &uris[1]); | |
3525 | uri_count = 2; | |
3526 | break; | |
3527 | default: | |
3528 | ret_code = LTTNG_ERR_INVALID; | |
e32d7f27 | 3529 | goto end; |
2f77fc4b DG |
3530 | } |
3531 | ||
b178f53e JG |
3532 | switch (session_type) { |
3533 | case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT: | |
3534 | { | |
3535 | struct snapshot_output *new_output = NULL; | |
3536 | ||
3537 | new_output = snapshot_output_alloc(); | |
3538 | if (!new_output) { | |
3539 | ret_code = LTTNG_ERR_NOMEM; | |
3540 | goto end; | |
3541 | } | |
3542 | ||
3543 | ret = snapshot_output_init_with_uri(session, | |
3544 | DEFAULT_SNAPSHOT_MAX_SIZE, | |
3545 | NULL, uris, uri_count, session->consumer, | |
3546 | new_output, &session->snapshot); | |
3547 | if (ret < 0) { | |
3548 | ret_code = (ret == -ENOMEM) ? | |
3549 | LTTNG_ERR_NOMEM : LTTNG_ERR_INVALID; | |
3550 | snapshot_output_destroy(new_output); | |
3551 | goto end; | |
3552 | } | |
3553 | snapshot_add_output(&session->snapshot, new_output); | |
3554 | break; | |
3555 | } | |
3556 | case LTTNG_SESSION_DESCRIPTOR_TYPE_REGULAR: | |
3557 | case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE: | |
3558 | { | |
3559 | ret_code = cmd_set_consumer_uri(session, uri_count, uris); | |
3560 | break; | |
3561 | } | |
3562 | default: | |
3563 | ret_code = LTTNG_ERR_INVALID; | |
e32d7f27 | 3564 | goto end; |
2f77fc4b | 3565 | } |
b178f53e JG |
3566 | end: |
3567 | return ret_code; | |
3568 | } | |
3569 | ||
3570 | static | |
3571 | enum lttng_error_code cmd_create_session_from_descriptor( | |
3572 | struct lttng_session_descriptor *descriptor, | |
3573 | const lttng_sock_cred *creds, | |
3574 | const char *home_path) | |
3575 | { | |
3576 | int ret; | |
3577 | enum lttng_error_code ret_code; | |
3578 | const char *session_name; | |
3579 | struct ltt_session *new_session = NULL; | |
3580 | enum lttng_session_descriptor_status descriptor_status; | |
2f77fc4b | 3581 | |
e32d7f27 | 3582 | session_lock_list(); |
b178f53e JG |
3583 | if (home_path) { |
3584 | if (*home_path != '/') { | |
3585 | ERR("Home path provided by client is not absolute"); | |
3586 | ret_code = LTTNG_ERR_INVALID; | |
3587 | goto end; | |
3588 | } | |
3589 | } | |
2f77fc4b | 3590 | |
b178f53e JG |
3591 | descriptor_status = lttng_session_descriptor_get_session_name( |
3592 | descriptor, &session_name); | |
3593 | switch (descriptor_status) { | |
3594 | case LTTNG_SESSION_DESCRIPTOR_STATUS_OK: | |
3595 | break; | |
3596 | case LTTNG_SESSION_DESCRIPTOR_STATUS_UNSET: | |
3597 | session_name = NULL; | |
3598 | break; | |
3599 | default: | |
3600 | ret_code = LTTNG_ERR_INVALID; | |
3601 | goto end; | |
3602 | } | |
e3876bf0 | 3603 | |
b178f53e | 3604 | ret_code = session_create(session_name, creds->uid, creds->gid, |
e3876bf0 | 3605 | &new_session); |
b178f53e | 3606 | if (ret_code != LTTNG_OK) { |
e32d7f27 | 3607 | goto end; |
2f77fc4b DG |
3608 | } |
3609 | ||
b178f53e JG |
3610 | if (!session_name) { |
3611 | ret = lttng_session_descriptor_set_session_name(descriptor, | |
3612 | new_session->name); | |
3613 | if (ret) { | |
3614 | ret_code = LTTNG_ERR_SESSION_FAIL; | |
3615 | goto end; | |
3616 | } | |
3617 | } | |
3618 | ||
3619 | if (!lttng_session_descriptor_is_output_destination_initialized( | |
3620 | descriptor)) { | |
3621 | /* | |
3622 | * Only include the session's creation time in the output | |
3623 | * destination if the name of the session itself was | |
3624 | * not auto-generated. | |
3625 | */ | |
3626 | ret_code = lttng_session_descriptor_set_default_output( | |
3627 | descriptor, | |
3628 | session_name ? &new_session->creation_time : NULL, | |
3629 | home_path); | |
3630 | if (ret_code != LTTNG_OK) { | |
e32d7f27 | 3631 | goto end; |
2bba9e53 | 3632 | } |
2bba9e53 | 3633 | } else { |
b178f53e JG |
3634 | new_session->has_user_specified_directory = |
3635 | lttng_session_descriptor_has_output_directory( | |
3636 | descriptor); | |
2f77fc4b DG |
3637 | } |
3638 | ||
b178f53e JG |
3639 | switch (lttng_session_descriptor_get_type(descriptor)) { |
3640 | case LTTNG_SESSION_DESCRIPTOR_TYPE_SNAPSHOT: | |
3641 | new_session->snapshot_mode = 1; | |
3642 | break; | |
3643 | case LTTNG_SESSION_DESCRIPTOR_TYPE_LIVE: | |
3644 | new_session->live_timer = | |
3645 | lttng_session_descriptor_live_get_timer_interval( | |
3646 | descriptor); | |
3647 | break; | |
3648 | default: | |
3649 | break; | |
3650 | } | |
2f77fc4b | 3651 | |
b178f53e JG |
3652 | ret_code = set_session_output_from_descriptor(new_session, descriptor); |
3653 | if (ret_code != LTTNG_OK) { | |
3654 | goto end; | |
3655 | } | |
3656 | new_session->consumer->enabled = 1; | |
3657 | ret_code = LTTNG_OK; | |
e32d7f27 | 3658 | end: |
b178f53e JG |
3659 | /* Release reference provided by the session_create function. */ |
3660 | session_put(new_session); | |
3661 | if (ret_code != LTTNG_OK && new_session) { | |
3662 | /* Release the global reference on error. */ | |
3663 | session_destroy(new_session); | |
e32d7f27 | 3664 | } |
b178f53e JG |
3665 | session_unlock_list(); |
3666 | return ret_code; | |
2f77fc4b DG |
3667 | } |
3668 | ||
b178f53e JG |
3669 | enum lttng_error_code cmd_create_session(struct command_ctx *cmd_ctx, int sock, |
3670 | struct lttng_session_descriptor **return_descriptor) | |
27babd3a DG |
3671 | { |
3672 | int ret; | |
b178f53e JG |
3673 | size_t payload_size; |
3674 | struct lttng_dynamic_buffer payload; | |
3675 | struct lttng_buffer_view home_dir_view; | |
3676 | struct lttng_buffer_view session_descriptor_view; | |
3677 | struct lttng_session_descriptor *session_descriptor = NULL; | |
3678 | enum lttng_error_code ret_code; | |
3679 | ||
3680 | lttng_dynamic_buffer_init(&payload); | |
3a91de3a | 3681 | if (cmd_ctx->lsm.u.create_session.home_dir_size >= |
b178f53e JG |
3682 | LTTNG_PATH_MAX) { |
3683 | ret_code = LTTNG_ERR_INVALID; | |
3684 | goto error; | |
27babd3a | 3685 | } |
3a91de3a | 3686 | if (cmd_ctx->lsm.u.create_session.session_descriptor_size > |
b178f53e JG |
3687 | LTTNG_SESSION_DESCRIPTOR_MAX_LEN) { |
3688 | ret_code = LTTNG_ERR_INVALID; | |
3689 | goto error; | |
27babd3a DG |
3690 | } |
3691 | ||
3a91de3a JG |
3692 | payload_size = cmd_ctx->lsm.u.create_session.home_dir_size + |
3693 | cmd_ctx->lsm.u.create_session.session_descriptor_size; | |
b178f53e JG |
3694 | ret = lttng_dynamic_buffer_set_size(&payload, payload_size); |
3695 | if (ret) { | |
3696 | ret_code = LTTNG_ERR_NOMEM; | |
3697 | goto error; | |
27babd3a DG |
3698 | } |
3699 | ||
b178f53e JG |
3700 | ret = lttcomm_recv_unix_sock(sock, payload.data, payload.size); |
3701 | if (ret <= 0) { | |
3702 | ERR("Reception of session descriptor failed, aborting."); | |
3703 | ret_code = LTTNG_ERR_SESSION_FAIL; | |
3704 | goto error; | |
27babd3a DG |
3705 | } |
3706 | ||
b178f53e JG |
3707 | home_dir_view = lttng_buffer_view_from_dynamic_buffer( |
3708 | &payload, | |
3709 | 0, | |
3a91de3a | 3710 | cmd_ctx->lsm.u.create_session.home_dir_size); |
3e6e0df2 JG |
3711 | if (cmd_ctx->lsm.u.create_session.home_dir_size > 0 && |
3712 | !lttng_buffer_view_is_valid(&home_dir_view)) { | |
3713 | ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory"); | |
3714 | ret_code = LTTNG_ERR_INVALID_PROTOCOL; | |
3715 | goto error; | |
3716 | } | |
3717 | ||
b178f53e JG |
3718 | session_descriptor_view = lttng_buffer_view_from_dynamic_buffer( |
3719 | &payload, | |
3a91de3a JG |
3720 | cmd_ctx->lsm.u.create_session.home_dir_size, |
3721 | cmd_ctx->lsm.u.create_session.session_descriptor_size); | |
3e6e0df2 JG |
3722 | if (!lttng_buffer_view_is_valid(&session_descriptor_view)) { |
3723 | ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor"); | |
3724 | ret_code = LTTNG_ERR_INVALID_PROTOCOL; | |
3725 | goto error; | |
3726 | } | |
27babd3a | 3727 | |
b178f53e JG |
3728 | ret = lttng_session_descriptor_create_from_buffer( |
3729 | &session_descriptor_view, &session_descriptor); | |
3730 | if (ret < 0) { | |
3731 | ERR("Failed to create session descriptor from payload of \"create session\" command"); | |
3732 | ret_code = LTTNG_ERR_INVALID; | |
3733 | goto error; | |
3734 | } | |
27babd3a | 3735 | |
b178f53e JG |
3736 | /* |
3737 | * Sets the descriptor's auto-generated properties (name, output) if | |
3738 | * needed. | |
3739 | */ | |
3740 | ret_code = cmd_create_session_from_descriptor(session_descriptor, | |
3741 | &cmd_ctx->creds, | |
3742 | home_dir_view.size ? home_dir_view.data : NULL); | |
3743 | if (ret_code != LTTNG_OK) { | |
3744 | goto error; | |
e32d7f27 | 3745 | } |
b178f53e JG |
3746 | |
3747 | ret_code = LTTNG_OK; | |
3748 | *return_descriptor = session_descriptor; | |
3749 | session_descriptor = NULL; | |
3750 | error: | |
3751 | lttng_dynamic_buffer_reset(&payload); | |
3752 | lttng_session_descriptor_destroy(session_descriptor); | |
3753 | return ret_code; | |
27babd3a DG |
3754 | } |
3755 | ||
3e3665b8 JG |
3756 | static |
3757 | void cmd_destroy_session_reply(const struct ltt_session *session, | |
3758 | void *_reply_context) | |
3759 | { | |
3760 | int ret; | |
3761 | ssize_t comm_ret; | |
3762 | const struct cmd_destroy_session_reply_context *reply_context = | |
3763 | _reply_context; | |
3764 | struct lttng_dynamic_buffer payload; | |
3765 | struct lttcomm_session_destroy_command_header cmd_header; | |
3766 | struct lttng_trace_archive_location *location = NULL; | |
3767 | struct lttcomm_lttng_msg llm = { | |
3768 | .cmd_type = LTTNG_DESTROY_SESSION, | |
3285a971 | 3769 | .ret_code = reply_context->destruction_status, |
3e3665b8 JG |
3770 | .pid = UINT32_MAX, |
3771 | .cmd_header_size = | |
3772 | sizeof(struct lttcomm_session_destroy_command_header), | |
3773 | .data_size = 0, | |
3774 | }; | |
3775 | size_t payload_size_before_location; | |
3776 | ||
3777 | lttng_dynamic_buffer_init(&payload); | |
3778 | ||
3779 | ret = lttng_dynamic_buffer_append(&payload, &llm, sizeof(llm)); | |
0e270a1e | 3780 | if (ret) { |
3e3665b8 JG |
3781 | ERR("Failed to append session destruction message"); |
3782 | goto error; | |
0e270a1e | 3783 | } |
3e3665b8 JG |
3784 | |
3785 | cmd_header.rotation_state = | |
3786 | (int32_t) (reply_context->implicit_rotation_on_destroy ? | |
3787 | session->rotation_state : | |
3788 | LTTNG_ROTATION_STATE_NO_ROTATION); | |
3789 | ret = lttng_dynamic_buffer_append(&payload, &cmd_header, | |
3790 | sizeof(cmd_header)); | |
3791 | if (ret) { | |
3792 | ERR("Failed to append session destruction command header"); | |
3793 | goto error; | |
3794 | } | |
3795 | ||
3796 | if (!reply_context->implicit_rotation_on_destroy) { | |
3797 | DBG("No implicit rotation performed during the destruction of session \"%s\", sending reply", | |
3798 | session->name); | |
3799 | goto send_reply; | |
3800 | } | |
3801 | if (session->rotation_state != LTTNG_ROTATION_STATE_COMPLETED) { | |
3802 | DBG("Rotation state of session \"%s\" is not \"completed\", sending session destruction reply", | |
3803 | session->name); | |
3804 | goto send_reply; | |
3805 | } | |
3806 | ||
3807 | location = session_get_trace_archive_location(session); | |
3808 | if (!location) { | |
3809 | ERR("Failed to get the location of the trace archive produced during the destruction of session \"%s\"", | |
3810 | session->name); | |
3811 | goto error; | |
3812 | } | |
3813 | ||
3814 | payload_size_before_location = payload.size; | |
3815 | comm_ret = lttng_trace_archive_location_serialize(location, | |
3816 | &payload); | |
3817 | if (comm_ret < 0) { | |
3818 | ERR("Failed to serialize the location of the trace archive produced during the destruction of session \"%s\"", | |
3819 | session->name); | |
3820 | goto error; | |
3821 | } | |
3822 | /* Update the message to indicate the location's length. */ | |
3823 | ((struct lttcomm_lttng_msg *) payload.data)->data_size = | |
3824 | payload.size - payload_size_before_location; | |
3825 | send_reply: | |
3826 | comm_ret = lttcomm_send_unix_sock(reply_context->reply_sock_fd, | |
3827 | payload.data, payload.size); | |
3828 | if (comm_ret != (ssize_t) payload.size) { | |
3829 | ERR("Failed to send result of the destruction of session \"%s\" to client", | |
3830 | session->name); | |
3831 | } | |
3832 | error: | |
3833 | ret = close(reply_context->reply_sock_fd); | |
3834 | if (ret) { | |
3835 | PERROR("Failed to close client socket in deferred session destroy reply"); | |
3836 | } | |
3837 | lttng_dynamic_buffer_reset(&payload); | |
3838 | free(_reply_context); | |
3839 | } | |
3840 | ||
2f77fc4b DG |
3841 | /* |
3842 | * Command LTTNG_DESTROY_SESSION processed by the client thread. | |
a9ad0c8f MD |
3843 | * |
3844 | * Called with session lock held. | |
2f77fc4b | 3845 | */ |
e32d7f27 | 3846 | int cmd_destroy_session(struct ltt_session *session, |
3e3665b8 JG |
3847 | struct notification_thread_handle *notification_thread_handle, |
3848 | int *sock_fd) | |
2f77fc4b DG |
3849 | { |
3850 | int ret; | |
3285a971 | 3851 | enum lttng_error_code destruction_last_error = LTTNG_OK; |
3e3665b8 JG |
3852 | struct cmd_destroy_session_reply_context *reply_context = NULL; |
3853 | ||
3854 | if (sock_fd) { | |
3855 | reply_context = zmalloc(sizeof(*reply_context)); | |
3856 | if (!reply_context) { | |
3857 | ret = LTTNG_ERR_NOMEM; | |
3858 | goto end; | |
3859 | } | |
3860 | reply_context->reply_sock_fd = *sock_fd; | |
3861 | } | |
2f77fc4b DG |
3862 | |
3863 | /* Safety net */ | |
3864 | assert(session); | |
3865 | ||
3e3665b8 JG |
3866 | DBG("Begin destroy session %s (id %" PRIu64 ")", session->name, |
3867 | session->id); | |
3868 | if (session->active) { | |
3869 | DBG("Session \"%s\" is active, attempting to stop it before destroying it", | |
3870 | session->name); | |
3871 | ret = cmd_stop_trace(session); | |
3872 | if (ret != LTTNG_OK && ret != LTTNG_ERR_TRACE_ALREADY_STOPPED) { | |
3873 | /* Carry on with the destruction of the session. */ | |
3874 | ERR("Failed to stop session \"%s\" as part of its destruction: %s", | |
3875 | session->name, lttng_strerror(-ret)); | |
3285a971 | 3876 | destruction_last_error = ret; |
3e3665b8 JG |
3877 | } |
3878 | } | |
5c408ad8 | 3879 | |
92816cc3 JG |
3880 | if (session->rotation_schedule_timer_enabled) { |
3881 | if (timer_session_rotation_schedule_timer_stop( | |
3882 | session)) { | |
3883 | ERR("Failed to stop the \"rotation schedule\" timer of session %s", | |
3884 | session->name); | |
3285a971 | 3885 | destruction_last_error = LTTNG_ERR_TIMER_STOP_ERROR; |
92816cc3 | 3886 | } |
259c2674 JD |
3887 | } |
3888 | ||
90936dcf JD |
3889 | if (session->rotate_size) { |
3890 | unsubscribe_session_consumed_size_rotation(session, notification_thread_handle); | |
3891 | session->rotate_size = 0; | |
3892 | } | |
3893 | ||
a7ceb342 | 3894 | if (session->rotated && session->current_trace_chunk && session->output_traces) { |
b5893d8e JG |
3895 | /* |
3896 | * Perform a last rotation on destruction if rotations have | |
3897 | * occurred during the session's lifetime. | |
3898 | */ | |
343defc2 MD |
3899 | ret = cmd_rotate_session(session, NULL, false, |
3900 | LTTNG_TRACE_CHUNK_COMMAND_TYPE_MOVE_TO_COMPLETED); | |
d2956687 JG |
3901 | if (ret != LTTNG_OK) { |
3902 | ERR("Failed to perform an implicit rotation as part of the destruction of session \"%s\": %s", | |
3903 | session->name, lttng_strerror(-ret)); | |
3285a971 | 3904 | destruction_last_error = -ret; |
124473a3 | 3905 | } |
0e270a1e | 3906 | if (reply_context) { |
3e3665b8 | 3907 | reply_context->implicit_rotation_on_destroy = true; |
0e270a1e JG |
3908 | } |
3909 | } else if (session->has_been_started && session->current_trace_chunk) { | |
7fdbed1c JG |
3910 | /* |
3911 | * The user has not triggered a session rotation. However, to | |
3912 | * ensure all data has been consumed, the session is rotated | |
3913 | * to a 'null' trace chunk before it is destroyed. | |
3914 | * | |
3915 | * This is a "quiet" rotation meaning that no notification is | |
3916 | * emitted and no renaming of the current trace chunk takes | |
3917 | * place. | |
3918 | */ | |
343defc2 MD |
3919 | ret = cmd_rotate_session(session, NULL, true, |
3920 | LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION); | |
53fb6336 JG |
3921 | /* |
3922 | * Rotation operations may not be supported by the kernel | |
3923 | * tracer. Hence, do not consider this implicit rotation as | |
3924 | * a session destruction error. The library has already stopped | |
3925 | * the session and waited for pending data; there is nothing | |
3926 | * left to do but complete the destruction of the session. | |
3927 | */ | |
3928 | if (ret != LTTNG_OK && | |
3929 | ret != -LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL) { | |
7fdbed1c | 3930 | ERR("Failed to perform a quiet rotation as part of the destruction of session \"%s\": %s", |
53fb6336 | 3931 | session->name, lttng_strerror(ret)); |
3285a971 | 3932 | destruction_last_error = -ret; |
7fdbed1c JG |
3933 | } |
3934 | } | |
5c408ad8 | 3935 | |
a503e1ef JG |
3936 | if (session->shm_path[0]) { |
3937 | /* | |
3938 | * When a session is created with an explicit shm_path, | |
3939 | * the consumer daemon will create its shared memory files | |
3940 | * at that location and will *not* unlink them. This is normal | |
3941 | * as the intention of that feature is to make it possible | |
3942 | * to retrieve the content of those files should a crash occur. | |
3943 | * | |
3944 | * To ensure the content of those files can be used, the | |
3945 | * sessiond daemon will replicate the content of the metadata | |
3946 | * cache in a metadata file. | |
3947 | * | |
3948 | * On clean-up, it is expected that the consumer daemon will | |
3949 | * unlink the shared memory files and that the session daemon | |
3950 | * will unlink the metadata file. Then, the session's directory | |
3951 | * in the shm path can be removed. | |
3952 | * | |
3953 | * Unfortunately, a flaw in the design of the sessiond's and | |
3954 | * consumerd's tear down of channels makes it impossible to | |
3955 | * determine when the sessiond _and_ the consumerd have both | |
3956 | * destroyed their representation of a channel. For one, the | |
3957 | * unlinking, close, and rmdir happen in deferred 'call_rcu' | |
3958 | * callbacks in both daemons. | |
3959 | * | |
3960 | * However, it is also impossible for the sessiond to know when | |
3961 | * the consumer daemon is done destroying its channel(s) since | |
3962 | * it occurs as a reaction to the closing of the channel's file | |
3963 | * descriptor. There is no resulting communication initiated | |
3964 | * from the consumerd to the sessiond to confirm that the | |
3965 | * operation is completed (and was successful). | |
3966 | * | |
3967 | * Until this is all fixed, the session daemon checks for the | |
3968 | * removal of the session's shm path which makes it possible | |
3969 | * to safely advertise a session as having been destroyed. | |
3970 | * | |
3971 | * Prior to this fix, it was not possible to reliably save | |
3972 | * a session making use of the --shm-path option, destroy it, | |
3973 | * and load it again. This is because the creation of the | |
3974 | * session would fail upon seeing the session's shm path | |
3975 | * already in existence. | |
3976 | * | |
3977 | * Note that none of the error paths in the check for the | |
3978 | * directory's existence return an error. This is normal | |
3979 | * as there isn't much that can be done. The session will | |
3980 | * be destroyed properly, except that we can't offer the | |
3981 | * guarantee that the same session can be re-created. | |
3982 | */ | |
3983 | current_completion_handler = &destroy_completion_handler.handler; | |
3984 | ret = lttng_strncpy(destroy_completion_handler.shm_path, | |
3985 | session->shm_path, | |
3986 | sizeof(destroy_completion_handler.shm_path)); | |
3987 | assert(!ret); | |
3988 | } | |
e32d7f27 JG |
3989 | |
3990 | /* | |
3991 | * The session is destroyed. However, note that the command context | |
3992 | * still holds a reference to the session, thus delaying its destruction | |
3993 | * _at least_ up to the point when that reference is released. | |
3994 | */ | |
3995 | session_destroy(session); | |
3e3665b8 | 3996 | if (reply_context) { |
3285a971 | 3997 | reply_context->destruction_status = destruction_last_error; |
3e3665b8 JG |
3998 | ret = session_add_destroy_notifier(session, |
3999 | cmd_destroy_session_reply, | |
4000 | (void *) reply_context); | |
4001 | if (ret) { | |
4002 | ret = LTTNG_ERR_FATAL; | |
4003 | goto end; | |
4004 | } else { | |
4005 | *sock_fd = -1; | |
4006 | } | |
0e270a1e JG |
4007 | } |
4008 | ret = LTTNG_OK; | |
3e3665b8 | 4009 | end: |
2f77fc4b DG |
4010 | return ret; |
4011 | } | |
4012 | ||
2f77fc4b DG |
4013 | /* |
4014 | * Command LTTNG_REGISTER_CONSUMER processed by the client thread. | |
4015 | */ | |
56a37563 JG |
4016 | int cmd_register_consumer(struct ltt_session *session, |
4017 | enum lttng_domain_type domain, const char *sock_path, | |
4018 | struct consumer_data *cdata) | |
2f77fc4b DG |
4019 | { |
4020 | int ret, sock; | |
dd81b457 | 4021 | struct consumer_socket *socket = NULL; |
2f77fc4b DG |
4022 | |
4023 | assert(session); | |
4024 | assert(cdata); | |
4025 | assert(sock_path); | |
4026 | ||
4027 | switch (domain) { | |
4028 | case LTTNG_DOMAIN_KERNEL: | |
4029 | { | |
4030 | struct ltt_kernel_session *ksess = session->kernel_session; | |
4031 | ||
4032 | assert(ksess); | |
4033 | ||
4034 | /* Can't register a consumer if there is already one */ | |
4035 | if (ksess->consumer_fds_sent != 0) { | |
f73fabfd | 4036 | ret = LTTNG_ERR_KERN_CONSUMER_FAIL; |
2f77fc4b DG |
4037 | goto error; |
4038 | } | |
4039 | ||
4040 | sock = lttcomm_connect_unix_sock(sock_path); | |
4041 | if (sock < 0) { | |
f73fabfd | 4042 | ret = LTTNG_ERR_CONNECT_FAIL; |
2f77fc4b DG |
4043 | goto error; |
4044 | } | |
4ce514c4 | 4045 | cdata->cmd_sock = sock; |
2f77fc4b | 4046 | |
4ce514c4 | 4047 | socket = consumer_allocate_socket(&cdata->cmd_sock); |
2f77fc4b | 4048 | if (socket == NULL) { |
f66c074c DG |
4049 | ret = close(sock); |
4050 | if (ret < 0) { | |
4051 | PERROR("close register consumer"); | |
4052 | } | |
4ce514c4 | 4053 | cdata->cmd_sock = -1; |
f73fabfd | 4054 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
4055 | goto error; |
4056 | } | |
4057 | ||
4058 | socket->lock = zmalloc(sizeof(pthread_mutex_t)); | |
4059 | if (socket->lock == NULL) { | |
4060 | PERROR("zmalloc pthread mutex"); | |
f73fabfd | 4061 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
4062 | goto error; |
4063 | } | |
4064 | pthread_mutex_init(socket->lock, NULL); | |
4065 | socket->registered = 1; | |
4066 | ||
4067 | rcu_read_lock(); | |
4068 | consumer_add_socket(socket, ksess->consumer); | |
4069 | rcu_read_unlock(); | |
4070 | ||
4071 | pthread_mutex_lock(&cdata->pid_mutex); | |
4072 | cdata->pid = -1; | |
4073 | pthread_mutex_unlock(&cdata->pid_mutex); | |
4074 | ||
4075 | break; | |
4076 | } | |
4077 | default: | |
4078 | /* TODO: Userspace tracing */ | |
f73fabfd | 4079 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
4080 | goto error; |
4081 | } | |
4082 | ||
dd81b457 | 4083 | return LTTNG_OK; |
2f77fc4b DG |
4084 | |
4085 | error: | |
dd81b457 DG |
4086 | if (socket) { |
4087 | consumer_destroy_socket(socket); | |
4088 | } | |
2f77fc4b DG |
4089 | return ret; |
4090 | } | |
4091 | ||
4092 | /* | |
4093 | * Command LTTNG_LIST_DOMAINS processed by the client thread. | |
4094 | */ | |
4095 | ssize_t cmd_list_domains(struct ltt_session *session, | |
4096 | struct lttng_domain **domains) | |
4097 | { | |
4098 | int ret, index = 0; | |
4099 | ssize_t nb_dom = 0; | |
fefd409b DG |
4100 | struct agent *agt; |
4101 | struct lttng_ht_iter iter; | |
2f77fc4b DG |
4102 | |
4103 | if (session->kernel_session != NULL) { | |
4104 | DBG3("Listing domains found kernel domain"); | |
4105 | nb_dom++; | |
4106 | } | |
4107 | ||
4108 | if (session->ust_session != NULL) { | |
4109 | DBG3("Listing domains found UST global domain"); | |
4110 | nb_dom++; | |
3c6a091f | 4111 | |
e0a74f01 | 4112 | rcu_read_lock(); |
fefd409b DG |
4113 | cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter, |
4114 | agt, node.node) { | |
4115 | if (agt->being_used) { | |
4116 | nb_dom++; | |
4117 | } | |
3c6a091f | 4118 | } |
e0a74f01 | 4119 | rcu_read_unlock(); |
2f77fc4b DG |
4120 | } |
4121 | ||
fa64dfb4 JG |
4122 | if (!nb_dom) { |
4123 | goto end; | |
4124 | } | |
4125 | ||
2f77fc4b DG |
4126 | *domains = zmalloc(nb_dom * sizeof(struct lttng_domain)); |
4127 | if (*domains == NULL) { | |
f73fabfd | 4128 | ret = LTTNG_ERR_FATAL; |
2f77fc4b DG |
4129 | goto error; |
4130 | } | |
4131 | ||
4132 | if (session->kernel_session != NULL) { | |
4133 | (*domains)[index].type = LTTNG_DOMAIN_KERNEL; | |
b5edb9e8 PP |
4134 | |
4135 | /* Kernel session buffer type is always GLOBAL */ | |
4136 | (*domains)[index].buf_type = LTTNG_BUFFER_GLOBAL; | |
4137 | ||
2f77fc4b DG |
4138 | index++; |
4139 | } | |
4140 | ||
4141 | if (session->ust_session != NULL) { | |
4142 | (*domains)[index].type = LTTNG_DOMAIN_UST; | |
88c5f0d8 | 4143 | (*domains)[index].buf_type = session->ust_session->buffer_type; |
2f77fc4b | 4144 | index++; |
3c6a091f | 4145 | |
e0a74f01 | 4146 | rcu_read_lock(); |
fefd409b DG |
4147 | cds_lfht_for_each_entry(session->ust_session->agents->ht, &iter.iter, |
4148 | agt, node.node) { | |
4149 | if (agt->being_used) { | |
4150 | (*domains)[index].type = agt->domain; | |
4151 | (*domains)[index].buf_type = session->ust_session->buffer_type; | |
4152 | index++; | |
4153 | } | |
3c6a091f | 4154 | } |
e0a74f01 | 4155 | rcu_read_unlock(); |
2f77fc4b | 4156 | } |
fa64dfb4 | 4157 | end: |
2f77fc4b DG |
4158 | return nb_dom; |
4159 | ||
4160 | error: | |
f73fabfd DG |
4161 | /* Return negative value to differentiate return code */ |
4162 | return -ret; | |
2f77fc4b DG |
4163 | } |
4164 | ||
4165 | ||
4166 | /* | |
4167 | * Command LTTNG_LIST_CHANNELS processed by the client thread. | |
4168 | */ | |
56a37563 JG |
4169 | ssize_t cmd_list_channels(enum lttng_domain_type domain, |
4170 | struct ltt_session *session, struct lttng_channel **channels) | |
2f77fc4b | 4171 | { |
53e367f9 | 4172 | ssize_t nb_chan = 0, payload_size = 0, ret; |
2f77fc4b DG |
4173 | |
4174 | switch (domain) { | |
4175 | case LTTNG_DOMAIN_KERNEL: | |
4176 | if (session->kernel_session != NULL) { | |
4177 | nb_chan = session->kernel_session->channel_count; | |
4178 | } | |
4179 | DBG3("Number of kernel channels %zd", nb_chan); | |
c7d620a2 | 4180 | if (nb_chan <= 0) { |
53e367f9 JG |
4181 | ret = -LTTNG_ERR_KERN_CHAN_NOT_FOUND; |
4182 | goto end; | |
c7d620a2 | 4183 | } |
2f77fc4b DG |
4184 | break; |
4185 | case LTTNG_DOMAIN_UST: | |
4186 | if (session->ust_session != NULL) { | |
7ffc31a2 | 4187 | rcu_read_lock(); |
2f77fc4b | 4188 | nb_chan = lttng_ht_get_count( |
7ffc31a2 JG |
4189 | session->ust_session->domain_global.channels); |
4190 | rcu_read_unlock(); | |
2f77fc4b DG |
4191 | } |
4192 | DBG3("Number of UST global channels %zd", nb_chan); | |
ade7ce52 | 4193 | if (nb_chan < 0) { |
53e367f9 JG |
4194 | ret = -LTTNG_ERR_UST_CHAN_NOT_FOUND; |
4195 | goto end; | |
c7d620a2 | 4196 | } |
2f77fc4b DG |
4197 | break; |
4198 | default: | |
53e367f9 JG |
4199 | ret = -LTTNG_ERR_UND; |
4200 | goto end; | |
2f77fc4b DG |
4201 | } |
4202 | ||
4203 | if (nb_chan > 0) { | |
53e367f9 | 4204 | const size_t channel_size = sizeof(struct lttng_channel) + |
cf0bcb51 JG |
4205 | sizeof(struct lttng_channel_extended); |
4206 | struct lttng_channel_extended *channel_exts; | |
53e367f9 JG |
4207 | |
4208 | payload_size = nb_chan * channel_size; | |
4209 | *channels = zmalloc(payload_size); | |
2f77fc4b | 4210 | if (*channels == NULL) { |
53e367f9 JG |
4211 | ret = -LTTNG_ERR_FATAL; |
4212 | goto end; | |
2f77fc4b DG |
4213 | } |
4214 | ||
53e367f9 JG |
4215 | channel_exts = ((void *) *channels) + |
4216 | (nb_chan * sizeof(struct lttng_channel)); | |
d449df4a MD |
4217 | ret = list_lttng_channels(domain, session, *channels, channel_exts); |
4218 | if (ret != LTTNG_OK) { | |
4219 | free(*channels); | |
4220 | *channels = NULL; | |
4221 | goto end; | |
4222 | } | |
53e367f9 JG |
4223 | } else { |
4224 | *channels = NULL; | |
2f77fc4b DG |
4225 | } |
4226 | ||
53e367f9 JG |
4227 | ret = payload_size; |
4228 | end: | |
4229 | return ret; | |
2f77fc4b DG |
4230 | } |
4231 | ||
c3e68e71 JR |
4232 | enum lttng_error_code cmd_list_maps(enum lttng_domain_type domain, |
4233 | struct ltt_session *session, | |
4234 | struct lttng_map_list **return_map_list) | |
4235 | { | |
4236 | enum lttng_error_code ret_code; | |
4237 | struct lttng_map_list *map_list = NULL; | |
4238 | ||
4239 | map_list = lttng_map_list_create(); | |
4240 | ||
4241 | switch (domain) { | |
4242 | case LTTNG_DOMAIN_KERNEL: | |
4243 | if (session->kernel_session != NULL) { | |
4244 | struct ltt_kernel_map *kmap; | |
4245 | cds_list_for_each_entry(kmap, | |
4246 | &session->kernel_session->map_list.head, list) { | |
4247 | enum lttng_map_status map_status; | |
4248 | map_status = lttng_map_list_add(map_list, kmap->map); | |
4249 | if (map_status != LTTNG_MAP_STATUS_OK) { | |
4250 | ERR("Error appending kernel map to list"); | |
4251 | ret_code = LTTNG_ERR_FATAL; | |
4252 | break; | |
4253 | } | |
4254 | } | |
4255 | ||
4256 | } | |
4257 | break; | |
4258 | case LTTNG_DOMAIN_UST: | |
4259 | { | |
4260 | struct ltt_ust_map *umap; | |
4261 | struct lttng_ht_iter iter; | |
4262 | ||
4263 | rcu_read_lock(); | |
4264 | cds_lfht_for_each_entry(session->ust_session->domain_global.maps->ht, | |
4265 | &iter.iter, umap, node.node) { | |
4266 | enum lttng_map_status map_status; | |
4267 | map_status = lttng_map_list_add(map_list, umap->map); | |
4268 | if (map_status != LTTNG_MAP_STATUS_OK) { | |
4269 | ERR("Error appending UST map to list"); | |
4270 | ret_code = LTTNG_ERR_FATAL; | |
4271 | break; | |
4272 | } | |
4273 | } | |
4274 | rcu_read_unlock(); | |
4275 | break; | |
4276 | } | |
4277 | default: | |
4278 | ret_code = LTTNG_ERR_UND; | |
4279 | goto end; | |
4280 | } | |
4281 | ||
4282 | *return_map_list = map_list; | |
4283 | map_list = NULL; | |
4284 | ret_code = LTTNG_OK; | |
4285 | end: | |
4286 | lttng_map_list_destroy(map_list); | |
4287 | return ret_code; | |
4288 | } | |
4289 | ||
2f77fc4b DG |
4290 | /* |
4291 | * Command LTTNG_LIST_EVENTS processed by the client thread. | |
4292 | */ | |
56a37563 JG |
4293 | ssize_t cmd_list_events(enum lttng_domain_type domain, |
4294 | struct ltt_session *session, char *channel_name, | |
e368fb43 | 4295 | struct lttng_payload *payload) |
2f77fc4b DG |
4296 | { |
4297 | int ret = 0; | |
e368fb43 JG |
4298 | ssize_t nb_events = 0; |
4299 | struct lttcomm_event_command_header cmd_header = {}; | |
4300 | const size_t cmd_header_offset = payload->buffer.size; | |
4301 | ||
4302 | ret = lttng_dynamic_buffer_append( | |
4303 | &payload->buffer, &cmd_header, sizeof(cmd_header)); | |
4304 | if (ret) { | |
4305 | ret = LTTNG_ERR_NOMEM; | |
4306 | goto error; | |
4307 | } | |
2f77fc4b DG |
4308 | |
4309 | switch (domain) { | |
4310 | case LTTNG_DOMAIN_KERNEL: | |
4311 | if (session->kernel_session != NULL) { | |
e368fb43 JG |
4312 | nb_events = list_lttng_kernel_events(channel_name, |
4313 | session->kernel_session, payload); | |
2f77fc4b DG |
4314 | } |
4315 | break; | |
4316 | case LTTNG_DOMAIN_UST: | |
4317 | { | |
4318 | if (session->ust_session != NULL) { | |
e368fb43 JG |
4319 | nb_events = list_lttng_ust_global_events(channel_name, |
4320 | &session->ust_session->domain_global, | |
4321 | payload); | |
2f77fc4b DG |
4322 | } |
4323 | break; | |
4324 | } | |
5cdb6027 | 4325 | case LTTNG_DOMAIN_LOG4J: |
3c6a091f | 4326 | case LTTNG_DOMAIN_JUL: |
0e115563 | 4327 | case LTTNG_DOMAIN_PYTHON: |
3c6a091f | 4328 | if (session->ust_session) { |
fefd409b DG |
4329 | struct lttng_ht_iter iter; |
4330 | struct agent *agt; | |
4331 | ||
b11feea5 | 4332 | rcu_read_lock(); |
fefd409b DG |
4333 | cds_lfht_for_each_entry(session->ust_session->agents->ht, |
4334 | &iter.iter, agt, node.node) { | |
1dfd9906 | 4335 | if (agt->domain == domain) { |
e368fb43 JG |
4336 | nb_events = list_lttng_agent_events( |
4337 | agt, payload); | |
1dfd9906 JG |
4338 | break; |
4339 | } | |
fefd409b | 4340 | } |
b11feea5 | 4341 | rcu_read_unlock(); |
3c6a091f DG |
4342 | } |
4343 | break; | |
2f77fc4b | 4344 | default: |
f73fabfd | 4345 | ret = LTTNG_ERR_UND; |
2f77fc4b DG |
4346 | goto error; |
4347 | } | |
4348 | ||
e368fb43 JG |
4349 | ((struct lttcomm_event_command_header *) (payload->buffer.data + |
4350 | cmd_header_offset))->nb_events = (uint32_t) nb_events; | |
4351 | ||
4352 | return nb_events; | |
2f77fc4b DG |
4353 | |
4354 | error: | |
f73fabfd DG |
4355 | /* Return negative value to differentiate return code */ |
4356 | return -ret; | |
2f77fc4b DG |
4357 | } |
4358 | ||
4359 | /* | |
4360 | * Using the session list, filled a lttng_session array to send back to the | |
4361 | * client for session listing. | |
4362 | * | |
4363 | * The session list lock MUST be acquired before calling this function. Use | |
4364 | * session_lock_list() and session_unlock_list(). | |
4365 | */ | |
b178f53e JG |
4366 | void cmd_list_lttng_sessions(struct lttng_session *sessions, |
4367 | size_t session_count, uid_t uid, gid_t gid) | |
2f77fc4b DG |
4368 | { |
4369 | int ret; | |
4370 | unsigned int i = 0; | |
4371 | struct ltt_session *session; | |
4372 | struct ltt_session_list *list = session_get_list(); | |
0e270a1e | 4373 | struct lttng_session_extended *extended = |
b178f53e | 4374 | (typeof(extended)) (&sessions[session_count]); |
2f77fc4b DG |
4375 | |
4376 | DBG("Getting all available session for UID %d GID %d", | |
4377 | uid, gid); | |
4378 | /* | |
4379 | * Iterate over session list and append data after the control struct in | |
4380 | * the buffer. | |
4381 | */ | |
4382 | cds_list_for_each_entry(session, &list->head, list) { | |
e32d7f27 JG |
4383 | if (!session_get(session)) { |
4384 | continue; | |
4385 | } | |
2f77fc4b DG |
4386 | /* |
4387 | * Only list the sessions the user can control. | |
4388 | */ | |
d7b377ed | 4389 | if (!session_access_ok(session, uid) || |
e32d7f27 JG |
4390 | session->destroyed) { |
4391 | session_put(session); | |
2f77fc4b DG |
4392 | continue; |
4393 | } | |
4394 | ||
4395 | struct ltt_kernel_session *ksess = session->kernel_session; | |
4396 | struct ltt_ust_session *usess = session->ust_session; | |
4397 | ||
4398 | if (session->consumer->type == CONSUMER_DST_NET || | |
4399 | (ksess && ksess->consumer->type == CONSUMER_DST_NET) || | |
4400 | (usess && usess->consumer->type == CONSUMER_DST_NET)) { | |
4401 | ret = build_network_session_path(sessions[i].path, | |
dec56f6c | 4402 | sizeof(sessions[i].path), session); |
2f77fc4b | 4403 | } else { |
dec56f6c | 4404 | ret = snprintf(sessions[i].path, sizeof(sessions[i].path), "%s", |
366a9222 | 4405 | session->consumer->dst.session_root_path); |
2f77fc4b DG |
4406 | } |
4407 | if (ret < 0) { | |
4408 | PERROR("snprintf session path"); | |
e32d7f27 | 4409 | session_put(session); |
2f77fc4b DG |
4410 | continue; |
4411 | } | |
4412 | ||
4413 | strncpy(sessions[i].name, session->name, NAME_MAX); | |
4414 | sessions[i].name[NAME_MAX - 1] = '\0'; | |
8382cf6f | 4415 | sessions[i].enabled = session->active; |
2cbf8fed | 4416 | sessions[i].snapshot_mode = session->snapshot_mode; |
8960e9cd | 4417 | sessions[i].live_timer_interval = session->live_timer; |
b178f53e JG |
4418 | extended[i].creation_time.value = (uint64_t) session->creation_time; |
4419 | extended[i].creation_time.is_set = 1; | |
2f77fc4b | 4420 | i++; |
e32d7f27 | 4421 | session_put(session); |
2f77fc4b DG |
4422 | } |
4423 | } | |
4424 | ||
806e2684 | 4425 | /* |
6d805429 | 4426 | * Command LTTNG_DATA_PENDING returning 0 if the data is NOT pending meaning |
d3f14b8a | 4427 | * ready for trace analysis (or any kind of reader) or else 1 for pending data. |
806e2684 | 4428 | */ |
6d805429 | 4429 | int cmd_data_pending(struct ltt_session *session) |
806e2684 DG |
4430 | { |
4431 | int ret; | |
4432 | struct ltt_kernel_session *ksess = session->kernel_session; | |
4433 | struct ltt_ust_session *usess = session->ust_session; | |
4434 | ||
4435 | assert(session); | |
4436 | ||
5c408ad8 JD |
4437 | DBG("Data pending for session %s", session->name); |
4438 | ||
806e2684 | 4439 | /* Session MUST be stopped to ask for data availability. */ |
8382cf6f | 4440 | if (session->active) { |
806e2684 DG |
4441 | ret = LTTNG_ERR_SESSION_STARTED; |
4442 | goto error; | |
3a89d11a DG |
4443 | } else { |
4444 | /* | |
4445 | * If stopped, just make sure we've started before else the above call | |
4446 | * will always send that there is data pending. | |
4447 | * | |
4448 | * The consumer assumes that when the data pending command is received, | |
4449 | * the trace has been started before or else no output data is written | |
4450 | * by the streams which is a condition for data pending. So, this is | |
4451 | * *VERY* important that we don't ask the consumer before a start | |
4452 | * trace. | |
4453 | */ | |
8382cf6f | 4454 | if (!session->has_been_started) { |
3a89d11a DG |
4455 | ret = 0; |
4456 | goto error; | |
4457 | } | |
806e2684 DG |
4458 | } |
4459 | ||
92816cc3 JG |
4460 | /* A rotation is still pending, we have to wait. */ |
4461 | if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) { | |
5c408ad8 JD |
4462 | DBG("Rotate still pending for session %s", session->name); |
4463 | ret = 1; | |
4464 | goto error; | |
4465 | } | |
4466 | ||
806e2684 | 4467 | if (ksess && ksess->consumer) { |
6d805429 DG |
4468 | ret = consumer_is_data_pending(ksess->id, ksess->consumer); |
4469 | if (ret == 1) { | |
806e2684 DG |
4470 | /* Data is still being extracted for the kernel. */ |
4471 | goto error; | |
4472 | } | |
4473 | } | |
4474 | ||
4475 | if (usess && usess->consumer) { | |
6d805429 DG |
4476 | ret = consumer_is_data_pending(usess->id, usess->consumer); |
4477 | if (ret == 1) { | |
806e2684 DG |
4478 | /* Data is still being extracted for the kernel. */ |
4479 | goto error; | |
4480 | } | |
4481 | } | |
4482 | ||
4483 | /* Data is ready to be read by a viewer */ | |
6d805429 | 4484 | ret = 0; |
806e2684 DG |
4485 | |
4486 | error: | |
4487 | return ret; | |
4488 | } | |
4489 | ||
6dc3064a DG |
4490 | /* |
4491 | * Command LTTNG_SNAPSHOT_ADD_OUTPUT from the lttng ctl library. | |
4492 | * | |
4493 | * Return LTTNG_OK on success or else a LTTNG_ERR code. | |
4494 | */ | |
4495 | int cmd_snapshot_add_output(struct ltt_session *session, | |
df4f5a87 | 4496 | const struct lttng_snapshot_output *output, uint32_t *id) |
6dc3064a DG |
4497 | { |
4498 | int ret; | |
4499 | struct snapshot_output *new_output; | |
4500 | ||
4501 | assert(session); | |
4502 | assert(output); | |
4503 | ||
4504 | DBG("Cmd snapshot add output for session %s", session->name); | |
4505 | ||
4506 | /* | |
903ef685 | 4507 | * Can't create an output if the session is not set in no-output mode. |
6dc3064a DG |
4508 | */ |
4509 | if (session->output_traces) { | |
903ef685 | 4510 | ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
6dc3064a DG |
4511 | goto error; |
4512 | } | |
4513 | ||
54213acc JG |
4514 | if (session->has_non_mmap_channel) { |
4515 | ret = LTTNG_ERR_SNAPSHOT_UNSUPPORTED; | |
4516 | goto error; | |
4517 | } | |
4518 | ||
6dc3064a DG |
4519 | /* Only one output is allowed until we have the "tee" feature. */ |
4520 | if (session->snapshot.nb_output == 1) { | |
4521 | ret = LTTNG_ERR_SNAPSHOT_OUTPUT_EXIST; | |
4522 | goto error; | |
4523 | } | |
4524 | ||
4525 | new_output = snapshot_output_alloc(); | |
4526 | if (!new_output) { | |
4527 | ret = LTTNG_ERR_NOMEM; | |
4528 | goto error; | |
4529 | } | |
4530 | ||
b178f53e | 4531 | ret = snapshot_output_init(session, output->max_size, output->name, |
6dc3064a DG |
4532 | output->ctrl_url, output->data_url, session->consumer, new_output, |
4533 | &session->snapshot); | |
4534 | if (ret < 0) { | |
4535 | if (ret == -ENOMEM) { | |
4536 | ret = LTTNG_ERR_NOMEM; | |
4537 | } else { | |
4538 | ret = LTTNG_ERR_INVALID; | |
4539 | } | |
4540 | goto free_error; | |
4541 | } | |
4542 | ||
6dc3064a DG |
4543 | rcu_read_lock(); |
4544 | snapshot_add_output(&session->snapshot, new_output); | |
4545 | if (id) { | |
4546 | *id = new_output->id; | |
4547 | } | |
4548 | rcu_read_unlock(); | |
4549 | ||
4550 | return LTTNG_OK; | |
4551 | ||
4552 | free_error: | |
4553 | snapshot_output_destroy(new_output); | |
4554 | error: | |
4555 | return ret; | |
4556 | } | |
4557 | ||
4558 | /* | |
4559 | * Command LTTNG_SNAPSHOT_DEL_OUTPUT from lib lttng ctl. | |
4560 | * | |
4561 | * Return LTTNG_OK on success or else a LTTNG_ERR code. | |
4562 | */ | |
4563 | int cmd_snapshot_del_output(struct ltt_session *session, | |
df4f5a87 | 4564 | const struct lttng_snapshot_output *output) |
6dc3064a DG |
4565 | { |
4566 | int ret; | |
eb240553 | 4567 | struct snapshot_output *sout = NULL; |
6dc3064a DG |
4568 | |
4569 | assert(session); | |
4570 | assert(output); | |
4571 | ||
6dc3064a DG |
4572 | rcu_read_lock(); |
4573 | ||
4574 | /* | |
d3f14b8a MD |
4575 | * Permission denied to create an output if the session is not |
4576 | * set in no output mode. | |
6dc3064a DG |
4577 | */ |
4578 | if (session->output_traces) { | |
903ef685 | 4579 | ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
6dc3064a DG |
4580 | goto error; |
4581 | } | |
4582 | ||
eb240553 DG |
4583 | if (output->id) { |
4584 | DBG("Cmd snapshot del output id %" PRIu32 " for session %s", output->id, | |
4585 | session->name); | |
4586 | sout = snapshot_find_output_by_id(output->id, &session->snapshot); | |
4587 | } else if (*output->name != '\0') { | |
4588 | DBG("Cmd snapshot del output name %s for session %s", output->name, | |
4589 | session->name); | |
4590 | sout = snapshot_find_output_by_name(output->name, &session->snapshot); | |
4591 | } | |
6dc3064a DG |
4592 | if (!sout) { |
4593 | ret = LTTNG_ERR_INVALID; | |
4594 | goto error; | |
4595 | } | |
4596 | ||
4597 | snapshot_delete_output(&session->snapshot, sout); | |
4598 | snapshot_output_destroy(sout); | |
4599 | ret = LTTNG_OK; | |
4600 | ||
4601 | error: | |
4602 | rcu_read_unlock(); | |
4603 | return ret; | |
4604 | } | |
4605 | ||
4606 | /* | |
4607 | * Command LTTNG_SNAPSHOT_LIST_OUTPUT from lib lttng ctl. | |
4608 | * | |
4609 | * If no output is available, outputs is untouched and 0 is returned. | |
4610 | * | |
4611 | * Return the size of the newly allocated outputs or a negative LTTNG_ERR code. | |
4612 | */ | |
4613 | ssize_t cmd_snapshot_list_outputs(struct ltt_session *session, | |
4614 | struct lttng_snapshot_output **outputs) | |
4615 | { | |
4616 | int ret, idx = 0; | |
b223ca94 | 4617 | struct lttng_snapshot_output *list = NULL; |
6dc3064a DG |
4618 | struct lttng_ht_iter iter; |
4619 | struct snapshot_output *output; | |
4620 | ||
4621 | assert(session); | |
4622 | assert(outputs); | |
4623 | ||
4624 | DBG("Cmd snapshot list outputs for session %s", session->name); | |
4625 | ||
4626 | /* | |
d3f14b8a MD |
4627 | * Permission denied to create an output if the session is not |
4628 | * set in no output mode. | |
6dc3064a DG |
4629 | */ |
4630 | if (session->output_traces) { | |
903ef685 JG |
4631 | ret = -LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
4632 | goto end; | |
6dc3064a DG |
4633 | } |
4634 | ||
4635 | if (session->snapshot.nb_output == 0) { | |
4636 | ret = 0; | |
903ef685 | 4637 | goto end; |
6dc3064a DG |
4638 | } |
4639 | ||
4640 | list = zmalloc(session->snapshot.nb_output * sizeof(*list)); | |
4641 | if (!list) { | |
b223ca94 | 4642 | ret = -LTTNG_ERR_NOMEM; |
903ef685 | 4643 | goto end; |
6dc3064a DG |
4644 | } |
4645 | ||
4646 | /* Copy list from session to the new list object. */ | |
b223ca94 | 4647 | rcu_read_lock(); |
6dc3064a DG |
4648 | cds_lfht_for_each_entry(session->snapshot.output_ht->ht, &iter.iter, |
4649 | output, node.node) { | |
4650 | assert(output->consumer); | |
4651 | list[idx].id = output->id; | |
4652 | list[idx].max_size = output->max_size; | |
6ce22875 MD |
4653 | if (lttng_strncpy(list[idx].name, output->name, |
4654 | sizeof(list[idx].name))) { | |
4655 | ret = -LTTNG_ERR_INVALID; | |
903ef685 | 4656 | goto error; |
6ce22875 | 4657 | } |
6dc3064a | 4658 | if (output->consumer->type == CONSUMER_DST_LOCAL) { |
6ce22875 | 4659 | if (lttng_strncpy(list[idx].ctrl_url, |
366a9222 | 4660 | output->consumer->dst.session_root_path, |
6ce22875 MD |
4661 | sizeof(list[idx].ctrl_url))) { |
4662 | ret = -LTTNG_ERR_INVALID; | |
903ef685 | 4663 | goto error; |
6ce22875 | 4664 | } |
6dc3064a DG |
4665 | } else { |
4666 | /* Control URI. */ | |
4667 | ret = uri_to_str_url(&output->consumer->dst.net.control, | |
4668 | list[idx].ctrl_url, sizeof(list[idx].ctrl_url)); | |
4669 | if (ret < 0) { | |
b223ca94 | 4670 | ret = -LTTNG_ERR_NOMEM; |
903ef685 | 4671 | goto error; |
6dc3064a DG |
4672 | } |
4673 | ||
4674 | /* Data URI. */ | |
4675 | ret = uri_to_str_url(&output->consumer->dst.net.data, | |
4676 | list[idx].data_url, sizeof(list[idx].data_url)); | |
4677 | if (ret < 0) { | |
b223ca94 | 4678 | ret = -LTTNG_ERR_NOMEM; |
903ef685 | 4679 | goto error; |
6dc3064a DG |
4680 | } |
4681 | } | |
4682 | idx++; | |
4683 | } | |
4684 | ||
4685 | *outputs = list; | |
b223ca94 JG |
4686 | list = NULL; |
4687 | ret = session->snapshot.nb_output; | |
6dc3064a | 4688 | error: |
903ef685 | 4689 | rcu_read_unlock(); |
b223ca94 | 4690 | free(list); |
903ef685 | 4691 | end: |
b223ca94 | 4692 | return ret; |
6dc3064a DG |
4693 | } |
4694 | ||
93ec662e JD |
4695 | /* |
4696 | * Check if we can regenerate the metadata for this session. | |
4697 | * Only kernel, UST per-uid and non-live sessions are supported. | |
4698 | * | |
4699 | * Return 0 if the metadata can be generated, a LTTNG_ERR code otherwise. | |
4700 | */ | |
4701 | static | |
eded6438 | 4702 | int check_regenerate_metadata_support(struct ltt_session *session) |
93ec662e JD |
4703 | { |
4704 | int ret; | |
4705 | ||
4706 | assert(session); | |
4707 | ||
4708 | if (session->live_timer != 0) { | |
4709 | ret = LTTNG_ERR_LIVE_SESSION; | |
4710 | goto end; | |
4711 | } | |
4712 | if (!session->active) { | |
4713 | ret = LTTNG_ERR_SESSION_NOT_STARTED; | |
4714 | goto end; | |
4715 | } | |
4716 | if (session->ust_session) { | |
4717 | switch (session->ust_session->buffer_type) { | |
4718 | case LTTNG_BUFFER_PER_UID: | |
4719 | break; | |
4720 | case LTTNG_BUFFER_PER_PID: | |
4721 | ret = LTTNG_ERR_PER_PID_SESSION; | |
4722 | goto end; | |
4723 | default: | |
4724 | assert(0); | |
4725 | ret = LTTNG_ERR_UNK; | |
4726 | goto end; | |
4727 | } | |
4728 | } | |
4729 | if (session->consumer->type == CONSUMER_DST_NET && | |
4730 | session->consumer->relay_minor_version < 8) { | |
4731 | ret = LTTNG_ERR_RELAYD_VERSION_FAIL; | |
4732 | goto end; | |
4733 | } | |
4734 | ret = 0; | |
4735 | ||
4736 | end: | |
4737 | return ret; | |
4738 | } | |
4739 | ||
7802bae3 LL |
4740 | static |
4741 | int clear_metadata_file(int fd) | |
4742 | { | |
4743 | int ret; | |
a5df8828 | 4744 | off_t lseek_ret; |
7802bae3 | 4745 | |
a5df8828 GL |
4746 | lseek_ret = lseek(fd, 0, SEEK_SET); |
4747 | if (lseek_ret < 0) { | |
7802bae3 | 4748 | PERROR("lseek"); |
a5df8828 | 4749 | ret = -1; |
7802bae3 LL |
4750 | goto end; |
4751 | } | |
4752 | ||
4753 | ret = ftruncate(fd, 0); | |
4754 | if (ret < 0) { | |
4755 | PERROR("ftruncate"); | |
4756 | goto end; | |
4757 | } | |
4758 | ||
4759 | end: | |
4760 | return ret; | |
4761 | } | |
4762 | ||
93ec662e | 4763 | static |
eded6438 | 4764 | int ust_regenerate_metadata(struct ltt_ust_session *usess) |
93ec662e JD |
4765 | { |
4766 | int ret = 0; | |
4767 | struct buffer_reg_uid *uid_reg = NULL; | |
4768 | struct buffer_reg_session *session_reg = NULL; | |
4769 | ||
4770 | rcu_read_lock(); | |
4771 | cds_list_for_each_entry(uid_reg, &usess->buffer_reg_uid_list, lnode) { | |
4772 | struct ust_registry_session *registry; | |
4773 | struct ust_registry_channel *chan; | |
4774 | struct lttng_ht_iter iter_chan; | |
4775 | ||
4776 | session_reg = uid_reg->registry; | |
4777 | registry = session_reg->reg.ust; | |
4778 | ||
4779 | pthread_mutex_lock(®istry->lock); | |
4780 | registry->metadata_len_sent = 0; | |
4781 | memset(registry->metadata, 0, registry->metadata_alloc_len); | |
4782 | registry->metadata_len = 0; | |
4783 | registry->metadata_version++; | |
7802bae3 LL |
4784 | if (registry->metadata_fd > 0) { |
4785 | /* Clear the metadata file's content. */ | |
4786 | ret = clear_metadata_file(registry->metadata_fd); | |
4787 | if (ret) { | |
4788 | pthread_mutex_unlock(®istry->lock); | |
4789 | goto end; | |
4790 | } | |
4791 | } | |
4792 | ||
93ec662e JD |
4793 | ret = ust_metadata_session_statedump(registry, NULL, |
4794 | registry->major, registry->minor); | |
4795 | if (ret) { | |
4796 | pthread_mutex_unlock(®istry->lock); | |
4797 | ERR("Failed to generate session metadata (err = %d)", | |
4798 | ret); | |
4799 | goto end; | |
4800 | } | |
4801 | cds_lfht_for_each_entry(registry->channels->ht, &iter_chan.iter, | |
4802 | chan, node.node) { | |
4803 | struct ust_registry_event *event; | |
4804 | struct lttng_ht_iter iter_event; | |
4805 | ||
4806 | ret = ust_metadata_channel_statedump(registry, chan); | |
4807 | if (ret) { | |
4808 | pthread_mutex_unlock(®istry->lock); | |
4809 | ERR("Failed to generate channel metadata " | |
4810 | "(err = %d)", ret); | |
4811 | goto end; | |
4812 | } | |
4813 | cds_lfht_for_each_entry(chan->ht->ht, &iter_event.iter, | |
4814 | event, node.node) { | |
4815 | ret = ust_metadata_event_statedump(registry, | |
4816 | chan, event); | |
4817 | if (ret) { | |
4818 | pthread_mutex_unlock(®istry->lock); | |
4819 | ERR("Failed to generate event metadata " | |
4820 | "(err = %d)", ret); | |
4821 | goto end; | |
4822 | } | |
4823 | } | |
4824 | } | |
4825 | pthread_mutex_unlock(®istry->lock); | |
4826 | } | |
4827 | ||
4828 | end: | |
4829 | rcu_read_unlock(); | |
4830 | return ret; | |
4831 | } | |
4832 | ||
4833 | /* | |
eded6438 | 4834 | * Command LTTNG_REGENERATE_METADATA from the lttng-ctl library. |
93ec662e JD |
4835 | * |
4836 | * Ask the consumer to truncate the existing metadata file(s) and | |
4837 | * then regenerate the metadata. Live and per-pid sessions are not | |
4838 | * supported and return an error. | |
4839 | * | |
1136f41b | 4840 | * Return LTTNG_OK on success or else a LTTNG_ERR code. |
93ec662e | 4841 | */ |
eded6438 | 4842 | int cmd_regenerate_metadata(struct ltt_session *session) |
93ec662e JD |
4843 | { |
4844 | int ret; | |
4845 | ||
4846 | assert(session); | |
4847 | ||
eded6438 | 4848 | ret = check_regenerate_metadata_support(session); |
93ec662e JD |
4849 | if (ret) { |
4850 | goto end; | |
4851 | } | |
4852 | ||
4853 | if (session->kernel_session) { | |
eded6438 | 4854 | ret = kernctl_session_regenerate_metadata( |
93ec662e JD |
4855 | session->kernel_session->fd); |
4856 | if (ret < 0) { | |
4857 | ERR("Failed to regenerate the kernel metadata"); | |
4858 | goto end; | |
4859 | } | |
4860 | } | |
4861 | ||
4862 | if (session->ust_session) { | |
eded6438 | 4863 | ret = ust_regenerate_metadata(session->ust_session); |
93ec662e JD |
4864 | if (ret < 0) { |
4865 | ERR("Failed to regenerate the UST metadata"); | |
4866 | goto end; | |
4867 | } | |
4868 | } | |
4869 | DBG("Cmd metadata regenerate for session %s", session->name); | |
4870 | ret = LTTNG_OK; | |
4871 | ||
4872 | end: | |
4873 | return ret; | |
4874 | } | |
4875 | ||
c2561365 JD |
4876 | /* |
4877 | * Command LTTNG_REGENERATE_STATEDUMP from the lttng-ctl library. | |
4878 | * | |
4879 | * Ask the tracer to regenerate a new statedump. | |
4880 | * | |
1136f41b | 4881 | * Return LTTNG_OK on success or else a LTTNG_ERR code. |
c2561365 JD |
4882 | */ |
4883 | int cmd_regenerate_statedump(struct ltt_session *session) | |
4884 | { | |
4885 | int ret; | |
4886 | ||
4887 | assert(session); | |
4888 | ||
4889 | if (!session->active) { | |
4890 | ret = LTTNG_ERR_SESSION_NOT_STARTED; | |
4891 | goto end; | |
4892 | } | |
c2561365 JD |
4893 | |
4894 | if (session->kernel_session) { | |
4895 | ret = kernctl_session_regenerate_statedump( | |
4896 | session->kernel_session->fd); | |
4897 | /* | |
4898 | * Currently, the statedump in kernel can only fail if out | |
4899 | * of memory. | |
4900 | */ | |
4901 | if (ret < 0) { | |
4902 | if (ret == -ENOMEM) { | |
4903 | ret = LTTNG_ERR_REGEN_STATEDUMP_NOMEM; | |
4904 | } else { | |
4905 | ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL; | |
4906 | } | |
4907 | ERR("Failed to regenerate the kernel statedump"); | |
4908 | goto end; | |
4909 | } | |
4910 | } | |
4911 | ||
4912 | if (session->ust_session) { | |
4913 | ret = ust_app_regenerate_statedump_all(session->ust_session); | |
4914 | /* | |
4915 | * Currently, the statedump in UST always returns 0. | |
4916 | */ | |
4917 | if (ret < 0) { | |
4918 | ret = LTTNG_ERR_REGEN_STATEDUMP_FAIL; | |
4919 | ERR("Failed to regenerate the UST statedump"); | |
4920 | goto end; | |
4921 | } | |
4922 | } | |
4923 | DBG("Cmd regenerate statedump for session %s", session->name); | |
4924 | ret = LTTNG_OK; | |
4925 | ||
4926 | end: | |
4927 | return ret; | |
4928 | } | |
4929 | ||
c3e68e71 JR |
4930 | static |
4931 | enum lttng_error_code synchronize_tracer_notifier_register( | |
4932 | struct notification_thread_handle *notification_thread, | |
4933 | struct lttng_trigger *trigger, const struct lttng_credentials *cmd_creds) | |
70670472 | 4934 | { |
c3e68e71 JR |
4935 | enum lttng_error_code ret_code; |
4936 | struct lttng_condition *condition = lttng_trigger_get_condition(trigger); | |
4937 | const char *trigger_name; | |
4938 | uid_t trigger_owner; | |
4939 | enum lttng_trigger_status trigger_status; | |
4940 | const enum lttng_domain_type trigger_domain = | |
4941 | lttng_trigger_get_underlying_domain_type_restriction( | |
4942 | trigger); | |
70670472 | 4943 | |
c3e68e71 JR |
4944 | trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner); |
4945 | assert(trigger_status == LTTNG_TRIGGER_STATUS_OK); | |
4946 | ||
4947 | assert(condition); | |
4948 | assert(lttng_condition_get_type(condition) == LTTNG_CONDITION_TYPE_ON_EVENT); | |
4949 | ||
4950 | trigger_status = lttng_trigger_get_name(trigger, &trigger_name); | |
4951 | trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(unnamed)"; | |
4952 | ||
4953 | session_lock_list(); | |
4954 | switch (trigger_domain) { | |
4955 | case LTTNG_DOMAIN_KERNEL: | |
4956 | { | |
4957 | ret_code = kernel_register_event_notifier(trigger, cmd_creds); | |
4958 | if (ret_code != LTTNG_OK) { | |
4959 | enum lttng_error_code notif_thread_unregister_ret; | |
4960 | ||
4961 | notif_thread_unregister_ret = | |
4962 | notification_thread_command_unregister_trigger( | |
4963 | notification_thread, trigger); | |
4964 | ||
4965 | if (notif_thread_unregister_ret != LTTNG_OK) { | |
4966 | /* Return the original error code. */ | |
4967 | ERR("Failed to unregister trigger from notification thread during error recovery: trigger name = '%s', trigger owner uid = %d, error code = %d", | |
4968 | trigger_name, | |
4969 | (int) trigger_owner, | |
4970 | ret_code); | |
4971 | } | |
4972 | } | |
4973 | break; | |
70670472 | 4974 | } |
c3e68e71 JR |
4975 | case LTTNG_DOMAIN_UST: |
4976 | ust_app_global_update_all_event_notifier_rules(); | |
4977 | break; | |
4978 | case LTTNG_DOMAIN_NONE: | |
4979 | abort(); | |
4980 | default: | |
4981 | { | |
4982 | /* Agent domains. */ | |
4983 | struct agent *agt = agent_find_by_event_notifier_domain( | |
4984 | trigger_domain); | |
4985 | if (!agt) { | |
4986 | agt = agent_create(trigger_domain); | |
4987 | if (!agt) { | |
4988 | ret_code = LTTNG_ERR_NOMEM; | |
4989 | goto end_unlock_session_list; | |
4990 | } | |
4991 | agent_add(agt, trigger_agents_ht_by_domain); | |
4992 | } | |
70670472 | 4993 | |
c3e68e71 JR |
4994 | ret_code = trigger_agent_enable(trigger, agt); |
4995 | if (ret_code != LTTNG_OK) { | |
4996 | goto end_unlock_session_list; | |
4997 | } | |
4998 | ||
4999 | break; | |
5000 | } | |
5001 | } | |
5002 | ||
5003 | ret_code = LTTNG_OK; | |
5004 | end_unlock_session_list: | |
5005 | session_unlock_list(); | |
70670472 JR |
5006 | return ret_code; |
5007 | } | |
5008 | ||
5009 | enum lttng_error_code cmd_register_trigger(const struct lttng_credentials *cmd_creds, | |
746e08d7 | 5010 | struct lttng_trigger *trigger, |
242388e4 JR |
5011 | struct notification_thread_handle *notification_thread, |
5012 | struct lttng_trigger **return_trigger) | |
b0880ae5 | 5013 | { |
70670472 | 5014 | enum lttng_error_code ret_code; |
70670472 | 5015 | const char *trigger_name; |
c3e68e71 | 5016 | const struct lttng_condition *condition; |
70670472 JR |
5017 | uid_t trigger_owner; |
5018 | enum lttng_trigger_status trigger_status; | |
5019 | ||
5020 | trigger_status = lttng_trigger_get_name(trigger, &trigger_name); | |
ce0b1d61 JG |
5021 | trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? |
5022 | trigger_name : "(unnamed)"; | |
5023 | ||
70670472 JR |
5024 | trigger_status = lttng_trigger_get_owner_uid( |
5025 | trigger, &trigger_owner); | |
5026 | assert(trigger_status == LTTNG_TRIGGER_STATUS_OK); | |
5027 | ||
5028 | DBG("Running register trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d", | |
5029 | trigger_name, (int) trigger_owner, | |
5030 | (int) lttng_credentials_get_uid(cmd_creds)); | |
b0880ae5 | 5031 | |
c3e68e71 | 5032 | /* |
64eafdf6 JR |
5033 | * Validate the trigger credentials against the command credentials. |
5034 | * Only the root user can register a trigger with non-matching | |
5035 | * credentials. | |
5036 | */ | |
5037 | if (!lttng_credentials_is_equal_uid( | |
5038 | lttng_trigger_get_credentials(trigger), | |
746e08d7 JG |
5039 | cmd_creds)) { |
5040 | if (lttng_credentials_get_uid(cmd_creds) != 0) { | |
70670472 JR |
5041 | ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d", |
5042 | trigger_name, (int) trigger_owner, | |
5043 | (int) lttng_credentials_get_uid(cmd_creds)); | |
5044 | ret_code = LTTNG_ERR_INVALID_TRIGGER; | |
64eafdf6 JR |
5045 | goto end; |
5046 | } | |
5047 | } | |
3da864a9 | 5048 | |
58daac01 JR |
5049 | /* |
5050 | * The bytecode generation also serves as a validation step for the | |
5051 | * bytecode expressions. | |
5052 | */ | |
70670472 JR |
5053 | ret_code = lttng_trigger_generate_bytecode(trigger, cmd_creds); |
5054 | if (ret_code != LTTNG_OK) { | |
5055 | ERR("Failed to generate bytecode of trigger: trigger name = '%s', trigger owner uid = %d, error code = %d", | |
5056 | trigger_name, (int) trigger_owner, ret_code); | |
58daac01 JR |
5057 | goto end; |
5058 | } | |
5059 | ||
242388e4 JR |
5060 | /* |
5061 | * A reference to the trigger is acquired by the notification thread. | |
5062 | * It is safe to return the same trigger to the caller since it the | |
5063 | * other user holds a reference. | |
5064 | * | |
5065 | * The trigger is modified during the execution of the | |
5066 | * "register trigger" command. However, by the time the command returns, | |
5067 | * it is safe to use without any locking as its properties are | |
5068 | * immutable. | |
5069 | */ | |
70670472 | 5070 | ret_code = notification_thread_command_register_trigger(notification_thread, |
b0880ae5 | 5071 | trigger); |
70670472 | 5072 | if (ret_code != LTTNG_OK) { |
ce0b1d61 | 5073 | DBG("Failed to register trigger to notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d", |
70670472 | 5074 | trigger_name, (int) trigger_owner, ret_code); |
44760c20 | 5075 | goto end; |
70670472 JR |
5076 | } |
5077 | ||
ce0b1d61 JG |
5078 | trigger_status = lttng_trigger_get_name(trigger, &trigger_name); |
5079 | trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? | |
5080 | trigger_name : "(unnamed)"; | |
5081 | ||
70670472 JR |
5082 | /* |
5083 | * Synchronize tracers if the trigger adds an event notifier. | |
5084 | */ | |
c3e68e71 JR |
5085 | if (lttng_trigger_needs_tracer_notifier(trigger)) { |
5086 | ret_code = synchronize_tracer_notifier_register(notification_thread, | |
5087 | trigger, cmd_creds); | |
5088 | if (ret_code != LTTNG_OK) { | |
5089 | ERR("Error registering tracer notifier"); | |
5090 | goto end; | |
44760c20 | 5091 | } |
c3e68e71 | 5092 | } |
44760c20 | 5093 | |
c3e68e71 | 5094 | condition = lttng_trigger_get_const_condition(trigger); |
9b7cbebd | 5095 | |
c3e68e71 JR |
5096 | /* TODO: Extract condition below to lttng_trigger internal function */ |
5097 | if (lttng_condition_get_type(condition) == LTTNG_CONDITION_TYPE_ON_EVENT) { | |
5098 | session_lock_list(); | |
5099 | ret_code = sync_all_tracer_executed_actions(trigger, | |
5100 | cmd_creds, TRACER_EXECUTED_ACTION_STATE_REGISTER); | |
9b7cbebd | 5101 | session_unlock_list(); |
c3e68e71 JR |
5102 | if (ret_code != LTTNG_OK) { |
5103 | ERR("Error registering tracer executed actions"); | |
5104 | goto end; | |
5105 | } | |
70670472 JR |
5106 | } |
5107 | ||
746e08d7 JG |
5108 | /* |
5109 | * Return an updated trigger to the client. | |
5110 | * | |
5111 | * Since a modified version of the same trigger is returned, acquire a | |
5112 | * reference to the trigger so the caller doesn't have to care if those | |
5113 | * are distinct instances or not. | |
5114 | */ | |
39b95a70 JG |
5115 | if (ret_code == LTTNG_OK) { |
5116 | lttng_trigger_get(trigger); | |
5117 | *return_trigger = trigger; | |
5118 | /* Ownership of trigger was transferred to caller. */ | |
5119 | trigger = NULL; | |
5120 | } | |
b0880ae5 | 5121 | end: |
70670472 | 5122 | return ret_code; |
c3e68e71 JR |
5123 | } |
5124 | ||
5125 | static | |
5126 | enum lttng_error_code synchronize_tracer_notifier_unregister( | |
5127 | const struct lttng_trigger *trigger) | |
5128 | { | |
5129 | enum lttng_error_code ret_code; | |
5130 | const struct lttng_condition *condition = | |
5131 | lttng_trigger_get_const_condition(trigger); | |
5132 | const enum lttng_domain_type trigger_domain = | |
5133 | lttng_trigger_get_underlying_domain_type_restriction( | |
5134 | trigger); | |
5135 | ||
5136 | assert(condition); | |
5137 | assert(lttng_condition_get_type(condition) == LTTNG_CONDITION_TYPE_ON_EVENT); | |
5138 | ||
5139 | session_lock_list(); | |
5140 | switch (trigger_domain) { | |
5141 | case LTTNG_DOMAIN_KERNEL: | |
5142 | ret_code = kernel_unregister_event_notifier(trigger); | |
5143 | break; | |
5144 | case LTTNG_DOMAIN_UST: | |
5145 | ust_app_global_update_all_event_notifier_rules(); | |
5146 | break; | |
5147 | case LTTNG_DOMAIN_NONE: | |
5148 | abort(); | |
5149 | default: | |
5150 | { | |
5151 | /* Agent domains. */ | |
5152 | struct agent *agt = agent_find_by_event_notifier_domain( | |
5153 | trigger_domain); | |
5154 | if (!agt) { | |
5155 | agt = agent_create(trigger_domain); | |
5156 | if (!agt) { | |
5157 | ret_code = LTTNG_ERR_NOMEM; | |
5158 | goto end_unlock_session_list; | |
5159 | } | |
5160 | agent_add(agt, trigger_agents_ht_by_domain); | |
5161 | } | |
5162 | ||
5163 | ret_code = trigger_agent_disable(trigger, agt); | |
5164 | if (ret_code != LTTNG_OK) { | |
5165 | goto end_unlock_session_list; | |
5166 | } | |
5167 | ||
5168 | break; | |
5169 | } | |
5170 | } | |
5171 | ||
5172 | ret_code = LTTNG_OK; | |
5173 | ||
9b7cbebd JG |
5174 | end_unlock_session_list: |
5175 | session_unlock_list(); | |
5176 | return ret_code; | |
b0880ae5 JG |
5177 | } |
5178 | ||
70670472 | 5179 | enum lttng_error_code cmd_unregister_trigger(const struct lttng_credentials *cmd_creds, |
746e08d7 | 5180 | const struct lttng_trigger *trigger, |
b0880ae5 JG |
5181 | struct notification_thread_handle *notification_thread) |
5182 | { | |
70670472 | 5183 | enum lttng_error_code ret_code; |
70670472 | 5184 | const char *trigger_name; |
c3e68e71 | 5185 | struct lttng_trigger *real_trigger; |
70670472 JR |
5186 | uid_t trigger_owner; |
5187 | enum lttng_trigger_status trigger_status; | |
5188 | ||
5189 | trigger_status = lttng_trigger_get_name(trigger, &trigger_name); | |
ce0b1d61 | 5190 | trigger_name = trigger_status == LTTNG_TRIGGER_STATUS_OK ? trigger_name : "(unnamed)"; |
c3e68e71 | 5191 | trigger_status = lttng_trigger_get_owner_uid(trigger, &trigger_owner); |
70670472 JR |
5192 | assert(trigger_status == LTTNG_TRIGGER_STATUS_OK); |
5193 | ||
5194 | DBG("Running unregister trigger command: trigger name = '%s', trigger owner uid = %d, command creds uid = %d", | |
5195 | trigger_name, (int) trigger_owner, | |
5196 | (int) lttng_credentials_get_uid(cmd_creds)); | |
b0880ae5 | 5197 | |
64eafdf6 JR |
5198 | /* |
5199 | * Validate the trigger credentials against the command credentials. | |
5200 | * Only the root user can unregister a trigger with non-matching | |
5201 | * credentials. | |
5202 | */ | |
5203 | if (!lttng_credentials_is_equal_uid( | |
5204 | lttng_trigger_get_credentials(trigger), | |
746e08d7 JG |
5205 | cmd_creds)) { |
5206 | if (lttng_credentials_get_uid(cmd_creds) != 0) { | |
70670472 JR |
5207 | ERR("Trigger credentials do not match the command credentials: trigger name = '%s', trigger owner uid = %d, command creds uid = %d", |
5208 | trigger_name, (int) trigger_owner, | |
5209 | (int) lttng_credentials_get_uid(cmd_creds)); | |
5210 | ret_code = LTTNG_ERR_INVALID_TRIGGER; | |
64eafdf6 JR |
5211 | goto end; |
5212 | } | |
5213 | } | |
3da864a9 | 5214 | |
c3e68e71 JR |
5215 | ret_code = notification_thread_command_get_trigger( |
5216 | notification_thread, trigger, &real_trigger); | |
70670472 | 5217 | if (ret_code != LTTNG_OK) { |
c3e68e71 | 5218 | DBG("Failed to get the real trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d", |
70670472 JR |
5219 | trigger_name, (int) trigger_owner, ret_code); |
5220 | goto end; | |
5221 | } | |
5222 | ||
c3e68e71 JR |
5223 | ret_code = notification_thread_command_unregister_trigger( |
5224 | notification_thread, real_trigger); | |
70670472 | 5225 | if (ret_code != LTTNG_OK) { |
ce0b1d61 | 5226 | DBG("Failed to unregister trigger from notification thread: trigger name = '%s', trigger owner uid = %d, error code = %d", |
70670472 | 5227 | trigger_name, (int) trigger_owner, ret_code); |
c3e68e71 | 5228 | goto end; |
70670472 JR |
5229 | } |
5230 | ||
5231 | /* | |
5232 | * Synchronize tracers if the trigger removes an event notifier. | |
44760c20 JR |
5233 | * Do this even if the trigger unregistration failed to at least stop |
5234 | * the tracers from producing notifications associated with this | |
5235 | * event notifier. | |
70670472 | 5236 | */ |
c3e68e71 JR |
5237 | if (lttng_trigger_needs_tracer_notifier(real_trigger)) { |
5238 | ret_code = synchronize_tracer_notifier_unregister(real_trigger); | |
5239 | if (ret_code != LTTNG_OK) { | |
5240 | ERR("Error unregistering trigger to tracer."); | |
5241 | goto end; | |
44760c20 | 5242 | } |
44760c20 | 5243 | |
c3e68e71 | 5244 | } |
9b7cbebd | 5245 | |
c3e68e71 JR |
5246 | session_lock_list(); |
5247 | ret_code = sync_all_tracer_executed_actions(real_trigger, | |
5248 | cmd_creds, TRACER_EXECUTED_ACTION_STATE_UNREGISTER); | |
5249 | session_unlock_list(); | |
5250 | if (ret_code != LTTNG_OK) { | |
5251 | ERR("Error registering tracer executed actions"); | |
5252 | goto end; | |
70670472 JR |
5253 | } |
5254 | ||
c3e68e71 | 5255 | lttng_trigger_put(real_trigger); |
b0880ae5 | 5256 | end: |
70670472 | 5257 | return ret_code; |
c3e68e71 | 5258 | } |
b0880ae5 | 5259 | |
fbc9f37d JR |
5260 | int cmd_list_triggers(struct command_ctx *cmd_ctx, |
5261 | struct notification_thread_handle *notification_thread, | |
5262 | struct lttng_triggers **return_triggers) | |
5263 | { | |
5264 | int ret = 0; | |
5265 | enum lttng_error_code ret_code; | |
5266 | struct lttng_triggers *triggers = NULL; | |
5267 | ||
5268 | /* Get the set of triggers from the notification thread. */ | |
5269 | ret_code = notification_thread_command_list_triggers( | |
5270 | notification_thread, cmd_ctx->creds.uid, &triggers); | |
5271 | if (ret_code != LTTNG_OK) { | |
5272 | ret = ret_code; | |
5273 | goto end; | |
5274 | } | |
5275 | ||
5276 | *return_triggers = triggers; | |
5277 | triggers = NULL; | |
5278 | ret = LTTNG_OK; | |
5279 | end: | |
5280 | lttng_triggers_destroy(triggers); | |
5281 | return ret; | |
5282 | } | |
c3e68e71 JR |
5283 | |
5284 | int cmd_list_map_values(const char *session_name, | |
5285 | const struct lttng_map *map, | |
5286 | const struct lttng_map_query *map_query, | |
5287 | struct lttng_map_content **return_map_content) | |
5288 | { | |
5289 | enum lttng_error_code ret; | |
5290 | struct ltt_session *session; | |
5291 | enum lttng_domain_type domain; | |
5292 | const char *map_name; | |
5293 | enum lttng_map_status map_status; | |
5294 | ||
5295 | /* Returns a refcounted reference */ | |
5296 | session = session_find_by_name(session_name); | |
5297 | if (!session) { | |
5298 | DBG("Session '%s' not found", session_name); | |
5299 | ret = LTTNG_ERR_SESS_NOT_FOUND; | |
5300 | goto end; | |
5301 | } | |
5302 | ||
5303 | domain = lttng_map_get_domain(map); | |
5304 | ||
5305 | map_status = lttng_map_get_name(map, &map_name); | |
5306 | assert(map_status == LTTNG_MAP_STATUS_OK); | |
5307 | ||
5308 | if (domain == LTTNG_DOMAIN_KERNEL) { | |
5309 | if (session->kernel_session) { | |
5310 | struct ltt_kernel_map *kmap; | |
5311 | ||
5312 | kmap = trace_kernel_get_map_by_name(map_name, | |
5313 | session->kernel_session); | |
5314 | if (kmap) { | |
5315 | ret = kernel_list_map_values(kmap, map_query, | |
5316 | return_map_content); | |
5317 | if (ret != LTTNG_OK) { | |
5318 | ERR("Error listing kernel map '%s' values", map_name); | |
5319 | goto end; | |
5320 | } | |
5321 | } else { | |
5322 | DBG("No kernel map '%s' in session '%s'", map_name, session_name); | |
5323 | } | |
5324 | } | |
5325 | } else if (domain == LTTNG_DOMAIN_UST) { | |
5326 | if (session->ust_session) { | |
5327 | struct ltt_ust_map *umap; | |
5328 | struct ltt_ust_session *usess = session->ust_session; | |
5329 | ||
5330 | umap = trace_ust_find_map_by_name( | |
5331 | usess->domain_global.maps, map_name); | |
5332 | if (umap) { | |
5333 | ret = ust_app_map_list_values(usess, umap, | |
5334 | map_query, return_map_content); | |
5335 | if (ret) { | |
5336 | ret = LTTNG_ERR_MAP_VALUES_LIST_FAIL; | |
5337 | ERR("Error listing UST map '%s' values", map_name); | |
5338 | goto end; | |
5339 | } | |
5340 | } else { | |
5341 | DBG("No UST map '%s' in session '%s'", map_name, session_name); | |
5342 | } | |
5343 | } | |
5344 | } | |
5345 | ||
5346 | ||
5347 | ret = LTTNG_OK; | |
5348 | end: | |
5349 | session_put(session); | |
5350 | return ret; | |
5351 | } | |
5352 | ||
6dc3064a DG |
5353 | /* |
5354 | * Send relayd sockets from snapshot output to consumer. Ignore request if the | |
5355 | * snapshot output is *not* set with a remote destination. | |
5356 | * | |
9a654598 | 5357 | * Return LTTNG_OK on success or a LTTNG_ERR code. |
6dc3064a | 5358 | */ |
9a654598 | 5359 | static enum lttng_error_code set_relayd_for_snapshot( |
348a81dc | 5360 | struct consumer_output *output, |
fb9a95c4 | 5361 | const struct ltt_session *session) |
6dc3064a | 5362 | { |
9a654598 | 5363 | enum lttng_error_code status = LTTNG_OK; |
6dc3064a DG |
5364 | struct lttng_ht_iter iter; |
5365 | struct consumer_socket *socket; | |
1e791a74 | 5366 | LTTNG_OPTIONAL(uint64_t) current_chunk_id = {}; |
6fa5fe7c | 5367 | const char *base_path; |
6dc3064a | 5368 | |
348a81dc | 5369 | assert(output); |
6dc3064a DG |
5370 | assert(session); |
5371 | ||
5372 | DBG2("Set relayd object from snapshot output"); | |
5373 | ||
1e791a74 | 5374 | if (session->current_trace_chunk) { |
348a81dc JG |
5375 | enum lttng_trace_chunk_status chunk_status = |
5376 | lttng_trace_chunk_get_id( | |
5377 | session->current_trace_chunk, | |
5378 | ¤t_chunk_id.value); | |
1e791a74 | 5379 | |
348a81dc | 5380 | if (chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK) { |
1e791a74 JG |
5381 | current_chunk_id.is_set = true; |
5382 | } else { | |
5383 | ERR("Failed to get current trace chunk id"); | |
5384 | status = LTTNG_ERR_UNK; | |
5385 | goto error; | |
5386 | } | |
5387 | } | |
5388 | ||
6dc3064a | 5389 | /* Ignore if snapshot consumer output is not network. */ |
348a81dc | 5390 | if (output->type != CONSUMER_DST_NET) { |
6dc3064a DG |
5391 | goto error; |
5392 | } | |
5393 | ||
6fa5fe7c MD |
5394 | /* |
5395 | * The snapshot record URI base path overrides the session | |
5396 | * base path. | |
5397 | */ | |
5398 | if (output->dst.net.control.subdir[0] != '\0') { | |
5399 | base_path = output->dst.net.control.subdir; | |
5400 | } else { | |
5401 | base_path = session->base_path; | |
5402 | } | |
5403 | ||
6dc3064a DG |
5404 | /* |
5405 | * For each consumer socket, create and send the relayd object of the | |
5406 | * snapshot output. | |
5407 | */ | |
5408 | rcu_read_lock(); | |
348a81dc | 5409 | cds_lfht_for_each_entry(output->socks->ht, &iter.iter, |
5eecee74 | 5410 | socket, node.node) { |
ecd0f96d | 5411 | pthread_mutex_lock(socket->lock); |
9a654598 | 5412 | status = send_consumer_relayd_sockets(0, session->id, |
348a81dc | 5413 | output, socket, |
d3e2ba59 | 5414 | session->name, session->hostname, |
6fa5fe7c | 5415 | base_path, |
1e791a74 | 5416 | session->live_timer, |
db1da059 | 5417 | current_chunk_id.is_set ? ¤t_chunk_id.value : NULL, |
46ef2188 MD |
5418 | session->creation_time, |
5419 | session->name_contains_creation_time); | |
ecd0f96d | 5420 | pthread_mutex_unlock(socket->lock); |
9a654598 | 5421 | if (status != LTTNG_OK) { |
6dc3064a DG |
5422 | rcu_read_unlock(); |
5423 | goto error; | |
5424 | } | |
5425 | } | |
5426 | rcu_read_unlock(); | |
5427 | ||
5428 | error: | |
9a654598 | 5429 | return status; |
6dc3064a DG |
5430 | } |
5431 | ||
5432 | /* | |
5433 | * Record a kernel snapshot. | |
5434 | * | |
fac41e72 | 5435 | * Return LTTNG_OK on success or a LTTNG_ERR code. |
6dc3064a | 5436 | */ |
fb9a95c4 JG |
5437 | static enum lttng_error_code record_kernel_snapshot( |
5438 | struct ltt_kernel_session *ksess, | |
348a81dc | 5439 | const struct consumer_output *output, |
fb9a95c4 | 5440 | const struct ltt_session *session, |
d07ceecd | 5441 | int wait, uint64_t nb_packets_per_stream) |
6dc3064a | 5442 | { |
9a654598 | 5443 | enum lttng_error_code status; |
6dc3064a DG |
5444 | |
5445 | assert(ksess); | |
5446 | assert(output); | |
5447 | assert(session); | |
5448 | ||
348a81dc JG |
5449 | status = kernel_snapshot_record( |
5450 | ksess, output, wait, nb_packets_per_stream); | |
9a654598 | 5451 | return status; |
6dc3064a DG |
5452 | } |
5453 | ||
5454 | /* | |
5455 | * Record a UST snapshot. | |
5456 | * | |
9a654598 | 5457 | * Returns LTTNG_OK on success or a LTTNG_ERR error code. |
6dc3064a | 5458 | */ |
9a654598 | 5459 | static enum lttng_error_code record_ust_snapshot(struct ltt_ust_session *usess, |
348a81dc JG |
5460 | const struct consumer_output *output, |
5461 | const struct ltt_session *session, | |
5462 | int wait, uint64_t nb_packets_per_stream) | |
6dc3064a | 5463 | { |
9a654598 | 5464 | enum lttng_error_code status; |
6dc3064a DG |
5465 | |
5466 | assert(usess); | |
5467 | assert(output); | |
5468 | assert(session); | |
5469 | ||
348a81dc JG |
5470 | status = ust_app_snapshot_record( |
5471 | usess, output, wait, nb_packets_per_stream); | |
9a654598 | 5472 | return status; |
6dc3064a DG |
5473 | } |
5474 | ||
d07ceecd | 5475 | static |
fb9a95c4 JG |
5476 | uint64_t get_session_size_one_more_packet_per_stream( |
5477 | const struct ltt_session *session, uint64_t cur_nr_packets) | |
68808f4e | 5478 | { |
d07ceecd | 5479 | uint64_t tot_size = 0; |
68808f4e DG |
5480 | |
5481 | if (session->kernel_session) { | |
5482 | struct ltt_kernel_channel *chan; | |
fb9a95c4 JG |
5483 | const struct ltt_kernel_session *ksess = |
5484 | session->kernel_session; | |
68808f4e | 5485 | |
68808f4e | 5486 | cds_list_for_each_entry(chan, &ksess->channel_list.head, list) { |
d07ceecd MD |
5487 | if (cur_nr_packets >= chan->channel->attr.num_subbuf) { |
5488 | /* | |
5489 | * Don't take channel into account if we | |
5490 | * already grab all its packets. | |
5491 | */ | |
5492 | continue; | |
68808f4e | 5493 | } |
d07ceecd MD |
5494 | tot_size += chan->channel->attr.subbuf_size |
5495 | * chan->stream_count; | |
68808f4e DG |
5496 | } |
5497 | } | |
5498 | ||
5499 | if (session->ust_session) { | |
fb9a95c4 | 5500 | const struct ltt_ust_session *usess = session->ust_session; |
68808f4e | 5501 | |
d07ceecd MD |
5502 | tot_size += ust_app_get_size_one_more_packet_per_stream(usess, |
5503 | cur_nr_packets); | |
68808f4e DG |
5504 | } |
5505 | ||
d07ceecd | 5506 | return tot_size; |
68808f4e DG |
5507 | } |
5508 | ||
5c786ded | 5509 | /* |
d07ceecd MD |
5510 | * Calculate the number of packets we can grab from each stream that |
5511 | * fits within the overall snapshot max size. | |
5512 | * | |
5513 | * Returns -1 on error, 0 means infinite number of packets, else > 0 is | |
5514 | * the number of packets per stream. | |
5515 | * | |
5516 | * TODO: this approach is not perfect: we consider the worse case | |
5517 | * (packet filling the sub-buffers) as an upper bound, but we could do | |
5518 | * better if we do this calculation while we actually grab the packet | |
5519 | * content: we would know how much padding we don't actually store into | |
5520 | * the file. | |
5521 | * | |
5522 | * This algorithm is currently bounded by the number of packets per | |
5523 | * stream. | |
5524 | * | |
5525 | * Since we call this algorithm before actually grabbing the data, it's | |
5526 | * an approximation: for instance, applications could appear/disappear | |
5527 | * in between this call and actually grabbing data. | |
5c786ded | 5528 | */ |
d07ceecd | 5529 | static |
fb9a95c4 JG |
5530 | int64_t get_session_nb_packets_per_stream(const struct ltt_session *session, |
5531 | uint64_t max_size) | |
5c786ded | 5532 | { |
d07ceecd MD |
5533 | int64_t size_left; |
5534 | uint64_t cur_nb_packets = 0; | |
5c786ded | 5535 | |
d07ceecd MD |
5536 | if (!max_size) { |
5537 | return 0; /* Infinite */ | |
5c786ded JD |
5538 | } |
5539 | ||
d07ceecd MD |
5540 | size_left = max_size; |
5541 | for (;;) { | |
5542 | uint64_t one_more_packet_tot_size; | |
5c786ded | 5543 | |
fb9a95c4 JG |
5544 | one_more_packet_tot_size = get_session_size_one_more_packet_per_stream( |
5545 | session, cur_nb_packets); | |
d07ceecd MD |
5546 | if (!one_more_packet_tot_size) { |
5547 | /* We are already grabbing all packets. */ | |
5548 | break; | |
5549 | } | |
5550 | size_left -= one_more_packet_tot_size; | |
5551 | if (size_left < 0) { | |
5552 | break; | |
5553 | } | |
5554 | cur_nb_packets++; | |
5c786ded | 5555 | } |
aecf2da5 | 5556 | if (!cur_nb_packets && size_left != max_size) { |
d07ceecd MD |
5557 | /* Not enough room to grab one packet of each stream, error. */ |
5558 | return -1; | |
5559 | } | |
5560 | return cur_nb_packets; | |
5c786ded JD |
5561 | } |
5562 | ||
fb9a95c4 | 5563 | static |
d2956687 | 5564 | enum lttng_error_code snapshot_record(struct ltt_session *session, |
fb9a95c4 JG |
5565 | const struct snapshot_output *snapshot_output, int wait) |
5566 | { | |
5567 | int64_t nb_packets_per_stream; | |
d2956687 | 5568 | char snapshot_chunk_name[LTTNG_NAME_MAX]; |
348a81dc JG |
5569 | int ret; |
5570 | enum lttng_error_code ret_code = LTTNG_OK; | |
d2956687 | 5571 | struct lttng_trace_chunk *snapshot_trace_chunk; |
348a81dc JG |
5572 | struct consumer_output *original_ust_consumer_output = NULL; |
5573 | struct consumer_output *original_kernel_consumer_output = NULL; | |
5574 | struct consumer_output *snapshot_ust_consumer_output = NULL; | |
5575 | struct consumer_output *snapshot_kernel_consumer_output = NULL; | |
d2956687 | 5576 | |
348a81dc | 5577 | ret = snprintf(snapshot_chunk_name, sizeof(snapshot_chunk_name), |
d2956687 JG |
5578 | "%s-%s-%" PRIu64, |
5579 | snapshot_output->name, | |
5580 | snapshot_output->datetime, | |
5581 | snapshot_output->nb_snapshot); | |
348a81dc | 5582 | if (ret < 0 || ret >= sizeof(snapshot_chunk_name)) { |
d2956687 | 5583 | ERR("Failed to format snapshot name"); |
348a81dc JG |
5584 | ret_code = LTTNG_ERR_INVALID; |
5585 | goto error; | |
d2956687 JG |
5586 | } |
5587 | DBG("Recording snapshot \"%s\" for session \"%s\" with chunk name \"%s\"", | |
5588 | snapshot_output->name, session->name, | |
5589 | snapshot_chunk_name); | |
348a81dc JG |
5590 | if (!session->kernel_session && !session->ust_session) { |
5591 | ERR("Failed to record snapshot as no channels exist"); | |
5592 | ret_code = LTTNG_ERR_NO_CHANNEL; | |
5593 | goto error; | |
5594 | } | |
5595 | ||
5596 | if (session->kernel_session) { | |
5597 | original_kernel_consumer_output = | |
5598 | session->kernel_session->consumer; | |
5599 | snapshot_kernel_consumer_output = | |
5600 | consumer_copy_output(snapshot_output->consumer); | |
3b967712 MD |
5601 | strcpy(snapshot_kernel_consumer_output->chunk_path, |
5602 | snapshot_chunk_name); | |
348a81dc JG |
5603 | ret = consumer_copy_sockets(snapshot_kernel_consumer_output, |
5604 | original_kernel_consumer_output); | |
5605 | if (ret < 0) { | |
5606 | ERR("Failed to copy consumer sockets from snapshot output configuration"); | |
5607 | ret_code = LTTNG_ERR_NOMEM; | |
5608 | goto error; | |
5609 | } | |
5610 | ret_code = set_relayd_for_snapshot( | |
5611 | snapshot_kernel_consumer_output, session); | |
5612 | if (ret_code != LTTNG_OK) { | |
5613 | ERR("Failed to setup relay daemon for kernel tracer snapshot"); | |
5614 | goto error; | |
5615 | } | |
5616 | session->kernel_session->consumer = | |
5617 | snapshot_kernel_consumer_output; | |
5618 | } | |
5619 | if (session->ust_session) { | |
5620 | original_ust_consumer_output = session->ust_session->consumer; | |
5621 | snapshot_ust_consumer_output = | |
5622 | consumer_copy_output(snapshot_output->consumer); | |
3b967712 MD |
5623 | strcpy(snapshot_ust_consumer_output->chunk_path, |
5624 | snapshot_chunk_name); | |
348a81dc JG |
5625 | ret = consumer_copy_sockets(snapshot_ust_consumer_output, |
5626 | original_ust_consumer_output); | |
5627 | if (ret < 0) { | |
5628 | ERR("Failed to copy consumer sockets from snapshot output configuration"); | |
5629 | ret_code = LTTNG_ERR_NOMEM; | |
5630 | goto error; | |
5631 | } | |
5632 | ret_code = set_relayd_for_snapshot( | |
5633 | snapshot_ust_consumer_output, session); | |
5634 | if (ret_code != LTTNG_OK) { | |
5635 | ERR("Failed to setup relay daemon for userspace tracer snapshot"); | |
5636 | goto error; | |
5637 | } | |
5638 | session->ust_session->consumer = | |
5639 | snapshot_ust_consumer_output; | |
5640 | } | |
5641 | ||
d2956687 | 5642 | snapshot_trace_chunk = session_create_new_trace_chunk(session, |
348a81dc JG |
5643 | snapshot_kernel_consumer_output ?: |
5644 | snapshot_ust_consumer_output, | |
5645 | consumer_output_get_base_path( | |
5646 | snapshot_output->consumer), | |
d2956687 JG |
5647 | snapshot_chunk_name); |
5648 | if (!snapshot_trace_chunk) { | |
348a81dc JG |
5649 | ERR("Failed to create temporary trace chunk to record a snapshot of session \"%s\"", |
5650 | session->name); | |
5651 | ret_code = LTTNG_ERR_CREATE_DIR_FAIL; | |
5652 | goto error; | |
d2956687 JG |
5653 | } |
5654 | assert(!session->current_trace_chunk); | |
5655 | ret = session_set_trace_chunk(session, snapshot_trace_chunk, NULL); | |
5656 | lttng_trace_chunk_put(snapshot_trace_chunk); | |
5657 | snapshot_trace_chunk = NULL; | |
5658 | if (ret) { | |
348a81dc JG |
5659 | ERR("Failed to set temporary trace chunk to record a snapshot of session \"%s\"", |
5660 | session->name); | |
5661 | ret_code = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER; | |
5662 | goto error; | |
d2956687 | 5663 | } |
fb9a95c4 JG |
5664 | |
5665 | nb_packets_per_stream = get_session_nb_packets_per_stream(session, | |
5666 | snapshot_output->max_size); | |
5667 | if (nb_packets_per_stream < 0) { | |
348a81dc | 5668 | ret_code = LTTNG_ERR_MAX_SIZE_INVALID; |
5151d412 | 5669 | goto error_close_trace_chunk; |
fb9a95c4 JG |
5670 | } |
5671 | ||
5672 | if (session->kernel_session) { | |
348a81dc JG |
5673 | ret_code = record_kernel_snapshot(session->kernel_session, |
5674 | snapshot_kernel_consumer_output, session, | |
fb9a95c4 | 5675 | wait, nb_packets_per_stream); |
348a81dc | 5676 | if (ret_code != LTTNG_OK) { |
5151d412 | 5677 | goto error_close_trace_chunk; |
fb9a95c4 JG |
5678 | } |
5679 | } | |
5680 | ||
5681 | if (session->ust_session) { | |
348a81dc JG |
5682 | ret_code = record_ust_snapshot(session->ust_session, |
5683 | snapshot_ust_consumer_output, session, | |
fb9a95c4 | 5684 | wait, nb_packets_per_stream); |
348a81dc | 5685 | if (ret_code != LTTNG_OK) { |
5151d412 | 5686 | goto error_close_trace_chunk; |
fb9a95c4 JG |
5687 | } |
5688 | } | |
d2956687 | 5689 | |
5151d412 | 5690 | error_close_trace_chunk: |
dbfee52c MD |
5691 | if (session_set_trace_chunk(session, NULL, &snapshot_trace_chunk)) { |
5692 | ERR("Failed to release the current trace chunk of session \"%s\"", | |
5693 | session->name); | |
5694 | ret_code = LTTNG_ERR_UNK; | |
5695 | } | |
5696 | ||
5697 | if (session_close_trace_chunk(session, snapshot_trace_chunk, | |
343defc2 | 5698 | LTTNG_TRACE_CHUNK_COMMAND_TYPE_NO_OPERATION, NULL)) { |
d2956687 JG |
5699 | /* |
5700 | * Don't goto end; make sure the chunk is closed for the session | |
5701 | * to allow future snapshots. | |
5702 | */ | |
5703 | ERR("Failed to close snapshot trace chunk of session \"%s\"", | |
5704 | session->name); | |
348a81dc | 5705 | ret_code = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER; |
d2956687 | 5706 | } |
348a81dc JG |
5707 | error: |
5708 | if (original_ust_consumer_output) { | |
5709 | session->ust_session->consumer = original_ust_consumer_output; | |
5710 | } | |
5711 | if (original_kernel_consumer_output) { | |
5712 | session->kernel_session->consumer = | |
5713 | original_kernel_consumer_output; | |
5714 | } | |
5715 | consumer_output_put(snapshot_ust_consumer_output); | |
5716 | consumer_output_put(snapshot_kernel_consumer_output); | |
5717 | return ret_code; | |
fb9a95c4 JG |
5718 | } |
5719 | ||
6dc3064a DG |
5720 | /* |
5721 | * Command LTTNG_SNAPSHOT_RECORD from lib lttng ctl. | |
5722 | * | |
5723 | * The wait parameter is ignored so this call always wait for the snapshot to | |
5724 | * complete before returning. | |
5725 | * | |
5726 | * Return LTTNG_OK on success or else a LTTNG_ERR code. | |
5727 | */ | |
5728 | int cmd_snapshot_record(struct ltt_session *session, | |
df4f5a87 | 5729 | const struct lttng_snapshot_output *output, int wait) |
6dc3064a | 5730 | { |
9a654598 JG |
5731 | enum lttng_error_code cmd_ret = LTTNG_OK; |
5732 | int ret; | |
00e1dfc4 | 5733 | unsigned int snapshot_success = 0; |
10ba83fe | 5734 | char datetime[16]; |
2abe7969 | 5735 | struct snapshot_output *tmp_output = NULL; |
6dc3064a DG |
5736 | |
5737 | assert(session); | |
ba45d9f0 | 5738 | assert(output); |
6dc3064a DG |
5739 | |
5740 | DBG("Cmd snapshot record for session %s", session->name); | |
5741 | ||
10ba83fe JR |
5742 | /* Get the datetime for the snapshot output directory. */ |
5743 | ret = utils_get_current_time_str("%Y%m%d-%H%M%S", datetime, | |
5744 | sizeof(datetime)); | |
5745 | if (!ret) { | |
9a654598 | 5746 | cmd_ret = LTTNG_ERR_INVALID; |
10ba83fe JR |
5747 | goto error; |
5748 | } | |
5749 | ||
6dc3064a | 5750 | /* |
d3f14b8a MD |
5751 | * Permission denied to create an output if the session is not |
5752 | * set in no output mode. | |
6dc3064a DG |
5753 | */ |
5754 | if (session->output_traces) { | |
9a654598 | 5755 | cmd_ret = LTTNG_ERR_NOT_SNAPSHOT_SESSION; |
6dc3064a DG |
5756 | goto error; |
5757 | } | |
5758 | ||
5759 | /* The session needs to be started at least once. */ | |
8382cf6f | 5760 | if (!session->has_been_started) { |
9a654598 | 5761 | cmd_ret = LTTNG_ERR_START_SESSION_ONCE; |
6dc3064a DG |
5762 | goto error; |
5763 | } | |
5764 | ||
5765 | /* Use temporary output for the session. */ | |
ba45d9f0 | 5766 | if (*output->ctrl_url != '\0') { |
2abe7969 JG |
5767 | tmp_output = snapshot_output_alloc(); |
5768 | if (!tmp_output) { | |
5769 | cmd_ret = LTTNG_ERR_NOMEM; | |
5770 | goto error; | |
5771 | } | |
5772 | ||
b178f53e JG |
5773 | ret = snapshot_output_init(session, output->max_size, |
5774 | output->name, | |
5775 | output->ctrl_url, output->data_url, | |
5776 | session->consumer, | |
2abe7969 | 5777 | tmp_output, NULL); |
6dc3064a DG |
5778 | if (ret < 0) { |
5779 | if (ret == -ENOMEM) { | |
9a654598 | 5780 | cmd_ret = LTTNG_ERR_NOMEM; |
6dc3064a | 5781 | } else { |
9a654598 | 5782 | cmd_ret = LTTNG_ERR_INVALID; |
6dc3064a DG |
5783 | } |
5784 | goto error; | |
5785 | } | |
1bfe7328 | 5786 | /* Use the global session count for the temporary snapshot. */ |
2abe7969 | 5787 | tmp_output->nb_snapshot = session->snapshot.nb_snapshot; |
10ba83fe JR |
5788 | |
5789 | /* Use the global datetime */ | |
2abe7969 JG |
5790 | memcpy(tmp_output->datetime, datetime, sizeof(datetime)); |
5791 | cmd_ret = snapshot_record(session, tmp_output, wait); | |
fb9a95c4 | 5792 | if (cmd_ret != LTTNG_OK) { |
804c90a8 JR |
5793 | goto error; |
5794 | } | |
804c90a8 JR |
5795 | snapshot_success = 1; |
5796 | } else { | |
5797 | struct snapshot_output *sout; | |
5798 | struct lttng_ht_iter iter; | |
68808f4e | 5799 | |
804c90a8 JR |
5800 | rcu_read_lock(); |
5801 | cds_lfht_for_each_entry(session->snapshot.output_ht->ht, | |
5802 | &iter.iter, sout, node.node) { | |
2abe7969 JG |
5803 | struct snapshot_output output_copy; |
5804 | ||
804c90a8 | 5805 | /* |
2abe7969 JG |
5806 | * Make a local copy of the output and override output |
5807 | * parameters with those provided as part of the | |
5808 | * command. | |
804c90a8 | 5809 | */ |
2abe7969 | 5810 | memcpy(&output_copy, sout, sizeof(output_copy)); |
1bfe7328 | 5811 | |
804c90a8 | 5812 | if (output->max_size != (uint64_t) -1ULL) { |
2abe7969 | 5813 | output_copy.max_size = output->max_size; |
6dc3064a | 5814 | } |
d07ceecd | 5815 | |
2abe7969 JG |
5816 | output_copy.nb_snapshot = session->snapshot.nb_snapshot; |
5817 | memcpy(output_copy.datetime, datetime, | |
5818 | sizeof(datetime)); | |
6dc3064a | 5819 | |
804c90a8 JR |
5820 | /* Use temporary name. */ |
5821 | if (*output->name != '\0') { | |
2abe7969 JG |
5822 | if (lttng_strncpy(output_copy.name, |
5823 | output->name, | |
5824 | sizeof(output_copy.name))) { | |
9a654598 | 5825 | cmd_ret = LTTNG_ERR_INVALID; |
cf3e357d MD |
5826 | rcu_read_unlock(); |
5827 | goto error; | |
5828 | } | |
804c90a8 | 5829 | } |
e1986656 | 5830 | |
2abe7969 | 5831 | cmd_ret = snapshot_record(session, &output_copy, wait); |
fb9a95c4 JG |
5832 | if (cmd_ret != LTTNG_OK) { |
5833 | rcu_read_unlock(); | |
5834 | goto error; | |
6dc3064a | 5835 | } |
804c90a8 | 5836 | snapshot_success = 1; |
6dc3064a | 5837 | } |
804c90a8 | 5838 | rcu_read_unlock(); |
6dc3064a DG |
5839 | } |
5840 | ||
1bfe7328 DG |
5841 | if (snapshot_success) { |
5842 | session->snapshot.nb_snapshot++; | |
b67578cb | 5843 | } else { |
9a654598 | 5844 | cmd_ret = LTTNG_ERR_SNAPSHOT_FAIL; |
1bfe7328 DG |
5845 | } |
5846 | ||
6dc3064a | 5847 | error: |
2abe7969 JG |
5848 | if (tmp_output) { |
5849 | snapshot_output_destroy(tmp_output); | |
5850 | } | |
9a654598 | 5851 | return cmd_ret; |
6dc3064a DG |
5852 | } |
5853 | ||
d7ba1388 MD |
5854 | /* |
5855 | * Command LTTNG_SET_SESSION_SHM_PATH processed by the client thread. | |
5856 | */ | |
5857 | int cmd_set_session_shm_path(struct ltt_session *session, | |
5858 | const char *shm_path) | |
5859 | { | |
5860 | /* Safety net */ | |
5861 | assert(session); | |
5862 | ||
5863 | /* | |
5864 | * Can only set shm path before session is started. | |
5865 | */ | |
5866 | if (session->has_been_started) { | |
5867 | return LTTNG_ERR_SESSION_STARTED; | |
5868 | } | |
5869 | ||
5870 | strncpy(session->shm_path, shm_path, | |
5871 | sizeof(session->shm_path)); | |
5872 | session->shm_path[sizeof(session->shm_path) - 1] = '\0'; | |
5873 | ||
7e397c55 | 5874 | return LTTNG_OK; |
d7ba1388 MD |
5875 | } |
5876 | ||
5c408ad8 JD |
5877 | /* |
5878 | * Command LTTNG_ROTATE_SESSION from the lttng-ctl library. | |
5879 | * | |
5880 | * Ask the consumer to rotate the session output directory. | |
5881 | * The session lock must be held. | |
5882 | * | |
d5a1b7aa | 5883 | * Returns LTTNG_OK on success or else a negative LTTng error code. |
5c408ad8 JD |
5884 | */ |
5885 | int cmd_rotate_session(struct ltt_session *session, | |
7fdbed1c | 5886 | struct lttng_rotate_session_return *rotate_return, |
343defc2 MD |
5887 | bool quiet_rotation, |
5888 | enum lttng_trace_chunk_command_type command) | |
5c408ad8 JD |
5889 | { |
5890 | int ret; | |
d2956687 | 5891 | uint64_t ongoing_rotation_chunk_id; |
d5a1b7aa | 5892 | enum lttng_error_code cmd_ret = LTTNG_OK; |
d2956687 JG |
5893 | struct lttng_trace_chunk *chunk_being_archived = NULL; |
5894 | struct lttng_trace_chunk *new_trace_chunk = NULL; | |
5895 | enum lttng_trace_chunk_status chunk_status; | |
3156892b JG |
5896 | bool failed_to_rotate = false; |
5897 | enum lttng_error_code rotation_fail_code = LTTNG_OK; | |
5c408ad8 JD |
5898 | |
5899 | assert(session); | |
5900 | ||
5901 | if (!session->has_been_started) { | |
d5a1b7aa | 5902 | cmd_ret = LTTNG_ERR_START_SESSION_ONCE; |
d68c9a04 | 5903 | goto end; |
5c408ad8 JD |
5904 | } |
5905 | ||
d48d65e1 MD |
5906 | /* |
5907 | * Explicit rotation is not supported for live sessions. | |
5908 | * However, live sessions can perform a quiet rotation on | |
5909 | * destroy. | |
5910 | * Rotation is not supported for snapshot traces (no output). | |
5911 | */ | |
5912 | if ((!quiet_rotation && session->live_timer) || | |
5913 | !session->output_traces) { | |
d5a1b7aa | 5914 | cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE; |
d68c9a04 | 5915 | goto end; |
5c408ad8 JD |
5916 | } |
5917 | ||
d2956687 | 5918 | /* Unsupported feature in lttng-relayd before 2.11. */ |
070b6a86 | 5919 | if (!quiet_rotation && session->consumer->type == CONSUMER_DST_NET && |
5c408ad8 JD |
5920 | (session->consumer->relay_major_version == 2 && |
5921 | session->consumer->relay_minor_version < 11)) { | |
d5a1b7aa | 5922 | cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_RELAY; |
d68c9a04 | 5923 | goto end; |
5c408ad8 JD |
5924 | } |
5925 | ||
a40a503f MD |
5926 | /* Unsupported feature in lttng-modules before 2.8 (lack of sequence number). */ |
5927 | if (session->kernel_session && !kernel_supports_ring_buffer_packet_sequence_number()) { | |
5928 | cmd_ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE_KERNEL; | |
5929 | goto end; | |
5930 | } | |
5931 | ||
92816cc3 | 5932 | if (session->rotation_state == LTTNG_ROTATION_STATE_ONGOING) { |
92816cc3 JG |
5933 | DBG("Refusing to launch a rotation; a rotation is already in progress for session %s", |
5934 | session->name); | |
d5a1b7aa | 5935 | cmd_ret = LTTNG_ERR_ROTATION_PENDING; |
d68c9a04 | 5936 | goto end; |
5c408ad8 JD |
5937 | } |
5938 | ||
5939 | /* | |
5940 | * After a stop, we only allow one rotation to occur, the other ones are | |
5941 | * useless until a new start. | |
5942 | */ | |
5943 | if (session->rotated_after_last_stop) { | |
5944 | DBG("Session \"%s\" was already rotated after stop, refusing rotation", | |
5945 | session->name); | |
d5a1b7aa | 5946 | cmd_ret = LTTNG_ERR_ROTATION_MULTIPLE_AFTER_STOP; |
d68c9a04 | 5947 | goto end; |
5c408ad8 | 5948 | } |
b02f5986 MD |
5949 | |
5950 | /* | |
5951 | * After a stop followed by a clear, disallow following rotations a they would | |
5952 | * generate empty chunks. | |
5953 | */ | |
5954 | if (session->cleared_after_last_stop) { | |
5955 | DBG("Session \"%s\" was already cleared after stop, refusing rotation", | |
5956 | session->name); | |
5957 | cmd_ret = LTTNG_ERR_ROTATION_AFTER_STOP_CLEAR; | |
5958 | goto end; | |
5959 | } | |
5960 | ||
d2956687 | 5961 | if (session->active) { |
348a81dc | 5962 | new_trace_chunk = session_create_new_trace_chunk(session, NULL, |
d2956687 JG |
5963 | NULL, NULL); |
5964 | if (!new_trace_chunk) { | |
5965 | cmd_ret = LTTNG_ERR_CREATE_DIR_FAIL; | |
5966 | goto error; | |
5c408ad8 | 5967 | } |
0e270a1e | 5968 | } |
2961f09e | 5969 | |
3156892b JG |
5970 | /* |
5971 | * The current trace chunk becomes the chunk being archived. | |
5972 | * | |
5973 | * After this point, "chunk_being_archived" must absolutely | |
5974 | * be closed on the consumer(s), otherwise it will never be | |
5975 | * cleaned-up, which will result in a leak. | |
5976 | */ | |
d2956687 JG |
5977 | ret = session_set_trace_chunk(session, new_trace_chunk, |
5978 | &chunk_being_archived); | |
5979 | if (ret) { | |
5980 | cmd_ret = LTTNG_ERR_CREATE_TRACE_CHUNK_FAIL_CONSUMER; | |
b178f53e JG |
5981 | goto error; |
5982 | } | |
5983 | ||
5c408ad8 | 5984 | if (session->kernel_session) { |
d5a1b7aa JG |
5985 | cmd_ret = kernel_rotate_session(session); |
5986 | if (cmd_ret != LTTNG_OK) { | |
3156892b JG |
5987 | failed_to_rotate = true; |
5988 | rotation_fail_code = cmd_ret; | |
5c408ad8 JD |
5989 | } |
5990 | } | |
5991 | if (session->ust_session) { | |
d5a1b7aa JG |
5992 | cmd_ret = ust_app_rotate_session(session); |
5993 | if (cmd_ret != LTTNG_OK) { | |
3156892b JG |
5994 | failed_to_rotate = true; |
5995 | rotation_fail_code = cmd_ret; | |
5c408ad8 | 5996 | } |
92816cc3 | 5997 | } |
17dd1232 | 5998 | |
3b61d9ee JG |
5999 | if (!session->active) { |
6000 | session->rotated_after_last_stop = true; | |
6001 | } | |
6002 | ||
6003 | if (!chunk_being_archived) { | |
6004 | DBG("Rotating session \"%s\" from a \"NULL\" trace chunk to a new trace chunk, skipping completion check", | |
6005 | session->name); | |
6006 | if (failed_to_rotate) { | |
6007 | cmd_ret = rotation_fail_code; | |
6008 | goto error; | |
6009 | } | |
6010 | cmd_ret = LTTNG_OK; | |
6011 | goto end; | |
6012 | } | |
6013 | ||
6014 | session->rotation_state = LTTNG_ROTATION_STATE_ONGOING; | |
6015 | chunk_status = lttng_trace_chunk_get_id(chunk_being_archived, | |
6016 | &ongoing_rotation_chunk_id); | |
6017 | assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK); | |
6018 | ||
bbc4768c | 6019 | ret = session_close_trace_chunk(session, chunk_being_archived, |
343defc2 | 6020 | command, session->last_chunk_path); |
d2956687 JG |
6021 | if (ret) { |
6022 | cmd_ret = LTTNG_ERR_CLOSE_TRACE_CHUNK_FAIL_CONSUMER; | |
6023 | goto error; | |
6024 | } | |
6025 | ||
3156892b JG |
6026 | if (failed_to_rotate) { |
6027 | cmd_ret = rotation_fail_code; | |
6028 | goto error; | |
6029 | } | |
6030 | ||
7fdbed1c | 6031 | session->quiet_rotation = quiet_rotation; |
92816cc3 JG |
6032 | ret = timer_session_rotation_pending_check_start(session, |
6033 | DEFAULT_ROTATE_PENDING_TIMER); | |
6034 | if (ret) { | |
d5a1b7aa | 6035 | cmd_ret = LTTNG_ERR_UNK; |
2961f09e | 6036 | goto error; |
5c408ad8 JD |
6037 | } |
6038 | ||
5c408ad8 | 6039 | if (rotate_return) { |
d2956687 | 6040 | rotate_return->rotation_id = ongoing_rotation_chunk_id; |
5c408ad8 JD |
6041 | } |
6042 | ||
d2956687 JG |
6043 | session->chunk_being_archived = chunk_being_archived; |
6044 | chunk_being_archived = NULL; | |
7fdbed1c JG |
6045 | if (!quiet_rotation) { |
6046 | ret = notification_thread_command_session_rotation_ongoing( | |
6047 | notification_thread_handle, | |
6048 | session->name, session->uid, session->gid, | |
6049 | ongoing_rotation_chunk_id); | |
6050 | if (ret != LTTNG_OK) { | |
6051 | ERR("Failed to notify notification thread that a session rotation is ongoing for session %s", | |
6052 | session->name); | |
6053 | cmd_ret = ret; | |
6054 | } | |
2961f09e JG |
6055 | } |
6056 | ||
92816cc3 | 6057 | DBG("Cmd rotate session %s, archive_id %" PRIu64 " sent", |
d2956687 | 6058 | session->name, ongoing_rotation_chunk_id); |
5c408ad8 | 6059 | end: |
d2956687 JG |
6060 | lttng_trace_chunk_put(new_trace_chunk); |
6061 | lttng_trace_chunk_put(chunk_being_archived); | |
d5a1b7aa | 6062 | ret = (cmd_ret == LTTNG_OK) ? cmd_ret : -((int) cmd_ret); |
5c408ad8 | 6063 | return ret; |
2961f09e | 6064 | error: |
2961f09e | 6065 | if (session_reset_rotation_state(session, |
d2956687 | 6066 | LTTNG_ROTATION_STATE_ERROR)) { |
2961f09e JG |
6067 | ERR("Failed to reset rotation state of session \"%s\"", |
6068 | session->name); | |
6069 | } | |
6070 | goto end; | |
5c408ad8 JD |
6071 | } |
6072 | ||
6073 | /* | |
d68c9a04 | 6074 | * Command LTTNG_ROTATION_GET_INFO from the lttng-ctl library. |
5c408ad8 JD |
6075 | * |
6076 | * Check if the session has finished its rotation. | |
6077 | * | |
d2956687 | 6078 | * Return LTTNG_OK on success or else an LTTNG_ERR code. |
5c408ad8 | 6079 | */ |
d68c9a04 JD |
6080 | int cmd_rotate_get_info(struct ltt_session *session, |
6081 | struct lttng_rotation_get_info_return *info_return, | |
6082 | uint64_t rotation_id) | |
5c408ad8 | 6083 | { |
d2956687 JG |
6084 | enum lttng_error_code cmd_ret = LTTNG_OK; |
6085 | enum lttng_rotation_state rotation_state; | |
5c408ad8 | 6086 | |
d68c9a04 | 6087 | DBG("Cmd rotate_get_info session %s, rotation id %" PRIu64, session->name, |
d2956687 | 6088 | session->most_recent_chunk_id.value); |
5c408ad8 | 6089 | |
d2956687 JG |
6090 | if (session->chunk_being_archived) { |
6091 | enum lttng_trace_chunk_status chunk_status; | |
6092 | uint64_t chunk_id; | |
6093 | ||
6094 | chunk_status = lttng_trace_chunk_get_id( | |
6095 | session->chunk_being_archived, | |
6096 | &chunk_id); | |
6097 | assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK); | |
6098 | ||
6099 | rotation_state = rotation_id == chunk_id ? | |
6100 | LTTNG_ROTATION_STATE_ONGOING : | |
6101 | LTTNG_ROTATION_STATE_EXPIRED; | |
6102 | } else { | |
6103 | if (session->last_archived_chunk_id.is_set && | |
6104 | rotation_id != session->last_archived_chunk_id.value) { | |
6105 | rotation_state = LTTNG_ROTATION_STATE_EXPIRED; | |
6106 | } else { | |
6107 | rotation_state = session->rotation_state; | |
6108 | } | |
5c408ad8 JD |
6109 | } |
6110 | ||
d2956687 JG |
6111 | switch (rotation_state) { |
6112 | case LTTNG_ROTATION_STATE_NO_ROTATION: | |
83ed9e90 | 6113 | DBG("Reporting that no rotation has occurred within the lifetime of session \"%s\"", |
d2956687 JG |
6114 | session->name); |
6115 | goto end; | |
6116 | case LTTNG_ROTATION_STATE_EXPIRED: | |
6117 | DBG("Reporting that the rotation state of rotation id %" PRIu64 " of session \"%s\" has expired", | |
6118 | rotation_id, session->name); | |
6119 | break; | |
d68c9a04 | 6120 | case LTTNG_ROTATION_STATE_ONGOING: |
d2956687 | 6121 | DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is still pending", |
d68c9a04 JD |
6122 | rotation_id, session->name); |
6123 | break; | |
6124 | case LTTNG_ROTATION_STATE_COMPLETED: | |
dd73d57b | 6125 | { |
d2956687 JG |
6126 | int fmt_ret; |
6127 | char *chunk_path; | |
dd73d57b JG |
6128 | char *current_tracing_path_reply; |
6129 | size_t current_tracing_path_reply_len; | |
6130 | ||
d2956687 JG |
6131 | DBG("Reporting that rotation id %" PRIu64 " of session \"%s\" is completed", |
6132 | rotation_id, session->name); | |
6133 | ||
dd73d57b JG |
6134 | switch (session_get_consumer_destination_type(session)) { |
6135 | case CONSUMER_DST_LOCAL: | |
6136 | current_tracing_path_reply = | |
6137 | info_return->location.local.absolute_path; | |
6138 | current_tracing_path_reply_len = | |
6139 | sizeof(info_return->location.local.absolute_path); | |
6140 | info_return->location_type = | |
05f8afa9 | 6141 | (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL; |
ecd1a12f MD |
6142 | fmt_ret = asprintf(&chunk_path, |
6143 | "%s/" DEFAULT_ARCHIVED_TRACE_CHUNKS_DIRECTORY "/%s", | |
6144 | session_get_base_path(session), | |
6145 | session->last_archived_chunk_name); | |
6146 | if (fmt_ret == -1) { | |
6147 | PERROR("Failed to format the path of the last archived trace chunk"); | |
6148 | info_return->status = LTTNG_ROTATION_STATUS_ERROR; | |
6149 | cmd_ret = LTTNG_ERR_UNK; | |
6150 | goto end; | |
6151 | } | |
dd73d57b JG |
6152 | break; |
6153 | case CONSUMER_DST_NET: | |
09cfbe47 JG |
6154 | { |
6155 | uint16_t ctrl_port, data_port; | |
6156 | ||
dd73d57b JG |
6157 | current_tracing_path_reply = |
6158 | info_return->location.relay.relative_path; | |
6159 | current_tracing_path_reply_len = | |
6160 | sizeof(info_return->location.relay.relative_path); | |
6161 | /* Currently the only supported relay protocol. */ | |
6162 | info_return->location.relay.protocol = | |
05f8afa9 | 6163 | (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP; |
dd73d57b | 6164 | |
d2956687 | 6165 | fmt_ret = lttng_strncpy(info_return->location.relay.host, |
dd73d57b JG |
6166 | session_get_net_consumer_hostname(session), |
6167 | sizeof(info_return->location.relay.host)); | |
d2956687 JG |
6168 | if (fmt_ret) { |
6169 | ERR("Failed to copy host name to rotate_get_info reply"); | |
dd73d57b | 6170 | info_return->status = LTTNG_ROTATION_STATUS_ERROR; |
d2956687 | 6171 | cmd_ret = LTTNG_ERR_SET_URL; |
dd73d57b JG |
6172 | goto end; |
6173 | } | |
6174 | ||
09cfbe47 JG |
6175 | session_get_net_consumer_ports(session, &ctrl_port, &data_port); |
6176 | info_return->location.relay.ports.control = ctrl_port; | |
6177 | info_return->location.relay.ports.data = data_port; | |
dd73d57b | 6178 | info_return->location_type = |
05f8afa9 | 6179 | (int8_t) LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY; |
ecd1a12f MD |
6180 | chunk_path = strdup(session->last_chunk_path); |
6181 | if (!chunk_path) { | |
6182 | ERR("Failed to allocate the path of the last archived trace chunk"); | |
6183 | info_return->status = LTTNG_ROTATION_STATUS_ERROR; | |
6184 | cmd_ret = LTTNG_ERR_UNK; | |
6185 | goto end; | |
6186 | } | |
dd73d57b | 6187 | break; |
09cfbe47 | 6188 | } |
dd73d57b JG |
6189 | default: |
6190 | abort(); | |
6191 | } | |
d2956687 JG |
6192 | |
6193 | fmt_ret = lttng_strncpy(current_tracing_path_reply, | |
6194 | chunk_path, current_tracing_path_reply_len); | |
6195 | free(chunk_path); | |
6196 | if (fmt_ret) { | |
6197 | ERR("Failed to copy path of the last archived trace chunk to rotate_get_info reply"); | |
d68c9a04 | 6198 | info_return->status = LTTNG_ROTATION_STATUS_ERROR; |
d2956687 | 6199 | cmd_ret = LTTNG_ERR_UNK; |
5c408ad8 JD |
6200 | goto end; |
6201 | } | |
dd73d57b | 6202 | |
d68c9a04 | 6203 | break; |
dd73d57b | 6204 | } |
d68c9a04 | 6205 | case LTTNG_ROTATION_STATE_ERROR: |
d2956687 | 6206 | DBG("Reporting that an error occurred during rotation %" PRIu64 " of session \"%s\"", |
d68c9a04 JD |
6207 | rotation_id, session->name); |
6208 | break; | |
6209 | default: | |
6210 | abort(); | |
5c408ad8 JD |
6211 | } |
6212 | ||
d2956687 | 6213 | cmd_ret = LTTNG_OK; |
5c408ad8 | 6214 | end: |
d2956687 JG |
6215 | info_return->status = (int32_t) rotation_state; |
6216 | return cmd_ret; | |
5c408ad8 JD |
6217 | } |
6218 | ||
259c2674 JD |
6219 | /* |
6220 | * Command LTTNG_ROTATION_SET_SCHEDULE from the lttng-ctl library. | |
6221 | * | |
6222 | * Configure the automatic rotation parameters. | |
66ea93b1 JG |
6223 | * 'activate' to true means activate the rotation schedule type with 'new_value'. |
6224 | * 'activate' to false means deactivate the rotation schedule and validate that | |
6225 | * 'new_value' has the same value as the currently active value. | |
259c2674 | 6226 | * |
1136f41b | 6227 | * Return LTTNG_OK on success or else a positive LTTNG_ERR code. |
259c2674 JD |
6228 | */ |
6229 | int cmd_rotation_set_schedule(struct ltt_session *session, | |
66ea93b1 JG |
6230 | bool activate, enum lttng_rotation_schedule_type schedule_type, |
6231 | uint64_t new_value, | |
90936dcf | 6232 | struct notification_thread_handle *notification_thread_handle) |
259c2674 JD |
6233 | { |
6234 | int ret; | |
66ea93b1 | 6235 | uint64_t *parameter_value; |
259c2674 JD |
6236 | |
6237 | assert(session); | |
6238 | ||
6239 | DBG("Cmd rotate set schedule session %s", session->name); | |
6240 | ||
92fe5ca1 | 6241 | if (session->live_timer || !session->output_traces) { |
66ea93b1 | 6242 | DBG("Failing ROTATION_SET_SCHEDULE command as the rotation feature is not available for this session"); |
259c2674 JD |
6243 | ret = LTTNG_ERR_ROTATION_NOT_AVAILABLE; |
6244 | goto end; | |
6245 | } | |
6246 | ||
66ea93b1 JG |
6247 | switch (schedule_type) { |
6248 | case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD: | |
6249 | parameter_value = &session->rotate_size; | |
6250 | break; | |
6251 | case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC: | |
6252 | parameter_value = &session->rotate_timer_period; | |
6253 | if (new_value >= UINT_MAX) { | |
6254 | DBG("Failing ROTATION_SET_SCHEDULE command as the value requested for a periodic rotation schedule is invalid: %" PRIu64 " > %u (UINT_MAX)", | |
6255 | new_value, UINT_MAX); | |
6256 | ret = LTTNG_ERR_INVALID; | |
6257 | goto end; | |
6258 | } | |
6259 | break; | |
6260 | default: | |
6261 | WARN("Failing ROTATION_SET_SCHEDULE command on unknown schedule type"); | |
6262 | ret = LTTNG_ERR_INVALID; | |
259c2674 | 6263 | goto end; |
66ea93b1 JG |
6264 | } |
6265 | ||
6266 | /* Improper use of the API. */ | |
6267 | if (new_value == -1ULL) { | |
6268 | WARN("Failing ROTATION_SET_SCHEDULE command as the value requested is -1"); | |
6269 | ret = LTTNG_ERR_INVALID; | |
259c2674 JD |
6270 | goto end; |
6271 | } | |
6272 | ||
66ea93b1 JG |
6273 | /* |
6274 | * As indicated in struct ltt_session's comments, a value of == 0 means | |
6275 | * this schedule rotation type is not in use. | |
6276 | * | |
6277 | * Reject the command if we were asked to activate a schedule that was | |
6278 | * already active. | |
6279 | */ | |
6280 | if (activate && *parameter_value != 0) { | |
6281 | DBG("Failing ROTATION_SET_SCHEDULE (activate) command as the schedule is already active"); | |
6282 | ret = LTTNG_ERR_ROTATION_SCHEDULE_SET; | |
90936dcf | 6283 | goto end; |
66ea93b1 JG |
6284 | } |
6285 | ||
6286 | /* | |
6287 | * Reject the command if we were asked to deactivate a schedule that was | |
6288 | * not active. | |
6289 | */ | |
6290 | if (!activate && *parameter_value == 0) { | |
6291 | DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as the schedule is already inactive"); | |
6292 | ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET; | |
90936dcf JD |
6293 | goto end; |
6294 | } | |
6295 | ||
66ea93b1 JG |
6296 | /* |
6297 | * Reject the command if we were asked to deactivate a schedule that | |
6298 | * doesn't exist. | |
6299 | */ | |
6300 | if (!activate && *parameter_value != new_value) { | |
6301 | DBG("Failing ROTATION_SET_SCHEDULE (deactivate) command as an inexistant schedule was provided"); | |
6302 | ret = LTTNG_ERR_ROTATION_SCHEDULE_NOT_SET; | |
6303 | goto end; | |
6304 | } | |
259c2674 | 6305 | |
66ea93b1 JG |
6306 | *parameter_value = activate ? new_value : 0; |
6307 | ||
6308 | switch (schedule_type) { | |
6309 | case LTTNG_ROTATION_SCHEDULE_TYPE_PERIODIC: | |
6310 | if (activate && session->active) { | |
6311 | /* | |
6312 | * Only start the timer if the session is active, | |
6313 | * otherwise it will be started when the session starts. | |
6314 | */ | |
92816cc3 JG |
6315 | ret = timer_session_rotation_schedule_timer_start( |
6316 | session, new_value); | |
259c2674 | 6317 | if (ret) { |
66ea93b1 | 6318 | ERR("Failed to enable session rotation timer in ROTATION_SET_SCHEDULE command"); |
259c2674 JD |
6319 | ret = LTTNG_ERR_UNK; |
6320 | goto end; | |
6321 | } | |
66ea93b1 | 6322 | } else { |
92816cc3 JG |
6323 | ret = timer_session_rotation_schedule_timer_stop( |
6324 | session); | |
66ea93b1 JG |
6325 | if (ret) { |
6326 | ERR("Failed to disable session rotation timer in ROTATION_SET_SCHEDULE command"); | |
6327 | ret = LTTNG_ERR_UNK; | |
f3ce6946 | 6328 | goto end; |
66ea93b1 | 6329 | } |
259c2674 | 6330 | } |
66ea93b1 JG |
6331 | break; |
6332 | case LTTNG_ROTATION_SCHEDULE_TYPE_SIZE_THRESHOLD: | |
6333 | if (activate) { | |
6334 | ret = subscribe_session_consumed_size_rotation(session, | |
6335 | new_value, notification_thread_handle); | |
90936dcf | 6336 | if (ret) { |
66ea93b1 | 6337 | ERR("Failed to enable consumed-size notification in ROTATION_SET_SCHEDULE command"); |
90936dcf JD |
6338 | ret = LTTNG_ERR_UNK; |
6339 | goto end; | |
6340 | } | |
90936dcf | 6341 | } else { |
66ea93b1 JG |
6342 | ret = unsubscribe_session_consumed_size_rotation(session, |
6343 | notification_thread_handle); | |
90936dcf | 6344 | if (ret) { |
66ea93b1 | 6345 | ERR("Failed to disable consumed-size notification in ROTATION_SET_SCHEDULE command"); |
90936dcf JD |
6346 | ret = LTTNG_ERR_UNK; |
6347 | goto end; | |
6348 | } | |
66ea93b1 | 6349 | |
90936dcf | 6350 | } |
66ea93b1 JG |
6351 | break; |
6352 | default: | |
6353 | /* Would have been caught before. */ | |
6354 | abort(); | |
90936dcf JD |
6355 | } |
6356 | ||
259c2674 JD |
6357 | ret = LTTNG_OK; |
6358 | ||
6359 | goto end; | |
6360 | ||
6361 | end: | |
6362 | return ret; | |
6363 | } | |
6364 | ||
a503e1ef JG |
6365 | /* Wait for a given path to be removed before continuing. */ |
6366 | static enum lttng_error_code wait_on_path(void *path_data) | |
6367 | { | |
6368 | const char *shm_path = path_data; | |
6369 | ||
6370 | DBG("Waiting for the shm path at %s to be removed before completing session destruction", | |
6371 | shm_path); | |
6372 | while (true) { | |
6373 | int ret; | |
6374 | struct stat st; | |
6375 | ||
6376 | ret = stat(shm_path, &st); | |
6377 | if (ret) { | |
6378 | if (errno != ENOENT) { | |
6379 | PERROR("stat() returned an error while checking for the existence of the shm path"); | |
6380 | } else { | |
6381 | DBG("shm path no longer exists, completing the destruction of session"); | |
6382 | } | |
6383 | break; | |
6384 | } else { | |
6385 | if (!S_ISDIR(st.st_mode)) { | |
6386 | ERR("The type of shm path %s returned by stat() is not a directory; aborting the wait for shm path removal", | |
6387 | shm_path); | |
6388 | break; | |
6389 | } | |
6390 | } | |
6391 | usleep(SESSION_DESTROY_SHM_PATH_CHECK_DELAY_US); | |
6392 | } | |
6393 | return LTTNG_OK; | |
6394 | } | |
6395 | ||
6396 | /* | |
6397 | * Returns a pointer to a handler to run on completion of a command. | |
6398 | * Returns NULL if no handler has to be run for the last command executed. | |
6399 | */ | |
6400 | const struct cmd_completion_handler *cmd_pop_completion_handler(void) | |
6401 | { | |
6402 | struct cmd_completion_handler *handler = current_completion_handler; | |
6403 | ||
6404 | current_completion_handler = NULL; | |
6405 | return handler; | |
6406 | } | |
6407 | ||
2f77fc4b DG |
6408 | /* |
6409 | * Init command subsystem. | |
6410 | */ | |
6411 | void cmd_init(void) | |
6412 | { | |
6413 | /* | |
d88aee68 DG |
6414 | * Set network sequence index to 1 for streams to match a relayd |
6415 | * socket on the consumer side. | |
2f77fc4b | 6416 | */ |
d88aee68 DG |
6417 | pthread_mutex_lock(&relayd_net_seq_idx_lock); |
6418 | relayd_net_seq_idx = 1; | |
6419 | pthread_mutex_unlock(&relayd_net_seq_idx_lock); | |
2f77fc4b DG |
6420 | |
6421 | DBG("Command subsystem initialized"); | |
6422 | } |