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