James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | // The bootstat command provides options to persist boot events with the current |
| 18 | // timestamp, dump the persisted events, and log all events to EventLog to be |
| 19 | // uploaded to Android log storage via Tron. |
| 20 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 21 | #include <getopt.h> |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 22 | #include <sys/klog.h> |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 23 | #include <unistd.h> |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 24 | |
James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 25 | #include <chrono> |
James Hawkins | 0660b30 | 2016-03-08 16:18:15 -0800 | [diff] [blame] | 26 | #include <cmath> |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 27 | #include <cstddef> |
| 28 | #include <cstdio> |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 29 | #include <ctime> |
Mark Salyzyn | 5c58c9d | 2018-06-28 09:21:55 -0700 | [diff] [blame^] | 30 | #include <iterator> |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 31 | #include <map> |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 32 | #include <memory> |
Mark Salyzyn | 25900dd | 2018-03-16 09:05:59 -0700 | [diff] [blame] | 33 | #include <regex> |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 34 | #include <string> |
Mark Salyzyn | 853bb80 | 2018-03-16 08:44:56 -0700 | [diff] [blame] | 35 | #include <utility> |
James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 36 | #include <vector> |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 37 | |
James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 38 | #include <android-base/chrono_utils.h> |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 39 | #include <android-base/file.h> |
James Hawkins | eabe08b | 2016-01-19 16:54:35 -0800 | [diff] [blame] | 40 | #include <android-base/logging.h> |
James Hawkins | 4dded61 | 2016-07-28 11:50:23 -0700 | [diff] [blame] | 41 | #include <android-base/parseint.h> |
Luis Hector Chavez | 03aae15 | 2018-04-12 15:25:15 -0700 | [diff] [blame] | 42 | #include <android-base/properties.h> |
James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 43 | #include <android-base/strings.h> |
James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 44 | #include <android/log.h> |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 45 | #include <cutils/android_reboot.h> |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 46 | #include <cutils/properties.h> |
James Hawkins | 9aec926 | 2017-01-31 11:42:24 -0800 | [diff] [blame] | 47 | #include <metricslogger/metrics_logger.h> |
Mark Salyzyn | ff2dcd9 | 2016-09-28 15:54:45 -0700 | [diff] [blame] | 48 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 49 | #include "boot_event_record_store.h" |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 50 | |
| 51 | namespace { |
| 52 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 53 | // Scans the boot event record store for record files and logs each boot event |
| 54 | // via EventLog. |
| 55 | void LogBootEvents() { |
| 56 | BootEventRecordStore boot_event_store; |
| 57 | |
| 58 | auto events = boot_event_store.GetAllBootEvents(); |
| 59 | for (auto i = events.cbegin(); i != events.cend(); ++i) { |
James Hawkins | 9aec926 | 2017-01-31 11:42:24 -0800 | [diff] [blame] | 60 | android::metricslogger::LogHistogram(i->first, i->second); |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 64 | // Records the named boot |event| to the record store. If |value| is non-empty |
| 65 | // and is a proper string representation of an integer value, the converted |
| 66 | // integer value is associated with the boot event. |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 67 | void RecordBootEventFromCommandLine(const std::string& event, const std::string& value_str) { |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 68 | BootEventRecordStore boot_event_store; |
| 69 | if (!value_str.empty()) { |
| 70 | int32_t value = 0; |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 71 | if (android::base::ParseInt(value_str, &value)) { |
James Hawkins | 4dded61 | 2016-07-28 11:50:23 -0700 | [diff] [blame] | 72 | boot_event_store.AddBootEventWithValue(event, value); |
| 73 | } |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 74 | } else { |
| 75 | boot_event_store.AddBootEvent(event); |
| 76 | } |
| 77 | } |
| 78 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 79 | void PrintBootEvents() { |
| 80 | printf("Boot events:\n"); |
| 81 | printf("------------\n"); |
| 82 | |
| 83 | BootEventRecordStore boot_event_store; |
| 84 | auto events = boot_event_store.GetAllBootEvents(); |
| 85 | for (auto i = events.cbegin(); i != events.cend(); ++i) { |
| 86 | printf("%s\t%d\n", i->first.c_str(), i->second); |
| 87 | } |
| 88 | } |
| 89 | |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 90 | void ShowHelp(const char* cmd) { |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 91 | fprintf(stderr, "Usage: %s [options]\n", cmd); |
| 92 | fprintf(stderr, |
| 93 | "options include:\n" |
Yongqin Liu | 78b2b94 | 2017-07-07 13:26:49 +0800 | [diff] [blame] | 94 | " -h, --help Show this help\n" |
| 95 | " -l, --log Log all metrics to logstorage\n" |
| 96 | " -p, --print Dump the boot event records to the console\n" |
| 97 | " -r, --record Record the timestamp of a named boot event\n" |
| 98 | " --value Optional value to associate with the boot event\n" |
| 99 | " --record_boot_complete Record metrics related to the time for the device boot\n" |
| 100 | " --record_boot_reason Record the reason why the device booted\n" |
James Hawkins | 53684ea | 2016-02-23 16:18:19 -0800 | [diff] [blame] | 101 | " --record_time_since_factory_reset Record the time since the device was reset\n"); |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // Constructs a readable, printable string from the givencommand line |
| 105 | // arguments. |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 106 | std::string GetCommandLine(int argc, char** argv) { |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 107 | std::string cmd; |
| 108 | for (int i = 0; i < argc; ++i) { |
| 109 | cmd += argv[i]; |
| 110 | cmd += " "; |
| 111 | } |
| 112 | |
| 113 | return cmd; |
| 114 | } |
| 115 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 116 | // Convenience wrapper over the property API that returns an |
| 117 | // std::string. |
| 118 | std::string GetProperty(const char* key) { |
| 119 | std::vector<char> temp(PROPERTY_VALUE_MAX); |
| 120 | const int len = property_get(key, &temp[0], nullptr); |
| 121 | if (len < 0) { |
| 122 | return ""; |
| 123 | } |
| 124 | return std::string(&temp[0], len); |
| 125 | } |
| 126 | |
Mark Salyzyn | 5c58c9d | 2018-06-28 09:21:55 -0700 | [diff] [blame^] | 127 | bool SetProperty(const char* key, const std::string& val) { |
| 128 | return property_set(key, val.c_str()) == 0; |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 129 | } |
| 130 | |
Mark Salyzyn | 5c58c9d | 2018-06-28 09:21:55 -0700 | [diff] [blame^] | 131 | bool SetProperty(const char* key, const char* val) { |
| 132 | return property_set(key, val) == 0; |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 133 | } |
| 134 | |
James Hawkins | 25f7122 | 2017-10-10 16:37:05 -0700 | [diff] [blame] | 135 | constexpr int32_t kEmptyBootReason = 0; |
James Hawkins | 6f74c0b | 2016-02-12 15:49:16 -0800 | [diff] [blame] | 136 | constexpr int32_t kUnknownBootReason = 1; |
| 137 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 138 | // A mapping from boot reason string, as read from the ro.boot.bootreason |
| 139 | // system property, to a unique integer ID. Viewers of log data dashboards for |
| 140 | // the boot_reason metric may refer to this mapping to discern the histogram |
| 141 | // values. |
James Hawkins | 6f74c0b | 2016-02-12 15:49:16 -0800 | [diff] [blame] | 142 | const std::map<std::string, int32_t> kBootReasonMap = { |
James Hawkins | 25f7122 | 2017-10-10 16:37:05 -0700 | [diff] [blame] | 143 | {"empty", kEmptyBootReason}, |
Mark Salyzyn | 2b82053 | 2018-03-16 08:53:34 -0700 | [diff] [blame] | 144 | {"__BOOTSTAT_UNKNOWN__", kUnknownBootReason}, |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 145 | {"normal", 2}, |
| 146 | {"recovery", 3}, |
| 147 | {"reboot", 4}, |
| 148 | {"PowerKey", 5}, |
| 149 | {"hard_reset", 6}, |
| 150 | {"kernel_panic", 7}, |
| 151 | {"rpm_err", 8}, |
| 152 | {"hw_reset", 9}, |
| 153 | {"tz_err", 10}, |
| 154 | {"adsp_err", 11}, |
| 155 | {"modem_err", 12}, |
| 156 | {"mba_err", 13}, |
| 157 | {"Watchdog", 14}, |
| 158 | {"Panic", 15}, |
| 159 | {"power_key", 16}, |
| 160 | {"power_on", 17}, |
| 161 | {"Reboot", 18}, |
| 162 | {"rtc", 19}, |
| 163 | {"edl", 20}, |
| 164 | {"oem_pon1", 21}, |
| 165 | {"oem_powerkey", 22}, |
| 166 | {"oem_unknown_reset", 23}, |
| 167 | {"srto: HWWDT reset SC", 24}, |
| 168 | {"srto: HWWDT reset platform", 25}, |
| 169 | {"srto: bootloader", 26}, |
| 170 | {"srto: kernel panic", 27}, |
| 171 | {"srto: kernel watchdog reset", 28}, |
| 172 | {"srto: normal", 29}, |
| 173 | {"srto: reboot", 30}, |
| 174 | {"srto: reboot-bootloader", 31}, |
| 175 | {"srto: security watchdog reset", 32}, |
| 176 | {"srto: wakesrc", 33}, |
| 177 | {"srto: watchdog", 34}, |
| 178 | {"srto:1-1", 35}, |
| 179 | {"srto:omap_hsmm", 36}, |
| 180 | {"srto:phy0", 37}, |
| 181 | {"srto:rtc0", 38}, |
| 182 | {"srto:touchpad", 39}, |
| 183 | {"watchdog", 40}, |
| 184 | {"watchdogr", 41}, |
| 185 | {"wdog_bark", 42}, |
| 186 | {"wdog_bite", 43}, |
| 187 | {"wdog_reset", 44}, |
| 188 | {"shutdown,", 45}, // Trailing comma is intentional. |
| 189 | {"shutdown,userrequested", 46}, |
| 190 | {"reboot,bootloader", 47}, |
| 191 | {"reboot,cold", 48}, |
| 192 | {"reboot,recovery", 49}, |
| 193 | {"thermal_shutdown", 50}, |
| 194 | {"s3_wakeup", 51}, |
| 195 | {"kernel_panic,sysrq", 52}, |
| 196 | {"kernel_panic,NULL", 53}, |
Mark Salyzyn | 853bb80 | 2018-03-16 08:44:56 -0700 | [diff] [blame] | 197 | {"kernel_panic,null", 53}, |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 198 | {"kernel_panic,BUG", 54}, |
Mark Salyzyn | 853bb80 | 2018-03-16 08:44:56 -0700 | [diff] [blame] | 199 | {"kernel_panic,bug", 54}, |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 200 | {"bootloader", 55}, |
| 201 | {"cold", 56}, |
| 202 | {"hard", 57}, |
| 203 | {"warm", 58}, |
Mark Salyzyn | 1519925 | 2018-03-16 09:26:05 -0700 | [diff] [blame] | 204 | {"reboot,kernel_power_off_charging__reboot_system", 59}, // Can not happen |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 205 | {"thermal-shutdown", 60}, |
| 206 | {"shutdown,thermal", 61}, |
| 207 | {"shutdown,battery", 62}, |
| 208 | {"reboot,ota", 63}, |
| 209 | {"reboot,factory_reset", 64}, |
| 210 | {"reboot,", 65}, |
| 211 | {"reboot,shell", 66}, |
| 212 | {"reboot,adb", 67}, |
Mark Salyzyn | 9033bf5 | 2017-09-21 11:30:29 -0700 | [diff] [blame] | 213 | {"reboot,userrequested", 68}, |
Mark Salyzyn | 161b862 | 2017-09-26 08:26:12 -0700 | [diff] [blame] | 214 | {"shutdown,container", 69}, // Host OS asking Android Container to shutdown |
Mark Salyzyn | 243fa29 | 2017-10-11 09:02:04 -0700 | [diff] [blame] | 215 | {"cold,powerkey", 70}, |
| 216 | {"warm,s3_wakeup", 71}, |
| 217 | {"hard,hw_reset", 72}, |
| 218 | {"shutdown,suspend", 73}, // Suspend to RAM |
| 219 | {"shutdown,hibernate", 74}, // Suspend to DISK |
James Hawkins | 34073b5 | 2017-10-17 15:53:27 -0700 | [diff] [blame] | 220 | {"power_on_key", 75}, |
| 221 | {"reboot_by_key", 76}, |
| 222 | {"wdt_by_pass_pwk", 77}, |
| 223 | {"reboot_longkey", 78}, |
| 224 | {"powerkey", 79}, |
| 225 | {"usb", 80}, |
| 226 | {"wdt", 81}, |
| 227 | {"tool_by_pass_pwk", 82}, |
| 228 | {"2sec_reboot", 83}, |
| 229 | {"reboot,by_key", 84}, |
| 230 | {"reboot,longkey", 85}, |
Mark Salyzyn | 186f676 | 2018-03-16 11:00:26 -0700 | [diff] [blame] | 231 | {"reboot,2sec", 86}, // Deprecate in two years, replaced with cold,rtc,2sec |
Mark Salyzyn | c89f9da | 2017-10-24 15:35:34 -0700 | [diff] [blame] | 232 | {"shutdown,thermal,battery", 87}, |
Mark Salyzyn | 72a8ea3 | 2017-10-25 09:23:19 -0700 | [diff] [blame] | 233 | {"reboot,its_just_so_hard", 88}, // produced by boot_reason_test |
| 234 | {"reboot,Its Just So Hard", 89}, // produced by boot_reason_test |
Mark Salyzyn | 7504689 | 2018-05-03 13:11:15 -0700 | [diff] [blame] | 235 | {"reboot,rescueparty", 90}, |
James Hawkins | 74b1758 | 2017-11-20 14:13:41 -0800 | [diff] [blame] | 236 | {"charge", 91}, |
| 237 | {"oem_tz_crash", 92}, |
| 238 | {"uvlo", 93}, |
| 239 | {"oem_ps_hold", 94}, |
| 240 | {"abnormal_reset", 95}, |
| 241 | {"oemerr_unknown", 96}, |
| 242 | {"reboot_fastboot_mode", 97}, |
James Hawkins | 5f85f83 | 2017-11-29 14:30:06 -0800 | [diff] [blame] | 243 | {"watchdog_apps_bite", 98}, |
| 244 | {"xpu_err", 99}, |
| 245 | {"power_on_usb", 100}, |
James Hawkins | f4444f0 | 2017-11-30 15:01:40 -0800 | [diff] [blame] | 246 | {"watchdog_rpm", 101}, |
| 247 | {"watchdog_nonsec", 102}, |
| 248 | {"watchdog_apps_bark", 103}, |
| 249 | {"reboot_dmverity_corrupted", 104}, |
James Hawkins | 00433a2 | 2017-12-04 14:20:21 -0800 | [diff] [blame] | 250 | {"reboot_smpl", 105}, |
| 251 | {"watchdog_sdi_apps_reset", 106}, |
| 252 | {"smpl", 107}, |
| 253 | {"oem_modem_failed_to_powerup", 108}, |
James Hawkins | e2c2724 | 2017-12-18 13:40:27 -0800 | [diff] [blame] | 254 | {"reboot_normal", 109}, |
| 255 | {"oem_lpass_cfg", 110}, |
| 256 | {"oem_xpu_ns_error", 111}, |
| 257 | {"power_key_press", 112}, |
| 258 | {"hardware_reset", 113}, |
| 259 | {"reboot_by_powerkey", 114}, |
| 260 | {"reboot_verity", 115}, |
| 261 | {"oem_rpm_undef_error", 116}, |
| 262 | {"oem_crash_on_the_lk", 117}, |
| 263 | {"oem_rpm_reset", 118}, |
| 264 | {"oem_lpass_cfg", 119}, |
| 265 | {"oem_xpu_ns_error", 120}, |
| 266 | {"factory_cable", 121}, |
| 267 | {"oem_ar6320_failed_to_powerup", 122}, |
| 268 | {"watchdog_rpm_bite", 123}, |
| 269 | {"power_on_cable", 124}, |
| 270 | {"reboot_unknown", 125}, |
| 271 | {"wireless_charger", 126}, |
| 272 | {"0x776655ff", 127}, |
| 273 | {"oem_thermal_bite_reset", 128}, |
| 274 | {"charger", 129}, |
| 275 | {"pon1", 130}, |
| 276 | {"unknown", 131}, |
| 277 | {"reboot_rtc", 132}, |
| 278 | {"cold_boot", 133}, |
| 279 | {"hard_rst", 134}, |
James Hawkins | b607dae | 2018-01-05 14:42:55 -0800 | [diff] [blame] | 280 | {"power-on", 135}, |
| 281 | {"oem_adsp_resetting_the_soc", 136}, |
| 282 | {"kpdpwr", 137}, |
| 283 | {"oem_modem_timeout_waiting", 138}, |
| 284 | {"usb_chg", 139}, |
| 285 | {"warm_reset_0x02", 140}, |
| 286 | {"warm_reset_0x80", 141}, |
| 287 | {"pon_reason_0xb0", 142}, |
| 288 | {"reboot_download", 143}, |
James Hawkins | 79a4ee2 | 2018-01-26 14:31:04 -0800 | [diff] [blame] | 289 | {"reboot_recovery_mode", 144}, |
| 290 | {"oem_sdi_err_fatal", 145}, |
| 291 | {"pmic_watchdog", 146}, |
| 292 | {"software_master", 147}, |
Mark Salyzyn | 8aa36c6 | 2018-03-16 11:00:14 -0700 | [diff] [blame] | 293 | {"cold,charger", 148}, |
| 294 | {"cold,rtc", 149}, |
Mark Salyzyn | 39cc3e7 | 2018-03-19 15:16:29 -0700 | [diff] [blame] | 295 | {"cold,rtc,2sec", 150}, |
| 296 | {"reboot,tool", 151}, |
| 297 | {"reboot,wdt", 152}, |
| 298 | {"reboot,unknown", 153}, |
Mark Salyzyn | 3f48fa9 | 2018-03-22 08:41:22 -0700 | [diff] [blame] | 299 | {"kernel_panic,audit", 154}, |
| 300 | {"kernel_panic,atomic", 155}, |
| 301 | {"kernel_panic,hung", 156}, |
| 302 | {"kernel_panic,hung,rcu", 157}, |
| 303 | {"kernel_panic,init", 158}, |
| 304 | {"kernel_panic,oom", 159}, |
| 305 | {"kernel_panic,stack", 160}, |
Mark Salyzyn | afd66f2 | 2018-03-19 15:16:29 -0700 | [diff] [blame] | 306 | {"kernel_panic,sysrq,livelock,alarm", 161}, // llkd |
| 307 | {"kernel_panic,sysrq,livelock,driver", 162}, // llkd |
| 308 | {"kernel_panic,sysrq,livelock,zombie", 163}, // llkd |
Mark Salyzyn | 8ad6e67 | 2018-06-01 08:59:05 -0700 | [diff] [blame] | 309 | {"kernel_panic,modem", 164}, |
| 310 | {"kernel_panic,adsp", 165}, |
| 311 | {"kernel_panic,dsps", 166}, |
| 312 | {"kernel_panic,wcnss", 167}, |
Mark Salyzyn | 78e54fd | 2018-06-08 10:19:16 -0700 | [diff] [blame] | 313 | {"kernel_panic,_sde_encoder_phys_cmd_handle_ppdone_timeout", 168}, |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 314 | }; |
| 315 | |
| 316 | // Converts a string value representing the reason the system booted to an |
| 317 | // integer representation. This is necessary for logging the boot_reason metric |
| 318 | // via Tron, which does not accept non-integer buckets in histograms. |
| 319 | int32_t BootReasonStrToEnum(const std::string& boot_reason) { |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 320 | auto mapping = kBootReasonMap.find(boot_reason); |
| 321 | if (mapping != kBootReasonMap.end()) { |
| 322 | return mapping->second; |
| 323 | } |
| 324 | |
James Hawkins | 25f7122 | 2017-10-10 16:37:05 -0700 | [diff] [blame] | 325 | if (boot_reason.empty()) { |
| 326 | return kEmptyBootReason; |
| 327 | } |
| 328 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 329 | LOG(INFO) << "Unknown boot reason: " << boot_reason; |
| 330 | return kUnknownBootReason; |
| 331 | } |
| 332 | |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 333 | // Canonical list of supported primary reboot reasons. |
| 334 | const std::vector<const std::string> knownReasons = { |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 335 | // clang-format off |
| 336 | // kernel |
| 337 | "watchdog", |
| 338 | "kernel_panic", |
| 339 | // strong |
| 340 | "recovery", // Should not happen from ro.boot.bootreason |
| 341 | "bootloader", // Should not happen from ro.boot.bootreason |
| 342 | // blunt |
| 343 | "cold", |
| 344 | "hard", |
| 345 | "warm", |
Mark Salyzyn | 6290982 | 2017-10-09 09:27:16 -0700 | [diff] [blame] | 346 | // super blunt |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 347 | "shutdown", // Can not happen from ro.boot.bootreason |
| 348 | "reboot", // Default catch-all for anything unknown |
| 349 | // clang-format on |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 350 | }; |
| 351 | |
| 352 | // Returns true if the supplied reason prefix is considered detailed enough. |
| 353 | bool isStrongRebootReason(const std::string& r) { |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 354 | for (auto& s : knownReasons) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 355 | if (s == "cold") break; |
| 356 | // Prefix defined as terminated by a nul or comma (,). |
Elliott Hughes | 579e682 | 2017-12-20 09:41:00 -0800 | [diff] [blame] | 357 | if (android::base::StartsWith(r, s) && ((r.length() == s.length()) || (r[s.length()] == ','))) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 358 | return true; |
| 359 | } |
| 360 | } |
| 361 | return false; |
| 362 | } |
| 363 | |
| 364 | // Returns true if the supplied reason prefix is associated with the kernel. |
| 365 | bool isKernelRebootReason(const std::string& r) { |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 366 | for (auto& s : knownReasons) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 367 | if (s == "recovery") break; |
| 368 | // Prefix defined as terminated by a nul or comma (,). |
Elliott Hughes | 579e682 | 2017-12-20 09:41:00 -0800 | [diff] [blame] | 369 | if (android::base::StartsWith(r, s) && ((r.length() == s.length()) || (r[s.length()] == ','))) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 370 | return true; |
| 371 | } |
| 372 | } |
| 373 | return false; |
| 374 | } |
| 375 | |
| 376 | // Returns true if the supplied reason prefix is considered known. |
| 377 | bool isKnownRebootReason(const std::string& r) { |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 378 | for (auto& s : knownReasons) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 379 | // Prefix defined as terminated by a nul or comma (,). |
Elliott Hughes | 579e682 | 2017-12-20 09:41:00 -0800 | [diff] [blame] | 380 | if (android::base::StartsWith(r, s) && ((r.length() == s.length()) || (r[s.length()] == ','))) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 381 | return true; |
| 382 | } |
| 383 | } |
| 384 | return false; |
| 385 | } |
| 386 | |
| 387 | // If the reboot reason should be improved, report true if is too blunt. |
| 388 | bool isBluntRebootReason(const std::string& r) { |
| 389 | if (isStrongRebootReason(r)) return false; |
| 390 | |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 391 | if (!isKnownRebootReason(r)) return true; // Can not support unknown as detail |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 392 | |
| 393 | size_t pos = 0; |
| 394 | while ((pos = r.find(',', pos)) != std::string::npos) { |
| 395 | ++pos; |
| 396 | std::string next(r.substr(pos)); |
| 397 | if (next.length() == 0) break; |
| 398 | if (next[0] == ',') continue; |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 399 | if (!isKnownRebootReason(next)) return false; // Unknown subreason is good. |
| 400 | if (isStrongRebootReason(next)) return false; // eg: reboot,reboot |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 401 | } |
| 402 | return true; |
| 403 | } |
| 404 | |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 405 | bool readPstoreConsole(std::string& console) { |
| 406 | if (android::base::ReadFileToString("/sys/fs/pstore/console-ramoops-0", &console)) { |
| 407 | return true; |
| 408 | } |
| 409 | return android::base::ReadFileToString("/sys/fs/pstore/console-ramoops", &console); |
| 410 | } |
| 411 | |
Mark Salyzyn | 293cb3b | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 412 | // Implement a variant of std::string::rfind that is resilient to errors in |
| 413 | // the data stream being inspected. |
| 414 | class pstoreConsole { |
| 415 | private: |
| 416 | const size_t kBitErrorRate = 8; // number of bits per error |
| 417 | const std::string& console; |
| 418 | |
| 419 | // Number of bits that differ between the two arguments l and r. |
| 420 | // Returns zero if the values for l and r are identical. |
| 421 | size_t numError(uint8_t l, uint8_t r) const { return std::bitset<8>(l ^ r).count(); } |
| 422 | |
| 423 | // A string comparison function, reports the number of errors discovered |
| 424 | // in the match to a maximum of the bitLength / kBitErrorRate, at that |
| 425 | // point returning npos to indicate match is too poor. |
| 426 | // |
| 427 | // Since called in rfind which works backwards, expect cache locality will |
| 428 | // help if we check in reverse here as well for performance. |
| 429 | // |
| 430 | // Assumption: l (from console.c_str() + pos) is long enough to house |
| 431 | // _r.length(), checked in rfind caller below. |
| 432 | // |
| 433 | size_t numError(size_t pos, const std::string& _r) const { |
| 434 | const char* l = console.c_str() + pos; |
| 435 | const char* r = _r.c_str(); |
| 436 | size_t n = _r.length(); |
| 437 | const uint8_t* le = reinterpret_cast<const uint8_t*>(l) + n; |
| 438 | const uint8_t* re = reinterpret_cast<const uint8_t*>(r) + n; |
| 439 | size_t count = 0; |
| 440 | n = 0; |
| 441 | do { |
| 442 | // individual character bit error rate > threshold + slop |
| 443 | size_t num = numError(*--le, *--re); |
| 444 | if (num > ((8 + kBitErrorRate) / kBitErrorRate)) return std::string::npos; |
| 445 | // total bit error rate > threshold + slop |
| 446 | count += num; |
| 447 | ++n; |
| 448 | if (count > ((n * 8 + kBitErrorRate - (n > 2)) / kBitErrorRate)) { |
| 449 | return std::string::npos; |
| 450 | } |
| 451 | } while (le != reinterpret_cast<const uint8_t*>(l)); |
| 452 | return count; |
| 453 | } |
| 454 | |
| 455 | public: |
| 456 | explicit pstoreConsole(const std::string& console) : console(console) {} |
| 457 | // scope of argument must be equal to or greater than scope of pstoreConsole |
| 458 | explicit pstoreConsole(const std::string&& console) = delete; |
| 459 | explicit pstoreConsole(std::string&& console) = delete; |
| 460 | |
| 461 | // Our implementation of rfind, use exact match first, then resort to fuzzy. |
| 462 | size_t rfind(const std::string& needle) const { |
| 463 | size_t pos = console.rfind(needle); // exact match? |
| 464 | if (pos != std::string::npos) return pos; |
| 465 | |
| 466 | // Check to make sure needle fits in console string. |
| 467 | pos = console.length(); |
| 468 | if (needle.length() > pos) return std::string::npos; |
| 469 | pos -= needle.length(); |
| 470 | // fuzzy match to maximum kBitErrorRate |
Ivan Lozano | 44d3cac | 2017-11-07 13:13:55 -0800 | [diff] [blame] | 471 | for (;;) { |
Mark Salyzyn | 293cb3b | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 472 | if (numError(pos, needle) != std::string::npos) return pos; |
Ivan Lozano | 44d3cac | 2017-11-07 13:13:55 -0800 | [diff] [blame] | 473 | if (pos == 0) break; |
| 474 | --pos; |
| 475 | } |
Mark Salyzyn | 293cb3b | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 476 | return std::string::npos; |
| 477 | } |
| 478 | |
| 479 | // Our implementation of find, use only fuzzy match. |
| 480 | size_t find(const std::string& needle, size_t start = 0) const { |
| 481 | // Check to make sure needle fits in console string. |
| 482 | if (needle.length() > console.length()) return std::string::npos; |
| 483 | const size_t last_pos = console.length() - needle.length(); |
| 484 | // fuzzy match to maximum kBitErrorRate |
| 485 | for (size_t pos = start; pos <= last_pos; ++pos) { |
| 486 | if (numError(pos, needle) != std::string::npos) return pos; |
| 487 | } |
| 488 | return std::string::npos; |
| 489 | } |
Mark Salyzyn | 39cc3e7 | 2018-03-19 15:16:29 -0700 | [diff] [blame] | 490 | |
| 491 | operator const std::string&() const { return console; } |
Mark Salyzyn | 293cb3b | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 492 | }; |
| 493 | |
| 494 | // If bit error match to needle, correct it. |
| 495 | // Return true if any corrections were discovered and applied. |
Mark Salyzyn | 1e7d1c7 | 2018-03-16 08:57:20 -0700 | [diff] [blame] | 496 | bool correctForBitError(std::string& reason, const std::string& needle) { |
Mark Salyzyn | 293cb3b | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 497 | bool corrected = false; |
| 498 | if (reason.length() < needle.length()) return corrected; |
| 499 | const pstoreConsole console(reason); |
| 500 | const size_t last_pos = reason.length() - needle.length(); |
| 501 | for (size_t pos = 0; pos <= last_pos; pos += needle.length()) { |
| 502 | pos = console.find(needle, pos); |
| 503 | if (pos == std::string::npos) break; |
| 504 | |
| 505 | // exact match has no malice |
| 506 | if (needle == reason.substr(pos, needle.length())) continue; |
| 507 | |
| 508 | corrected = true; |
| 509 | reason = reason.substr(0, pos) + needle + reason.substr(pos + needle.length()); |
| 510 | } |
| 511 | return corrected; |
| 512 | } |
| 513 | |
Mark Salyzyn | 1e7d1c7 | 2018-03-16 08:57:20 -0700 | [diff] [blame] | 514 | // If bit error match to needle, correct it. |
| 515 | // Return true if any corrections were discovered and applied. |
| 516 | // Try again if we can replace underline with spaces. |
| 517 | bool correctForBitErrorOrUnderline(std::string& reason, const std::string& needle) { |
| 518 | bool corrected = correctForBitError(reason, needle); |
| 519 | std::string _needle(needle); |
| 520 | std::transform(_needle.begin(), _needle.end(), _needle.begin(), |
| 521 | [](char c) { return (c == '_') ? ' ' : c; }); |
| 522 | if (needle != _needle) { |
| 523 | corrected |= correctForBitError(reason, _needle); |
| 524 | } |
| 525 | return corrected; |
| 526 | } |
| 527 | |
Mark Salyzyn | 39cc3e7 | 2018-03-19 15:16:29 -0700 | [diff] [blame] | 528 | // Converts a string value representing the reason the system booted to a |
| 529 | // string complying with Android system standard reason. |
| 530 | void transformReason(std::string& reason) { |
| 531 | std::transform(reason.begin(), reason.end(), reason.begin(), ::tolower); |
| 532 | std::transform(reason.begin(), reason.end(), reason.begin(), |
| 533 | [](char c) { return ::isblank(c) ? '_' : c; }); |
| 534 | std::transform(reason.begin(), reason.end(), reason.begin(), |
| 535 | [](char c) { return ::isprint(c) ? c : '?'; }); |
| 536 | } |
| 537 | |
Mark Salyzyn | 3f48fa9 | 2018-03-22 08:41:22 -0700 | [diff] [blame] | 538 | // Check subreasons for reboot,<subreason> kernel_panic,sysrq,<subreason> or |
| 539 | // kernel_panic,<subreason>. |
| 540 | // |
| 541 | // If quoted flag is set, pull out and correct single quoted ('), newline (\n) |
| 542 | // or unprintable character terminated subreason, pos is supplied just beyond |
| 543 | // first quote. if quoted false, pull out and correct newline (\n) or |
| 544 | // unprintable character terminated subreason. |
| 545 | // |
| 546 | // Heuristics to find termination is painted into a corner: |
| 547 | |
| 548 | // single bit error for quote ' that we can block. It is acceptable for |
| 549 | // the others 7, g in reason. 2/9 chance will miss the terminating quote, |
| 550 | // but there is always the terminating newline that usually immediately |
| 551 | // follows to fortify our chances. |
| 552 | bool likely_single_quote(char c) { |
| 553 | switch (static_cast<uint8_t>(c)) { |
| 554 | case '\'': // '\'' |
| 555 | case '\'' ^ 0x01: // '&' |
| 556 | case '\'' ^ 0x02: // '%' |
| 557 | case '\'' ^ 0x04: // '#' |
| 558 | case '\'' ^ 0x08: // '/' |
| 559 | return true; |
| 560 | case '\'' ^ 0x10: // '7' |
| 561 | break; |
| 562 | case '\'' ^ 0x20: // '\a' (unprintable) |
| 563 | return true; |
| 564 | case '\'' ^ 0x40: // 'g' |
| 565 | break; |
| 566 | case '\'' ^ 0x80: // 0xA7 (unprintable) |
| 567 | return true; |
| 568 | } |
| 569 | return false; |
| 570 | } |
| 571 | |
| 572 | // ::isprint(c) and likely_space() will prevent us from being called for |
| 573 | // fundamentally printable entries, except for '\r' and '\b'. |
| 574 | // |
| 575 | // Except for * and J, single bit errors for \n, all others are non- |
| 576 | // printable so easy catch. It is _acceptable_ for *, J or j to exist in |
| 577 | // the reason string, so 2/9 chance we will miss the terminating newline. |
| 578 | // |
| 579 | // NB: J might not be acceptable, except if at the beginning or preceded |
| 580 | // with a space, '(' or any of the quotes and their BER aliases. |
| 581 | // NB: * might not be acceptable, except if at the beginning or preceded |
| 582 | // with a space, another *, or any of the quotes or their BER aliases. |
| 583 | // |
| 584 | // To reduce the chances to closer to 1/9 is too complicated for the gain. |
| 585 | bool likely_newline(char c) { |
| 586 | switch (static_cast<uint8_t>(c)) { |
| 587 | case '\n': // '\n' (unprintable) |
| 588 | case '\n' ^ 0x01: // '\r' (unprintable) |
| 589 | case '\n' ^ 0x02: // '\b' (unprintable) |
| 590 | case '\n' ^ 0x04: // 0x0E (unprintable) |
| 591 | case '\n' ^ 0x08: // 0x02 (unprintable) |
| 592 | case '\n' ^ 0x10: // 0x1A (unprintable) |
| 593 | return true; |
| 594 | case '\n' ^ 0x20: // '*' |
| 595 | case '\n' ^ 0x40: // 'J' |
| 596 | break; |
| 597 | case '\n' ^ 0x80: // 0x8A (unprintable) |
| 598 | return true; |
| 599 | } |
| 600 | return false; |
| 601 | } |
| 602 | |
| 603 | // ::isprint(c) will prevent us from being called for all the printable |
| 604 | // matches below. If we let unprintables through because of this, they |
| 605 | // get converted to underscore (_) by the validation phase. |
| 606 | bool likely_space(char c) { |
| 607 | switch (static_cast<uint8_t>(c)) { |
| 608 | case ' ': // ' ' |
| 609 | case ' ' ^ 0x01: // '!' |
| 610 | case ' ' ^ 0x02: // '"' |
| 611 | case ' ' ^ 0x04: // '$' |
| 612 | case ' ' ^ 0x08: // '(' |
| 613 | case ' ' ^ 0x10: // '0' |
| 614 | case ' ' ^ 0x20: // '\0' (unprintable) |
| 615 | case ' ' ^ 0x40: // 'P' |
| 616 | case ' ' ^ 0x80: // 0xA0 (unprintable) |
| 617 | case '\t': // '\t' |
| 618 | case '\t' ^ 0x01: // '\b' (unprintable) (likely_newline counters) |
| 619 | case '\t' ^ 0x02: // '\v' (unprintable) |
| 620 | case '\t' ^ 0x04: // '\r' (unprintable) (likely_newline counters) |
| 621 | case '\t' ^ 0x08: // 0x01 (unprintable) |
| 622 | case '\t' ^ 0x10: // 0x19 (unprintable) |
| 623 | case '\t' ^ 0x20: // ')' |
| 624 | case '\t' ^ 0x40: // '1' |
| 625 | case '\t' ^ 0x80: // 0x89 (unprintable) |
| 626 | return true; |
| 627 | } |
| 628 | return false; |
| 629 | } |
| 630 | |
| 631 | std::string getSubreason(const std::string& content, size_t pos, bool quoted) { |
Mark Salyzyn | 39cc3e7 | 2018-03-19 15:16:29 -0700 | [diff] [blame] | 632 | static constexpr size_t max_reason_length = 256; |
| 633 | |
| 634 | std::string subReason(content.substr(pos, max_reason_length)); |
| 635 | // Correct against any known strings that Bit Error Match |
| 636 | for (const auto& s : knownReasons) { |
| 637 | correctForBitErrorOrUnderline(subReason, s); |
| 638 | } |
Mark Salyzyn | 3f48fa9 | 2018-03-22 08:41:22 -0700 | [diff] [blame] | 639 | std::string terminator(quoted ? "'" : ""); |
Mark Salyzyn | 39cc3e7 | 2018-03-19 15:16:29 -0700 | [diff] [blame] | 640 | for (const auto& m : kBootReasonMap) { |
| 641 | if (m.first.length() <= strlen("cold")) continue; // too short? |
Mark Salyzyn | 3f48fa9 | 2018-03-22 08:41:22 -0700 | [diff] [blame] | 642 | if (correctForBitErrorOrUnderline(subReason, m.first + terminator)) continue; |
Mark Salyzyn | 39cc3e7 | 2018-03-19 15:16:29 -0700 | [diff] [blame] | 643 | if (m.first.length() <= strlen("reboot,cold")) continue; // short? |
| 644 | if (android::base::StartsWith(m.first, "reboot,")) { |
Mark Salyzyn | 3f48fa9 | 2018-03-22 08:41:22 -0700 | [diff] [blame] | 645 | correctForBitErrorOrUnderline(subReason, m.first.substr(strlen("reboot,")) + terminator); |
Mark Salyzyn | 39cc3e7 | 2018-03-19 15:16:29 -0700 | [diff] [blame] | 646 | } else if (android::base::StartsWith(m.first, "kernel_panic,sysrq,")) { |
Mark Salyzyn | 3f48fa9 | 2018-03-22 08:41:22 -0700 | [diff] [blame] | 647 | correctForBitErrorOrUnderline(subReason, |
| 648 | m.first.substr(strlen("kernel_panic,sysrq,")) + terminator); |
| 649 | } else if (android::base::StartsWith(m.first, "kernel_panic,")) { |
| 650 | correctForBitErrorOrUnderline(subReason, m.first.substr(strlen("kernel_panic,")) + terminator); |
Mark Salyzyn | 39cc3e7 | 2018-03-19 15:16:29 -0700 | [diff] [blame] | 651 | } |
| 652 | } |
| 653 | for (pos = 0; pos < subReason.length(); ++pos) { |
| 654 | char c = subReason[pos]; |
Mark Salyzyn | 3f48fa9 | 2018-03-22 08:41:22 -0700 | [diff] [blame] | 655 | if (!(::isprint(c) || likely_space(c)) || likely_newline(c) || |
| 656 | (quoted && likely_single_quote(c))) { |
Mark Salyzyn | 39cc3e7 | 2018-03-19 15:16:29 -0700 | [diff] [blame] | 657 | subReason.erase(pos); |
| 658 | break; |
| 659 | } |
| 660 | } |
| 661 | transformReason(subReason); |
| 662 | return subReason; |
| 663 | } |
| 664 | |
Mark Salyzyn | 293cb3b | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 665 | bool addKernelPanicSubReason(const pstoreConsole& console, std::string& ret) { |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 666 | // Check for kernel panic types to refine information |
Mark Salyzyn | 853bb80 | 2018-03-16 08:44:56 -0700 | [diff] [blame] | 667 | if ((console.rfind("SysRq : Trigger a crash") != std::string::npos) || |
| 668 | (console.rfind("PC is at sysrq_handle_crash+") != std::string::npos)) { |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 669 | ret = "kernel_panic,sysrq"; |
Mark Salyzyn | 39cc3e7 | 2018-03-19 15:16:29 -0700 | [diff] [blame] | 670 | // Invented for Android to allow daemons that specifically trigger sysrq |
| 671 | // to communicate more accurate boot subreasons via last console messages. |
| 672 | static constexpr char sysrqSubreason[] = "SysRq : Trigger a crash : '"; |
| 673 | auto pos = console.rfind(sysrqSubreason); |
| 674 | if (pos != std::string::npos) { |
Mark Salyzyn | 3f48fa9 | 2018-03-22 08:41:22 -0700 | [diff] [blame] | 675 | ret += "," + getSubreason(console, pos + strlen(sysrqSubreason), /* quoted */ true); |
Mark Salyzyn | 39cc3e7 | 2018-03-19 15:16:29 -0700 | [diff] [blame] | 676 | } |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 677 | return true; |
| 678 | } |
| 679 | if (console.rfind("Unable to handle kernel NULL pointer dereference at virtual address") != |
| 680 | std::string::npos) { |
Mark Salyzyn | 853bb80 | 2018-03-16 08:44:56 -0700 | [diff] [blame] | 681 | ret = "kernel_panic,null"; |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 682 | return true; |
| 683 | } |
| 684 | if (console.rfind("Kernel BUG at ") != std::string::npos) { |
Mark Salyzyn | 853bb80 | 2018-03-16 08:44:56 -0700 | [diff] [blame] | 685 | ret = "kernel_panic,bug"; |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 686 | return true; |
| 687 | } |
Mark Salyzyn | 3f48fa9 | 2018-03-22 08:41:22 -0700 | [diff] [blame] | 688 | |
| 689 | std::string panic("Kernel panic - not syncing: "); |
| 690 | auto pos = console.rfind(panic); |
| 691 | if (pos != std::string::npos) { |
| 692 | static const std::vector<std::pair<const std::string, const std::string>> panicReasons = { |
| 693 | {"Out of memory", "oom"}, |
| 694 | {"out of memory", "oom"}, |
| 695 | {"Oh boy, that early out of memory", "oom"}, // omg |
| 696 | {"BUG!", "bug"}, |
| 697 | {"hung_task: blocked tasks", "hung"}, |
| 698 | {"audit: ", "audit"}, |
| 699 | {"scheduling while atomic", "atomic"}, |
| 700 | {"Attempted to kill init!", "init"}, |
| 701 | {"Requested init", "init"}, |
| 702 | {"No working init", "init"}, |
| 703 | {"Could not decompress init", "init"}, |
| 704 | {"RCU Stall", "hung,rcu"}, |
| 705 | {"stack-protector", "stack"}, |
| 706 | {"kernel stack overflow", "stack"}, |
| 707 | {"Corrupt kernel stack", "stack"}, |
| 708 | {"low stack detected", "stack"}, |
| 709 | {"corrupted stack end", "stack"}, |
Mark Salyzyn | 8ad6e67 | 2018-06-01 08:59:05 -0700 | [diff] [blame] | 710 | {"subsys-restart: Resetting the SoC - modem crashed.", "modem"}, |
| 711 | {"subsys-restart: Resetting the SoC - adsp crashed.", "adsp"}, |
| 712 | {"subsys-restart: Resetting the SoC - dsps crashed.", "dsps"}, |
| 713 | {"subsys-restart: Resetting the SoC - wcnss crashed.", "wcnss"}, |
Mark Salyzyn | 3f48fa9 | 2018-03-22 08:41:22 -0700 | [diff] [blame] | 714 | }; |
| 715 | |
| 716 | ret = "kernel_panic"; |
| 717 | for (auto& s : panicReasons) { |
| 718 | if (console.find(panic + s.first, pos) != std::string::npos) { |
| 719 | ret += "," + s.second; |
| 720 | return true; |
| 721 | } |
| 722 | } |
| 723 | auto reason = getSubreason(console, pos + panic.length(), /* newline */ false); |
| 724 | if (reason.length() > 3) { |
| 725 | ret += "," + reason; |
| 726 | } |
| 727 | return true; |
| 728 | } |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 729 | return false; |
| 730 | } |
| 731 | |
Mark Salyzyn | 293cb3b | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 732 | bool addKernelPanicSubReason(const std::string& content, std::string& ret) { |
| 733 | return addKernelPanicSubReason(pstoreConsole(content), ret); |
| 734 | } |
| 735 | |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 736 | const char system_reboot_reason_property[] = "sys.boot.reason"; |
| 737 | const char last_reboot_reason_property[] = LAST_REBOOT_REASON_PROPERTY; |
Mark Salyzyn | adc433d | 2018-06-05 08:17:35 -0700 | [diff] [blame] | 738 | const char last_last_reboot_reason_property[] = "sys.boot.reason.last"; |
Mark Salyzyn | 5c58c9d | 2018-06-28 09:21:55 -0700 | [diff] [blame^] | 739 | constexpr size_t history_reboot_reason_size = 4; |
| 740 | const char history_reboot_reason_property[] = LAST_REBOOT_REASON_PROPERTY ".history"; |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 741 | const char bootloader_reboot_reason_property[] = "ro.boot.bootreason"; |
| 742 | |
Mark Salyzyn | 5c58c9d | 2018-06-28 09:21:55 -0700 | [diff] [blame^] | 743 | // Land system_boot_reason into system_reboot_reason_property. |
| 744 | // Shift system_boot_reason into history_reboot_reason_property. |
| 745 | void BootReasonAddToHistory(const std::string& system_boot_reason) { |
| 746 | if (system_boot_reason.empty()) return; |
| 747 | LOG(INFO) << "Canonical boot reason: " << system_boot_reason; |
| 748 | auto old_system_boot_reason = GetProperty(system_reboot_reason_property); |
| 749 | if (!SetProperty(system_reboot_reason_property, system_boot_reason)) { |
| 750 | SetProperty(system_reboot_reason_property, system_boot_reason.substr(0, PROPERTY_VALUE_MAX - 1)); |
| 751 | } |
| 752 | auto reason_history = android::base::Split(GetProperty(history_reboot_reason_property), "\n"); |
| 753 | static auto mark = time(nullptr); |
| 754 | auto mark_str = std::string(",") + std::to_string(mark); |
| 755 | auto marked_system_boot_reason = system_boot_reason + mark_str; |
| 756 | if (!reason_history.empty()) { |
| 757 | // delete any entries that we just wrote in a previous |
| 758 | // call and leveraging duplicate line handling |
| 759 | auto last = old_system_boot_reason + mark_str; |
| 760 | // trim the list to (history_reboot_reason_size - 1) |
| 761 | ssize_t max = history_reboot_reason_size; |
| 762 | for (auto it = reason_history.begin(); it != reason_history.end();) { |
| 763 | if (it->empty() || (last == *it) || (marked_system_boot_reason == *it) || (--max <= 0)) { |
| 764 | it = reason_history.erase(it); |
| 765 | } else { |
| 766 | last = *it; |
| 767 | ++it; |
| 768 | } |
| 769 | } |
| 770 | } |
| 771 | // insert at the front, concatenating mark (<epoch time>) detail to the value. |
| 772 | reason_history.insert(reason_history.begin(), marked_system_boot_reason); |
| 773 | // If the property string is too long ( > PROPERTY_VALUE_MAX) |
| 774 | // we get an error, so trim out last entry and try again. |
| 775 | while (!(SetProperty(history_reboot_reason_property, android::base::Join(reason_history, '\n')))) { |
| 776 | auto it = std::prev(reason_history.end()); |
| 777 | if (it == reason_history.end()) break; |
| 778 | reason_history.erase(it); |
| 779 | } |
| 780 | } |
| 781 | |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 782 | // Scrub, Sanitize, Standardize and Enhance the boot reason string supplied. |
| 783 | std::string BootReasonStrToReason(const std::string& boot_reason) { |
| 784 | std::string ret(GetProperty(system_reboot_reason_property)); |
| 785 | std::string reason(boot_reason); |
| 786 | // If sys.boot.reason == ro.boot.bootreason, let's re-evaluate |
| 787 | if (reason == ret) ret = ""; |
| 788 | |
Mark Salyzyn | 88d692c | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 789 | transformReason(reason); |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 790 | |
| 791 | // Is the current system boot reason sys.boot.reason valid? |
| 792 | if (!isKnownRebootReason(ret)) ret = ""; |
| 793 | |
| 794 | if (ret == "") { |
| 795 | // Is the bootloader boot reason ro.boot.bootreason known? |
| 796 | std::vector<std::string> words(android::base::Split(reason, ",_-")); |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 797 | for (auto& s : knownReasons) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 798 | std::string blunt; |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 799 | for (auto& r : words) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 800 | if (r == s) { |
| 801 | if (isBluntRebootReason(s)) { |
| 802 | blunt = s; |
| 803 | } else { |
| 804 | ret = s; |
| 805 | break; |
| 806 | } |
| 807 | } |
| 808 | } |
| 809 | if (ret == "") ret = blunt; |
| 810 | if (ret != "") break; |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | if (ret == "") { |
| 815 | // A series of checks to take some officially unsupported reasons |
| 816 | // reported by the bootloader and find some logical and canonical |
| 817 | // sense. In an ideal world, we would require those bootloaders |
Mark Salyzyn | 25900dd | 2018-03-16 09:05:59 -0700 | [diff] [blame] | 818 | // to behave and follow our CTS standards. |
| 819 | // |
| 820 | // first member is the output |
| 821 | // second member is an unanchored regex for an alias |
| 822 | // |
Mark Salyzyn | 2819328 | 2018-03-16 09:05:59 -0700 | [diff] [blame] | 823 | // If output has a prefix of <bang> '!', we do not use it as a |
| 824 | // match needle (and drop the <bang> prefix when landing in output), |
| 825 | // otherwise look for it as well. This helps keep the scale of the |
Mark Salyzyn | 25900dd | 2018-03-16 09:05:59 -0700 | [diff] [blame] | 826 | // following table smaller. |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 827 | static const std::vector<std::pair<const std::string, const std::string>> aliasReasons = { |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 828 | {"watchdog", "wdog"}, |
Mark Salyzyn | 25900dd | 2018-03-16 09:05:59 -0700 | [diff] [blame] | 829 | {"cold,powerkey", "powerkey|power_key|PowerKey"}, |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 830 | {"kernel_panic", "panic"}, |
| 831 | {"shutdown,thermal", "thermal"}, |
| 832 | {"warm,s3_wakeup", "s3_wakeup"}, |
| 833 | {"hard,hw_reset", "hw_reset"}, |
Mark Salyzyn | 8aa36c6 | 2018-03-16 11:00:14 -0700 | [diff] [blame] | 834 | {"cold,charger", "usb"}, |
| 835 | {"cold,rtc", "rtc"}, |
Mark Salyzyn | 186f676 | 2018-03-16 11:00:26 -0700 | [diff] [blame] | 836 | {"cold,rtc,2sec", "2sec_reboot"}, |
| 837 | {"!warm", "wdt_by_pass_pwk"}, // change flavour of blunt |
| 838 | {"!reboot", "^wdt$"}, // change flavour of blunt |
| 839 | {"reboot,tool", "tool_by_pass_pwk"}, |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 840 | {"bootloader", ""}, |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 841 | }; |
| 842 | |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 843 | for (auto& s : aliasReasons) { |
Mark Salyzyn | 2819328 | 2018-03-16 09:05:59 -0700 | [diff] [blame] | 844 | size_t firstHasNot = s.first[0] == '!'; |
| 845 | if (!firstHasNot && (reason.find(s.first) != std::string::npos)) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 846 | ret = s.first; |
| 847 | break; |
| 848 | } |
Mark Salyzyn | 25900dd | 2018-03-16 09:05:59 -0700 | [diff] [blame] | 849 | if (s.second.size() && std::regex_search(reason, std::regex(s.second))) { |
Mark Salyzyn | 2819328 | 2018-03-16 09:05:59 -0700 | [diff] [blame] | 850 | ret = s.first.substr(firstHasNot); |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 851 | break; |
| 852 | } |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | // If watchdog is the reason, see if there is a security angle? |
| 857 | if (ret == "watchdog") { |
| 858 | if (reason.find("sec") != std::string::npos) { |
| 859 | ret += ",security"; |
| 860 | } |
| 861 | } |
| 862 | |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 863 | if (ret == "kernel_panic") { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 864 | // Check to see if last klog has some refinement hints. |
| 865 | std::string content; |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 866 | if (readPstoreConsole(content)) { |
| 867 | addKernelPanicSubReason(content, ret); |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 868 | } |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 869 | } else if (isBluntRebootReason(ret)) { |
| 870 | // Check the other available reason resources if the reason is still blunt. |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 871 | |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 872 | // Check to see if last klog has some refinement hints. |
| 873 | std::string content; |
| 874 | if (readPstoreConsole(content)) { |
Mark Salyzyn | 293cb3b | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 875 | const pstoreConsole console(content); |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 876 | // The toybox reboot command used directly (unlikely)? But also |
| 877 | // catches init's response to Android's more controlled reboot command. |
Mark Salyzyn | 293cb3b | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 878 | if (console.rfind("reboot: Power down") != std::string::npos) { |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 879 | ret = "shutdown"; // Still too blunt, but more accurate. |
| 880 | // ToDo: init should record the shutdown reason to kernel messages ala: |
| 881 | // init: shutdown system with command 'last_reboot_reason' |
| 882 | // so that if pstore has persistence we can get some details |
| 883 | // that could be missing in last_reboot_reason_property. |
| 884 | } |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 885 | |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 886 | static const char cmd[] = "reboot: Restarting system with command '"; |
Mark Salyzyn | 293cb3b | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 887 | size_t pos = console.rfind(cmd); |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 888 | if (pos != std::string::npos) { |
Mark Salyzyn | 3f48fa9 | 2018-03-22 08:41:22 -0700 | [diff] [blame] | 889 | std::string subReason(getSubreason(content, pos + strlen(cmd), /* quoted */ true)); |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 890 | if (subReason != "") { // Will not land "reboot" as that is too blunt. |
| 891 | if (isKernelRebootReason(subReason)) { |
| 892 | ret = "reboot," + subReason; // User space can't talk kernel reasons. |
Mark Salyzyn | dafced9 | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 893 | } else if (isKnownRebootReason(subReason)) { |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 894 | ret = subReason; |
Mark Salyzyn | dafced9 | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 895 | } else { |
| 896 | ret = "reboot," + subReason; // legitimize unknown reasons |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 897 | } |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 898 | } |
Mark Salyzyn | 1519925 | 2018-03-16 09:26:05 -0700 | [diff] [blame] | 899 | // Some bootloaders shutdown results record in last kernel message. |
| 900 | if (!strcmp(ret.c_str(), "reboot,kernel_power_off_charging__reboot_system")) { |
| 901 | ret = "shutdown"; |
| 902 | } |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 903 | } |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 904 | |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 905 | // Check for kernel panics, allowed to override reboot command. |
Mark Salyzyn | 293cb3b | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 906 | if (!addKernelPanicSubReason(console, ret) && |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 907 | // check for long-press power down |
Mark Salyzyn | 293cb3b | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 908 | ((console.rfind("Power held for ") != std::string::npos) || |
| 909 | (console.rfind("charger: [") != std::string::npos))) { |
Mark Salyzyn | 6461089 | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 910 | ret = "cold"; |
| 911 | } |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 912 | } |
| 913 | |
Elliott Hughes | 50a24eb | 2018-06-14 10:59:09 -0700 | [diff] [blame] | 914 | // TODO: use the HAL to get battery level (http://b/77725702). |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 915 | |
| 916 | // Is there a controlled shutdown hint in last_reboot_reason_property? |
| 917 | if (isBluntRebootReason(ret)) { |
| 918 | // Content buffer no longer will have console data. Beware if more |
| 919 | // checks added below, that depend on parsing console content. |
| 920 | content = GetProperty(last_reboot_reason_property); |
Mark Salyzyn | 88d692c | 2017-09-20 08:37:46 -0700 | [diff] [blame] | 921 | transformReason(content); |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 922 | |
Mark Salyzyn | 6290982 | 2017-10-09 09:27:16 -0700 | [diff] [blame] | 923 | // Anything in last is better than 'super-blunt' reboot or shutdown. |
| 924 | if ((ret == "") || (ret == "reboot") || (ret == "shutdown") || !isBluntRebootReason(content)) { |
| 925 | ret = content; |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 926 | } |
| 927 | } |
| 928 | |
| 929 | // Other System Health HAL reasons? |
| 930 | |
| 931 | // ToDo: /proc/sys/kernel/boot_reason needs a HAL interface to |
| 932 | // possibly offer hardware-specific clues from the PMIC. |
| 933 | } |
| 934 | |
| 935 | // If unknown left over from above, make it "reboot,<boot_reason>" |
| 936 | if (ret == "") { |
| 937 | ret = "reboot"; |
| 938 | if (android::base::StartsWith(reason, "reboot")) { |
| 939 | reason = reason.substr(strlen("reboot")); |
Mark Salyzyn | 0af71a5 | 2017-10-05 13:58:04 -0700 | [diff] [blame] | 940 | while ((reason[0] == ',') || (reason[0] == '_')) { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 941 | reason = reason.substr(1); |
| 942 | } |
| 943 | } |
| 944 | if (reason != "") { |
| 945 | ret += ","; |
| 946 | ret += reason; |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | LOG(INFO) << "Canonical boot reason: " << ret; |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 951 | return ret; |
| 952 | } |
| 953 | |
James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 954 | // Returns the appropriate metric key prefix for the boot_complete metric such |
| 955 | // that boot metrics after a system update are labeled as ota_boot_complete; |
| 956 | // otherwise, they are labeled as boot_complete. This method encapsulates the |
| 957 | // bookkeeping required to track when a system update has occurred by storing |
| 958 | // the UTC timestamp of the system build date and comparing against the current |
| 959 | // system build date. |
| 960 | std::string CalculateBootCompletePrefix() { |
| 961 | static const std::string kBuildDateKey = "build_date"; |
| 962 | std::string boot_complete_prefix = "boot_complete"; |
| 963 | |
| 964 | std::string build_date_str = GetProperty("ro.build.date.utc"); |
James Hawkins | 4dded61 | 2016-07-28 11:50:23 -0700 | [diff] [blame] | 965 | int32_t build_date; |
Elliott Hughes | da46b39 | 2016-10-11 17:09:00 -0700 | [diff] [blame] | 966 | if (!android::base::ParseInt(build_date_str, &build_date)) { |
James Hawkins | 4dded61 | 2016-07-28 11:50:23 -0700 | [diff] [blame] | 967 | return std::string(); |
| 968 | } |
James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 969 | |
| 970 | BootEventRecordStore boot_event_store; |
| 971 | BootEventRecordStore::BootEventRecord record; |
James Hawkins | 0bc4ad4 | 2017-05-30 15:03:15 -0700 | [diff] [blame] | 972 | if (!boot_event_store.GetBootEvent(kBuildDateKey, &record)) { |
| 973 | boot_complete_prefix = "factory_reset_" + boot_complete_prefix; |
| 974 | boot_event_store.AddBootEventWithValue(kBuildDateKey, build_date); |
Mark Salyzyn | 5c58c9d | 2018-06-28 09:21:55 -0700 | [diff] [blame^] | 975 | BootReasonAddToHistory("reboot,factory_reset"); |
James Hawkins | 0bc4ad4 | 2017-05-30 15:03:15 -0700 | [diff] [blame] | 976 | } else if (build_date != record.second) { |
James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 977 | boot_complete_prefix = "ota_" + boot_complete_prefix; |
| 978 | boot_event_store.AddBootEventWithValue(kBuildDateKey, build_date); |
Mark Salyzyn | 5c58c9d | 2018-06-28 09:21:55 -0700 | [diff] [blame^] | 979 | BootReasonAddToHistory("reboot,ota"); |
James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 980 | } |
| 981 | |
| 982 | return boot_complete_prefix; |
| 983 | } |
| 984 | |
James Hawkins | ef0a090 | 2017-01-06 14:38:23 -0800 | [diff] [blame] | 985 | // Records the value of a given ro.boottime.init property in milliseconds. |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 986 | void RecordInitBootTimeProp(BootEventRecordStore* boot_event_store, const char* property) { |
James Hawkins | ef0a090 | 2017-01-06 14:38:23 -0800 | [diff] [blame] | 987 | std::string value = GetProperty(property); |
| 988 | |
James Hawkins | 27c0522 | 2017-01-26 11:55:44 -0800 | [diff] [blame] | 989 | int32_t time_in_ms; |
| 990 | if (android::base::ParseInt(value, &time_in_ms)) { |
James Hawkins | ef0a090 | 2017-01-06 14:38:23 -0800 | [diff] [blame] | 991 | boot_event_store->AddBootEventWithValue(property, time_in_ms); |
| 992 | } |
| 993 | } |
| 994 | |
James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 995 | // A map from bootloader timing stage to the time that stage took during boot. |
| 996 | typedef std::map<std::string, int32_t> BootloaderTimingMap; |
| 997 | |
| 998 | // Returns a mapping from bootloader stage names to the time those stages |
| 999 | // took to boot. |
| 1000 | const BootloaderTimingMap GetBootLoaderTimings() { |
| 1001 | BootloaderTimingMap timings; |
| 1002 | |
| 1003 | // |ro.boot.boottime| is of the form 'stage1:time1,...,stageN:timeN', |
| 1004 | // where timeN is in milliseconds. |
James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 1005 | std::string value = GetProperty("ro.boot.boottime"); |
James Hawkins | 6b5c5aa | 2017-02-16 11:53:03 -0800 | [diff] [blame] | 1006 | if (value.empty()) { |
| 1007 | // ro.boot.boottime is not reported on all devices. |
James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 1008 | return BootloaderTimingMap(); |
James Hawkins | 6b5c5aa | 2017-02-16 11:53:03 -0800 | [diff] [blame] | 1009 | } |
James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 1010 | |
| 1011 | auto stages = android::base::Split(value, ","); |
James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 1012 | for (const auto& stageTiming : stages) { |
James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 1013 | // |stageTiming| is of the form 'stage:time'. |
| 1014 | auto stageTimingValues = android::base::Split(stageTiming, ":"); |
James Hawkins | 0bc4ad4 | 2017-05-30 15:03:15 -0700 | [diff] [blame] | 1015 | DCHECK_EQ(2U, stageTimingValues.size()); |
James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 1016 | |
| 1017 | std::string stageName = stageTimingValues[0]; |
| 1018 | int32_t time_ms; |
| 1019 | if (android::base::ParseInt(stageTimingValues[1], &time_ms)) { |
James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 1020 | timings[stageName] = time_ms; |
James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 1021 | } |
| 1022 | } |
James Hawkins | 6b5c5aa | 2017-02-16 11:53:03 -0800 | [diff] [blame] | 1023 | |
James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 1024 | return timings; |
| 1025 | } |
| 1026 | |
| 1027 | // Parses and records the set of bootloader stages and associated boot times |
| 1028 | // from the ro.boot.boottime system property. |
| 1029 | void RecordBootloaderTimings(BootEventRecordStore* boot_event_store, |
| 1030 | const BootloaderTimingMap& bootloader_timings) { |
| 1031 | int32_t total_time = 0; |
| 1032 | for (const auto& timing : bootloader_timings) { |
| 1033 | total_time += timing.second; |
| 1034 | boot_event_store->AddBootEventWithValue("boottime.bootloader." + timing.first, timing.second); |
| 1035 | } |
| 1036 | |
James Hawkins | 6b5c5aa | 2017-02-16 11:53:03 -0800 | [diff] [blame] | 1037 | boot_event_store->AddBootEventWithValue("boottime.bootloader.total", total_time); |
James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 1038 | } |
| 1039 | |
James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 1040 | // Records the closest estimation to the absolute device boot time, i.e., |
| 1041 | // from power on to boot_complete, including bootloader times. |
| 1042 | void RecordAbsoluteBootTime(BootEventRecordStore* boot_event_store, |
| 1043 | const BootloaderTimingMap& bootloader_timings, |
| 1044 | std::chrono::milliseconds uptime) { |
| 1045 | int32_t bootloader_time_ms = 0; |
| 1046 | |
| 1047 | for (const auto& timing : bootloader_timings) { |
| 1048 | if (timing.first.compare("SW") != 0) { |
| 1049 | bootloader_time_ms += timing.second; |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | auto bootloader_duration = std::chrono::milliseconds(bootloader_time_ms); |
| 1054 | auto absolute_total = |
| 1055 | std::chrono::duration_cast<std::chrono::seconds>(bootloader_duration + uptime); |
| 1056 | boot_event_store->AddBootEventWithValue("absolute_boot_time", absolute_total.count()); |
| 1057 | } |
| 1058 | |
Luis Hector Chavez | 03aae15 | 2018-04-12 15:25:15 -0700 | [diff] [blame] | 1059 | // Gets the boot time offset. This is useful when Android is running in a |
| 1060 | // container, because the boot_clock is not reset when Android reboots. |
| 1061 | std::chrono::nanoseconds GetBootTimeOffset() { |
| 1062 | static const int64_t boottime_offset = |
| 1063 | android::base::GetIntProperty<int64_t>("ro.boot.boottime_offset", 0); |
| 1064 | return std::chrono::nanoseconds(boottime_offset); |
| 1065 | } |
| 1066 | |
| 1067 | // Returns the current uptime, accounting for any offset in the CLOCK_BOOTTIME |
| 1068 | // clock. |
| 1069 | android::base::boot_clock::duration GetUptime() { |
| 1070 | return android::base::boot_clock::now().time_since_epoch() - GetBootTimeOffset(); |
| 1071 | } |
| 1072 | |
Tej Singh | 993f656 | 2018-02-06 15:57:38 -0800 | [diff] [blame] | 1073 | void SetSystemBootReason() { |
| 1074 | const std::string bootloader_boot_reason(GetProperty(bootloader_reboot_reason_property)); |
| 1075 | const std::string system_boot_reason(BootReasonStrToReason(bootloader_boot_reason)); |
| 1076 | // Record the scrubbed system_boot_reason to the property |
Mark Salyzyn | 5c58c9d | 2018-06-28 09:21:55 -0700 | [diff] [blame^] | 1077 | BootReasonAddToHistory(system_boot_reason); |
Mark Salyzyn | adc433d | 2018-06-05 08:17:35 -0700 | [diff] [blame] | 1078 | // Shift last_reboot_reason_property to last_last_reboot_reason_property |
| 1079 | std::string last_boot_reason(GetProperty(last_reboot_reason_property)); |
| 1080 | if (last_boot_reason.empty() || isKernelRebootReason(system_boot_reason)) { |
| 1081 | last_boot_reason = system_boot_reason; |
| 1082 | } else { |
| 1083 | transformReason(last_boot_reason); |
| 1084 | } |
| 1085 | SetProperty(last_last_reboot_reason_property, last_boot_reason); |
| 1086 | SetProperty(last_reboot_reason_property, ""); |
Tej Singh | 993f656 | 2018-02-06 15:57:38 -0800 | [diff] [blame] | 1087 | } |
| 1088 | |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 1089 | // Records several metrics related to the time it takes to boot the device, |
| 1090 | // including disambiguating boot time on encrypted or non-encrypted devices. |
| 1091 | void RecordBootComplete() { |
| 1092 | BootEventRecordStore boot_event_store; |
James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 1093 | BootEventRecordStore::BootEventRecord record; |
James Hawkins | 2d8b3e6 | 2016-04-14 14:13:20 -0700 | [diff] [blame] | 1094 | |
Luis Hector Chavez | 03aae15 | 2018-04-12 15:25:15 -0700 | [diff] [blame] | 1095 | auto uptime_ns = GetUptime(); |
| 1096 | auto uptime_s = std::chrono::duration_cast<std::chrono::seconds>(uptime_ns); |
James Hawkins | 2d8b3e6 | 2016-04-14 14:13:20 -0700 | [diff] [blame] | 1097 | time_t current_time_utc = time(nullptr); |
| 1098 | |
| 1099 | if (boot_event_store.GetBootEvent("last_boot_time_utc", &record)) { |
| 1100 | time_t last_boot_time_utc = record.second; |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 1101 | time_t time_since_last_boot = difftime(current_time_utc, last_boot_time_utc); |
| 1102 | boot_event_store.AddBootEventWithValue("time_since_last_boot", time_since_last_boot); |
James Hawkins | 2d8b3e6 | 2016-04-14 14:13:20 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | boot_event_store.AddBootEventWithValue("last_boot_time_utc", current_time_utc); |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 1106 | |
James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 1107 | // The boot_complete metric has two variants: boot_complete and |
| 1108 | // ota_boot_complete. The latter signifies that the device is booting after |
| 1109 | // a system update. |
| 1110 | std::string boot_complete_prefix = CalculateBootCompletePrefix(); |
James Hawkins | 4dded61 | 2016-07-28 11:50:23 -0700 | [diff] [blame] | 1111 | if (boot_complete_prefix.empty()) { |
| 1112 | // The system is hosed because the build date property could not be read. |
| 1113 | return; |
| 1114 | } |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 1115 | |
| 1116 | // post_decrypt_time_elapsed is only logged on encrypted devices. |
| 1117 | if (boot_event_store.GetBootEvent("post_decrypt_time_elapsed", &record)) { |
| 1118 | // Log the amount of time elapsed until the device is decrypted, which |
| 1119 | // includes the variable amount of time the user takes to enter the |
| 1120 | // decryption password. |
Luis Hector Chavez | 03aae15 | 2018-04-12 15:25:15 -0700 | [diff] [blame] | 1121 | boot_event_store.AddBootEventWithValue("boot_decryption_complete", uptime_s.count()); |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 1122 | |
| 1123 | // Subtract the decryption time to normalize the boot cycle timing. |
Luis Hector Chavez | 03aae15 | 2018-04-12 15:25:15 -0700 | [diff] [blame] | 1124 | std::chrono::seconds boot_complete = std::chrono::seconds(uptime_s.count() - record.second); |
James Hawkins | b9cf771 | 2016-04-08 15:32:19 -0700 | [diff] [blame] | 1125 | boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_post_decrypt", |
James Hawkins | e78ea77 | 2017-03-24 11:43:02 -0700 | [diff] [blame] | 1126 | boot_complete.count()); |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 1127 | } else { |
Luis Hector Chavez | 03aae15 | 2018-04-12 15:25:15 -0700 | [diff] [blame] | 1128 | boot_event_store.AddBootEventWithValue(boot_complete_prefix + "_no_encryption", |
| 1129 | uptime_s.count()); |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | // Record the total time from device startup to boot complete, regardless of |
| 1133 | // encryption state. |
Luis Hector Chavez | 03aae15 | 2018-04-12 15:25:15 -0700 | [diff] [blame] | 1134 | boot_event_store.AddBootEventWithValue(boot_complete_prefix, uptime_s.count()); |
James Hawkins | ef0a090 | 2017-01-06 14:38:23 -0800 | [diff] [blame] | 1135 | |
| 1136 | RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init"); |
| 1137 | RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init.selinux"); |
| 1138 | RecordInitBootTimeProp(&boot_event_store, "ro.boottime.init.cold_boot_wait"); |
James Hawkins | be46fd1 | 2017-02-02 16:21:25 -0800 | [diff] [blame] | 1139 | |
James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 1140 | const BootloaderTimingMap bootloader_timings = GetBootLoaderTimings(); |
| 1141 | RecordBootloaderTimings(&boot_event_store, bootloader_timings); |
| 1142 | |
Luis Hector Chavez | 03aae15 | 2018-04-12 15:25:15 -0700 | [diff] [blame] | 1143 | auto uptime_ms = std::chrono::duration_cast<std::chrono::milliseconds>(uptime_ns); |
James Hawkins | 1bfcaec | 2017-05-19 14:27:27 -0700 | [diff] [blame] | 1144 | RecordAbsoluteBootTime(&boot_event_store, bootloader_timings, uptime_ms); |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 1145 | } |
| 1146 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 1147 | // Records the boot_reason metric by querying the ro.boot.bootreason system |
| 1148 | // property. |
| 1149 | void RecordBootReason() { |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 1150 | const std::string reason(GetProperty(bootloader_reboot_reason_property)); |
James Hawkins | 25f7122 | 2017-10-10 16:37:05 -0700 | [diff] [blame] | 1151 | |
| 1152 | if (reason.empty()) { |
| 1153 | // Log an empty boot reason value as '<EMPTY>' to ensure the value is intentional |
| 1154 | // (and not corruption anywhere else in the reporting pipeline). |
| 1155 | android::metricslogger::LogMultiAction(android::metricslogger::ACTION_BOOT, |
| 1156 | android::metricslogger::FIELD_PLATFORM_REASON, "<EMPTY>"); |
| 1157 | } else { |
| 1158 | android::metricslogger::LogMultiAction(android::metricslogger::ACTION_BOOT, |
| 1159 | android::metricslogger::FIELD_PLATFORM_REASON, reason); |
| 1160 | } |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 1161 | |
| 1162 | // Log the raw bootloader_boot_reason property value. |
| 1163 | int32_t boot_reason = BootReasonStrToEnum(reason); |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 1164 | BootEventRecordStore boot_event_store; |
| 1165 | boot_event_store.AddBootEventWithValue("boot_reason", boot_reason); |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 1166 | |
| 1167 | // Log the scrubbed system_boot_reason. |
Tej Singh | 993f656 | 2018-02-06 15:57:38 -0800 | [diff] [blame] | 1168 | const std::string system_reason(GetProperty(system_reboot_reason_property)); |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 1169 | int32_t system_boot_reason = BootReasonStrToEnum(system_reason); |
| 1170 | boot_event_store.AddBootEventWithValue("system_boot_reason", system_boot_reason); |
| 1171 | |
Mark Salyzyn | b304f6d | 2017-08-04 13:35:51 -0700 | [diff] [blame] | 1172 | if (reason == "") { |
| 1173 | SetProperty(bootloader_reboot_reason_property, system_reason); |
| 1174 | } |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 1175 | } |
| 1176 | |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 1177 | // Records two metrics related to the user resetting a device: the time at |
| 1178 | // which the device is reset, and the time since the user last reset the |
| 1179 | // device. The former is only set once per-factory reset. |
| 1180 | void RecordFactoryReset() { |
| 1181 | BootEventRecordStore boot_event_store; |
| 1182 | BootEventRecordStore::BootEventRecord record; |
| 1183 | |
| 1184 | time_t current_time_utc = time(nullptr); |
| 1185 | |
James Hawkins | 0660b30 | 2016-03-08 16:18:15 -0800 | [diff] [blame] | 1186 | if (current_time_utc < 0) { |
| 1187 | // UMA does not display negative values in buckets, so convert to positive. |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 1188 | android::metricslogger::LogHistogram("factory_reset_current_time_failure", |
| 1189 | std::abs(current_time_utc)); |
James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 1190 | |
James Hawkins | 9aec926 | 2017-01-31 11:42:24 -0800 | [diff] [blame] | 1191 | // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram |
James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 1192 | // is losing records somehow. |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 1193 | boot_event_store.AddBootEventWithValue("factory_reset_current_time_failure", |
| 1194 | std::abs(current_time_utc)); |
James Hawkins | 0660b30 | 2016-03-08 16:18:15 -0800 | [diff] [blame] | 1195 | return; |
| 1196 | } else { |
James Hawkins | 9aec926 | 2017-01-31 11:42:24 -0800 | [diff] [blame] | 1197 | android::metricslogger::LogHistogram("factory_reset_current_time", current_time_utc); |
James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 1198 | |
James Hawkins | 9aec926 | 2017-01-31 11:42:24 -0800 | [diff] [blame] | 1199 | // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram |
James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 1200 | // is losing records somehow. |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 1201 | boot_event_store.AddBootEventWithValue("factory_reset_current_time", current_time_utc); |
James Hawkins | 0660b30 | 2016-03-08 16:18:15 -0800 | [diff] [blame] | 1202 | } |
| 1203 | |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 1204 | // The factory_reset boot event does not exist after the device is reset, so |
| 1205 | // use this signal to mark the time of the factory reset. |
| 1206 | if (!boot_event_store.GetBootEvent("factory_reset", &record)) { |
| 1207 | boot_event_store.AddBootEventWithValue("factory_reset", current_time_utc); |
James Hawkins | 3bf9b14 | 2016-03-03 14:50:24 -0800 | [diff] [blame] | 1208 | |
| 1209 | // Don't log the time_since_factory_reset until some time has elapsed. |
| 1210 | // The data is not meaningful yet and skews the histogram buckets. |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 1211 | return; |
| 1212 | } |
| 1213 | |
| 1214 | // Calculate and record the difference in time between now and the |
| 1215 | // factory_reset time. |
| 1216 | time_t factory_reset_utc = record.second; |
James Hawkins | 9aec926 | 2017-01-31 11:42:24 -0800 | [diff] [blame] | 1217 | android::metricslogger::LogHistogram("factory_reset_record_value", factory_reset_utc); |
James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 1218 | |
James Hawkins | 9aec926 | 2017-01-31 11:42:24 -0800 | [diff] [blame] | 1219 | // Logging via BootEventRecordStore to see if using android::metricslogger::LogHistogram |
James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 1220 | // is losing records somehow. |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 1221 | boot_event_store.AddBootEventWithValue("factory_reset_record_value", factory_reset_utc); |
James Hawkins | fff95ba | 2016-03-29 16:13:49 -0700 | [diff] [blame] | 1222 | |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 1223 | time_t time_since_factory_reset = difftime(current_time_utc, factory_reset_utc); |
| 1224 | boot_event_store.AddBootEventWithValue("time_since_factory_reset", time_since_factory_reset); |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 1225 | } |
| 1226 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 1227 | } // namespace |
| 1228 | |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 1229 | int main(int argc, char** argv) { |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 1230 | android::base::InitLogging(argv); |
| 1231 | |
| 1232 | const std::string cmd_line = GetCommandLine(argc, argv); |
| 1233 | LOG(INFO) << "Service started: " << cmd_line; |
| 1234 | |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 1235 | int option_index = 0; |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 1236 | static const char value_str[] = "value"; |
Tej Singh | 993f656 | 2018-02-06 15:57:38 -0800 | [diff] [blame] | 1237 | static const char system_boot_reason_str[] = "set_system_boot_reason"; |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 1238 | static const char boot_complete_str[] = "record_boot_complete"; |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 1239 | static const char boot_reason_str[] = "record_boot_reason"; |
James Hawkins | 53684ea | 2016-02-23 16:18:19 -0800 | [diff] [blame] | 1240 | static const char factory_reset_str[] = "record_time_since_factory_reset"; |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 1241 | static const struct option long_options[] = { |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 1242 | // clang-format off |
Tej Singh | 993f656 | 2018-02-06 15:57:38 -0800 | [diff] [blame] | 1243 | { "help", no_argument, NULL, 'h' }, |
| 1244 | { "log", no_argument, NULL, 'l' }, |
| 1245 | { "print", no_argument, NULL, 'p' }, |
| 1246 | { "record", required_argument, NULL, 'r' }, |
| 1247 | { value_str, required_argument, NULL, 0 }, |
| 1248 | { system_boot_reason_str, no_argument, NULL, 0 }, |
| 1249 | { boot_complete_str, no_argument, NULL, 0 }, |
| 1250 | { boot_reason_str, no_argument, NULL, 0 }, |
| 1251 | { factory_reset_str, no_argument, NULL, 0 }, |
| 1252 | { NULL, 0, NULL, 0 } |
Mark Salyzyn | 14b1e6d | 2017-09-18 10:41:14 -0700 | [diff] [blame] | 1253 | // clang-format on |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 1254 | }; |
| 1255 | |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 1256 | std::string boot_event; |
| 1257 | std::string value; |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 1258 | int opt = 0; |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 1259 | while ((opt = getopt_long(argc, argv, "hlpr:", long_options, &option_index)) != -1) { |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 1260 | switch (opt) { |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 1261 | // This case handles long options which have no single-character mapping. |
| 1262 | case 0: { |
| 1263 | const std::string option_name = long_options[option_index].name; |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 1264 | if (option_name == value_str) { |
| 1265 | // |optarg| is an external variable set by getopt representing |
| 1266 | // the option argument. |
| 1267 | value = optarg; |
Tej Singh | 993f656 | 2018-02-06 15:57:38 -0800 | [diff] [blame] | 1268 | } else if (option_name == system_boot_reason_str) { |
| 1269 | SetSystemBootReason(); |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 1270 | } else if (option_name == boot_complete_str) { |
James Hawkins | c08e996 | 2016-03-11 14:59:50 -0800 | [diff] [blame] | 1271 | RecordBootComplete(); |
| 1272 | } else if (option_name == boot_reason_str) { |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 1273 | RecordBootReason(); |
James Hawkins | 500d715 | 2016-02-16 15:05:54 -0800 | [diff] [blame] | 1274 | } else if (option_name == factory_reset_str) { |
| 1275 | RecordFactoryReset(); |
James Hawkins | a4a1a4a | 2016-02-09 15:32:38 -0800 | [diff] [blame] | 1276 | } else { |
| 1277 | LOG(ERROR) << "Invalid option: " << option_name; |
| 1278 | } |
| 1279 | break; |
| 1280 | } |
| 1281 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 1282 | case 'h': { |
| 1283 | ShowHelp(argv[0]); |
| 1284 | break; |
| 1285 | } |
| 1286 | |
| 1287 | case 'l': { |
| 1288 | LogBootEvents(); |
| 1289 | break; |
| 1290 | } |
| 1291 | |
| 1292 | case 'p': { |
| 1293 | PrintBootEvents(); |
| 1294 | break; |
| 1295 | } |
| 1296 | |
| 1297 | case 'r': { |
| 1298 | // |optarg| is an external variable set by getopt representing |
| 1299 | // the option argument. |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 1300 | boot_event = optarg; |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 1301 | break; |
| 1302 | } |
| 1303 | |
| 1304 | default: { |
| 1305 | DCHECK_EQ(opt, '?'); |
| 1306 | |
| 1307 | // |optopt| is an external variable set by getopt representing |
| 1308 | // the value of the invalid option. |
| 1309 | LOG(ERROR) << "Invalid option: " << optopt; |
| 1310 | ShowHelp(argv[0]); |
| 1311 | return EXIT_FAILURE; |
| 1312 | } |
| 1313 | } |
| 1314 | } |
| 1315 | |
James Hawkins | c627558 | 2016-03-22 10:47:44 -0700 | [diff] [blame] | 1316 | if (!boot_event.empty()) { |
| 1317 | RecordBootEventFromCommandLine(boot_event, value); |
| 1318 | } |
| 1319 | |
James Hawkins | abd73e6 | 2016-01-19 15:10:38 -0800 | [diff] [blame] | 1320 | return 0; |
| 1321 | } |