blob: 344fa9aae39d3daee1a151d5634e6514800f0689 [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"
Yongqin Liu78b2b942017-07-07 13:26:49 +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"
92 " --value Optional value to associate with the boot event\n"
93 " --record_boot_complete Record metrics related to the time for the device boot\n"
94 " --record_boot_reason Record the reason why the device booted\n"
James Hawkins53684ea2016-02-23 16:18:19 -080095 " --record_time_since_factory_reset Record the time since the device was reset\n");
James Hawkinsabd73e62016-01-19 15:10:38 -080096}
97
98// Constructs a readable, printable string from the givencommand line
99// arguments.
100std::string GetCommandLine(int argc, char **argv) {
101 std::string cmd;
102 for (int i = 0; i < argc; ++i) {
103 cmd += argv[i];
104 cmd += " ";
105 }
106
107 return cmd;
108}
109
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800110// Convenience wrapper over the property API that returns an
111// std::string.
112std::string GetProperty(const char* key) {
113 std::vector<char> temp(PROPERTY_VALUE_MAX);
114 const int len = property_get(key, &temp[0], nullptr);
115 if (len < 0) {
116 return "";
117 }
118 return std::string(&temp[0], len);
119}
120
James Hawkins6f74c0b2016-02-12 15:49:16 -0800121constexpr int32_t kUnknownBootReason = 1;
122
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800123// A mapping from boot reason string, as read from the ro.boot.bootreason
124// system property, to a unique integer ID. Viewers of log data dashboards for
125// the boot_reason metric may refer to this mapping to discern the histogram
126// values.
James Hawkins6f74c0b2016-02-12 15:49:16 -0800127const std::map<std::string, int32_t> kBootReasonMap = {
128 {"unknown", kUnknownBootReason},
129 {"normal", 2},
130 {"recovery", 3},
131 {"reboot", 4},
132 {"PowerKey", 5},
133 {"hard_reset", 6},
134 {"kernel_panic", 7},
135 {"rpm_err", 8},
136 {"hw_reset", 9},
137 {"tz_err", 10},
138 {"adsp_err", 11},
139 {"modem_err", 12},
140 {"mba_err", 13},
141 {"Watchdog", 14},
142 {"Panic", 15},
143 {"power_key", 16},
144 {"power_on", 17},
145 {"Reboot", 18},
146 {"rtc", 19},
147 {"edl", 20},
James Hawkins45ead352016-03-08 16:42:07 -0800148 {"oem_pon1", 21},
149 {"oem_powerkey", 22},
150 {"oem_unknown_reset", 23},
151 {"srto: HWWDT reset SC", 24},
152 {"srto: HWWDT reset platform", 25},
153 {"srto: bootloader", 26},
154 {"srto: kernel panic", 27},
155 {"srto: kernel watchdog reset", 28},
156 {"srto: normal", 29},
157 {"srto: reboot", 30},
158 {"srto: reboot-bootloader", 31},
159 {"srto: security watchdog reset", 32},
160 {"srto: wakesrc", 33},
161 {"srto: watchdog", 34},
162 {"srto:1-1", 35},
163 {"srto:omap_hsmm", 36},
164 {"srto:phy0", 37},
165 {"srto:rtc0", 38},
166 {"srto:touchpad", 39},
167 {"watchdog", 40},
168 {"watchdogr", 41},
169 {"wdog_bark", 42},
170 {"wdog_bite", 43},
171 {"wdog_reset", 44},
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800172};
173
174// Converts a string value representing the reason the system booted to an
175// integer representation. This is necessary for logging the boot_reason metric
176// via Tron, which does not accept non-integer buckets in histograms.
177int32_t BootReasonStrToEnum(const std::string& boot_reason) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800178 auto mapping = kBootReasonMap.find(boot_reason);
179 if (mapping != kBootReasonMap.end()) {
180 return mapping->second;
181 }
182
183 LOG(INFO) << "Unknown boot reason: " << boot_reason;
184 return kUnknownBootReason;
185}
186
James Hawkinsb9cf7712016-04-08 15:32:19 -0700187// Returns the appropriate metric key prefix for the boot_complete metric such
188// that boot metrics after a system update are labeled as ota_boot_complete;
189// otherwise, they are labeled as boot_complete. This method encapsulates the
190// bookkeeping required to track when a system update has occurred by storing
191// the UTC timestamp of the system build date and comparing against the current
192// system build date.
193std::string CalculateBootCompletePrefix() {
194 static const std::string kBuildDateKey = "build_date";
195 std::string boot_complete_prefix = "boot_complete";
196
197 std::string build_date_str = GetProperty("ro.build.date.utc");
James Hawkins4dded612016-07-28 11:50:23 -0700198 int32_t build_date;
Elliott Hughesda46b392016-10-11 17:09:00 -0700199 if (!android::base::ParseInt(build_date_str, &build_date)) {
James Hawkins4dded612016-07-28 11:50:23 -0700200 return std::string();
201 }
James Hawkinsb9cf7712016-04-08 15:32:19 -0700202
203 BootEventRecordStore boot_event_store;
204 BootEventRecordStore::BootEventRecord record;
James Hawkins0bc4ad42017-05-30 15:03:15 -0700205 if (!boot_event_store.GetBootEvent(kBuildDateKey, &record)) {
206 boot_complete_prefix = "factory_reset_" + boot_complete_prefix;
207 boot_event_store.AddBootEventWithValue(kBuildDateKey, build_date);
208 } else if (build_date != record.second) {
James Hawkinsb9cf7712016-04-08 15:32:19 -0700209 boot_complete_prefix = "ota_" + boot_complete_prefix;
210 boot_event_store.AddBootEventWithValue(kBuildDateKey, build_date);
211 }
212
213 return boot_complete_prefix;
214}
215
James Hawkinsef0a0902017-01-06 14:38:23 -0800216// Records the value of a given ro.boottime.init property in milliseconds.
217void RecordInitBootTimeProp(
218 BootEventRecordStore* boot_event_store, const char* property) {
219 std::string value = GetProperty(property);
220
James Hawkins27c05222017-01-26 11:55:44 -0800221 int32_t time_in_ms;
222 if (android::base::ParseInt(value, &time_in_ms)) {
James Hawkinsef0a0902017-01-06 14:38:23 -0800223 boot_event_store->AddBootEventWithValue(property, time_in_ms);
224 }
225}
226
James Hawkins1bfcaec2017-05-19 14:27:27 -0700227// A map from bootloader timing stage to the time that stage took during boot.
228typedef std::map<std::string, int32_t> BootloaderTimingMap;
229
230// Returns a mapping from bootloader stage names to the time those stages
231// took to boot.
232const BootloaderTimingMap GetBootLoaderTimings() {
233 BootloaderTimingMap timings;
234
235 // |ro.boot.boottime| is of the form 'stage1:time1,...,stageN:timeN',
236 // where timeN is in milliseconds.
James Hawkinsbe46fd12017-02-02 16:21:25 -0800237 std::string value = GetProperty("ro.boot.boottime");
James Hawkins6b5c5aa2017-02-16 11:53:03 -0800238 if (value.empty()) {
239 // ro.boot.boottime is not reported on all devices.
James Hawkins1bfcaec2017-05-19 14:27:27 -0700240 return BootloaderTimingMap();
James Hawkins6b5c5aa2017-02-16 11:53:03 -0800241 }
James Hawkinsbe46fd12017-02-02 16:21:25 -0800242
243 auto stages = android::base::Split(value, ",");
James Hawkins1bfcaec2017-05-19 14:27:27 -0700244 for (const auto& stageTiming : stages) {
James Hawkinsbe46fd12017-02-02 16:21:25 -0800245 // |stageTiming| is of the form 'stage:time'.
246 auto stageTimingValues = android::base::Split(stageTiming, ":");
James Hawkins0bc4ad42017-05-30 15:03:15 -0700247 DCHECK_EQ(2U, stageTimingValues.size());
James Hawkinsbe46fd12017-02-02 16:21:25 -0800248
249 std::string stageName = stageTimingValues[0];
250 int32_t time_ms;
251 if (android::base::ParseInt(stageTimingValues[1], &time_ms)) {
James Hawkins1bfcaec2017-05-19 14:27:27 -0700252 timings[stageName] = time_ms;
James Hawkinsbe46fd12017-02-02 16:21:25 -0800253 }
254 }
James Hawkins6b5c5aa2017-02-16 11:53:03 -0800255
James Hawkins1bfcaec2017-05-19 14:27:27 -0700256 return timings;
257}
258
259// Parses and records the set of bootloader stages and associated boot times
260// from the ro.boot.boottime system property.
261void RecordBootloaderTimings(BootEventRecordStore* boot_event_store,
262 const BootloaderTimingMap& bootloader_timings) {
263 int32_t total_time = 0;
264 for (const auto& timing : bootloader_timings) {
265 total_time += timing.second;
266 boot_event_store->AddBootEventWithValue("boottime.bootloader." + timing.first, timing.second);
267 }
268
James Hawkins6b5c5aa2017-02-16 11:53:03 -0800269 boot_event_store->AddBootEventWithValue("boottime.bootloader.total", total_time);
James Hawkinsbe46fd12017-02-02 16:21:25 -0800270}
271
James Hawkins1bfcaec2017-05-19 14:27:27 -0700272// Records the closest estimation to the absolute device boot time, i.e.,
273// from power on to boot_complete, including bootloader times.
274void RecordAbsoluteBootTime(BootEventRecordStore* boot_event_store,
275 const BootloaderTimingMap& bootloader_timings,
276 std::chrono::milliseconds uptime) {
277 int32_t bootloader_time_ms = 0;
278
279 for (const auto& timing : bootloader_timings) {
280 if (timing.first.compare("SW") != 0) {
281 bootloader_time_ms += timing.second;
282 }
283 }
284
285 auto bootloader_duration = std::chrono::milliseconds(bootloader_time_ms);
286 auto absolute_total =
287 std::chrono::duration_cast<std::chrono::seconds>(bootloader_duration + uptime);
288 boot_event_store->AddBootEventWithValue("absolute_boot_time", absolute_total.count());
289}
290
James Hawkinsc08e9962016-03-11 14:59:50 -0800291// Records several metrics related to the time it takes to boot the device,
292// including disambiguating boot time on encrypted or non-encrypted devices.
293void RecordBootComplete() {
294 BootEventRecordStore boot_event_store;
James Hawkinsb9cf7712016-04-08 15:32:19 -0700295 BootEventRecordStore::BootEventRecord record;
James Hawkins2d8b3e62016-04-14 14:13:20 -0700296
James Hawkins1bfcaec2017-05-19 14:27:27 -0700297 auto time_since_epoch = android::base::boot_clock::now().time_since_epoch();
298 auto uptime = std::chrono::duration_cast<std::chrono::seconds>(time_since_epoch);
James Hawkins2d8b3e62016-04-14 14:13:20 -0700299 time_t current_time_utc = time(nullptr);
300
301 if (boot_event_store.GetBootEvent("last_boot_time_utc", &record)) {
302 time_t last_boot_time_utc = record.second;
303 time_t time_since_last_boot = difftime(current_time_utc,
304 last_boot_time_utc);
305 boot_event_store.AddBootEventWithValue("time_since_last_boot",
306 time_since_last_boot);
307 }
308
309 boot_event_store.AddBootEventWithValue("last_boot_time_utc", current_time_utc);
James Hawkinsc08e9962016-03-11 14:59:50 -0800310
James Hawkinsb9cf7712016-04-08 15:32:19 -0700311 // The boot_complete metric has two variants: boot_complete and
312 // ota_boot_complete. The latter signifies that the device is booting after
313 // a system update.
314 std::string boot_complete_prefix = CalculateBootCompletePrefix();
James Hawkins4dded612016-07-28 11:50:23 -0700315 if (boot_complete_prefix.empty()) {
316 // The system is hosed because the build date property could not be read.
317 return;
318 }
James Hawkinsc08e9962016-03-11 14:59:50 -0800319
320 // post_decrypt_time_elapsed is only logged on encrypted devices.
321 if (boot_event_store.GetBootEvent("post_decrypt_time_elapsed", &record)) {
322 // Log the amount of time elapsed until the device is decrypted, which
323 // includes the variable amount of time the user takes to enter the
324 // decryption password.
James Hawkinse78ea772017-03-24 11:43:02 -0700325 boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime.count());
James Hawkinsc08e9962016-03-11 14:59:50 -0800326
327 // Subtract the decryption time to normalize the boot cycle timing.
James Hawkinse78ea772017-03-24 11:43:02 -0700328 std::chrono::seconds boot_complete = std::chrono::seconds(uptime.count() - record.second);
James Hawkinsb9cf7712016-04-08 15:32:19 -0700329 boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_post_decrypt",
James Hawkinse78ea772017-03-24 11:43:02 -0700330 boot_complete.count());
James Hawkinsc08e9962016-03-11 14:59:50 -0800331 } else {
James Hawkinse78ea772017-03-24 11:43:02 -0700332 boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_no_encryption",
333 uptime.count());
James Hawkinsc08e9962016-03-11 14:59:50 -0800334 }
335
336 // Record the total time from device startup to boot complete, regardless of
337 // encryption state.
James Hawkinse78ea772017-03-24 11:43:02 -0700338 boot_event_store.AddBootEventWithValue(boot_complete_prefix, uptime.count());
James Hawkinsef0a0902017-01-06 14:38:23 -0800339
340 RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init");
341 RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init.selinux");
342 RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init.cold_boot_wait");
James Hawkinsbe46fd12017-02-02 16:21:25 -0800343
James Hawkins1bfcaec2017-05-19 14:27:27 -0700344 const BootloaderTimingMap bootloader_timings = GetBootLoaderTimings();
345 RecordBootloaderTimings(&boot_event_store, bootloader_timings);
346
347 auto uptime_ms = std::chrono::duration_cast<std::chrono::milliseconds>(time_since_epoch);
348 RecordAbsoluteBootTime(&boot_event_store, bootloader_timings, uptime_ms);
James Hawkinsc08e9962016-03-11 14:59:50 -0800349}
350
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800351// Records the boot_reason metric by querying the ro.boot.bootreason system
352// property.
353void RecordBootReason() {
354 int32_t boot_reason = BootReasonStrToEnum(GetProperty("ro.boot.bootreason"));
355 BootEventRecordStore boot_event_store;
356 boot_event_store.AddBootEventWithValue("boot_reason", boot_reason);
357}
358
James Hawkins500d7152016-02-16 15:05:54 -0800359// Records two metrics related to the user resetting a device: the time at
360// which the device is reset, and the time since the user last reset the
361// device. The former is only set once per-factory reset.
362void RecordFactoryReset() {
363 BootEventRecordStore boot_event_store;
364 BootEventRecordStore::BootEventRecord record;
365
366 time_t current_time_utc = time(nullptr);
367
James Hawkins0660b302016-03-08 16:18:15 -0800368 if (current_time_utc < 0) {
369 // UMA does not display negative values in buckets, so convert to positive.
James Hawkins9aec9262017-01-31 11:42:24 -0800370 android::metricslogger::LogHistogram(
James Hawkinsfff95ba2016-03-29 16:13:49 -0700371 "factory_reset_current_time_failure", std::abs(current_time_utc));
372
James Hawkins9aec9262017-01-31 11:42:24 -0800373 // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram
James Hawkinsfff95ba2016-03-29 16:13:49 -0700374 // is losing records somehow.
375 boot_event_store.AddBootEventWithValue(
376 "factory_reset_current_time_failure", std::abs(current_time_utc));
James Hawkins0660b302016-03-08 16:18:15 -0800377 return;
378 } else {
James Hawkins9aec9262017-01-31 11:42:24 -0800379 android::metricslogger::LogHistogram("factory_reset_current_time", current_time_utc);
James Hawkinsfff95ba2016-03-29 16:13:49 -0700380
James Hawkins9aec9262017-01-31 11:42:24 -0800381 // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram
James Hawkinsfff95ba2016-03-29 16:13:49 -0700382 // is losing records somehow.
383 boot_event_store.AddBootEventWithValue(
384 "factory_reset_current_time", current_time_utc);
James Hawkins0660b302016-03-08 16:18:15 -0800385 }
386
James Hawkins500d7152016-02-16 15:05:54 -0800387 // The factory_reset boot event does not exist after the device is reset, so
388 // use this signal to mark the time of the factory reset.
389 if (!boot_event_store.GetBootEvent("factory_reset", &record)) {
390 boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc);
James Hawkins3bf9b142016-03-03 14:50:24 -0800391
392 // Don't log the time_since_factory_reset until some time has elapsed.
393 // The data is not meaningful yet and skews the histogram buckets.
James Hawkins500d7152016-02-16 15:05:54 -0800394 return;
395 }
396
397 // Calculate and record the difference in time between now and the
398 // factory_reset time.
399 time_t factory_reset_utc = record.second;
James Hawkins9aec9262017-01-31 11:42:24 -0800400 android::metricslogger::LogHistogram("factory_reset_record_value", factory_reset_utc);
James Hawkinsfff95ba2016-03-29 16:13:49 -0700401
James Hawkins9aec9262017-01-31 11:42:24 -0800402 // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram
James Hawkinsfff95ba2016-03-29 16:13:49 -0700403 // is losing records somehow.
404 boot_event_store.AddBootEventWithValue(
405 "factory_reset_record_value", factory_reset_utc);
406
James Hawkins500d7152016-02-16 15:05:54 -0800407 time_t time_since_factory_reset = difftime(current_time_utc,
408 factory_reset_utc);
409 boot_event_store.AddBootEventWithValue("time_since_factory_reset",
410 time_since_factory_reset);
411}
412
James Hawkinsabd73e62016-01-19 15:10:38 -0800413} // namespace
414
415int main(int argc, char **argv) {
416 android::base::InitLogging(argv);
417
418 const std::string cmd_line = GetCommandLine(argc, argv);
419 LOG(INFO) << "Service started: " << cmd_line;
420
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800421 int option_index = 0;
James Hawkinsc6275582016-03-22 10:47:44 -0700422 static const char value_str[] = "value";
James Hawkinsc08e9962016-03-11 14:59:50 -0800423 static const char boot_complete_str[] = "record_boot_complete";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800424 static const char boot_reason_str[] = "record_boot_reason";
James Hawkins53684ea2016-02-23 16:18:19 -0800425 static const char factory_reset_str[] = "record_time_since_factory_reset";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800426 static const struct option long_options[] = {
427 { "help", no_argument, NULL, 'h' },
428 { "log", no_argument, NULL, 'l' },
429 { "print", no_argument, NULL, 'p' },
430 { "record", required_argument, NULL, 'r' },
James Hawkinsc6275582016-03-22 10:47:44 -0700431 { value_str, required_argument, NULL, 0 },
James Hawkinsc08e9962016-03-11 14:59:50 -0800432 { boot_complete_str, no_argument, NULL, 0 },
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800433 { boot_reason_str, no_argument, NULL, 0 },
James Hawkins500d7152016-02-16 15:05:54 -0800434 { factory_reset_str, no_argument, NULL, 0 },
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800435 { NULL, 0, NULL, 0 }
436 };
437
James Hawkinsc6275582016-03-22 10:47:44 -0700438 std::string boot_event;
439 std::string value;
James Hawkinsabd73e62016-01-19 15:10:38 -0800440 int opt = 0;
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800441 while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) {
James Hawkinsabd73e62016-01-19 15:10:38 -0800442 switch (opt) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800443 // This case handles long options which have no single-character mapping.
444 case 0: {
445 const std::string option_name = long_options[option_index].name;
James Hawkinsc6275582016-03-22 10:47:44 -0700446 if (option_name == value_str) {
447 // |optarg| is an external variable set by getopt representing
448 // the option argument.
449 value = optarg;
450 } else if (option_name == boot_complete_str) {
James Hawkinsc08e9962016-03-11 14:59:50 -0800451 RecordBootComplete();
452 } else if (option_name == boot_reason_str) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800453 RecordBootReason();
James Hawkins500d7152016-02-16 15:05:54 -0800454 } else if (option_name == factory_reset_str) {
455 RecordFactoryReset();
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800456 } else {
457 LOG(ERROR) << "Invalid option: " << option_name;
458 }
459 break;
460 }
461
James Hawkinsabd73e62016-01-19 15:10:38 -0800462 case 'h': {
463 ShowHelp(argv[0]);
464 break;
465 }
466
467 case 'l': {
468 LogBootEvents();
469 break;
470 }
471
472 case 'p': {
473 PrintBootEvents();
474 break;
475 }
476
477 case 'r': {
478 // |optarg| is an external variable set by getopt representing
479 // the option argument.
James Hawkinsc6275582016-03-22 10:47:44 -0700480 boot_event = optarg;
James Hawkinsabd73e62016-01-19 15:10:38 -0800481 break;
482 }
483
484 default: {
485 DCHECK_EQ(opt, '?');
486
487 // |optopt| is an external variable set by getopt representing
488 // the value of the invalid option.
489 LOG(ERROR) << "Invalid option: " << optopt;
490 ShowHelp(argv[0]);
491 return EXIT_FAILURE;
492 }
493 }
494 }
495
James Hawkinsc6275582016-03-22 10:47:44 -0700496 if (!boot_event.empty()) {
497 RecordBootEventFromCommandLine(boot_event, value);
498 }
499
James Hawkinsabd73e62016-01-19 15:10:38 -0800500 return 0;
501}