2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
10 #define _LTT_UST_APP_H
12 #include "session.hpp"
13 #include "trace-ust.hpp"
14 #include "ust-field-convert.hpp"
15 #include "ust-registry-session.hpp"
16 #include "ust-registry.hpp"
18 #include <common/format.hpp>
19 #include <common/index-allocator.hpp>
20 #include <common/scope-exit.hpp>
21 #include <common/uuid.hpp>
25 #define UST_APP_EVENT_LIST_SIZE 32
27 /* Process name (short). */
28 #define UST_APP_PROCNAME_LEN 16
30 struct lttng_bytecode;
31 struct lttng_ust_filter_bytecode;
33 extern int the_ust_consumerd64_fd, the_ust_consumerd32_fd;
36 * Object used to close the notify socket in a call_rcu(). Since the
37 * application might not be found, we need an independant object containing the
40 struct ust_app_notify_sock_obj {
45 struct ust_app_ht_key {
47 const struct lttng_bytecode *filter;
48 enum lttng_ust_abi_loglevel_type loglevel_type;
50 const struct lttng_event_exclusion *exclusion;
54 * Application registration data structure.
56 struct ust_register_msg {
57 enum lttng_ust_ctl_socket_type type;
66 uint32_t bits_per_long;
67 uint32_t uint8_t_alignment;
68 uint32_t uint16_t_alignment;
69 uint32_t uint32_t_alignment;
70 uint32_t uint64_t_alignment;
71 uint32_t long_alignment;
72 int byte_order; /* BIG_ENDIAN or LITTLE_ENDIAN */
73 char name[LTTNG_UST_ABI_PROCNAME_LEN];
77 * Global applications HT used by the session daemon. This table is indexed by
78 * PID using the pid_n node and pid value of an ust_app.
80 extern struct lttng_ht *ust_app_ht;
83 * Global applications HT used by the session daemon. This table is indexed by
84 * socket using the sock_n node and sock value of an ust_app.
86 * The 'sock' in question here is the 'command' socket.
88 extern struct lttng_ht *ust_app_ht_by_sock;
91 * Global applications HT used by the session daemon. This table is indexed by
92 * socket using the notify_sock_n node and notify_sock value of an ust_app.
94 extern struct lttng_ht *ust_app_ht_by_notify_sock;
96 /* Stream list containing ust_app_stream. */
97 struct ust_app_stream_list {
99 struct cds_list_head head;
104 struct lttng_ust_context_attr ctx;
105 struct lttng_ust_abi_object_data *obj;
106 struct lttng_ht_node_ulong node;
107 struct cds_list_head list;
110 struct ust_app_event {
113 struct lttng_ust_abi_object_data *obj;
114 struct lttng_ust_abi_event attr;
115 char name[LTTNG_UST_ABI_SYM_NAME_LEN];
116 struct lttng_ht_node_str node;
117 struct lttng_bytecode *filter;
118 struct lttng_event_exclusion *exclusion;
121 struct ust_app_event_notifier_rule {
123 uint64_t error_counter_index;
125 struct lttng_ust_abi_object_data *obj;
126 /* Holds a strong reference. */
127 struct lttng_trigger *trigger;
128 /* Unique ID returned by the tracer to identify this event notifier. */
130 struct lttng_ht_node_u64 node;
131 /* The trigger object owns the filter. */
132 const struct lttng_bytecode *filter;
134 struct lttng_event_exclusion *exclusion;
135 /* For delayed reclaim. */
136 struct rcu_head rcu_head;
139 struct ust_app_stream {
141 char pathname[PATH_MAX];
142 /* Format is %s_%d respectively channel name and CPU number. */
143 char name[DEFAULT_STREAM_NAME_LEN];
144 struct lttng_ust_abi_object_data *obj;
145 /* Using a list of streams to keep order. */
146 struct cds_list_head list;
149 struct ust_app_channel {
153 * Unique key used to identify the channel on the consumer side.
154 * 0 is a reserved 'invalid' value used to indicate that the consumer
155 * does not know about this channel (i.e. an error occurred).
158 /* Id of the tracing channel set on creation. */
159 uint64_t tracing_channel_id;
160 /* Number of stream that this channel is expected to receive. */
161 unsigned int expected_stream_count;
162 char name[LTTNG_UST_ABI_SYM_NAME_LEN];
163 struct lttng_ust_abi_object_data *obj;
164 struct lttng_ust_ctl_consumer_channel_attr attr;
165 struct ust_app_stream_list streams;
166 /* Session pointer that owns this object. */
167 struct ust_app_session *session;
169 * Contexts are kept in a hash table for fast lookup and in an ordered list
170 * so we are able to enable them on the tracer side in the same order the
173 struct lttng_ht *ctx;
174 struct cds_list_head ctx_list;
176 struct lttng_ht *events;
177 uint64_t tracefile_size;
178 uint64_t tracefile_count;
179 uint64_t monitor_timer_interval;
181 * Node indexed by channel name in the channels' hash table of a session.
183 struct lttng_ht_node_str node;
185 * Node indexed by UST channel object descriptor (handle). Stored in the
186 * ust_objd hash table in the ust_app object.
188 struct lttng_ht_node_ulong ust_objd_node;
189 /* For delayed reclaim */
190 struct rcu_head rcu_head;
193 struct ust_app_session {
195 static void _session_unlock(ust_app_session *session);
196 static void _const_session_unlock(const ust_app_session *session);
199 using locked_weak_ref = lttng::non_copyable_reference<
201 lttng::memory::create_deleter_class<ust_app_session,
202 ust_app_session::_session_unlock>::deleter>;
203 using const_locked_weak_ref = lttng::non_copyable_reference<
204 const ust_app_session,
205 lttng::memory::create_deleter_class<const ust_app_session,
206 ust_app_session::_const_session_unlock>::deleter>;
208 ust_app_session::const_locked_weak_ref lock() const noexcept;
209 ust_app_session::locked_weak_ref lock() noexcept;
211 bool enabled = false;
212 /* started: has the session been in started state at any time ? */
213 bool started = false; /* allows detection of start vs restart. */
214 int handle = 0; /* used has unique identifier for app session */
216 bool deleted = false; /* Session deleted flag. Check with lock held. */
219 * Tracing session ID. Multiple ust app session can have the same tracing
220 * session id making this value NOT unique to the object.
222 uint64_t tracing_id = 0;
223 uint64_t id = 0; /* Unique session identifier */
224 struct lttng_ht *channels = nullptr; /* Registered channels */
225 struct lttng_ht_node_u64 node = {};
227 * Node indexed by UST session object descriptor (handle). Stored in the
228 * ust_sessions_objd hash table in the ust_app object.
230 struct lttng_ht_node_ulong ust_objd_node = {};
231 /* Starts with 'ust'; no leading slash. */
232 char path[PATH_MAX] = {};
233 /* UID/GID of the application owning the session */
234 struct lttng_credentials real_credentials = {};
235 /* Effective UID and GID. Same as the tracing session. */
236 struct lttng_credentials effective_credentials = {};
237 struct cds_list_head teardown_node = {};
239 * Once at least *one* session is created onto the application, the
240 * corresponding consumer is set so we can use it on unregistration.
242 struct consumer_output *consumer = nullptr;
243 enum lttng_buffer_type buffer_type = LTTNG_BUFFER_PER_PID;
244 /* ABI of the session. Same value as the application. */
245 uint32_t bits_per_long = 0;
246 /* For delayed reclaim */
247 struct rcu_head rcu_head = {};
248 /* If the channel's streams have to be outputed or not. */
249 unsigned int output_traces = 0;
250 unsigned int live_timer_interval = 0; /* usec */
252 /* Metadata channel attributes. */
253 struct lttng_ust_ctl_consumer_channel_attr metadata_attr = {};
255 char root_shm_path[PATH_MAX] = {};
256 char shm_path[PATH_MAX] = {};
260 * Lock protecting this session's ust app interaction. Held
261 * across command send/recv to/from app. Never nests within the
262 * session registry lock.
264 mutable pthread_mutex_t _lock = PTHREAD_MUTEX_INITIALIZER;
268 * Registered traceable applications. Libust registers to the session daemon
269 * and a linked list is kept of all running traceable app.
273 * The lifetime of 'sock' holds a reference to the application; the
274 * application management thread will release a reference to the
275 * application if the application dies.
279 /* Traffic initiated from the session daemon to the application. */
281 pthread_mutex_t sock_lock; /* Protects sock protocol. */
283 /* Traffic initiated from the application to the session daemon. */
287 uid_t uid; /* User ID that owns the apps */
288 gid_t gid; /* Group ID that owns the apps */
291 lttng::sessiond::trace::abi abi;
293 int compatible; /* If the lttng-ust tracer version does not match the
294 supported version of the session daemon, this flag is
295 set to 0 (NOT compatible) else 1. */
296 struct lttng_ust_abi_tracer_version version;
297 uint32_t v_major; /* Version major number */
298 uint32_t v_minor; /* Version minor number */
299 /* Extra for the NULL byte. */
300 char name[UST_APP_PROCNAME_LEN + 1];
302 struct lttng_ht *sessions;
303 struct lttng_ht_node_ulong pid_n;
304 struct lttng_ht_node_ulong sock_n;
305 struct lttng_ht_node_ulong notify_sock_n;
307 * This is a list of ust app session that, once the app is going into
308 * teardown mode, in the RCU call, each node in this list is removed and
311 * Element of the list are added when an application unregisters after each
312 * ht_del of ust_app_session associated to this app. This list is NOT used
313 * when a session is destroyed.
315 struct cds_list_head teardown_head;
317 * Hash table containing ust_app_channel indexed by channel objd.
319 struct lttng_ht *ust_objd;
321 * Hash table containing ust_app_session indexed by objd.
323 struct lttng_ht *ust_sessions_objd;
326 * If this application is of the agent domain and this is non negative then
327 * a lookup MUST be done to acquire a read side reference to the
328 * corresponding agent app object. If the lookup fails, this should be set
329 * to a negative value indicating that the agent application is gone.
333 * Time at which the app is registred.
334 * Used for path creation
336 time_t registration_time;
342 * Handle to the lttng_ust object representing the event
345 struct lttng_ust_abi_object_data *object;
346 struct lttng_pipe *event_pipe;
347 struct lttng_ust_abi_object_data *counter;
348 struct lttng_ust_abi_object_data **counter_cpu;
350 } event_notifier_group;
352 * Hashtable indexing the application's event notifier rule's
353 * (ust_app_event_notifier_rule) by their token's value.
355 struct lttng_ht *token_to_event_notifier_rule_ht;
357 lttng::sessiond::ust::ctl_field_quirks ctl_field_quirks() const;
361 * Due to a bug in g++ < 7.1, this specialization must be enclosed in the fmt namespace,
362 * see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480.
366 struct formatter<ust_app> : formatter<std::string> {
367 template <typename FormatContextType>
368 typename FormatContextType::iterator format(const ust_app& app, FormatContextType& ctx)
372 "{{ procname = `{}`, ppid = {}, pid = {}, uid = {}, gid = {}, version = {}.{}, registration time = {} }}",
380 lttng::utils::time_to_iso8601_str(app.registration_time));
383 } /* namespace fmt */
385 #ifdef HAVE_LIBLTTNG_UST_CTL
387 int ust_app_register(struct ust_register_msg *msg, int sock);
388 int ust_app_register_done(struct ust_app *app);
389 int ust_app_version(struct ust_app *app);
390 void ust_app_unregister_by_socket(int sock);
391 int ust_app_start_trace_all(struct ltt_ust_session *usess);
392 int ust_app_stop_trace_all(struct ltt_ust_session *usess);
393 int ust_app_destroy_trace_all(struct ltt_ust_session *usess);
394 int ust_app_list_events(struct lttng_event **events);
395 int ust_app_list_event_fields(struct lttng_event_field **fields);
396 int ust_app_create_event_glb(struct ltt_ust_session *usess,
397 struct ltt_ust_channel *uchan,
398 struct ltt_ust_event *uevent);
399 int ust_app_disable_channel_glb(struct ltt_ust_session *usess, struct ltt_ust_channel *uchan);
400 int ust_app_enable_channel_glb(struct ltt_ust_session *usess, struct ltt_ust_channel *uchan);
401 int ust_app_enable_event_glb(struct ltt_ust_session *usess,
402 struct ltt_ust_channel *uchan,
403 struct ltt_ust_event *uevent);
404 int ust_app_disable_event_glb(struct ltt_ust_session *usess,
405 struct ltt_ust_channel *uchan,
406 struct ltt_ust_event *uevent);
407 int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess,
408 struct ltt_ust_channel *uchan,
409 struct ltt_ust_context *uctx);
410 void ust_app_global_update(struct ltt_ust_session *usess, struct ust_app *app);
411 void ust_app_global_update_all(struct ltt_ust_session *usess);
412 void ust_app_global_update_event_notifier_rules(struct ust_app *app);
413 void ust_app_global_update_all_event_notifier_rules();
415 void ust_app_clean_list();
416 int ust_app_ht_alloc();
417 struct ust_app *ust_app_find_by_pid(pid_t pid);
418 struct ust_app_stream *ust_app_alloc_stream();
419 int ust_app_recv_registration(int sock, struct ust_register_msg *msg);
420 int ust_app_recv_notify(int sock);
421 void ust_app_add(struct ust_app *app);
422 struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock);
423 void ust_app_notify_sock_unregister(int sock);
424 ssize_t ust_app_push_metadata(const lttng::sessiond::ust::registry_session::locked_ref& registry,
425 struct consumer_socket *socket,
427 enum lttng_error_code ust_app_snapshot_record(const struct ltt_ust_session *usess,
428 const struct consumer_output *output,
429 uint64_t nb_packets_per_stream);
430 uint64_t ust_app_get_size_one_more_packet_per_stream(const struct ltt_ust_session *usess,
431 uint64_t cur_nr_packets);
432 struct ust_app *ust_app_find_by_sock(int sock);
433 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id,
434 struct cds_list_head *buffer_reg_uid_list,
435 struct consumer_output *consumer,
440 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session *usess,
441 struct ltt_ust_channel *uchan,
442 struct consumer_output *consumer,
446 int ust_app_regenerate_statedump_all(struct ltt_ust_session *usess);
447 enum lttng_error_code ust_app_rotate_session(const ltt_session::locked_ref& session);
448 enum lttng_error_code ust_app_create_channel_subdirectories(const struct ltt_ust_session *session);
449 int ust_app_release_object(struct ust_app *app, struct lttng_ust_abi_object_data *data);
450 enum lttng_error_code ust_app_clear_session(const ltt_session::locked_ref& session);
451 enum lttng_error_code ust_app_open_packets(const ltt_session::locked_ref& session);
453 int ust_app_setup_event_notifier_group(struct ust_app *app);
455 static inline int ust_app_supported()
460 bool ust_app_supports_notifiers(const struct ust_app *app);
461 bool ust_app_supports_counters(const struct ust_app *app);
463 bool ust_app_get(ust_app& app);
464 void ust_app_put(ust_app *app);
466 using ust_app_reference =
467 std::unique_ptr<ust_app, lttng::memory::create_deleter_class<ust_app, ust_app_put>::deleter>;
469 #else /* HAVE_LIBLTTNG_UST_CTL */
471 static inline int ust_app_destroy_trace_all(struct ltt_ust_session *usess __attribute__((unused)))
476 static inline int ust_app_start_trace(struct ltt_ust_session *usess __attribute__((unused)),
477 struct ust_app *app __attribute__((unused)))
482 static inline int ust_app_start_trace_all(struct ltt_ust_session *usess __attribute__((unused)))
487 static inline int ust_app_stop_trace_all(struct ltt_ust_session *usess __attribute__((unused)))
492 static inline int ust_app_list_events(struct lttng_event **events __attribute__((unused)))
497 static inline int ust_app_list_event_fields(struct lttng_event_field **fields
498 __attribute__((unused)))
503 static inline int ust_app_register(struct ust_register_msg *msg __attribute__((unused)),
504 int sock __attribute__((unused)))
509 static inline int ust_app_register_done(struct ust_app *app __attribute__((unused)))
514 static inline int ust_app_version(struct ust_app *app __attribute__((unused)))
519 static inline void ust_app_unregister_by_socket(int sock __attribute__((unused)))
523 static inline void ust_app_clean_list(void)
527 static inline struct ust_app_list *ust_app_get_list(void)
532 static inline struct ust_app *ust_app_get_by_pid(pid_t pid __attribute__((unused)))
537 static inline int ust_app_ht_alloc(void)
542 static inline void ust_app_global_update(struct ltt_ust_session *usess __attribute__((unused)),
543 struct ust_app *app __attribute__((unused)))
547 static inline void ust_app_global_update_event_notifier_rules(struct ust_app *app
548 __attribute__((unused)))
552 static inline void ust_app_global_update_all_event_notifier_rules(void)
556 static inline int ust_app_setup_event_notifier_group(struct ust_app *app __attribute__((unused)))
561 static inline int ust_app_disable_channel_glb(struct ltt_ust_session *usess __attribute__((unused)),
562 struct ltt_ust_channel *uchan __attribute__((unused)))
567 static inline int ust_app_enable_channel_glb(struct ltt_ust_session *usess __attribute__((unused)),
568 struct ltt_ust_channel *uchan __attribute__((unused)))
573 static inline int ust_app_create_event_glb(struct ltt_ust_session *usess __attribute__((unused)),
574 struct ltt_ust_channel *uchan __attribute__((unused)),
575 struct ltt_ust_event *uevent __attribute__((unused)))
580 static inline int ust_app_disable_event_glb(struct ltt_ust_session *usess __attribute__((unused)),
581 struct ltt_ust_channel *uchan __attribute__((unused)),
582 struct ltt_ust_event *uevent __attribute__((unused)))
587 static inline int ust_app_enable_event_glb(struct ltt_ust_session *usess __attribute__((unused)),
588 struct ltt_ust_channel *uchan __attribute__((unused)),
589 struct ltt_ust_event *uevent __attribute__((unused)))
594 static inline int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess __attribute__((unused)),
595 struct ltt_ust_channel *uchan __attribute__((unused)),
596 struct ltt_ust_context *uctx __attribute__((unused)))
601 static inline int ust_app_enable_event_pid(struct ltt_ust_session *usess __attribute__((unused)),
602 struct ltt_ust_channel *uchan __attribute__((unused)),
603 struct ltt_ust_event *uevent __attribute__((unused)),
604 pid_t pid __attribute__((unused)))
609 static inline int ust_app_recv_registration(int sock __attribute__((unused)),
610 struct ust_register_msg *msg __attribute__((unused)))
615 static inline int ust_app_recv_notify(int sock __attribute__((unused)))
620 static inline struct ust_app *ust_app_create(struct ust_register_msg *msg __attribute__((unused)),
621 int sock __attribute__((unused)))
626 static inline void ust_app_add(struct ust_app *app __attribute__((unused)))
630 static inline void ust_app_notify_sock_unregister(int sock __attribute__((unused)))
634 static inline ssize_t ust_app_push_metadata(lttng::sessiond::ust::registry_session *registry
635 __attribute__((unused)),
636 struct consumer_socket *socket __attribute__((unused)),
637 int send_zero_data __attribute__((unused)))
642 static inline enum lttng_error_code
643 ust_app_snapshot_record(struct ltt_ust_session *usess __attribute__((unused)),
644 const struct consumer_output *output __attribute__((unused)),
645 uint64_t max_stream_size __attribute__((unused)))
647 return LTTNG_ERR_UNK;
650 static inline unsigned int ust_app_get_nb_stream(struct ltt_ust_session *usess
651 __attribute__((unused)))
656 static inline void ust_app_update_event_notifier_error_count(struct lttng_trigger *lttng_trigger
657 __attribute__((unused)))
662 static inline int ust_app_supported(void)
667 static inline bool ust_app_supports_notifiers(const struct ust_app *app __attribute__((unused)))
672 static inline bool ust_app_supports_counters(const struct ust_app *app __attribute__((unused)))
677 static inline struct ust_app *ust_app_find_by_sock(int sock __attribute__((unused)))
682 static inline struct ust_app *ust_app_find_by_pid(pid_t pid __attribute__((unused)))
687 static inline uint64_t
688 ust_app_get_size_one_more_packet_per_stream(const struct ltt_ust_session *usess
689 __attribute__((unused)),
690 uint64_t cur_nr_packets __attribute__((unused)))
695 static inline int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
696 __attribute__((unused)),
697 struct cds_list_head *buffer_reg_uid_list
698 __attribute__((unused)),
699 struct consumer_output *consumer
700 __attribute__((unused)),
701 int overwrite __attribute__((unused)),
702 uint64_t uchan_id __attribute__((unused)),
703 uint64_t *discarded __attribute__((unused)),
704 uint64_t *lost __attribute__((unused)))
710 ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session *usess __attribute__((unused)),
711 struct ltt_ust_channel *uchan __attribute__((unused)),
712 struct consumer_output *consumer __attribute__((unused)),
713 int overwrite __attribute__((unused)),
714 uint64_t *discarded __attribute__((unused)),
715 uint64_t *lost __attribute__((unused)))
720 static inline int ust_app_regenerate_statedump_all(struct ltt_ust_session *usess
721 __attribute__((unused)))
726 static inline enum lttng_error_code ust_app_rotate_session(const ltt_session::locked_ref& session
727 __attribute__((unused)))
729 return LTTNG_ERR_UNK;
732 static inline enum lttng_error_code
733 ust_app_create_channel_subdirectories(const struct ltt_ust_session *session __attribute__((unused)))
735 return LTTNG_ERR_UNK;
738 static inline int ust_app_release_object(struct ust_app *app __attribute__((unused)),
739 struct lttng_ust_abi_object_data *data
740 __attribute__((unused)))
745 static inline enum lttng_error_code ust_app_clear_session(const ltt_session::locked_ref& session
746 __attribute__((unused)))
748 return LTTNG_ERR_UNK;
751 static inline enum lttng_error_code ust_app_open_packets(const ltt_session::locked_ref& session
752 __attribute__((unused)))
754 return LTTNG_ERR_UNK;
757 static inline void ust_app_get(ust_app& app __attribute__((unused)))
761 static inline void ust_app_put(ust_app *app __attribute__((unused)))
765 #endif /* HAVE_LIBLTTNG_UST_CTL */
767 #endif /* _LTT_UST_APP_H */