blob: 3ca73149bf63c7ea5dc632ea199eb9c47819b241 [file] [log] [blame]
John Park398c74a2018-10-20 21:17:39 -07001#include "aos/init.h"
brians343bc112013-02-10 01:53:46 +00002
brians343bc112013-02-10 01:53:46 +00003#include <sched.h>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07004#include <sys/mman.h>
brians343bc112013-02-10 01:53:46 +00005#include <sys/resource.h>
brians343bc112013-02-10 01:53:46 +00006#include <sys/types.h>
7#include <unistd.h>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07008
9#include <cerrno>
10#include <cstdio>
11#include <cstdlib>
12#include <cstring>
brians343bc112013-02-10 01:53:46 +000013
Austin Schuh094d09b2020-11-20 23:26:52 -080014#include "gflags/gflags.h"
15#include "glog/logging.h"
Brian Silverman40486622014-12-30 17:38:55 -080016
Philipp Schrader790cb542023-07-05 21:06:52 -070017#include "aos/realtime.h"
James Kuszmaula791b762023-07-13 14:56:21 -070018#include "aos/uuid.h"
Philipp Schrader790cb542023-07-05 21:06:52 -070019
Austin Schuh707c8352020-03-15 14:27:25 -070020DEFINE_bool(coredump, false, "If true, write core dumps on failure.");
21
brians343bc112013-02-10 01:53:46 +000022namespace aos {
brians343bc112013-02-10 01:53:46 +000023namespace {
Austin Schuh094d09b2020-11-20 23:26:52 -080024bool initialized = false;
Austin Schuh3d4d5df2015-10-17 15:51:41 -070025} // namespace
26
Austin Schuh094d09b2020-11-20 23:26:52 -080027bool IsInitialized() { return initialized; }
28
Alex Perrycb7da4b2019-08-28 19:35:56 -070029void InitGoogle(int *argc, char ***argv) {
Austin Schuh094d09b2020-11-20 23:26:52 -080030 CHECK(!IsInitialized()) << "Only initialize once.";
Alex Perrycb7da4b2019-08-28 19:35:56 -070031 FLAGS_logtostderr = true;
32 google::InitGoogleLogging((*argv)[0]);
33 gflags::ParseCommandLineFlags(argc, argv, true);
34 google::InstallFailureSignalHandler();
Austin Schuh62288252020-11-18 23:26:04 -080035
Austin Schuh094d09b2020-11-20 23:26:52 -080036 if (FLAGS_coredump) {
37 WriteCoreDumps();
brians343bc112013-02-10 01:53:46 +000038 }
39
Austin Schuh094d09b2020-11-20 23:26:52 -080040 RegisterMallocHook();
James Kuszmaula791b762023-07-13 14:56:21 -070041 // Ensure that the random number generator for the UUID code is initialized
42 // (it does some potentially expensive random number generation).
43 UUID::Random();
44
Austin Schuh094d09b2020-11-20 23:26:52 -080045 initialized = true;
Brian Silvermane4d8b282015-12-24 13:44:48 -080046}
47
Brian Silvermane4c79ce2022-08-15 05:57:28 -070048void InitFromRust(const char *argv0) {
49 CHECK(!IsInitialized()) << "Only initialize once.";
50
51 FLAGS_logtostderr = true;
52
53 google::InitGoogleLogging(argv0);
54
55 // TODO(Brian): Provide a way for Rust to configure C++ flags.
56 char fake_argv0_val[] = "rust";
57 char *fake_argv0 = fake_argv0_val;
58 char **fake_argv = &fake_argv0;
59 int fake_argc = 1;
60 gflags::ParseCommandLineFlags(&fake_argc, &fake_argv, true);
61
62 // TODO(Brian): Where should Rust binaries be configured to write coredumps?
63
64 // TODO(Brian): Figure out what to do with allocator hooks for C++ and Rust.
65
66 initialized = true;
67}
68
brians343bc112013-02-10 01:53:46 +000069} // namespace aos