John Park | 398c74a | 2018-10-20 21:17:39 -0700 | [diff] [blame] | 1 | #include "aos/init.h" |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 2 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 3 | #include <sched.h> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 4 | #include <sys/mman.h> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 5 | #include <sys/resource.h> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 6 | #include <sys/types.h> |
| 7 | #include <unistd.h> |
Tyler Chatow | bf0609c | 2021-07-31 16:13:27 -0700 | [diff] [blame] | 8 | |
| 9 | #include <cerrno> |
| 10 | #include <cstdio> |
| 11 | #include <cstdlib> |
| 12 | #include <cstring> |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 13 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 14 | #include "absl/debugging/failure_signal_handler.h" |
| 15 | #include "absl/debugging/symbolize.h" |
| 16 | #include "absl/flags/flag.h" |
| 17 | #include "absl/flags/parse.h" |
| 18 | #include "absl/log/check.h" |
| 19 | #include "absl/log/flags.h" |
| 20 | #include "absl/log/globals.h" |
| 21 | #include "absl/log/initialize.h" |
| 22 | #include "absl/log/log.h" |
Brian Silverman | 4048662 | 2014-12-30 17:38:55 -0800 | [diff] [blame] | 23 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 24 | #include "aos/realtime.h" |
James Kuszmaul | a791b76 | 2023-07-13 14:56:21 -0700 | [diff] [blame] | 25 | #include "aos/uuid.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 26 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 27 | ABSL_FLAG(bool, coredump, false, "If true, write core dumps on failure."); |
| 28 | ABSL_FLAG(bool, backtrace, true, "If true, print backtraces out on crashes."); |
Austin Schuh | 707c835 | 2020-03-15 14:27:25 -0700 | [diff] [blame] | 29 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 30 | namespace aos { |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 31 | namespace { |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 32 | std::atomic<bool> initialized{false}; |
Austin Schuh | 3d4d5df | 2015-10-17 15:51:41 -0700 | [diff] [blame] | 33 | } // namespace |
| 34 | |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 35 | bool IsInitialized() { return initialized; } |
| 36 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 37 | void InitGoogle(int *argc, char ***argv) { |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 38 | CHECK(!IsInitialized()) << "Only initialize once."; |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 39 | absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo); |
| 40 | std::vector<char *> positional_arguments = |
| 41 | absl::ParseCommandLine(*argc, *argv); |
Austin Schuh | 6228825 | 2020-11-18 23:26:04 -0800 | [diff] [blame] | 42 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 43 | { |
| 44 | const std::vector<absl::UnrecognizedFlag> unrecognized_flags; |
| 45 | absl::ReportUnrecognizedFlags(unrecognized_flags); |
| 46 | if (!unrecognized_flags.empty()) { |
| 47 | for (const absl::UnrecognizedFlag &flag : unrecognized_flags) { |
| 48 | LOG(ERROR) << "Unrecognized flag " << flag.flag_name; |
| 49 | } |
| 50 | LOG(FATAL) << "Found unrecognized flags, aborting"; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | CHECK_LE(positional_arguments.size(), static_cast<size_t>(*argc)); |
| 55 | for (size_t i = 0; i < positional_arguments.size(); ++i) { |
| 56 | (*argv)[i] = positional_arguments[i]; |
| 57 | } |
| 58 | *argc = positional_arguments.size(); |
| 59 | |
| 60 | absl::InitializeLog(); |
| 61 | |
| 62 | if (absl::GetFlag(FLAGS_backtrace)) { |
| 63 | absl::InitializeSymbolizer((*argv)[0]); |
| 64 | absl::FailureSignalHandlerOptions options; |
| 65 | absl::InstallFailureSignalHandler(options); |
| 66 | } |
| 67 | |
| 68 | if (absl::GetFlag(FLAGS_coredump)) { |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 69 | WriteCoreDumps(); |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 72 | RegisterMallocHook(); |
James Kuszmaul | a791b76 | 2023-07-13 14:56:21 -0700 | [diff] [blame] | 73 | // Ensure that the random number generator for the UUID code is initialized |
| 74 | // (it does some potentially expensive random number generation). |
| 75 | UUID::Random(); |
| 76 | |
Austin Schuh | 094d09b | 2020-11-20 23:26:52 -0800 | [diff] [blame] | 77 | initialized = true; |
Brian Silverman | e4d8b28 | 2015-12-24 13:44:48 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Adam Snaider | 48a62f3 | 2023-10-02 15:49:23 -0700 | [diff] [blame] | 80 | void MarkInitialized() { initialized = true; } |
Brian Silverman | e4c79ce | 2022-08-15 05:57:28 -0700 | [diff] [blame] | 81 | |
brians | 343bc11 | 2013-02-10 01:53:46 +0000 | [diff] [blame] | 82 | } // namespace aos |