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>
34 #include <semaphore.h>
38 #include <urcu/uatomic.h>
39 #include <urcu/futex.h>
40 #include <urcu/compiler.h>
42 #include <lttng/ust-events.h>
43 #include <lttng/ust-abi.h>
44 #include <lttng/ust.h>
45 #include <lttng/ust-error.h>
46 #include <lttng/ust-ctl.h>
47 #include <urcu/tls-compat.h>
50 #include <usterr-signal-safe.h>
52 #include "tracepoint-internal.h"
53 #include "lttng-tracer-core.h"
55 #include "../libringbuffer/tlsfixup.h"
56 #include "lttng-ust-statedump.h"
58 #include "../libringbuffer/getcpu.h"
62 * Has lttng ust comm constructor been called ?
64 static int initialized
;
67 * The ust_lock/ust_unlock lock is used as a communication thread mutex.
68 * Held when handling a command, also held by fork() to deal with
69 * removal of threads, and by exit path.
71 * The UST lock is the centralized mutex across UST tracing control and
74 * ust_exit_mutex must never nest in ust_mutex.
76 * ust_fork_mutex must never nest in ust_mutex.
78 * ust_mutex_nest is a per-thread nesting counter, allowing the perf
79 * counter lazy initialization called by events within the statedump,
80 * which traces while the ust_mutex is held.
82 * ust_lock nests within the dynamic loader lock (within glibc) because
83 * it is taken within the library constructor.
85 * The ust fd tracker lock nests within the ust_mutex.
87 static pthread_mutex_t ust_mutex
= PTHREAD_MUTEX_INITIALIZER
;
89 /* Allow nesting the ust_mutex within the same thread. */
90 static DEFINE_URCU_TLS(int, ust_mutex_nest
);
93 * ust_exit_mutex protects thread_active variable wrt thread exit. It
94 * cannot be done by ust_mutex because pthread_cancel(), which takes an
95 * internal libc lock, cannot nest within ust_mutex.
97 * It never nests within a ust_mutex.
99 static pthread_mutex_t ust_exit_mutex
= PTHREAD_MUTEX_INITIALIZER
;
102 * ust_fork_mutex protects base address statedump tracing against forks. It
103 * prevents the dynamic loader lock to be taken (by base address statedump
104 * tracing) while a fork is happening, thus preventing deadlock issues with
105 * the dynamic loader lock.
107 static pthread_mutex_t ust_fork_mutex
= PTHREAD_MUTEX_INITIALIZER
;
109 /* Should the ust comm thread quit ? */
110 static int lttng_ust_comm_should_quit
;
113 * This variable can be tested by applications to check whether
114 * lttng-ust is loaded. They simply have to define their own
115 * "lttng_ust_loaded" weak symbol, and test it. It is set to 1 by the
116 * library constructor.
118 int lttng_ust_loaded
__attribute__((weak
));
121 * Return 0 on success, -1 if should quit.
122 * The lock is taken in both cases.
127 sigset_t sig_all_blocked
, orig_mask
;
130 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
132 ERR("pthread_setcancelstate: %s", strerror(ret
));
134 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
135 ERR("pthread_setcancelstate: unexpected oldstate");
137 sigfillset(&sig_all_blocked
);
138 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
140 ERR("pthread_sigmask: %s", strerror(ret
));
142 if (!URCU_TLS(ust_mutex_nest
)++)
143 pthread_mutex_lock(&ust_mutex
);
144 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
146 ERR("pthread_sigmask: %s", strerror(ret
));
148 if (lttng_ust_comm_should_quit
) {
156 * ust_lock_nocheck() can be used in constructors/destructors, because
157 * they are already nested within the dynamic loader lock, and therefore
158 * have exclusive access against execution of liblttng-ust destructor.
161 void ust_lock_nocheck(void)
163 sigset_t sig_all_blocked
, orig_mask
;
166 ret
= pthread_setcancelstate(PTHREAD_CANCEL_DISABLE
, &oldstate
);
168 ERR("pthread_setcancelstate: %s", strerror(ret
));
170 if (oldstate
!= PTHREAD_CANCEL_ENABLE
) {
171 ERR("pthread_setcancelstate: unexpected oldstate");
173 sigfillset(&sig_all_blocked
);
174 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
176 ERR("pthread_sigmask: %s", strerror(ret
));
178 if (!URCU_TLS(ust_mutex_nest
)++)
179 pthread_mutex_lock(&ust_mutex
);
180 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
182 ERR("pthread_sigmask: %s", strerror(ret
));
189 void ust_unlock(void)
191 sigset_t sig_all_blocked
, orig_mask
;
194 sigfillset(&sig_all_blocked
);
195 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_mask
);
197 ERR("pthread_sigmask: %s", strerror(ret
));
199 if (!--URCU_TLS(ust_mutex_nest
))
200 pthread_mutex_unlock(&ust_mutex
);
201 ret
= pthread_sigmask(SIG_SETMASK
, &orig_mask
, NULL
);
203 ERR("pthread_sigmask: %s", strerror(ret
));
205 ret
= pthread_setcancelstate(PTHREAD_CANCEL_ENABLE
, &oldstate
);
207 ERR("pthread_setcancelstate: %s", strerror(ret
));
209 if (oldstate
!= PTHREAD_CANCEL_DISABLE
) {
210 ERR("pthread_setcancelstate: unexpected oldstate");
215 * Wait for either of these before continuing to the main
217 * - the register_done message from sessiond daemon
218 * (will let the sessiond daemon enable sessions before main
220 * - sessiond daemon is not reachable.
221 * - timeout (ensuring applications are resilient to session
224 static sem_t constructor_wait
;
226 * Doing this for both the global and local sessiond.
229 sem_count_initial_value
= 4,
232 static int sem_count
= sem_count_initial_value
;
235 * Counting nesting within lttng-ust. Used to ensure that calling fork()
236 * from liblttng-ust does not execute the pre/post fork handlers.
238 static DEFINE_URCU_TLS(int, lttng_ust_nest_count
);
241 * Info about socket and associated listener thread.
245 pthread_t ust_listener
; /* listener thread */
247 int registration_done
;
252 char sock_path
[PATH_MAX
];
256 char wait_shm_path
[PATH_MAX
];
258 /* Keep track of lazy state dump not performed yet. */
259 int statedump_pending
;
260 int initial_statedump_done
;
263 /* Socket from app (connect) to session daemon (listen) for communication */
264 struct sock_info global_apps
= {
269 .registration_done
= 0,
273 .sock_path
= LTTNG_DEFAULT_RUNDIR
"/" LTTNG_UST_SOCK_FILENAME
,
277 .wait_shm_path
= "/" LTTNG_UST_WAIT_FILENAME
,
279 .statedump_pending
= 0,
280 .initial_statedump_done
= 0,
283 /* TODO: allow global_apps_sock_path override */
285 struct sock_info local_apps
= {
289 .registration_done
= 0,
290 .allowed
= 0, /* Check setuid bit first */
296 .statedump_pending
= 0,
297 .initial_statedump_done
= 0,
300 static int wait_poll_fallback
;
302 static const char *cmd_name_mapping
[] = {
303 [ LTTNG_UST_RELEASE
] = "Release",
304 [ LTTNG_UST_SESSION
] = "Create Session",
305 [ LTTNG_UST_TRACER_VERSION
] = "Get Tracer Version",
307 [ LTTNG_UST_TRACEPOINT_LIST
] = "Create Tracepoint List",
308 [ LTTNG_UST_WAIT_QUIESCENT
] = "Wait for Quiescent State",
309 [ LTTNG_UST_REGISTER_DONE
] = "Registration Done",
310 [ LTTNG_UST_TRACEPOINT_FIELD_LIST
] = "Create Tracepoint Field List",
312 /* Session FD commands */
313 [ LTTNG_UST_CHANNEL
] = "Create Channel",
314 [ LTTNG_UST_SESSION_START
] = "Start Session",
315 [ LTTNG_UST_SESSION_STOP
] = "Stop Session",
317 /* Channel FD commands */
318 [ LTTNG_UST_STREAM
] = "Create Stream",
319 [ LTTNG_UST_EVENT
] = "Create Event",
321 /* Event and Channel FD commands */
322 [ LTTNG_UST_CONTEXT
] = "Create Context",
323 [ LTTNG_UST_FLUSH_BUFFER
] = "Flush Buffer",
325 /* Event, Channel and Session commands */
326 [ LTTNG_UST_ENABLE
] = "Enable",
327 [ LTTNG_UST_DISABLE
] = "Disable",
329 /* Tracepoint list commands */
330 [ LTTNG_UST_TRACEPOINT_LIST_GET
] = "List Next Tracepoint",
331 [ LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
] = "List Next Tracepoint Field",
333 /* Event FD commands */
334 [ LTTNG_UST_FILTER
] = "Create Filter",
335 [ LTTNG_UST_EXCLUSION
] = "Add exclusions to event",
338 static const char *str_timeout
;
339 static int got_timeout_env
;
341 extern void lttng_ring_buffer_client_overwrite_init(void);
342 extern void lttng_ring_buffer_client_overwrite_rt_init(void);
343 extern void lttng_ring_buffer_client_discard_init(void);
344 extern void lttng_ring_buffer_client_discard_rt_init(void);
345 extern void lttng_ring_buffer_metadata_client_init(void);
346 extern void lttng_ring_buffer_client_overwrite_exit(void);
347 extern void lttng_ring_buffer_client_overwrite_rt_exit(void);
348 extern void lttng_ring_buffer_client_discard_exit(void);
349 extern void lttng_ring_buffer_client_discard_rt_exit(void);
350 extern void lttng_ring_buffer_metadata_client_exit(void);
352 static char *get_map_shm(struct sock_info
*sock_info
);
354 ssize_t
lttng_ust_read(int fd
, void *buf
, size_t len
)
357 size_t copied
= 0, to_copy
= len
;
360 ret
= read(fd
, buf
+ copied
, to_copy
);
365 } while ((ret
> 0 && to_copy
> 0)
366 || (ret
< 0 && errno
== EINTR
));
373 * Returns the HOME directory path. Caller MUST NOT free(3) the returned
377 const char *get_lttng_home_dir(void)
381 val
= (const char *) lttng_getenv("LTTNG_HOME");
385 return (const char *) lttng_getenv("HOME");
389 * Force a read (imply TLS fixup for dlopen) of TLS variables.
392 void lttng_fixup_nest_count_tls(void)
394 asm volatile ("" : : "m" (URCU_TLS(lttng_ust_nest_count
)));
398 void lttng_fixup_ust_mutex_nest_tls(void)
400 asm volatile ("" : : "m" (URCU_TLS(ust_mutex_nest
)));
407 void lttng_fixup_urcu_bp_tls(void)
413 void lttng_ust_fixup_tls(void)
415 lttng_fixup_urcu_bp_tls();
416 lttng_fixup_ringbuffer_tls();
417 lttng_fixup_vtid_tls();
418 lttng_fixup_nest_count_tls();
419 lttng_fixup_procname_tls();
420 lttng_fixup_ust_mutex_nest_tls();
421 lttng_ust_fixup_fd_tracker_tls();
424 int lttng_get_notify_socket(void *owner
)
426 struct sock_info
*info
= owner
;
428 return info
->notify_socket
;
432 void print_cmd(int cmd
, int handle
)
434 const char *cmd_name
= "Unknown";
436 if (cmd
>= 0 && cmd
< LTTNG_ARRAY_SIZE(cmd_name_mapping
)
437 && cmd_name_mapping
[cmd
]) {
438 cmd_name
= cmd_name_mapping
[cmd
];
440 DBG("Message Received \"%s\" (%d), Handle \"%s\" (%d)",
442 lttng_ust_obj_get_name(handle
), handle
);
446 int setup_global_apps(void)
449 assert(!global_apps
.wait_shm_mmap
);
451 global_apps
.wait_shm_mmap
= get_map_shm(&global_apps
);
452 if (!global_apps
.wait_shm_mmap
) {
453 WARN("Unable to get map shm for global apps. Disabling LTTng-UST global tracing.");
454 global_apps
.allowed
= 0;
459 global_apps
.allowed
= 1;
464 int setup_local_apps(void)
467 const char *home_dir
;
470 assert(!local_apps
.wait_shm_mmap
);
474 * Disallow per-user tracing for setuid binaries.
476 if (uid
!= geteuid()) {
477 assert(local_apps
.allowed
== 0);
481 home_dir
= get_lttng_home_dir();
483 WARN("HOME environment variable not set. Disabling LTTng-UST per-user tracing.");
484 assert(local_apps
.allowed
== 0);
488 local_apps
.allowed
= 1;
489 snprintf(local_apps
.sock_path
, PATH_MAX
, "%s/%s/%s",
491 LTTNG_DEFAULT_HOME_RUNDIR
,
492 LTTNG_UST_SOCK_FILENAME
);
493 snprintf(local_apps
.wait_shm_path
, PATH_MAX
, "/%s-%u",
494 LTTNG_UST_WAIT_FILENAME
,
497 local_apps
.wait_shm_mmap
= get_map_shm(&local_apps
);
498 if (!local_apps
.wait_shm_mmap
) {
499 WARN("Unable to get map shm for local apps. Disabling LTTng-UST per-user tracing.");
500 local_apps
.allowed
= 0;
509 * Get socket timeout, in ms.
510 * -1: wait forever. 0: don't wait. >0: timeout, in ms.
513 long get_timeout(void)
515 long constructor_delay_ms
= LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS
;
517 if (!got_timeout_env
) {
518 str_timeout
= lttng_getenv("LTTNG_UST_REGISTER_TIMEOUT");
522 constructor_delay_ms
= strtol(str_timeout
, NULL
, 10);
523 /* All negative values are considered as "-1". */
524 if (constructor_delay_ms
< -1)
525 constructor_delay_ms
= -1;
526 return constructor_delay_ms
;
529 /* Timeout for notify socket send and recv. */
531 long get_notify_sock_timeout(void)
533 return get_timeout();
536 /* Timeout for connecting to cmd and notify sockets. */
538 long get_connect_sock_timeout(void)
540 return get_timeout();
544 * Return values: -1: wait forever. 0: don't wait. 1: timeout wait.
547 int get_constructor_timeout(struct timespec
*constructor_timeout
)
549 long constructor_delay_ms
;
552 constructor_delay_ms
= get_timeout();
554 switch (constructor_delay_ms
) {
555 case -1:/* fall-through */
557 return constructor_delay_ms
;
563 * If we are unable to find the current time, don't wait.
565 ret
= clock_gettime(CLOCK_REALTIME
, constructor_timeout
);
570 constructor_timeout
->tv_sec
+= constructor_delay_ms
/ 1000UL;
571 constructor_timeout
->tv_nsec
+=
572 (constructor_delay_ms
% 1000UL) * 1000000UL;
573 if (constructor_timeout
->tv_nsec
>= 1000000000UL) {
574 constructor_timeout
->tv_sec
++;
575 constructor_timeout
->tv_nsec
-= 1000000000UL;
577 /* Timeout wait (constructor_delay_ms). */
582 int register_to_sessiond(int socket
, enum ustctl_socket_type type
)
584 return ustcomm_send_reg_msg(socket
,
587 lttng_alignof(uint8_t) * CHAR_BIT
,
588 lttng_alignof(uint16_t) * CHAR_BIT
,
589 lttng_alignof(uint32_t) * CHAR_BIT
,
590 lttng_alignof(uint64_t) * CHAR_BIT
,
591 lttng_alignof(unsigned long) * CHAR_BIT
);
595 int send_reply(int sock
, struct ustcomm_ust_reply
*lur
)
599 len
= ustcomm_send_unix_sock(sock
, lur
, sizeof(*lur
));
602 DBG("message successfully sent");
605 if (len
== -ECONNRESET
) {
606 DBG("remote end closed connection");
611 DBG("incorrect message size: %zd", len
);
617 void decrement_sem_count(unsigned int count
)
621 assert(uatomic_read(&sem_count
) >= count
);
623 if (uatomic_read(&sem_count
) <= 0) {
627 ret
= uatomic_add_return(&sem_count
, -count
);
629 ret
= sem_post(&constructor_wait
);
635 int handle_register_done(struct sock_info
*sock_info
)
637 if (sock_info
->registration_done
)
639 sock_info
->registration_done
= 1;
641 decrement_sem_count(1);
642 if (!sock_info
->statedump_pending
) {
643 sock_info
->initial_statedump_done
= 1;
644 decrement_sem_count(1);
651 int handle_register_failed(struct sock_info
*sock_info
)
653 if (sock_info
->registration_done
)
655 sock_info
->registration_done
= 1;
656 sock_info
->initial_statedump_done
= 1;
658 decrement_sem_count(2);
664 * Only execute pending statedump after the constructor semaphore has
665 * been posted by the current listener thread. This means statedump will
666 * only be performed after the "registration done" command is received
667 * from this thread's session daemon.
669 * This ensures we don't run into deadlock issues with the dynamic
670 * loader mutex, which is held while the constructor is called and
671 * waiting on the constructor semaphore. All operations requiring this
672 * dynamic loader lock need to be postponed using this mechanism.
674 * In a scenario with two session daemons connected to the application,
675 * it is possible that the first listener thread which receives the
676 * registration done command issues its statedump while the dynamic
677 * loader lock is still held by the application constructor waiting on
678 * the semaphore. It will however be allowed to proceed when the
679 * second session daemon sends the registration done command to the
680 * second listener thread. This situation therefore does not produce
684 void handle_pending_statedump(struct sock_info
*sock_info
)
686 if (sock_info
->registration_done
&& sock_info
->statedump_pending
) {
687 sock_info
->statedump_pending
= 0;
688 pthread_mutex_lock(&ust_fork_mutex
);
689 lttng_handle_pending_statedump(sock_info
);
690 pthread_mutex_unlock(&ust_fork_mutex
);
692 if (!sock_info
->initial_statedump_done
) {
693 sock_info
->initial_statedump_done
= 1;
694 decrement_sem_count(1);
700 int handle_message(struct sock_info
*sock_info
,
701 int sock
, struct ustcomm_ust_msg
*lum
)
704 const struct lttng_ust_objd_ops
*ops
;
705 struct ustcomm_ust_reply lur
;
707 char ctxstr
[LTTNG_UST_SYM_NAME_LEN
]; /* App context string. */
710 memset(&lur
, 0, sizeof(lur
));
713 ret
= -LTTNG_UST_ERR_EXITING
;
717 ops
= objd_ops(lum
->handle
);
724 case LTTNG_UST_REGISTER_DONE
:
725 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
726 ret
= handle_register_done(sock_info
);
730 case LTTNG_UST_RELEASE
:
731 if (lum
->handle
== LTTNG_UST_ROOT_HANDLE
)
734 ret
= lttng_ust_objd_unref(lum
->handle
, 1);
736 case LTTNG_UST_FILTER
:
738 /* Receive filter data */
739 struct lttng_ust_filter_bytecode_node
*bytecode
;
741 if (lum
->u
.filter
.data_size
> FILTER_BYTECODE_MAX_LEN
) {
742 ERR("Filter data size is too large: %u bytes",
743 lum
->u
.filter
.data_size
);
748 if (lum
->u
.filter
.reloc_offset
> lum
->u
.filter
.data_size
) {
749 ERR("Filter reloc offset %u is not within data",
750 lum
->u
.filter
.reloc_offset
);
755 bytecode
= zmalloc(sizeof(*bytecode
) + lum
->u
.filter
.data_size
);
760 len
= ustcomm_recv_unix_sock(sock
, bytecode
->bc
.data
,
761 lum
->u
.filter
.data_size
);
763 case 0: /* orderly shutdown */
768 if (len
== lum
->u
.filter
.data_size
) {
769 DBG("filter data received");
771 } else if (len
< 0) {
772 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
773 if (len
== -ECONNRESET
) {
774 ERR("%s remote end closed connection", sock_info
->name
);
783 DBG("incorrect filter data message size: %zd", len
);
789 bytecode
->bc
.len
= lum
->u
.filter
.data_size
;
790 bytecode
->bc
.reloc_offset
= lum
->u
.filter
.reloc_offset
;
791 bytecode
->bc
.seqnum
= lum
->u
.filter
.seqnum
;
793 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
794 (unsigned long) bytecode
,
799 /* don't free bytecode if everything went fine. */
806 case LTTNG_UST_EXCLUSION
:
808 /* Receive exclusion names */
809 struct lttng_ust_excluder_node
*node
;
812 count
= lum
->u
.exclusion
.count
;
814 /* There are no names to read */
818 node
= zmalloc(sizeof(*node
) +
819 count
* LTTNG_UST_SYM_NAME_LEN
);
824 node
->excluder
.count
= count
;
825 len
= ustcomm_recv_unix_sock(sock
, node
->excluder
.names
,
826 count
* LTTNG_UST_SYM_NAME_LEN
);
828 case 0: /* orderly shutdown */
833 if (len
== count
* LTTNG_UST_SYM_NAME_LEN
) {
834 DBG("Exclusion data received");
836 } else if (len
< 0) {
837 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
838 if (len
== -ECONNRESET
) {
839 ERR("%s remote end closed connection", sock_info
->name
);
848 DBG("Incorrect exclusion data message size: %zd", len
);
855 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
856 (unsigned long) node
,
861 /* Don't free exclusion data if everything went fine. */
868 case LTTNG_UST_CHANNEL
:
873 len
= ustcomm_recv_channel_from_sessiond(sock
,
874 &chan_data
, lum
->u
.channel
.len
,
877 case 0: /* orderly shutdown */
881 if (len
== lum
->u
.channel
.len
) {
882 DBG("channel data received");
884 } else if (len
< 0) {
885 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
886 if (len
== -ECONNRESET
) {
887 ERR("%s remote end closed connection", sock_info
->name
);
894 DBG("incorrect channel data message size: %zd", len
);
899 args
.channel
.chan_data
= chan_data
;
900 args
.channel
.wakeup_fd
= wakeup_fd
;
902 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
903 (unsigned long) &lum
->u
,
909 case LTTNG_UST_STREAM
:
911 /* Receive shm_fd, wakeup_fd */
912 ret
= ustcomm_recv_stream_from_sessiond(sock
,
915 &args
.stream
.wakeup_fd
);
921 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
922 (unsigned long) &lum
->u
,
928 case LTTNG_UST_CONTEXT
:
929 switch (lum
->u
.context
.ctx
) {
930 case LTTNG_UST_CONTEXT_APP_CONTEXT
:
933 size_t ctxlen
, recvlen
;
935 ctxlen
= strlen("$app.") + lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1
936 + strlen(":") + lum
->u
.context
.u
.app_ctx
.ctx_name_len
;
937 if (ctxlen
>= LTTNG_UST_SYM_NAME_LEN
) {
938 ERR("Application context string length size is too large: %zu bytes",
943 strcpy(ctxstr
, "$app.");
944 p
= &ctxstr
[strlen("$app.")];
945 recvlen
= ctxlen
- strlen("$app.");
946 len
= ustcomm_recv_unix_sock(sock
, p
, recvlen
);
948 case 0: /* orderly shutdown */
952 if (len
== recvlen
) {
953 DBG("app context data received");
955 } else if (len
< 0) {
956 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
957 if (len
== -ECONNRESET
) {
958 ERR("%s remote end closed connection", sock_info
->name
);
965 DBG("incorrect app context data message size: %zd", len
);
970 /* Put : between provider and ctxname. */
971 p
[lum
->u
.context
.u
.app_ctx
.provider_name_len
- 1] = ':';
972 args
.app_context
.ctxname
= ctxstr
;
979 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
980 (unsigned long) &lum
->u
,
988 ret
= ops
->cmd(lum
->handle
, lum
->cmd
,
989 (unsigned long) &lum
->u
,
996 lur
.handle
= lum
->handle
;
1000 lur
.ret_code
= LTTNG_UST_OK
;
1003 * Use -LTTNG_UST_ERR as wildcard for UST internal
1004 * error that are not caused by the transport, except if
1005 * we already have a more precise error message to
1008 if (ret
> -LTTNG_UST_ERR
) {
1009 /* Translate code to UST error. */
1012 lur
.ret_code
= -LTTNG_UST_ERR_EXIST
;
1015 lur
.ret_code
= -LTTNG_UST_ERR_INVAL
;
1018 lur
.ret_code
= -LTTNG_UST_ERR_NOENT
;
1021 lur
.ret_code
= -LTTNG_UST_ERR_PERM
;
1024 lur
.ret_code
= -LTTNG_UST_ERR_NOSYS
;
1027 lur
.ret_code
= -LTTNG_UST_ERR
;
1036 case LTTNG_UST_TRACER_VERSION
:
1037 lur
.u
.version
= lum
->u
.version
;
1039 case LTTNG_UST_TRACEPOINT_LIST_GET
:
1040 memcpy(&lur
.u
.tracepoint
, &lum
->u
.tracepoint
, sizeof(lur
.u
.tracepoint
));
1044 DBG("Return value: %d", lur
.ret_val
);
1049 * Performed delayed statedump operations outside of the UST
1050 * lock. We need to take the dynamic loader lock before we take
1051 * the UST lock internally within handle_pending_statedump().
1053 handle_pending_statedump(sock_info
);
1056 ret
= -LTTNG_UST_ERR_EXITING
;
1060 ret
= send_reply(sock
, &lur
);
1062 DBG("error sending reply");
1067 * LTTNG_UST_TRACEPOINT_FIELD_LIST_GET needs to send the field
1070 if (lur
.ret_code
== LTTNG_UST_OK
) {
1072 case LTTNG_UST_TRACEPOINT_FIELD_LIST_GET
:
1073 len
= ustcomm_send_unix_sock(sock
,
1074 &args
.field_list
.entry
,
1075 sizeof(args
.field_list
.entry
));
1080 if (len
!= sizeof(args
.field_list
.entry
)) {
1094 void cleanup_sock_info(struct sock_info
*sock_info
, int exiting
)
1098 if (sock_info
->root_handle
!= -1) {
1099 ret
= lttng_ust_objd_unref(sock_info
->root_handle
, 1);
1101 ERR("Error unref root handle");
1103 sock_info
->root_handle
= -1;
1105 sock_info
->registration_done
= 0;
1106 sock_info
->initial_statedump_done
= 0;
1109 * wait_shm_mmap, socket and notify socket are used by listener
1110 * threads outside of the ust lock, so we cannot tear them down
1111 * ourselves, because we cannot join on these threads. Leave
1112 * responsibility of cleaning up these resources to the OS
1118 if (sock_info
->socket
!= -1) {
1119 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1121 ERR("Error closing ust cmd socket");
1123 sock_info
->socket
= -1;
1125 if (sock_info
->notify_socket
!= -1) {
1126 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1128 ERR("Error closing ust notify socket");
1130 sock_info
->notify_socket
= -1;
1132 if (sock_info
->wait_shm_mmap
) {
1135 page_size
= sysconf(_SC_PAGE_SIZE
);
1136 if (page_size
<= 0) {
1140 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1142 ret
= munmap(sock_info
->wait_shm_mmap
, page_size
);
1144 ERR("Error unmapping wait shm");
1147 sock_info
->wait_shm_mmap
= NULL
;
1152 * Using fork to set umask in the child process (not multi-thread safe).
1153 * We deal with the shm_open vs ftruncate race (happening when the
1154 * sessiond owns the shm and does not let everybody modify it, to ensure
1155 * safety against shm_unlink) by simply letting the mmap fail and
1156 * retrying after a few seconds.
1157 * For global shm, everybody has rw access to it until the sessiond
1161 int get_wait_shm(struct sock_info
*sock_info
, size_t mmap_size
)
1163 int wait_shm_fd
, ret
;
1167 * Try to open read-only.
1169 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1170 if (wait_shm_fd
>= 0) {
1173 size_t bytes_read
= 0;
1176 * Try to read the fd. If unable to do so, try opening
1180 len
= read(wait_shm_fd
,
1181 &((char *) &tmp_read
)[bytes_read
],
1182 sizeof(tmp_read
) - bytes_read
);
1186 } while ((len
< 0 && errno
== EINTR
)
1187 || (len
> 0 && bytes_read
< sizeof(tmp_read
)));
1188 if (bytes_read
!= sizeof(tmp_read
)) {
1189 ret
= close(wait_shm_fd
);
1191 ERR("close wait_shm_fd");
1196 } else if (wait_shm_fd
< 0 && errno
!= ENOENT
) {
1198 * Real-only open did not work, and it's not because the
1199 * entry was not present. It's a failure that prohibits
1202 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1208 * If the open failed because the file did not exist, or because
1209 * the file was not truncated yet, try creating it ourself.
1211 URCU_TLS(lttng_ust_nest_count
)++;
1213 URCU_TLS(lttng_ust_nest_count
)--;
1218 * Parent: wait for child to return, in which case the
1219 * shared memory map will have been created.
1221 pid
= wait(&status
);
1222 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
1227 * Try to open read-only again after creation.
1229 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
, O_RDONLY
, 0);
1230 if (wait_shm_fd
< 0) {
1232 * Real-only open did not work. It's a failure
1233 * that prohibits using shm.
1235 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1239 } else if (pid
== 0) {
1243 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
;
1244 if (sock_info
->global
)
1245 create_mode
|= S_IROTH
| S_IWGRP
| S_IWOTH
;
1247 * We're alone in a child process, so we can modify the
1248 * process-wide umask.
1250 umask(~create_mode
);
1252 * Try creating shm (or get rw access).
1253 * We don't do an exclusive open, because we allow other
1254 * processes to create+ftruncate it concurrently.
1256 wait_shm_fd
= shm_open(sock_info
->wait_shm_path
,
1257 O_RDWR
| O_CREAT
, create_mode
);
1258 if (wait_shm_fd
>= 0) {
1259 ret
= ftruncate(wait_shm_fd
, mmap_size
);
1261 PERROR("ftruncate");
1262 _exit(EXIT_FAILURE
);
1264 _exit(EXIT_SUCCESS
);
1267 * For local shm, we need to have rw access to accept
1268 * opening it: this means the local sessiond will be
1269 * able to wake us up. For global shm, we open it even
1270 * if rw access is not granted, because the root.root
1271 * sessiond will be able to override all rights and wake
1274 if (!sock_info
->global
&& errno
!= EACCES
) {
1275 ERR("Error opening shm %s", sock_info
->wait_shm_path
);
1276 _exit(EXIT_FAILURE
);
1279 * The shm exists, but we cannot open it RW. Report
1282 _exit(EXIT_SUCCESS
);
1287 if (wait_shm_fd
>= 0 && !sock_info
->global
) {
1288 struct stat statbuf
;
1291 * Ensure that our user is the owner of the shm file for
1292 * local shm. If we do not own the file, it means our
1293 * sessiond will not have access to wake us up (there is
1294 * probably a rogue process trying to fake our
1295 * sessiond). Fallback to polling method in this case.
1297 ret
= fstat(wait_shm_fd
, &statbuf
);
1302 if (statbuf
.st_uid
!= getuid())
1308 ret
= close(wait_shm_fd
);
1310 PERROR("Error closing fd");
1316 char *get_map_shm(struct sock_info
*sock_info
)
1319 int wait_shm_fd
, ret
;
1320 char *wait_shm_mmap
;
1322 page_size
= sysconf(_SC_PAGE_SIZE
);
1323 if (page_size
<= 0) {
1327 PERROR("Error in sysconf(_SC_PAGE_SIZE)");
1331 lttng_ust_lock_fd_tracker();
1332 wait_shm_fd
= get_wait_shm(sock_info
, page_size
);
1333 if (wait_shm_fd
< 0) {
1334 lttng_ust_unlock_fd_tracker();
1338 ret
= lttng_ust_add_fd_to_tracker(wait_shm_fd
);
1340 ret
= close(wait_shm_fd
);
1342 PERROR("Error closing fd");
1344 lttng_ust_unlock_fd_tracker();
1349 lttng_ust_unlock_fd_tracker();
1351 wait_shm_mmap
= mmap(NULL
, page_size
, PROT_READ
,
1352 MAP_SHARED
, wait_shm_fd
, 0);
1354 /* close shm fd immediately after taking the mmap reference */
1355 lttng_ust_lock_fd_tracker();
1356 ret
= close(wait_shm_fd
);
1358 lttng_ust_delete_fd_from_tracker(wait_shm_fd
);
1360 PERROR("Error closing fd");
1362 lttng_ust_unlock_fd_tracker();
1364 if (wait_shm_mmap
== MAP_FAILED
) {
1365 DBG("mmap error (can be caused by race with sessiond). Fallback to poll mode.");
1368 return wait_shm_mmap
;
1375 void wait_for_sessiond(struct sock_info
*sock_info
)
1377 /* Use ust_lock to check if we should quit. */
1381 if (wait_poll_fallback
) {
1386 assert(sock_info
->wait_shm_mmap
);
1388 DBG("Waiting for %s apps sessiond", sock_info
->name
);
1389 /* Wait for futex wakeup */
1390 if (uatomic_read((int32_t *) sock_info
->wait_shm_mmap
))
1393 while (futex_async((int32_t *) sock_info
->wait_shm_mmap
,
1394 FUTEX_WAIT
, 0, NULL
, NULL
, 0)) {
1397 /* Value already changed. */
1400 /* Retry if interrupted by signal. */
1401 break; /* Get out of switch. */
1403 wait_poll_fallback
= 1;
1405 "Linux kernels 2.6.33 to 3.0 (with the exception of stable versions) "
1406 "do not support FUTEX_WAKE on read-only memory mappings correctly. "
1407 "Please upgrade your kernel "
1408 "(fix is commit 9ea71503a8ed9184d2d0b8ccc4d269d05f7940ae in Linux kernel "
1409 "mainline). LTTng-UST will use polling mode fallback.");
1428 * This thread does not allocate any resource, except within
1429 * handle_message, within mutex protection. This mutex protects against
1431 * The other moment it allocates resources is at socket connection, which
1432 * is also protected by the mutex.
1435 void *ust_listener_thread(void *arg
)
1437 struct sock_info
*sock_info
= arg
;
1438 int sock
, ret
, prev_connect_failed
= 0, has_waited
= 0, fd
;
1441 lttng_ust_fixup_tls();
1442 lttng_ust_fd_tracker_register_thread();
1444 * If available, add '-ust' to the end of this thread's
1447 ret
= lttng_ust_setustprocname();
1449 ERR("Unable to set UST process name");
1452 /* Restart trying to connect to the session daemon */
1454 if (prev_connect_failed
) {
1455 /* Wait for sessiond availability with pipe */
1456 wait_for_sessiond(sock_info
);
1460 * Sleep for 5 seconds before retrying after a
1461 * sequence of failure / wait / failure. This
1462 * deals with a killed or broken session daemon.
1468 prev_connect_failed
= 0;
1475 if (sock_info
->socket
!= -1) {
1476 /* FD tracker is updated by ustcomm_close_unix_sock() */
1477 ret
= ustcomm_close_unix_sock(sock_info
->socket
);
1479 ERR("Error closing %s ust cmd socket",
1482 sock_info
->socket
= -1;
1484 if (sock_info
->notify_socket
!= -1) {
1485 /* FD tracker is updated by ustcomm_close_unix_sock() */
1486 ret
= ustcomm_close_unix_sock(sock_info
->notify_socket
);
1488 ERR("Error closing %s ust notify socket",
1491 sock_info
->notify_socket
= -1;
1496 * Register. We need to perform both connect and sending
1497 * registration message before doing the next connect otherwise
1498 * we may reach unix socket connect queue max limits and block
1499 * on the 2nd connect while the session daemon is awaiting the
1500 * first connect registration message.
1502 /* Connect cmd socket */
1503 lttng_ust_lock_fd_tracker();
1504 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1505 get_connect_sock_timeout());
1507 lttng_ust_unlock_fd_tracker();
1508 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1509 prev_connect_failed
= 1;
1512 * If we cannot find the sessiond daemon, don't delay
1513 * constructor execution.
1515 ret
= handle_register_failed(sock_info
);
1521 ret
= lttng_ust_add_fd_to_tracker(fd
);
1525 PERROR("close on sock_info->socket");
1528 lttng_ust_unlock_fd_tracker();
1533 sock_info
->socket
= ret
;
1534 lttng_ust_unlock_fd_tracker();
1538 * Unlock/relock ust lock because connect is blocking (with
1539 * timeout). Don't delay constructors on the ust lock for too
1547 * Create only one root handle per listener thread for the whole
1548 * process lifetime, so we ensure we get ID which is statically
1549 * assigned to the root handle.
1551 if (sock_info
->root_handle
== -1) {
1552 ret
= lttng_abi_create_root_handle();
1554 ERR("Error creating root handle");
1557 sock_info
->root_handle
= ret
;
1560 ret
= register_to_sessiond(sock_info
->socket
, USTCTL_SOCKET_CMD
);
1562 ERR("Error registering to %s ust cmd socket",
1564 prev_connect_failed
= 1;
1566 * If we cannot register to the sessiond daemon, don't
1567 * delay constructor execution.
1569 ret
= handle_register_failed(sock_info
);
1577 * Unlock/relock ust lock because connect is blocking (with
1578 * timeout). Don't delay constructors on the ust lock for too
1585 /* Connect notify socket */
1586 lttng_ust_lock_fd_tracker();
1587 ret
= ustcomm_connect_unix_sock(sock_info
->sock_path
,
1588 get_connect_sock_timeout());
1590 lttng_ust_unlock_fd_tracker();
1591 DBG("Info: sessiond not accepting connections to %s apps socket", sock_info
->name
);
1592 prev_connect_failed
= 1;
1595 * If we cannot find the sessiond daemon, don't delay
1596 * constructor execution.
1598 ret
= handle_register_failed(sock_info
);
1605 ret
= lttng_ust_add_fd_to_tracker(fd
);
1609 PERROR("close on sock_info->notify_socket");
1612 lttng_ust_unlock_fd_tracker();
1617 sock_info
->notify_socket
= ret
;
1618 lttng_ust_unlock_fd_tracker();
1622 * Unlock/relock ust lock because connect is blocking (with
1623 * timeout). Don't delay constructors on the ust lock for too
1630 timeout
= get_notify_sock_timeout();
1633 * Give at least 10ms to sessiond to reply to
1638 ret
= ustcomm_setsockopt_rcv_timeout(sock_info
->notify_socket
,
1641 WARN("Error setting socket receive timeout");
1643 ret
= ustcomm_setsockopt_snd_timeout(sock_info
->notify_socket
,
1646 WARN("Error setting socket send timeout");
1648 } else if (timeout
< -1) {
1649 WARN("Unsupported timeout value %ld", timeout
);
1652 ret
= register_to_sessiond(sock_info
->notify_socket
,
1653 USTCTL_SOCKET_NOTIFY
);
1655 ERR("Error registering to %s ust notify socket",
1657 prev_connect_failed
= 1;
1659 * If we cannot register to the sessiond daemon, don't
1660 * delay constructor execution.
1662 ret
= handle_register_failed(sock_info
);
1667 sock
= sock_info
->socket
;
1673 struct ustcomm_ust_msg lum
;
1675 len
= ustcomm_recv_unix_sock(sock
, &lum
, sizeof(lum
));
1677 case 0: /* orderly shutdown */
1678 DBG("%s lttng-sessiond has performed an orderly shutdown", sock_info
->name
);
1683 * Either sessiond has shutdown or refused us by closing the socket.
1684 * In either case, we don't want to delay construction execution,
1685 * and we need to wait before retry.
1687 prev_connect_failed
= 1;
1689 * If we cannot register to the sessiond daemon, don't
1690 * delay constructor execution.
1692 ret
= handle_register_failed(sock_info
);
1697 print_cmd(lum
.cmd
, lum
.handle
);
1698 ret
= handle_message(sock_info
, sock
, &lum
);
1700 ERR("Error handling message for %s socket",
1703 * Close socket if protocol error is
1711 DBG("Receive failed from lttng-sessiond with errno %d", (int) -len
);
1713 DBG("incorrect message size (%s socket): %zd", sock_info
->name
, len
);
1715 if (len
== -ECONNRESET
) {
1716 DBG("%s remote end closed connection", sock_info
->name
);
1727 /* Cleanup socket handles before trying to reconnect */
1728 lttng_ust_objd_table_owner_cleanup(sock_info
);
1730 goto restart
; /* try to reconnect */
1735 pthread_mutex_lock(&ust_exit_mutex
);
1736 sock_info
->thread_active
= 0;
1737 pthread_mutex_unlock(&ust_exit_mutex
);
1742 * Weak symbol to call when the ust malloc wrapper is not loaded.
1744 __attribute__((weak
))
1745 void lttng_ust_malloc_wrapper_init(void)
1750 * sessiond monitoring thread: monitor presence of global and per-user
1751 * sessiond by polling the application common named pipe.
1753 void __attribute__((constructor
)) lttng_ust_init(void)
1755 struct timespec constructor_timeout
;
1756 sigset_t sig_all_blocked
, orig_parent_mask
;
1757 pthread_attr_t thread_attr
;
1761 if (uatomic_xchg(&initialized
, 1) == 1)
1765 * Fixup interdependency between TLS fixup mutex (which happens
1766 * to be the dynamic linker mutex) and ust_lock, taken within
1769 lttng_ust_fixup_tls();
1771 lttng_ust_loaded
= 1;
1774 * We want precise control over the order in which we construct
1775 * our sub-libraries vs starting to receive commands from
1776 * sessiond (otherwise leading to errors when trying to create
1777 * sessiond before the init functions are completed).
1780 lttng_ust_getenv_init(); /* Needs init_usterr() to be completed. */
1782 lttng_ust_init_fd_tracker();
1783 lttng_ust_clock_init();
1784 lttng_ust_getcpu_init();
1785 lttng_ust_statedump_init();
1786 lttng_ring_buffer_metadata_client_init();
1787 lttng_ring_buffer_client_overwrite_init();
1788 lttng_ring_buffer_client_overwrite_rt_init();
1789 lttng_ring_buffer_client_discard_init();
1790 lttng_ring_buffer_client_discard_rt_init();
1791 lttng_perf_counter_init();
1793 * Invoke ust malloc wrapper init before starting other threads.
1795 lttng_ust_malloc_wrapper_init();
1797 timeout_mode
= get_constructor_timeout(&constructor_timeout
);
1799 ret
= sem_init(&constructor_wait
, 0, 0);
1804 ret
= setup_global_apps();
1806 assert(global_apps
.allowed
== 0);
1807 DBG("global apps setup returned %d", ret
);
1810 ret
= setup_local_apps();
1812 assert(local_apps
.allowed
== 0);
1813 DBG("local apps setup returned %d", ret
);
1816 /* A new thread created by pthread_create inherits the signal mask
1817 * from the parent. To avoid any signal being received by the
1818 * listener thread, we block all signals temporarily in the parent,
1819 * while we create the listener thread.
1821 sigfillset(&sig_all_blocked
);
1822 ret
= pthread_sigmask(SIG_SETMASK
, &sig_all_blocked
, &orig_parent_mask
);
1824 ERR("pthread_sigmask: %s", strerror(ret
));
1827 ret
= pthread_attr_init(&thread_attr
);
1829 ERR("pthread_attr_init: %s", strerror(ret
));
1831 ret
= pthread_attr_setdetachstate(&thread_attr
, PTHREAD_CREATE_DETACHED
);
1833 ERR("pthread_attr_setdetachstate: %s", strerror(ret
));
1836 if (global_apps
.allowed
) {
1837 pthread_mutex_lock(&ust_exit_mutex
);
1838 ret
= pthread_create(&global_apps
.ust_listener
, &thread_attr
,
1839 ust_listener_thread
, &global_apps
);
1841 ERR("pthread_create global: %s", strerror(ret
));
1843 global_apps
.thread_active
= 1;
1844 pthread_mutex_unlock(&ust_exit_mutex
);
1846 handle_register_done(&global_apps
);
1849 if (local_apps
.allowed
) {
1850 pthread_mutex_lock(&ust_exit_mutex
);
1851 ret
= pthread_create(&local_apps
.ust_listener
, &thread_attr
,
1852 ust_listener_thread
, &local_apps
);
1854 ERR("pthread_create local: %s", strerror(ret
));
1856 local_apps
.thread_active
= 1;
1857 pthread_mutex_unlock(&ust_exit_mutex
);
1859 handle_register_done(&local_apps
);
1861 ret
= pthread_attr_destroy(&thread_attr
);
1863 ERR("pthread_attr_destroy: %s", strerror(ret
));
1866 /* Restore original signal mask in parent */
1867 ret
= pthread_sigmask(SIG_SETMASK
, &orig_parent_mask
, NULL
);
1869 ERR("pthread_sigmask: %s", strerror(ret
));
1872 switch (timeout_mode
) {
1873 case 1: /* timeout wait */
1875 ret
= sem_timedwait(&constructor_wait
,
1876 &constructor_timeout
);
1877 } while (ret
< 0 && errno
== EINTR
);
1881 ERR("Timed out waiting for lttng-sessiond");
1884 PERROR("sem_timedwait");
1887 ERR("Unexpected error \"%s\" returned by sem_timedwait",
1892 case -1:/* wait forever */
1894 ret
= sem_wait(&constructor_wait
);
1895 } while (ret
< 0 && errno
== EINTR
);
1902 ERR("Unexpected error \"%s\" returned by sem_wait",
1907 case 0: /* no timeout */
1913 void lttng_ust_cleanup(int exiting
)
1915 cleanup_sock_info(&global_apps
, exiting
);
1916 cleanup_sock_info(&local_apps
, exiting
);
1917 local_apps
.allowed
= 0;
1918 global_apps
.allowed
= 0;
1920 * The teardown in this function all affect data structures
1921 * accessed under the UST lock by the listener thread. This
1922 * lock, along with the lttng_ust_comm_should_quit flag, ensure
1923 * that none of these threads are accessing this data at this
1926 lttng_ust_abi_exit();
1927 lttng_ust_events_exit();
1928 lttng_perf_counter_exit();
1929 lttng_ring_buffer_client_discard_rt_exit();
1930 lttng_ring_buffer_client_discard_exit();
1931 lttng_ring_buffer_client_overwrite_rt_exit();
1932 lttng_ring_buffer_client_overwrite_exit();
1933 lttng_ring_buffer_metadata_client_exit();
1934 lttng_ust_statedump_destroy();
1937 /* Reinitialize values for fork */
1938 sem_count
= sem_count_initial_value
;
1939 lttng_ust_comm_should_quit
= 0;
1944 void __attribute__((destructor
)) lttng_ust_exit(void)
1949 * Using pthread_cancel here because:
1950 * A) we don't want to hang application teardown.
1951 * B) the thread is not allocating any resource.
1955 * Require the communication thread to quit. Synchronize with
1956 * mutexes to ensure it is not in a mutex critical section when
1957 * pthread_cancel is later called.
1960 lttng_ust_comm_should_quit
= 1;
1963 pthread_mutex_lock(&ust_exit_mutex
);
1964 /* cancel threads */
1965 if (global_apps
.thread_active
) {
1966 ret
= pthread_cancel(global_apps
.ust_listener
);
1968 ERR("Error cancelling global ust listener thread: %s",
1971 global_apps
.thread_active
= 0;
1974 if (local_apps
.thread_active
) {
1975 ret
= pthread_cancel(local_apps
.ust_listener
);
1977 ERR("Error cancelling local ust listener thread: %s",
1980 local_apps
.thread_active
= 0;
1983 pthread_mutex_unlock(&ust_exit_mutex
);
1986 * Do NOT join threads: use of sys_futex makes it impossible to
1987 * join the threads without using async-cancel, but async-cancel
1988 * is delivered by a signal, which could hit the target thread
1989 * anywhere in its code path, including while the ust_lock() is
1990 * held, causing a deadlock for the other thread. Let the OS
1991 * cleanup the threads if there are stalled in a syscall.
1993 lttng_ust_cleanup(1);
1997 * We exclude the worker threads across fork and clone (except
1998 * CLONE_VM), because these system calls only keep the forking thread
1999 * running in the child. Therefore, we don't want to call fork or clone
2000 * in the middle of an tracepoint or ust tracing state modification.
2001 * Holding this mutex protects these structures across fork and clone.
2003 void ust_before_fork(sigset_t
*save_sigset
)
2006 * Disable signals. This is to avoid that the child intervenes
2007 * before it is properly setup for tracing. It is safer to
2008 * disable all signals, because then we know we are not breaking
2009 * anything by restoring the original mask.
2014 /* Fixup lttng-ust TLS. */
2015 lttng_ust_fixup_tls();
2017 if (URCU_TLS(lttng_ust_nest_count
))
2019 /* Disable signals */
2020 sigfillset(&all_sigs
);
2021 ret
= sigprocmask(SIG_BLOCK
, &all_sigs
, save_sigset
);
2023 PERROR("sigprocmask");
2026 pthread_mutex_lock(&ust_fork_mutex
);
2029 rcu_bp_before_fork();
2030 lttng_ust_lock_fd_tracker();
2033 static void ust_after_fork_common(sigset_t
*restore_sigset
)
2037 DBG("process %d", getpid());
2038 lttng_ust_unlock_fd_tracker();
2041 pthread_mutex_unlock(&ust_fork_mutex
);
2043 /* Restore signals */
2044 ret
= sigprocmask(SIG_SETMASK
, restore_sigset
, NULL
);
2046 PERROR("sigprocmask");
2050 void ust_after_fork_parent(sigset_t
*restore_sigset
)
2052 if (URCU_TLS(lttng_ust_nest_count
))
2054 DBG("process %d", getpid());
2055 rcu_bp_after_fork_parent();
2056 /* Release mutexes and reenable signals */
2057 ust_after_fork_common(restore_sigset
);
2061 * After fork, in the child, we need to cleanup all the leftover state,
2062 * except the worker thread which already magically disappeared thanks
2063 * to the weird Linux fork semantics. After tyding up, we call
2064 * lttng_ust_init() again to start over as a new PID.
2066 * This is meant for forks() that have tracing in the child between the
2067 * fork and following exec call (if there is any).
2069 void ust_after_fork_child(sigset_t
*restore_sigset
)
2071 if (URCU_TLS(lttng_ust_nest_count
))
2073 lttng_context_vpid_reset();
2074 lttng_context_vtid_reset();
2075 lttng_context_procname_reset();
2076 DBG("process %d", getpid());
2077 /* Release urcu mutexes */
2078 rcu_bp_after_fork_child();
2079 lttng_ust_cleanup(0);
2080 /* Release mutexes and reenable signals */
2081 ust_after_fork_common(restore_sigset
);
2085 void lttng_ust_sockinfo_session_enabled(void *owner
)
2087 struct sock_info
*sock_info
= owner
;
2088 sock_info
->statedump_pending
= 1;