blob: d4e215efea6a48f7dc975c15e7b706a63a8bca16 [file] [log] [blame]
James Hawkinsabd73e62016-01-19 15:10:38 -08001/*
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 Hawkinsa4a1a4a2016-02-09 15:32:38 -080021#include <getopt.h>
James Hawkinsabd73e62016-01-19 15:10:38 -080022#include <unistd.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070023
James Hawkinse78ea772017-03-24 11:43:02 -070024#include <chrono>
James Hawkins0660b302016-03-08 16:18:15 -080025#include <cmath>
James Hawkinsabd73e62016-01-19 15:10:38 -080026#include <cstddef>
27#include <cstdio>
James Hawkins500d7152016-02-16 15:05:54 -080028#include <ctime>
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080029#include <map>
James Hawkinsabd73e62016-01-19 15:10:38 -080030#include <memory>
31#include <string>
James Hawkinsbe46fd12017-02-02 16:21:25 -080032#include <vector>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070033
James Hawkinse78ea772017-03-24 11:43:02 -070034#include <android-base/chrono_utils.h>
James Hawkinseabe08b2016-01-19 16:54:35 -080035#include <android-base/logging.h>
James Hawkins4dded612016-07-28 11:50:23 -070036#include <android-base/parseint.h>
James Hawkinsbe46fd12017-02-02 16:21:25 -080037#include <android-base/strings.h>
James Hawkinse78ea772017-03-24 11:43:02 -070038#include <android/log.h>
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080039#include <cutils/properties.h>
James Hawkins9aec9262017-01-31 11:42:24 -080040#include <metricslogger/metrics_logger.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070041
James Hawkinsabd73e62016-01-19 15:10:38 -080042#include "boot_event_record_store.h"
James Hawkinsabd73e62016-01-19 15:10:38 -080043
44namespace {
45
James Hawkinsabd73e62016-01-19 15:10:38 -080046// Scans the boot event record store for record files and logs each boot event
47// via EventLog.
48void LogBootEvents() {
49 BootEventRecordStore boot_event_store;
50
51 auto events = boot_event_store.GetAllBootEvents();
52 for (auto i = events.cbegin(); i != events.cend(); ++i) {
James Hawkins9aec9262017-01-31 11:42:24 -080053 android::metricslogger::LogHistogram(i->first, i->second);
James Hawkinsabd73e62016-01-19 15:10:38 -080054 }
55}
56
James Hawkinsc6275582016-03-22 10:47:44 -070057// Records the named boot |event| to the record store. If |value| is non-empty
58// and is a proper string representation of an integer value, the converted
59// integer value is associated with the boot event.
60void RecordBootEventFromCommandLine(
61 const std::string& event, const std::string& value_str) {
62 BootEventRecordStore boot_event_store;
63 if (!value_str.empty()) {
64 int32_t value = 0;
Elliott Hughesda46b392016-10-11 17:09:00 -070065 if (android::base::ParseInt(value_str, &value)) {
James Hawkins4dded612016-07-28 11:50:23 -070066 boot_event_store.AddBootEventWithValue(event, value);
67 }
James Hawkinsc6275582016-03-22 10:47:44 -070068 } else {
69 boot_event_store.AddBootEvent(event);
70 }
71}
72
James Hawkinsabd73e62016-01-19 15:10:38 -080073void PrintBootEvents() {
74 printf("Boot events:\n");
75 printf("------------\n");
76
77 BootEventRecordStore boot_event_store;
78 auto events = boot_event_store.GetAllBootEvents();
79 for (auto i = events.cbegin(); i != events.cend(); ++i) {
80 printf("%s\t%d\n", i->first.c_str(), i->second);
81 }
82}
83
84void ShowHelp(const char *cmd) {
85 fprintf(stderr, "Usage: %s [options]\n", cmd);
86 fprintf(stderr,
87 "options include:\n"
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080088 " -h, --help Show this help\n"
89 " -l, --log Log all metrics to logstorage\n"
90 " -p, --print Dump the boot event records to the console\n"
91 " -r, --record Record the timestamp of a named boot event\n"
James Hawkinsc6275582016-03-22 10:47:44 -070092 " --value Optional value to associate with the boot event\n"
James Hawkins53684ea2016-02-23 16:18:19 -080093 " --record_boot_reason Record the reason why the device booted\n"
94 " --record_time_since_factory_reset Record the time since the device was reset\n");
James Hawkinsabd73e62016-01-19 15:10:38 -080095}
96
97// Constructs a readable, printable string from the givencommand line
98// arguments.
99std::string GetCommandLine(int argc, char **argv) {
100 std::string cmd;
101 for (int i = 0; i < argc; ++i) {
102 cmd += argv[i];
103 cmd += " ";
104 }
105
106 return cmd;
107}
108
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800109// Convenience wrapper over the property API that returns an
110// std::string.
111std::string GetProperty(const char* key) {
112 std::vector<char> temp(PROPERTY_VALUE_MAX);
113 const int len = property_get(key, &temp[0], nullptr);
114 if (len < 0) {
115 return "";
116 }
117 return std::string(&temp[0], len);
118}
119
James Hawkins6f74c0b2016-02-12 15:49:16 -0800120constexpr int32_t kUnknownBootReason = 1;
121
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800122// A mapping from boot reason string, as read from the ro.boot.bootreason
123// system property, to a unique integer ID. Viewers of log data dashboards for
124// the boot_reason metric may refer to this mapping to discern the histogram
125// values.
James Hawkins6f74c0b2016-02-12 15:49:16 -0800126const std::map<std::string, int32_t> kBootReasonMap = {
127 {"unknown", kUnknownBootReason},
128 {"normal", 2},
129 {"recovery", 3},
130 {"reboot", 4},
131 {"PowerKey", 5},
132 {"hard_reset", 6},
133 {"kernel_panic", 7},
134 {"rpm_err", 8},
135 {"hw_reset", 9},
136 {"tz_err", 10},
137 {"adsp_err", 11},
138 {"modem_err", 12},
139 {"mba_err", 13},
140 {"Watchdog", 14},
141 {"Panic", 15},
142 {"power_key", 16},
143 {"power_on", 17},
144 {"Reboot", 18},
145 {"rtc", 19},
146 {"edl", 20},
James Hawkins45ead352016-03-08 16:42:07 -0800147 {"oem_pon1", 21},
148 {"oem_powerkey", 22},
149 {"oem_unknown_reset", 23},
150 {"srto: HWWDT reset SC", 24},
151 {"srto: HWWDT reset platform", 25},
152 {"srto: bootloader", 26},
153 {"srto: kernel panic", 27},
154 {"srto: kernel watchdog reset", 28},
155 {"srto: normal", 29},
156 {"srto: reboot", 30},
157 {"srto: reboot-bootloader", 31},
158 {"srto: security watchdog reset", 32},
159 {"srto: wakesrc", 33},
160 {"srto: watchdog", 34},
161 {"srto:1-1", 35},
162 {"srto:omap_hsmm", 36},
163 {"srto:phy0", 37},
164 {"srto:rtc0", 38},
165 {"srto:touchpad", 39},
166 {"watchdog", 40},
167 {"watchdogr", 41},
168 {"wdog_bark", 42},
169 {"wdog_bite", 43},
170 {"wdog_reset", 44},
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800171};
172
173// Converts a string value representing the reason the system booted to an
174// integer representation. This is necessary for logging the boot_reason metric
175// via Tron, which does not accept non-integer buckets in histograms.
176int32_t BootReasonStrToEnum(const std::string& boot_reason) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800177 auto mapping = kBootReasonMap.find(boot_reason);
178 if (mapping != kBootReasonMap.end()) {
179 return mapping->second;
180 }
181
182 LOG(INFO) << "Unknown boot reason: " << boot_reason;
183 return kUnknownBootReason;
184}
185
James Hawkinsb9cf7712016-04-08 15:32:19 -0700186// Returns the appropriate metric key prefix for the boot_complete metric such
187// that boot metrics after a system update are labeled as ota_boot_complete;
188// otherwise, they are labeled as boot_complete. This method encapsulates the
189// bookkeeping required to track when a system update has occurred by storing
190// the UTC timestamp of the system build date and comparing against the current
191// system build date.
192std::string CalculateBootCompletePrefix() {
193 static const std::string kBuildDateKey = "build_date";
194 std::string boot_complete_prefix = "boot_complete";
195
196 std::string build_date_str = GetProperty("ro.build.date.utc");
James Hawkins4dded612016-07-28 11:50:23 -0700197 int32_t build_date;
Elliott Hughesda46b392016-10-11 17:09:00 -0700198 if (!android::base::ParseInt(build_date_str, &build_date)) {
James Hawkins4dded612016-07-28 11:50:23 -0700199 return std::string();
200 }
James Hawkinsb9cf7712016-04-08 15:32:19 -0700201
202 BootEventRecordStore boot_event_store;
203 BootEventRecordStore::BootEventRecord record;
204 if (!boot_event_store.GetBootEvent(kBuildDateKey, &record) ||
205 build_date != record.second) {
206 boot_complete_prefix = "ota_" + boot_complete_prefix;
207 boot_event_store.AddBootEventWithValue(kBuildDateKey, build_date);
208 }
209
210 return boot_complete_prefix;
211}
212
James Hawkinsef0a0902017-01-06 14:38:23 -0800213// Records the value of a given ro.boottime.init property in milliseconds.
214void RecordInitBootTimeProp(
215 BootEventRecordStore* boot_event_store, const char* property) {
216 std::string value = GetProperty(property);
217
James Hawkins27c05222017-01-26 11:55:44 -0800218 int32_t time_in_ms;
219 if (android::base::ParseInt(value, &time_in_ms)) {
James Hawkinsef0a0902017-01-06 14:38:23 -0800220 boot_event_store->AddBootEventWithValue(property, time_in_ms);
221 }
222}
223
James Hawkins1bfcaec2017-05-19 14:27:27 -0700224// A map from bootloader timing stage to the time that stage took during boot.
225typedef std::map<std::string, int32_t> BootloaderTimingMap;
226
227// Returns a mapping from bootloader stage names to the time those stages
228// took to boot.
229const BootloaderTimingMap GetBootLoaderTimings() {
230 BootloaderTimingMap timings;
231
232 // |ro.boot.boottime| is of the form 'stage1:time1,...,stageN:timeN',
233 // where timeN is in milliseconds.
James Hawkinsbe46fd12017-02-02 16:21:25 -0800234 std::string value = GetProperty("ro.boot.boottime");
James Hawkins6b5c5aa2017-02-16 11:53:03 -0800235 if (value.empty()) {
236 // ro.boot.boottime is not reported on all devices.
James Hawkins1bfcaec2017-05-19 14:27:27 -0700237 return BootloaderTimingMap();
James Hawkins6b5c5aa2017-02-16 11:53:03 -0800238 }
James Hawkinsbe46fd12017-02-02 16:21:25 -0800239
240 auto stages = android::base::Split(value, ",");
James Hawkins1bfcaec2017-05-19 14:27:27 -0700241 for (const auto& stageTiming : stages) {
James Hawkinsbe46fd12017-02-02 16:21:25 -0800242 // |stageTiming| is of the form 'stage:time'.
243 auto stageTimingValues = android::base::Split(stageTiming, ":");
244 DCHECK_EQ(2, stageTimingValues.size());
245
246 std::string stageName = stageTimingValues[0];
247 int32_t time_ms;
248 if (android::base::ParseInt(stageTimingValues[1], &time_ms)) {
James Hawkins1bfcaec2017-05-19 14:27:27 -0700249 timings[stageName] = time_ms;
James Hawkinsbe46fd12017-02-02 16:21:25 -0800250 }
251 }
James Hawkins6b5c5aa2017-02-16 11:53:03 -0800252
James Hawkins1bfcaec2017-05-19 14:27:27 -0700253 return timings;
254}
255
256// Parses and records the set of bootloader stages and associated boot times
257// from the ro.boot.boottime system property.
258void RecordBootloaderTimings(BootEventRecordStore* boot_event_store,
259 const BootloaderTimingMap& bootloader_timings) {
260 int32_t total_time = 0;
261 for (const auto& timing : bootloader_timings) {
262 total_time += timing.second;
263 boot_event_store->AddBootEventWithValue("boottime.bootloader." + timing.first, timing.second);
264 }
265
James Hawkins6b5c5aa2017-02-16 11:53:03 -0800266 boot_event_store->AddBootEventWithValue("boottime.bootloader.total", total_time);
James Hawkinsbe46fd12017-02-02 16:21:25 -0800267}
268
James Hawkins1bfcaec2017-05-19 14:27:27 -0700269// Records the closest estimation to the absolute device boot time, i.e.,
270// from power on to boot_complete, including bootloader times.
271void RecordAbsoluteBootTime(BootEventRecordStore* boot_event_store,
272 const BootloaderTimingMap& bootloader_timings,
273 std::chrono::milliseconds uptime) {
274 int32_t bootloader_time_ms = 0;
275
276 for (const auto& timing : bootloader_timings) {
277 if (timing.first.compare("SW") != 0) {
278 bootloader_time_ms += timing.second;
279 }
280 }
281
282 auto bootloader_duration = std::chrono::milliseconds(bootloader_time_ms);
283 auto absolute_total =
284 std::chrono::duration_cast<std::chrono::seconds>(bootloader_duration + uptime);
285 boot_event_store->AddBootEventWithValue("absolute_boot_time", absolute_total.count());
286}
287
James Hawkinsc08e9962016-03-11 14:59:50 -0800288// Records several metrics related to the time it takes to boot the device,
289// including disambiguating boot time on encrypted or non-encrypted devices.
290void RecordBootComplete() {
291 BootEventRecordStore boot_event_store;
James Hawkinsb9cf7712016-04-08 15:32:19 -0700292 BootEventRecordStore::BootEventRecord record;
James Hawkins2d8b3e62016-04-14 14:13:20 -0700293
James Hawkins1bfcaec2017-05-19 14:27:27 -0700294 auto time_since_epoch = android::base::boot_clock::now().time_since_epoch();
295 auto uptime = std::chrono::duration_cast<std::chrono::seconds>(time_since_epoch);
James Hawkins2d8b3e62016-04-14 14:13:20 -0700296 time_t current_time_utc = time(nullptr);
297
298 if (boot_event_store.GetBootEvent("last_boot_time_utc", &record)) {
299 time_t last_boot_time_utc = record.second;
300 time_t time_since_last_boot = difftime(current_time_utc,
301 last_boot_time_utc);
302 boot_event_store.AddBootEventWithValue("time_since_last_boot",
303 time_since_last_boot);
304 }
305
306 boot_event_store.AddBootEventWithValue("last_boot_time_utc", current_time_utc);
James Hawkinsc08e9962016-03-11 14:59:50 -0800307
James Hawkinsb9cf7712016-04-08 15:32:19 -0700308 // The boot_complete metric has two variants: boot_complete and
309 // ota_boot_complete. The latter signifies that the device is booting after
310 // a system update.
311 std::string boot_complete_prefix = CalculateBootCompletePrefix();
James Hawkins4dded612016-07-28 11:50:23 -0700312 if (boot_complete_prefix.empty()) {
313 // The system is hosed because the build date property could not be read.
314 return;
315 }
James Hawkinsc08e9962016-03-11 14:59:50 -0800316
317 // post_decrypt_time_elapsed is only logged on encrypted devices.
318 if (boot_event_store.GetBootEvent("post_decrypt_time_elapsed", &record)) {
319 // Log the amount of time elapsed until the device is decrypted, which
320 // includes the variable amount of time the user takes to enter the
321 // decryption password.
James Hawkinse78ea772017-03-24 11:43:02 -0700322 boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime.count());
James Hawkinsc08e9962016-03-11 14:59:50 -0800323
324 // Subtract the decryption time to normalize the boot cycle timing.
James Hawkinse78ea772017-03-24 11:43:02 -0700325 std::chrono::seconds boot_complete = std::chrono::seconds(uptime.count() - record.second);
James Hawkinsb9cf7712016-04-08 15:32:19 -0700326 boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_post_decrypt",
James Hawkinse78ea772017-03-24 11:43:02 -0700327 boot_complete.count());
James Hawkinsc08e9962016-03-11 14:59:50 -0800328 } else {
James Hawkinse78ea772017-03-24 11:43:02 -0700329 boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_no_encryption",
330 uptime.count());
James Hawkinsc08e9962016-03-11 14:59:50 -0800331 }
332
333 // Record the total time from device startup to boot complete, regardless of
334 // encryption state.
James Hawkinse78ea772017-03-24 11:43:02 -0700335 boot_event_store.AddBootEventWithValue(boot_complete_prefix, uptime.count());
James Hawkinsef0a0902017-01-06 14:38:23 -0800336
337 RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init");
338 RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init.selinux");
339 RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init.cold_boot_wait");
James Hawkinsbe46fd12017-02-02 16:21:25 -0800340
James Hawkins1bfcaec2017-05-19 14:27:27 -0700341 const BootloaderTimingMap bootloader_timings = GetBootLoaderTimings();
342 RecordBootloaderTimings(&boot_event_store, bootloader_timings);
343
344 auto uptime_ms = std::chrono::duration_cast<std::chrono::milliseconds>(time_since_epoch);
345 RecordAbsoluteBootTime(&boot_event_store, bootloader_timings, uptime_ms);
James Hawkinsc08e9962016-03-11 14:59:50 -0800346}
347
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800348// Records the boot_reason metric by querying the ro.boot.bootreason system
349// property.
350void RecordBootReason() {
351 int32_t boot_reason = BootReasonStrToEnum(GetProperty("ro.boot.bootreason"));
352 BootEventRecordStore boot_event_store;
353 boot_event_store.AddBootEventWithValue("boot_reason", boot_reason);
354}
355
James Hawkins500d7152016-02-16 15:05:54 -0800356// Records two metrics related to the user resetting a device: the time at
357// which the device is reset, and the time since the user last reset the
358// device. The former is only set once per-factory reset.
359void RecordFactoryReset() {
360 BootEventRecordStore boot_event_store;
361 BootEventRecordStore::BootEventRecord record;
362
363 time_t current_time_utc = time(nullptr);
364
James Hawkins0660b302016-03-08 16:18:15 -0800365 if (current_time_utc < 0) {
366 // UMA does not display negative values in buckets, so convert to positive.
James Hawkins9aec9262017-01-31 11:42:24 -0800367 android::metricslogger::LogHistogram(
James Hawkinsfff95ba2016-03-29 16:13:49 -0700368 "factory_reset_current_time_failure", std::abs(current_time_utc));
369
James Hawkins9aec9262017-01-31 11:42:24 -0800370 // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram
James Hawkinsfff95ba2016-03-29 16:13:49 -0700371 // is losing records somehow.
372 boot_event_store.AddBootEventWithValue(
373 "factory_reset_current_time_failure", std::abs(current_time_utc));
James Hawkins0660b302016-03-08 16:18:15 -0800374 return;
375 } else {
James Hawkins9aec9262017-01-31 11:42:24 -0800376 android::metricslogger::LogHistogram("factory_reset_current_time", current_time_utc);
James Hawkinsfff95ba2016-03-29 16:13:49 -0700377
James Hawkins9aec9262017-01-31 11:42:24 -0800378 // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram
James Hawkinsfff95ba2016-03-29 16:13:49 -0700379 // is losing records somehow.
380 boot_event_store.AddBootEventWithValue(
381 "factory_reset_current_time", current_time_utc);
James Hawkins0660b302016-03-08 16:18:15 -0800382 }
383
James Hawkins500d7152016-02-16 15:05:54 -0800384 // The factory_reset boot event does not exist after the device is reset, so
385 // use this signal to mark the time of the factory reset.
386 if (!boot_event_store.GetBootEvent("factory_reset", &record)) {
387 boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc);
James Hawkins3bf9b142016-03-03 14:50:24 -0800388
389 // Don't log the time_since_factory_reset until some time has elapsed.
390 // The data is not meaningful yet and skews the histogram buckets.
James Hawkins500d7152016-02-16 15:05:54 -0800391 return;
392 }
393
394 // Calculate and record the difference in time between now and the
395 // factory_reset time.
396 time_t factory_reset_utc = record.second;
James Hawkins9aec9262017-01-31 11:42:24 -0800397 android::metricslogger::LogHistogram("factory_reset_record_value", factory_reset_utc);
James Hawkinsfff95ba2016-03-29 16:13:49 -0700398
James Hawkins9aec9262017-01-31 11:42:24 -0800399 // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram
James Hawkinsfff95ba2016-03-29 16:13:49 -0700400 // is losing records somehow.
401 boot_event_store.AddBootEventWithValue(
402 "factory_reset_record_value", factory_reset_utc);
403
James Hawkins500d7152016-02-16 15:05:54 -0800404 time_t time_since_factory_reset = difftime(current_time_utc,
405 factory_reset_utc);
406 boot_event_store.AddBootEventWithValue("time_since_factory_reset",
407 time_since_factory_reset);
408}
409
James Hawkinsabd73e62016-01-19 15:10:38 -0800410} // namespace
411
412int main(int argc, char **argv) {
413 android::base::InitLogging(argv);
414
415 const std::string cmd_line = GetCommandLine(argc, argv);
416 LOG(INFO) << "Service started: " << cmd_line;
417
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800418 int option_index = 0;
James Hawkinsc6275582016-03-22 10:47:44 -0700419 static const char value_str[] = "value";
James Hawkinsc08e9962016-03-11 14:59:50 -0800420 static const char boot_complete_str[] = "record_boot_complete";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800421 static const char boot_reason_str[] = "record_boot_reason";
James Hawkins53684ea2016-02-23 16:18:19 -0800422 static const char factory_reset_str[] = "record_time_since_factory_reset";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800423 static const struct option long_options[] = {
424 { "help", no_argument, NULL, 'h' },
425 { "log", no_argument, NULL, 'l' },
426 { "print", no_argument, NULL, 'p' },
427 { "record", required_argument, NULL, 'r' },
James Hawkinsc6275582016-03-22 10:47:44 -0700428 { value_str, required_argument, NULL, 0 },
James Hawkinsc08e9962016-03-11 14:59:50 -0800429 { boot_complete_str, no_argument, NULL, 0 },
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800430 { boot_reason_str, no_argument, NULL, 0 },
James Hawkins500d7152016-02-16 15:05:54 -0800431 { factory_reset_str, no_argument, NULL, 0 },
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800432 { NULL, 0, NULL, 0 }
433 };
434
James Hawkinsc6275582016-03-22 10:47:44 -0700435 std::string boot_event;
436 std::string value;
James Hawkinsabd73e62016-01-19 15:10:38 -0800437 int opt = 0;
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800438 while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) {
James Hawkinsabd73e62016-01-19 15:10:38 -0800439 switch (opt) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800440 // This case handles long options which have no single-character mapping.
441 case 0: {
442 const std::string option_name = long_options[option_index].name;
James Hawkinsc6275582016-03-22 10:47:44 -0700443 if (option_name == value_str) {
444 // |optarg| is an external variable set by getopt representing
445 // the option argument.
446 value = optarg;
447 } else if (option_name == boot_complete_str) {
James Hawkinsc08e9962016-03-11 14:59:50 -0800448 RecordBootComplete();
449 } else if (option_name == boot_reason_str) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800450 RecordBootReason();
James Hawkins500d7152016-02-16 15:05:54 -0800451 } else if (option_name == factory_reset_str) {
452 RecordFactoryReset();
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800453 } else {
454 LOG(ERROR) << "Invalid option: " << option_name;
455 }
456 break;
457 }
458
James Hawkinsabd73e62016-01-19 15:10:38 -0800459 case 'h': {
460 ShowHelp(argv[0]);
461 break;
462 }
463
464 case 'l': {
465 LogBootEvents();
466 break;
467 }
468
469 case 'p': {
470 PrintBootEvents();
471 break;
472 }
473
474 case 'r': {
475 // |optarg| is an external variable set by getopt representing
476 // the option argument.
James Hawkinsc6275582016-03-22 10:47:44 -0700477 boot_event = optarg;
James Hawkinsabd73e62016-01-19 15:10:38 -0800478 break;
479 }
480
481 default: {
482 DCHECK_EQ(opt, '?');
483
484 // |optopt| is an external variable set by getopt representing
485 // the value of the invalid option.
486 LOG(ERROR) << "Invalid option: " << optopt;
487 ShowHelp(argv[0]);
488 return EXIT_FAILURE;
489 }
490 }
491 }
492
James Hawkinsc6275582016-03-22 10:47:44 -0700493 if (!boot_event.empty()) {
494 RecordBootEventFromCommandLine(boot_event, value);
495 }
496
James Hawkinsabd73e62016-01-19 15:10:38 -0800497 return 0;
498}