blob: 9926833ec1956487b18d51d5be267ac09b7dde4f [file] [log] [blame]
Matt Spinler711d51d2019-11-06 09:36:51 -06001/**
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 */
Aatir7b291ec2019-11-19 10:37:37 -060016#include "config.h"
17
18#include "../bcd_time.hpp"
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +080019#include "../json_utils.hpp"
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +080020#include "../paths.hpp"
Aatir186ce8c2019-10-20 15:13:39 -050021#include "../pel.hpp"
Aatir7b291ec2019-11-19 10:37:37 -060022#include "../pel_types.hpp"
23#include "../pel_values.hpp"
Aatir186ce8c2019-10-20 15:13:39 -050024
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +080025#include <Python.h>
26
Aatir186ce8c2019-10-20 15:13:39 -050027#include <CLI/CLI.hpp>
Aatir7b291ec2019-11-19 10:37:37 -060028#include <bitset>
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +080029#include <fstream>
Aatir186ce8c2019-10-20 15:13:39 -050030#include <iostream>
Aatir7b291ec2019-11-19 10:37:37 -060031#include <phosphor-logging/log.hpp>
32#include <regex>
Aatir186ce8c2019-10-20 15:13:39 -050033#include <string>
Aatir7b291ec2019-11-19 10:37:37 -060034#include <xyz/openbmc_project/Common/File/error.hpp>
Aatir186ce8c2019-10-20 15:13:39 -050035
Aatir7b291ec2019-11-19 10:37:37 -060036namespace fs = std::filesystem;
Aatir186ce8c2019-10-20 15:13:39 -050037using namespace phosphor::logging;
38using namespace openpower::pels;
Matt Spinler27de6f32020-02-21 08:35:57 -060039using sdbusplus::exception::SdBusError;
Aatir7b291ec2019-11-19 10:37:37 -060040namespace file_error = sdbusplus::xyz::openbmc_project::Common::File::Error;
41namespace message = openpower::pels::message;
42namespace pv = openpower::pels::pel_values;
Aatir186ce8c2019-10-20 15:13:39 -050043
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +080044using PELFunc = std::function<void(const PEL&, bool hexDump)>;
Matt Spinler0d804ef2020-05-12 16:16:26 -050045message::Registry registry(getPELReadOnlyDataPath() / message::registryFileName,
Matt Spinlerd8e29002020-04-09 09:11:22 -050046 false);
Matt Spinler27de6f32020-02-21 08:35:57 -060047namespace service
48{
49constexpr auto logging = "xyz.openbmc_project.Logging";
50} // namespace service
51
52namespace interface
53{
54constexpr auto deleteObj = "xyz.openbmc_project.Object.Delete";
55constexpr auto deleteAll = "xyz.openbmc_project.Collection.DeleteAll";
56} // namespace interface
57
58namespace object_path
59{
60constexpr auto logEntry = "/xyz/openbmc_project/logging/entry/";
61constexpr auto logging = "/xyz/openbmc_project/logging";
62} // namespace object_path
63
Aatire340c132019-12-09 14:19:29 -060064/**
Aatir37822f62019-12-10 14:40:27 -060065 * @brief helper function to get PEL commit timestamp from file name
66 * @retrun BCDTime - PEL commit timestamp
67 * @param[in] std::string - file name
68 */
69BCDTime fileNameToTimestamp(const std::string& fileName)
70{
71 std::string token = fileName.substr(0, fileName.find("_"));
72 int i = 0;
73 BCDTime tmp;
74 if (token.length() >= 14)
75 {
76 try
77 {
78 tmp.yearMSB = std::stoi(token.substr(i, 2), 0, 16);
79 }
80 catch (std::exception& err)
81 {
82 std::cout << "Conversion failure: " << err.what() << std::endl;
83 }
84 i += 2;
85 try
86 {
87 tmp.yearLSB = std::stoi(token.substr(i, 2), 0, 16);
88 }
89 catch (std::exception& err)
90 {
91 std::cout << "Conversion failure: " << err.what() << std::endl;
92 }
93 i += 2;
94 try
95 {
96 tmp.month = std::stoi(token.substr(i, 2), 0, 16);
97 }
98 catch (std::exception& err)
99 {
100 std::cout << "Conversion failure: " << err.what() << std::endl;
101 }
102 i += 2;
103 try
104 {
105 tmp.day = std::stoi(token.substr(i, 2), 0, 16);
106 }
107 catch (std::exception& err)
108 {
109 std::cout << "Conversion failure: " << err.what() << std::endl;
110 }
111 i += 2;
112 try
113 {
114 tmp.hour = std::stoi(token.substr(i, 2), 0, 16);
115 }
116 catch (std::exception& err)
117 {
118 std::cout << "Conversion failure: " << err.what() << std::endl;
119 }
120 i += 2;
121 try
122 {
123 tmp.minutes = std::stoi(token.substr(i, 2), 0, 16);
124 }
125 catch (std::exception& err)
126 {
127 std::cout << "Conversion failure: " << err.what() << std::endl;
128 }
129 i += 2;
130 try
131 {
132 tmp.seconds = std::stoi(token.substr(i, 2), 0, 16);
133 }
134 catch (std::exception& err)
135 {
136 std::cout << "Conversion failure: " << err.what() << std::endl;
137 }
138 i += 2;
139 try
140 {
141 tmp.hundredths = std::stoi(token.substr(i, 2), 0, 16);
142 }
143 catch (std::exception& err)
144 {
145 std::cout << "Conversion failure: " << err.what() << std::endl;
146 }
147 }
148 return tmp;
149}
150
151/**
152 * @brief helper function to get PEL id from file name
153 * @retrun uint32_t - PEL id
154 * @param[in] std::string - file name
155 */
156uint32_t fileNameToPELId(const std::string& fileName)
157{
158 uint32_t num = 0;
159 try
160 {
161 num = std::stoi(fileName.substr(fileName.find("_") + 1), 0, 16);
162 }
163 catch (std::exception& err)
164 {
165 std::cout << "Conversion failure: " << err.what() << std::endl;
166 }
167 return num;
168}
169
170/**
Aatire340c132019-12-09 14:19:29 -0600171 * @brief helper function to check string suffix
172 * @retrun bool - true with suffix matches
173 * @param[in] std::string - string to check for suffix
174 * @param[in] std::string - suffix string
175 */
176bool ends_with(const std::string& str, const std::string& end)
177{
178 size_t slen = str.size(), elen = end.size();
179 if (slen < elen)
180 return false;
181 while (elen)
182 {
183 if (str[--slen] != end[--elen])
184 return false;
185 }
186 return true;
187}
188
Aatir37822f62019-12-10 14:40:27 -0600189/**
190 * @brief get data form raw PEL file.
191 * @param[in] std::string Name of file with raw PEL
192 * @return std::vector<uint8_t> char vector read from raw PEL file.
193 */
Aatirbad5f8a2019-12-10 15:27:16 -0600194std::vector<uint8_t> getFileData(const std::string& name)
Aatir37822f62019-12-10 14:40:27 -0600195{
196 std::ifstream file(name, std::ifstream::in);
197 if (file.good())
198 {
199 std::vector<uint8_t> data{std::istreambuf_iterator<char>(file),
200 std::istreambuf_iterator<char>()};
201 return data;
202 }
203 else
204 {
Aatir37822f62019-12-10 14:40:27 -0600205 return {};
206 }
207}
208
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800209/**
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800210 * @brief Initialize Python interpreter and gather all UD parser modules under
211 * the paths found in Python sys.path and the current user directory.
212 * This is to prevent calling a non-existant module which causes Python
213 * to print an import error message and breaking JSON output.
214 *
215 * @return std::vector<std::string> Vector of plugins found in filesystem
216 */
217std::vector<std::string> getPlugins()
218{
219 Py_Initialize();
220 std::vector<std::string> plugins;
221 std::vector<std::string> siteDirs;
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800222 std::array<std::string, 2> parserDirs = {"udparsers", "srcparsers"};
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800223 PyObject* pName = PyUnicode_FromString("sys");
224 PyObject* pModule = PyImport_Import(pName);
225 Py_XDECREF(pName);
226 PyObject* pDict = PyModule_GetDict(pModule);
227 Py_XDECREF(pModule);
228 PyObject* pResult = PyDict_GetItemString(pDict, "path");
229 PyObject* pValue = PyUnicode_FromString(".");
230 PyList_Append(pResult, pValue);
231 Py_XDECREF(pValue);
232 auto list_size = PyList_Size(pResult);
233 for (auto i = 0; i < list_size; i++)
234 {
235 PyObject* item = PyList_GetItem(pResult, i);
236 PyObject* pBytes = PyUnicode_AsEncodedString(item, "utf-8", "~E~");
237 const char* output = PyBytes_AS_STRING(pBytes);
238 Py_XDECREF(pBytes);
239 std::string tmpStr(output);
240 siteDirs.push_back(tmpStr);
241 }
242 for (const auto& dir : siteDirs)
243 {
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800244 for (const auto& parserDir : parserDirs)
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800245 {
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800246 if (fs::exists(dir + "/" + parserDir))
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800247 {
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800248 for (const auto& entry :
249 fs::directory_iterator(dir + "/" + parserDir))
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800250 {
Harisuddin Mohamed Isac8d6cc62020-08-19 22:47:19 +0800251 if (entry.is_directory() and
252 fs::exists(entry.path().string() + "/" +
253 entry.path().stem().string() + ".py"))
254 {
255 plugins.push_back(entry.path().stem());
256 }
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800257 }
258 }
259 }
260 }
261 return plugins;
262}
263
264/**
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800265 * @brief Creates JSON string of a PEL entry if fullPEL is false or prints to
266 * stdout the full PEL in JSON if fullPEL is true
267 * @param[in] itr - std::map iterator of <uint32_t, BCDTime>
268 * @param[in] hidden - Boolean to include hidden PELs
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800269 * @param[in] includeInfo - Boolean to include informational PELs
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800270 * @param[in] fullPEL - Boolean to print full JSON representation of PEL
271 * @param[in] foundPEL - Boolean to check if any PEL is present
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800272 * @param[in] scrubRegex - SRC regex object
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800273 * @param[in] plugins - Vector of strings of plugins found in filesystem
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800274 * @param[in] hexDump - Boolean to print hexdump of PEL instead of JSON
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800275 * @return std::string - JSON string of PEL entry (empty if fullPEL is true)
276 */
Aatir7b291ec2019-11-19 10:37:37 -0600277template <typename T>
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800278std::string genPELJSON(T itr, bool hidden, bool includeInfo, bool fullPEL,
279 bool& foundPEL,
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800280 const std::optional<std::regex>& scrubRegex,
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800281 const std::vector<std::string>& plugins, bool hexDump)
Aatir7b291ec2019-11-19 10:37:37 -0600282{
283 std::size_t found;
284 std::string val;
285 char tmpValStr[50];
286 std::string listStr;
287 char name[50];
288 sprintf(name, "%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X_%.8X", itr.second.yearMSB,
289 itr.second.yearLSB, itr.second.month, itr.second.day,
290 itr.second.hour, itr.second.minutes, itr.second.seconds,
291 itr.second.hundredths, itr.first);
292 std::string fileName(name);
293 fileName = EXTENSION_PERSIST_DIR "/pels/logs/" + fileName;
294 try
295 {
Aatir37822f62019-12-10 14:40:27 -0600296 std::vector<uint8_t> data = getFileData(fileName);
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800297 if (data.empty())
Aatir37822f62019-12-10 14:40:27 -0600298 {
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800299 log<level::ERR>("Empty PEL file",
300 entry("FILENAME=%s", fileName.c_str()));
301 return listStr;
302 }
303 PEL pel{data};
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800304 if (!pel.valid())
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800305 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800306 return listStr;
307 }
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800308 if (!includeInfo && pel.userHeader().severity() == 0)
309 {
310 return listStr;
311 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800312 std::bitset<16> actionFlags{pel.userHeader().actionFlags()};
313 if (!hidden && actionFlags.test(hiddenFlagBit))
314 {
315 return listStr;
316 }
317 if (pel.primarySRC() && scrubRegex)
318 {
319 val = pel.primarySRC().value()->asciiString();
320 if (std::regex_search(trimEnd(val), scrubRegex.value(),
321 std::regex_constants::match_not_null))
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800322 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800323 return listStr;
324 }
325 }
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800326 if (hexDump)
327 {
328 std::cout << dumpHex(std::data(pel.data()), pel.size(), 0, false)
329 << std::endl;
330 }
331 else if (fullPEL)
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800332 {
333 if (!foundPEL)
334 {
335 std::cout << "[\n";
336 foundPEL = true;
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800337 }
338 else
Aatir7b291ec2019-11-19 10:37:37 -0600339 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800340 std::cout << ",\n\n";
341 }
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800342 pel.toJSON(registry, plugins);
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800343 }
344 else
345 {
346 // id
347 listStr += "\t\"" +
348 getNumberString("0x%X", pel.privateHeader().id()) +
349 "\": {\n";
350 // ASCII
351 if (pel.primarySRC())
352 {
353 val = pel.primarySRC().value()->asciiString();
354 listStr += "\t\t\"SRC\": \"" + trimEnd(val) + "\",\n";
355 // Registry message
356 auto regVal = pel.primarySRC().value()->getErrorDetails(
357 registry, DetailLevel::message, true);
358 if (regVal)
Harisuddin Mohamed Isa0f717e12020-01-15 20:05:33 +0800359 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800360 val = regVal.value();
361 listStr += "\t\t\"Message\": \"" + val + "\",\n";
Aatir37822f62019-12-10 14:40:27 -0600362 }
Aatir7b291ec2019-11-19 10:37:37 -0600363 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800364 else
365 {
366 listStr += "\t\t\"SRC\": \"No SRC\",\n";
367 }
368 // platformid
369 listStr += "\t\t\"PLID\": \"" +
370 getNumberString("0x%X", pel.privateHeader().plid()) +
371 "\",\n";
372 // creatorid
373 std::string creatorID =
374 getNumberString("%c", pel.privateHeader().creatorID());
375 val = pv::creatorIDs.count(creatorID) ? pv::creatorIDs.at(creatorID)
376 : "Unknown Creator ID";
377 listStr += "\t\t\"CreatorID\": \"" + val + "\",\n";
378 // subsytem
379 std::string subsystem = pv::getValue(pel.userHeader().subsystem(),
380 pel_values::subsystemValues);
381 listStr += "\t\t\"Subsystem\": \"" + subsystem + "\",\n";
382 // commit time
383 sprintf(tmpValStr, "%02X/%02X/%02X%02X %02X:%02X:%02X",
384 pel.privateHeader().commitTimestamp().month,
385 pel.privateHeader().commitTimestamp().day,
386 pel.privateHeader().commitTimestamp().yearMSB,
387 pel.privateHeader().commitTimestamp().yearLSB,
388 pel.privateHeader().commitTimestamp().hour,
389 pel.privateHeader().commitTimestamp().minutes,
390 pel.privateHeader().commitTimestamp().seconds);
391 val = std::string(tmpValStr);
392 listStr += "\t\t\"Commit Time\": \"" + val + "\",\n";
393 // severity
394 std::string severity = pv::getValue(pel.userHeader().severity(),
395 pel_values::severityValues);
396 listStr += "\t\t\"Sev\": \"" + severity + "\",\n ";
397 // compID
398 listStr += "\t\t\"CompID\": \"" +
399 getNumberString(
400 "0x%X", pel.privateHeader().header().componentID) +
401 "\",\n ";
402 found = listStr.rfind(",");
403 if (found != std::string::npos)
404 {
405 listStr.replace(found, 1, "");
406 listStr += "\t},\n";
407 }
408 foundPEL = true;
Aatir7b291ec2019-11-19 10:37:37 -0600409 }
410 }
411 catch (std::exception& e)
412 {
413 log<level::ERR>("Hit exception while reading PEL File",
414 entry("FILENAME=%s", fileName.c_str()),
415 entry("ERROR=%s", e.what()));
416 }
417 return listStr;
418}
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800419
Aatir7b291ec2019-11-19 10:37:37 -0600420/**
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800421 * @brief Print a list of PELs or a JSON array of PELs
422 * @param[in] order - Boolean to print in reverse orser
423 * @param[in] hidden - Boolean to include hidden PELs
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800424 * @param[in] includeInfo - Boolean to include informational PELs
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800425 * @param[in] fullPEL - Boolean to print full PEL into a JSON array
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800426 * @param[in] scrubRegex - SRC regex object
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800427 * @param[in] hexDump - Boolean to print hexdump of PEL instead of JSON
Aatir7b291ec2019-11-19 10:37:37 -0600428 */
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800429void printPELs(bool order, bool hidden, bool includeInfo, bool fullPEL,
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800430 const std::optional<std::regex>& scrubRegex, bool hexDump)
Aatir7b291ec2019-11-19 10:37:37 -0600431{
432 std::string listStr;
433 std::map<uint32_t, BCDTime> PELs;
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800434 std::vector<std::string> plugins;
Aatir7b291ec2019-11-19 10:37:37 -0600435 listStr = "{\n";
436 for (auto it = fs::directory_iterator(EXTENSION_PERSIST_DIR "/pels/logs");
437 it != fs::directory_iterator(); ++it)
438 {
439 if (!fs::is_regular_file((*it).path()))
440 {
441 continue;
442 }
Aatir37822f62019-12-10 14:40:27 -0600443 else
Aatir7b291ec2019-11-19 10:37:37 -0600444 {
Aatir37822f62019-12-10 14:40:27 -0600445 PELs.emplace(fileNameToPELId((*it).path().filename()),
446 fileNameToTimestamp((*it).path().filename()));
Aatir7b291ec2019-11-19 10:37:37 -0600447 }
448 }
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800449 bool foundPEL = false;
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800450
451 if (fullPEL && !hexDump)
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800452 {
453 plugins = getPlugins();
454 }
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800455 auto buildJSON = [&listStr, &hidden, &includeInfo, &fullPEL, &foundPEL,
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800456 &scrubRegex, &plugins, &hexDump](const auto& i) {
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800457 listStr += genPELJSON(i, hidden, includeInfo, fullPEL, foundPEL,
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800458 scrubRegex, plugins, hexDump);
Aatir37822f62019-12-10 14:40:27 -0600459 };
Aatir7b291ec2019-11-19 10:37:37 -0600460 if (order)
461 {
462 std::for_each(PELs.rbegin(), PELs.rend(), buildJSON);
463 }
464 else
465 {
466 std::for_each(PELs.begin(), PELs.end(), buildJSON);
467 }
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800468 if (hexDump)
469 {
470 return;
471 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800472 if (foundPEL)
Aatir7b291ec2019-11-19 10:37:37 -0600473 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800474 if (fullPEL)
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800475 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800476 std::cout << "]" << std::endl;
477 }
478 else
479 {
480 std::size_t found;
481 found = listStr.rfind(",");
482 if (found != std::string::npos)
483 {
484 listStr.replace(found, 1, "");
485 listStr += "}\n";
486 printf("%s", listStr.c_str());
487 }
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800488 }
489 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800490 else
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800491 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800492 std::string emptyJSON = fullPEL ? "[]" : "{}";
493 std::cout << emptyJSON << std::endl;
Aatir7b291ec2019-11-19 10:37:37 -0600494 }
495}
Aatir186ce8c2019-10-20 15:13:39 -0500496
Matt Spinler27de6f32020-02-21 08:35:57 -0600497/**
498 * @brief Calls the function passed in on the PEL with the ID
499 * passed in.
500 *
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800501 * @param[in] id - The string version of the PEL or BMC Log ID, either with or
Matt Spinler27de6f32020-02-21 08:35:57 -0600502 * without the 0x prefix.
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800503 * @param[in] func - The std::function<void(const PEL&, bool hexDump)> function
504 * to run.
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800505 * @param[in] useBMC - if true, search by BMC Log ID, else search by PEL ID
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800506 * @param[in] hexDump - Boolean to print hexdump of PEL instead of JSON
Matt Spinler27de6f32020-02-21 08:35:57 -0600507 */
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800508void callFunctionOnPEL(const std::string& id, const PELFunc& func,
509 bool useBMC = false, bool hexDump = false)
Matt Spinler27de6f32020-02-21 08:35:57 -0600510{
511 std::string pelID{id};
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800512 if (!useBMC)
Matt Spinler27de6f32020-02-21 08:35:57 -0600513 {
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800514 std::transform(pelID.begin(), pelID.end(), pelID.begin(), toupper);
515
516 if (pelID.find("0X") == 0)
517 {
518 pelID.erase(0, 2);
519 }
Matt Spinler27de6f32020-02-21 08:35:57 -0600520 }
521
522 bool found = false;
523
524 for (auto it = fs::directory_iterator(EXTENSION_PERSIST_DIR "/pels/logs");
525 it != fs::directory_iterator(); ++it)
526 {
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800527 // The PEL ID is part of the filename, so use that to find the PEL if
528 // "useBMC" is set to false, otherwise we have to search within the PEL
Matt Spinler27de6f32020-02-21 08:35:57 -0600529
530 if (!fs::is_regular_file((*it).path()))
531 {
532 continue;
533 }
534
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800535 if ((ends_with((*it).path(), pelID) && !useBMC) || useBMC)
Matt Spinler27de6f32020-02-21 08:35:57 -0600536 {
Matt Spinler27de6f32020-02-21 08:35:57 -0600537 auto data = getFileData((*it).path());
538 if (!data.empty())
539 {
540 PEL pel{data};
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800541 if (!useBMC ||
542 (useBMC && pel.obmcLogID() == std::stoul(id, nullptr, 0)))
Matt Spinler27de6f32020-02-21 08:35:57 -0600543 {
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800544 found = true;
545 try
546 {
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800547 func(pel, hexDump);
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800548 break;
549 }
550 catch (std::exception& e)
551 {
552 std::cerr << " Internal function threw an exception: "
553 << e.what() << "\n";
554 exit(1);
555 }
Matt Spinler27de6f32020-02-21 08:35:57 -0600556 }
557 }
558 else
559 {
560 std::cerr << "Could not read PEL file\n";
561 exit(1);
562 }
Matt Spinler27de6f32020-02-21 08:35:57 -0600563 }
564 }
565
566 if (!found)
567 {
568 std::cerr << "PEL not found\n";
569 exit(1);
570 }
571}
572
573/**
574 * @brief Delete a PEL by deleting its corresponding event log.
575 *
576 * @param[in] pel - The PEL to delete
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800577 * @param[in] hexDump - Boolean to print hexdump of PEL instead of JSON (unused)
Matt Spinler27de6f32020-02-21 08:35:57 -0600578 */
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800579void deletePEL(const PEL& pel, bool hexDump = false)
Matt Spinler27de6f32020-02-21 08:35:57 -0600580{
581 std::string path{object_path::logEntry};
582 path += std::to_string(pel.obmcLogID());
583
584 try
585 {
586 auto bus = sdbusplus::bus::new_default();
587 auto method = bus.new_method_call(service::logging, path.c_str(),
588 interface::deleteObj, "Delete");
589 auto reply = bus.call(method);
590 }
591 catch (const SdBusError& e)
592 {
593 std::cerr << "D-Bus call to delete event log " << pel.obmcLogID()
594 << " failed: " << e.what() << "\n";
595 exit(1);
596 }
597}
598
599/**
600 * @brief Delete all PELs by deleting all event logs.
601 */
602void deleteAllPELs()
603{
604 try
605 {
606 // This may move to an audit log some day
607 log<level::INFO>("peltool deleting all event logs");
608
609 auto bus = sdbusplus::bus::new_default();
610 auto method =
611 bus.new_method_call(service::logging, object_path::logging,
612 interface::deleteAll, "DeleteAll");
613 auto reply = bus.call(method);
614 }
615 catch (const SdBusError& e)
616 {
617 std::cerr << "D-Bus call to delete all event logs failed: " << e.what()
618 << "\n";
619 exit(1);
620 }
621}
622
Matt Spinler1b420bc2020-02-21 08:54:25 -0600623/**
624 * @brief Display a single PEL
625 *
626 * @param[in] pel - the PEL to display
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800627 * @param[in] hexDump - Boolean to print hexdump of PEL instead of JSON
Matt Spinler1b420bc2020-02-21 08:54:25 -0600628 */
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800629void displayPEL(const PEL& pel, bool hexDump)
Matt Spinler1b420bc2020-02-21 08:54:25 -0600630{
631 if (pel.valid())
632 {
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800633 if (hexDump)
634 {
635 std::string dstr =
636 dumpHex(std::data(pel.data()), pel.size(), 0, false);
637 std::cout << dstr << std::endl;
638 }
639 else
640 {
641 auto plugins = getPlugins();
642 pel.toJSON(registry, plugins);
643 }
Matt Spinler1b420bc2020-02-21 08:54:25 -0600644 }
645 else
646 {
647 std::cerr << "PEL was malformed\n";
648 exit(1);
649 }
650}
651
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800652/**
653 * @brief Print number of PELs
654 * @param[in] hidden - Bool to include hidden logs
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800655 * @param[in] includeInfo - Bool to include informational logs
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800656 * @param[in] scrubRegex - SRC regex object
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800657 */
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800658void printPELCount(bool hidden, bool includeInfo,
659 const std::optional<std::regex>& scrubRegex)
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800660{
661 std::size_t count = 0;
662 for (auto it = fs::directory_iterator(EXTENSION_PERSIST_DIR "/pels/logs");
663 it != fs::directory_iterator(); ++it)
664 {
665 if (!fs::is_regular_file((*it).path()))
666 {
667 continue;
668 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800669 std::vector<uint8_t> data = getFileData((*it).path());
670 if (data.empty())
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800671 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800672 continue;
673 }
674 PEL pel{data};
675 if (!pel.valid())
676 {
677 continue;
678 }
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800679 if (!includeInfo && pel.userHeader().severity() == 0)
680 {
681 continue;
682 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800683 std::bitset<16> actionFlags{pel.userHeader().actionFlags()};
684 if (!hidden && actionFlags.test(hiddenFlagBit))
685 {
686 continue;
687 }
688 if (pel.primarySRC() && scrubRegex)
689 {
690 std::string val = pel.primarySRC().value()->asciiString();
691 if (std::regex_search(trimEnd(val), scrubRegex.value(),
692 std::regex_constants::match_not_null))
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800693 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800694 continue;
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800695 }
696 }
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800697 count++;
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800698 }
699 std::cout << "{\n"
700 << " \"Number of PELs found\": "
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800701 << getNumberString("%d", count) << "\n}\n";
702}
703
704/**
705 * @brief Generate regex pattern object from file contents
706 * @param[in] scrubFile - File containing regex pattern
707 * @return std::regex - SRC regex object
708 */
709std::regex genRegex(std::string& scrubFile)
710{
711 std::string pattern;
712 std::ifstream contents(scrubFile);
713 if (contents.fail())
714 {
715 std::cerr << "Can't open \"" << scrubFile << "\"\n";
716 exit(1);
717 }
718 std::string line;
719 while (std::getline(contents, line))
720 {
721 if (!line.empty())
722 {
723 pattern.append(line + "|");
724 }
725 }
726 try
727 {
728 std::regex scrubRegex(pattern, std::regex::icase);
729 return scrubRegex;
730 }
731 catch (std::regex_error& e)
732 {
733 if (e.code() == std::regex_constants::error_collate)
734 std::cerr << "Invalid collating element request\n";
735 else if (e.code() == std::regex_constants::error_ctype)
736 std::cerr << "Invalid character class\n";
737 else if (e.code() == std::regex_constants::error_escape)
738 std::cerr << "Invalid escape character or trailing escape\n";
739 else if (e.code() == std::regex_constants::error_backref)
740 std::cerr << "Invalid back reference\n";
741 else if (e.code() == std::regex_constants::error_brack)
742 std::cerr << "Mismatched bracket ([ or ])\n";
743 else if (e.code() == std::regex_constants::error_paren)
744 {
745 // to catch return code error_badrepeat when error_paren is retured
746 // instead
747 size_t pos = pattern.find_first_of("*+?{");
748 while (pos != std::string::npos)
749 {
750 if (pos == 0 || pattern.substr(pos - 1, 1) == "|")
751 {
752 std::cerr
753 << "A repetition character (*, ?, +, or {) was not "
754 "preceded by a valid regular expression\n";
755 exit(1);
756 }
757 pos = pattern.find_first_of("*+?{", pos + 1);
758 }
759 std::cerr << "Mismatched parentheses (( or ))\n";
760 }
761 else if (e.code() == std::regex_constants::error_brace)
762 std::cerr << "Mismatched brace ({ or })\n";
763 else if (e.code() == std::regex_constants::error_badbrace)
764 std::cerr << "Invalid range inside a { }\n";
765 else if (e.code() == std::regex_constants::error_range)
766 std::cerr << "Invalid character range (e.g., [z-a])\n";
767 else if (e.code() == std::regex_constants::error_space)
768 std::cerr << "Insufficient memory to handle regular expression\n";
769 else if (e.code() == std::regex_constants::error_badrepeat)
770 std::cerr << "A repetition character (*, ?, +, or {) was not "
771 "preceded by a valid regular expression\n";
772 else if (e.code() == std::regex_constants::error_complexity)
773 std::cerr << "The requested match is too complex\n";
774 else if (e.code() == std::regex_constants::error_stack)
775 std::cerr << "Insufficient memory to evaluate a match\n";
776 exit(1);
777 }
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800778}
779
Aatir186ce8c2019-10-20 15:13:39 -0500780static void exitWithError(const std::string& help, const char* err)
781{
782 std::cerr << "ERROR: " << err << std::endl << help << std::endl;
783 exit(-1);
784}
785
786int main(int argc, char** argv)
787{
788 CLI::App app{"OpenBMC PEL Tool"};
789 std::string fileName;
Aatire340c132019-12-09 14:19:29 -0600790 std::string idPEL;
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800791 std::string bmcId;
Matt Spinler27de6f32020-02-21 08:35:57 -0600792 std::string idToDelete;
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800793 std::string scrubFile;
794 std::optional<std::regex> scrubRegex;
Aatire340c132019-12-09 14:19:29 -0600795 bool listPEL = false;
796 bool listPELDescOrd = false;
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800797 bool hidden = false;
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800798 bool includeInfo = false;
Matt Spinler27de6f32020-02-21 08:35:57 -0600799 bool deleteAll = false;
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800800 bool showPELCount = false;
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800801 bool fullPEL = false;
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800802 bool hexDump = false;
Matt Spinler27de6f32020-02-21 08:35:57 -0600803
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800804 app.set_help_flag("--help", "Print this help message and exit");
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800805 app.add_option("--file", fileName, "Display a PEL using its Raw PEL file");
Aatire340c132019-12-09 14:19:29 -0600806 app.add_option("-i, --id", idPEL, "Display a PEL based on its ID");
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800807 app.add_option("--bmc-id", bmcId,
808 "Display a PEL based on its BMC Event ID");
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800809 app.add_flag("-a", fullPEL, "Display all PELs");
Aatirbad5f8a2019-12-10 15:27:16 -0600810 app.add_flag("-l", listPEL, "List PELs");
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800811 app.add_flag("-n", showPELCount, "Show number of PELs");
Aatir7b291ec2019-11-19 10:37:37 -0600812 app.add_flag("-r", listPELDescOrd, "Reverse order of output");
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800813 app.add_flag("-h", hidden, "Include hidden PELs");
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800814 app.add_flag("-f,--info", includeInfo, "Include informational PELs");
Matt Spinler27de6f32020-02-21 08:35:57 -0600815 app.add_option("-d, --delete", idToDelete, "Delete a PEL based on its ID");
816 app.add_flag("-D, --delete-all", deleteAll, "Delete all PELs");
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800817 app.add_option("-s, --scrub", scrubFile,
818 "File containing SRC regular expressions to ignore");
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800819 app.add_flag("-x", hexDump, "Display PEL(s) in hexdump instead of JSON");
Matt Spinler27de6f32020-02-21 08:35:57 -0600820
Aatir186ce8c2019-10-20 15:13:39 -0500821 CLI11_PARSE(app, argc, argv);
822
823 if (!fileName.empty())
824 {
825 std::vector<uint8_t> data = getFileData(fileName);
826 if (!data.empty())
827 {
828 PEL pel{data};
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800829 if (hexDump)
830 {
831 std::string dstr =
832 dumpHex(std::data(pel.data()), pel.size(), 0, false);
833 std::cout << dstr << std::endl;
834 }
835 else
836 {
837 auto plugins = getPlugins();
838 pel.toJSON(registry, plugins);
839 }
Aatir186ce8c2019-10-20 15:13:39 -0500840 }
841 else
842 {
843 exitWithError(app.help("", CLI::AppFormatMode::All),
844 "Raw PEL file can't be read.");
845 }
846 }
Aatire340c132019-12-09 14:19:29 -0600847 else if (!idPEL.empty())
848 {
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800849 callFunctionOnPEL(idPEL, displayPEL, false, hexDump);
Harisuddin Mohamed Isa58d3a982020-09-17 19:48:05 +0800850 }
851 else if (!bmcId.empty())
852 {
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800853 callFunctionOnPEL(bmcId, displayPEL, true, hexDump);
Aatire340c132019-12-09 14:19:29 -0600854 }
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800855 else if (fullPEL || listPEL)
Aatir7b291ec2019-11-19 10:37:37 -0600856 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800857 if (!scrubFile.empty())
858 {
859 scrubRegex = genRegex(scrubFile);
860 }
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800861 printPELs(listPELDescOrd, hidden, includeInfo, fullPEL, scrubRegex,
862 hexDump);
Harisuddin Mohamed Isad4002f32020-02-26 16:19:58 +0800863 }
864 else if (showPELCount)
865 {
Harisuddin Mohamed Isa6f005192020-03-10 17:39:53 +0800866 if (!scrubFile.empty())
867 {
868 scrubRegex = genRegex(scrubFile);
869 }
Harisuddin Mohamed Isa0c57a672020-03-27 14:16:28 +0800870 printPELCount(hidden, includeInfo, scrubRegex);
Aatir7b291ec2019-11-19 10:37:37 -0600871 }
Matt Spinler27de6f32020-02-21 08:35:57 -0600872 else if (!idToDelete.empty())
873 {
Harisuddin Mohamed Isaed32c8d2020-10-01 18:12:39 +0800874 callFunctionOnPEL(idToDelete, deletePEL);
Matt Spinler27de6f32020-02-21 08:35:57 -0600875 }
876 else if (deleteAll)
877 {
878 deleteAllPELs();
879 }
Aatir186ce8c2019-10-20 15:13:39 -0500880 else
881 {
Harisuddin Mohamed Isaa214ed32020-02-28 15:58:23 +0800882 std::cout << app.help("", CLI::AppFormatMode::All) << std::endl;
Aatir186ce8c2019-10-20 15:13:39 -0500883 }
Harisuddin Mohamed Isaf67bafd2020-07-06 17:51:21 +0800884 Py_Finalize();
Aatir186ce8c2019-10-20 15:13:39 -0500885 return 0;
886}