blob: acd35982ee777c68bbe554349dd916d5da1cf4e9 [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{
George Liu2a6835d2021-10-11 18:59:09 +080026 namespace fs = std::filesystem;
27
28 fs::path dir(filePath);
29 if (!fs::exists(dir) || fs::is_empty(dir))
30 {
31 return;
32 }
33
Andrew Geissler7d069302023-11-09 13:25:31 -060034 // Create error to notify user that a ramoops has been detected
35 createError();
36
George Liuff92ffe2021-02-09 15:01:53 +080037 std::vector<std::string> files;
38 files.push_back(filePath);
39
40 createHelper(files);
41}
42
Andrew Geissler7d069302023-11-09 13:25:31 -060043void Manager::createError()
44{
45 try
46 {
47 std::map<std::string, std::string> additionalData;
48
49 // Always add the _PID on for some extra logging debug
50 additionalData.emplace("_PID", std::to_string(getpid()));
51
52 auto bus = sdbusplus::bus::new_default();
53 auto method = bus.new_method_call(
54 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
55 "xyz.openbmc_project.Logging.Create", "Create");
56
57 method.append("xyz.openbmc_project.Dump.Error.Ramoops",
58 sdbusplus::server::xyz::openbmc_project::logging::Entry::
59 Level::Error,
60 additionalData);
61 auto resp = bus.call(method);
62 }
63 catch (const sdbusplus::exception_t& e)
64 {
65 lg2::error(
66 "sdbusplus D-Bus call exception, error {ERROR} trying to create "
67 "an error for ramoops detection",
68 "ERROR", e);
69 // This is a best-effort logging situation so don't throw anything
70 }
71 catch (const std::exception& e)
72 {
73 lg2::error("D-bus call exception: {ERROR}", "ERROR", e);
74 // This is a best-effort logging situation so don't throw anything
75 }
76}
77
George Liuff92ffe2021-02-09 15:01:53 +080078void Manager::createHelper(const std::vector<std::string>& files)
79{
George Liuff92ffe2021-02-09 15:01:53 +080080 constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
81 constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
82 constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
Dhruvaraj Subhashchandran7d7e0012023-06-29 05:35:07 -050083 constexpr auto DUMP_CREATE_IFACE = "xyz.openbmc_project.Dump.Create";
George Liuff92ffe2021-02-09 15:01:53 +080084
85 auto b = sdbusplus::bus::new_default();
86 auto mapper = b.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
87 MAPPER_INTERFACE, "GetObject");
Dhruvaraj Subhashchandran7d7e0012023-06-29 05:35:07 -050088 mapper.append(BMC_DUMP_OBJPATH, std::set<std::string>({DUMP_CREATE_IFACE}));
George Liuff92ffe2021-02-09 15:01:53 +080089
George Liuff92ffe2021-02-09 15:01:53 +080090 std::map<std::string, std::set<std::string>> mapperResponse;
91 try
92 {
George Liuf4694d72021-08-16 13:49:09 +080093 auto mapperResponseMsg = b.call(mapper);
George Liuff92ffe2021-02-09 15:01:53 +080094 mapperResponseMsg.read(mapperResponse);
95 }
Patrick Williams9b18bf22022-07-22 19:26:55 -050096 catch (const sdbusplus::exception_t& e)
George Liuff92ffe2021-02-09 15:01:53 +080097 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050098 lg2::error("Failed to parse dump create message, error: {ERROR}",
99 "ERROR", e);
George Liuff92ffe2021-02-09 15:01:53 +0800100 return;
101 }
102 if (mapperResponse.empty())
103 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500104 lg2::error("Error reading mapper response");
George Liuff92ffe2021-02-09 15:01:53 +0800105 return;
106 }
107
108 const auto& host = mapperResponse.cbegin()->first;
Dhruvaraj Subhashchandran7d7e0012023-06-29 05:35:07 -0500109 auto m = b.new_method_call(host.c_str(), BMC_DUMP_OBJPATH,
110 DUMP_CREATE_IFACE, "CreateDump");
111 phosphor::dump::DumpCreateParams params;
112 using CreateParameters =
113 sdbusplus::common::xyz::openbmc_project::dump::Create::CreateParameters;
114 using DumpType =
115 sdbusplus::common::xyz::openbmc_project::dump::Create::DumpType;
116 using DumpIntr = sdbusplus::common::xyz::openbmc_project::dump::Create;
117 params[DumpIntr::convertCreateParametersToString(
118 CreateParameters::DumpType)] =
119 DumpIntr::convertDumpTypeToString(DumpType::Ramoops);
120 params[DumpIntr::convertCreateParametersToString(
121 CreateParameters::FilePath)] = files.front();
122 m.append(params);
George Liuf4694d72021-08-16 13:49:09 +0800123 try
124 {
125 b.call_noreply(m);
126 }
Patrick Williams9b18bf22022-07-22 19:26:55 -0500127 catch (const sdbusplus::exception_t& e)
George Liuf4694d72021-08-16 13:49:09 +0800128 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500129 lg2::error("Failed to create ramoops dump, errormsg: {ERROR}", "ERROR",
130 e);
George Liuf4694d72021-08-16 13:49:09 +0800131 }
George Liuff92ffe2021-02-09 15:01:53 +0800132}
133
134} // namespace ramoops
135} // namespace dump
136} // namespace phosphor