Brian Silverman | b089388 | 2014-02-10 14:48:30 -0800 | [diff] [blame] | 1 | #include "aos/linux_code/logging/linux_logging.h" |
| 2 | |
| 3 | #include <sys/prctl.h> |
| 4 | |
Brian Silverman | 17311c9 | 2014-09-06 00:48:59 -0400 | [diff] [blame] | 5 | #include "aos/linux_code/complex_thread_local.h" |
Brian Silverman | b089388 | 2014-02-10 14:48:30 -0800 | [diff] [blame] | 6 | #include "aos/linux_code/thread_local.h" |
| 7 | #include "aos/common/die.h" |
| 8 | |
| 9 | namespace aos { |
| 10 | namespace logging { |
| 11 | namespace internal { |
| 12 | namespace { |
| 13 | |
Brian Silverman | 2fe007c | 2014-12-28 12:20:01 -0800 | [diff] [blame] | 14 | // TODO(brians): Differentiate between threads with the same name in the same |
| 15 | // process. |
Brian Silverman | 8bd3153 | 2014-09-05 11:40:55 -0400 | [diff] [blame] | 16 | |
Brian Silverman | b089388 | 2014-02-10 14:48:30 -0800 | [diff] [blame] | 17 | ::std::string GetMyName() { |
| 18 | // The maximum number of characters that can make up a thread name. |
| 19 | // The docs are unclear if it can be 16 characters with no '\0', so we'll be |
| 20 | // safe by adding our own where necessary. |
| 21 | static const size_t kThreadNameLength = 16; |
| 22 | |
| 23 | ::std::string process_name(program_invocation_short_name); |
| 24 | |
| 25 | char thread_name_array[kThreadNameLength + 1]; |
| 26 | if (prctl(PR_GET_NAME, thread_name_array) != 0) { |
Brian Silverman | 01be000 | 2014-05-10 15:44:38 -0700 | [diff] [blame] | 27 | PDie("prctl(PR_GET_NAME, %p) failed", thread_name_array); |
Brian Silverman | b089388 | 2014-02-10 14:48:30 -0800 | [diff] [blame] | 28 | } |
| 29 | thread_name_array[sizeof(thread_name_array) - 1] = '\0'; |
| 30 | ::std::string thread_name(thread_name_array); |
| 31 | |
| 32 | // If the first bunch of characters are the same. |
| 33 | // We cut off comparing at the shorter of the 2 strings because one or the |
| 34 | // other often ends up cut off. |
| 35 | if (strncmp(thread_name.c_str(), process_name.c_str(), |
| 36 | ::std::min(thread_name.length(), process_name.length())) == 0) { |
| 37 | // This thread doesn't have an actual name. |
| 38 | return process_name; |
| 39 | } |
| 40 | |
| 41 | return process_name + '.' + thread_name; |
| 42 | } |
| 43 | |
Brian Silverman | 17311c9 | 2014-09-06 00:48:59 -0400 | [diff] [blame] | 44 | ::aos::ComplexThreadLocal<Context> my_context; |
Brian Silverman | 43d57d1 | 2014-05-20 14:04:07 -0700 | [diff] [blame] | 45 | |
Brian Silverman | 17311c9 | 2014-09-06 00:48:59 -0400 | [diff] [blame] | 46 | // True if we're going to delete the current Context object ASAP. The |
Brian Silverman | 43d57d1 | 2014-05-20 14:04:07 -0700 | [diff] [blame] | 47 | // reason for doing this instead of just deleting them is that tsan (at least) |
| 48 | // doesn't like it when pthread_atfork handlers do complicated stuff and it's |
| 49 | // not a great idea anyways. |
Brian Silverman | 17311c9 | 2014-09-06 00:48:59 -0400 | [diff] [blame] | 50 | AOS_THREAD_LOCAL bool delete_current_context(false); |
Brian Silverman | b089388 | 2014-02-10 14:48:30 -0800 | [diff] [blame] | 51 | |
| 52 | } // namespace |
| 53 | |
Brian Silverman | 2fe007c | 2014-12-28 12:20:01 -0800 | [diff] [blame] | 54 | // Used in aos/linux_code/init.cc when a thread's name is changed. |
| 55 | void ReloadThreadName() { |
| 56 | if (my_context.created()) { |
Austin Schuh | aebbc34 | 2015-01-25 02:25:13 -0800 | [diff] [blame^] | 57 | ::std::string my_name = GetMyName(); |
| 58 | if (my_name.size() + 1 > sizeof(Context::name)) { |
Brian Silverman | 2fe007c | 2014-12-28 12:20:01 -0800 | [diff] [blame] | 59 | Die("logging: process/thread name '%s' is too long\n", |
Austin Schuh | aebbc34 | 2015-01-25 02:25:13 -0800 | [diff] [blame^] | 60 | my_name.c_str()); |
Brian Silverman | 2fe007c | 2014-12-28 12:20:01 -0800 | [diff] [blame] | 61 | } |
Austin Schuh | aebbc34 | 2015-01-25 02:25:13 -0800 | [diff] [blame^] | 62 | strcpy(my_context->name, my_name.c_str()); |
| 63 | my_context->name_size = my_name.size(); |
Brian Silverman | 2fe007c | 2014-12-28 12:20:01 -0800 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
Brian Silverman | b089388 | 2014-02-10 14:48:30 -0800 | [diff] [blame] | 67 | Context *Context::Get() { |
Brian Silverman | 17311c9 | 2014-09-06 00:48:59 -0400 | [diff] [blame] | 68 | if (__builtin_expect(delete_current_context, false)) { |
| 69 | my_context.Clear(); |
| 70 | delete_current_context = false; |
| 71 | } |
| 72 | if (__builtin_expect(!my_context.created(), false)) { |
| 73 | my_context.Create(); |
Brian Silverman | 2fe007c | 2014-12-28 12:20:01 -0800 | [diff] [blame] | 74 | ReloadThreadName(); |
Brian Silverman | b089388 | 2014-02-10 14:48:30 -0800 | [diff] [blame] | 75 | my_context->source = getpid(); |
| 76 | } |
Brian Silverman | 17311c9 | 2014-09-06 00:48:59 -0400 | [diff] [blame] | 77 | return my_context.get(); |
Brian Silverman | b089388 | 2014-02-10 14:48:30 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void Context::Delete() { |
Brian Silverman | 17311c9 | 2014-09-06 00:48:59 -0400 | [diff] [blame] | 81 | delete_current_context = true; |
Brian Silverman | b089388 | 2014-02-10 14:48:30 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | } // namespace internal |
| 85 | } // namespace logging |
| 86 | } // namespace aos |