blob: ae9aaa5de1e15ee7da6c56c4175a8a69f1a189ff [file] [log] [blame]
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -05001#include "config.h"
2
3#include "dump_manager_system.hpp"
4
Dhruvaraj Subhashchandran6a54d9a2020-12-17 22:24:37 -06005#include "dump_utils.hpp"
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -05006#include "system_dump_entry.hpp"
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -05007#include "xyz/openbmc_project/Common/error.hpp"
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -05008
George Liu858fbb22021-07-01 12:25:44 +08009#include <fmt/core.h>
10
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050011#include <phosphor-logging/elog-errors.hpp>
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050012#include <phosphor-logging/elog.hpp>
13
Dhruvaraj Subhashchandran341d6832021-01-15 06:28:04 -060014namespace openpower
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050015{
16namespace dump
17{
18namespace system
19{
20
21using namespace phosphor::logging;
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050022using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050023
Dhruvaraj Subhashchandranf37c5c32020-12-17 22:08:19 -060024void Manager::notify(uint32_t dumpId, uint64_t size)
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050025{
26
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050027 // Get the timestamp
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -050028 std::time_t timeStamp = std::time(nullptr);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050029
30 // System dump can get created due to a fault in server
31 // or by request from user. A system dump by fault is
32 // first reported here, but for a user requested dump an
33 // entry will be created first with invalid source id.
34 // Since there can be only one system dump creation at a time,
35 // if there is an entry with invalid sourceId update that.
36 for (auto& entry : entries)
37 {
Dhruvaraj Subhashchandran341d6832021-01-15 06:28:04 -060038 openpower::dump::system::Entry* sysEntry =
39 dynamic_cast<openpower::dump::system::Entry*>(entry.second.get());
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050040 if (sysEntry->sourceDumpId() == INVALID_SOURCE_ID)
41 {
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -050042 sysEntry->update(timeStamp, size, dumpId);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050043 return;
44 }
45 }
46
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050047 // Get the id
48 auto id = lastEntryId + 1;
49 auto idString = std::to_string(id);
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -050050 auto objPath = std::filesystem::path(baseEntryPath) / idString;
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050051
52 try
53 {
54 entries.insert(std::make_pair(
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -050055 id, std::make_unique<system::Entry>(
56 bus, objPath.c_str(), id, timeStamp, size, dumpId,
57 phosphor::dump::OperationStatus::Completed, *this)));
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050058 }
59 catch (const std::invalid_argument& e)
60 {
George Liu858fbb22021-07-01 12:25:44 +080061 log<level::ERR>(
62 fmt::format(
63 "Error in creating system dump entry, errormsg({}), "
64 "OBJECTPATH({}), ID({}), TIMESTAMP({}),SIZE({}), SOURCEID({})",
George Liu363af242021-07-16 17:52:36 +080065 e.what(), objPath.c_str(), id, timeStamp, size, dumpId)
George Liu858fbb22021-07-01 12:25:44 +080066 .c_str());
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050067 report<InternalFailure>();
68 return;
69 }
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050070 lastEntryId++;
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050071 return;
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050072}
73
Dhruvaraj Subhashchandran969f9a52020-10-30 01:42:39 -050074sdbusplus::message::object_path
Dhruvaraj Subhashchandranddc33662021-07-19 09:28:42 -050075 Manager::createDump(phosphor::dump::DumpCreateParams params)
Dhruvaraj Subhashchandran7040bce2020-09-16 00:50:19 -050076{
77 constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
78 constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
79 constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
Andrew Geissler24e0c592021-01-19 16:47:27 -060080 constexpr auto DIAG_MOD_TARGET = "obmc-host-crash@0.target";
Dhruvaraj Subhashchandran969f9a52020-10-30 01:42:39 -050081
82 if (!params.empty())
83 {
84 log<level::WARNING>("System dump accepts no additional parameters");
85 }
86
Dhruvaraj Subhashchandran6a54d9a2020-12-17 22:24:37 -060087 using NotAllowed =
88 sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
89 using Reason = xyz::openbmc_project::Common::NotAllowed::REASON;
90
91 // Allow creating system dump only when the host is up.
92 if (!phosphor::dump::isHostRunning())
93 {
94 elog<NotAllowed>(
95 Reason("System dump can be initiated only when the host is up"));
96 return std::string();
97 }
98
Dhruvaraj Subhashchandran7040bce2020-09-16 00:50:19 -050099 auto b = sdbusplus::bus::new_default();
100 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
101 SYSTEMD_INTERFACE, "StartUnit");
102 method.append(DIAG_MOD_TARGET); // unit to activate
103 method.append("replace");
104 bus.call_noreply(method);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500105
106 auto id = lastEntryId + 1;
107 auto idString = std::to_string(id);
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500108 auto objPath = std::filesystem::path(baseEntryPath) / idString;
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -0500109 std::time_t timeStamp = std::time(nullptr);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500110
111 try
112 {
113 entries.insert(std::make_pair(
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -0500114 id, std::make_unique<system::Entry>(
115 bus, objPath.c_str(), id, timeStamp, 0, INVALID_SOURCE_ID,
116 phosphor::dump::OperationStatus::InProgress, *this)));
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500117 }
118 catch (const std::invalid_argument& e)
119 {
George Liu858fbb22021-07-01 12:25:44 +0800120 log<level::ERR>(
121 fmt::format("Error in creating system dump entry, errormsg({}), "
122 "OBJECTPATH({}), ID({})",
George Liu363af242021-07-16 17:52:36 +0800123 e.what(), objPath.c_str(), id)
George Liu858fbb22021-07-01 12:25:44 +0800124 .c_str());
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500125 elog<InternalFailure>();
126 return std::string();
127 }
128 lastEntryId++;
129 return objPath.string();
Dhruvaraj Subhashchandran7040bce2020-09-16 00:50:19 -0500130}
131
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500132} // namespace system
133} // namespace dump
Dhruvaraj Subhashchandran341d6832021-01-15 06:28:04 -0600134} // namespace openpower