blob: 02ad3ebed8517c1185cec074d8a475b66d0440c7 [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
Brian Silvermancb5da1f2015-12-05 22:19:58 -050010#include <sys/prctl.h>
11#include <sys/types.h>
12#include <unistd.h>
13
Tyler Chatowbf0609c2021-07-31 16:13:27 -070014#include <cerrno>
15#include <cstring>
Brian Silvermancb5da1f2015-12-05 22:19:58 -050016#include <string>
17
Alex Perrycb7da4b2019-08-28 19:35:56 -070018extern char *program_invocation_name;
19extern char *program_invocation_short_name;
20
Philipp Schrader790cb542023-07-05 21:06:52 -070021#include "glog/logging.h"
22
Austin Schuh8d2e3fa2020-09-10 23:01:55 -070023#include "aos/die.h"
Austin Schuha0c41ba2020-09-10 22:59:14 -070024#include "aos/logging/implementations.h"
Brian Silvermanb47f5552020-10-01 15:08:14 -070025#include "aos/thread_local.h"
Austin Schuh044e18b2015-10-21 20:17:09 -070026
27namespace aos {
28namespace logging {
29namespace internal {
Brian Silvermancb5da1f2015-12-05 22:19:58 -050030namespace {
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) {
Austin Schuh56196432020-10-24 20:15:21 -070045 PLOG(FATAL) << "prctl(PR_GET_NAME, " << thread_name_array << ") failed";
Brian Silvermancb5da1f2015-12-05 22:19:58 -050046 }
Austin Schuh8d2e3fa2020-09-10 23:01:55 -070047#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 Silvermancb5da1f2015-12-05 22:19:58 -050051 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
Tyler Chatow5bc9d682022-02-25 21:27:16 -080066thread_local std::optional<Context> my_context;
Brian Silvermancb5da1f2015-12-05 22:19:58 -050067
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 Silvermanb47f5552020-10-01 15:08:14 -070072AOS_THREAD_LOCAL bool delete_current_context(false);
Brian Silvermancb5da1f2015-12-05 22:19:58 -050073
74} // namespace
Austin Schuh044e18b2015-10-21 20:17:09 -070075
Austin Schuh56196432020-10-24 20:15:21 -070076Context::Context() : sequence(0) {}
Austin Schuh044e18b2015-10-21 20:17:09 -070077
Brian Silvermancb5da1f2015-12-05 22:19:58 -050078// Used in aos/linux_code/init.cc when a thread's name is changed.
79void ReloadThreadName() {
Tyler Chatow5bc9d682022-02-25 21:27:16 -080080 if (my_context.has_value()) {
Austin Schuhad9e5eb2021-11-19 20:33:55 -080081 my_context->ClearName();
82 }
83}
84
85void Context::ClearName() { name_size = std::numeric_limits<size_t>::max(); }
86
87std::string_view Context::MyName() {
88 if (name_size == std::numeric_limits<size_t>::max()) {
Brian Silvermancb5da1f2015-12-05 22:19:58 -050089 ::std::string my_name = GetMyName();
90 if (my_name.size() + 1 > sizeof(Context::name)) {
Austin Schuh8d2e3fa2020-09-10 23:01:55 -070091 Die("logging: process/thread name '%s' is too long\n", my_name.c_str());
Brian Silvermancb5da1f2015-12-05 22:19:58 -050092 }
Austin Schuhad9e5eb2021-11-19 20:33:55 -080093 strcpy(name, my_name.c_str());
94 name_size = my_name.size();
Brian Silvermancb5da1f2015-12-05 22:19:58 -050095 }
Austin Schuhad9e5eb2021-11-19 20:33:55 -080096
97 return std::string_view(&name[0], name_size);
Brian Silvermancb5da1f2015-12-05 22:19:58 -050098}
99
100Context *Context::Get() {
101 if (__builtin_expect(delete_current_context, false)) {
Tyler Chatow5bc9d682022-02-25 21:27:16 -0800102 my_context.reset();
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500103 delete_current_context = false;
104 }
Tyler Chatow5bc9d682022-02-25 21:27:16 -0800105 if (__builtin_expect(!my_context.has_value(), false)) {
106 my_context.emplace();
Austin Schuhad9e5eb2021-11-19 20:33:55 -0800107 my_context->ClearName();
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500108 my_context->source = getpid();
109 }
Tyler Chatow5bc9d682022-02-25 21:27:16 -0800110 return &*my_context;
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500111}
112
Austin Schuh8d2e3fa2020-09-10 23:01:55 -0700113void Context::Delete() { delete_current_context = true; }
Brian Silvermancb5da1f2015-12-05 22:19:58 -0500114
Austin Schuha0c41ba2020-09-10 22:59:14 -0700115void Context::DeleteNow() {
Tyler Chatow5bc9d682022-02-25 21:27:16 -0800116 my_context.reset();
Austin Schuha0c41ba2020-09-10 22:59:14 -0700117 delete_current_context = false;
118}
119
Austin Schuh044e18b2015-10-21 20:17:09 -0700120} // namespace internal
121} // namespace logging
122} // namespace aos