blob: bd611f0b9fd0704b50a256df408742917c5c4df5 [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 Hawkins8d7f63d2017-07-25 12:55:12 -0700172 {"shutdown,", 45}, // Trailing comma is intentional.
173 {"shutdown,userrequested", 46},
174 {"reboot,bootloader", 47},
175 {"reboot,cold", 48},
176 {"reboot,recovery", 49},
177 {"thermal_shutdown", 50},
178 {"s3_wakeup", 51}
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800179};
180
181// Converts a string value representing the reason the system booted to an
182// integer representation. This is necessary for logging the boot_reason metric
183// via Tron, which does not accept non-integer buckets in histograms.
184int32_t BootReasonStrToEnum(const std::string& boot_reason) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800185 auto mapping = kBootReasonMap.find(boot_reason);
186 if (mapping != kBootReasonMap.end()) {
187 return mapping->second;
188 }
189
190 LOG(INFO) << "Unknown boot reason: " << boot_reason;
191 return kUnknownBootReason;
192}
193
James Hawkinsb9cf7712016-04-08 15:32:19 -0700194// Returns the appropriate metric key prefix for the boot_complete metric such
195// that boot metrics after a system update are labeled as ota_boot_complete;
196// otherwise, they are labeled as boot_complete. This method encapsulates the
197// bookkeeping required to track when a system update has occurred by storing
198// the UTC timestamp of the system build date and comparing against the current
199// system build date.
200std::string CalculateBootCompletePrefix() {
201 static const std::string kBuildDateKey = "build_date";
202 std::string boot_complete_prefix = "boot_complete";
203
204 std::string build_date_str = GetProperty("ro.build.date.utc");
James Hawkins4dded612016-07-28 11:50:23 -0700205 int32_t build_date;
Elliott Hughesda46b392016-10-11 17:09:00 -0700206 if (!android::base::ParseInt(build_date_str, &build_date)) {
James Hawkins4dded612016-07-28 11:50:23 -0700207 return std::string();
208 }
James Hawkinsb9cf7712016-04-08 15:32:19 -0700209
210 BootEventRecordStore boot_event_store;
211 BootEventRecordStore::BootEventRecord record;
James Hawkins0bc4ad42017-05-30 15:03:15 -0700212 if (!boot_event_store.GetBootEvent(kBuildDateKey, &record)) {
213 boot_complete_prefix = "factory_reset_" + boot_complete_prefix;
214 boot_event_store.AddBootEventWithValue(kBuildDateKey, build_date);
215 } else if (build_date != record.second) {
James Hawkinsb9cf7712016-04-08 15:32:19 -0700216 boot_complete_prefix = "ota_" + boot_complete_prefix;
217 boot_event_store.AddBootEventWithValue(kBuildDateKey, build_date);
218 }
219
220 return boot_complete_prefix;
221}
222
James Hawkinsef0a0902017-01-06 14:38:23 -0800223// Records the value of a given ro.boottime.init property in milliseconds.
224void RecordInitBootTimeProp(
225 BootEventRecordStore* boot_event_store, const char* property) {
226 std::string value = GetProperty(property);
227
James Hawkins27c05222017-01-26 11:55:44 -0800228 int32_t time_in_ms;
229 if (android::base::ParseInt(value, &time_in_ms)) {
James Hawkinsef0a0902017-01-06 14:38:23 -0800230 boot_event_store->AddBootEventWithValue(property, time_in_ms);
231 }
232}
233
James Hawkins1bfcaec2017-05-19 14:27:27 -0700234// A map from bootloader timing stage to the time that stage took during boot.
235typedef std::map<std::string, int32_t> BootloaderTimingMap;
236
237// Returns a mapping from bootloader stage names to the time those stages
238// took to boot.
239const BootloaderTimingMap GetBootLoaderTimings() {
240 BootloaderTimingMap timings;
241
242 // |ro.boot.boottime| is of the form 'stage1:time1,...,stageN:timeN',
243 // where timeN is in milliseconds.
James Hawkinsbe46fd12017-02-02 16:21:25 -0800244 std::string value = GetProperty("ro.boot.boottime");
James Hawkins6b5c5aa2017-02-16 11:53:03 -0800245 if (value.empty()) {
246 // ro.boot.boottime is not reported on all devices.
James Hawkins1bfcaec2017-05-19 14:27:27 -0700247 return BootloaderTimingMap();
James Hawkins6b5c5aa2017-02-16 11:53:03 -0800248 }
James Hawkinsbe46fd12017-02-02 16:21:25 -0800249
250 auto stages = android::base::Split(value, ",");
James Hawkins1bfcaec2017-05-19 14:27:27 -0700251 for (const auto& stageTiming : stages) {
James Hawkinsbe46fd12017-02-02 16:21:25 -0800252 // |stageTiming| is of the form 'stage:time'.
253 auto stageTimingValues = android::base::Split(stageTiming, ":");
James Hawkins0bc4ad42017-05-30 15:03:15 -0700254 DCHECK_EQ(2U, stageTimingValues.size());
James Hawkinsbe46fd12017-02-02 16:21:25 -0800255
256 std::string stageName = stageTimingValues[0];
257 int32_t time_ms;
258 if (android::base::ParseInt(stageTimingValues[1], &time_ms)) {
James Hawkins1bfcaec2017-05-19 14:27:27 -0700259 timings[stageName] = time_ms;
James Hawkinsbe46fd12017-02-02 16:21:25 -0800260 }
261 }
James Hawkins6b5c5aa2017-02-16 11:53:03 -0800262
James Hawkins1bfcaec2017-05-19 14:27:27 -0700263 return timings;
264}
265
266// Parses and records the set of bootloader stages and associated boot times
267// from the ro.boot.boottime system property.
268void RecordBootloaderTimings(BootEventRecordStore* boot_event_store,
269 const BootloaderTimingMap& bootloader_timings) {
270 int32_t total_time = 0;
271 for (const auto& timing : bootloader_timings) {
272 total_time += timing.second;
273 boot_event_store->AddBootEventWithValue("boottime.bootloader." + timing.first, timing.second);
274 }
275
James Hawkins6b5c5aa2017-02-16 11:53:03 -0800276 boot_event_store->AddBootEventWithValue("boottime.bootloader.total", total_time);
James Hawkinsbe46fd12017-02-02 16:21:25 -0800277}
278
James Hawkins1bfcaec2017-05-19 14:27:27 -0700279// Records the closest estimation to the absolute device boot time, i.e.,
280// from power on to boot_complete, including bootloader times.
281void RecordAbsoluteBootTime(BootEventRecordStore* boot_event_store,
282 const BootloaderTimingMap& bootloader_timings,
283 std::chrono::milliseconds uptime) {
284 int32_t bootloader_time_ms = 0;
285
286 for (const auto& timing : bootloader_timings) {
287 if (timing.first.compare("SW") != 0) {
288 bootloader_time_ms += timing.second;
289 }
290 }
291
292 auto bootloader_duration = std::chrono::milliseconds(bootloader_time_ms);
293 auto absolute_total =
294 std::chrono::duration_cast<std::chrono::seconds>(bootloader_duration + uptime);
295 boot_event_store->AddBootEventWithValue("absolute_boot_time", absolute_total.count());
296}
297
James Hawkinsc08e9962016-03-11 14:59:50 -0800298// Records several metrics related to the time it takes to boot the device,
299// including disambiguating boot time on encrypted or non-encrypted devices.
300void RecordBootComplete() {
301 BootEventRecordStore boot_event_store;
James Hawkinsb9cf7712016-04-08 15:32:19 -0700302 BootEventRecordStore::BootEventRecord record;
James Hawkins2d8b3e62016-04-14 14:13:20 -0700303
James Hawkins1bfcaec2017-05-19 14:27:27 -0700304 auto time_since_epoch = android::base::boot_clock::now().time_since_epoch();
305 auto uptime = std::chrono::duration_cast<std::chrono::seconds>(time_since_epoch);
James Hawkins2d8b3e62016-04-14 14:13:20 -0700306 time_t current_time_utc = time(nullptr);
307
308 if (boot_event_store.GetBootEvent("last_boot_time_utc", &record)) {
309 time_t last_boot_time_utc = record.second;
310 time_t time_since_last_boot = difftime(current_time_utc,
311 last_boot_time_utc);
312 boot_event_store.AddBootEventWithValue("time_since_last_boot",
313 time_since_last_boot);
314 }
315
316 boot_event_store.AddBootEventWithValue("last_boot_time_utc", current_time_utc);
James Hawkinsc08e9962016-03-11 14:59:50 -0800317
James Hawkinsb9cf7712016-04-08 15:32:19 -0700318 // The boot_complete metric has two variants: boot_complete and
319 // ota_boot_complete. The latter signifies that the device is booting after
320 // a system update.
321 std::string boot_complete_prefix = CalculateBootCompletePrefix();
James Hawkins4dded612016-07-28 11:50:23 -0700322 if (boot_complete_prefix.empty()) {
323 // The system is hosed because the build date property could not be read.
324 return;
325 }
James Hawkinsc08e9962016-03-11 14:59:50 -0800326
327 // post_decrypt_time_elapsed is only logged on encrypted devices.
328 if (boot_event_store.GetBootEvent("post_decrypt_time_elapsed", &record)) {
329 // Log the amount of time elapsed until the device is decrypted, which
330 // includes the variable amount of time the user takes to enter the
331 // decryption password.
James Hawkinse78ea772017-03-24 11:43:02 -0700332 boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime.count());
James Hawkinsc08e9962016-03-11 14:59:50 -0800333
334 // Subtract the decryption time to normalize the boot cycle timing.
James Hawkinse78ea772017-03-24 11:43:02 -0700335 std::chrono::seconds boot_complete = std::chrono::seconds(uptime.count() - record.second);
James Hawkinsb9cf7712016-04-08 15:32:19 -0700336 boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_post_decrypt",
James Hawkinse78ea772017-03-24 11:43:02 -0700337 boot_complete.count());
James Hawkinsc08e9962016-03-11 14:59:50 -0800338 } else {
James Hawkinse78ea772017-03-24 11:43:02 -0700339 boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_no_encryption",
340 uptime.count());
James Hawkinsc08e9962016-03-11 14:59:50 -0800341 }
342
343 // Record the total time from device startup to boot complete, regardless of
344 // encryption state.
James Hawkinse78ea772017-03-24 11:43:02 -0700345 boot_event_store.AddBootEventWithValue(boot_complete_prefix, uptime.count());
James Hawkinsef0a0902017-01-06 14:38:23 -0800346
347 RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init");
348 RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init.selinux");
349 RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init.cold_boot_wait");
James Hawkinsbe46fd12017-02-02 16:21:25 -0800350
James Hawkins1bfcaec2017-05-19 14:27:27 -0700351 const BootloaderTimingMap bootloader_timings = GetBootLoaderTimings();
352 RecordBootloaderTimings(&boot_event_store, bootloader_timings);
353
354 auto uptime_ms = std::chrono::duration_cast<std::chrono::milliseconds>(time_since_epoch);
355 RecordAbsoluteBootTime(&boot_event_store, bootloader_timings, uptime_ms);
James Hawkinsc08e9962016-03-11 14:59:50 -0800356}
357
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800358// Records the boot_reason metric by querying the ro.boot.bootreason system
359// property.
360void RecordBootReason() {
361 int32_t boot_reason = BootReasonStrToEnum(GetProperty("ro.boot.bootreason"));
362 BootEventRecordStore boot_event_store;
363 boot_event_store.AddBootEventWithValue("boot_reason", boot_reason);
364}
365
James Hawkins500d7152016-02-16 15:05:54 -0800366// Records two metrics related to the user resetting a device: the time at
367// which the device is reset, and the time since the user last reset the
368// device. The former is only set once per-factory reset.
369void RecordFactoryReset() {
370 BootEventRecordStore boot_event_store;
371 BootEventRecordStore::BootEventRecord record;
372
373 time_t current_time_utc = time(nullptr);
374
James Hawkins0660b302016-03-08 16:18:15 -0800375 if (current_time_utc < 0) {
376 // UMA does not display negative values in buckets, so convert to positive.
James Hawkins9aec9262017-01-31 11:42:24 -0800377 android::metricslogger::LogHistogram(
James Hawkinsfff95ba2016-03-29 16:13:49 -0700378 "factory_reset_current_time_failure", std::abs(current_time_utc));
379
James Hawkins9aec9262017-01-31 11:42:24 -0800380 // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram
James Hawkinsfff95ba2016-03-29 16:13:49 -0700381 // is losing records somehow.
382 boot_event_store.AddBootEventWithValue(
383 "factory_reset_current_time_failure", std::abs(current_time_utc));
James Hawkins0660b302016-03-08 16:18:15 -0800384 return;
385 } else {
James Hawkins9aec9262017-01-31 11:42:24 -0800386 android::metricslogger::LogHistogram("factory_reset_current_time", current_time_utc);
James Hawkinsfff95ba2016-03-29 16:13:49 -0700387
James Hawkins9aec9262017-01-31 11:42:24 -0800388 // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram
James Hawkinsfff95ba2016-03-29 16:13:49 -0700389 // is losing records somehow.
390 boot_event_store.AddBootEventWithValue(
391 "factory_reset_current_time", current_time_utc);
James Hawkins0660b302016-03-08 16:18:15 -0800392 }
393
James Hawkins500d7152016-02-16 15:05:54 -0800394 // The factory_reset boot event does not exist after the device is reset, so
395 // use this signal to mark the time of the factory reset.
396 if (!boot_event_store.GetBootEvent("factory_reset", &record)) {
397 boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc);
James Hawkins3bf9b142016-03-03 14:50:24 -0800398
399 // Don't log the time_since_factory_reset until some time has elapsed.
400 // The data is not meaningful yet and skews the histogram buckets.
James Hawkins500d7152016-02-16 15:05:54 -0800401 return;
402 }
403
404 // Calculate and record the difference in time between now and the
405 // factory_reset time.
406 time_t factory_reset_utc = record.second;
James Hawkins9aec9262017-01-31 11:42:24 -0800407 android::metricslogger::LogHistogram("factory_reset_record_value", factory_reset_utc);
James Hawkinsfff95ba2016-03-29 16:13:49 -0700408
James Hawkins9aec9262017-01-31 11:42:24 -0800409 // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram
James Hawkinsfff95ba2016-03-29 16:13:49 -0700410 // is losing records somehow.
411 boot_event_store.AddBootEventWithValue(
412 "factory_reset_record_value", factory_reset_utc);
413
James Hawkins500d7152016-02-16 15:05:54 -0800414 time_t time_since_factory_reset = difftime(current_time_utc,
415 factory_reset_utc);
416 boot_event_store.AddBootEventWithValue("time_since_factory_reset",
417 time_since_factory_reset);
418}
419
James Hawkinsabd73e62016-01-19 15:10:38 -0800420} // namespace
421
422int main(int argc, char **argv) {
423 android::base::InitLogging(argv);
424
425 const std::string cmd_line = GetCommandLine(argc, argv);
426 LOG(INFO) << "Service started: " << cmd_line;
427
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800428 int option_index = 0;
James Hawkinsc6275582016-03-22 10:47:44 -0700429 static const char value_str[] = "value";
James Hawkinsc08e9962016-03-11 14:59:50 -0800430 static const char boot_complete_str[] = "record_boot_complete";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800431 static const char boot_reason_str[] = "record_boot_reason";
James Hawkins53684ea2016-02-23 16:18:19 -0800432 static const char factory_reset_str[] = "record_time_since_factory_reset";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800433 static const struct option long_options[] = {
434 { "help", no_argument, NULL, 'h' },
435 { "log", no_argument, NULL, 'l' },
436 { "print", no_argument, NULL, 'p' },
437 { "record", required_argument, NULL, 'r' },
James Hawkinsc6275582016-03-22 10:47:44 -0700438 { value_str, required_argument, NULL, 0 },
James Hawkinsc08e9962016-03-11 14:59:50 -0800439 { boot_complete_str, no_argument, NULL, 0 },
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800440 { boot_reason_str, no_argument, NULL, 0 },
James Hawkins500d7152016-02-16 15:05:54 -0800441 { factory_reset_str, no_argument, NULL, 0 },
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800442 { NULL, 0, NULL, 0 }
443 };
444
James Hawkinsc6275582016-03-22 10:47:44 -0700445 std::string boot_event;
446 std::string value;
James Hawkinsabd73e62016-01-19 15:10:38 -0800447 int opt = 0;
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800448 while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) {
James Hawkinsabd73e62016-01-19 15:10:38 -0800449 switch (opt) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800450 // This case handles long options which have no single-character mapping.
451 case 0: {
452 const std::string option_name = long_options[option_index].name;
James Hawkinsc6275582016-03-22 10:47:44 -0700453 if (option_name == value_str) {
454 // |optarg| is an external variable set by getopt representing
455 // the option argument.
456 value = optarg;
457 } else if (option_name == boot_complete_str) {
James Hawkinsc08e9962016-03-11 14:59:50 -0800458 RecordBootComplete();
459 } else if (option_name == boot_reason_str) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800460 RecordBootReason();
James Hawkins500d7152016-02-16 15:05:54 -0800461 } else if (option_name == factory_reset_str) {
462 RecordFactoryReset();
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800463 } else {
464 LOG(ERROR) << "Invalid option: " << option_name;
465 }
466 break;
467 }
468
James Hawkinsabd73e62016-01-19 15:10:38 -0800469 case 'h': {
470 ShowHelp(argv[0]);
471 break;
472 }
473
474 case 'l': {
475 LogBootEvents();
476 break;
477 }
478
479 case 'p': {
480 PrintBootEvents();
481 break;
482 }
483
484 case 'r': {
485 // |optarg| is an external variable set by getopt representing
486 // the option argument.
James Hawkinsc6275582016-03-22 10:47:44 -0700487 boot_event = optarg;
James Hawkinsabd73e62016-01-19 15:10:38 -0800488 break;
489 }
490
491 default: {
492 DCHECK_EQ(opt, '?');
493
494 // |optopt| is an external variable set by getopt representing
495 // the value of the invalid option.
496 LOG(ERROR) << "Invalid option: " << optopt;
497 ShowHelp(argv[0]);
498 return EXIT_FAILURE;
499 }
500 }
501 }
502
James Hawkinsc6275582016-03-22 10:47:44 -0700503 if (!boot_event.empty()) {
504 RecordBootEventFromCommandLine(boot_event, value);
505 }
506
James Hawkinsabd73e62016-01-19 15:10:38 -0800507 return 0;
508}