blob: 7d4840c7db1d15c77f0f99581f845dfe1dda6328 [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"
18
Austin Schuh707c8352020-03-15 14:27:25 -070019DEFINE_bool(coredump, false, "If true, write core dumps on failure.");
20
brians343bc112013-02-10 01:53:46 +000021namespace aos {
brians343bc112013-02-10 01:53:46 +000022namespace {
Austin Schuh094d09b2020-11-20 23:26:52 -080023bool initialized = false;
Austin Schuh3d4d5df2015-10-17 15:51:41 -070024} // namespace
25
Austin Schuh094d09b2020-11-20 23:26:52 -080026bool IsInitialized() { return initialized; }
27
Alex Perrycb7da4b2019-08-28 19:35:56 -070028void InitGoogle(int *argc, char ***argv) {
Austin Schuh094d09b2020-11-20 23:26:52 -080029 CHECK(!IsInitialized()) << "Only initialize once.";
Alex Perrycb7da4b2019-08-28 19:35:56 -070030 FLAGS_logtostderr = true;
31 google::InitGoogleLogging((*argv)[0]);
32 gflags::ParseCommandLineFlags(argc, argv, true);
33 google::InstallFailureSignalHandler();
Austin Schuh62288252020-11-18 23:26:04 -080034
Austin Schuh094d09b2020-11-20 23:26:52 -080035 if (FLAGS_coredump) {
36 WriteCoreDumps();
brians343bc112013-02-10 01:53:46 +000037 }
38
Austin Schuh094d09b2020-11-20 23:26:52 -080039 RegisterMallocHook();
40 initialized = true;
Brian Silvermane4d8b282015-12-24 13:44:48 -080041}
42
Brian Silvermane4c79ce2022-08-15 05:57:28 -070043void InitFromRust(const char *argv0) {
44 CHECK(!IsInitialized()) << "Only initialize once.";
45
46 FLAGS_logtostderr = true;
47
48 google::InitGoogleLogging(argv0);
49
50 // TODO(Brian): Provide a way for Rust to configure C++ flags.
51 char fake_argv0_val[] = "rust";
52 char *fake_argv0 = fake_argv0_val;
53 char **fake_argv = &fake_argv0;
54 int fake_argc = 1;
55 gflags::ParseCommandLineFlags(&fake_argc, &fake_argv, true);
56
57 // TODO(Brian): Where should Rust binaries be configured to write coredumps?
58
59 // TODO(Brian): Figure out what to do with allocator hooks for C++ and Rust.
60
61 initialized = true;
62}
63
brians343bc112013-02-10 01:53:46 +000064} // namespace aos