blob: 3bdf1c27637ef17aeef7b86ee32b040697149d58 [file] [log] [blame]
Ben Tynerb1ebfcb2020-05-08 18:52:48 -05001#include <unistd.h>
2
Ben Tynerb797b3e2020-06-29 10:12:05 -05003#include <attn/attn_logging.hpp>
Ben Tynerb1ebfcb2020-05-08 18:52:48 -05004#include <phosphor-logging/log.hpp>
Ben Tynerb1ebfcb2020-05-08 18:52:48 -05005namespace attn
6{
7
8/** @brief journal entry of type INFO using phosphor logging */
9template <>
10void trace<INFO>(const char* i_message)
11{
12 phosphor::logging::log<phosphor::logging::level::INFO>(i_message);
13}
14
15/** @brief add an event to the log for PEL generation */
16void event(EventType i_event, std::map<std::string, std::string>& i_additional)
17{
18 bool eventValid = false; // assume no event created
19
20 std::string eventName;
21
22 switch (i_event)
23 {
24 case EventType::Checkstop:
25 eventName = "org.open_power.HwDiags.Error.Checkstop";
26 eventValid = true;
27 break;
28 case EventType::Terminate:
29 eventName = "org.open_power.Attn.Error.Terminate";
30 eventValid = true;
31 break;
32 case EventType::Vital:
33 eventName = "org.open_power.Attn.Error.Vital";
34 eventValid = true;
35 break;
36 case EventType::HwDiagsFail:
37 eventName = "org.open_power.HwDiags.Error.Fail";
38 eventValid = true;
39 break;
40 case EventType::AttentionFail:
41 eventName = "org.open_power.Attn.Error.Fail";
42 eventValid = true;
43 break;
44 default:
45 eventValid = false;
46 break;
47 }
48
49 if (true == eventValid)
50 {
51 // Get access to logging interface and method for creating log
52 auto bus = sdbusplus::bus::new_default_system();
53
54 // using direct create method (for additional data)
55 auto method = bus.new_method_call(
56 "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
Ben Tyner1b1915e2020-10-23 15:13:38 -050057 "xyz.openbmc_project.Logging.Create", "CreateWithFFDCFiles");
58
59 // Create FFDC files containing debug data to store in error log
60 std::vector<util::FFDCFile> files{createFFDCFiles()};
61
62 // Create FFDC tuples used to pass FFDC files to D-Bus method
Zane Shelley584f1792020-11-17 10:04:08 -060063 std::vector<util::FFDCTuple> ffdcTuples{};
64 util::transformFFDC(files, ffdcTuples);
Ben Tynerb1ebfcb2020-05-08 18:52:48 -050065
66 // attach additional data
67 method.append(eventName,
68 "xyz.openbmc_project.Logging.Entry.Level.Error",
Ben Tyner1b1915e2020-10-23 15:13:38 -050069 i_additional, ffdcTuples);
Ben Tynerb1ebfcb2020-05-08 18:52:48 -050070
71 // log the event
Ben Tyner1c4b02e2020-11-09 14:00:29 -060072 bus.call_noreply(method);
Ben Tynerb1ebfcb2020-05-08 18:52:48 -050073 }
74}
75
76/** @brief commit checkstop event to log */
77void eventCheckstop(std::map<std::string, std::string>& i_errors)
78{
79 std::map<std::string, std::string> additionalData;
80
81 // TODO need multi-error/multi-callout stuff here
82
83 // if analyzer isolated errors
84 if (!(i_errors.empty()))
85 {
86 // FIXME TEMP CODE - begin
87
88 std::string signature = i_errors.begin()->first;
89 std::string chip = i_errors.begin()->second;
90
91 additionalData["_PID"] = std::to_string(getpid());
92 additionalData["SIGNATURE"] = signature;
93 additionalData["CHIP_ID"] = chip;
94
95 // FIXME TEMP CODE -end
96
97 event(EventType::Checkstop, additionalData);
98 }
99}
100
101/** @brief commit special attention TI event to log */
Ben Tyner40717722020-09-23 09:43:20 -0500102void eventTerminate(std::map<std::string, std::string> i_additionalData)
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500103{
Ben Tyner40717722020-09-23 09:43:20 -0500104 event(EventType::Terminate, i_additionalData);
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500105}
106
107/** @brief commit SBE vital event to log */
108void eventVital()
109{
110 std::map<std::string, std::string> additionalData;
111
112 additionalData["_PID"] = std::to_string(getpid());
113
114 event(EventType::Vital, additionalData);
115}
116
117/** @brief commit analyzer failure event to log */
118void eventHwDiagsFail(int i_error)
119{
120 std::map<std::string, std::string> additionalData;
121
122 additionalData["_PID"] = std::to_string(getpid());
123
124 event(EventType::HwDiagsFail, additionalData);
125}
126
127/** @brief commit attention handler failure event to log */
128void eventAttentionFail(int i_error)
129{
130 std::map<std::string, std::string> additionalData;
131
132 additionalData["_PID"] = std::to_string(getpid());
133 additionalData["ERROR_CODE"] = std::to_string(i_error);
134
135 event(EventType::AttentionFail, additionalData);
136}
137
Ben Tyner1b1915e2020-10-23 15:13:38 -0500138/** @brief parse systemd journal message field */
139std::string sdjGetFieldValue(sd_journal* journal, const char* field)
140{
141 const char* data{nullptr};
142 size_t length{0};
143 size_t prefix{0};
144
145 // get field value
146 if (0 == sd_journal_get_data(journal, field, (const void**)&data, &length))
147 {
148 // The data returned by sd_journal_get_data will be prefixed with the
149 // field name and "="
150 const void* eq = memchr(data, '=', length);
151 if (nullptr != eq)
152 {
153 // get just data following the "="
154 prefix = (const char*)eq - data + 1;
155 }
156 else
157 {
158 // all the data (should not happen)
159 prefix = 0;
160 std::string value{}; // empty string
161 }
162
163 return std::string{data + prefix, length - prefix};
164 }
165 else
166 {
167 return std::string{}; // empty string
168 }
169}
170
171/** @brief get messages from systemd journal */
172std::vector<std::string> sdjGetMessages(const std::string& field,
173 const std::string& fieldValue,
174 unsigned int max)
175{
176 sd_journal* journal;
177 std::vector<std::string> messages;
178
179 if (0 == sd_journal_open(&journal, SD_JOURNAL_LOCAL_ONLY))
180 {
181 SD_JOURNAL_FOREACH_BACKWARDS(journal)
182 {
183 // Get input field
184 std::string value = sdjGetFieldValue(journal, field.c_str());
185
186 // Compare field value and read data
187 if (value == fieldValue)
188 {
189 // Get SYSLOG_IDENTIFIER field (process that logged message)
190 std::string syslog =
191 sdjGetFieldValue(journal, "SYSLOG_IDENTIFIER");
192
193 // Get _PID field
194 std::string pid = sdjGetFieldValue(journal, "_PID");
195
196 // Get MESSAGE field
197 std::string message = sdjGetFieldValue(journal, "MESSAGE");
198
199 // Get timestamp
200 uint64_t usec{0};
201 if (0 == sd_journal_get_realtime_usec(journal, &usec))
202 {
203
204 // Convert realtime microseconds to date format
205 char dateBuffer[80];
206 std::string date;
207 std::time_t timeInSecs = usec / 1000000;
208 strftime(dateBuffer, sizeof(dateBuffer), "%b %d %H:%M:%S",
209 std::localtime(&timeInSecs));
210 date = dateBuffer;
211
212 // Store value to messages
213 value = date + " " + syslog + "[" + pid + "]: " + message;
214 messages.insert(messages.begin(), value);
215 }
216 }
217
218 // limit maximum number of messages
219 if (messages.size() >= max)
220 {
221 break;
222 }
223 }
224
225 sd_journal_close(journal); // close journal when done
226 }
227
228 return messages;
229}
230
231/** @brief create a file containing FFDC data */
232util::FFDCFile createFFDCFile(const std::vector<std::string>& lines)
233{
234 // Create FFDC file of type Text
235 util::FFDCFile file{util::FFDCFormat::Text};
236 int fd = file.getFileDescriptor();
237
238 // Write FFDC lines to file
239 std::string buffer;
240 for (const std::string& line : lines)
241 {
242 // Copy line to buffer. Add newline if necessary.
243 buffer = line;
244 if (line.empty() || (line.back() != '\n'))
245 {
246 buffer += '\n';
247 }
248
249 // write buffer to file
250 write(fd, buffer.c_str(), buffer.size());
251 }
252
253 // Seek to beginning of file so error logging system can read data
254 lseek(fd, 0, SEEK_SET);
255
256 return file;
257}
258
259/** @brief Create FDDC files from journal messages of relevant executables */
260std::vector<util::FFDCFile> createFFDCFiles()
261{
262 std::vector<util::FFDCFile> files{};
263
264 // Executables of interest
265 std::vector<std::string> executables{"openpower-hw-diags"};
266
267 for (const std::string& executable : executables)
268 {
269 try
270 {
271 // get journal messages
272 std::vector<std::string> messages =
273 sdjGetMessages("SYSLOG_IDENTIFIER", executable, 30);
274
275 // Create FFDC file containing the journal messages
276 if (!messages.empty())
277 {
278 files.emplace_back(createFFDCFile(messages));
279 }
280 }
281 catch (const std::exception& e)
282 {
283 std::stringstream ss;
284 ss << "createFFDCFiles: " << e.what();
285 trace<level::INFO>(ss.str().c_str());
286 }
287 }
288
289 return files;
290}
291
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500292} // namespace attn