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