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 |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 10 | #include <sys/prctl.h> |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 11 | #include <unistd.h> |
| 12 | |
Stephan Pleines | 0960c26 | 2024-05-31 20:29:24 -0700 | [diff] [blame] | 13 | #include <algorithm> |
| 14 | #include <cstddef> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 15 | #include <cstring> |
Stephan Pleines | 0960c26 | 2024-05-31 20:29:24 -0700 | [diff] [blame] | 16 | #include <limits> |
| 17 | #include <optional> |
| 18 | #include <ostream> |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 19 | #include <string> |
| 20 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 21 | extern char *program_invocation_name; |
| 22 | extern char *program_invocation_short_name; |
| 23 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 24 | #include "absl/log/check.h" |
| 25 | #include "absl/log/log.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 26 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 27 | namespace aos::logging::internal { |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 28 | namespace { |
| 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) { |
Austin Schuh | 5619643 | 2020-10-24 20:15:21 -0700 | [diff] [blame] | 43 | PLOG(FATAL) << "prctl(PR_GET_NAME, " << thread_name_array << ") failed"; |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 44 | } |
Austin Schuh | 8d2e3fa | 2020-09-10 23:01:55 -0700 | [diff] [blame] | 45 | #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 Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 49 | 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 | |
Tyler Chatow | 5bc9d68 | 2022-02-25 21:27:16 -0800 | [diff] [blame] | 64 | thread_local std::optional<Context> my_context; |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 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. |
Austin Schuh | f7bfb65 | 2023-08-25 14:22:50 -0700 | [diff] [blame] | 70 | thread_local bool delete_current_context(false); |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 71 | |
| 72 | } // namespace |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 73 | |
Austin Schuh | 5619643 | 2020-10-24 20:15:21 -0700 | [diff] [blame] | 74 | Context::Context() : sequence(0) {} |
Austin Schuh | 044e18b | 2015-10-21 20:17:09 -0700 | [diff] [blame] | 75 | |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 76 | // Used in aos/linux_code/init.cc when a thread's name is changed. |
| 77 | void ReloadThreadName() { |
Tyler Chatow | 5bc9d68 | 2022-02-25 21:27:16 -0800 | [diff] [blame] | 78 | if (my_context.has_value()) { |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 79 | my_context->ClearName(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void Context::ClearName() { name_size = std::numeric_limits<size_t>::max(); } |
| 84 | |
| 85 | std::string_view Context::MyName() { |
| 86 | if (name_size == std::numeric_limits<size_t>::max()) { |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 87 | ::std::string my_name = GetMyName(); |
Austin Schuh | 7bb558e | 2023-08-30 20:50:51 -0700 | [diff] [blame] | 88 | CHECK_LE(my_name.size() + 1, sizeof(Context::name)) |
| 89 | << ": process/thread name '" << my_name << "' is too long"; |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 90 | strcpy(name, my_name.c_str()); |
| 91 | name_size = my_name.size(); |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 92 | } |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 93 | |
| 94 | return std::string_view(&name[0], name_size); |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | Context *Context::Get() { |
| 98 | if (__builtin_expect(delete_current_context, false)) { |
Tyler Chatow | 5bc9d68 | 2022-02-25 21:27:16 -0800 | [diff] [blame] | 99 | my_context.reset(); |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 100 | delete_current_context = false; |
| 101 | } |
Tyler Chatow | 5bc9d68 | 2022-02-25 21:27:16 -0800 | [diff] [blame] | 102 | if (__builtin_expect(!my_context.has_value(), false)) { |
| 103 | my_context.emplace(); |
Austin Schuh | ad9e5eb | 2021-11-19 20:33:55 -0800 | [diff] [blame] | 104 | my_context->ClearName(); |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 105 | my_context->source = getpid(); |
| 106 | } |
Tyler Chatow | 5bc9d68 | 2022-02-25 21:27:16 -0800 | [diff] [blame] | 107 | return &*my_context; |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 108 | } |
| 109 | |
Austin Schuh | 8d2e3fa | 2020-09-10 23:01:55 -0700 | [diff] [blame] | 110 | void Context::Delete() { delete_current_context = true; } |
Brian Silverman | cb5da1f | 2015-12-05 22:19:58 -0500 | [diff] [blame] | 111 | |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 112 | void Context::DeleteNow() { |
Tyler Chatow | 5bc9d68 | 2022-02-25 21:27:16 -0800 | [diff] [blame] | 113 | my_context.reset(); |
Austin Schuh | a0c41ba | 2020-09-10 22:59:14 -0700 | [diff] [blame] | 114 | delete_current_context = false; |
| 115 | } |
| 116 | |
Stephan Pleines | f63bde8 | 2024-01-13 15:59:33 -0800 | [diff] [blame] | 117 | } // namespace aos::logging::internal |