| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // This file contains a very small number of declarations that should be | |
| 6 // included in most programs. | |
| 7 // | |
| 8 // Sets the usage message to "*usage", parses the command-line flags, | |
| 9 // and initializes various other pieces of global state. If running | |
| 10 // as root, this routine attempts to setuid to FLAGS_uid, which defaults | |
| 11 // to "nobody". | |
| 12 // | |
| 13 // It also logs all the program's command-line flags to the INFO log file. | |
| 14 // | |
| 15 // If the global variable FLAGS_uid is not equal to the empty string, | |
| 16 // then InitGoogle also does an 'su' to the user specified in | |
| 17 // FLAGS_uid, and sets the group equal to FLAGS_gid. | |
| 18 // | |
| 19 // The functions in here are thread-safe unless specified otherwise, | |
| 20 // but they must be called after InitGoogle() (or the | |
| 21 // InitGoogleExceptChangeRootAndUser/ChangeRootAndUser pair). | |
| 22 | |
| 23 #ifndef _GOOGLE_H_ | |
| 24 #define _GOOGLE_H_ | |
| 25 | |
| 26 #include <iosfwd> // to forward declare ostream | |
| 27 #include "base/basictypes.h" | |
| 28 #include "third_party/cld/base/closure.h" | |
| 29 #include "third_party/cld/base/googleinit.h" | |
| 30 | |
| 31 class Closure; | |
| 32 | |
| 33 // A more-convenient way to access gethostname() | |
| 34 const char* Hostname(); | |
| 35 // Return kernel version string in /proc/version | |
| 36 const char* GetKernelVersionString(); | |
| 37 // Return true and set kernel version (x.y.z), patch level and revision (#p.r), | |
| 38 // false, if not known. | |
| 39 struct KernelVersion { | |
| 40 int major; // Major release | |
| 41 int minor; // Minor release | |
| 42 int micro; // Whatever the third no. is called ... | |
| 43 int patch; // Patch level | |
| 44 int revision; // Patch revision | |
| 45 }; | |
| 46 bool GetKernelVersion(KernelVersion* kv); | |
| 47 // A string saying when InitGoogle() was called -- probably program start | |
| 48 const char* GetStartTime(); | |
| 49 // time in ms since InitGoogle() was called -- | |
| 50 int64 GetUptime(); | |
| 51 // the pid for the startup thread | |
| 52 int32 GetMainThreadPid(); | |
| 53 | |
| 54 // the resource limit for core size when InitGoogle() was called. | |
| 55 const struct rlimit* GetSavedCoreLimit(); | |
| 56 | |
| 57 // Restore the core size limit saved in InitGoogle(). This is a no-op if | |
| 58 // FLAGS_allow_kernel_coredumps is true. | |
| 59 int32 RestoreSavedCoreLimit(); | |
| 60 | |
| 61 // Return true if we have determined that all CPUs have the same timing | |
| 62 // (same model, clock rate, stepping). Returns true if there is only one | |
| 63 // CPU. Returns false if we cannot read or parse /proc/cpuinfo. | |
| 64 bool CPUsHaveSameTiming( | |
| 65 const char *cpuinfo = "/proc/cpuinfo", | |
| 66 const char *cpuinfo_max_freq = "/sys/devices/system/cpu/cpu%d/" | |
| 67 "cpufreq/cpuinfo_max_freq"); | |
| 68 | |
| 69 // FlagsParsed is called once for every run VLOG() call site. | |
| 70 // Returns true if command line flags have been parsed | |
| 71 bool FlagsParsed(); | |
| 72 | |
| 73 // A place-holder module initializer to declare initialization ordering | |
| 74 // with respect to it to make chosen initalizers run before command line flag | |
| 75 // parsing (see googleinit.h for more details). | |
| 76 DECLARE_MODULE_INITIALIZER(command_line_flags_parsing); | |
| 77 | |
| 78 // Checks (only in debug mode) if main() has been started and crashes if not | |
| 79 // i.e. makes sure that we are out of the global constructor execution stage. | |
| 80 // Intended to for checking that some code should not be executed during | |
| 81 // global object construction (only specially crafted code might be safe | |
| 82 // to execute at that time). | |
| 83 void AssertMainHasStarted(); | |
| 84 | |
| 85 // Call this from main() if AssertMainHasStarted() is incorrectly failing | |
| 86 // for your code (its current implmentation relies on a call to InitGoogle() | |
| 87 // as the signal that we have reached main(), hence it is not 100% accurate). | |
| 88 void SetMainHasStarted(); | |
| 89 | |
| 90 // Checks (only in debug mode) if InitGoogle() has been fully executed | |
| 91 // and crashes if it has not been. | |
| 92 // Indtended for checking that code that depends on complete execution | |
| 93 // of InitGoogle() for its proper functioning is safe to execute. | |
| 94 void AssertInitGoogleIsDone(); | |
| 95 | |
| 96 // Initializes misc google-related things in the binary. | |
| 97 // In particular it does REQUIRE_MODULE_INITIALIZED(command_line_flags_parsing) | |
| 98 // parses command line flags and does RUN_MODULE_INITIALIZERS() (in that order). | |
| 99 // If a flag is defined more than once in the command line or flag | |
| 100 // file, the last definition is used. | |
| 101 // Typically called early on in main() and must be called before other | |
| 102 // threads start using functions from this file. | |
| 103 // | |
| 104 // 'usage' provides a short usage message passed to SetUsageMessage(). | |
| 105 // 'argc' and 'argv' are the command line flags to parse. | |
| 106 // If 'remove_flags' then parsed flags are removed. | |
| 107 void InitGoogle(const char* usage, int* argc, char*** argv, bool remove_flags); | |
| 108 | |
| 109 // Normally, InitGoogle will chroot (if requested with the --chroot flag) | |
| 110 // and setuid to --uid and --gid (default nobody). | |
| 111 // This version will not, and you will be responsible for calling | |
| 112 // ChangeRootAndUser | |
| 113 // This option is provided for applications that need to read files outside | |
| 114 // the chroot before chrooting. | |
| 115 void InitGoogleExceptChangeRootAndUser(const char* usage, int* argc, | |
| 116 char*** argv, bool remove_flags); | |
| 117 // Thread-hostile. | |
| 118 void ChangeRootAndUser(); | |
| 119 | |
| 120 // if you need to flush InitGoogle's resources from a sighandler | |
| 121 void SigHandlerFlushInitGoogleResources(); | |
| 122 | |
| 123 // Alter behavior of error to not dump core on an error. | |
| 124 // Simply cleanup and exit. Thread-hostile. | |
| 125 void SetNoCoreOnError(); | |
| 126 | |
| 127 // limit the amount of physical memory used by this process to a | |
| 128 // fraction of the available physical memory. The process is killed if | |
| 129 // it tries to go beyond this limit. If randomize is set, we reduce | |
| 130 // the fraction a little in a sort-of-random way. randomize is meant | |
| 131 // to be used for applications which run many copies -- by randomizing | |
| 132 // the limit, we can avoid having all copies of the application hit | |
| 133 // the limit (and die) at the same time. | |
| 134 void LimitPhysicalMemory(double fraction, bool randomize); | |
| 135 | |
| 136 // Return the limit set on physical memory, zero if error or no limit set. | |
| 137 uint64 GetPhysicalMemoryLimit(); | |
| 138 | |
| 139 // Add specified closure to the set of closures which are executed | |
| 140 // when the program dies a horrible death (signal, etc.) | |
| 141 // | |
| 142 // Note: These are not particularly efficient. Use sparingly. | |
| 143 // Note: you can't just use atexit() because functions registered with | |
| 144 // atexit() are supposedly only called on normal program exit, and we | |
| 145 // want to do things like flush logs on failures. | |
| 146 void RunOnFailure(Closure* closure); | |
| 147 | |
| 148 // Remove specified closure references from the set created by RunOnFailure. | |
| 149 void CancelRunOnFailure(Closure* closure); | |
| 150 | |
| 151 // Adds specified Callback2 instances to a set of callbacks that are | |
| 152 // executed when the program crashes. Two values: signo and ucontext_t* | |
| 153 // will be passed into these callback functions. We use void* to avoid the | |
| 154 // use of ucontext_t on non-POSIX systems. | |
| 155 // | |
| 156 // Note: it is recommended that these callbacks are signal-handler | |
| 157 // safe. Also, the calls of these callbacks are not protected by | |
| 158 // a mutex, so they are better to be multithread-safe. | |
| 159 void RunOnFailureCallback2(Callback2<int, void*>* callback); | |
| 160 void CancelRunOnFailureCallback2(Callback2<int, void*>* callback); | |
| 161 | |
| 162 // Return true if the google default signal handler is running, false | |
| 163 // otherwise. Sometimes callbacks specified with | |
| 164 // RunOnFailure{,Callback2} are not called because the process hangs | |
| 165 // or takes too long to symbolize callstacks. Users may want to | |
| 166 // augment the RunOnFailure mechanism with a dedicated thread which | |
| 167 // polls the below function periodically (say, every second) and runs | |
| 168 // their failure closures when it returns true. | |
| 169 bool IsFailureSignalHandlerRunning(); | |
| 170 | |
| 171 // Type of function used for printing in stack trace dumping, etc. | |
| 172 // We avoid closures to keep things simple. | |
| 173 typedef void DebugWriter(const char*, void*); | |
| 174 | |
| 175 // A few useful DebugWriters | |
| 176 DebugWriter DebugWriteToStderr; | |
| 177 DebugWriter DebugWriteToStream; | |
| 178 DebugWriter DebugWriteToFile; | |
| 179 DebugWriter DebugWriteToString; | |
| 180 | |
| 181 // Dump current stack trace omitting the topmost 'skip_count' stack frames. | |
| 182 void DumpStackTrace(int skip_count, DebugWriter *w, void* arg); | |
| 183 | |
| 184 // Dump given pc and stack trace. | |
| 185 void DumpPCAndStackTrace(void *pc, void *stack[], int depth, | |
| 186 DebugWriter *writerfn, void *arg); | |
| 187 | |
| 188 // Returns the program counter from signal context, NULL if unknown. | |
| 189 // vuc is a ucontext_t *. We use void* to avoid the use | |
| 190 // of ucontext_t on non-POSIX systems. | |
| 191 void* GetPC(void* vuc); | |
| 192 | |
| 193 // Dump current address map. | |
| 194 void DumpAddressMap(DebugWriter *w, void* arg); | |
| 195 | |
| 196 // Dump information about currently allocated memory. | |
| 197 void DumpMallocStats(DebugWriter *w, void* arg); | |
| 198 | |
| 199 // Return true if currently executing in the google failure signal | |
| 200 // handler. If this returns true you should: | |
| 201 // | |
| 202 // - avoid allocating anything via malloc/new | |
| 203 // - assume that your stack limit is SIGSTKSZ | |
| 204 // - assume that no other thread can be executing in the failure handler | |
| 205 bool InFailureSignalHandler(); | |
| 206 | |
| 207 // Return the alternate signal stack size (in bytes) needed in order to | |
| 208 // safely run the failure signal handlers. The returned value will | |
| 209 // always be a multiple of the system page size. | |
| 210 int32 GetRequiredAlternateSignalStackSize(); | |
| 211 | |
| 212 #endif // _GOOGLE_H_ | |
| OLD | NEW |