James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | // The bootstat command provides options to persist boot events with the current |
| 18 | // timestamp, dump the persisted events, and log all events to EventLog to be |
| 19 | // uploaded to Android log storage via Tron. |
| 20 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 21 | #include <getopt.h> |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 22 | #include <unistd.h> |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 23 | |
James Hawkins | 0660b30 | 2016-03-08 16:18:15 -0800 | [diff] [blame] | 24 | #include <cmath> |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 25 | #include <cstddef> |
| 26 | #include <cstdio> |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 27 | #include <ctime> |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 28 | #include <map> |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 29 | #include <memory> |
| 30 | #include <string> |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 31 | |
| 32 | #include <android/log.h> |
James Hawkins | eabe08b | 2016-01-19 16:54:35 -0800 | [diff] [blame] | 33 | #include <android-base/logging.h> |
James Hawkins | 4dded61 | 2016-07-28 11:50:23 -0700 | [diff] [blame] | 34 | #include <android-base/parseint.h> |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 35 | #include <cutils/properties.h> |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 36 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 37 | #include "boot_event_record_store.h" |
James Hawkins | 6f28299 | 2016-03-22 14:13:06 -0700 | [diff] [blame] | 38 | #include "histogram_logger.h" |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 39 | #include "uptime_parser.h" |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 40 | |
| 41 | namespace { |
| 42 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 43 | // Scans the boot event record store for record files and logs each boot event |
| 44 | // via EventLog. |
| 45 | void LogBootEvents() { |
| 46 | BootEventRecordStore boot_event_store; |
| 47 | |
| 48 | auto events = boot_event_store.GetAllBootEvents(); |
| 49 | for (auto i = events.cbegin(); i != events.cend(); ++i) { |
James Hawkins | 6f28299 | 2016-03-22 14:13:06 -0700 | [diff] [blame] | 50 | bootstat::LogHistogram(i->first, i->second); |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 54 | // Records the named boot |event| to the record store. If |value| is non-empty |
| 55 | // and is a proper string representation of an integer value, the converted |
| 56 | // integer value is associated with the boot event. |
| 57 | void RecordBootEventFromCommandLine( |
| 58 | const std::string& event, const std::string& value_str) { |
| 59 | BootEventRecordStore boot_event_store; |
| 60 | if (!value_str.empty()) { |
| 61 | int32_t value = 0; |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 62 | if (android::base::ParseInt(value_str, &value)) { |
James Hawkins | 4dded61 | 2016-07-28 11:50:23 -0700 | [diff] [blame] | 63 | boot_event_store.AddBootEventWithValue(event, value); |
| 64 | } |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 65 | } else { |
| 66 | boot_event_store.AddBootEvent(event); |
| 67 | } |
| 68 | } |
| 69 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 70 | void PrintBootEvents() { |
| 71 | printf("Boot events:\n"); |
| 72 | printf("------------\n"); |
| 73 | |
| 74 | BootEventRecordStore boot_event_store; |
| 75 | auto events = boot_event_store.GetAllBootEvents(); |
| 76 | for (auto i = events.cbegin(); i != events.cend(); ++i) { |
| 77 | printf("%s\t%d\n", i->first.c_str(), i->second); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void ShowHelp(const char *cmd) { |
| 82 | fprintf(stderr, "Usage: %s [options]\n", cmd); |
| 83 | fprintf(stderr, |
| 84 | "options include:\n" |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 85 | " -h, --help Show this help\n" |
| 86 | " -l, --log Log all metrics to logstorage\n" |
| 87 | " -p, --print Dump the boot event records to the console\n" |
| 88 | " -r, --record Record the timestamp of a named boot event\n" |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 89 | " --value Optional value to associate with the boot event\n" |
James Hawkins | 53684ea | 2016-02-23 16:18:19 -0800 | [diff] [blame] | 90 | " --record_boot_reason Record the reason why the device booted\n" |
| 91 | " --record_time_since_factory_reset Record the time since the device was reset\n"); |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // Constructs a readable, printable string from the givencommand line |
| 95 | // arguments. |
| 96 | std::string GetCommandLine(int argc, char **argv) { |
| 97 | std::string cmd; |
| 98 | for (int i = 0; i < argc; ++i) { |
| 99 | cmd += argv[i]; |
| 100 | cmd += " "; |
| 101 | } |
| 102 | |
| 103 | return cmd; |
| 104 | } |
| 105 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 106 | // Convenience wrapper over the property API that returns an |
| 107 | // std::string. |
| 108 | std::string GetProperty(const char* key) { |
| 109 | std::vector<char> temp(PROPERTY_VALUE_MAX); |
| 110 | const int len = property_get(key, &temp[0], nullptr); |
| 111 | if (len < 0) { |
| 112 | return ""; |
| 113 | } |
| 114 | return std::string(&temp[0], len); |
| 115 | } |
| 116 | |
James Hawkins | 6f74c0b | 2016-02-12 15:49:16 -0800 | [diff] [blame] | 117 | constexpr int32_t kUnknownBootReason = 1; |
| 118 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 119 | // A mapping from boot reason string, as read from the ro.boot.bootreason |
| 120 | // system property, to a unique integer ID. Viewers of log data dashboards for |
| 121 | // the boot_reason metric may refer to this mapping to discern the histogram |
| 122 | // values. |
James Hawkins | 6f74c0b | 2016-02-12 15:49:16 -0800 | [diff] [blame] | 123 | const std::map<std::string, int32_t> kBootReasonMap = { |
| 124 | {"unknown", kUnknownBootReason}, |
| 125 | {"normal", 2}, |
| 126 | {"recovery", 3}, |
| 127 | {"reboot", 4}, |
| 128 | {"PowerKey", 5}, |
| 129 | {"hard_reset", 6}, |
| 130 | {"kernel_panic", 7}, |
| 131 | {"rpm_err", 8}, |
| 132 | {"hw_reset", 9}, |
| 133 | {"tz_err", 10}, |
| 134 | {"adsp_err", 11}, |
| 135 | {"modem_err", 12}, |
| 136 | {"mba_err", 13}, |
| 137 | {"Watchdog", 14}, |
| 138 | {"Panic", 15}, |
| 139 | {"power_key", 16}, |
| 140 | {"power_on", 17}, |
| 141 | {"Reboot", 18}, |
| 142 | {"rtc", 19}, |
| 143 | {"edl", 20}, |
James Hawkins | 45ead35 | 2016-03-08 16:42:07 -0800 | [diff] [blame] | 144 | {"oem_pon1", 21}, |
| 145 | {"oem_powerkey", 22}, |
| 146 | {"oem_unknown_reset", 23}, |
| 147 | {"srto: HWWDT reset SC", 24}, |
| 148 | {"srto: HWWDT reset platform", 25}, |
| 149 | {"srto: bootloader", 26}, |
| 150 | {"srto: kernel panic", 27}, |
| 151 | {"srto: kernel watchdog reset", 28}, |
| 152 | {"srto: normal", 29}, |
| 153 | {"srto: reboot", 30}, |
| 154 | {"srto: reboot-bootloader", 31}, |
| 155 | {"srto: security watchdog reset", 32}, |
| 156 | {"srto: wakesrc", 33}, |
| 157 | {"srto: watchdog", 34}, |
| 158 | {"srto:1-1", 35}, |
| 159 | {"srto:omap_hsmm", 36}, |
| 160 | {"srto:phy0", 37}, |
| 161 | {"srto:rtc0", 38}, |
| 162 | {"srto:touchpad", 39}, |
| 163 | {"watchdog", 40}, |
| 164 | {"watchdogr", 41}, |
| 165 | {"wdog_bark", 42}, |
| 166 | {"wdog_bite", 43}, |
| 167 | {"wdog_reset", 44}, |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | // Converts a string value representing the reason the system booted to an |
| 171 | // integer representation. This is necessary for logging the boot_reason metric |
| 172 | // via Tron, which does not accept non-integer buckets in histograms. |
| 173 | int32_t BootReasonStrToEnum(const std::string& boot_reason) { |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 174 | auto mapping = kBootReasonMap.find(boot_reason); |
| 175 | if (mapping != kBootReasonMap.end()) { |
| 176 | return mapping->second; |
| 177 | } |
| 178 | |
| 179 | LOG(INFO) << "Unknown boot reason: " << boot_reason; |
| 180 | return kUnknownBootReason; |
| 181 | } |
| 182 | |
James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 183 | // Returns the appropriate metric key prefix for the boot_complete metric such |
| 184 | // that boot metrics after a system update are labeled as ota_boot_complete; |
| 185 | // otherwise, they are labeled as boot_complete. This method encapsulates the |
| 186 | // bookkeeping required to track when a system update has occurred by storing |
| 187 | // the UTC timestamp of the system build date and comparing against the current |
| 188 | // system build date. |
| 189 | std::string CalculateBootCompletePrefix() { |
| 190 | static const std::string kBuildDateKey = "build_date"; |
| 191 | std::string boot_complete_prefix = "boot_complete"; |
| 192 | |
| 193 | std::string build_date_str = GetProperty("ro.build.date.utc"); |
James Hawkins | 4dded61 | 2016-07-28 11:50:23 -0700 | [diff] [blame] | 194 | int32_t build_date; |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 195 | if (!android::base::ParseInt(build_date_str, &build_date)) { |
James Hawkins | 4dded61 | 2016-07-28 11:50:23 -0700 | [diff] [blame] | 196 | return std::string(); |
| 197 | } |
James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 198 | |
| 199 | BootEventRecordStore boot_event_store; |
| 200 | BootEventRecordStore::BootEventRecord record; |
| 201 | if (!boot_event_store.GetBootEvent(kBuildDateKey, &record) || |
| 202 | build_date != record.second) { |
| 203 | boot_complete_prefix = "ota_" + boot_complete_prefix; |
| 204 | boot_event_store.AddBootEventWithValue(kBuildDateKey, build_date); |
| 205 | } |
| 206 | |
| 207 | return boot_complete_prefix; |
| 208 | } |
| 209 | |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 210 | // Records several metrics related to the time it takes to boot the device, |
| 211 | // including disambiguating boot time on encrypted or non-encrypted devices. |
| 212 | void RecordBootComplete() { |
| 213 | BootEventRecordStore boot_event_store; |
James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 214 | BootEventRecordStore::BootEventRecord record; |
James Hawkins | 2d8b3e6 | 2016-04-14 14:13:20 -0700 | [diff] [blame] | 215 | |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 216 | time_t uptime = bootstat::ParseUptime(); |
James Hawkins | 2d8b3e6 | 2016-04-14 14:13:20 -0700 | [diff] [blame] | 217 | time_t current_time_utc = time(nullptr); |
| 218 | |
| 219 | if (boot_event_store.GetBootEvent("last_boot_time_utc", &record)) { |
| 220 | time_t last_boot_time_utc = record.second; |
| 221 | time_t time_since_last_boot = difftime(current_time_utc, |
| 222 | last_boot_time_utc); |
| 223 | boot_event_store.AddBootEventWithValue("time_since_last_boot", |
| 224 | time_since_last_boot); |
| 225 | } |
| 226 | |
| 227 | boot_event_store.AddBootEventWithValue("last_boot_time_utc", current_time_utc); |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 228 | |
James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 229 | // The boot_complete metric has two variants: boot_complete and |
| 230 | // ota_boot_complete. The latter signifies that the device is booting after |
| 231 | // a system update. |
| 232 | std::string boot_complete_prefix = CalculateBootCompletePrefix(); |
James Hawkins | 4dded61 | 2016-07-28 11:50:23 -0700 | [diff] [blame] | 233 | if (boot_complete_prefix.empty()) { |
| 234 | // The system is hosed because the build date property could not be read. |
| 235 | return; |
| 236 | } |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 237 | |
| 238 | // post_decrypt_time_elapsed is only logged on encrypted devices. |
| 239 | if (boot_event_store.GetBootEvent("post_decrypt_time_elapsed", &record)) { |
| 240 | // Log the amount of time elapsed until the device is decrypted, which |
| 241 | // includes the variable amount of time the user takes to enter the |
| 242 | // decryption password. |
| 243 | boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime); |
| 244 | |
| 245 | // Subtract the decryption time to normalize the boot cycle timing. |
| 246 | time_t boot_complete = uptime - record.second; |
James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 247 | boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_post_decrypt", |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 248 | boot_complete); |
| 249 | |
| 250 | |
| 251 | } else { |
James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 252 | boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_no_encryption", |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 253 | uptime); |
| 254 | } |
| 255 | |
| 256 | // Record the total time from device startup to boot complete, regardless of |
| 257 | // encryption state. |
James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 258 | boot_event_store.AddBootEventWithValue(boot_complete_prefix, uptime); |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 259 | } |
| 260 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 261 | // Records the boot_reason metric by querying the ro.boot.bootreason system |
| 262 | // property. |
| 263 | void RecordBootReason() { |
| 264 | int32_t boot_reason = BootReasonStrToEnum(GetProperty("ro.boot.bootreason")); |
| 265 | BootEventRecordStore boot_event_store; |
| 266 | boot_event_store.AddBootEventWithValue("boot_reason", boot_reason); |
| 267 | } |
| 268 | |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 269 | // Records two metrics related to the user resetting a device: the time at |
| 270 | // which the device is reset, and the time since the user last reset the |
| 271 | // device. The former is only set once per-factory reset. |
| 272 | void RecordFactoryReset() { |
| 273 | BootEventRecordStore boot_event_store; |
| 274 | BootEventRecordStore::BootEventRecord record; |
| 275 | |
| 276 | time_t current_time_utc = time(nullptr); |
| 277 | |
James Hawkins | 0660b30 | 2016-03-08 16:18:15 -0800 | [diff] [blame] | 278 | if (current_time_utc < 0) { |
| 279 | // UMA does not display negative values in buckets, so convert to positive. |
James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 280 | bootstat::LogHistogram( |
| 281 | "factory_reset_current_time_failure", std::abs(current_time_utc)); |
| 282 | |
| 283 | // Logging via BootEventRecordStore to see if using bootstat::LogHistogram |
| 284 | // is losing records somehow. |
| 285 | boot_event_store.AddBootEventWithValue( |
| 286 | "factory_reset_current_time_failure", std::abs(current_time_utc)); |
James Hawkins | 0660b30 | 2016-03-08 16:18:15 -0800 | [diff] [blame] | 287 | return; |
| 288 | } else { |
James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 289 | bootstat::LogHistogram("factory_reset_current_time", current_time_utc); |
| 290 | |
| 291 | // Logging via BootEventRecordStore to see if using bootstat::LogHistogram |
| 292 | // is losing records somehow. |
| 293 | boot_event_store.AddBootEventWithValue( |
| 294 | "factory_reset_current_time", current_time_utc); |
James Hawkins | 0660b30 | 2016-03-08 16:18:15 -0800 | [diff] [blame] | 295 | } |
| 296 | |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 297 | // The factory_reset boot event does not exist after the device is reset, so |
| 298 | // use this signal to mark the time of the factory reset. |
| 299 | if (!boot_event_store.GetBootEvent("factory_reset", &record)) { |
| 300 | boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc); |
James Hawkins | 3bf9b14 | 2016-03-03 14:50:24 -0800 | [diff] [blame] | 301 | |
| 302 | // Don't log the time_since_factory_reset until some time has elapsed. |
| 303 | // The data is not meaningful yet and skews the histogram buckets. |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 304 | return; |
| 305 | } |
| 306 | |
| 307 | // Calculate and record the difference in time between now and the |
| 308 | // factory_reset time. |
| 309 | time_t factory_reset_utc = record.second; |
James Hawkins | 6f28299 | 2016-03-22 14:13:06 -0700 | [diff] [blame] | 310 | bootstat::LogHistogram("factory_reset_record_value", factory_reset_utc); |
James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 311 | |
| 312 | // Logging via BootEventRecordStore to see if using bootstat::LogHistogram |
| 313 | // is losing records somehow. |
| 314 | boot_event_store.AddBootEventWithValue( |
| 315 | "factory_reset_record_value", factory_reset_utc); |
| 316 | |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 317 | time_t time_since_factory_reset = difftime(current_time_utc, |
| 318 | factory_reset_utc); |
| 319 | boot_event_store.AddBootEventWithValue("time_since_factory_reset", |
| 320 | time_since_factory_reset); |
| 321 | } |
| 322 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 323 | } // namespace |
| 324 | |
| 325 | int main(int argc, char **argv) { |
| 326 | android::base::InitLogging(argv); |
| 327 | |
| 328 | const std::string cmd_line = GetCommandLine(argc, argv); |
| 329 | LOG(INFO) << "Service started: " << cmd_line; |
| 330 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 331 | int option_index = 0; |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 332 | static const char value_str[] = "value"; |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 333 | static const char boot_complete_str[] = "record_boot_complete"; |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 334 | static const char boot_reason_str[] = "record_boot_reason"; |
James Hawkins | 53684ea | 2016-02-23 16:18:19 -0800 | [diff] [blame] | 335 | static const char factory_reset_str[] = "record_time_since_factory_reset"; |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 336 | static const struct option long_options[] = { |
| 337 | { "help", no_argument, NULL, 'h' }, |
| 338 | { "log", no_argument, NULL, 'l' }, |
| 339 | { "print", no_argument, NULL, 'p' }, |
| 340 | { "record", required_argument, NULL, 'r' }, |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 341 | { value_str, required_argument, NULL, 0 }, |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 342 | { boot_complete_str, no_argument, NULL, 0 }, |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 343 | { boot_reason_str, no_argument, NULL, 0 }, |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 344 | { factory_reset_str, no_argument, NULL, 0 }, |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 345 | { NULL, 0, NULL, 0 } |
| 346 | }; |
| 347 | |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 348 | std::string boot_event; |
| 349 | std::string value; |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 350 | int opt = 0; |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 351 | while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) { |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 352 | switch (opt) { |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 353 | // This case handles long options which have no single-character mapping. |
| 354 | case 0: { |
| 355 | const std::string option_name = long_options[option_index].name; |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 356 | if (option_name == value_str) { |
| 357 | // |optarg| is an external variable set by getopt representing |
| 358 | // the option argument. |
| 359 | value = optarg; |
| 360 | } else if (option_name == boot_complete_str) { |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 361 | RecordBootComplete(); |
| 362 | } else if (option_name == boot_reason_str) { |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 363 | RecordBootReason(); |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 364 | } else if (option_name == factory_reset_str) { |
| 365 | RecordFactoryReset(); |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 366 | } else { |
| 367 | LOG(ERROR) << "Invalid option: " << option_name; |
| 368 | } |
| 369 | break; |
| 370 | } |
| 371 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 372 | case 'h': { |
| 373 | ShowHelp(argv[0]); |
| 374 | break; |
| 375 | } |
| 376 | |
| 377 | case 'l': { |
| 378 | LogBootEvents(); |
| 379 | break; |
| 380 | } |
| 381 | |
| 382 | case 'p': { |
| 383 | PrintBootEvents(); |
| 384 | break; |
| 385 | } |
| 386 | |
| 387 | case 'r': { |
| 388 | // |optarg| is an external variable set by getopt representing |
| 389 | // the option argument. |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 390 | boot_event = optarg; |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 391 | break; |
| 392 | } |
| 393 | |
| 394 | default: { |
| 395 | DCHECK_EQ(opt, '?'); |
| 396 | |
| 397 | // |optopt| is an external variable set by getopt representing |
| 398 | // the value of the invalid option. |
| 399 | LOG(ERROR) << "Invalid option: " << optopt; |
| 400 | ShowHelp(argv[0]); |
| 401 | return EXIT_FAILURE; |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 406 | if (!boot_event.empty()) { |
| 407 | RecordBootEventFromCommandLine(boot_event, value); |
| 408 | } |
| 409 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 410 | return 0; |
| 411 | } |