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