blob: 57250cc576a5c88069a50c3384a368c8234fc131 [file] [log] [blame]
George Liuff92ffe2021-02-09 15:01:53 +08001#include "config.h"
2
3#include "ramoops_manager.hpp"
4
Dhruvaraj Subhashchandran7d7e0012023-06-29 05:35:07 -05005#include "dump_manager.hpp"
6
Andrew Geissler7d069302023-11-09 13:25:31 -06007#include <phosphor-logging/elog-errors.hpp>
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -05008#include <phosphor-logging/lg2.hpp>
Dhruvaraj Subhashchandran0b566d52023-06-14 09:47:38 -05009#include <sdbusplus/bus.hpp>
George Liuff92ffe2021-02-09 15:01:53 +080010#include <sdbusplus/exception.hpp>
Dhruvaraj Subhashchandran7d7e0012023-06-29 05:35:07 -050011#include <xyz/openbmc_project/Dump/Create/common.hpp>
12#include <xyz/openbmc_project/Dump/Create/server.hpp>
George Liuff92ffe2021-02-09 15:01:53 +080013
George Liu2a6835d2021-10-11 18:59:09 +080014#include <filesystem>
Dhruvaraj Subhashchandran0b566d52023-06-14 09:47:38 -050015#include <set>
George Liu2a6835d2021-10-11 18:59:09 +080016
George Liuff92ffe2021-02-09 15:01:53 +080017namespace phosphor
18{
19namespace dump
20{
21namespace ramoops
22{
23
24Manager::Manager(const std::string& filePath)
25{
Patrick Williamse9ec9522025-02-05 16:49:52 -050026 std::filesystem::path dir(filePath);
27 if (!std::filesystem::exists(dir) || std::filesystem::is_empty(dir))
George Liu2a6835d2021-10-11 18:59:09 +080028 {
29 return;
30 }
31
Andrew Geissler7d069302023-11-09 13:25:31 -060032 // Create error to notify user that a ramoops has been detected
33 createError();
34
George Liuff92ffe2021-02-09 15:01:53 +080035 std::vector<std::string> files;
36 files.push_back(filePath);
37
38 createHelper(files);
39}
40
Andrew Geissler7d069302023-11-09 13:25:31 -060041void Manager::createError()
42{
43 try
44 {
45 std::map<std::string, std::string> additionalData;
46
47 // Always add the _PID on for some extra logging debug
48 additionalData.emplace("_PID", std::to_string(getpid()));
49
50 auto bus = sdbusplus::bus::new_default();
51 auto method = bus.new_method_call(
52 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
53 "xyz.openbmc_project.Logging.Create", "Create");
54
55 method.append("xyz.openbmc_project.Dump.Error.Ramoops",
56 sdbusplus::server::xyz::openbmc_project::logging::Entry::
57 Level::Error,
58 additionalData);
59 auto resp = bus.call(method);
60 }
61 catch (const sdbusplus::exception_t& e)
62 {
63 lg2::error(
64 "sdbusplus D-Bus call exception, error {ERROR} trying to create "
65 "an error for ramoops detection",
66 "ERROR", e);
67 // This is a best-effort logging situation so don't throw anything
68 }
69 catch (const std::exception& e)
70 {
71 lg2::error("D-bus call exception: {ERROR}", "ERROR", e);
72 // This is a best-effort logging situation so don't throw anything
73 }
74}
75
George Liuff92ffe2021-02-09 15:01:53 +080076void Manager::createHelper(const std::vector<std::string>& files)
77{
George Liuff92ffe2021-02-09 15:01:53 +080078 constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
79 constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
80 constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
Dhruvaraj Subhashchandran7d7e0012023-06-29 05:35:07 -050081 constexpr auto DUMP_CREATE_IFACE = "xyz.openbmc_project.Dump.Create";
George Liuff92ffe2021-02-09 15:01:53 +080082
83 auto b = sdbusplus::bus::new_default();
84 auto mapper = b.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
85 MAPPER_INTERFACE, "GetObject");
Dhruvaraj Subhashchandran7d7e0012023-06-29 05:35:07 -050086 mapper.append(BMC_DUMP_OBJPATH, std::set<std::string>({DUMP_CREATE_IFACE}));
George Liuff92ffe2021-02-09 15:01:53 +080087
George Liuff92ffe2021-02-09 15:01:53 +080088 std::map<std::string, std::set<std::string>> mapperResponse;
89 try
90 {
George Liuf4694d72021-08-16 13:49:09 +080091 auto mapperResponseMsg = b.call(mapper);
George Liuff92ffe2021-02-09 15:01:53 +080092 mapperResponseMsg.read(mapperResponse);
93 }
Patrick Williams9b18bf22022-07-22 19:26:55 -050094 catch (const sdbusplus::exception_t& e)
George Liuff92ffe2021-02-09 15:01:53 +080095 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050096 lg2::error("Failed to parse dump create message, error: {ERROR}",
97 "ERROR", e);
George Liuff92ffe2021-02-09 15:01:53 +080098 return;
99 }
100 if (mapperResponse.empty())
101 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500102 lg2::error("Error reading mapper response");
George Liuff92ffe2021-02-09 15:01:53 +0800103 return;
104 }
105
106 const auto& host = mapperResponse.cbegin()->first;
Dhruvaraj Subhashchandran7d7e0012023-06-29 05:35:07 -0500107 auto m = b.new_method_call(host.c_str(), BMC_DUMP_OBJPATH,
108 DUMP_CREATE_IFACE, "CreateDump");
109 phosphor::dump::DumpCreateParams params;
110 using CreateParameters =
111 sdbusplus::common::xyz::openbmc_project::dump::Create::CreateParameters;
112 using DumpType =
113 sdbusplus::common::xyz::openbmc_project::dump::Create::DumpType;
114 using DumpIntr = sdbusplus::common::xyz::openbmc_project::dump::Create;
115 params[DumpIntr::convertCreateParametersToString(
116 CreateParameters::DumpType)] =
117 DumpIntr::convertDumpTypeToString(DumpType::Ramoops);
118 params[DumpIntr::convertCreateParametersToString(
119 CreateParameters::FilePath)] = files.front();
120 m.append(params);
George Liuf4694d72021-08-16 13:49:09 +0800121 try
122 {
123 b.call_noreply(m);
124 }
Patrick Williams9b18bf22022-07-22 19:26:55 -0500125 catch (const sdbusplus::exception_t& e)
George Liuf4694d72021-08-16 13:49:09 +0800126 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500127 lg2::error("Failed to create ramoops dump, errormsg: {ERROR}", "ERROR",
128 e);
George Liuf4694d72021-08-16 13:49:09 +0800129 }
George Liuff92ffe2021-02-09 15:01:53 +0800130}
131
132} // namespace ramoops
133} // namespace dump
134} // namespace phosphor