4 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
5 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; only
10 * version 2.1 of the License.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include <sys/types.h>
25 #include <sys/socket.h>
28 #include <sys/types.h>
35 #include <semaphore.h>
40 #include <urcu/uatomic.h>
41 #include <urcu/futex.h>
42 #include <urcu/compiler.h>
44 #include <lttng/ust-events.h>
45 #include <lttng/ust-abi.h>
46 #include <lttng/ust.h>
47 #include <lttng/ust-error.h>
48 #include <lttng/ust-ctl.h>
49 #include <urcu/tls-compat.h>
52 #include <usterr-signal-safe.h>
54 #include "tracepoint-internal.h"
55 #include "lttng-tracer-core.h"
57 #include "../libringbuffer/rb-init.h"
58 #include "lttng-ust-statedump.h"
60 #include "../libringbuffer/getcpu.h"
63 /* Concatenate lttng ust shared library name with its major version number. */
64 #define LTTNG_UST_LIB_SO_NAME "liblttng-ust.so." __ust_stringify(CONFIG_LTTNG_UST_LIBRARY_VERSION_MAJOR)
67 * Has lttng ust comm constructor been called ?
69 static int initialized
;
72 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
73 * Held when handling a command, also held by fork() to deal with
74 * removal of threads, and by exit path.
76 * The UST lock is the centralized mutex across UST tracing control and
79 * ust_exit_mutex must never nest in ust_mutex.
81 * ust_fork_mutex must never nest in ust_mutex.
83 * ust_mutex_nest is a per-thread nesting counter, allowing the perf
84 * counter lazy initialization called by events within the statedump,
85 * which traces while the ust_mutex is held.
87 * ust_lock nests within the dynamic loader lock (within glibc) because
88 * it is taken within the library constructor.
90 static pthread_mutex_t ust_mutex
= PTHREAD_MUTEX_INITIALIZER
;
92 /* Allow nesting the ust_mutex within the same thread. */
93 static DEFINE_URCU_TLS(int, ust_mutex_nest
);
96 * ust_exit_mutex protects thread_active variable wrt thread exit. It
97 * cannot be done by ust_mutex because pthread_cancel(), which takes an
98 * internal libc lock, cannot nest within ust_mutex.
100 * It never nests within a ust_mutex.
102 static pthread_mutex_t ust_exit_mutex
= PTHREAD_MUTEX_INITIALIZER
;
105 * ust_fork_mutex protects base address statedump tracing against forks. It
106 * prevents the dynamic loader lock to be taken (by base address statedump
107 * tracing) while a fork is happening, thus preventing deadlock issues with
108 * the dynamic loader lock.
110 static pthread_mutex_t ust_fork_mutex
= PTHREAD_MUTEX_INITIALIZER
;
112 /* Should the ust comm thread quit ? */
113 static int lttng_ust_comm_should_quit
;
116 * This variable can be tested by applications to check whether
117 * lttng-ust is loaded. They simply have to define their own
118 * "lttng_ust_loaded" weak symbol, and test it. It is set to 1 by the
119 * library constructor.
121 int lttng_ust_loaded
__attribute__((weak
));
124 * Return 0 on success, -1 if should quit.
125 * The lock is taken in both cases.
130 sigset_t sig_all_blocked
, orig_mask
;
133 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
135 ERR("pthread_setcancelstate: %s", strerror(ret
));
137 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
138 ERR("pthread_setcancelstate: unexpected oldstate");
140 sigfillset(&sig_all_blocked
);
141 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
143 ERR("pthread_sigmask: %s", strerror(ret
));
145 if (!URCU_TLS(ust_mutex_nest
)++)
146 pthread_mutex_lock(&ust_mutex
);
147 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
149 ERR("pthread_sigmask: %s", strerror(ret
));
151 if (lttng_ust_comm_should_quit
) {
159 * ust_lock_nocheck() can be used in constructors/destructors, because
160 * they are already nested within the dynamic loader lock, and therefore
161 * have exclusive access against execution of liblttng-ust destructor.
164 void ust_lock_nocheck(void)
166 sigset_t sig_all_blocked
, orig_mask
;
169 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
171 ERR("pthread_setcancelstate: %s", strerror(ret
));
173 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
174 ERR("pthread_setcancelstate: unexpected oldstate");
176 sigfillset(&sig_all_blocked
);
177 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
179 ERR("pthread_sigmask: %s", strerror(ret
));
181 if (!URCU_TLS(ust_mutex_nest
)++)
182 pthread_mutex_lock(&ust_mutex
);
183 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
185 ERR("pthread_sigmask: %s", strerror(ret
));
192 void ust_unlock(void)
194 sigset_t sig_all_blocked
, orig_mask
;
197 sigfillset(&sig_all_blocked
);
198 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
200 ERR("pthread_sigmask: %s", strerror(ret
));
202 if (!--URCU_TLS(ust_mutex_nest
))
203 pthread_mutex_unlock(&ust_mutex
);
204 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
206 ERR("pthread_sigmask: %s", strerror(ret
));
208 ret
= pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
210 ERR("pthread_setcancelstate: %s", strerror(ret
));
212 if (oldstate
!= PTHREAD_CANCEL_DISABLE
) {
213 ERR("pthread_setcancelstate: unexpected oldstate");
218 * Wait for either of these before continuing to the main
220 * - the register_done message from sessiond daemon
221 * (will let the sessiond daemon enable sessions before main
223 * - sessiond daemon is not reachable.
224 * - timeout (ensuring applications are resilient to session
227 static sem_t constructor_wait
;
229 * Doing this for both the global and local sessiond.
231 static int sem_count
= { 2 };
234 * Counting nesting within lttng-ust. Used to ensure that calling fork()
235 * from liblttng-ust does not execute the pre/post fork handlers.
237 static DEFINE_URCU_TLS(int, lttng_ust_nest_count
);
240 * Info about socket and associated listener thread.
244 pthread_t ust_listener
; /* listener thread */
246 int constructor_sem_posted
;
251 char sock_path
[PATH_MAX
];
255 char wait_shm_path
[PATH_MAX
];
257 /* Keep track of lazy state dump not performed yet. */
258 int statedump_pending
;
261 /* Socket from app (connect) to session daemon (listen) for communication */
262 struct sock_info global_apps
= {
270 .sock_path
= LTTNG_DEFAULT_RUNDIR
"/" LTTNG_UST_SOCK_FILENAME
,
274 .wait_shm_path
= "/" LTTNG_UST_WAIT_FILENAME
,
276 .statedump_pending
= 0,
279 /* TODO: allow global_apps_sock_path override */
281 struct sock_info local_apps
= {
285 .allowed
= 0, /* Check setuid bit first */
291 .statedump_pending
= 0,
294 static int wait_poll_fallback
;
296 static const char *cmd_name_mapping
[] = {
297 [ LTTNG_UST_RELEASE
] = "Release",
298 [ LTTNG_UST_SESSION
] = "Create Session",
299 [ LTTNG_UST_TRACER_VERSION
] = "Get Tracer Version",
301 [ LTTNG_UST_TRACEPOINT_LIST
] = "Create Tracepoint List",
302 [ LTTNG_UST_WAIT_QUIESCENT
] = "Wait for Quiescent State",
303 [ LTTNG_UST_REGISTER_DONE
] = "Registration Done",
304 [ LTTNG_UST_TRACEPOINT_FIELD_LIST
] = "Create Tracepoint Field List",
306 /* Session FD commands */
307 [ LTTNG_UST_CHANNEL
] = "Create Channel",
308 [ LTTNG_UST_SESSION_START
] = "Start Session",
309 [ LTTNG_UST_SESSION_STOP
] = "Stop Session",
311 /* Channel FD commands */
312 [ LTTNG_UST_STREAM
] = "Create Stream",
313 [ LTTNG_UST_EVENT
] = "Create Event",
315 /* Event and Channel FD commands */
316 [ LTTNG_UST_CONTEXT
] = "Create Context",
317 [ LTTNG_UST_FLUSH_BUFFER
] = "Flush Buffer",
319 /* Event, Channel and Session commands */
320 [ LTTNG_UST_ENABLE
] = "Enable",
321 [ LTTNG_UST_DISABLE
] = "Disable",
323 /* Tracepoint list commands */
324 [ LTTNG_UST_TRACEPOINT_LIST_GET
] = "List Next Tracepoint",
325 [ LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
] = "List Next Tracepoint Field",
327 /* Event FD commands */
328 [ LTTNG_UST_FILTER
] = "Create Filter",
329 [ LTTNG_UST_EXCLUSION
] = "Add exclusions to event",
332 static const char *str_timeout
;
333 static int got_timeout_env
;
335 extern void lttng_ring_buffer_client_overwrite_init(void);
336 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
337 extern void lttng_ring_buffer_client_discard_init(void);
338 extern void lttng_ring_buffer_client_discard_rt_init(void);
339 extern void lttng_ring_buffer_metadata_client_init(void);
340 extern void lttng_ring_buffer_client_overwrite_exit(void);
341 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
342 extern void lttng_ring_buffer_client_discard_exit(void);
343 extern void lttng_ring_buffer_client_discard_rt_exit(void);
344 extern void lttng_ring_buffer_metadata_client_exit(void);
346 static char *get_map_shm(struct sock_info
*sock_info
);
348 ssize_t
lttng_ust_read(int fd
, void *buf
, size_t len
)
351 size_t copied
= 0, to_copy
= len
;
354 ret
= read(fd
, buf
+ copied
, to_copy
);
359 } while ((ret
> 0 && to_copy
> 0)
360 || (ret
< 0 && errno
== EINTR
));
367 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
371 const char *get_lttng_home_dir(void)
375 val
= (const char *) lttng_getenv("LTTNG_HOME");
379 return (const char *) lttng_getenv("HOME");
383 * Force a read (imply TLS fixup for dlopen) of TLS variables.
386 void lttng_fixup_nest_count_tls(void)
388 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count
)));
392 void lttng_fixup_ust_mutex_nest_tls(void)
394 asm volatile ("" : : "m" (URCU_TLS(ust_mutex_nest
)));
401 void lttng_fixup_urcu_bp_tls(void)
407 void lttng_ust_fixup_tls(void)
409 lttng_fixup_urcu_bp_tls();
410 lttng_fixup_ringbuffer_tls();
411 lttng_fixup_vtid_tls();
412 lttng_fixup_nest_count_tls();
413 lttng_fixup_procname_tls();
414 lttng_fixup_ust_mutex_nest_tls();
415 lttng_ust_fixup_fd_tracker_tls();
418 int lttng_get_notify_socket(void *owner
)
420 struct sock_info
*info
= owner
;
422 return info
->notify_socket
;
426 void print_cmd(int cmd
, int handle
)
428 const char *cmd_name
= "Unknown";
430 if (cmd
>= 0 && cmd
< LTTNG_ARRAY_SIZE(cmd_name_mapping
)
431 && cmd_name_mapping
[cmd
]) {
432 cmd_name
= cmd_name_mapping
[cmd
];
434 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
436 lttng_ust_obj_get_name(handle
), handle
);
440 int setup_global_apps(void)
443 assert(!global_apps
.wait_shm_mmap
);
445 global_apps
.wait_shm_mmap
= get_map_shm(&global_apps
);
446 if (!global_apps
.wait_shm_mmap
) {
447 WARN("Unable to get map shm for global apps. Disabling LTTng-UST global tracing.");
448 global_apps
.allowed
= 0;
453 global_apps
.allowed
= 1;
458 int setup_local_apps(void)
461 const char *home_dir
;
464 assert(!local_apps
.wait_shm_mmap
);
468 * Disallow per-user tracing for setuid binaries.
470 if (uid
!= geteuid()) {
471 assert(local_apps
.allowed
== 0);
475 home_dir
= get_lttng_home_dir();
477 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
478 assert(local_apps
.allowed
== 0);
482 local_apps
.allowed
= 1;
483 snprintf(local_apps
.sock_path
, PATH_MAX
, "%s/%s/%s",
485 LTTNG_DEFAULT_HOME_RUNDIR
,
486 LTTNG_UST_SOCK_FILENAME
);
487 snprintf(local_apps
.wait_shm_path
, PATH_MAX
, "/%s-%u",
488 LTTNG_UST_WAIT_FILENAME
,
491 local_apps
.wait_shm_mmap
= get_map_shm(&local_apps
);
492 if (!local_apps
.wait_shm_mmap
) {
493 WARN("Unable to get map shm for local apps. Disabling LTTng-UST per-user tracing.");
494 local_apps
.allowed
= 0;
503 * Get socket timeout, in ms.
504 * -1: wait forever. 0: don't wait. >0: timeout, in ms.
507 long get_timeout(void)
509 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
511 if (!got_timeout_env
) {
512 str_timeout
= lttng_getenv("LTTNG_UST_REGISTER_TIMEOUT");
516 constructor_delay_ms
= strtol(str_timeout
, NULL
, 10);
517 /* All negative values are considered as "-1". */
518 if (constructor_delay_ms
< -1)
519 constructor_delay_ms
= -1;
520 return constructor_delay_ms
;
523 /* Timeout for notify socket send and recv. */
525 long get_notify_sock_timeout(void)
527 return get_timeout();
530 /* Timeout for connecting to cmd and notify sockets. */
532 long get_connect_sock_timeout(void)
534 return get_timeout();
538 * Return values: -1: wait forever. 0: don't wait. 1: timeout wait.
541 int get_constructor_timeout(struct timespec
*constructor_timeout
)
543 long constructor_delay_ms
;
546 constructor_delay_ms
= get_timeout();
548 switch (constructor_delay_ms
) {
549 case -1:/* fall-through */
551 return constructor_delay_ms
;
557 * If we are unable to find the current time, don't wait.
559 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
564 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
565 constructor_timeout
->tv_nsec
+=
566 (constructor_delay_ms
% 1000UL) * 1000000UL;
567 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
568 constructor_timeout
->tv_sec
++;
569 constructor_timeout
->tv_nsec
-= 1000000000UL;
571 /* Timeout wait (constructor_delay_ms). */
576 void get_allow_blocking(void)
578 const char *str_allow_blocking
=
579 lttng_getenv("LTTNG_UST_ALLOW_BLOCKING");
581 if (str_allow_blocking
) {
582 DBG("%s environment variable is set",
583 "LTTNG_UST_ALLOW_BLOCKING");
584 lttng_ust_ringbuffer_set_allow_blocking();
589 int register_to_sessiond(int socket
, enum ustctl_socket_type type
)
591 return ustcomm_send_reg_msg(socket
,
594 lttng_alignof(uint8_t) * CHAR_BIT
,
595 lttng_alignof(uint16_t) * CHAR_BIT
,
596 lttng_alignof(uint32_t) * CHAR_BIT
,
597 lttng_alignof(uint64_t) * CHAR_BIT
,
598 lttng_alignof(unsigned long) * CHAR_BIT
);
602 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
606 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
609 DBG("message successfully sent");
612 if (len
== -ECONNRESET
) {
613 DBG("remote end closed connection");
618 DBG("incorrect message size: %zd", len
);
624 int handle_register_done(struct sock_info
*sock_info
)
628 if (sock_info
->constructor_sem_posted
)
630 sock_info
->constructor_sem_posted
= 1;
631 if (uatomic_read(&sem_count
) <= 0) {
634 ret
= uatomic_add_return(&sem_count
, -1);
636 ret
= sem_post(&constructor_wait
);
643 * Only execute pending statedump after the constructor semaphore has
644 * been posted by each listener thread. This means statedump will only
645 * be performed after the "registration done" command is received from
646 * each session daemon the application is connected to.
648 * This ensures we don't run into deadlock issues with the dynamic
649 * loader mutex, which is held while the constructor is called and
650 * waiting on the constructor semaphore. All operations requiring this
651 * dynamic loader lock need to be postponed using this mechanism.
654 void handle_pending_statedump(struct sock_info
*sock_info
)
656 int ctor_passed
= sock_info
->constructor_sem_posted
;
658 if (ctor_passed
&& sock_info
->statedump_pending
) {
659 sock_info
->statedump_pending
= 0;
660 pthread_mutex_lock(&ust_fork_mutex
);
661 lttng_handle_pending_statedump(sock_info
);
662 pthread_mutex_unlock(&ust_fork_mutex
);
667 int handle_message(struct sock_info
*sock_info
,
668 int sock
, struct ustcomm_ust_msg
*lum
)
671 const struct lttng_ust_objd_ops
*ops
;
672 struct ustcomm_ust_reply lur
;
674 char ctxstr
[LTTNG_UST_SYM_NAME_LEN
]; /* App context string. */
677 memset(&lur
, 0, sizeof(lur
));
680 ret
= -LTTNG_UST_ERR_EXITING
;
684 ops
= objd_ops(lum
->handle
);
691 case LTTNG_UST_REGISTER_DONE
:
692 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
693 ret
= handle_register_done(sock_info
);
697 case LTTNG_UST_RELEASE
:
698 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
701 ret
= lttng_ust_objd_unref(lum
->handle
, 1);
703 case LTTNG_UST_FILTER
:
705 /* Receive filter data */
706 struct lttng_ust_filter_bytecode_node
*bytecode
;
708 if (lum
->u
.filter
.data_size
> FILTER_BYTECODE_MAX_LEN
) {
709 ERR("Filter data size is too large: %u bytes",
710 lum
->u
.filter
.data_size
);
715 if (lum
->u
.filter
.reloc_offset
> lum
->u
.filter
.data_size
) {
716 ERR("Filter reloc offset %u is not within data",
717 lum
->u
.filter
.reloc_offset
);
722 bytecode
= zmalloc(sizeof(*bytecode
) + lum
->u
.filter
.data_size
);
727 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
,
728 lum
->u
.filter
.data_size
);
730 case 0: /* orderly shutdown */
735 if (len
== lum
->u
.filter
.data_size
) {
736 DBG("filter data received");
738 } else if (len
< 0) {
739 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
740 if (len
== -ECONNRESET
) {
741 ERR("%s remote end closed connection", sock_info
->name
);
750 DBG("incorrect filter data message size: %zd", len
);
756 bytecode
->bc
.len
= lum
->u
.filter
.data_size
;
757 bytecode
->bc
.reloc_offset
= lum
->u
.filter
.reloc_offset
;
758 bytecode
->bc
.seqnum
= lum
->u
.filter
.seqnum
;
760 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
761 (unsigned long) bytecode
,
766 /* don't free bytecode if everything went fine. */
773 case LTTNG_UST_EXCLUSION
:
775 /* Receive exclusion names */
776 struct lttng_ust_excluder_node
*node
;
779 count
= lum
->u
.exclusion
.count
;
781 /* There are no names to read */
785 node
= zmalloc(sizeof(*node
) +
786 count
* LTTNG_UST_SYM_NAME_LEN
);
791 node
->excluder
.count
= count
;
792 len
= ustcomm_recv_unix_sock(sock
, node
->excluder
.names
,
793 count
* LTTNG_UST_SYM_NAME_LEN
);
795 case 0: /* orderly shutdown */
800 if (len
== count
* LTTNG_UST_SYM_NAME_LEN
) {
801 DBG("Exclusion data received");
803 } else if (len
< 0) {
804 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
805 if (len
== -ECONNRESET
) {
806 ERR("%s remote end closed connection", sock_info
->name
);
815 DBG("Incorrect exclusion data message size: %zd", len
);
822 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
823 (unsigned long) node
,
828 /* Don't free exclusion data if everything went fine. */
835 case LTTNG_UST_CHANNEL
:
840 len
= ustcomm_recv_channel_from_sessiond(sock
,
841 &chan_data
, lum
->u
.channel
.len
,
844 case 0: /* orderly shutdown */
848 if (len
== lum
->u
.channel
.len
) {
849 DBG("channel data received");
851 } else if (len
< 0) {
852 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
853 if (len
== -ECONNRESET
) {
854 ERR("%s remote end closed connection", sock_info
->name
);
861 DBG("incorrect channel data message size: %zd", len
);
866 args
.channel
.chan_data
= chan_data
;
867 args
.channel
.wakeup_fd
= wakeup_fd
;
869 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
870 (unsigned long) &lum
->u
,
876 case LTTNG_UST_STREAM
:
878 /* Receive shm_fd, wakeup_fd */
879 ret
= ustcomm_recv_stream_from_sessiond(sock
,
882 &args
.stream
.wakeup_fd
);
887 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
888 (unsigned long) &lum
->u
,
894 case LTTNG_UST_CONTEXT
:
895 switch (lum
->u
.context
.ctx
) {
896 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
899 size_t ctxlen
, recvlen
;
901 ctxlen
= strlen("$app.") + lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1
902 + strlen(":") + lum
->u
.context
.u
.app_ctx
.ctx_name_len
;
903 if (ctxlen
>= LTTNG_UST_SYM_NAME_LEN
) {
904 ERR("Application context string length size is too large: %zu bytes",
909 strcpy(ctxstr
, "$app.");
910 p
= &ctxstr
[strlen("$app.")];
911 recvlen
= ctxlen
- strlen("$app.");
912 len
= ustcomm_recv_unix_sock(sock
, p
, recvlen
);
914 case 0: /* orderly shutdown */
918 if (len
== recvlen
) {
919 DBG("app context data received");
921 } else if (len
< 0) {
922 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
923 if (len
== -ECONNRESET
) {
924 ERR("%s remote end closed connection", sock_info
->name
);
931 DBG("incorrect app context data message size: %zd", len
);
936 /* Put : between provider and ctxname. */
937 p
[lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1] = ':';
938 args
.app_context
.ctxname
= ctxstr
;
945 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
946 (unsigned long) &lum
->u
,
954 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
955 (unsigned long) &lum
->u
,
962 lur
.handle
= lum
->handle
;
966 lur
.ret_code
= LTTNG_UST_OK
;
969 * Use -LTTNG_UST_ERR as wildcard for UST internal
970 * error that are not caused by the transport, except if
971 * we already have a more precise error message to
974 if (ret
> -LTTNG_UST_ERR
) {
975 /* Translate code to UST error. */
978 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
981 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
984 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
987 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
990 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
993 lur
.ret_code
= -LTTNG_UST_ERR
;
1002 case LTTNG_UST_TRACER_VERSION
:
1003 lur
.u
.version
= lum
->u
.version
;
1005 case LTTNG_UST_TRACEPOINT_LIST_GET
:
1006 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
1010 DBG("Return value: %d", lur
.ret_val
);
1015 * Performed delayed statedump operations outside of the UST
1016 * lock. We need to take the dynamic loader lock before we take
1017 * the UST lock internally within handle_pending_statedump().
1019 handle_pending_statedump(sock_info
);
1022 ret
= -LTTNG_UST_ERR_EXITING
;
1026 ret
= send_reply(sock
, &lur
);
1028 DBG("error sending reply");
1033 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
1036 if (lur
.ret_code
== LTTNG_UST_OK
) {
1038 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
:
1039 len
= ustcomm_send_unix_sock(sock
,
1040 &args
.field_list
.entry
,
1041 sizeof(args
.field_list
.entry
));
1046 if (len
!= sizeof(args
.field_list
.entry
)) {
1060 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
1064 if (sock_info
->root_handle
!= -1) {
1065 ret
= lttng_ust_objd_unref(sock_info
->root_handle
, 1);
1067 ERR("Error unref root handle");
1069 sock_info
->root_handle
= -1;
1071 sock_info
->constructor_sem_posted
= 0;
1074 * wait_shm_mmap, socket and notify socket are used by listener
1075 * threads outside of the ust lock, so we cannot tear them down
1076 * ourselves, because we cannot join on these threads. Leave
1077 * responsibility of cleaning up these resources to the OS
1083 if (sock_info
->socket
!= -1) {
1084 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1086 ERR("Error closing ust cmd socket");
1088 sock_info
->socket
= -1;
1090 if (sock_info
->notify_socket
!= -1) {
1091 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1093 ERR("Error closing ust notify socket");
1095 sock_info
->notify_socket
= -1;
1097 if (sock_info
->wait_shm_mmap
) {
1100 page_size
= sysconf(_SC_PAGE_SIZE
);
1101 if (page_size
<= 0) {
1105 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1107 ret
= munmap(sock_info
->wait_shm_mmap
, page_size
);
1109 ERR("Error unmapping wait shm");
1112 sock_info
->wait_shm_mmap
= NULL
;
1117 * Using fork to set umask in the child process (not multi-thread safe).
1118 * We deal with the shm_open vs ftruncate race (happening when the
1119 * sessiond owns the shm and does not let everybody modify it, to ensure
1120 * safety against shm_unlink) by simply letting the mmap fail and
1121 * retrying after a few seconds.
1122 * For global shm, everybody has rw access to it until the sessiond
1126 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
1128 int wait_shm_fd
, ret
;
1132 * Try to open read-only.
1134 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1135 if (wait_shm_fd
>= 0) {
1138 size_t bytes_read
= 0;
1141 * Try to read the fd. If unable to do so, try opening
1145 len
= read(wait_shm_fd
,
1146 &((char *) &tmp_read
)[bytes_read
],
1147 sizeof(tmp_read
) - bytes_read
);
1151 } while ((len
< 0 && errno
== EINTR
)
1152 || (len
> 0 && bytes_read
< sizeof(tmp_read
)));
1153 if (bytes_read
!= sizeof(tmp_read
)) {
1154 ret
= close(wait_shm_fd
);
1156 ERR("close wait_shm_fd");
1161 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
1163 * Real-only open did not work, and it's not because the
1164 * entry was not present. It's a failure that prohibits
1167 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1173 * If the open failed because the file did not exist, or because
1174 * the file was not truncated yet, try creating it ourself.
1176 URCU_TLS(lttng_ust_nest_count
)++;
1178 URCU_TLS(lttng_ust_nest_count
)--;
1183 * Parent: wait for child to return, in which case the
1184 * shared memory map will have been created.
1186 pid
= wait(&status
);
1187 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
1192 * Try to open read-only again after creation.
1194 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1195 if (wait_shm_fd
< 0) {
1197 * Real-only open did not work. It's a failure
1198 * that prohibits using shm.
1200 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1204 } else if (pid
== 0) {
1208 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
1209 if (sock_info
->global
)
1210 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
1212 * We're alone in a child process, so we can modify the
1213 * process-wide umask.
1215 umask(~create_mode
);
1217 * Try creating shm (or get rw access).
1218 * We don't do an exclusive open, because we allow other
1219 * processes to create+ftruncate it concurrently.
1221 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
1222 O_RDWR
| O_CREAT
, create_mode
);
1223 if (wait_shm_fd
>= 0) {
1224 ret
= ftruncate(wait_shm_fd
, mmap_size
);
1226 PERROR("ftruncate");
1227 _exit(EXIT_FAILURE
);
1229 _exit(EXIT_SUCCESS
);
1232 * For local shm, we need to have rw access to accept
1233 * opening it: this means the local sessiond will be
1234 * able to wake us up. For global shm, we open it even
1235 * if rw access is not granted, because the root.root
1236 * sessiond will be able to override all rights and wake
1239 if (!sock_info
->global
&& errno
!= EACCES
) {
1240 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1241 _exit(EXIT_FAILURE
);
1244 * The shm exists, but we cannot open it RW. Report
1247 _exit(EXIT_SUCCESS
);
1252 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
1253 struct stat statbuf
;
1256 * Ensure that our user is the owner of the shm file for
1257 * local shm. If we do not own the file, it means our
1258 * sessiond will not have access to wake us up (there is
1259 * probably a rogue process trying to fake our
1260 * sessiond). Fallback to polling method in this case.
1262 ret
= fstat(wait_shm_fd
, &statbuf
);
1267 if (statbuf
.st_uid
!= getuid())
1273 ret
= close(wait_shm_fd
);
1275 PERROR("Error closing fd");
1281 char *get_map_shm(struct sock_info
*sock_info
)
1284 int wait_shm_fd
, ret
;
1285 char *wait_shm_mmap
;
1287 page_size
= sysconf(_SC_PAGE_SIZE
);
1288 if (page_size
<= 0) {
1292 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1296 lttng_ust_lock_fd_tracker();
1297 wait_shm_fd
= get_wait_shm(sock_info
, page_size
);
1298 if (wait_shm_fd
< 0) {
1299 lttng_ust_unlock_fd_tracker();
1303 ret
= lttng_ust_add_fd_to_tracker(wait_shm_fd
);
1305 ret
= close(wait_shm_fd
);
1307 PERROR("Error closing fd");
1309 lttng_ust_unlock_fd_tracker();
1314 lttng_ust_unlock_fd_tracker();
1316 wait_shm_mmap
= mmap(NULL
, page_size
, PROT_READ
,
1317 MAP_SHARED
, wait_shm_fd
, 0);
1319 /* close shm fd immediately after taking the mmap reference */
1320 lttng_ust_lock_fd_tracker();
1321 ret
= close(wait_shm_fd
);
1323 lttng_ust_delete_fd_from_tracker(wait_shm_fd
);
1325 PERROR("Error closing fd");
1327 lttng_ust_unlock_fd_tracker();
1329 if (wait_shm_mmap
== MAP_FAILED
) {
1330 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1333 return wait_shm_mmap
;
1340 void wait_for_sessiond(struct sock_info
*sock_info
)
1342 /* Use ust_lock to check if we should quit. */
1346 if (wait_poll_fallback
) {
1351 assert(sock_info
->wait_shm_mmap
);
1353 DBG("Waiting for %s apps sessiond", sock_info
->name
);
1354 /* Wait for futex wakeup */
1355 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
))
1358 while (futex_async((int32_t *) sock_info
->wait_shm_mmap
,
1359 FUTEX_WAIT
, 0, NULL
, NULL
, 0)) {
1362 /* Value already changed. */
1365 /* Retry if interrupted by signal. */
1366 break; /* Get out of switch. */
1368 wait_poll_fallback
= 1;
1370 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1371 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
1372 "Please upgrade your kernel "
1373 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1374 "mainline). LTTng-UST will use polling mode fallback.");
1393 * This thread does not allocate any resource, except within
1394 * handle_message, within mutex protection. This mutex protects against
1396 * The other moment it allocates resources is at socket connection, which
1397 * is also protected by the mutex.
1400 void *ust_listener_thread(void *arg
)
1402 struct sock_info
*sock_info
= arg
;
1403 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0, fd
;
1406 lttng_ust_fixup_tls();
1408 * If available, add '-ust' to the end of this thread's
1411 ret
= lttng_ust_setustprocname();
1413 ERR("Unable to set UST process name");
1416 /* Restart trying to connect to the session daemon */
1418 if (prev_connect_failed
) {
1419 /* Wait for sessiond availability with pipe */
1420 wait_for_sessiond(sock_info
);
1424 * Sleep for 5 seconds before retrying after a
1425 * sequence of failure / wait / failure. This
1426 * deals with a killed or broken session daemon.
1432 prev_connect_failed
= 0;
1439 if (sock_info
->socket
!= -1) {
1440 /* FD tracker is updated by ustcomm_close_unix_sock() */
1441 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1443 ERR("Error closing %s ust cmd socket",
1446 sock_info
->socket
= -1;
1448 if (sock_info
->notify_socket
!= -1) {
1449 /* FD tracker is updated by ustcomm_close_unix_sock() */
1450 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1452 ERR("Error closing %s ust notify socket",
1455 sock_info
->notify_socket
= -1;
1460 * Register. We need to perform both connect and sending
1461 * registration message before doing the next connect otherwise
1462 * we may reach unix socket connect queue max limits and block
1463 * on the 2nd connect while the session daemon is awaiting the
1464 * first connect registration message.
1466 /* Connect cmd socket */
1467 lttng_ust_lock_fd_tracker();
1468 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1469 get_connect_sock_timeout());
1471 lttng_ust_unlock_fd_tracker();
1472 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1473 prev_connect_failed
= 1;
1476 * If we cannot find the sessiond daemon, don't delay
1477 * constructor execution.
1479 ret
= handle_register_done(sock_info
);
1485 ret
= lttng_ust_add_fd_to_tracker(fd
);
1489 PERROR("close on sock_info->socket");
1492 lttng_ust_unlock_fd_tracker();
1497 sock_info
->socket
= ret
;
1498 lttng_ust_unlock_fd_tracker();
1502 * Unlock/relock ust lock because connect is blocking (with
1503 * timeout). Don't delay constructors on the ust lock for too
1511 * Create only one root handle per listener thread for the whole
1512 * process lifetime, so we ensure we get ID which is statically
1513 * assigned to the root handle.
1515 if (sock_info
->root_handle
== -1) {
1516 ret
= lttng_abi_create_root_handle();
1518 ERR("Error creating root handle");
1521 sock_info
->root_handle
= ret
;
1524 ret
= register_to_sessiond(sock_info
->socket
, USTCTL_SOCKET_CMD
);
1526 ERR("Error registering to %s ust cmd socket",
1528 prev_connect_failed
= 1;
1530 * If we cannot register to the sessiond daemon, don't
1531 * delay constructor execution.
1533 ret
= handle_register_done(sock_info
);
1541 * Unlock/relock ust lock because connect is blocking (with
1542 * timeout). Don't delay constructors on the ust lock for too
1549 /* Connect notify socket */
1550 lttng_ust_lock_fd_tracker();
1551 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1552 get_connect_sock_timeout());
1554 lttng_ust_unlock_fd_tracker();
1555 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1556 prev_connect_failed
= 1;
1559 * If we cannot find the sessiond daemon, don't delay
1560 * constructor execution.
1562 ret
= handle_register_done(sock_info
);
1569 ret
= lttng_ust_add_fd_to_tracker(fd
);
1573 PERROR("close on sock_info->notify_socket");
1576 lttng_ust_unlock_fd_tracker();
1581 sock_info
->notify_socket
= ret
;
1582 lttng_ust_unlock_fd_tracker();
1586 * Unlock/relock ust lock because connect is blocking (with
1587 * timeout). Don't delay constructors on the ust lock for too
1594 timeout
= get_notify_sock_timeout();
1597 * Give at least 10ms to sessiond to reply to
1602 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
1605 WARN("Error setting socket receive timeout");
1607 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
1610 WARN("Error setting socket send timeout");
1612 } else if (timeout
< -1) {
1613 WARN("Unsupported timeout value %ld", timeout
);
1616 ret
= register_to_sessiond(sock_info
->notify_socket
,
1617 USTCTL_SOCKET_NOTIFY
);
1619 ERR("Error registering to %s ust notify socket",
1621 prev_connect_failed
= 1;
1623 * If we cannot register to the sessiond daemon, don't
1624 * delay constructor execution.
1626 ret
= handle_register_done(sock_info
);
1631 sock
= sock_info
->socket
;
1637 struct ustcomm_ust_msg lum
;
1639 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
1641 case 0: /* orderly shutdown */
1642 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
1647 * Either sessiond has shutdown or refused us by closing the socket.
1648 * In either case, we don't want to delay construction execution,
1649 * and we need to wait before retry.
1651 prev_connect_failed
= 1;
1653 * If we cannot register to the sessiond daemon, don't
1654 * delay constructor execution.
1656 ret
= handle_register_done(sock_info
);
1661 print_cmd(lum
.cmd
, lum
.handle
);
1662 ret
= handle_message(sock_info
, sock
, &lum
);
1664 ERR("Error handling message for %s socket",
1667 * Close socket if protocol error is
1675 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1677 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
1679 if (len
== -ECONNRESET
) {
1680 DBG("%s remote end closed connection", sock_info
->name
);
1691 /* Cleanup socket handles before trying to reconnect */
1692 lttng_ust_objd_table_owner_cleanup(sock_info
);
1694 goto restart
; /* try to reconnect */
1699 pthread_mutex_lock(&ust_exit_mutex
);
1700 sock_info
->thread_active
= 0;
1701 pthread_mutex_unlock(&ust_exit_mutex
);
1706 * Weak symbol to call when the ust malloc wrapper is not loaded.
1708 __attribute__((weak
))
1709 void lttng_ust_malloc_wrapper_init(void)
1714 * sessiond monitoring thread: monitor presence of global and per-user
1715 * sessiond by polling the application common named pipe.
1717 void __attribute__((constructor
)) lttng_ust_init(void)
1719 struct timespec constructor_timeout
;
1720 sigset_t sig_all_blocked
, orig_parent_mask
;
1721 pthread_attr_t thread_attr
;
1726 if (uatomic_xchg(&initialized
, 1) == 1)
1730 * Fixup interdependency between TLS fixup mutex (which happens
1731 * to be the dynamic linker mutex) and ust_lock, taken within
1734 lttng_ust_fixup_tls();
1736 lttng_ust_loaded
= 1;
1739 * We need to ensure that the liblttng-ust library is not unloaded to avoid
1740 * the unloading of code used by the ust_listener_threads as we can not
1741 * reliably know when they exited. To do that, manually load
1742 * liblttng-ust.so to increment the dynamic loader's internal refcount for
1743 * this library so it never becomes zero, thus never gets unloaded from the
1744 * address space of the process. Since we are already running in the
1745 * constructor of the LTTNG_UST_LIB_SO_NAME library, calling dlopen will
1746 * simply increment the refcount and no additionnal work is needed by the
1747 * dynamic loader as the shared library is already loaded in the address
1748 * space. As a safe guard, we use the RTLD_NODELETE flag to prevent
1749 * unloading of the UST library if its refcount becomes zero (which should
1750 * never happen). Do the return value check but discard the handle at the
1751 * end of the function as it's not needed.
1753 handle
= dlopen(LTTNG_UST_LIB_SO_NAME
, RTLD_LAZY
| RTLD_NODELETE
);
1755 ERR("dlopen of liblttng-ust shared library (%s).", LTTNG_UST_LIB_SO_NAME
);
1759 * We want precise control over the order in which we construct
1760 * our sub-libraries vs starting to receive commands from
1761 * sessiond (otherwise leading to errors when trying to create
1762 * sessiond before the init functions are completed).
1765 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
1767 lttng_ust_init_fd_tracker();
1768 lttng_ust_clock_init();
1769 lttng_ust_getcpu_init();
1770 lttng_ust_statedump_init();
1771 lttng_ring_buffer_metadata_client_init();
1772 lttng_ring_buffer_client_overwrite_init();
1773 lttng_ring_buffer_client_overwrite_rt_init();
1774 lttng_ring_buffer_client_discard_init();
1775 lttng_ring_buffer_client_discard_rt_init();
1776 lttng_perf_counter_init();
1778 * Invoke ust malloc wrapper init before starting other threads.
1780 lttng_ust_malloc_wrapper_init();
1782 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
1784 get_allow_blocking();
1786 ret
= sem_init(&constructor_wait
, 0, 0);
1791 ret
= setup_global_apps();
1793 assert(global_apps
.allowed
== 0);
1794 DBG("global apps setup returned %d", ret
);
1797 ret
= setup_local_apps();
1799 assert(local_apps
.allowed
== 0);
1800 DBG("local apps setup returned %d", ret
);
1803 /* A new thread created by pthread_create inherits the signal mask
1804 * from the parent. To avoid any signal being received by the
1805 * listener thread, we block all signals temporarily in the parent,
1806 * while we create the listener thread.
1808 sigfillset(&sig_all_blocked
);
1809 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1811 ERR("pthread_sigmask: %s", strerror(ret
));
1814 ret
= pthread_attr_init(&thread_attr
);
1816 ERR("pthread_attr_init: %s", strerror(ret
));
1818 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
1820 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
1823 if (global_apps
.allowed
) {
1824 pthread_mutex_lock(&ust_exit_mutex
);
1825 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
1826 ust_listener_thread
, &global_apps
);
1828 ERR("pthread_create global: %s", strerror(ret
));
1830 global_apps
.thread_active
= 1;
1831 pthread_mutex_unlock(&ust_exit_mutex
);
1833 handle_register_done(&global_apps
);
1836 if (local_apps
.allowed
) {
1837 pthread_mutex_lock(&ust_exit_mutex
);
1838 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
1839 ust_listener_thread
, &local_apps
);
1841 ERR("pthread_create local: %s", strerror(ret
));
1843 local_apps
.thread_active
= 1;
1844 pthread_mutex_unlock(&ust_exit_mutex
);
1846 handle_register_done(&local_apps
);
1848 ret
= pthread_attr_destroy(&thread_attr
);
1850 ERR("pthread_attr_destroy: %s", strerror(ret
));
1853 /* Restore original signal mask in parent */
1854 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1856 ERR("pthread_sigmask: %s", strerror(ret
));
1859 switch (timeout_mode
) {
1860 case 1: /* timeout wait */
1862 ret
= sem_timedwait(&constructor_wait
,
1863 &constructor_timeout
);
1864 } while (ret
< 0 && errno
== EINTR
);
1868 ERR("Timed out waiting for lttng-sessiond");
1871 PERROR("sem_timedwait");
1874 ERR("Unexpected error \"%s\" returned by sem_timedwait",
1879 case -1:/* wait forever */
1881 ret
= sem_wait(&constructor_wait
);
1882 } while (ret
< 0 && errno
== EINTR
);
1889 ERR("Unexpected error \"%s\" returned by sem_wait",
1894 case 0: /* no timeout */
1900 void lttng_ust_cleanup(int exiting
)
1902 cleanup_sock_info(&global_apps
, exiting
);
1903 cleanup_sock_info(&local_apps
, exiting
);
1904 local_apps
.allowed
= 0;
1905 global_apps
.allowed
= 0;
1907 * The teardown in this function all affect data structures
1908 * accessed under the UST lock by the listener thread. This
1909 * lock, along with the lttng_ust_comm_should_quit flag, ensure
1910 * that none of these threads are accessing this data at this
1913 lttng_ust_abi_exit();
1914 lttng_ust_events_exit();
1915 lttng_perf_counter_exit();
1916 lttng_ring_buffer_client_discard_rt_exit();
1917 lttng_ring_buffer_client_discard_exit();
1918 lttng_ring_buffer_client_overwrite_rt_exit();
1919 lttng_ring_buffer_client_overwrite_exit();
1920 lttng_ring_buffer_metadata_client_exit();
1921 lttng_ust_statedump_destroy();
1924 /* Reinitialize values for fork */
1926 lttng_ust_comm_should_quit
= 0;
1931 void __attribute__((destructor
)) lttng_ust_exit(void)
1936 * Using pthread_cancel here because:
1937 * A) we don't want to hang application teardown.
1938 * B) the thread is not allocating any resource.
1942 * Require the communication thread to quit. Synchronize with
1943 * mutexes to ensure it is not in a mutex critical section when
1944 * pthread_cancel is later called.
1947 lttng_ust_comm_should_quit
= 1;
1950 pthread_mutex_lock(&ust_exit_mutex
);
1951 /* cancel threads */
1952 if (global_apps
.thread_active
) {
1953 ret
= pthread_cancel(global_apps
.ust_listener
);
1955 ERR("Error cancelling global ust listener thread: %s",
1958 global_apps
.thread_active
= 0;
1961 if (local_apps
.thread_active
) {
1962 ret
= pthread_cancel(local_apps
.ust_listener
);
1964 ERR("Error cancelling local ust listener thread: %s",
1967 local_apps
.thread_active
= 0;
1970 pthread_mutex_unlock(&ust_exit_mutex
);
1973 * Do NOT join threads: use of sys_futex makes it impossible to
1974 * join the threads without using async-cancel, but async-cancel
1975 * is delivered by a signal, which could hit the target thread
1976 * anywhere in its code path, including while the ust_lock() is
1977 * held, causing a deadlock for the other thread. Let the OS
1978 * cleanup the threads if there are stalled in a syscall.
1980 lttng_ust_cleanup(1);
1984 * We exclude the worker threads across fork and clone (except
1985 * CLONE_VM), because these system calls only keep the forking thread
1986 * running in the child. Therefore, we don't want to call fork or clone
1987 * in the middle of an tracepoint or ust tracing state modification.
1988 * Holding this mutex protects these structures across fork and clone.
1990 void ust_before_fork(sigset_t
*save_sigset
)
1993 * Disable signals. This is to avoid that the child intervenes
1994 * before it is properly setup for tracing. It is safer to
1995 * disable all signals, because then we know we are not breaking
1996 * anything by restoring the original mask.
2001 /* Fixup lttng-ust TLS. */
2002 lttng_ust_fixup_tls();
2004 if (URCU_TLS(lttng_ust_nest_count
))
2006 /* Disable signals */
2007 sigfillset(&all_sigs
);
2008 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
2010 PERROR("sigprocmask");
2013 pthread_mutex_lock(&ust_fork_mutex
);
2016 urcu_bp_before_fork();
2019 static void ust_after_fork_common(sigset_t
*restore_sigset
)
2023 DBG("process %d", getpid());
2026 pthread_mutex_unlock(&ust_fork_mutex
);
2028 /* Restore signals */
2029 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
2031 PERROR("sigprocmask");
2035 void ust_after_fork_parent(sigset_t
*restore_sigset
)
2037 if (URCU_TLS(lttng_ust_nest_count
))
2039 DBG("process %d", getpid());
2040 urcu_bp_after_fork_parent();
2041 /* Release mutexes and reenable signals */
2042 ust_after_fork_common(restore_sigset
);
2046 * After fork, in the child, we need to cleanup all the leftover state,
2047 * except the worker thread which already magically disappeared thanks
2048 * to the weird Linux fork semantics. After tyding up, we call
2049 * lttng_ust_init() again to start over as a new PID.
2051 * This is meant for forks() that have tracing in the child between the
2052 * fork and following exec call (if there is any).
2054 void ust_after_fork_child(sigset_t
*restore_sigset
)
2056 if (URCU_TLS(lttng_ust_nest_count
))
2058 lttng_context_vpid_reset();
2059 lttng_context_vtid_reset();
2060 lttng_context_procname_reset();
2061 DBG("process %d", getpid());
2062 /* Release urcu mutexes */
2063 urcu_bp_after_fork_child();
2064 lttng_ust_cleanup(0);
2065 /* Release mutexes and reenable signals */
2066 ust_after_fork_common(restore_sigset
);
2070 void lttng_ust_sockinfo_session_enabled(void *owner
)
2072 struct sock_info
*sock_info
= owner
;
2073 sock_info
->statedump_pending
= 1;