xref: /openbmc/openpower-debug-collector/dump/dump_utils.cpp (revision 540521edd78007b78f8e6df4b38ca71496862f25)
1858d1aafSDhruvaraj Subhashchandran #include "dump_utils.hpp"
2858d1aafSDhruvaraj Subhashchandran 
3858d1aafSDhruvaraj Subhashchandran #include <phosphor-logging/elog-errors.hpp>
4858d1aafSDhruvaraj Subhashchandran #include <phosphor-logging/elog.hpp>
5858d1aafSDhruvaraj Subhashchandran #include <phosphor-logging/lg2.hpp>
6858d1aafSDhruvaraj Subhashchandran #include <phosphor-logging/log.hpp>
7858d1aafSDhruvaraj Subhashchandran #include <xyz/openbmc_project/Common/File/error.hpp>
8858d1aafSDhruvaraj Subhashchandran 
9858d1aafSDhruvaraj Subhashchandran #include <format>
10858d1aafSDhruvaraj Subhashchandran #include <fstream>
11858d1aafSDhruvaraj Subhashchandran #include <string>
12858d1aafSDhruvaraj Subhashchandran 
13858d1aafSDhruvaraj Subhashchandran namespace openpower::dump::util
14858d1aafSDhruvaraj Subhashchandran {
15858d1aafSDhruvaraj Subhashchandran using namespace phosphor::logging;
16858d1aafSDhruvaraj Subhashchandran 
monitorDumpCreation(const std::string & path,const uint32_t timeout)175f5c94d5SDhruvaraj Subhashchandran static void monitorDumpCreation(const std::string& path, const uint32_t timeout)
185f5c94d5SDhruvaraj Subhashchandran {
195f5c94d5SDhruvaraj Subhashchandran     bool inProgress = true;
205f5c94d5SDhruvaraj Subhashchandran     auto bus = sdbusplus::bus::new_system();
219ae780d5SPatrick Williams     auto match = sdbusplus::bus::match_t(
225f5c94d5SDhruvaraj Subhashchandran         bus,
235f5c94d5SDhruvaraj Subhashchandran         sdbusplus::bus::match::rules::propertiesChanged(
245f5c94d5SDhruvaraj Subhashchandran             path, "xyz.openbmc_project.Common.Progress"),
259ae780d5SPatrick Williams         [&](sdbusplus::message_t& msg) {
265f5c94d5SDhruvaraj Subhashchandran             std::string interface;
275f5c94d5SDhruvaraj Subhashchandran             std::map<std::string, std::variant<std::string, uint8_t>> property;
285f5c94d5SDhruvaraj Subhashchandran             msg.read(interface, property);
295f5c94d5SDhruvaraj Subhashchandran 
305f5c94d5SDhruvaraj Subhashchandran             const auto dumpStatus = property.find("Status");
315f5c94d5SDhruvaraj Subhashchandran             if (dumpStatus != property.end())
325f5c94d5SDhruvaraj Subhashchandran             {
335f5c94d5SDhruvaraj Subhashchandran                 const std::string* status =
345f5c94d5SDhruvaraj Subhashchandran                     std::get_if<std::string>(&(dumpStatus->second));
355f5c94d5SDhruvaraj Subhashchandran                 if (status &&
365f5c94d5SDhruvaraj Subhashchandran                     *status !=
375f5c94d5SDhruvaraj Subhashchandran                         "xyz.openbmc_project.Common.Progress.OperationStatus.InProgress")
385f5c94d5SDhruvaraj Subhashchandran                 {
395f5c94d5SDhruvaraj Subhashchandran                     lg2::info("Dump status({STATUS}) : path={PATH}", "STATUS",
405f5c94d5SDhruvaraj Subhashchandran                               status->c_str(), "PATH", path.c_str());
415f5c94d5SDhruvaraj Subhashchandran                     inProgress = false;
425f5c94d5SDhruvaraj Subhashchandran                 }
435f5c94d5SDhruvaraj Subhashchandran             }
445f5c94d5SDhruvaraj Subhashchandran         });
455f5c94d5SDhruvaraj Subhashchandran 
465f5c94d5SDhruvaraj Subhashchandran     // Timeout management
475f5c94d5SDhruvaraj Subhashchandran     for (uint32_t secondsCount = 0; inProgress && secondsCount < timeout;
485f5c94d5SDhruvaraj Subhashchandran          ++secondsCount)
495f5c94d5SDhruvaraj Subhashchandran     {
505f5c94d5SDhruvaraj Subhashchandran         bus.wait(std::chrono::seconds(1));
515f5c94d5SDhruvaraj Subhashchandran         bus.process_discard();
525f5c94d5SDhruvaraj Subhashchandran     }
535f5c94d5SDhruvaraj Subhashchandran 
545f5c94d5SDhruvaraj Subhashchandran     if (inProgress)
555f5c94d5SDhruvaraj Subhashchandran     {
565f5c94d5SDhruvaraj Subhashchandran         lg2::error("Dump progress timeout; dump may not be complete.");
575f5c94d5SDhruvaraj Subhashchandran     }
585f5c94d5SDhruvaraj Subhashchandran }
595f5c94d5SDhruvaraj Subhashchandran 
requestSBEDump(const uint32_t failingUnit,const uint32_t eid,SBETypes sbeType)605f5c94d5SDhruvaraj Subhashchandran void requestSBEDump(const uint32_t failingUnit, const uint32_t eid,
615f5c94d5SDhruvaraj Subhashchandran                     SBETypes sbeType)
625f5c94d5SDhruvaraj Subhashchandran {
63e74e9167SDhruvaraj Subhashchandran     lg2::info(
64e74e9167SDhruvaraj Subhashchandran         "Requesting Dump PEL({EID}) chip({CHIPTYPE}) position({FAILINGUNIT})",
65e74e9167SDhruvaraj Subhashchandran         "EID", eid, "CHIPTYPE", sbeTypeAttributes.at(sbeType).chipName,
66e74e9167SDhruvaraj Subhashchandran         "FAILINGUNIT", failingUnit);
675f5c94d5SDhruvaraj Subhashchandran 
68d48f8e34SDhruvaraj Subhashchandran     constexpr auto path = "/xyz/openbmc_project/dump/system";
69d48f8e34SDhruvaraj Subhashchandran     auto dumpRequestType = sbeTypeAttributes.at(sbeType).dumpType;
705f5c94d5SDhruvaraj Subhashchandran     constexpr auto interface = "xyz.openbmc_project.Dump.Create";
715f5c94d5SDhruvaraj Subhashchandran     constexpr auto function = "CreateDump";
725f5c94d5SDhruvaraj Subhashchandran 
735f5c94d5SDhruvaraj Subhashchandran     try
745f5c94d5SDhruvaraj Subhashchandran     {
755f5c94d5SDhruvaraj Subhashchandran         auto bus = sdbusplus::bus::new_default();
765f5c94d5SDhruvaraj Subhashchandran         auto service = getService(bus, interface, path);
77*540521edSPatrick Williams         auto method =
78*540521edSPatrick Williams             bus.new_method_call(service.c_str(), path, interface, function);
795f5c94d5SDhruvaraj Subhashchandran 
805f5c94d5SDhruvaraj Subhashchandran         std::unordered_map<std::string, std::variant<std::string, uint64_t>>
815f5c94d5SDhruvaraj Subhashchandran             createParams = {
82d48f8e34SDhruvaraj Subhashchandran                 {"com.ibm.Dump.Create.CreateParameters.DumpType",
83d48f8e34SDhruvaraj Subhashchandran                  dumpRequestType},
845f5c94d5SDhruvaraj Subhashchandran                 {"com.ibm.Dump.Create.CreateParameters.ErrorLogId",
855f5c94d5SDhruvaraj Subhashchandran                  uint64_t(eid)},
865f5c94d5SDhruvaraj Subhashchandran                 {"com.ibm.Dump.Create.CreateParameters.FailingUnitId",
875f5c94d5SDhruvaraj Subhashchandran                  uint64_t(failingUnit)}};
885f5c94d5SDhruvaraj Subhashchandran 
895f5c94d5SDhruvaraj Subhashchandran         method.append(createParams);
905f5c94d5SDhruvaraj Subhashchandran         sdbusplus::message::object_path reply;
915f5c94d5SDhruvaraj Subhashchandran         bus.call(method).read(reply);
925f5c94d5SDhruvaraj Subhashchandran 
935f5c94d5SDhruvaraj Subhashchandran         monitorDumpCreation(reply.str, SBE_DUMP_TIMEOUT);
945f5c94d5SDhruvaraj Subhashchandran     }
959ae780d5SPatrick Williams     catch (const sdbusplus::exception_t& e)
965f5c94d5SDhruvaraj Subhashchandran     {
975f5c94d5SDhruvaraj Subhashchandran         lg2::error("D-Bus call createDump exception OBJPATH={OBJPATH}, "
985f5c94d5SDhruvaraj Subhashchandran                    "INTERFACE={INTERFACE}, EXCEPTION={ERROR}",
995f5c94d5SDhruvaraj Subhashchandran                    "OBJPATH", path, "INTERFACE", interface, "ERROR", e);
1005f5c94d5SDhruvaraj Subhashchandran         constexpr auto ERROR_DUMP_DISABLED =
1015f5c94d5SDhruvaraj Subhashchandran             "xyz.openbmc_project.Dump.Create.Error.Disabled";
1025f5c94d5SDhruvaraj Subhashchandran         if (e.name() == ERROR_DUMP_DISABLED)
1035f5c94d5SDhruvaraj Subhashchandran         {
1045f5c94d5SDhruvaraj Subhashchandran             // Dump is disabled, Skip the dump collection.
1055f5c94d5SDhruvaraj Subhashchandran             lg2::info("Dump is disabled unit({FAILINGUNIT}), "
1065f5c94d5SDhruvaraj Subhashchandran                       "skipping dump collection",
1075f5c94d5SDhruvaraj Subhashchandran                       "FAILINGUNIT", failingUnit);
1085f5c94d5SDhruvaraj Subhashchandran         }
1095f5c94d5SDhruvaraj Subhashchandran         else
1105f5c94d5SDhruvaraj Subhashchandran         {
1115f5c94d5SDhruvaraj Subhashchandran             throw;
1125f5c94d5SDhruvaraj Subhashchandran         }
1135f5c94d5SDhruvaraj Subhashchandran     }
1145f5c94d5SDhruvaraj Subhashchandran     catch (const std::exception& e)
1155f5c94d5SDhruvaraj Subhashchandran     {
1165f5c94d5SDhruvaraj Subhashchandran         throw e;
1175f5c94d5SDhruvaraj Subhashchandran     }
1185f5c94d5SDhruvaraj Subhashchandran }
1195f5c94d5SDhruvaraj Subhashchandran 
getService(sdbusplus::bus_t & bus,const std::string & intf,const std::string & path)1209ae780d5SPatrick Williams std::string getService(sdbusplus::bus_t& bus, const std::string& intf,
121858d1aafSDhruvaraj Subhashchandran                        const std::string& path)
122858d1aafSDhruvaraj Subhashchandran {
123858d1aafSDhruvaraj Subhashchandran     constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
124858d1aafSDhruvaraj Subhashchandran     constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
125858d1aafSDhruvaraj Subhashchandran     constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
126858d1aafSDhruvaraj Subhashchandran     try
127858d1aafSDhruvaraj Subhashchandran     {
128858d1aafSDhruvaraj Subhashchandran         auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
129858d1aafSDhruvaraj Subhashchandran                                           MAPPER_INTERFACE, "GetObject");
130858d1aafSDhruvaraj Subhashchandran 
131858d1aafSDhruvaraj Subhashchandran         mapper.append(path, std::vector<std::string>({intf}));
132858d1aafSDhruvaraj Subhashchandran 
133858d1aafSDhruvaraj Subhashchandran         auto mapperResponseMsg = bus.call(mapper);
134858d1aafSDhruvaraj Subhashchandran         std::map<std::string, std::vector<std::string>> mapperResponse;
135858d1aafSDhruvaraj Subhashchandran         mapperResponseMsg.read(mapperResponse);
136858d1aafSDhruvaraj Subhashchandran 
137858d1aafSDhruvaraj Subhashchandran         if (mapperResponse.empty())
138858d1aafSDhruvaraj Subhashchandran         {
139858d1aafSDhruvaraj Subhashchandran             lg2::error(
140858d1aafSDhruvaraj Subhashchandran                 "Empty mapper response for GetObject interface({INTERFACE}), "
141858d1aafSDhruvaraj Subhashchandran                 "path({PATH})",
142858d1aafSDhruvaraj Subhashchandran                 "INTERFACE", intf, "PATH", path);
143858d1aafSDhruvaraj Subhashchandran 
144858d1aafSDhruvaraj Subhashchandran             throw std::runtime_error("Empty mapper response for GetObject");
145858d1aafSDhruvaraj Subhashchandran         }
146858d1aafSDhruvaraj Subhashchandran         return mapperResponse.begin()->first;
147858d1aafSDhruvaraj Subhashchandran     }
1489ae780d5SPatrick Williams     catch (const sdbusplus::exception_t& ex)
149858d1aafSDhruvaraj Subhashchandran     {
150858d1aafSDhruvaraj Subhashchandran         lg2::error(
151858d1aafSDhruvaraj Subhashchandran             "Mapper call failed for GetObject errorMsg({ERROR}), path({PATH}),"
152858d1aafSDhruvaraj Subhashchandran             "interface({INTERFACE})",
153858d1aafSDhruvaraj Subhashchandran             "ERROR", ex, "PATH", path, "INTERFACE", intf);
154858d1aafSDhruvaraj Subhashchandran 
155858d1aafSDhruvaraj Subhashchandran         throw;
156858d1aafSDhruvaraj Subhashchandran     }
157858d1aafSDhruvaraj Subhashchandran }
158858d1aafSDhruvaraj Subhashchandran 
159858d1aafSDhruvaraj Subhashchandran } // namespace openpower::dump::util
160