Matt Spinler | 711d51d | 2019-11-06 09:36:51 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2019 IBM Corporation |
| 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 | */ |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 16 | #include "pel.hpp" |
| 17 | |
| 18 | #include "bcd_time.hpp" |
Matt Spinler | 386a61e | 2020-08-13 15:51:12 -0500 | [diff] [blame] | 19 | #include "extended_user_data.hpp" |
Matt Spinler | c63e2e8 | 2019-12-02 15:50:12 -0600 | [diff] [blame] | 20 | #include "extended_user_header.hpp" |
Matt Spinler | aa65947 | 2019-10-23 09:26:48 -0500 | [diff] [blame] | 21 | #include "failing_mtms.hpp" |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 22 | #include "json_utils.hpp" |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 23 | #include "log_id.hpp" |
Matt Spinler | f1e85e2 | 2019-11-01 11:31:31 -0500 | [diff] [blame] | 24 | #include "pel_rules.hpp" |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 25 | #include "pel_values.hpp" |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 26 | #include "section_factory.hpp" |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 27 | #include "src.hpp" |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 28 | #include "stream.hpp" |
Matt Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 29 | #include "user_data_formats.hpp" |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 30 | |
Matt Spinler | 5b289b2 | 2020-03-26 14:27:19 -0500 | [diff] [blame] | 31 | #include <sys/stat.h> |
| 32 | #include <unistd.h> |
| 33 | |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 34 | #include <iostream> |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 35 | #include <phosphor-logging/log.hpp> |
| 36 | |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 37 | namespace openpower |
| 38 | { |
| 39 | namespace pels |
| 40 | { |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 41 | namespace message = openpower::pels::message; |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 42 | namespace pv = openpower::pels::pel_values; |
Matt Spinler | 4bfc908 | 2020-03-24 15:05:54 -0500 | [diff] [blame] | 43 | using namespace phosphor::logging; |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 44 | |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 45 | constexpr auto unknownValue = "Unknown"; |
Matt Spinler | 5a90a95 | 2020-08-27 09:39:03 -0500 | [diff] [blame] | 46 | constexpr uint8_t jsonCalloutSubtype = 0xCA; |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 47 | |
Matt Spinler | 4bfc908 | 2020-03-24 15:05:54 -0500 | [diff] [blame] | 48 | PEL::PEL(const message::Entry& regEntry, uint32_t obmcLogID, uint64_t timestamp, |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 49 | phosphor::logging::Entry::Level severity, |
Matt Spinler | 56ad2a0 | 2020-03-26 14:00:52 -0500 | [diff] [blame] | 50 | const AdditionalData& additionalData, const PelFFDC& ffdcFiles, |
Matt Spinler | aa65947 | 2019-10-23 09:26:48 -0500 | [diff] [blame] | 51 | const DataInterfaceBase& dataIface) |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 52 | { |
Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 53 | std::map<std::string, std::vector<std::string>> debugData; |
Matt Spinler | 5a90a95 | 2020-08-27 09:39:03 -0500 | [diff] [blame] | 54 | nlohmann::json callouts; |
Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 55 | |
Matt Spinler | 4bfc908 | 2020-03-24 15:05:54 -0500 | [diff] [blame] | 56 | _ph = std::make_unique<PrivateHeader>(regEntry.componentID, obmcLogID, |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 57 | timestamp); |
Matt Spinler | aadccc8 | 2020-04-10 14:33:42 -0500 | [diff] [blame] | 58 | _uh = std::make_unique<UserHeader>(regEntry, severity, dataIface); |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 59 | |
Matt Spinler | 5a90a95 | 2020-08-27 09:39:03 -0500 | [diff] [blame] | 60 | // Extract any callouts embedded in an FFDC file. |
| 61 | if (!ffdcFiles.empty()) |
| 62 | { |
| 63 | try |
| 64 | { |
| 65 | callouts = getCalloutJSON(ffdcFiles); |
| 66 | } |
| 67 | catch (const std::exception& e) |
| 68 | { |
| 69 | debugData.emplace("FFDC file JSON callouts error", |
| 70 | std::vector<std::string>{e.what()}); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | auto src = |
| 75 | std::make_unique<SRC>(regEntry, additionalData, callouts, dataIface); |
| 76 | |
Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 77 | if (!src->getDebugData().empty()) |
| 78 | { |
| 79 | // Something didn't go as planned |
| 80 | debugData.emplace("SRC", src->getDebugData()); |
| 81 | } |
Matt Spinler | c63e2e8 | 2019-12-02 15:50:12 -0600 | [diff] [blame] | 82 | |
Matt Spinler | 4bfc908 | 2020-03-24 15:05:54 -0500 | [diff] [blame] | 83 | auto euh = std::make_unique<ExtendedUserHeader>(dataIface, regEntry, *src); |
Matt Spinler | c63e2e8 | 2019-12-02 15:50:12 -0600 | [diff] [blame] | 84 | |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 85 | _optionalSections.push_back(std::move(src)); |
Matt Spinler | c63e2e8 | 2019-12-02 15:50:12 -0600 | [diff] [blame] | 86 | _optionalSections.push_back(std::move(euh)); |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 87 | |
Matt Spinler | aa65947 | 2019-10-23 09:26:48 -0500 | [diff] [blame] | 88 | auto mtms = std::make_unique<FailingMTMS>(dataIface); |
| 89 | _optionalSections.push_back(std::move(mtms)); |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 90 | |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 91 | auto ud = util::makeSysInfoUserDataSection(additionalData, dataIface); |
Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 92 | addUserDataSection(std::move(ud)); |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 93 | |
Matt Spinler | 9b7e94f | 2020-03-24 15:44:41 -0500 | [diff] [blame] | 94 | // Create a UserData section from AdditionalData. |
Matt Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 95 | if (!additionalData.empty()) |
| 96 | { |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 97 | ud = util::makeADUserDataSection(additionalData); |
Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 98 | addUserDataSection(std::move(ud)); |
Matt Spinler | afa857c | 2019-10-24 13:03:46 -0500 | [diff] [blame] | 99 | } |
| 100 | |
Matt Spinler | 56ad2a0 | 2020-03-26 14:00:52 -0500 | [diff] [blame] | 101 | // Add any FFDC files into UserData sections |
| 102 | for (const auto& file : ffdcFiles) |
| 103 | { |
| 104 | ud = util::makeFFDCuserDataSection(regEntry.componentID, file); |
| 105 | if (!ud) |
| 106 | { |
Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 107 | // Add this error into the debug data UserData section |
| 108 | std::ostringstream msg; |
| 109 | msg << "Could not make PEL FFDC UserData section from file" |
| 110 | << std::hex << regEntry.componentID << " " << file.subType |
| 111 | << " " << file.version; |
| 112 | if (debugData.count("FFDC File")) |
| 113 | { |
| 114 | debugData.at("FFDC File").push_back(msg.str()); |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | debugData.emplace("FFDC File", |
| 119 | std::vector<std::string>{msg.str()}); |
| 120 | } |
| 121 | |
Matt Spinler | 56ad2a0 | 2020-03-26 14:00:52 -0500 | [diff] [blame] | 122 | continue; |
| 123 | } |
| 124 | |
Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 125 | addUserDataSection(std::move(ud)); |
| 126 | } |
Matt Spinler | 56ad2a0 | 2020-03-26 14:00:52 -0500 | [diff] [blame] | 127 | |
Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 128 | // Store in the PEL any important debug data created while |
| 129 | // building the PEL sections. |
| 130 | if (!debugData.empty()) |
| 131 | { |
| 132 | nlohmann::json data; |
| 133 | data["PEL Internal Debug Data"] = debugData; |
| 134 | ud = util::makeJSONUserDataSection(data); |
| 135 | |
| 136 | addUserDataSection(std::move(ud)); |
| 137 | |
| 138 | // Also put in the journal for debug |
| 139 | for (const auto& [name, data] : debugData) |
| 140 | { |
| 141 | for (const auto& message : data) |
| 142 | { |
| 143 | std::string entry = name + ": " + message; |
| 144 | log<level::INFO>(entry.c_str()); |
Matt Spinler | 56ad2a0 | 2020-03-26 14:00:52 -0500 | [diff] [blame] | 145 | } |
| 146 | } |
Matt Spinler | 56ad2a0 | 2020-03-26 14:00:52 -0500 | [diff] [blame] | 147 | } |
| 148 | |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 149 | _ph->setSectionCount(2 + _optionalSections.size()); |
Matt Spinler | f1e85e2 | 2019-11-01 11:31:31 -0500 | [diff] [blame] | 150 | |
| 151 | checkRulesAndFix(); |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 152 | } |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 153 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 154 | PEL::PEL(std::vector<uint8_t>& data) : PEL(data, 0) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 155 | { |
| 156 | } |
| 157 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 158 | PEL::PEL(std::vector<uint8_t>& data, uint32_t obmcLogID) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 159 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 160 | populateFromRawData(data, obmcLogID); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 161 | } |
| 162 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 163 | void PEL::populateFromRawData(std::vector<uint8_t>& data, uint32_t obmcLogID) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 164 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 165 | Stream pelData{data}; |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 166 | _ph = std::make_unique<PrivateHeader>(pelData); |
| 167 | if (obmcLogID != 0) |
| 168 | { |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 169 | _ph->setOBMCLogID(obmcLogID); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | _uh = std::make_unique<UserHeader>(pelData); |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 173 | |
| 174 | // Use the section factory to create the rest of the objects |
| 175 | for (size_t i = 2; i < _ph->sectionCount(); i++) |
| 176 | { |
| 177 | auto section = section_factory::create(pelData); |
| 178 | _optionalSections.push_back(std::move(section)); |
| 179 | } |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | bool PEL::valid() const |
| 183 | { |
| 184 | bool valid = _ph->valid(); |
| 185 | |
| 186 | if (valid) |
| 187 | { |
| 188 | valid = _uh->valid(); |
| 189 | } |
| 190 | |
Matt Spinler | 131870c | 2019-09-25 13:29:04 -0500 | [diff] [blame] | 191 | if (valid) |
| 192 | { |
| 193 | if (!std::all_of(_optionalSections.begin(), _optionalSections.end(), |
| 194 | [](const auto& section) { return section->valid(); })) |
| 195 | { |
| 196 | valid = false; |
| 197 | } |
| 198 | } |
| 199 | |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 200 | return valid; |
| 201 | } |
| 202 | |
| 203 | void PEL::setCommitTime() |
| 204 | { |
| 205 | auto now = std::chrono::system_clock::now(); |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 206 | _ph->setCommitTimestamp(getBCDTime(now)); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void PEL::assignID() |
| 210 | { |
Matt Spinler | 97d19b4 | 2019-10-29 11:34:03 -0500 | [diff] [blame] | 211 | _ph->setID(generatePELID()); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 212 | } |
| 213 | |
Matt Spinler | 0688545 | 2019-11-06 10:35:42 -0600 | [diff] [blame] | 214 | void PEL::flatten(std::vector<uint8_t>& pelBuffer) const |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 215 | { |
| 216 | Stream pelData{pelBuffer}; |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 217 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 218 | if (!valid()) |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 219 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 220 | log<level::WARNING>("Unflattening an invalid PEL"); |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 221 | } |
| 222 | |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 223 | _ph->flatten(pelData); |
Matt Spinler | b832363 | 2019-09-20 15:11:04 -0500 | [diff] [blame] | 224 | _uh->flatten(pelData); |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 225 | |
| 226 | for (auto& section : _optionalSections) |
| 227 | { |
| 228 | section->flatten(pelData); |
| 229 | } |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 230 | } |
| 231 | |
Matt Spinler | 0688545 | 2019-11-06 10:35:42 -0600 | [diff] [blame] | 232 | std::vector<uint8_t> PEL::data() const |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 233 | { |
Matt Spinler | 07eefc5 | 2019-09-26 11:18:26 -0500 | [diff] [blame] | 234 | std::vector<uint8_t> pelData; |
| 235 | flatten(pelData); |
| 236 | return pelData; |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 237 | } |
| 238 | |
Matt Spinler | f1b46ff | 2020-01-22 14:10:04 -0600 | [diff] [blame] | 239 | size_t PEL::size() const |
| 240 | { |
| 241 | size_t size = 0; |
| 242 | |
| 243 | if (_ph) |
| 244 | { |
| 245 | size += _ph->header().size; |
| 246 | } |
| 247 | |
| 248 | if (_uh) |
| 249 | { |
| 250 | size += _uh->header().size; |
| 251 | } |
| 252 | |
| 253 | for (const auto& section : _optionalSections) |
| 254 | { |
| 255 | size += section->header().size; |
| 256 | } |
| 257 | |
| 258 | return size; |
| 259 | } |
| 260 | |
Matt Spinler | bd716f0 | 2019-10-15 10:54:11 -0500 | [diff] [blame] | 261 | std::optional<SRC*> PEL::primarySRC() const |
| 262 | { |
| 263 | auto src = std::find_if( |
| 264 | _optionalSections.begin(), _optionalSections.end(), [](auto& section) { |
| 265 | return section->header().id == |
| 266 | static_cast<uint16_t>(SectionID::primarySRC); |
| 267 | }); |
| 268 | if (src != _optionalSections.end()) |
| 269 | { |
| 270 | return static_cast<SRC*>(src->get()); |
| 271 | } |
| 272 | |
| 273 | return std::nullopt; |
| 274 | } |
| 275 | |
Matt Spinler | f1e85e2 | 2019-11-01 11:31:31 -0500 | [diff] [blame] | 276 | void PEL::checkRulesAndFix() |
| 277 | { |
| 278 | auto [actionFlags, eventType] = |
| 279 | pel_rules::check(_uh->actionFlags(), _uh->eventType(), _uh->severity()); |
| 280 | |
| 281 | _uh->setActionFlags(actionFlags); |
| 282 | _uh->setEventType(eventType); |
| 283 | } |
| 284 | |
Matt Spinler | acb7c10 | 2020-01-10 13:49:22 -0600 | [diff] [blame] | 285 | void PEL::printSectionInJSON(const Section& section, std::string& buf, |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 286 | std::map<uint16_t, size_t>& pluralSections, |
Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 287 | message::Registry& registry, |
| 288 | const std::vector<std::string>& plugins, |
| 289 | uint8_t creatorID) const |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 290 | { |
| 291 | char tmpB[5]; |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 292 | uint8_t id[] = {static_cast<uint8_t>(section.header().id >> 8), |
| 293 | static_cast<uint8_t>(section.header().id)}; |
| 294 | sprintf(tmpB, "%c%c", id[0], id[1]); |
| 295 | std::string sectionID(tmpB); |
| 296 | std::string sectionName = pv::sectionTitles.count(sectionID) |
| 297 | ? pv::sectionTitles.at(sectionID) |
| 298 | : "Unknown Section"; |
Matt Spinler | acb7c10 | 2020-01-10 13:49:22 -0600 | [diff] [blame] | 299 | |
| 300 | // Add a count if there are multiple of this type of section |
| 301 | auto count = pluralSections.find(section.header().id); |
| 302 | if (count != pluralSections.end()) |
| 303 | { |
| 304 | sectionName += " " + std::to_string(count->second); |
| 305 | count->second++; |
| 306 | } |
| 307 | |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 308 | if (section.valid()) |
| 309 | { |
Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 310 | std::optional<std::string> json; |
| 311 | if (sectionID == "PS" || sectionID == "SS") |
| 312 | { |
Harisuddin Mohamed Isa | c8d6cc6 | 2020-08-19 22:47:19 +0800 | [diff] [blame] | 313 | json = section.getJSON(registry, plugins, creatorID); |
Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 314 | } |
Matt Spinler | 386a61e | 2020-08-13 15:51:12 -0500 | [diff] [blame] | 315 | else if ((sectionID == "UD") || (sectionID == "ED")) |
Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 316 | { |
Harisuddin Mohamed Isa | 3fdcd4e | 2020-08-26 11:56:42 +0800 | [diff] [blame] | 317 | json = section.getJSON(creatorID, plugins); |
Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 318 | } |
| 319 | else |
| 320 | { |
| 321 | json = section.getJSON(); |
| 322 | } |
Matt Spinler | 4220a15 | 2020-03-26 10:18:09 -0500 | [diff] [blame] | 323 | |
| 324 | buf += "\"" + sectionName + "\": {\n"; |
| 325 | |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 326 | if (json) |
| 327 | { |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 328 | buf += *json + "\n},\n"; |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 329 | } |
| 330 | else |
| 331 | { |
Matt Spinler | 4220a15 | 2020-03-26 10:18:09 -0500 | [diff] [blame] | 332 | jsonInsert(buf, pv::sectionVer, |
| 333 | getNumberString("%d", section.header().version), 1); |
| 334 | jsonInsert(buf, pv::subSection, |
| 335 | getNumberString("%d", section.header().subType), 1); |
| 336 | jsonInsert(buf, pv::createdBy, |
| 337 | getNumberString("0x%X", section.header().componentID), |
| 338 | 1); |
| 339 | |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 340 | std::vector<uint8_t> data; |
| 341 | Stream s{data}; |
| 342 | section.flatten(s); |
Matt Spinler | 4220a15 | 2020-03-26 10:18:09 -0500 | [diff] [blame] | 343 | std::string dstr = |
| 344 | dumpHex(std::data(data) + SectionHeader::flattenedSize(), |
Harisuddin Mohamed Isa | 097ad12 | 2020-06-11 21:19:41 +0800 | [diff] [blame] | 345 | data.size() - SectionHeader::flattenedSize(), 2); |
Matt Spinler | 4220a15 | 2020-03-26 10:18:09 -0500 | [diff] [blame] | 346 | std::string jsonIndent(indentLevel, 0x20); |
| 347 | buf += jsonIndent + "\"Data\": [\n"; |
| 348 | buf += dstr; |
| 349 | buf += jsonIndent + "]\n"; |
| 350 | buf += "},\n"; |
Aatir Manzur | ad0e047 | 2019-10-07 13:18:37 -0500 | [diff] [blame] | 351 | } |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 352 | } |
| 353 | else |
| 354 | { |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 355 | buf += "\n\"Invalid Section\": [\n \"invalid\"\n],\n"; |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
Matt Spinler | acb7c10 | 2020-01-10 13:49:22 -0600 | [diff] [blame] | 359 | std::map<uint16_t, size_t> PEL::getPluralSections() const |
| 360 | { |
| 361 | std::map<uint16_t, size_t> sectionCounts; |
| 362 | |
| 363 | for (const auto& section : optionalSections()) |
| 364 | { |
| 365 | if (sectionCounts.find(section->header().id) == sectionCounts.end()) |
| 366 | { |
| 367 | sectionCounts[section->header().id] = 1; |
| 368 | } |
| 369 | else |
| 370 | { |
| 371 | sectionCounts[section->header().id]++; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | std::map<uint16_t, size_t> sections; |
| 376 | for (const auto& [id, count] : sectionCounts) |
| 377 | { |
| 378 | if (count > 1) |
| 379 | { |
| 380 | // Start with 0 here and printSectionInJSON() |
| 381 | // will increment it as it goes. |
| 382 | sections.emplace(id, 0); |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | return sections; |
| 387 | } |
| 388 | |
Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 389 | void PEL::toJSON(message::Registry& registry, |
| 390 | const std::vector<std::string>& plugins) const |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 391 | { |
Matt Spinler | acb7c10 | 2020-01-10 13:49:22 -0600 | [diff] [blame] | 392 | auto sections = getPluralSections(); |
| 393 | |
Harisuddin Mohamed Isa | a214ed3 | 2020-02-28 15:58:23 +0800 | [diff] [blame] | 394 | std::string buf = "{\n"; |
Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 395 | printSectionInJSON(*(_ph.get()), buf, sections, registry, plugins); |
| 396 | printSectionInJSON(*(_uh.get()), buf, sections, registry, plugins); |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 397 | for (auto& section : this->optionalSections()) |
| 398 | { |
Harisuddin Mohamed Isa | f67bafd | 2020-07-06 17:51:21 +0800 | [diff] [blame] | 399 | printSectionInJSON(*(section.get()), buf, sections, registry, plugins, |
| 400 | _ph->creatorID()); |
Aatir | 186ce8c | 2019-10-20 15:13:39 -0500 | [diff] [blame] | 401 | } |
| 402 | buf += "}"; |
| 403 | std::size_t found = buf.rfind(","); |
| 404 | if (found != std::string::npos) |
| 405 | buf.replace(found, 1, ""); |
| 406 | std::cout << buf << std::endl; |
| 407 | } |
Harisuddin Mohamed Isa | 600d15a | 2019-12-20 12:42:26 +0800 | [diff] [blame] | 408 | |
Matt Spinler | 85f61a6 | 2020-06-03 16:28:55 -0500 | [diff] [blame] | 409 | bool PEL::addUserDataSection(std::unique_ptr<UserData> userData) |
| 410 | { |
| 411 | if (size() + userData->header().size > _maxPELSize) |
| 412 | { |
| 413 | if (userData->shrink(_maxPELSize - size())) |
| 414 | { |
| 415 | _optionalSections.push_back(std::move(userData)); |
| 416 | } |
| 417 | else |
| 418 | { |
| 419 | log<level::WARNING>( |
| 420 | "Could not shrink UserData section. Dropping", |
| 421 | entry("SECTION_SIZE=%d\n", userData->header().size), |
| 422 | entry("COMPONENT_ID=0x%02X", userData->header().componentID), |
| 423 | entry("SUBTYPE=0x%X", userData->header().subType), |
| 424 | entry("VERSION=0x%X", userData->header().version)); |
| 425 | return false; |
| 426 | } |
| 427 | } |
| 428 | else |
| 429 | { |
| 430 | _optionalSections.push_back(std::move(userData)); |
| 431 | } |
| 432 | return true; |
| 433 | } |
| 434 | |
Matt Spinler | 5a90a95 | 2020-08-27 09:39:03 -0500 | [diff] [blame] | 435 | nlohmann::json PEL::getCalloutJSON(const PelFFDC& ffdcFiles) |
| 436 | { |
| 437 | nlohmann::json callouts; |
| 438 | |
| 439 | for (const auto& file : ffdcFiles) |
| 440 | { |
| 441 | if ((file.format == UserDataFormat::json) && |
| 442 | (file.subType == jsonCalloutSubtype)) |
| 443 | { |
| 444 | auto data = util::readFD(file.fd); |
| 445 | if (data.empty()) |
| 446 | { |
| 447 | throw std::runtime_error{ |
| 448 | "Could not get data from JSON callout file descriptor"}; |
| 449 | } |
| 450 | |
| 451 | std::string jsonString{data.begin(), data.begin() + data.size()}; |
| 452 | |
| 453 | callouts = nlohmann::json::parse(jsonString); |
| 454 | break; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | return callouts; |
| 459 | } |
| 460 | |
Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 461 | namespace util |
| 462 | { |
| 463 | |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 464 | std::unique_ptr<UserData> makeJSONUserDataSection(const nlohmann::json& json) |
| 465 | { |
| 466 | auto jsonString = json.dump(); |
| 467 | std::vector<uint8_t> jsonData(jsonString.begin(), jsonString.end()); |
| 468 | |
| 469 | // Pad to a 4 byte boundary |
| 470 | while ((jsonData.size() % 4) != 0) |
| 471 | { |
| 472 | jsonData.push_back(0); |
| 473 | } |
| 474 | |
| 475 | return std::make_unique<UserData>( |
| 476 | static_cast<uint16_t>(ComponentID::phosphorLogging), |
| 477 | static_cast<uint8_t>(UserDataFormat::json), |
| 478 | static_cast<uint8_t>(UserDataFormatVersion::json), jsonData); |
| 479 | } |
| 480 | |
Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 481 | std::unique_ptr<UserData> makeADUserDataSection(const AdditionalData& ad) |
| 482 | { |
| 483 | assert(!ad.empty()); |
| 484 | nlohmann::json json; |
| 485 | |
| 486 | // Remove the 'ESEL' entry, as it contains a full PEL in the value. |
| 487 | if (ad.getValue("ESEL")) |
| 488 | { |
| 489 | auto newAD = ad; |
| 490 | newAD.remove("ESEL"); |
| 491 | json = newAD.toJSON(); |
| 492 | } |
| 493 | else |
| 494 | { |
| 495 | json = ad.toJSON(); |
| 496 | } |
| 497 | |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 498 | return makeJSONUserDataSection(json); |
| 499 | } |
Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 500 | |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 501 | void addProcessNameToJSON(nlohmann::json& json, |
| 502 | const std::optional<std::string>& pid, |
| 503 | const DataInterfaceBase& dataIface) |
| 504 | { |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 505 | std::string name{unknownValue}; |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 506 | |
| 507 | try |
Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 508 | { |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 509 | if (pid) |
| 510 | { |
| 511 | auto n = dataIface.getProcessName(*pid); |
| 512 | if (n) |
| 513 | { |
| 514 | name = *n; |
| 515 | } |
| 516 | } |
| 517 | } |
| 518 | catch (std::exception& e) |
| 519 | { |
Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 520 | } |
| 521 | |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 522 | json["Process Name"] = std::move(name); |
| 523 | } |
| 524 | |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 525 | void addBMCFWVersionIDToJSON(nlohmann::json& json, |
| 526 | const DataInterfaceBase& dataIface) |
| 527 | { |
| 528 | auto id = dataIface.getBMCFWVersionID(); |
| 529 | if (id.empty()) |
| 530 | { |
| 531 | id = unknownValue; |
| 532 | } |
| 533 | |
| 534 | json["BMC Version ID"] = std::move(id); |
| 535 | } |
| 536 | |
Matt Spinler | 4aa23a1 | 2020-02-03 15:05:09 -0600 | [diff] [blame] | 537 | std::string lastSegment(char separator, std::string data) |
| 538 | { |
| 539 | auto pos = data.find_last_of(separator); |
| 540 | if (pos != std::string::npos) |
| 541 | { |
| 542 | data = data.substr(pos + 1); |
| 543 | } |
| 544 | |
| 545 | return data; |
| 546 | } |
| 547 | |
| 548 | void addStatesToJSON(nlohmann::json& json, const DataInterfaceBase& dataIface) |
| 549 | { |
| 550 | json["BMCState"] = lastSegment('.', dataIface.getBMCState()); |
| 551 | json["ChassisState"] = lastSegment('.', dataIface.getChassisState()); |
| 552 | json["HostState"] = lastSegment('.', dataIface.getHostState()); |
| 553 | } |
| 554 | |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 555 | std::unique_ptr<UserData> |
| 556 | makeSysInfoUserDataSection(const AdditionalData& ad, |
| 557 | const DataInterfaceBase& dataIface) |
| 558 | { |
| 559 | nlohmann::json json; |
| 560 | |
| 561 | addProcessNameToJSON(json, ad.getValue("_PID"), dataIface); |
Matt Spinler | 677381b | 2020-01-23 10:04:29 -0600 | [diff] [blame] | 562 | addBMCFWVersionIDToJSON(json, dataIface); |
Matt Spinler | 4aa23a1 | 2020-02-03 15:05:09 -0600 | [diff] [blame] | 563 | addStatesToJSON(json, dataIface); |
Matt Spinler | 4dcd3f4 | 2020-01-22 14:55:07 -0600 | [diff] [blame] | 564 | |
| 565 | return makeJSONUserDataSection(json); |
Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 566 | } |
| 567 | |
Matt Spinler | 5b289b2 | 2020-03-26 14:27:19 -0500 | [diff] [blame] | 568 | std::vector<uint8_t> readFD(int fd) |
| 569 | { |
| 570 | std::vector<uint8_t> data; |
| 571 | |
| 572 | // Get the size |
| 573 | struct stat s; |
| 574 | int r = fstat(fd, &s); |
| 575 | if (r != 0) |
| 576 | { |
| 577 | auto e = errno; |
| 578 | log<level::ERR>("Could not get FFDC file size from FD", |
| 579 | entry("ERRNO=%d", e)); |
| 580 | return data; |
| 581 | } |
| 582 | |
| 583 | if (0 == s.st_size) |
| 584 | { |
| 585 | log<level::ERR>("FFDC file is empty"); |
| 586 | return data; |
| 587 | } |
| 588 | |
| 589 | data.resize(s.st_size); |
| 590 | |
| 591 | // Make sure its at the beginning, as maybe another |
| 592 | // extension already used it. |
| 593 | r = lseek(fd, 0, SEEK_SET); |
| 594 | if (r == -1) |
| 595 | { |
| 596 | auto e = errno; |
| 597 | log<level::ERR>("Could not seek to beginning of FFDC file", |
| 598 | entry("ERRNO=%d", e)); |
| 599 | return data; |
| 600 | } |
| 601 | |
| 602 | r = read(fd, data.data(), s.st_size); |
| 603 | if (r == -1) |
| 604 | { |
| 605 | auto e = errno; |
| 606 | log<level::ERR>("Could not read FFDC file", entry("ERRNO=%d", e)); |
| 607 | } |
| 608 | else if (r != s.st_size) |
| 609 | { |
| 610 | log<level::WARNING>("Could not read full FFDC file", |
| 611 | entry("FILE_SIZE=%d", s.st_size), |
| 612 | entry("SIZE_READ=%d", r)); |
| 613 | } |
| 614 | |
| 615 | return data; |
| 616 | } |
| 617 | |
Matt Spinler | 56ad2a0 | 2020-03-26 14:00:52 -0500 | [diff] [blame] | 618 | std::unique_ptr<UserData> makeFFDCuserDataSection(uint16_t componentID, |
| 619 | const PelFFDCfile& file) |
| 620 | { |
Matt Spinler | 5b289b2 | 2020-03-26 14:27:19 -0500 | [diff] [blame] | 621 | auto data = readFD(file.fd); |
| 622 | |
| 623 | if (data.empty()) |
| 624 | { |
| 625 | return std::unique_ptr<UserData>(); |
| 626 | } |
| 627 | |
| 628 | // The data needs 4 Byte alignment, and save amount padded for the |
| 629 | // CBOR case. |
| 630 | uint32_t pad = 0; |
| 631 | while (data.size() % 4) |
| 632 | { |
| 633 | data.push_back(0); |
| 634 | pad++; |
| 635 | } |
| 636 | |
| 637 | // For JSON, CBOR, and Text use our component ID, subType, and version, |
| 638 | // otherwise use the supplied ones. |
| 639 | uint16_t compID = static_cast<uint16_t>(ComponentID::phosphorLogging); |
| 640 | uint8_t subType{}; |
| 641 | uint8_t version{}; |
| 642 | |
| 643 | switch (file.format) |
| 644 | { |
| 645 | case UserDataFormat::json: |
| 646 | subType = static_cast<uint8_t>(UserDataFormat::json); |
| 647 | version = static_cast<uint8_t>(UserDataFormatVersion::json); |
| 648 | break; |
| 649 | case UserDataFormat::cbor: |
| 650 | subType = static_cast<uint8_t>(UserDataFormat::cbor); |
| 651 | version = static_cast<uint8_t>(UserDataFormatVersion::cbor); |
| 652 | |
| 653 | // The CBOR parser will fail on the extra pad bytes since they |
| 654 | // aren't CBOR. Add the amount we padded to the end and other |
| 655 | // code will remove it all before parsing. |
| 656 | { |
| 657 | data.resize(data.size() + 4); |
| 658 | Stream stream{data}; |
| 659 | stream.offset(data.size() - 4); |
| 660 | stream << pad; |
| 661 | } |
| 662 | |
| 663 | break; |
| 664 | case UserDataFormat::text: |
| 665 | subType = static_cast<uint8_t>(UserDataFormat::text); |
| 666 | version = static_cast<uint8_t>(UserDataFormatVersion::text); |
| 667 | break; |
| 668 | case UserDataFormat::custom: |
| 669 | default: |
| 670 | // Use the passed in values |
| 671 | compID = componentID; |
| 672 | subType = file.subType; |
| 673 | version = file.version; |
| 674 | break; |
| 675 | } |
| 676 | |
| 677 | return std::make_unique<UserData>(compID, subType, version, data); |
Matt Spinler | 56ad2a0 | 2020-03-26 14:00:52 -0500 | [diff] [blame] | 678 | } |
| 679 | |
Matt Spinler | c7c3e40 | 2020-01-22 15:07:25 -0600 | [diff] [blame] | 680 | } // namespace util |
| 681 | |
Matt Spinler | cb6b059 | 2019-07-16 15:58:51 -0500 | [diff] [blame] | 682 | } // namespace pels |
| 683 | } // namespace openpower |