Ben Tyner | b1ebfcb | 2020-05-08 18:52:48 -0500 | [diff] [blame] | 1 | #include <unistd.h> |
| 2 | |
Ben Tyner | 5c5db65 | 2021-02-22 18:22:35 -0600 | [diff] [blame] | 3 | #include <attn/attn_common.hpp> |
Ben Tyner | 188f109 | 2021-02-01 09:33:06 -0600 | [diff] [blame] | 4 | #include <attn/attn_dbus.hpp> |
Ben Tyner | 7029e52 | 2021-08-09 19:18:24 -0500 | [diff] [blame] | 5 | #include <attn/attn_dump.hpp> |
Ben Tyner | b797b3e | 2020-06-29 10:12:05 -0500 | [diff] [blame] | 6 | #include <attn/attn_logging.hpp> |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 7 | #include <attn/pel/pel_minimal.hpp> |
Ben Tyner | b1ebfcb | 2020-05-08 18:52:48 -0500 | [diff] [blame] | 8 | #include <phosphor-logging/log.hpp> |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 9 | |
Ben Tyner | b1ebfcb | 2020-05-08 18:52:48 -0500 | [diff] [blame] | 10 | namespace attn |
| 11 | { |
| 12 | |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 13 | /** @brief Journal entry of type INFO using phosphor logging */ |
Ben Tyner | b1ebfcb | 2020-05-08 18:52:48 -0500 | [diff] [blame] | 14 | template <> |
| 15 | void trace<INFO>(const char* i_message) |
| 16 | { |
| 17 | phosphor::logging::log<phosphor::logging::level::INFO>(i_message); |
| 18 | } |
| 19 | |
Ben Tyner | d700609 | 2021-02-05 14:55:52 -0600 | [diff] [blame] | 20 | template <> |
| 21 | void trace<ERROR>(const char* i_message) |
| 22 | { |
| 23 | phosphor::logging::log<phosphor::logging::level::ERR>(i_message); |
| 24 | } |
| 25 | |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 26 | /** @brief Tuple containing information about ffdc files */ |
| 27 | using FFDCTuple = |
| 28 | std::tuple<util::FFDCFormat, uint8_t, uint8_t, sdbusplus::message::unix_fd>; |
| 29 | |
| 30 | /** @brief Gather messages from the journal */ |
| 31 | std::vector<std::string> sdjGetMessages(const std::string& field, |
| 32 | const std::string& fieldValue, |
| 33 | unsigned int max); |
| 34 | |
| 35 | /** |
| 36 | * Create FFDCTuple objects corresponding to the specified FFDC files. |
| 37 | * |
| 38 | * The D-Bus method to create an error log requires a vector of tuples to |
| 39 | * pass in the FFDC file information. |
| 40 | * |
| 41 | * @param files - FFDC files |
| 42 | * @return vector of FFDCTuple objects |
| 43 | */ |
| 44 | std::vector<FFDCTuple> |
| 45 | createFFDCTuples(const std::vector<util::FFDCFile>& files) |
| 46 | { |
| 47 | std::vector<FFDCTuple> ffdcTuples{}; |
Zane Shelley | a79f6c8 | 2021-01-12 16:38:49 -0600 | [diff] [blame] | 48 | util::transformFFDC(files, ffdcTuples); |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 49 | |
| 50 | return ffdcTuples; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @brief Create an FFDCFile object containing raw data |
| 55 | * |
| 56 | * Throws an exception if an error occurs. |
| 57 | * |
| 58 | * @param i_buffer - raw data to add to ffdc faw data file |
| 59 | * @param i_size - size of the raw data |
| 60 | * @return FFDCFile object |
| 61 | */ |
| 62 | util::FFDCFile createFFDCRawFile(void* i_buffer, size_t i_size) |
| 63 | { |
| 64 | util::FFDCFile file{util::FFDCFormat::Custom}; |
| 65 | |
| 66 | // Write buffer to file and then reset file description file offset |
Ben Tyner | d700609 | 2021-02-05 14:55:52 -0600 | [diff] [blame] | 67 | int fd = file.getFileDescriptor(); |
| 68 | size_t numBytes = write(fd, static_cast<char*>(i_buffer), i_size); |
| 69 | if (i_size != numBytes) |
| 70 | { |
| 71 | std::stringstream traceMsg; |
| 72 | traceMsg << file.getPath().c_str() << " only " << (int)numBytes |
| 73 | << " of " << (int)i_size << " bytes written"; |
| 74 | auto strobj = traceMsg.str(); |
| 75 | trace<level::ERROR>(strobj.c_str()); |
| 76 | } |
| 77 | |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 78 | lseek(fd, 0, SEEK_SET); |
| 79 | |
| 80 | return file; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * @brief Create an FFDCFile object containing the specified lines of text data |
| 85 | * |
| 86 | * Throws an exception if an error occurs. |
| 87 | * |
| 88 | * @param lines - lines of text data to write to file |
| 89 | * @return FFDCFile object |
| 90 | */ |
| 91 | util::FFDCFile createFFDCTraceFile(const std::vector<std::string>& lines) |
| 92 | { |
| 93 | // Create FFDC file of type Text |
| 94 | util::FFDCFile file{util::FFDCFormat::Text}; |
| 95 | int fd = file.getFileDescriptor(); |
| 96 | |
| 97 | // Write FFDC lines to file |
| 98 | std::string buffer; |
| 99 | for (const std::string& line : lines) |
| 100 | { |
| 101 | // Copy line to buffer. Add newline if necessary. |
| 102 | buffer = line; |
| 103 | if (line.empty() || (line.back() != '\n')) |
| 104 | { |
| 105 | buffer += '\n'; |
| 106 | } |
| 107 | |
| 108 | // write buffer to file |
Ben Tyner | d700609 | 2021-02-05 14:55:52 -0600 | [diff] [blame] | 109 | size_t numBytes = write(fd, buffer.c_str(), buffer.size()); |
| 110 | if (buffer.size() != numBytes) |
| 111 | { |
| 112 | std::stringstream traceMsg; |
| 113 | traceMsg << file.getPath().c_str() << " only " << (int)numBytes |
| 114 | << " of " << (int)buffer.size() << " bytes written"; |
| 115 | auto strobj = traceMsg.str(); |
| 116 | trace<level::ERROR>(strobj.c_str()); |
| 117 | } |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | // Seek to beginning of file so error logging system can read data |
| 121 | lseek(fd, 0, SEEK_SET); |
| 122 | |
| 123 | return file; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Create FDDC files from journal messages of relevant executables |
| 128 | * |
| 129 | * Parse the system journal looking for log entries created by the executables |
| 130 | * of interest for logging. For each of these entries create a ffdc trace file |
| 131 | * that will be used to create ffdc log entries. These files will be pushed |
| 132 | * onto the stack of ffdc files. |
| 133 | * |
| 134 | * @param i_files - vector of ffdc files that will become log entries |
| 135 | */ |
| 136 | void createFFDCTraceFiles(std::vector<util::FFDCFile>& i_files) |
| 137 | { |
| 138 | // Executables of interest |
| 139 | std::vector<std::string> executables{"openpower-hw-diags"}; |
| 140 | |
| 141 | for (const std::string& executable : executables) |
| 142 | { |
| 143 | try |
| 144 | { |
| 145 | // get journal messages |
| 146 | std::vector<std::string> messages = |
| 147 | sdjGetMessages("SYSLOG_IDENTIFIER", executable, 30); |
| 148 | |
| 149 | // Create FFDC file containing the journal messages |
| 150 | if (!messages.empty()) |
| 151 | { |
| 152 | i_files.emplace_back(createFFDCTraceFile(messages)); |
| 153 | } |
| 154 | } |
| 155 | catch (const std::exception& e) |
| 156 | { |
Ben Tyner | 6764d70 | 2021-02-12 09:17:23 -0600 | [diff] [blame] | 157 | trace<level::INFO>("createFFDCTraceFiles exception"); |
| 158 | std::string traceMsg = std::string(e.what(), maxTraceLen); |
| 159 | trace<level::INFO>(traceMsg.c_str()); |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Create FFDCFile objects containing debug data to store in the error log. |
| 166 | * |
| 167 | * If an error occurs, the error is written to the journal but an exception |
| 168 | * is not thrown. |
| 169 | * |
| 170 | * @param i_buffer - raw data (if creating raw dump ffdc entry in log) |
| 171 | * @return vector of FFDCFile objects |
| 172 | */ |
| 173 | std::vector<util::FFDCFile> createFFDCFiles(char* i_buffer = nullptr, |
| 174 | size_t i_size = 0) |
| 175 | { |
| 176 | std::vector<util::FFDCFile> files{}; |
| 177 | |
| 178 | // Create raw dump file |
| 179 | if ((nullptr != i_buffer) && (0 != i_size)) |
| 180 | { |
| 181 | files.emplace_back(createFFDCRawFile(i_buffer, i_size)); |
| 182 | } |
| 183 | |
| 184 | // Create trace dump file |
| 185 | createFFDCTraceFiles(files); |
| 186 | |
| 187 | return files; |
| 188 | } |
| 189 | |
| 190 | /** |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 191 | * Create a PEL from an existing PEL |
| 192 | * |
| 193 | * Create a new PEL based on the specified raw PEL and submit the new PEL |
| 194 | * to the backend logging code as a raw PEL. Note that additional data map |
| 195 | * here contains data to be committed to the PEL and it can also be used to |
| 196 | * create the PEL as it contains needed information. |
| 197 | * |
| 198 | * @param i_buffer - buffer containing a raw PEL |
| 199 | * @param i_additional - additional data to be added to the new PEL |
| 200 | */ |
| 201 | void createPelCustom(std::vector<uint8_t>& i_rawPel, |
| 202 | std::map<std::string, std::string> i_additional) |
| 203 | { |
| 204 | // create PEL object from buffer |
| 205 | auto tiPel = std::make_unique<pel::PelMinimal>(i_rawPel); |
| 206 | |
| 207 | // The additional data contains the TI info as well as the value for the |
| 208 | // subystem that provided the TI info. Get the subystem from additional |
| 209 | // data and then populate the prmary SRC and SRC words for the custom PEL |
| 210 | // based on the sybsystem's TI info. |
| 211 | uint8_t subsystem = std::stoi(i_additional["Subsystem"]); |
| 212 | tiPel->setSubsystem(subsystem); |
| 213 | |
| 214 | if (static_cast<uint8_t>(pel::SubsystemID::hypervisor) == subsystem) |
| 215 | { |
| 216 | // populate hypervisor SRC words |
| 217 | tiPel->setSrcWords(std::array<uint32_t, pel::numSrcWords>{ |
| 218 | (uint32_t)std::stoul(i_additional["0x10 SRC Word 12"], 0, 16), |
| 219 | (uint32_t)std::stoul(i_additional["0x14 SRC Word 13"], 0, 16), |
| 220 | (uint32_t)std::stoul(i_additional["0x18 SRC Word 14"], 0, 16), |
| 221 | (uint32_t)std::stoul(i_additional["0x1c SRC Word 15"], 0, 16), |
| 222 | (uint32_t)std::stoul(i_additional["0x20 SRC Word 16"], 0, 16), |
| 223 | (uint32_t)std::stoul(i_additional["0x24 SRC Word 17"], 0, 16), |
| 224 | (uint32_t)std::stoul(i_additional["0x28 SRC Word 18"], 0, 16), |
| 225 | (uint32_t)std::stoul(i_additional["0x2c SRC Word 19"], 0, 16)}); |
| 226 | |
Ben Tyner | 9d4f91c | 2021-02-09 08:27:58 -0600 | [diff] [blame] | 227 | // Populate phyp primary SRC |
| 228 | |
| 229 | // char array for raw pel src |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 230 | std::array<char, pel::asciiStringSize> srcChars{'0'}; |
Ben Tyner | 9d4f91c | 2021-02-09 08:27:58 -0600 | [diff] [blame] | 231 | |
| 232 | // src from TI info |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 233 | std::string srcString = i_additional["SrcAscii"]; |
Ben Tyner | 9d4f91c | 2021-02-09 08:27:58 -0600 | [diff] [blame] | 234 | |
| 235 | // copy from string to char array |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 236 | srcString.copy(srcChars.data(), |
| 237 | std::min(srcString.size(), pel::asciiStringSize), 0); |
Ben Tyner | 9d4f91c | 2021-02-09 08:27:58 -0600 | [diff] [blame] | 238 | |
| 239 | tiPel->setAsciiString(srcChars); // pel object src is char array |
Ben Tyner | feeea83 | 2021-04-06 10:08:11 -0500 | [diff] [blame] | 240 | |
| 241 | // set symptom-id |
| 242 | auto symptomId = (i_additional["SrcAscii"].substr(0, 8) + '_'); |
| 243 | |
| 244 | symptomId += (i_additional["0x10 SRC Word 12"]); |
| 245 | symptomId += (i_additional["0x14 SRC Word 13"] + '_'); |
| 246 | symptomId += (i_additional["0x18 SRC Word 14"]); |
| 247 | symptomId += (i_additional["0x1c SRC Word 15"] + '_'); |
| 248 | symptomId += (i_additional["0x20 SRC Word 16"]); |
| 249 | symptomId += (i_additional["0x24 SRC Word 17"] + '_'); |
| 250 | symptomId += (i_additional["0x28 SRC Word 18"]); |
| 251 | symptomId += (i_additional["0x2c SRC Word 19"]); |
| 252 | |
| 253 | // setSymptomId will take care of required null-terminate and padding |
| 254 | tiPel->setSymptomId(symptomId); |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 255 | } |
| 256 | else |
| 257 | { |
| 258 | // Populate hostboot SRC words - note HB word 0 from the shared info |
| 259 | // data (additional data "0x10 HB Word") is reflected in the PEL as |
| 260 | // "reason code" so we zero it here. Also note that the first word |
| 261 | // in this group of words starts at word 0 and word 1 does not exits. |
| 262 | tiPel->setSrcWords(std::array<uint32_t, pel::numSrcWords>{ |
| 263 | (uint32_t)0x00000000, |
| 264 | (uint32_t)std::stoul(i_additional["0x14 HB Word 2"], 0, 16), |
| 265 | (uint32_t)std::stoul(i_additional["0x18 HB Word 3"], 0, 16), |
| 266 | (uint32_t)std::stoul(i_additional["0x1c HB Word 4"], 0, 16), |
| 267 | (uint32_t)std::stoul(i_additional["0x20 HB Word 5"], 0, 16), |
| 268 | (uint32_t)std::stoul(i_additional["0x24 HB Word 6"], 0, 16), |
| 269 | (uint32_t)std::stoul(i_additional["0x28 HB Word 7"], 0, 16), |
| 270 | (uint32_t)std::stoul(i_additional["0x2c HB Word 8"], 0, 16)}); |
| 271 | |
Ben Tyner | 9d4f91c | 2021-02-09 08:27:58 -0600 | [diff] [blame] | 272 | // Populate hostboot primary SRC |
| 273 | |
| 274 | // char array for raw pel src |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 275 | std::array<char, pel::asciiStringSize> srcChars{'0'}; |
Ben Tyner | 9d4f91c | 2021-02-09 08:27:58 -0600 | [diff] [blame] | 276 | |
| 277 | // src from TI info |
| 278 | std::string srcString = i_additional["SrcAscii"]; |
| 279 | |
| 280 | // copy from string to char array |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 281 | srcString.copy(srcChars.data(), |
| 282 | std::min(srcString.size(), pel::asciiStringSize), 0); |
Ben Tyner | 9d4f91c | 2021-02-09 08:27:58 -0600 | [diff] [blame] | 283 | |
| 284 | tiPel->setAsciiString(srcChars); // pel object src is char array |
Ben Tyner | feeea83 | 2021-04-06 10:08:11 -0500 | [diff] [blame] | 285 | |
| 286 | // set symptom-id |
| 287 | auto symptomId = (i_additional["SrcAscii"].substr(0, 8) + '_'); |
| 288 | |
| 289 | symptomId += (i_additional["0x10 HB Word 0"]); // note: word 1 |
| 290 | symptomId += (i_additional["0x14 HB Word 2"] + '_'); // does not exist |
| 291 | symptomId += (i_additional["0x18 HB Word 3"]); |
| 292 | symptomId += (i_additional["0x1c HB Word 4"] + '_'); |
| 293 | symptomId += (i_additional["0x20 HB Word 5"]); |
| 294 | symptomId += (i_additional["0x24 HB Word 6"] + '_'); |
| 295 | symptomId += (i_additional["0x28 HB Word 7"]); |
| 296 | symptomId += (i_additional["0x2c HB Word 8"]); |
| 297 | |
| 298 | // setSymptomId will take care of required null-terminate and padding |
| 299 | tiPel->setSymptomId(symptomId); |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | // set severity, event type and action flags |
| 303 | tiPel->setSeverity(static_cast<uint8_t>(pel::Severity::termination)); |
| 304 | tiPel->setType(static_cast<uint8_t>(pel::EventType::na)); |
| 305 | tiPel->setAction(static_cast<uint16_t>(pel::ActionFlags::service | |
| 306 | pel::ActionFlags::report | |
| 307 | pel::ActionFlags::call)); |
| 308 | |
| 309 | // The raw PEL that we used as the basis for this custom PEL contains the |
| 310 | // attention handler trace data and does not needed to be in this PEL so |
| 311 | // we remove it here. |
| 312 | tiPel->setSectionCount(tiPel->getSectionCount() - 1); |
| 313 | |
| 314 | // Update the raw PEL with the new custom PEL data |
| 315 | tiPel->raw(i_rawPel); |
| 316 | |
| 317 | // create PEL from raw data |
| 318 | createPelRaw(i_rawPel); |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * Log an event handled by the attention handler |
| 323 | * |
| 324 | * Basic (non TI) events will generate a standard message-registry based PEL |
| 325 | * |
| 326 | * TI events will create two PEL's. One PEL will be informational and will |
| 327 | * contain trace information relevent to attention handler. The second PEL |
| 328 | * will be specific to the TI type (including the primary SRC) and will be |
| 329 | * based off of the TI information provided to the attention handler through |
| 330 | * shared TI info data area. |
| 331 | * |
| 332 | * @param i_event - The event type |
| 333 | * @param i_additional - Additional PEL data |
| 334 | * @param i_ffdc - FFDC PEL data |
Ben Tyner | 7f6ce6a | 2021-08-17 19:40:00 -0500 | [diff] [blame] | 335 | * @return Event log Id (0 if no event log generated) |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 336 | */ |
Ben Tyner | 7f6ce6a | 2021-08-17 19:40:00 -0500 | [diff] [blame] | 337 | uint32_t event(EventType i_event, |
| 338 | std::map<std::string, std::string>& i_additional, |
| 339 | const std::vector<util::FFDCFile>& i_ffdc) |
Ben Tyner | b1ebfcb | 2020-05-08 18:52:48 -0500 | [diff] [blame] | 340 | { |
Ben Tyner | 7f6ce6a | 2021-08-17 19:40:00 -0500 | [diff] [blame] | 341 | uint32_t pelId = 0; // assume no event log generated |
| 342 | |
Ben Tyner | b1ebfcb | 2020-05-08 18:52:48 -0500 | [diff] [blame] | 343 | bool eventValid = false; // assume no event created |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 344 | bool tiEvent = false; // assume not a terminate event |
Ben Tyner | b1ebfcb | 2020-05-08 18:52:48 -0500 | [diff] [blame] | 345 | |
| 346 | std::string eventName; |
| 347 | |
| 348 | switch (i_event) |
| 349 | { |
| 350 | case EventType::Checkstop: |
| 351 | eventName = "org.open_power.HwDiags.Error.Checkstop"; |
| 352 | eventValid = true; |
| 353 | break; |
| 354 | case EventType::Terminate: |
| 355 | eventName = "org.open_power.Attn.Error.Terminate"; |
| 356 | eventValid = true; |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 357 | tiEvent = true; |
Ben Tyner | b1ebfcb | 2020-05-08 18:52:48 -0500 | [diff] [blame] | 358 | break; |
| 359 | case EventType::Vital: |
| 360 | eventName = "org.open_power.Attn.Error.Vital"; |
| 361 | eventValid = true; |
| 362 | break; |
| 363 | case EventType::HwDiagsFail: |
Ben Tyner | b1ebfcb | 2020-05-08 18:52:48 -0500 | [diff] [blame] | 364 | case EventType::AttentionFail: |
| 365 | eventName = "org.open_power.Attn.Error.Fail"; |
| 366 | eventValid = true; |
| 367 | break; |
| 368 | default: |
| 369 | eventValid = false; |
| 370 | break; |
| 371 | } |
| 372 | |
| 373 | if (true == eventValid) |
| 374 | { |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 375 | // Create PEL with additional data and FFDC data. The newly created |
| 376 | // PEL's platform log-id will be returned. |
Ben Tyner | 7f6ce6a | 2021-08-17 19:40:00 -0500 | [diff] [blame] | 377 | pelId = createPel(eventName, i_additional, createFFDCTuples(i_ffdc)); |
Ben Tyner | b1ebfcb | 2020-05-08 18:52:48 -0500 | [diff] [blame] | 378 | |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 379 | // If this is a TI event we will create an additional PEL that is |
| 380 | // specific to the subsystem that generated the TI. |
Ben Tyner | 29651ef | 2021-02-08 10:51:03 -0600 | [diff] [blame] | 381 | if ((true == tiEvent) && (0 != pelId)) |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 382 | { |
| 383 | // get file descriptor and size of information PEL |
Ben Tyner | 188f109 | 2021-02-01 09:33:06 -0600 | [diff] [blame] | 384 | int pelFd = getPel(pelId); |
Ben Tyner | 1b1915e | 2020-10-23 15:13:38 -0500 | [diff] [blame] | 385 | |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 386 | // if PEL found, read into buffer |
| 387 | if (-1 != pelFd) |
| 388 | { |
| 389 | auto pelSize = lseek(pelFd, 0, SEEK_END); |
| 390 | lseek(pelFd, 0, SEEK_SET); |
Ben Tyner | 1b1915e | 2020-10-23 15:13:38 -0500 | [diff] [blame] | 391 | |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 392 | // read information PEL into buffer |
| 393 | std::vector<uint8_t> buffer(pelSize); |
Ben Tyner | d700609 | 2021-02-05 14:55:52 -0600 | [diff] [blame] | 394 | size_t numBytes = read(pelFd, buffer.data(), buffer.size()); |
| 395 | if (buffer.size() != numBytes) |
| 396 | { |
| 397 | std::stringstream traceMsg; |
| 398 | traceMsg << "Error reading event log: " << (int)numBytes |
| 399 | << " of " << (int)buffer.size() << " bytes read"; |
| 400 | auto strobj = traceMsg.str(); |
| 401 | trace<level::ERROR>(strobj.c_str()); |
| 402 | } |
| 403 | else |
| 404 | { |
| 405 | // create PEL from buffer |
| 406 | createPelCustom(buffer, i_additional); |
| 407 | } |
Ben Tyner | b1ebfcb | 2020-05-08 18:52:48 -0500 | [diff] [blame] | 408 | |
Ben Tyner | d700609 | 2021-02-05 14:55:52 -0600 | [diff] [blame] | 409 | close(pelFd); |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 410 | } |
Ben Tyner | 5c5db65 | 2021-02-22 18:22:35 -0600 | [diff] [blame] | 411 | |
Ben Tyner | 6bc43c9 | 2021-05-27 15:08:02 -0500 | [diff] [blame] | 412 | uint8_t subsystem = std::stoi(i_additional["Subsystem"]); |
Ben Tyner | 5c5db65 | 2021-02-22 18:22:35 -0600 | [diff] [blame] | 413 | |
Ben Tyner | 6bc43c9 | 2021-05-27 15:08:02 -0500 | [diff] [blame] | 414 | // If not hypervisor TI |
| 415 | if (static_cast<uint8_t>(pel::SubsystemID::hypervisor) != subsystem) |
Ben Tyner | 5c5db65 | 2021-02-22 18:22:35 -0600 | [diff] [blame] | 416 | { |
Ben Tyner | 6bc43c9 | 2021-05-27 15:08:02 -0500 | [diff] [blame] | 417 | // Request a dump and transition the host |
| 418 | if ("true" == i_additional["Dump"]) |
| 419 | { |
| 420 | // will not return until dump is complete |
Ben Tyner | 7029e52 | 2021-08-09 19:18:24 -0500 | [diff] [blame] | 421 | requestDump(DumpParameters{pelId, 0, DumpType::Hostboot}); |
Ben Tyner | 6bc43c9 | 2021-05-27 15:08:02 -0500 | [diff] [blame] | 422 | } |
Ben Tyner | 5c5db65 | 2021-02-22 18:22:35 -0600 | [diff] [blame] | 423 | } |
| 424 | } |
Ben Tyner | b1ebfcb | 2020-05-08 18:52:48 -0500 | [diff] [blame] | 425 | } |
Ben Tyner | 7f6ce6a | 2021-08-17 19:40:00 -0500 | [diff] [blame] | 426 | return pelId; |
Ben Tyner | b1ebfcb | 2020-05-08 18:52:48 -0500 | [diff] [blame] | 427 | } |
| 428 | |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 429 | /** |
| 430 | * Commit special attention TI event to log |
| 431 | * |
| 432 | * Create a event log with provided additional information and standard |
| 433 | * FFDC data plus TI FFDC data |
| 434 | * |
| 435 | * @param i_additional - Additional log data |
| 436 | * @param i_ti_InfoData - TI FFDC data |
| 437 | */ |
| 438 | void eventTerminate(std::map<std::string, std::string> i_additionalData, |
| 439 | char* i_tiInfoData) |
Ben Tyner | b1ebfcb | 2020-05-08 18:52:48 -0500 | [diff] [blame] | 440 | { |
Ben Tyner | b6401ed | 2021-01-11 09:08:07 -0600 | [diff] [blame] | 441 | |
Ben Tyner | 29651ef | 2021-02-08 10:51:03 -0600 | [diff] [blame] | 442 | uint32_t tiInfoSize = 0; // assume TI info was not available |
Ben Tyner | b6401ed | 2021-01-11 09:08:07 -0600 | [diff] [blame] | 443 | |
Ben Tyner | 29651ef | 2021-02-08 10:51:03 -0600 | [diff] [blame] | 444 | if (nullptr != i_tiInfoData) |
Ben Tyner | b6401ed | 2021-01-11 09:08:07 -0600 | [diff] [blame] | 445 | { |
Ben Tyner | 29651ef | 2021-02-08 10:51:03 -0600 | [diff] [blame] | 446 | tiInfoSize = 56; // assume not hypervisor TI |
Ben Tyner | b6401ed | 2021-01-11 09:08:07 -0600 | [diff] [blame] | 447 | |
Ben Tyner | 29651ef | 2021-02-08 10:51:03 -0600 | [diff] [blame] | 448 | uint8_t subsystem = std::stoi(i_additionalData["Subsystem"]); |
| 449 | |
| 450 | // If hypervisor |
| 451 | if (static_cast<uint8_t>(pel::SubsystemID::hypervisor) == subsystem) |
Ben Tyner | b6401ed | 2021-01-11 09:08:07 -0600 | [diff] [blame] | 452 | { |
Ben Tyner | 29651ef | 2021-02-08 10:51:03 -0600 | [diff] [blame] | 453 | tiInfoSize = 1024; // assume hypervisor max |
Ben Tyner | b6401ed | 2021-01-11 09:08:07 -0600 | [diff] [blame] | 454 | |
Ben Tyner | 29651ef | 2021-02-08 10:51:03 -0600 | [diff] [blame] | 455 | // hypervisor may just want some of the data |
| 456 | if (0 == (*(i_tiInfoData + 0x09) & 0x01)) |
| 457 | { |
| 458 | uint32_t* additionalLength = (uint32_t*)(i_tiInfoData + 0x50); |
| 459 | uint32_t tiAdditional = be32toh(*additionalLength); |
| 460 | tiInfoSize = std::min(tiInfoSize, (84 + tiAdditional)); |
| 461 | } |
Ben Tyner | b6401ed | 2021-01-11 09:08:07 -0600 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | |
| 465 | std::string traceMsg = "TI info size = " + std::to_string(tiInfoSize); |
| 466 | trace<level::INFO>(traceMsg.c_str()); |
| 467 | |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 468 | event(EventType::Terminate, i_additionalData, |
Ben Tyner | b6401ed | 2021-01-11 09:08:07 -0600 | [diff] [blame] | 469 | createFFDCFiles(i_tiInfoData, tiInfoSize)); |
Ben Tyner | b1ebfcb | 2020-05-08 18:52:48 -0500 | [diff] [blame] | 470 | } |
| 471 | |
Ben Tyner | 7f6ce6a | 2021-08-17 19:40:00 -0500 | [diff] [blame] | 472 | /** @brief Commit SBE vital event to log, returns event log ID */ |
| 473 | uint32_t eventVital() |
Ben Tyner | b1ebfcb | 2020-05-08 18:52:48 -0500 | [diff] [blame] | 474 | { |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 475 | // Additional data for log |
Ben Tyner | b1ebfcb | 2020-05-08 18:52:48 -0500 | [diff] [blame] | 476 | std::map<std::string, std::string> additionalData; |
| 477 | |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 478 | // Create log event with additional data and FFDC data |
Ben Tyner | 7f6ce6a | 2021-08-17 19:40:00 -0500 | [diff] [blame] | 479 | return event(EventType::Vital, additionalData, createFFDCFiles(nullptr, 0)); |
Ben Tyner | b1ebfcb | 2020-05-08 18:52:48 -0500 | [diff] [blame] | 480 | } |
| 481 | |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 482 | /** |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 483 | * Commit attention handler failure event to log |
| 484 | * |
| 485 | * Create an event log containing the specified error code. |
| 486 | * |
| 487 | * @param i_error - Error code |
| 488 | */ |
| 489 | void eventAttentionFail(int i_error) |
| 490 | { |
| 491 | // Additional data for log |
| 492 | std::map<std::string, std::string> additionalData; |
| 493 | additionalData["ERROR_CODE"] = std::to_string(i_error); |
| 494 | |
| 495 | // Create log event with additional data and FFDC data |
| 496 | event(EventType::AttentionFail, additionalData, |
| 497 | createFFDCFiles(nullptr, 0)); |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * Parse systemd journal message field |
| 502 | * |
| 503 | * Parse the journal looking for the specified field and return the journal |
| 504 | * data for that field. |
| 505 | * |
| 506 | * @param journal - The journal to parse |
| 507 | * @param field - Field containing the data to retrieve |
| 508 | * @return Data for the speciefied field |
| 509 | */ |
Ben Tyner | 1b1915e | 2020-10-23 15:13:38 -0500 | [diff] [blame] | 510 | std::string sdjGetFieldValue(sd_journal* journal, const char* field) |
| 511 | { |
| 512 | const char* data{nullptr}; |
| 513 | size_t length{0}; |
Ben Tyner | 1b1915e | 2020-10-23 15:13:38 -0500 | [diff] [blame] | 514 | |
| 515 | // get field value |
| 516 | if (0 == sd_journal_get_data(journal, field, (const void**)&data, &length)) |
| 517 | { |
Zane Shelley | 9fb657f | 2021-01-12 15:30:58 -0600 | [diff] [blame] | 518 | size_t prefix{0}; |
| 519 | |
Ben Tyner | 1b1915e | 2020-10-23 15:13:38 -0500 | [diff] [blame] | 520 | // The data returned by sd_journal_get_data will be prefixed with the |
| 521 | // field name and "=" |
| 522 | const void* eq = memchr(data, '=', length); |
| 523 | if (nullptr != eq) |
| 524 | { |
| 525 | // get just data following the "=" |
| 526 | prefix = (const char*)eq - data + 1; |
| 527 | } |
| 528 | else |
| 529 | { |
| 530 | // all the data (should not happen) |
| 531 | prefix = 0; |
| 532 | std::string value{}; // empty string |
| 533 | } |
| 534 | |
| 535 | return std::string{data + prefix, length - prefix}; |
| 536 | } |
| 537 | else |
| 538 | { |
| 539 | return std::string{}; // empty string |
| 540 | } |
| 541 | } |
| 542 | |
Ben Tyner | f5210bb | 2021-01-05 12:58:10 -0600 | [diff] [blame] | 543 | /** |
| 544 | * Gather messages from the journal |
| 545 | * |
| 546 | * Fetch journal entry data for all entries with the specified field equal to |
| 547 | * the specified value. |
| 548 | * |
| 549 | * @param field - Field to search on |
| 550 | * @param fieldValue - Value to search for |
| 551 | * @param max - Maximum number of messages fetch |
| 552 | * @return Vector of journal entry data |
| 553 | */ |
Ben Tyner | 1b1915e | 2020-10-23 15:13:38 -0500 | [diff] [blame] | 554 | std::vector<std::string> sdjGetMessages(const std::string& field, |
| 555 | const std::string& fieldValue, |
| 556 | unsigned int max) |
| 557 | { |
| 558 | sd_journal* journal; |
| 559 | std::vector<std::string> messages; |
| 560 | |
| 561 | if (0 == sd_journal_open(&journal, SD_JOURNAL_LOCAL_ONLY)) |
| 562 | { |
| 563 | SD_JOURNAL_FOREACH_BACKWARDS(journal) |
| 564 | { |
| 565 | // Get input field |
| 566 | std::string value = sdjGetFieldValue(journal, field.c_str()); |
| 567 | |
| 568 | // Compare field value and read data |
| 569 | if (value == fieldValue) |
| 570 | { |
| 571 | // Get SYSLOG_IDENTIFIER field (process that logged message) |
| 572 | std::string syslog = |
| 573 | sdjGetFieldValue(journal, "SYSLOG_IDENTIFIER"); |
| 574 | |
| 575 | // Get _PID field |
| 576 | std::string pid = sdjGetFieldValue(journal, "_PID"); |
| 577 | |
| 578 | // Get MESSAGE field |
| 579 | std::string message = sdjGetFieldValue(journal, "MESSAGE"); |
| 580 | |
| 581 | // Get timestamp |
| 582 | uint64_t usec{0}; |
| 583 | if (0 == sd_journal_get_realtime_usec(journal, &usec)) |
| 584 | { |
| 585 | |
| 586 | // Convert realtime microseconds to date format |
| 587 | char dateBuffer[80]; |
| 588 | std::string date; |
| 589 | std::time_t timeInSecs = usec / 1000000; |
| 590 | strftime(dateBuffer, sizeof(dateBuffer), "%b %d %H:%M:%S", |
| 591 | std::localtime(&timeInSecs)); |
| 592 | date = dateBuffer; |
| 593 | |
| 594 | // Store value to messages |
| 595 | value = date + " " + syslog + "[" + pid + "]: " + message; |
| 596 | messages.insert(messages.begin(), value); |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | // limit maximum number of messages |
| 601 | if (messages.size() >= max) |
| 602 | { |
| 603 | break; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | sd_journal_close(journal); // close journal when done |
| 608 | } |
| 609 | |
| 610 | return messages; |
| 611 | } |
| 612 | |
Ben Tyner | b1ebfcb | 2020-05-08 18:52:48 -0500 | [diff] [blame] | 613 | } // namespace attn |