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