John Park | 33858a3 | 2018-09-28 23:05:48 -0700 | [diff] [blame] | 1 | #include "aos/logging/context.h" |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 2 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 3 | #ifndef _GNU_SOURCE |
| 4 | #define _GNU_SOURCE /* See feature_test_macros(7) */ |
| 5 | #endif |
| 6 | |
Austin Schuh | 8d2e3fa | 2020-09-10 23:01:55 -0700 | [diff] [blame] | 7 | #if __has_feature(memory_sanitizer) |
| 8 | #include <sanitizer/msan_interface.h> |
| 9 | #endif |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 10 | #include <string.h> |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 11 | #include <sys/prctl.h> |
| 12 | #include <sys/types.h> |
| 13 | #include <unistd.h> |
| 14 | |
| 15 | #include <string> |
| 16 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | |
| 19 | extern char *program_invocation_name; |
| 20 | extern char *program_invocation_short_name; |
| 21 | |
John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 22 | #include "aos/complex_thread_local.h" |
Austin Schuh | 8d2e3fa | 2020-09-10 23:01:55 -0700 | [diff] [blame] | 23 | #include "aos/die.h" |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 24 | #include "aos/logging/implementations.h" |
Brian Silverman | b47f555 | 2020-10-01 15:08:14 -0700 | [diff] [blame^] | 25 | #include "aos/thread_local.h" |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 26 | |
| 27 | namespace aos { |
| 28 | namespace logging { |
| 29 | namespace internal { |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 30 | namespace { |
| 31 | |
| 32 | // TODO(brians): Differentiate between threads with the same name in the same |
| 33 | // process. |
| 34 | |
| 35 | ::std::string GetMyName() { |
| 36 | // The maximum number of characters that can make up a thread name. |
| 37 | // The docs are unclear if it can be 16 characters with no '\0', so we'll be |
| 38 | // safe by adding our own where necessary. |
| 39 | static const size_t kThreadNameLength = 16; |
| 40 | |
| 41 | ::std::string process_name(program_invocation_short_name); |
| 42 | |
| 43 | char thread_name_array[kThreadNameLength + 1]; |
| 44 | if (prctl(PR_GET_NAME, thread_name_array) != 0) { |
| 45 | PDie("prctl(PR_GET_NAME, %p) failed", thread_name_array); |
| 46 | } |
Austin Schuh | 8d2e3fa | 2020-09-10 23:01:55 -0700 | [diff] [blame] | 47 | #if __has_feature(memory_sanitizer) |
| 48 | // msan doesn't understand PR_GET_NAME, so help it along. |
| 49 | __msan_unpoison(thread_name_array, sizeof(thread_name_array)); |
| 50 | #endif |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 51 | thread_name_array[sizeof(thread_name_array) - 1] = '\0'; |
| 52 | ::std::string thread_name(thread_name_array); |
| 53 | |
| 54 | // If the first bunch of characters are the same. |
| 55 | // We cut off comparing at the shorter of the 2 strings because one or the |
| 56 | // other often ends up cut off. |
| 57 | if (strncmp(thread_name.c_str(), process_name.c_str(), |
| 58 | ::std::min(thread_name.length(), process_name.length())) == 0) { |
| 59 | // This thread doesn't have an actual name. |
| 60 | return process_name; |
| 61 | } |
| 62 | |
| 63 | return process_name + '.' + thread_name; |
| 64 | } |
| 65 | |
| 66 | ::aos::ComplexThreadLocal<Context> my_context; |
| 67 | |
| 68 | // True if we're going to delete the current Context object ASAP. The |
| 69 | // reason for doing this instead of just deleting them is that tsan (at least) |
| 70 | // doesn't like it when pthread_atfork handlers do complicated stuff and it's |
| 71 | // not a great idea anyways. |
Brian Silverman | b47f555 | 2020-10-01 15:08:14 -0700 | [diff] [blame^] | 72 | AOS_THREAD_LOCAL bool delete_current_context(false); |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 73 | |
| 74 | } // namespace |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 75 | |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 76 | Context::Context() : implementation(GetImplementation()), sequence(0) { |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 77 | cork_data.Reset(); |
| 78 | } |
| 79 | |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 80 | // Used in aos/linux_code/init.cc when a thread's name is changed. |
| 81 | void ReloadThreadName() { |
| 82 | if (my_context.created()) { |
| 83 | ::std::string my_name = GetMyName(); |
| 84 | if (my_name.size() + 1 > sizeof(Context::name)) { |
Austin Schuh | 8d2e3fa | 2020-09-10 23:01:55 -0700 | [diff] [blame] | 85 | Die("logging: process/thread name '%s' is too long\n", my_name.c_str()); |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 86 | } |
| 87 | strcpy(my_context->name, my_name.c_str()); |
| 88 | my_context->name_size = my_name.size(); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | Context *Context::Get() { |
| 93 | if (__builtin_expect(delete_current_context, false)) { |
| 94 | my_context.Clear(); |
| 95 | delete_current_context = false; |
| 96 | } |
| 97 | if (__builtin_expect(!my_context.created(), false)) { |
| 98 | my_context.Create(); |
| 99 | ReloadThreadName(); |
| 100 | my_context->source = getpid(); |
| 101 | } |
| 102 | return my_context.get(); |
| 103 | } |
| 104 | |
Austin Schuh | 8d2e3fa | 2020-09-10 23:01:55 -0700 | [diff] [blame] | 105 | void Context::Delete() { delete_current_context = true; } |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 106 | |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 107 | void Context::DeleteNow() { |
| 108 | my_context.Clear(); |
| 109 | delete_current_context = false; |
| 110 | } |
| 111 | |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 112 | } // namespace internal |
| 113 | } // namespace logging |
| 114 | } // namespace aos |