xref: /openbmc/phosphor-debug-collector/core_manager.cpp (revision 1615b824a14e7c676e932abacb6d1ffe71b61d6a)
1cb65ffceSJayanth Othayoth #include "config.h"
2d02153c9SJayanth Othayoth 
3d02153c9SJayanth Othayoth #include "core_manager.hpp"
4cb65ffceSJayanth Othayoth 
5d1f670feSDhruvaraj Subhashchandran #include <phosphor-logging/lg2.hpp>
6cb65ffceSJayanth Othayoth #include <sdbusplus/exception.hpp>
7d02153c9SJayanth Othayoth 
80af74a5eSJayanth Othayoth #include <filesystem>
90af74a5eSJayanth Othayoth #include <regex>
100af74a5eSJayanth Othayoth 
11d02153c9SJayanth Othayoth namespace phosphor
12d02153c9SJayanth Othayoth {
13d02153c9SJayanth Othayoth namespace dump
14d02153c9SJayanth Othayoth {
15d02153c9SJayanth Othayoth namespace core
16d02153c9SJayanth Othayoth {
17d02153c9SJayanth Othayoth 
18d3273eadSJayanth Othayoth using namespace std;
19d3273eadSJayanth Othayoth 
watchCallback(const UserMap & fileInfo)20bf6ec600SJayanth Othayoth void Manager::watchCallback(const UserMap& fileInfo)
21d02153c9SJayanth Othayoth {
22d3273eadSJayanth Othayoth     vector<string> files;
23d3273eadSJayanth Othayoth 
24d02153c9SJayanth Othayoth     for (const auto& i : fileInfo)
25d02153c9SJayanth Othayoth     {
263fc6df48SJayanth Othayoth         std::filesystem::path file(i.first);
279a56bfa9SJayanth Othayoth         std::string name = file.filename();
289a56bfa9SJayanth Othayoth 
299a56bfa9SJayanth Othayoth         /*
309a56bfa9SJayanth Othayoth           As per coredump source code systemd-coredump uses below format
319a56bfa9SJayanth Othayoth           https://github.com/systemd/systemd/blob/master/src/coredump/coredump.c
329a56bfa9SJayanth Othayoth           /var/lib/systemd/coredump/core.%s.%s." SD_ID128_FORMAT_STR “
339a56bfa9SJayanth Othayoth           systemd-coredump also creates temporary file in core file path prior
349a56bfa9SJayanth Othayoth           to actual core file creation. Checking the file name format will help
359a56bfa9SJayanth Othayoth           to limit dump creation only for the new core files.
369a56bfa9SJayanth Othayoth         */
379a56bfa9SJayanth Othayoth         if ("core" == name.substr(0, name.find('.')))
389a56bfa9SJayanth Othayoth         {
399a56bfa9SJayanth Othayoth             // Consider only file name start with "core."
409a56bfa9SJayanth Othayoth             files.push_back(file);
41d02153c9SJayanth Othayoth         }
42d02153c9SJayanth Othayoth     }
439a56bfa9SJayanth Othayoth 
44d3273eadSJayanth Othayoth     if (!files.empty())
45d3273eadSJayanth Othayoth     {
46d3273eadSJayanth Othayoth         createHelper(files);
47d3273eadSJayanth Othayoth     }
48d3273eadSJayanth Othayoth }
49d3273eadSJayanth Othayoth 
createHelper(const vector<string> & files)50bf6ec600SJayanth Othayoth void Manager::createHelper(const vector<string>& files)
51d3273eadSJayanth Othayoth {
52d3273eadSJayanth Othayoth     constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
53d3273eadSJayanth Othayoth     constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
54d3273eadSJayanth Othayoth     constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
55247159b0SDhruvaraj Subhashchandran     constexpr auto DUMP_CREATE_IFACE = "xyz.openbmc_project.Dump.Create";
56d3273eadSJayanth Othayoth 
57d3273eadSJayanth Othayoth     auto b = sdbusplus::bus::new_default();
58cb65ffceSJayanth Othayoth     auto mapper = b.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
59cb65ffceSJayanth Othayoth                                     MAPPER_INTERFACE, "GetObject");
60247159b0SDhruvaraj Subhashchandran     mapper.append(BMC_DUMP_OBJPATH, vector<string>({DUMP_CREATE_IFACE}));
61d3273eadSJayanth Othayoth 
62d3273eadSJayanth Othayoth     map<string, vector<string>> mapperResponse;
6315cd3ce7SWilliam A. Kennington III     try
6415cd3ce7SWilliam A. Kennington III     {
650eadeb7eSLei YU         auto mapperResponseMsg = b.call(mapper);
66d3273eadSJayanth Othayoth         mapperResponseMsg.read(mapperResponse);
6715cd3ce7SWilliam A. Kennington III     }
689b18bf2dSPatrick Williams     catch (const sdbusplus::exception_t& e)
6915cd3ce7SWilliam A. Kennington III     {
70*1615b824SDhruvaraj Subhashchandran         lg2::error("Failed to GetObject on Dump.Create: {ERROR}", "ERROR", e);
7115cd3ce7SWilliam A. Kennington III         return;
7215cd3ce7SWilliam A. Kennington III     }
73d3273eadSJayanth Othayoth     if (mapperResponse.empty())
74d3273eadSJayanth Othayoth     {
75d1f670feSDhruvaraj Subhashchandran         lg2::error("Error reading mapper response");
76d3273eadSJayanth Othayoth         return;
77d3273eadSJayanth Othayoth     }
78d3273eadSJayanth Othayoth 
79d3273eadSJayanth Othayoth     const auto& host = mapperResponse.cbegin()->first;
80247159b0SDhruvaraj Subhashchandran     auto m = b.new_method_call(host.c_str(), BMC_DUMP_OBJPATH,
81247159b0SDhruvaraj Subhashchandran                                DUMP_CREATE_IFACE, "CreateDump");
82247159b0SDhruvaraj Subhashchandran     phosphor::dump::DumpCreateParams params;
83247159b0SDhruvaraj Subhashchandran     using CreateParameters =
84247159b0SDhruvaraj Subhashchandran         sdbusplus::common::xyz::openbmc_project::dump::Create::CreateParameters;
85247159b0SDhruvaraj Subhashchandran     using DumpType =
86247159b0SDhruvaraj Subhashchandran         sdbusplus::common::xyz::openbmc_project::dump::Create::DumpType;
87247159b0SDhruvaraj Subhashchandran     using DumpIntr = sdbusplus::common::xyz::openbmc_project::dump::Create;
88247159b0SDhruvaraj Subhashchandran     params[DumpIntr::convertCreateParametersToString(
89247159b0SDhruvaraj Subhashchandran         CreateParameters::DumpType)] =
90247159b0SDhruvaraj Subhashchandran         DumpIntr::convertDumpTypeToString(DumpType::ApplicationCored);
91247159b0SDhruvaraj Subhashchandran     params[DumpIntr::convertCreateParametersToString(
92247159b0SDhruvaraj Subhashchandran         CreateParameters::FilePath)] = files.front();
93247159b0SDhruvaraj Subhashchandran     m.append(params);
940eadeb7eSLei YU     try
950eadeb7eSLei YU     {
96d3273eadSJayanth Othayoth         b.call_noreply(m);
97d02153c9SJayanth Othayoth     }
989b18bf2dSPatrick Williams     catch (const sdbusplus::exception_t& e)
990eadeb7eSLei YU     {
100d1f670feSDhruvaraj Subhashchandran         lg2::error("Failed to create dump: {ERROR}", "ERROR", e);
1010eadeb7eSLei YU     }
1020eadeb7eSLei YU }
103d02153c9SJayanth Othayoth 
104d02153c9SJayanth Othayoth } // namespace core
105d02153c9SJayanth Othayoth } // namespace dump
106d02153c9SJayanth Othayoth } // namespace phosphor
107