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/uuid.hpp>
24 #define UST_APP_EVENT_LIST_SIZE 32
26 /* Process name (short). */
27 #define UST_APP_PROCNAME_LEN 16
29 struct lttng_bytecode;
30 struct lttng_ust_filter_bytecode;
32 extern int the_ust_consumerd64_fd, the_ust_consumerd32_fd;
35 * Object used to close the notify socket in a call_rcu(). Since the
36 * application might not be found, we need an independant object containing the
39 struct ust_app_notify_sock_obj {
44 struct ust_app_ht_key {
46 const struct lttng_bytecode *filter;
47 enum lttng_ust_abi_loglevel_type loglevel_type;
49 const struct lttng_event_exclusion *exclusion;
53 * Application registration data structure.
55 struct ust_register_msg {
56 enum lttng_ust_ctl_socket_type type;
65 uint32_t bits_per_long;
66 uint32_t uint8_t_alignment;
67 uint32_t uint16_t_alignment;
68 uint32_t uint32_t_alignment;
69 uint32_t uint64_t_alignment;
70 uint32_t long_alignment;
71 int byte_order; /* BIG_ENDIAN or LITTLE_ENDIAN */
72 char name[LTTNG_UST_ABI_PROCNAME_LEN];
76 * Global applications HT used by the session daemon. This table is indexed by
77 * PID using the pid_n node and pid value of an ust_app.
79 extern struct lttng_ht *ust_app_ht;
82 * Global applications HT used by the session daemon. This table is indexed by
83 * socket using the sock_n node and sock value of an ust_app.
85 * The 'sock' in question here is the 'command' socket.
87 extern struct lttng_ht *ust_app_ht_by_sock;
90 * Global applications HT used by the session daemon. This table is indexed by
91 * socket using the notify_sock_n node and notify_sock value of an ust_app.
93 extern struct lttng_ht *ust_app_ht_by_notify_sock;
95 /* Stream list containing ust_app_stream. */
96 struct ust_app_stream_list {
98 struct cds_list_head head;
103 struct lttng_ust_context_attr ctx;
104 struct lttng_ust_abi_object_data *obj;
105 struct lttng_ht_node_ulong node;
106 struct cds_list_head list;
109 struct ust_app_event {
112 struct lttng_ust_abi_object_data *obj;
113 struct lttng_ust_abi_event attr;
114 char name[LTTNG_UST_ABI_SYM_NAME_LEN];
115 struct lttng_ht_node_str node;
116 struct lttng_bytecode *filter;
117 struct lttng_event_exclusion *exclusion;
120 struct ust_app_event_notifier_rule {
122 uint64_t error_counter_index;
124 struct lttng_ust_abi_object_data *obj;
125 /* Holds a strong reference. */
126 struct lttng_trigger *trigger;
127 /* Unique ID returned by the tracer to identify this event notifier. */
129 struct lttng_ht_node_u64 node;
130 /* The trigger object owns the filter. */
131 const struct lttng_bytecode *filter;
133 struct lttng_event_exclusion *exclusion;
134 /* For delayed reclaim. */
135 struct rcu_head rcu_head;
138 struct ust_app_stream {
140 char pathname[PATH_MAX];
141 /* Format is %s_%d respectively channel name and CPU number. */
142 char name[DEFAULT_STREAM_NAME_LEN];
143 struct lttng_ust_abi_object_data *obj;
144 /* Using a list of streams to keep order. */
145 struct cds_list_head list;
148 struct ust_app_channel {
152 * Unique key used to identify the channel on the consumer side.
153 * 0 is a reserved 'invalid' value used to indicate that the consumer
154 * does not know about this channel (i.e. an error occurred).
157 /* Id of the tracing channel set on creation. */
158 uint64_t tracing_channel_id;
159 /* Number of stream that this channel is expected to receive. */
160 unsigned int expected_stream_count;
161 char name[LTTNG_UST_ABI_SYM_NAME_LEN];
162 struct lttng_ust_abi_object_data *obj;
163 struct lttng_ust_ctl_consumer_channel_attr attr;
164 struct ust_app_stream_list streams;
165 /* Session pointer that owns this object. */
166 struct ust_app_session *session;
168 * Contexts are kept in a hash table for fast lookup and in an ordered list
169 * so we are able to enable them on the tracer side in the same order the
172 struct lttng_ht *ctx;
173 struct cds_list_head ctx_list;
175 struct lttng_ht *events;
176 uint64_t tracefile_size;
177 uint64_t tracefile_count;
178 uint64_t monitor_timer_interval;
180 * Node indexed by channel name in the channels' hash table of a session.
182 struct lttng_ht_node_str node;
184 * Node indexed by UST channel object descriptor (handle). Stored in the
185 * ust_objd hash table in the ust_app object.
187 struct lttng_ht_node_ulong ust_objd_node;
188 /* For delayed reclaim */
189 struct rcu_head rcu_head;
192 struct ust_app_session {
194 * Lock protecting this session's ust app interaction. Held
195 * across command send/recv to/from app. Never nests within the
196 * session registry lock.
198 pthread_mutex_t lock;
201 /* started: has the session been in started state at any time ? */
202 bool started; /* allows detection of start vs restart. */
203 int handle; /* used has unique identifier for app session */
205 bool deleted; /* Session deleted flag. Check with lock held. */
208 * Tracing session ID. Multiple ust app session can have the same tracing
209 * session id making this value NOT unique to the object.
212 uint64_t id; /* Unique session identifier */
213 struct lttng_ht *channels; /* Registered channels */
214 struct lttng_ht_node_u64 node;
216 * Node indexed by UST session object descriptor (handle). Stored in the
217 * ust_sessions_objd hash table in the ust_app object.
219 struct lttng_ht_node_ulong ust_objd_node;
220 /* Starts with 'ust'; no leading slash. */
222 /* UID/GID of the application owning the session */
223 struct lttng_credentials real_credentials;
224 /* Effective UID and GID. Same as the tracing session. */
225 struct lttng_credentials effective_credentials;
226 struct cds_list_head teardown_node;
228 * Once at least *one* session is created onto the application, the
229 * corresponding consumer is set so we can use it on unregistration.
231 struct consumer_output *consumer;
232 enum lttng_buffer_type buffer_type;
233 /* ABI of the session. Same value as the application. */
234 uint32_t bits_per_long;
235 /* For delayed reclaim */
236 struct rcu_head rcu_head;
237 /* If the channel's streams have to be outputed or not. */
238 unsigned int output_traces;
239 unsigned int live_timer_interval; /* usec */
241 /* Metadata channel attributes. */
242 struct lttng_ust_ctl_consumer_channel_attr metadata_attr;
244 char root_shm_path[PATH_MAX];
245 char shm_path[PATH_MAX];
249 * Registered traceable applications. Libust registers to the session daemon
250 * and a linked list is kept of all running traceable app.
254 * The lifetime of 'sock' holds a reference to the application; the
255 * application management thread will release a reference to the
256 * application if the application dies.
260 /* Traffic initiated from the session daemon to the application. */
262 pthread_mutex_t sock_lock; /* Protects sock protocol. */
264 /* Traffic initiated from the application to the session daemon. */
268 uid_t uid; /* User ID that owns the apps */
269 gid_t gid; /* Group ID that owns the apps */
272 lttng::sessiond::trace::abi abi;
274 int compatible; /* If the lttng-ust tracer version does not match the
275 supported version of the session daemon, this flag is
276 set to 0 (NOT compatible) else 1. */
277 struct lttng_ust_abi_tracer_version version;
278 uint32_t v_major; /* Version major number */
279 uint32_t v_minor; /* Version minor number */
280 /* Extra for the NULL byte. */
281 char name[UST_APP_PROCNAME_LEN + 1];
282 /* Type of buffer this application uses. */
283 enum lttng_buffer_type buffer_type;
284 struct lttng_ht *sessions;
285 struct lttng_ht_node_ulong pid_n;
286 struct lttng_ht_node_ulong sock_n;
287 struct lttng_ht_node_ulong notify_sock_n;
289 * This is a list of ust app session that, once the app is going into
290 * teardown mode, in the RCU call, each node in this list is removed and
293 * Element of the list are added when an application unregisters after each
294 * ht_del of ust_app_session associated to this app. This list is NOT used
295 * when a session is destroyed.
297 struct cds_list_head teardown_head;
299 * Hash table containing ust_app_channel indexed by channel objd.
301 struct lttng_ht *ust_objd;
303 * Hash table containing ust_app_session indexed by objd.
305 struct lttng_ht *ust_sessions_objd;
308 * If this application is of the agent domain and this is non negative then
309 * a lookup MUST be done to acquire a read side reference to the
310 * corresponding agent app object. If the lookup fails, this should be set
311 * to a negative value indicating that the agent application is gone.
315 * Time at which the app is registred.
316 * Used for path creation
318 time_t registration_time;
324 * Handle to the lttng_ust object representing the event
327 struct lttng_ust_abi_object_data *object;
328 struct lttng_pipe *event_pipe;
329 struct lttng_ust_abi_object_data *counter;
330 struct lttng_ust_abi_object_data **counter_cpu;
332 } event_notifier_group;
334 * Hashtable indexing the application's event notifier rule's
335 * (ust_app_event_notifier_rule) by their token's value.
337 struct lttng_ht *token_to_event_notifier_rule_ht;
339 lttng::sessiond::ust::ctl_field_quirks ctl_field_quirks() const;
343 * Due to a bug in g++ < 7.1, this specialization must be enclosed in the fmt namespace,
344 * see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480.
348 struct formatter<ust_app> : formatter<std::string> {
349 template <typename FormatContextType>
350 typename FormatContextType::iterator format(const ust_app& app, FormatContextType& ctx)
354 "{{ procname = `{}`, ppid = {}, pid = {}, uid = {}, gid = {}, version = {}.{}, registration time = {} }}",
362 lttng::utils::time_to_iso8601_str(app.registration_time));
365 } /* namespace fmt */
367 #ifdef HAVE_LIBLTTNG_UST_CTL
369 int ust_app_register(struct ust_register_msg *msg, int sock);
370 int ust_app_register_done(struct ust_app *app);
371 int ust_app_version(struct ust_app *app);
372 void ust_app_unregister_by_socket(int sock);
373 int ust_app_start_trace_all(struct ltt_ust_session *usess);
374 int ust_app_stop_trace_all(struct ltt_ust_session *usess);
375 int ust_app_destroy_trace_all(struct ltt_ust_session *usess);
376 int ust_app_list_events(struct lttng_event **events);
377 int ust_app_list_event_fields(struct lttng_event_field **fields);
378 int ust_app_create_event_glb(struct ltt_ust_session *usess,
379 struct ltt_ust_channel *uchan,
380 struct ltt_ust_event *uevent);
381 int ust_app_disable_channel_glb(struct ltt_ust_session *usess, struct ltt_ust_channel *uchan);
382 int ust_app_enable_channel_glb(struct ltt_ust_session *usess, struct ltt_ust_channel *uchan);
383 int ust_app_enable_event_glb(struct ltt_ust_session *usess,
384 struct ltt_ust_channel *uchan,
385 struct ltt_ust_event *uevent);
386 int ust_app_disable_event_glb(struct ltt_ust_session *usess,
387 struct ltt_ust_channel *uchan,
388 struct ltt_ust_event *uevent);
389 int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess,
390 struct ltt_ust_channel *uchan,
391 struct ltt_ust_context *uctx);
392 void ust_app_global_update(struct ltt_ust_session *usess, struct ust_app *app);
393 void ust_app_global_update_all(struct ltt_ust_session *usess);
394 void ust_app_global_update_event_notifier_rules(struct ust_app *app);
395 void ust_app_global_update_all_event_notifier_rules();
397 void ust_app_clean_list();
398 int ust_app_ht_alloc();
399 struct ust_app *ust_app_find_by_pid(pid_t pid);
400 struct ust_app_stream *ust_app_alloc_stream();
401 int ust_app_recv_registration(int sock, struct ust_register_msg *msg);
402 int ust_app_recv_notify(int sock);
403 void ust_app_add(struct ust_app *app);
404 struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock);
405 void ust_app_notify_sock_unregister(int sock);
406 ssize_t ust_app_push_metadata(const lttng::sessiond::ust::registry_session::locked_ref& registry,
407 struct consumer_socket *socket,
409 enum lttng_error_code ust_app_snapshot_record(const struct ltt_ust_session *usess,
410 const struct consumer_output *output,
411 uint64_t nb_packets_per_stream);
412 uint64_t ust_app_get_size_one_more_packet_per_stream(const struct ltt_ust_session *usess,
413 uint64_t cur_nr_packets);
414 struct ust_app *ust_app_find_by_sock(int sock);
415 int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id,
416 struct cds_list_head *buffer_reg_uid_list,
417 struct consumer_output *consumer,
422 int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session *usess,
423 struct ltt_ust_channel *uchan,
424 struct consumer_output *consumer,
428 int ust_app_regenerate_statedump_all(struct ltt_ust_session *usess);
429 enum lttng_error_code ust_app_rotate_session(const ltt_session::locked_ref& session);
430 enum lttng_error_code ust_app_create_channel_subdirectories(const struct ltt_ust_session *session);
431 int ust_app_release_object(struct ust_app *app, struct lttng_ust_abi_object_data *data);
432 enum lttng_error_code ust_app_clear_session(const ltt_session::locked_ref& session);
433 enum lttng_error_code ust_app_open_packets(const ltt_session::locked_ref& session);
435 int ust_app_setup_event_notifier_group(struct ust_app *app);
437 static inline int ust_app_supported()
442 bool ust_app_supports_notifiers(const struct ust_app *app);
443 bool ust_app_supports_counters(const struct ust_app *app);
445 bool ust_app_get(ust_app& app);
446 void ust_app_put(ust_app *app);
448 using ust_app_reference =
449 std::unique_ptr<ust_app, lttng::memory::create_deleter_class<ust_app, ust_app_put>::deleter>;
451 #else /* HAVE_LIBLTTNG_UST_CTL */
453 static inline int ust_app_destroy_trace_all(struct ltt_ust_session *usess __attribute__((unused)))
458 static inline int ust_app_start_trace(struct ltt_ust_session *usess __attribute__((unused)),
459 struct ust_app *app __attribute__((unused)))
464 static inline int ust_app_start_trace_all(struct ltt_ust_session *usess __attribute__((unused)))
469 static inline int ust_app_stop_trace_all(struct ltt_ust_session *usess __attribute__((unused)))
474 static inline int ust_app_list_events(struct lttng_event **events __attribute__((unused)))
479 static inline int ust_app_list_event_fields(struct lttng_event_field **fields
480 __attribute__((unused)))
485 static inline int ust_app_register(struct ust_register_msg *msg __attribute__((unused)),
486 int sock __attribute__((unused)))
491 static inline int ust_app_register_done(struct ust_app *app __attribute__((unused)))
496 static inline int ust_app_version(struct ust_app *app __attribute__((unused)))
501 static inline void ust_app_unregister_by_socket(int sock __attribute__((unused)))
505 static inline void ust_app_clean_list(void)
509 static inline struct ust_app_list *ust_app_get_list(void)
514 static inline struct ust_app *ust_app_get_by_pid(pid_t pid __attribute__((unused)))
519 static inline int ust_app_ht_alloc(void)
524 static inline void ust_app_global_update(struct ltt_ust_session *usess __attribute__((unused)),
525 struct ust_app *app __attribute__((unused)))
529 static inline void ust_app_global_update_event_notifier_rules(struct ust_app *app
530 __attribute__((unused)))
534 static inline void ust_app_global_update_all_event_notifier_rules(void)
538 static inline int ust_app_setup_event_notifier_group(struct ust_app *app __attribute__((unused)))
543 static inline int ust_app_disable_channel_glb(struct ltt_ust_session *usess __attribute__((unused)),
544 struct ltt_ust_channel *uchan __attribute__((unused)))
549 static inline int ust_app_enable_channel_glb(struct ltt_ust_session *usess __attribute__((unused)),
550 struct ltt_ust_channel *uchan __attribute__((unused)))
555 static inline int ust_app_create_event_glb(struct ltt_ust_session *usess __attribute__((unused)),
556 struct ltt_ust_channel *uchan __attribute__((unused)),
557 struct ltt_ust_event *uevent __attribute__((unused)))
562 static inline int ust_app_disable_event_glb(struct ltt_ust_session *usess __attribute__((unused)),
563 struct ltt_ust_channel *uchan __attribute__((unused)),
564 struct ltt_ust_event *uevent __attribute__((unused)))
569 static inline int ust_app_enable_event_glb(struct ltt_ust_session *usess __attribute__((unused)),
570 struct ltt_ust_channel *uchan __attribute__((unused)),
571 struct ltt_ust_event *uevent __attribute__((unused)))
576 static inline int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess __attribute__((unused)),
577 struct ltt_ust_channel *uchan __attribute__((unused)),
578 struct ltt_ust_context *uctx __attribute__((unused)))
583 static inline int ust_app_enable_event_pid(struct ltt_ust_session *usess __attribute__((unused)),
584 struct ltt_ust_channel *uchan __attribute__((unused)),
585 struct ltt_ust_event *uevent __attribute__((unused)),
586 pid_t pid __attribute__((unused)))
591 static inline int ust_app_recv_registration(int sock __attribute__((unused)),
592 struct ust_register_msg *msg __attribute__((unused)))
597 static inline int ust_app_recv_notify(int sock __attribute__((unused)))
602 static inline struct ust_app *ust_app_create(struct ust_register_msg *msg __attribute__((unused)),
603 int sock __attribute__((unused)))
608 static inline void ust_app_add(struct ust_app *app __attribute__((unused)))
612 static inline void ust_app_notify_sock_unregister(int sock __attribute__((unused)))
616 static inline ssize_t ust_app_push_metadata(lttng::sessiond::ust::registry_session *registry
617 __attribute__((unused)),
618 struct consumer_socket *socket __attribute__((unused)),
619 int send_zero_data __attribute__((unused)))
624 static inline enum lttng_error_code
625 ust_app_snapshot_record(struct ltt_ust_session *usess __attribute__((unused)),
626 const struct consumer_output *output __attribute__((unused)),
627 uint64_t max_stream_size __attribute__((unused)))
629 return LTTNG_ERR_UNK;
632 static inline unsigned int ust_app_get_nb_stream(struct ltt_ust_session *usess
633 __attribute__((unused)))
638 static inline void ust_app_update_event_notifier_error_count(struct lttng_trigger *lttng_trigger
639 __attribute__((unused)))
644 static inline int ust_app_supported(void)
649 static inline bool ust_app_supports_notifiers(const struct ust_app *app __attribute__((unused)))
654 static inline bool ust_app_supports_counters(const struct ust_app *app __attribute__((unused)))
659 static inline struct ust_app *ust_app_find_by_sock(int sock __attribute__((unused)))
664 static inline struct ust_app *ust_app_find_by_pid(pid_t pid __attribute__((unused)))
669 static inline uint64_t
670 ust_app_get_size_one_more_packet_per_stream(const struct ltt_ust_session *usess
671 __attribute__((unused)),
672 uint64_t cur_nr_packets __attribute__((unused)))
677 static inline int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id
678 __attribute__((unused)),
679 struct cds_list_head *buffer_reg_uid_list
680 __attribute__((unused)),
681 struct consumer_output *consumer
682 __attribute__((unused)),
683 int overwrite __attribute__((unused)),
684 uint64_t uchan_id __attribute__((unused)),
685 uint64_t *discarded __attribute__((unused)),
686 uint64_t *lost __attribute__((unused)))
692 ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session *usess __attribute__((unused)),
693 struct ltt_ust_channel *uchan __attribute__((unused)),
694 struct consumer_output *consumer __attribute__((unused)),
695 int overwrite __attribute__((unused)),
696 uint64_t *discarded __attribute__((unused)),
697 uint64_t *lost __attribute__((unused)))
702 static inline int ust_app_regenerate_statedump_all(struct ltt_ust_session *usess
703 __attribute__((unused)))
708 static inline enum lttng_error_code ust_app_rotate_session(const ltt_session::locked_ref& session
709 __attribute__((unused)))
711 return LTTNG_ERR_UNK;
714 static inline enum lttng_error_code
715 ust_app_create_channel_subdirectories(const struct ltt_ust_session *session __attribute__((unused)))
717 return LTTNG_ERR_UNK;
720 static inline int ust_app_release_object(struct ust_app *app __attribute__((unused)),
721 struct lttng_ust_abi_object_data *data
722 __attribute__((unused)))
727 static inline enum lttng_error_code ust_app_clear_session(const ltt_session::locked_ref& session
728 __attribute__((unused)))
730 return LTTNG_ERR_UNK;
733 static inline enum lttng_error_code ust_app_open_packets(const ltt_session::locked_ref& session
734 __attribute__((unused)))
736 return LTTNG_ERR_UNK;
739 static inline void ust_app_get(ust_app& app __attribute__((unused)))
743 static inline void ust_app_put(ust_app *app __attribute__((unused)))
747 #endif /* HAVE_LIBLTTNG_UST_CTL */
749 #endif /* _LTT_UST_APP_H */