blob: 7c0b15ead2e8b9c854ac72b237271bf080480edf [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>
Mark Salyzynb304f6d2017-08-04 13:35:51 -070022#include <sys/klog.h>
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -070023#include <unistd.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070024
James Hawkinse78ea772017-03-24 11:43:02 -070025#include <chrono>
James Hawkins0660b302016-03-08 16:18:15 -080026#include <cmath>
James Hawkinsabd73e62016-01-19 15:10:38 -080027#include <cstddef>
28#include <cstdio>
James Hawkins500d7152016-02-16 15:05:54 -080029#include <ctime>
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080030#include <map>
James Hawkinsabd73e62016-01-19 15:10:38 -080031#include <memory>
32#include <string>
James Hawkinsbe46fd12017-02-02 16:21:25 -080033#include <vector>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070034
James Hawkinse78ea772017-03-24 11:43:02 -070035#include <android-base/chrono_utils.h>
Mark Salyzynb304f6d2017-08-04 13:35:51 -070036#include <android-base/file.h>
James Hawkinseabe08b2016-01-19 16:54:35 -080037#include <android-base/logging.h>
James Hawkins4dded612016-07-28 11:50:23 -070038#include <android-base/parseint.h>
James Hawkinsbe46fd12017-02-02 16:21:25 -080039#include <android-base/strings.h>
James Hawkinse78ea772017-03-24 11:43:02 -070040#include <android/log.h>
Mark Salyzynb304f6d2017-08-04 13:35:51 -070041#include <cutils/android_reboot.h>
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080042#include <cutils/properties.h>
Mark Salyzynb304f6d2017-08-04 13:35:51 -070043#include <log/logcat.h>
James Hawkins9aec9262017-01-31 11:42:24 -080044#include <metricslogger/metrics_logger.h>
Mark Salyzynff2dcd92016-09-28 15:54:45 -070045
James Hawkinsabd73e62016-01-19 15:10:38 -080046#include "boot_event_record_store.h"
James Hawkinsabd73e62016-01-19 15:10:38 -080047
48namespace {
49
James Hawkinsabd73e62016-01-19 15:10:38 -080050// Scans the boot event record store for record files and logs each boot event
51// via EventLog.
52void LogBootEvents() {
53 BootEventRecordStore boot_event_store;
54
55 auto events = boot_event_store.GetAllBootEvents();
56 for (auto i = events.cbegin(); i != events.cend(); ++i) {
James Hawkins9aec9262017-01-31 11:42:24 -080057 android::metricslogger::LogHistogram(i->first, i->second);
James Hawkinsabd73e62016-01-19 15:10:38 -080058 }
59}
60
James Hawkinsc6275582016-03-22 10:47:44 -070061// Records the named boot |event| to the record store. If |value| is non-empty
62// and is a proper string representation of an integer value, the converted
63// integer value is associated with the boot event.
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -070064void RecordBootEventFromCommandLine(const std::string& event, const std::string& value_str) {
James Hawkinsc6275582016-03-22 10:47:44 -070065 BootEventRecordStore boot_event_store;
66 if (!value_str.empty()) {
67 int32_t value = 0;
Elliott Hughesda46b392016-10-11 17:09:00 -070068 if (android::base::ParseInt(value_str, &value)) {
James Hawkins4dded612016-07-28 11:50:23 -070069 boot_event_store.AddBootEventWithValue(event, value);
70 }
James Hawkinsc6275582016-03-22 10:47:44 -070071 } else {
72 boot_event_store.AddBootEvent(event);
73 }
74}
75
James Hawkinsabd73e62016-01-19 15:10:38 -080076void PrintBootEvents() {
77 printf("Boot events:\n");
78 printf("------------\n");
79
80 BootEventRecordStore boot_event_store;
81 auto events = boot_event_store.GetAllBootEvents();
82 for (auto i = events.cbegin(); i != events.cend(); ++i) {
83 printf("%s\t%d\n", i->first.c_str(), i->second);
84 }
85}
86
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -070087void ShowHelp(const char* cmd) {
James Hawkinsabd73e62016-01-19 15:10:38 -080088 fprintf(stderr, "Usage: %s [options]\n", cmd);
89 fprintf(stderr,
90 "options include:\n"
Yongqin Liu78b2b942017-07-07 13:26:49 +080091 " -h, --help Show this help\n"
92 " -l, --log Log all metrics to logstorage\n"
93 " -p, --print Dump the boot event records to the console\n"
94 " -r, --record Record the timestamp of a named boot event\n"
95 " --value Optional value to associate with the boot event\n"
96 " --record_boot_complete Record metrics related to the time for the device boot\n"
97 " --record_boot_reason Record the reason why the device booted\n"
James Hawkins53684ea2016-02-23 16:18:19 -080098 " --record_time_since_factory_reset Record the time since the device was reset\n");
James Hawkinsabd73e62016-01-19 15:10:38 -080099}
100
101// Constructs a readable, printable string from the givencommand line
102// arguments.
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700103std::string GetCommandLine(int argc, char** argv) {
James Hawkinsabd73e62016-01-19 15:10:38 -0800104 std::string cmd;
105 for (int i = 0; i < argc; ++i) {
106 cmd += argv[i];
107 cmd += " ";
108 }
109
110 return cmd;
111}
112
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800113// Convenience wrapper over the property API that returns an
114// std::string.
115std::string GetProperty(const char* key) {
116 std::vector<char> temp(PROPERTY_VALUE_MAX);
117 const int len = property_get(key, &temp[0], nullptr);
118 if (len < 0) {
119 return "";
120 }
121 return std::string(&temp[0], len);
122}
123
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700124void SetProperty(const char* key, const std::string& val) {
125 property_set(key, val.c_str());
126}
127
128void SetProperty(const char* key, const char* val) {
129 property_set(key, val);
130}
131
James Hawkins6f74c0b2016-02-12 15:49:16 -0800132constexpr int32_t kUnknownBootReason = 1;
133
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800134// A mapping from boot reason string, as read from the ro.boot.bootreason
135// system property, to a unique integer ID. Viewers of log data dashboards for
136// the boot_reason metric may refer to this mapping to discern the histogram
137// values.
James Hawkins6f74c0b2016-02-12 15:49:16 -0800138const std::map<std::string, int32_t> kBootReasonMap = {
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700139 {"unknown", kUnknownBootReason},
140 {"normal", 2},
141 {"recovery", 3},
142 {"reboot", 4},
143 {"PowerKey", 5},
144 {"hard_reset", 6},
145 {"kernel_panic", 7},
146 {"rpm_err", 8},
147 {"hw_reset", 9},
148 {"tz_err", 10},
149 {"adsp_err", 11},
150 {"modem_err", 12},
151 {"mba_err", 13},
152 {"Watchdog", 14},
153 {"Panic", 15},
154 {"power_key", 16},
155 {"power_on", 17},
156 {"Reboot", 18},
157 {"rtc", 19},
158 {"edl", 20},
159 {"oem_pon1", 21},
160 {"oem_powerkey", 22},
161 {"oem_unknown_reset", 23},
162 {"srto: HWWDT reset SC", 24},
163 {"srto: HWWDT reset platform", 25},
164 {"srto: bootloader", 26},
165 {"srto: kernel panic", 27},
166 {"srto: kernel watchdog reset", 28},
167 {"srto: normal", 29},
168 {"srto: reboot", 30},
169 {"srto: reboot-bootloader", 31},
170 {"srto: security watchdog reset", 32},
171 {"srto: wakesrc", 33},
172 {"srto: watchdog", 34},
173 {"srto:1-1", 35},
174 {"srto:omap_hsmm", 36},
175 {"srto:phy0", 37},
176 {"srto:rtc0", 38},
177 {"srto:touchpad", 39},
178 {"watchdog", 40},
179 {"watchdogr", 41},
180 {"wdog_bark", 42},
181 {"wdog_bite", 43},
182 {"wdog_reset", 44},
183 {"shutdown,", 45}, // Trailing comma is intentional.
184 {"shutdown,userrequested", 46},
185 {"reboot,bootloader", 47},
186 {"reboot,cold", 48},
187 {"reboot,recovery", 49},
188 {"thermal_shutdown", 50},
189 {"s3_wakeup", 51},
190 {"kernel_panic,sysrq", 52},
191 {"kernel_panic,NULL", 53},
192 {"kernel_panic,BUG", 54},
193 {"bootloader", 55},
194 {"cold", 56},
195 {"hard", 57},
196 {"warm", 58},
197 {"recovery", 59},
198 {"thermal-shutdown", 60},
199 {"shutdown,thermal", 61},
200 {"shutdown,battery", 62},
201 {"reboot,ota", 63},
202 {"reboot,factory_reset", 64},
203 {"reboot,", 65},
204 {"reboot,shell", 66},
205 {"reboot,adb", 67},
Mark Salyzyn9033bf52017-09-21 11:30:29 -0700206 {"reboot,userrequested", 68},
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800207};
208
209// Converts a string value representing the reason the system booted to an
210// integer representation. This is necessary for logging the boot_reason metric
211// via Tron, which does not accept non-integer buckets in histograms.
212int32_t BootReasonStrToEnum(const std::string& boot_reason) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800213 auto mapping = kBootReasonMap.find(boot_reason);
214 if (mapping != kBootReasonMap.end()) {
215 return mapping->second;
216 }
217
218 LOG(INFO) << "Unknown boot reason: " << boot_reason;
219 return kUnknownBootReason;
220}
221
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700222// Canonical list of supported primary reboot reasons.
223const std::vector<const std::string> knownReasons = {
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700224 // clang-format off
225 // kernel
226 "watchdog",
227 "kernel_panic",
228 // strong
229 "recovery", // Should not happen from ro.boot.bootreason
230 "bootloader", // Should not happen from ro.boot.bootreason
231 // blunt
232 "cold",
233 "hard",
234 "warm",
235 "shutdown", // Can not happen from ro.boot.bootreason
236 "reboot", // Default catch-all for anything unknown
237 // clang-format on
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700238};
239
240// Returns true if the supplied reason prefix is considered detailed enough.
241bool isStrongRebootReason(const std::string& r) {
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700242 for (auto& s : knownReasons) {
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700243 if (s == "cold") break;
244 // Prefix defined as terminated by a nul or comma (,).
245 if (android::base::StartsWith(r, s.c_str()) &&
246 ((r.length() == s.length()) || (r[s.length()] == ','))) {
247 return true;
248 }
249 }
250 return false;
251}
252
253// Returns true if the supplied reason prefix is associated with the kernel.
254bool isKernelRebootReason(const std::string& r) {
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700255 for (auto& s : knownReasons) {
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700256 if (s == "recovery") break;
257 // Prefix defined as terminated by a nul or comma (,).
258 if (android::base::StartsWith(r, s.c_str()) &&
259 ((r.length() == s.length()) || (r[s.length()] == ','))) {
260 return true;
261 }
262 }
263 return false;
264}
265
266// Returns true if the supplied reason prefix is considered known.
267bool isKnownRebootReason(const std::string& r) {
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700268 for (auto& s : knownReasons) {
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700269 // Prefix defined as terminated by a nul or comma (,).
270 if (android::base::StartsWith(r, s.c_str()) &&
271 ((r.length() == s.length()) || (r[s.length()] == ','))) {
272 return true;
273 }
274 }
275 return false;
276}
277
278// If the reboot reason should be improved, report true if is too blunt.
279bool isBluntRebootReason(const std::string& r) {
280 if (isStrongRebootReason(r)) return false;
281
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700282 if (!isKnownRebootReason(r)) return true; // Can not support unknown as detail
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700283
284 size_t pos = 0;
285 while ((pos = r.find(',', pos)) != std::string::npos) {
286 ++pos;
287 std::string next(r.substr(pos));
288 if (next.length() == 0) break;
289 if (next[0] == ',') continue;
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700290 if (!isKnownRebootReason(next)) return false; // Unknown subreason is good.
291 if (isStrongRebootReason(next)) return false; // eg: reboot,reboot
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700292 }
293 return true;
294}
295
Mark Salyzyn64610892017-09-18 10:41:14 -0700296bool readPstoreConsole(std::string& console) {
297 if (android::base::ReadFileToString("/sys/fs/pstore/console-ramoops-0", &console)) {
298 return true;
299 }
300 return android::base::ReadFileToString("/sys/fs/pstore/console-ramoops", &console);
301}
302
303bool addKernelPanicSubReason(const std::string& console, std::string& ret) {
304 // Check for kernel panic types to refine information
305 if (console.rfind("SysRq : Trigger a crash") != std::string::npos) {
306 // Can not happen, except on userdebug, during testing/debugging.
307 ret = "kernel_panic,sysrq";
308 return true;
309 }
310 if (console.rfind("Unable to handle kernel NULL pointer dereference at virtual address") !=
311 std::string::npos) {
312 ret = "kernel_panic,NULL";
313 return true;
314 }
315 if (console.rfind("Kernel BUG at ") != std::string::npos) {
316 ret = "kernel_panic,BUG";
317 return true;
318 }
319 return false;
320}
321
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700322// std::transform Helper callback functions:
323// Converts a string value representing the reason the system booted to a
324// string complying with Android system standard reason.
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700325char tounderline(char c) {
326 return ::isblank(c) ? '_' : c;
327}
328char toprintable(char c) {
329 return ::isprint(c) ? c : '?';
330}
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700331
332const char system_reboot_reason_property[] = "sys.boot.reason";
333const char last_reboot_reason_property[] = LAST_REBOOT_REASON_PROPERTY;
334const char bootloader_reboot_reason_property[] = "ro.boot.bootreason";
335
336// Scrub, Sanitize, Standardize and Enhance the boot reason string supplied.
337std::string BootReasonStrToReason(const std::string& boot_reason) {
Mark Salyzyna16e4372017-09-20 08:36:12 -0700338 static const size_t max_reason_length = 256;
339
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700340 std::string ret(GetProperty(system_reboot_reason_property));
341 std::string reason(boot_reason);
342 // If sys.boot.reason == ro.boot.bootreason, let's re-evaluate
343 if (reason == ret) ret = "";
344
345 // Cleanup boot_reason regarding acceptable character set
346 std::transform(reason.begin(), reason.end(), reason.begin(), ::tolower);
347 std::transform(reason.begin(), reason.end(), reason.begin(), tounderline);
348 std::transform(reason.begin(), reason.end(), reason.begin(), toprintable);
349
350 // Is the current system boot reason sys.boot.reason valid?
351 if (!isKnownRebootReason(ret)) ret = "";
352
353 if (ret == "") {
354 // Is the bootloader boot reason ro.boot.bootreason known?
355 std::vector<std::string> words(android::base::Split(reason, ",_-"));
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700356 for (auto& s : knownReasons) {
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700357 std::string blunt;
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700358 for (auto& r : words) {
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700359 if (r == s) {
360 if (isBluntRebootReason(s)) {
361 blunt = s;
362 } else {
363 ret = s;
364 break;
365 }
366 }
367 }
368 if (ret == "") ret = blunt;
369 if (ret != "") break;
370 }
371 }
372
373 if (ret == "") {
374 // A series of checks to take some officially unsupported reasons
375 // reported by the bootloader and find some logical and canonical
376 // sense. In an ideal world, we would require those bootloaders
377 // to behave and follow our standards.
378 static const std::vector<std::pair<const std::string, const std::string>> aliasReasons = {
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700379 {"watchdog", "wdog"},
380 {"cold,powerkey", "powerkey"},
381 {"kernel_panic", "panic"},
382 {"shutdown,thermal", "thermal"},
383 {"warm,s3_wakeup", "s3_wakeup"},
384 {"hard,hw_reset", "hw_reset"},
385 {"bootloader", ""},
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700386 };
387
388 // Either the primary or alias is found _somewhere_ in the reason string.
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700389 for (auto& s : aliasReasons) {
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700390 if (reason.find(s.first) != std::string::npos) {
391 ret = s.first;
392 break;
393 }
394 if (s.second.size() && (reason.find(s.second) != std::string::npos)) {
395 ret = s.first;
396 break;
397 }
398 }
399 }
400
401 // If watchdog is the reason, see if there is a security angle?
402 if (ret == "watchdog") {
403 if (reason.find("sec") != std::string::npos) {
404 ret += ",security";
405 }
406 }
407
Mark Salyzyn64610892017-09-18 10:41:14 -0700408 if (ret == "kernel_panic") {
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700409 // Check to see if last klog has some refinement hints.
410 std::string content;
Mark Salyzyn64610892017-09-18 10:41:14 -0700411 if (readPstoreConsole(content)) {
412 addKernelPanicSubReason(content, ret);
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700413 }
Mark Salyzyn64610892017-09-18 10:41:14 -0700414 } else if (isBluntRebootReason(ret)) {
415 // Check the other available reason resources if the reason is still blunt.
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700416
Mark Salyzyn64610892017-09-18 10:41:14 -0700417 // Check to see if last klog has some refinement hints.
418 std::string content;
419 if (readPstoreConsole(content)) {
420 // The toybox reboot command used directly (unlikely)? But also
421 // catches init's response to Android's more controlled reboot command.
422 if (content.rfind("reboot: Power down") != std::string::npos) {
423 ret = "shutdown"; // Still too blunt, but more accurate.
424 // ToDo: init should record the shutdown reason to kernel messages ala:
425 // init: shutdown system with command 'last_reboot_reason'
426 // so that if pstore has persistence we can get some details
427 // that could be missing in last_reboot_reason_property.
428 }
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700429
Mark Salyzyn64610892017-09-18 10:41:14 -0700430 static const char cmd[] = "reboot: Restarting system with command '";
431 size_t pos = content.rfind(cmd);
432 if (pos != std::string::npos) {
433 pos += strlen(cmd);
Mark Salyzyna16e4372017-09-20 08:36:12 -0700434 std::string subReason(content.substr(pos, max_reason_length));
435 for (pos = 0; pos < subReason.length(); ++pos) {
436 char c = tounderline(subReason[pos]);
437 if (!::isprint(c) || (c == '\'')) {
438 subReason.erase(pos);
439 break;
440 }
441 subReason[pos] = ::tolower(c);
442 }
Mark Salyzyn64610892017-09-18 10:41:14 -0700443 if (subReason != "") { // Will not land "reboot" as that is too blunt.
444 if (isKernelRebootReason(subReason)) {
445 ret = "reboot," + subReason; // User space can't talk kernel reasons.
446 } else {
447 ret = subReason;
448 }
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700449 }
450 }
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700451
Mark Salyzyn64610892017-09-18 10:41:14 -0700452 // Check for kernel panics, allowed to override reboot command.
453 if (!addKernelPanicSubReason(content, ret) &&
454 // check for long-press power down
455 ((content.rfind("Power held for ") != std::string::npos) ||
456 (content.rfind("charger: [") != std::string::npos))) {
457 ret = "cold";
458 }
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700459 }
460
461 // The following battery test should migrate to a default system health HAL
462
463 // Let us not worry if the reboot command was issued, for the cases of
464 // reboot -p, reboot <no reason>, reboot cold, reboot warm and reboot hard.
465 // Same for bootloader and ro.boot.bootreasons of this set, but a dead
466 // battery could conceivably lead to these, so worthy of override.
467 if (isBluntRebootReason(ret)) {
468 // Heuristic to determine if shutdown possibly because of a dead battery?
469 // Really a hail-mary pass to find it in last klog content ...
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700470 static const int battery_dead_threshold = 2; // percent
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700471 static const char battery[] = "healthd: battery l=";
Mark Salyzyn64610892017-09-18 10:41:14 -0700472 size_t pos = content.rfind(battery); // last one
Mark Salyzyna16e4372017-09-20 08:36:12 -0700473 std::string digits;
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700474 if (pos != std::string::npos) {
Mark Salyzyna16e4372017-09-20 08:36:12 -0700475 digits = content.substr(pos + strlen(battery));
476 }
477 char* endptr = NULL;
478 unsigned long long level = strtoull(digits.c_str(), &endptr, 10);
479 if ((level <= 100) && (endptr != digits.c_str()) && (*endptr == ' ')) {
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700480 LOG(INFO) << "Battery level at shutdown " << level << "%";
481 if (level <= battery_dead_threshold) {
482 ret = "shutdown,battery";
483 }
Mark Salyzyna16e4372017-09-20 08:36:12 -0700484 } else { // Most likely
485 digits = ""; // reset digits
486
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700487 // Content buffer no longer will have console data. Beware if more
488 // checks added below, that depend on parsing console content.
489 content = "";
490
491 LOG(DEBUG) << "Can not find last low battery in last console messages";
492 android_logcat_context ctx = create_android_logcat();
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700493 FILE* fp = android_logcat_popen(&ctx, "logcat -b kernel -v brief -d");
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700494 if (fp != nullptr) {
495 android::base::ReadFdToString(fileno(fp), &content);
496 }
497 android_logcat_pclose(&ctx, fp);
498 android_logcat_destroy(&ctx);
499 static const char logcat_battery[] = "W/healthd ( 0): battery l=";
500 const char* match = logcat_battery;
501
502 if (content == "") {
503 // Service logd.klog not running, go to smaller buffer in the kernel.
504 int rc = klogctl(KLOG_SIZE_BUFFER, nullptr, 0);
505 if (rc > 0) {
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700506 ssize_t len = rc + 1024; // 1K Margin should it grow between calls.
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700507 std::unique_ptr<char[]> buf(new char[len]);
508 rc = klogctl(KLOG_READ_ALL, buf.get(), len);
509 if (rc < len) {
510 len = rc + 1;
511 }
512 buf[--len] = '\0';
513 content = buf.get();
514 }
515 match = battery;
516 }
517
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700518 pos = content.find(match); // The first one it finds.
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700519 if (pos != std::string::npos) {
Mark Salyzyna16e4372017-09-20 08:36:12 -0700520 digits = content.substr(pos + strlen(match));
521 }
522 endptr = NULL;
523 level = strtoull(digits.c_str(), &endptr, 10);
524 if ((level <= 100) && (endptr != digits.c_str()) && (*endptr == ' ')) {
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700525 LOG(INFO) << "Battery level at startup " << level << "%";
526 if (level <= battery_dead_threshold) {
527 ret = "shutdown,battery";
528 }
529 } else {
530 LOG(DEBUG) << "Can not find first battery level in dmesg or logcat";
531 }
532 }
533 }
534
535 // Is there a controlled shutdown hint in last_reboot_reason_property?
536 if (isBluntRebootReason(ret)) {
537 // Content buffer no longer will have console data. Beware if more
538 // checks added below, that depend on parsing console content.
539 content = GetProperty(last_reboot_reason_property);
Mark Salyzyna16e4372017-09-20 08:36:12 -0700540 // Cleanup last_boot_reason regarding acceptable character set
541 std::transform(content.begin(), content.end(), content.begin(), ::tolower);
542 std::transform(content.begin(), content.end(), content.begin(), tounderline);
543 std::transform(content.begin(), content.end(), content.begin(), toprintable);
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700544
545 // String is either "reboot,<reason>" or "shutdown,<reason>".
546 // We will set if default reasons, only override with detail if thermal.
Mark Salyzyn08b02562017-09-18 10:07:07 -0700547 if ((android::base::StartsWith(content, ret.c_str()) && (content[ret.length()] == ',')) ||
548 !isBluntRebootReason(content)) {
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700549 // Ok, we want it, let's squash it if secondReason is known.
Mark Salyzyn64610892017-09-18 10:41:14 -0700550 size_t pos = content.find(',');
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700551 if (pos != std::string::npos) {
552 ++pos;
553 std::string secondReason(content.substr(pos));
554 ret = isKnownRebootReason(secondReason) ? secondReason : content;
555 } else {
556 ret = content;
557 }
558 }
559 }
560
561 // Other System Health HAL reasons?
562
563 // ToDo: /proc/sys/kernel/boot_reason needs a HAL interface to
564 // possibly offer hardware-specific clues from the PMIC.
565 }
566
567 // If unknown left over from above, make it "reboot,<boot_reason>"
568 if (ret == "") {
569 ret = "reboot";
570 if (android::base::StartsWith(reason, "reboot")) {
571 reason = reason.substr(strlen("reboot"));
572 while (reason[0] == ',') {
573 reason = reason.substr(1);
574 }
575 }
576 if (reason != "") {
577 ret += ",";
578 ret += reason;
579 }
580 }
581
582 LOG(INFO) << "Canonical boot reason: " << ret;
583 if (isKernelRebootReason(ret) && (GetProperty(last_reboot_reason_property) != "")) {
584 // Rewrite as it must be old news, kernel reasons trump user space.
585 SetProperty(last_reboot_reason_property, ret);
586 }
587 return ret;
588}
589
James Hawkinsb9cf7712016-04-08 15:32:19 -0700590// Returns the appropriate metric key prefix for the boot_complete metric such
591// that boot metrics after a system update are labeled as ota_boot_complete;
592// otherwise, they are labeled as boot_complete. This method encapsulates the
593// bookkeeping required to track when a system update has occurred by storing
594// the UTC timestamp of the system build date and comparing against the current
595// system build date.
596std::string CalculateBootCompletePrefix() {
597 static const std::string kBuildDateKey = "build_date";
598 std::string boot_complete_prefix = "boot_complete";
599
600 std::string build_date_str = GetProperty("ro.build.date.utc");
James Hawkins4dded612016-07-28 11:50:23 -0700601 int32_t build_date;
Elliott Hughesda46b392016-10-11 17:09:00 -0700602 if (!android::base::ParseInt(build_date_str, &build_date)) {
James Hawkins4dded612016-07-28 11:50:23 -0700603 return std::string();
604 }
James Hawkinsb9cf7712016-04-08 15:32:19 -0700605
606 BootEventRecordStore boot_event_store;
607 BootEventRecordStore::BootEventRecord record;
James Hawkins0bc4ad42017-05-30 15:03:15 -0700608 if (!boot_event_store.GetBootEvent(kBuildDateKey, &record)) {
609 boot_complete_prefix = "factory_reset_" + boot_complete_prefix;
610 boot_event_store.AddBootEventWithValue(kBuildDateKey, build_date);
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700611 LOG(INFO) << "Canonical boot reason: reboot,factory_reset";
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700612 SetProperty(system_reboot_reason_property, "reboot,factory_reset");
613 if (GetProperty(bootloader_reboot_reason_property) == "") {
614 SetProperty(bootloader_reboot_reason_property, "reboot,factory_reset");
615 }
James Hawkins0bc4ad42017-05-30 15:03:15 -0700616 } else if (build_date != record.second) {
James Hawkinsb9cf7712016-04-08 15:32:19 -0700617 boot_complete_prefix = "ota_" + boot_complete_prefix;
618 boot_event_store.AddBootEventWithValue(kBuildDateKey, build_date);
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700619 LOG(INFO) << "Canonical boot reason: reboot,ota";
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700620 SetProperty(system_reboot_reason_property, "reboot,ota");
621 if (GetProperty(bootloader_reboot_reason_property) == "") {
622 SetProperty(bootloader_reboot_reason_property, "reboot,ota");
623 }
James Hawkinsb9cf7712016-04-08 15:32:19 -0700624 }
625
626 return boot_complete_prefix;
627}
628
James Hawkinsef0a0902017-01-06 14:38:23 -0800629// Records the value of a given ro.boottime.init property in milliseconds.
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700630void RecordInitBootTimeProp(BootEventRecordStore* boot_event_store, const char* property) {
James Hawkinsef0a0902017-01-06 14:38:23 -0800631 std::string value = GetProperty(property);
632
James Hawkins27c05222017-01-26 11:55:44 -0800633 int32_t time_in_ms;
634 if (android::base::ParseInt(value, &time_in_ms)) {
James Hawkinsef0a0902017-01-06 14:38:23 -0800635 boot_event_store->AddBootEventWithValue(property, time_in_ms);
636 }
637}
638
James Hawkins1bfcaec2017-05-19 14:27:27 -0700639// A map from bootloader timing stage to the time that stage took during boot.
640typedef std::map<std::string, int32_t> BootloaderTimingMap;
641
642// Returns a mapping from bootloader stage names to the time those stages
643// took to boot.
644const BootloaderTimingMap GetBootLoaderTimings() {
645 BootloaderTimingMap timings;
646
647 // |ro.boot.boottime| is of the form 'stage1:time1,...,stageN:timeN',
648 // where timeN is in milliseconds.
James Hawkinsbe46fd12017-02-02 16:21:25 -0800649 std::string value = GetProperty("ro.boot.boottime");
James Hawkins6b5c5aa2017-02-16 11:53:03 -0800650 if (value.empty()) {
651 // ro.boot.boottime is not reported on all devices.
James Hawkins1bfcaec2017-05-19 14:27:27 -0700652 return BootloaderTimingMap();
James Hawkins6b5c5aa2017-02-16 11:53:03 -0800653 }
James Hawkinsbe46fd12017-02-02 16:21:25 -0800654
655 auto stages = android::base::Split(value, ",");
James Hawkins1bfcaec2017-05-19 14:27:27 -0700656 for (const auto& stageTiming : stages) {
James Hawkinsbe46fd12017-02-02 16:21:25 -0800657 // |stageTiming| is of the form 'stage:time'.
658 auto stageTimingValues = android::base::Split(stageTiming, ":");
James Hawkins0bc4ad42017-05-30 15:03:15 -0700659 DCHECK_EQ(2U, stageTimingValues.size());
James Hawkinsbe46fd12017-02-02 16:21:25 -0800660
661 std::string stageName = stageTimingValues[0];
662 int32_t time_ms;
663 if (android::base::ParseInt(stageTimingValues[1], &time_ms)) {
James Hawkins1bfcaec2017-05-19 14:27:27 -0700664 timings[stageName] = time_ms;
James Hawkinsbe46fd12017-02-02 16:21:25 -0800665 }
666 }
James Hawkins6b5c5aa2017-02-16 11:53:03 -0800667
James Hawkins1bfcaec2017-05-19 14:27:27 -0700668 return timings;
669}
670
671// Parses and records the set of bootloader stages and associated boot times
672// from the ro.boot.boottime system property.
673void RecordBootloaderTimings(BootEventRecordStore* boot_event_store,
674 const BootloaderTimingMap& bootloader_timings) {
675 int32_t total_time = 0;
676 for (const auto& timing : bootloader_timings) {
677 total_time += timing.second;
678 boot_event_store->AddBootEventWithValue("boottime.bootloader." + timing.first, timing.second);
679 }
680
James Hawkins6b5c5aa2017-02-16 11:53:03 -0800681 boot_event_store->AddBootEventWithValue("boottime.bootloader.total", total_time);
James Hawkinsbe46fd12017-02-02 16:21:25 -0800682}
683
James Hawkins1bfcaec2017-05-19 14:27:27 -0700684// Records the closest estimation to the absolute device boot time, i.e.,
685// from power on to boot_complete, including bootloader times.
686void RecordAbsoluteBootTime(BootEventRecordStore* boot_event_store,
687 const BootloaderTimingMap& bootloader_timings,
688 std::chrono::milliseconds uptime) {
689 int32_t bootloader_time_ms = 0;
690
691 for (const auto& timing : bootloader_timings) {
692 if (timing.first.compare("SW") != 0) {
693 bootloader_time_ms += timing.second;
694 }
695 }
696
697 auto bootloader_duration = std::chrono::milliseconds(bootloader_time_ms);
698 auto absolute_total =
699 std::chrono::duration_cast<std::chrono::seconds>(bootloader_duration + uptime);
700 boot_event_store->AddBootEventWithValue("absolute_boot_time", absolute_total.count());
701}
702
James Hawkinsc08e9962016-03-11 14:59:50 -0800703// Records several metrics related to the time it takes to boot the device,
704// including disambiguating boot time on encrypted or non-encrypted devices.
705void RecordBootComplete() {
706 BootEventRecordStore boot_event_store;
James Hawkinsb9cf7712016-04-08 15:32:19 -0700707 BootEventRecordStore::BootEventRecord record;
James Hawkins2d8b3e62016-04-14 14:13:20 -0700708
James Hawkins1bfcaec2017-05-19 14:27:27 -0700709 auto time_since_epoch = android::base::boot_clock::now().time_since_epoch();
710 auto uptime = std::chrono::duration_cast<std::chrono::seconds>(time_since_epoch);
James Hawkins2d8b3e62016-04-14 14:13:20 -0700711 time_t current_time_utc = time(nullptr);
712
713 if (boot_event_store.GetBootEvent("last_boot_time_utc", &record)) {
714 time_t last_boot_time_utc = record.second;
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700715 time_t time_since_last_boot = difftime(current_time_utc, last_boot_time_utc);
716 boot_event_store.AddBootEventWithValue("time_since_last_boot", time_since_last_boot);
James Hawkins2d8b3e62016-04-14 14:13:20 -0700717 }
718
719 boot_event_store.AddBootEventWithValue("last_boot_time_utc", current_time_utc);
James Hawkinsc08e9962016-03-11 14:59:50 -0800720
James Hawkinsb9cf7712016-04-08 15:32:19 -0700721 // The boot_complete metric has two variants: boot_complete and
722 // ota_boot_complete. The latter signifies that the device is booting after
723 // a system update.
724 std::string boot_complete_prefix = CalculateBootCompletePrefix();
James Hawkins4dded612016-07-28 11:50:23 -0700725 if (boot_complete_prefix.empty()) {
726 // The system is hosed because the build date property could not be read.
727 return;
728 }
James Hawkinsc08e9962016-03-11 14:59:50 -0800729
730 // post_decrypt_time_elapsed is only logged on encrypted devices.
731 if (boot_event_store.GetBootEvent("post_decrypt_time_elapsed", &record)) {
732 // Log the amount of time elapsed until the device is decrypted, which
733 // includes the variable amount of time the user takes to enter the
734 // decryption password.
James Hawkinse78ea772017-03-24 11:43:02 -0700735 boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime.count());
James Hawkinsc08e9962016-03-11 14:59:50 -0800736
737 // Subtract the decryption time to normalize the boot cycle timing.
James Hawkinse78ea772017-03-24 11:43:02 -0700738 std::chrono::seconds boot_complete = std::chrono::seconds(uptime.count() - record.second);
James Hawkinsb9cf7712016-04-08 15:32:19 -0700739 boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_post_decrypt",
James Hawkinse78ea772017-03-24 11:43:02 -0700740 boot_complete.count());
James Hawkinsc08e9962016-03-11 14:59:50 -0800741 } else {
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700742 boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_no_encryption", uptime.count());
James Hawkinsc08e9962016-03-11 14:59:50 -0800743 }
744
745 // Record the total time from device startup to boot complete, regardless of
746 // encryption state.
James Hawkinse78ea772017-03-24 11:43:02 -0700747 boot_event_store.AddBootEventWithValue(boot_complete_prefix, uptime.count());
James Hawkinsef0a0902017-01-06 14:38:23 -0800748
749 RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init");
750 RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init.selinux");
751 RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init.cold_boot_wait");
James Hawkinsbe46fd12017-02-02 16:21:25 -0800752
James Hawkins1bfcaec2017-05-19 14:27:27 -0700753 const BootloaderTimingMap bootloader_timings = GetBootLoaderTimings();
754 RecordBootloaderTimings(&boot_event_store, bootloader_timings);
755
756 auto uptime_ms = std::chrono::duration_cast<std::chrono::milliseconds>(time_since_epoch);
757 RecordAbsoluteBootTime(&boot_event_store, bootloader_timings, uptime_ms);
James Hawkinsc08e9962016-03-11 14:59:50 -0800758}
759
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800760// Records the boot_reason metric by querying the ro.boot.bootreason system
761// property.
762void RecordBootReason() {
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700763 const std::string reason(GetProperty(bootloader_reboot_reason_property));
James Hawkins5240f202017-09-15 16:01:57 -0700764 android::metricslogger::LogMultiAction(android::metricslogger::ACTION_BOOT,
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700765 android::metricslogger::FIELD_PLATFORM_REASON, reason);
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700766
767 // Log the raw bootloader_boot_reason property value.
768 int32_t boot_reason = BootReasonStrToEnum(reason);
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800769 BootEventRecordStore boot_event_store;
770 boot_event_store.AddBootEventWithValue("boot_reason", boot_reason);
Mark Salyzynb304f6d2017-08-04 13:35:51 -0700771
772 // Log the scrubbed system_boot_reason.
773 const std::string system_reason(BootReasonStrToReason(reason));
774 int32_t system_boot_reason = BootReasonStrToEnum(system_reason);
775 boot_event_store.AddBootEventWithValue("system_boot_reason", system_boot_reason);
776
777 // Record the scrubbed system_boot_reason to the property
778 SetProperty(system_reboot_reason_property, system_reason);
779 if (reason == "") {
780 SetProperty(bootloader_reboot_reason_property, system_reason);
781 }
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800782}
783
James Hawkins500d7152016-02-16 15:05:54 -0800784// Records two metrics related to the user resetting a device: the time at
785// which the device is reset, and the time since the user last reset the
786// device. The former is only set once per-factory reset.
787void RecordFactoryReset() {
788 BootEventRecordStore boot_event_store;
789 BootEventRecordStore::BootEventRecord record;
790
791 time_t current_time_utc = time(nullptr);
792
James Hawkins0660b302016-03-08 16:18:15 -0800793 if (current_time_utc < 0) {
794 // UMA does not display negative values in buckets, so convert to positive.
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700795 android::metricslogger::LogHistogram("factory_reset_current_time_failure",
796 std::abs(current_time_utc));
James Hawkinsfff95ba2016-03-29 16:13:49 -0700797
James Hawkins9aec9262017-01-31 11:42:24 -0800798 // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram
James Hawkinsfff95ba2016-03-29 16:13:49 -0700799 // is losing records somehow.
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700800 boot_event_store.AddBootEventWithValue("factory_reset_current_time_failure",
801 std::abs(current_time_utc));
James Hawkins0660b302016-03-08 16:18:15 -0800802 return;
803 } else {
James Hawkins9aec9262017-01-31 11:42:24 -0800804 android::metricslogger::LogHistogram("factory_reset_current_time", current_time_utc);
James Hawkinsfff95ba2016-03-29 16:13:49 -0700805
James Hawkins9aec9262017-01-31 11:42:24 -0800806 // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram
James Hawkinsfff95ba2016-03-29 16:13:49 -0700807 // is losing records somehow.
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700808 boot_event_store.AddBootEventWithValue("factory_reset_current_time", current_time_utc);
James Hawkins0660b302016-03-08 16:18:15 -0800809 }
810
James Hawkins500d7152016-02-16 15:05:54 -0800811 // The factory_reset boot event does not exist after the device is reset, so
812 // use this signal to mark the time of the factory reset.
813 if (!boot_event_store.GetBootEvent("factory_reset", &record)) {
814 boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc);
James Hawkins3bf9b142016-03-03 14:50:24 -0800815
816 // Don't log the time_since_factory_reset until some time has elapsed.
817 // The data is not meaningful yet and skews the histogram buckets.
James Hawkins500d7152016-02-16 15:05:54 -0800818 return;
819 }
820
821 // Calculate and record the difference in time between now and the
822 // factory_reset time.
823 time_t factory_reset_utc = record.second;
James Hawkins9aec9262017-01-31 11:42:24 -0800824 android::metricslogger::LogHistogram("factory_reset_record_value", factory_reset_utc);
James Hawkinsfff95ba2016-03-29 16:13:49 -0700825
James Hawkins9aec9262017-01-31 11:42:24 -0800826 // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram
James Hawkinsfff95ba2016-03-29 16:13:49 -0700827 // is losing records somehow.
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700828 boot_event_store.AddBootEventWithValue("factory_reset_record_value", factory_reset_utc);
James Hawkinsfff95ba2016-03-29 16:13:49 -0700829
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700830 time_t time_since_factory_reset = difftime(current_time_utc, factory_reset_utc);
831 boot_event_store.AddBootEventWithValue("time_since_factory_reset", time_since_factory_reset);
James Hawkins500d7152016-02-16 15:05:54 -0800832}
833
James Hawkinsabd73e62016-01-19 15:10:38 -0800834} // namespace
835
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700836int main(int argc, char** argv) {
James Hawkinsabd73e62016-01-19 15:10:38 -0800837 android::base::InitLogging(argv);
838
839 const std::string cmd_line = GetCommandLine(argc, argv);
840 LOG(INFO) << "Service started: " << cmd_line;
841
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800842 int option_index = 0;
James Hawkinsc6275582016-03-22 10:47:44 -0700843 static const char value_str[] = "value";
James Hawkinsc08e9962016-03-11 14:59:50 -0800844 static const char boot_complete_str[] = "record_boot_complete";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800845 static const char boot_reason_str[] = "record_boot_reason";
James Hawkins53684ea2016-02-23 16:18:19 -0800846 static const char factory_reset_str[] = "record_time_since_factory_reset";
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800847 static const struct option long_options[] = {
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700848 // clang-format off
849 { "help", no_argument, NULL, 'h' },
850 { "log", no_argument, NULL, 'l' },
851 { "print", no_argument, NULL, 'p' },
852 { "record", required_argument, NULL, 'r' },
853 { value_str, required_argument, NULL, 0 },
854 { boot_complete_str, no_argument, NULL, 0 },
855 { boot_reason_str, no_argument, NULL, 0 },
856 { factory_reset_str, no_argument, NULL, 0 },
857 { NULL, 0, NULL, 0 }
858 // clang-format on
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800859 };
860
James Hawkinsc6275582016-03-22 10:47:44 -0700861 std::string boot_event;
862 std::string value;
James Hawkinsabd73e62016-01-19 15:10:38 -0800863 int opt = 0;
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800864 while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) {
James Hawkinsabd73e62016-01-19 15:10:38 -0800865 switch (opt) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800866 // This case handles long options which have no single-character mapping.
867 case 0: {
868 const std::string option_name = long_options[option_index].name;
James Hawkinsc6275582016-03-22 10:47:44 -0700869 if (option_name == value_str) {
870 // |optarg| is an external variable set by getopt representing
871 // the option argument.
872 value = optarg;
873 } else if (option_name == boot_complete_str) {
James Hawkinsc08e9962016-03-11 14:59:50 -0800874 RecordBootComplete();
875 } else if (option_name == boot_reason_str) {
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800876 RecordBootReason();
James Hawkins500d7152016-02-16 15:05:54 -0800877 } else if (option_name == factory_reset_str) {
878 RecordFactoryReset();
James Hawkinsa4a1a4a2016-02-09 15:32:38 -0800879 } else {
880 LOG(ERROR) << "Invalid option: " << option_name;
881 }
882 break;
883 }
884
James Hawkinsabd73e62016-01-19 15:10:38 -0800885 case 'h': {
886 ShowHelp(argv[0]);
887 break;
888 }
889
890 case 'l': {
891 LogBootEvents();
892 break;
893 }
894
895 case 'p': {
896 PrintBootEvents();
897 break;
898 }
899
900 case 'r': {
901 // |optarg| is an external variable set by getopt representing
902 // the option argument.
James Hawkinsc6275582016-03-22 10:47:44 -0700903 boot_event = optarg;
James Hawkinsabd73e62016-01-19 15:10:38 -0800904 break;
905 }
906
907 default: {
908 DCHECK_EQ(opt, '?');
909
910 // |optopt| is an external variable set by getopt representing
911 // the value of the invalid option.
912 LOG(ERROR) << "Invalid option: " << optopt;
913 ShowHelp(argv[0]);
914 return EXIT_FAILURE;
915 }
916 }
917 }
918
James Hawkinsc6275582016-03-22 10:47:44 -0700919 if (!boot_event.empty()) {
920 RecordBootEventFromCommandLine(boot_event, value);
921 }
922
James Hawkinsabd73e62016-01-19 15:10:38 -0800923 return 0;
924}