blob: 84e4e80cc85e2260cd15cd278409bd35334f3982 [file] [log] [blame]
John Park33858a32018-09-28 23:05:48 -07001#include "aos/logging/context.h"
Austin Schuh044e18b2015-10-21 20:17:09 -07002
Alex Perrycb7da4b2019-08-28 19:35:56 -07003#ifndef _GNU_SOURCE
4#define _GNU_SOURCE /* See feature_test_macros(7) */
5#endif
6
Austin Schuh8d2e3fa2020-09-10 23:01:55 -07007#if __has_feature(memory_sanitizer)
8#include <sanitizer/msan_interface.h>
9#endif
Austin Schuh044e18b2015-10-21 20:17:09 -070010#include <string.h>
Brian Silvermancb5da1f2015-12-05 22:19:58 -050011#include <sys/prctl.h>
12#include <sys/types.h>
13#include <unistd.h>
14
15#include <string>
16
Alex Perrycb7da4b2019-08-28 19:35:56 -070017#include <errno.h>
18
19extern char *program_invocation_name;
20extern char *program_invocation_short_name;
21
John Park398c74a2018-10-20 21:17:39 -070022#include "aos/complex_thread_local.h"
Austin Schuh8d2e3fa2020-09-10 23:01:55 -070023#include "aos/die.h"
Austin Schuh044e18b2015-10-21 20:17:09 -070024
25namespace aos {
26namespace logging {
27namespace internal {
Brian Silvermancb5da1f2015-12-05 22:19:58 -050028namespace {
29
30// TODO(brians): Differentiate between threads with the same name in the same
31// process.
32
33::std::string GetMyName() {
34 // The maximum number of characters that can make up a thread name.
35 // The docs are unclear if it can be 16 characters with no '\0', so we'll be
36 // safe by adding our own where necessary.
37 static const size_t kThreadNameLength = 16;
38
39 ::std::string process_name(program_invocation_short_name);
40
41 char thread_name_array[kThreadNameLength + 1];
42 if (prctl(PR_GET_NAME, thread_name_array) != 0) {
43 PDie("prctl(PR_GET_NAME, %p) failed", thread_name_array);
44 }
Austin Schuh8d2e3fa2020-09-10 23:01:55 -070045#if __has_feature(memory_sanitizer)
46 // msan doesn't understand PR_GET_NAME, so help it along.
47 __msan_unpoison(thread_name_array, sizeof(thread_name_array));
48#endif
Brian Silvermancb5da1f2015-12-05 22:19:58 -050049 thread_name_array[sizeof(thread_name_array) - 1] = '\0';
50 ::std::string thread_name(thread_name_array);
51
52 // If the first bunch of characters are the same.
53 // We cut off comparing at the shorter of the 2 strings because one or the
54 // other often ends up cut off.
55 if (strncmp(thread_name.c_str(), process_name.c_str(),
56 ::std::min(thread_name.length(), process_name.length())) == 0) {
57 // This thread doesn't have an actual name.
58 return process_name;
59 }
60
61 return process_name + '.' + thread_name;
62}
63
64::aos::ComplexThreadLocal<Context> my_context;
65
66// True if we're going to delete the current Context object ASAP. The
67// reason for doing this instead of just deleting them is that tsan (at least)
68// doesn't like it when pthread_atfork handlers do complicated stuff and it's
69// not a great idea anyways.
70thread_local bool delete_current_context(false);
71
72} // namespace
Austin Schuh044e18b2015-10-21 20:17:09 -070073
74::std::atomic<LogImplementation *> global_top_implementation(NULL);
75
76Context::Context()
Austin Schuh8d2e3fa2020-09-10 23:01:55 -070077 : implementation(global_top_implementation.load()), sequence(0) {
Austin Schuh044e18b2015-10-21 20:17:09 -070078 cork_data.Reset();
79}
80
Brian Silvermancb5da1f2015-12-05 22:19:58 -050081// Used in aos/linux_code/init.cc when a thread's name is changed.
82void ReloadThreadName() {
83 if (my_context.created()) {
84 ::std::string my_name = GetMyName();
85 if (my_name.size() + 1 > sizeof(Context::name)) {
Austin Schuh8d2e3fa2020-09-10 23:01:55 -070086 Die("logging: process/thread name '%s' is too long\n", my_name.c_str());
Brian Silvermancb5da1f2015-12-05 22:19:58 -050087 }
88 strcpy(my_context->name, my_name.c_str());
89 my_context->name_size = my_name.size();
90 }
91}
92
93Context *Context::Get() {
94 if (__builtin_expect(delete_current_context, false)) {
95 my_context.Clear();
96 delete_current_context = false;
97 }
98 if (__builtin_expect(!my_context.created(), false)) {
99 my_context.Create();
100 ReloadThreadName();
101 my_context->source = getpid();
102 }
103 return my_context.get();
104}
105
Austin Schuh8d2e3fa2020-09-10 23:01:55 -0700106void Context::Delete() { delete_current_context = true; }
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500107
Austin Schuh044e18b2015-10-21 20:17:09 -0700108} // namespace internal
109} // namespace logging
110} // namespace aos