xref: /openbmc/phosphor-debug-collector/dump-extensions/openpower-dumps/dump_manager_system.cpp (revision f37c5c3b3e2785c51241bd34f700364406e8e9a4)
1 #include "config.h"
2 
3 #include "dump_manager_system.hpp"
4 
5 #include "system_dump_entry.hpp"
6 #include "xyz/openbmc_project/Common/error.hpp"
7 
8 #include <phosphor-logging/elog-errors.hpp>
9 #include <phosphor-logging/elog.hpp>
10 
11 namespace phosphor
12 {
13 namespace dump
14 {
15 namespace system
16 {
17 
18 using namespace phosphor::logging;
19 using namespace sdbusplus::xyz::openbmc_project::Common::Error;
20 
21 void Manager::notify(uint32_t dumpId, uint64_t size)
22 {
23 
24     // Get the timestamp
25     std::time_t timeStamp = std::time(nullptr);
26 
27     // System dump can get created due to a fault in server
28     // or by request from user. A system dump by fault is
29     // first reported here, but for a user requested dump an
30     // entry will be created first with invalid source id.
31     // Since there can be only one system dump creation at a time,
32     // if there is an entry with invalid sourceId update that.
33     for (auto& entry : entries)
34     {
35         phosphor::dump::system::Entry* sysEntry =
36             dynamic_cast<phosphor::dump::system::Entry*>(entry.second.get());
37         if (sysEntry->sourceDumpId() == INVALID_SOURCE_ID)
38         {
39             sysEntry->update(timeStamp, size, dumpId);
40             return;
41         }
42     }
43 
44     // Get the id
45     auto id = lastEntryId + 1;
46     auto idString = std::to_string(id);
47     auto objPath = fs::path(baseEntryPath) / idString;
48 
49     try
50     {
51         entries.insert(std::make_pair(
52             id, std::make_unique<system::Entry>(
53                     bus, objPath.c_str(), id, timeStamp, size, dumpId,
54                     phosphor::dump::OperationStatus::Completed, *this)));
55     }
56     catch (const std::invalid_argument& e)
57     {
58         log<level::ERR>(e.what());
59         log<level::ERR>("Error in creating system dump entry",
60                         entry("OBJECTPATH=%s", objPath.c_str()),
61                         entry("ID=%d", id), entry("TIMESTAMP=%ull", timeStamp),
62                         entry("SIZE=%d", size), entry("SOURCEID=%d", dumpId));
63         report<InternalFailure>();
64         return;
65     }
66     lastEntryId++;
67     return;
68 }
69 
70 sdbusplus::message::object_path
71     Manager::createDump(std::map<std::string, std::string> params)
72 {
73     constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
74     constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
75     constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
76     constexpr auto DIAG_MOD_TARGET = "obmc-host-diagnostic-mode@0.target";
77 
78     if (!params.empty())
79     {
80         log<level::WARNING>("System dump accepts no additional parameters");
81     }
82 
83     auto b = sdbusplus::bus::new_default();
84     auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
85                                       SYSTEMD_INTERFACE, "StartUnit");
86     method.append(DIAG_MOD_TARGET); // unit to activate
87     method.append("replace");
88     bus.call_noreply(method);
89 
90     auto id = lastEntryId + 1;
91     auto idString = std::to_string(id);
92     auto objPath = fs::path(baseEntryPath) / idString;
93     std::time_t timeStamp = std::time(nullptr);
94 
95     try
96     {
97         entries.insert(std::make_pair(
98             id, std::make_unique<system::Entry>(
99                     bus, objPath.c_str(), id, timeStamp, 0, INVALID_SOURCE_ID,
100                     phosphor::dump::OperationStatus::InProgress, *this)));
101     }
102     catch (const std::invalid_argument& e)
103     {
104         log<level::ERR>(e.what());
105         log<level::ERR>("Error in creating system dump entry",
106                         entry("OBJECTPATH=%s", objPath.c_str()),
107                         entry("ID=%d", id));
108         elog<InternalFailure>();
109         return std::string();
110     }
111     lastEntryId++;
112     return objPath.string();
113 }
114 
115 } // namespace system
116 } // namespace dump
117 } // namespace phosphor
118