blob: fad494736196d70a5dc76e6d81b3f206e7e014cb [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 Subhashchandranad50d422022-01-18 05:54:02 -06006#include "op_dump_consts.hpp"
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -05007#include "system_dump_entry.hpp"
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -05008#include "xyz/openbmc_project/Common/error.hpp"
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -05009
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050010#include <phosphor-logging/elog-errors.hpp>
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050011#include <phosphor-logging/elog.hpp>
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -050012#include <phosphor-logging/lg2.hpp>
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050013
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{
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050026 // Get the timestamp
Claire Weinanc0ab9d42022-08-17 23:01:07 -070027 uint64_t timeStamp =
28 std::chrono::duration_cast<std::chrono::microseconds>(
29 std::chrono::system_clock::now().time_since_epoch())
30 .count();
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050031
Dhruvaraj Subhashchandran0007b702022-02-01 03:23:14 -060032 // A system dump can be created due to a fault in the server or by a user
33 // request. A system dump by fault is first reported here, but for a
34 // user-requested dump, an entry will be created first with an invalid
35 // source id. Since only one system dump creation is allowed at a time, if
36 // there's an entry with an invalid sourceId, we will update that entry.
37 openpower::dump::system::Entry* upEntry = nullptr;
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050038 for (auto& entry : entries)
39 {
Dhruvaraj Subhashchandran341d6832021-01-15 06:28:04 -060040 openpower::dump::system::Entry* sysEntry =
41 dynamic_cast<openpower::dump::system::Entry*>(entry.second.get());
Dhruvaraj Subhashchandran0007b702022-02-01 03:23:14 -060042
43 // If there's already a completed entry with the input source id and
44 // size, ignore this notification
45 if ((sysEntry->sourceDumpId() == dumpId) && (sysEntry->size() == size))
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050046 {
Dhruvaraj Subhashchandran0007b702022-02-01 03:23:14 -060047 if (sysEntry->status() ==
48 phosphor::dump::OperationStatus::Completed)
49 {
50 lg2::info(
51 "System dump entry with source dump id:{SOURCE_ID} and "
52 "size: {SIZE} is already present with entry id:{ID}",
53 "SOURCE_ID", dumpId, "SIZE", size, "ID",
54 sysEntry->getDumpId());
55 return;
56 }
57 else
58 {
59 lg2::error("A duplicate notification for an incomplete dump "
60 "dump id: {SOURCE_ID} entry id: {ID}",
61 "SOURCE_D", dumpId, "ID", sysEntry->getDumpId());
62 upEntry = sysEntry;
63 break;
64 }
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050065 }
Dhruvaraj Subhashchandran0007b702022-02-01 03:23:14 -060066 else if (sysEntry->sourceDumpId() == dumpId)
67 {
68 // If the dump id is the same but the size is different, then this
69 // is a new dump. So, delete the stale entry and prepare to create a
70 // new one.
71 lg2::info("A previous dump entry found with same source id: "
72 "{SOURCE_ID}, deleting it, entry id: {DUMP_ID}",
73 "SOURCE_ID", dumpId, "DUMP_ID", sysEntry->getDumpId());
74 sysEntry->delete_();
75 // No 'break' here, as we need to continue checking other entries.
76 }
77
78 // Save the first entry with INVALID_SOURCE_ID, but continue in the loop
79 // to ensure the new entry is not a duplicate.
80 if ((sysEntry->sourceDumpId() == INVALID_SOURCE_ID) &&
81 (upEntry == nullptr))
82 {
83 upEntry = sysEntry;
84 }
85 }
86
87 if (upEntry != nullptr)
88 {
89 lg2::info(
90 "System Dump Notify: Updating dumpId:{ID} Source Id:{SOURCE_ID} "
91 "Size:{SIZE}",
92 "ID", upEntry->getDumpId(), "SOURCE_ID", dumpId, "SIZE", size);
93 upEntry->update(timeStamp, size, dumpId);
94 return;
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -050095 }
96
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -050097 // Get the id
98 auto id = lastEntryId + 1;
99 auto idString = std::to_string(id);
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500100 auto objPath = std::filesystem::path(baseEntryPath) / idString;
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500101
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -0500102 // TODO: Get the originator Id, Type from the persisted file.
103 // For now replacing it with null
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500104 try
105 {
Dhruvaraj Subhashchandran0007b702022-02-01 03:23:14 -0600106 lg2::info("System Dump Notify: creating new dump "
107 "entry dumpId:{ID} Source Id:{SOURCE_ID} Size:{SIZE}",
108 "ID", id, "SOURCE_ID", dumpId, "SIZE", size);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500109 entries.insert(std::make_pair(
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -0500110 id, std::make_unique<system::Entry>(
111 bus, objPath.c_str(), id, timeStamp, size, dumpId,
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -0500112 phosphor::dump::OperationStatus::Completed, std::string(),
113 originatorTypes::Internal, *this)));
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500114 }
115 catch (const std::invalid_argument& e)
116 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500117 lg2::error(
118 "Error in creating system dump entry, errormsg: {ERROR}, "
119 "OBJECTPATH: {OBJECT_PATH}, ID: {ID}, TIMESTAMP: {TIMESTAMP}, "
120 "SIZE: {SIZE}, SOURCEID: {SOURCE_ID}",
121 "ERROR", e, "OBJECT_PATH", objPath, "ID", id, "TIMESTAMP",
122 timeStamp, "SIZE", size, "SOURCE_ID", dumpId);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500123 report<InternalFailure>();
124 return;
125 }
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500126 lastEntryId++;
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500127 return;
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500128}
129
Dhruvaraj Subhashchandran969f9a52020-10-30 01:42:39 -0500130sdbusplus::message::object_path
Dhruvaraj Subhashchandranddc33662021-07-19 09:28:42 -0500131 Manager::createDump(phosphor::dump::DumpCreateParams params)
Dhruvaraj Subhashchandran7040bce2020-09-16 00:50:19 -0500132{
133 constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
134 constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
135 constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
Andrew Geissler24e0c592021-01-19 16:47:27 -0600136 constexpr auto DIAG_MOD_TARGET = "obmc-host-crash@0.target";
Dhruvaraj Subhashchandran969f9a52020-10-30 01:42:39 -0500137
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -0500138 if (params.size() > CREATE_DUMP_MAX_PARAMS)
Dhruvaraj Subhashchandran969f9a52020-10-30 01:42:39 -0500139 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500140 lg2::warning(
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -0500141 "System dump accepts not more than 2 additional parameters");
Dhruvaraj Subhashchandran969f9a52020-10-30 01:42:39 -0500142 }
143
Dhruvaraj Subhashchandran6a54d9a2020-12-17 22:24:37 -0600144 using NotAllowed =
145 sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
146 using Reason = xyz::openbmc_project::Common::NotAllowed::REASON;
147
148 // Allow creating system dump only when the host is up.
149 if (!phosphor::dump::isHostRunning())
150 {
151 elog<NotAllowed>(
152 Reason("System dump can be initiated only when the host is up"));
153 return std::string();
154 }
155
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -0500156 // Get the originator id and type from params
157 std::string originatorId;
158 originatorTypes originatorType;
159
160 phosphor::dump::extractOriginatorProperties(params, originatorId,
161 originatorType);
162
Dhruvaraj Subhashchandran7040bce2020-09-16 00:50:19 -0500163 auto b = sdbusplus::bus::new_default();
164 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
165 SYSTEMD_INTERFACE, "StartUnit");
166 method.append(DIAG_MOD_TARGET); // unit to activate
167 method.append("replace");
168 bus.call_noreply(method);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500169
170 auto id = lastEntryId + 1;
171 auto idString = std::to_string(id);
Jayanth Othayoth3fc6df42021-04-08 03:45:24 -0500172 auto objPath = std::filesystem::path(baseEntryPath) / idString;
Claire Weinanc0ab9d42022-08-17 23:01:07 -0700173 uint64_t timeStamp =
174 std::chrono::duration_cast<std::chrono::microseconds>(
175 std::chrono::system_clock::now().time_since_epoch())
176 .count();
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500177
178 try
179 {
180 entries.insert(std::make_pair(
Dhruvaraj Subhashchandrana6ab8062020-10-29 15:29:10 -0500181 id, std::make_unique<system::Entry>(
182 bus, objPath.c_str(), id, timeStamp, 0, INVALID_SOURCE_ID,
Asmitha Karunanithi74a1f392021-10-27 03:23:59 -0500183 phosphor::dump::OperationStatus::InProgress, originatorId,
184 originatorType, *this)));
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500185 }
186 catch (const std::invalid_argument& e)
187 {
Dhruvaraj Subhashchandrand1f670f2023-06-05 22:19:25 -0500188 lg2::error("Error in creating system dump entry, errormsg: {ERROR}, "
189 "OBJECTPATH: {OBJECT_PATH}, ID: {ID}",
190 "ERROR", e, "OBJECT_PATH", objPath, "ID", id);
Dhruvaraj Subhashchandran6ccb50e2020-10-29 09:33:18 -0500191 elog<InternalFailure>();
192 return std::string();
193 }
194 lastEntryId++;
195 return objPath.string();
Dhruvaraj Subhashchandran7040bce2020-09-16 00:50:19 -0500196}
197
Dhruvaraj Subhashchandranfef66a92020-09-06 13:10:59 -0500198} // namespace system
199} // namespace dump
Dhruvaraj Subhashchandran341d6832021-01-15 06:28:04 -0600200} // namespace openpower