blob: f8a34b15206ea7c9b76ffd178bcacebbbf6473a5 [file] [log] [blame]
Ben Tynerb1ebfcb2020-05-08 18:52:48 -05001#include <unistd.h>
2
Ben Tyner188f1092021-02-01 09:33:06 -06003#include <attn/attn_dbus.hpp>
Ben Tynerb797b3e2020-06-29 10:12:05 -05004#include <attn/attn_logging.hpp>
Ben Tynerf5210bb2021-01-05 12:58:10 -06005#include <attn/pel/pel_minimal.hpp>
Ben Tynerb1ebfcb2020-05-08 18:52:48 -05006#include <phosphor-logging/log.hpp>
Ben Tynerf5210bb2021-01-05 12:58:10 -06007
Ben Tynerb1ebfcb2020-05-08 18:52:48 -05008namespace attn
9{
10
Ben Tynerf5210bb2021-01-05 12:58:10 -060011/** @brief Journal entry of type INFO using phosphor logging */
Ben Tynerb1ebfcb2020-05-08 18:52:48 -050012template <>
13void trace<INFO>(const char* i_message)
14{
15 phosphor::logging::log<phosphor::logging::level::INFO>(i_message);
16}
17
Ben Tynerd7006092021-02-05 14:55:52 -060018template <>
19void trace<ERROR>(const char* i_message)
20{
21 phosphor::logging::log<phosphor::logging::level::ERR>(i_message);
22}
23
Ben Tynerf5210bb2021-01-05 12:58:10 -060024/** @brief Tuple containing information about ffdc files */
25using FFDCTuple =
26 std::tuple<util::FFDCFormat, uint8_t, uint8_t, sdbusplus::message::unix_fd>;
27
28/** @brief Gather messages from the journal */
29std::vector<std::string> sdjGetMessages(const std::string& field,
30 const std::string& fieldValue,
31 unsigned int max);
32
33/**
34 * Create FFDCTuple objects corresponding to the specified FFDC files.
35 *
36 * The D-Bus method to create an error log requires a vector of tuples to
37 * pass in the FFDC file information.
38 *
39 * @param files - FFDC files
40 * @return vector of FFDCTuple objects
41 */
42std::vector<FFDCTuple>
43 createFFDCTuples(const std::vector<util::FFDCFile>& files)
44{
45 std::vector<FFDCTuple> ffdcTuples{};
Zane Shelleya79f6c82021-01-12 16:38:49 -060046 util::transformFFDC(files, ffdcTuples);
Ben Tynerf5210bb2021-01-05 12:58:10 -060047
48 return ffdcTuples;
49}
50
51/**
52 * @brief Create an FFDCFile object containing raw data
53 *
54 * Throws an exception if an error occurs.
55 *
56 * @param i_buffer - raw data to add to ffdc faw data file
57 * @param i_size - size of the raw data
58 * @return FFDCFile object
59 */
60util::FFDCFile createFFDCRawFile(void* i_buffer, size_t i_size)
61{
62 util::FFDCFile file{util::FFDCFormat::Custom};
63
64 // Write buffer to file and then reset file description file offset
Ben Tynerd7006092021-02-05 14:55:52 -060065 int fd = file.getFileDescriptor();
66 size_t numBytes = write(fd, static_cast<char*>(i_buffer), i_size);
67 if (i_size != numBytes)
68 {
69 std::stringstream traceMsg;
70 traceMsg << file.getPath().c_str() << " only " << (int)numBytes
71 << " of " << (int)i_size << " bytes written";
72 auto strobj = traceMsg.str();
73 trace<level::ERROR>(strobj.c_str());
74 }
75
Ben Tynerf5210bb2021-01-05 12:58:10 -060076 lseek(fd, 0, SEEK_SET);
77
78 return file;
79}
80
81/**
82 * @brief Create an FFDCFile object containing the specified lines of text data
83 *
84 * Throws an exception if an error occurs.
85 *
86 * @param lines - lines of text data to write to file
87 * @return FFDCFile object
88 */
89util::FFDCFile createFFDCTraceFile(const std::vector<std::string>& lines)
90{
91 // Create FFDC file of type Text
92 util::FFDCFile file{util::FFDCFormat::Text};
93 int fd = file.getFileDescriptor();
94
95 // Write FFDC lines to file
96 std::string buffer;
97 for (const std::string& line : lines)
98 {
99 // Copy line to buffer. Add newline if necessary.
100 buffer = line;
101 if (line.empty() || (line.back() != '\n'))
102 {
103 buffer += '\n';
104 }
105
106 // write buffer to file
Ben Tynerd7006092021-02-05 14:55:52 -0600107 size_t numBytes = write(fd, buffer.c_str(), buffer.size());
108 if (buffer.size() != numBytes)
109 {
110 std::stringstream traceMsg;
111 traceMsg << file.getPath().c_str() << " only " << (int)numBytes
112 << " of " << (int)buffer.size() << " bytes written";
113 auto strobj = traceMsg.str();
114 trace<level::ERROR>(strobj.c_str());
115 }
Ben Tynerf5210bb2021-01-05 12:58:10 -0600116 }
117
118 // Seek to beginning of file so error logging system can read data
119 lseek(fd, 0, SEEK_SET);
120
121 return file;
122}
123
124/**
125 * Create FDDC files from journal messages of relevant executables
126 *
127 * Parse the system journal looking for log entries created by the executables
128 * of interest for logging. For each of these entries create a ffdc trace file
129 * that will be used to create ffdc log entries. These files will be pushed
130 * onto the stack of ffdc files.
131 *
132 * @param i_files - vector of ffdc files that will become log entries
133 */
134void createFFDCTraceFiles(std::vector<util::FFDCFile>& i_files)
135{
136 // Executables of interest
137 std::vector<std::string> executables{"openpower-hw-diags"};
138
139 for (const std::string& executable : executables)
140 {
141 try
142 {
143 // get journal messages
144 std::vector<std::string> messages =
145 sdjGetMessages("SYSLOG_IDENTIFIER", executable, 30);
146
147 // Create FFDC file containing the journal messages
148 if (!messages.empty())
149 {
150 i_files.emplace_back(createFFDCTraceFile(messages));
151 }
152 }
153 catch (const std::exception& e)
154 {
Ben Tyner8882c322021-02-05 12:13:21 -0600155 std::string traceMessage =
156 "createFFDCTraceFiles: " + std::string(e.what());
157 trace<level::INFO>(traceMessage.c_str());
Ben Tynerf5210bb2021-01-05 12:58:10 -0600158 }
159 }
160}
161
162/**
163 * Create FFDCFile objects containing debug data to store in the error log.
164 *
165 * If an error occurs, the error is written to the journal but an exception
166 * is not thrown.
167 *
168 * @param i_buffer - raw data (if creating raw dump ffdc entry in log)
169 * @return vector of FFDCFile objects
170 */
171std::vector<util::FFDCFile> createFFDCFiles(char* i_buffer = nullptr,
172 size_t i_size = 0)
173{
174 std::vector<util::FFDCFile> files{};
175
176 // Create raw dump file
177 if ((nullptr != i_buffer) && (0 != i_size))
178 {
179 files.emplace_back(createFFDCRawFile(i_buffer, i_size));
180 }
181
182 // Create trace dump file
183 createFFDCTraceFiles(files);
184
185 return files;
186}
187
188/**
Ben Tynerf5210bb2021-01-05 12:58:10 -0600189 * Create a PEL from an existing PEL
190 *
191 * Create a new PEL based on the specified raw PEL and submit the new PEL
192 * to the backend logging code as a raw PEL. Note that additional data map
193 * here contains data to be committed to the PEL and it can also be used to
194 * create the PEL as it contains needed information.
195 *
196 * @param i_buffer - buffer containing a raw PEL
197 * @param i_additional - additional data to be added to the new PEL
198 */
199void createPelCustom(std::vector<uint8_t>& i_rawPel,
200 std::map<std::string, std::string> i_additional)
201{
202 // create PEL object from buffer
203 auto tiPel = std::make_unique<pel::PelMinimal>(i_rawPel);
204
205 // The additional data contains the TI info as well as the value for the
206 // subystem that provided the TI info. Get the subystem from additional
207 // data and then populate the prmary SRC and SRC words for the custom PEL
208 // based on the sybsystem's TI info.
209 uint8_t subsystem = std::stoi(i_additional["Subsystem"]);
210 tiPel->setSubsystem(subsystem);
211
212 if (static_cast<uint8_t>(pel::SubsystemID::hypervisor) == subsystem)
213 {
214 // populate hypervisor SRC words
215 tiPel->setSrcWords(std::array<uint32_t, pel::numSrcWords>{
216 (uint32_t)std::stoul(i_additional["0x10 SRC Word 12"], 0, 16),
217 (uint32_t)std::stoul(i_additional["0x14 SRC Word 13"], 0, 16),
218 (uint32_t)std::stoul(i_additional["0x18 SRC Word 14"], 0, 16),
219 (uint32_t)std::stoul(i_additional["0x1c SRC Word 15"], 0, 16),
220 (uint32_t)std::stoul(i_additional["0x20 SRC Word 16"], 0, 16),
221 (uint32_t)std::stoul(i_additional["0x24 SRC Word 17"], 0, 16),
222 (uint32_t)std::stoul(i_additional["0x28 SRC Word 18"], 0, 16),
223 (uint32_t)std::stoul(i_additional["0x2c SRC Word 19"], 0, 16)});
224
225 // populate hypervisor primary SRC
226 std::array<char, pel::asciiStringSize> srcChars{'0'};
227 std::string srcString = i_additional["SrcAscii"];
228 srcString.copy(srcChars.data(),
229 std::min(srcString.size(), pel::asciiStringSize), 0);
230 tiPel->setAsciiString(srcChars);
231 }
232 else
233 {
234 // Populate hostboot SRC words - note HB word 0 from the shared info
235 // data (additional data "0x10 HB Word") is reflected in the PEL as
236 // "reason code" so we zero it here. Also note that the first word
237 // in this group of words starts at word 0 and word 1 does not exits.
238 tiPel->setSrcWords(std::array<uint32_t, pel::numSrcWords>{
239 (uint32_t)0x00000000,
240 (uint32_t)std::stoul(i_additional["0x14 HB Word 2"], 0, 16),
241 (uint32_t)std::stoul(i_additional["0x18 HB Word 3"], 0, 16),
242 (uint32_t)std::stoul(i_additional["0x1c HB Word 4"], 0, 16),
243 (uint32_t)std::stoul(i_additional["0x20 HB Word 5"], 0, 16),
244 (uint32_t)std::stoul(i_additional["0x24 HB Word 6"], 0, 16),
245 (uint32_t)std::stoul(i_additional["0x28 HB Word 7"], 0, 16),
246 (uint32_t)std::stoul(i_additional["0x2c HB Word 8"], 0, 16)});
247
248 // populate hostboot primary SRC
249 std::array<char, pel::asciiStringSize> srcChars{'0'};
250 std::string srcString = i_additional["0x30 error_data"];
251 srcString.copy(srcChars.data(),
252 std::min(srcString.size(), pel::asciiStringSize), 0);
253 tiPel->setAsciiString(srcChars);
254 }
255
256 // set severity, event type and action flags
257 tiPel->setSeverity(static_cast<uint8_t>(pel::Severity::termination));
258 tiPel->setType(static_cast<uint8_t>(pel::EventType::na));
259 tiPel->setAction(static_cast<uint16_t>(pel::ActionFlags::service |
260 pel::ActionFlags::report |
261 pel::ActionFlags::call));
262
263 // The raw PEL that we used as the basis for this custom PEL contains the
264 // attention handler trace data and does not needed to be in this PEL so
265 // we remove it here.
266 tiPel->setSectionCount(tiPel->getSectionCount() - 1);
267
268 // Update the raw PEL with the new custom PEL data
269 tiPel->raw(i_rawPel);
270
271 // create PEL from raw data
272 createPelRaw(i_rawPel);
273}
274
275/**
276 * Log an event handled by the attention handler
277 *
278 * Basic (non TI) events will generate a standard message-registry based PEL
279 *
280 * TI events will create two PEL's. One PEL will be informational and will
281 * contain trace information relevent to attention handler. The second PEL
282 * will be specific to the TI type (including the primary SRC) and will be
283 * based off of the TI information provided to the attention handler through
284 * shared TI info data area.
285 *
286 * @param i_event - The event type
287 * @param i_additional - Additional PEL data
288 * @param i_ffdc - FFDC PEL data
289 */
290void event(EventType i_event, std::map<std::string, std::string>& i_additional,
291 const std::vector<util::FFDCFile>& i_ffdc)
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500292{
293 bool eventValid = false; // assume no event created
Ben Tynerf5210bb2021-01-05 12:58:10 -0600294 bool tiEvent = false; // assume not a terminate event
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500295
296 std::string eventName;
297
298 switch (i_event)
299 {
300 case EventType::Checkstop:
301 eventName = "org.open_power.HwDiags.Error.Checkstop";
302 eventValid = true;
303 break;
304 case EventType::Terminate:
305 eventName = "org.open_power.Attn.Error.Terminate";
306 eventValid = true;
Ben Tynerf5210bb2021-01-05 12:58:10 -0600307 tiEvent = true;
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500308 break;
309 case EventType::Vital:
310 eventName = "org.open_power.Attn.Error.Vital";
311 eventValid = true;
312 break;
313 case EventType::HwDiagsFail:
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500314 case EventType::AttentionFail:
315 eventName = "org.open_power.Attn.Error.Fail";
316 eventValid = true;
317 break;
318 default:
319 eventValid = false;
320 break;
321 }
322
323 if (true == eventValid)
324 {
Ben Tynerf5210bb2021-01-05 12:58:10 -0600325 // Create PEL with additional data and FFDC data. The newly created
326 // PEL's platform log-id will be returned.
327 auto pelId =
328 createPel(eventName, i_additional, createFFDCTuples(i_ffdc));
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500329
Ben Tynerf5210bb2021-01-05 12:58:10 -0600330 // If this is a TI event we will create an additional PEL that is
331 // specific to the subsystem that generated the TI.
Ben Tyner29651ef2021-02-08 10:51:03 -0600332 if ((true == tiEvent) && (0 != pelId))
Ben Tynerf5210bb2021-01-05 12:58:10 -0600333 {
334 // get file descriptor and size of information PEL
Ben Tyner188f1092021-02-01 09:33:06 -0600335 int pelFd = getPel(pelId);
Ben Tyner1b1915e2020-10-23 15:13:38 -0500336
Ben Tynerf5210bb2021-01-05 12:58:10 -0600337 // if PEL found, read into buffer
338 if (-1 != pelFd)
339 {
340 auto pelSize = lseek(pelFd, 0, SEEK_END);
341 lseek(pelFd, 0, SEEK_SET);
Ben Tyner1b1915e2020-10-23 15:13:38 -0500342
Ben Tynerf5210bb2021-01-05 12:58:10 -0600343 // read information PEL into buffer
344 std::vector<uint8_t> buffer(pelSize);
Ben Tynerd7006092021-02-05 14:55:52 -0600345 size_t numBytes = read(pelFd, buffer.data(), buffer.size());
346 if (buffer.size() != numBytes)
347 {
348 std::stringstream traceMsg;
349 traceMsg << "Error reading event log: " << (int)numBytes
350 << " of " << (int)buffer.size() << " bytes read";
351 auto strobj = traceMsg.str();
352 trace<level::ERROR>(strobj.c_str());
353 }
354 else
355 {
356 // create PEL from buffer
357 createPelCustom(buffer, i_additional);
358 }
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500359
Ben Tynerd7006092021-02-05 14:55:52 -0600360 close(pelFd);
Ben Tynerf5210bb2021-01-05 12:58:10 -0600361 }
362 }
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500363 }
364}
365
Ben Tynerf5210bb2021-01-05 12:58:10 -0600366/**
367 * Commit special attention TI event to log
368 *
369 * Create a event log with provided additional information and standard
370 * FFDC data plus TI FFDC data
371 *
372 * @param i_additional - Additional log data
373 * @param i_ti_InfoData - TI FFDC data
374 */
375void eventTerminate(std::map<std::string, std::string> i_additionalData,
376 char* i_tiInfoData)
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500377{
Ben Tynerb6401ed2021-01-11 09:08:07 -0600378
Ben Tyner29651ef2021-02-08 10:51:03 -0600379 uint32_t tiInfoSize = 0; // assume TI info was not available
Ben Tynerb6401ed2021-01-11 09:08:07 -0600380
Ben Tyner29651ef2021-02-08 10:51:03 -0600381 if (nullptr != i_tiInfoData)
Ben Tynerb6401ed2021-01-11 09:08:07 -0600382 {
Ben Tyner29651ef2021-02-08 10:51:03 -0600383 tiInfoSize = 56; // assume not hypervisor TI
Ben Tynerb6401ed2021-01-11 09:08:07 -0600384
Ben Tyner29651ef2021-02-08 10:51:03 -0600385 uint8_t subsystem = std::stoi(i_additionalData["Subsystem"]);
386
387 // If hypervisor
388 if (static_cast<uint8_t>(pel::SubsystemID::hypervisor) == subsystem)
Ben Tynerb6401ed2021-01-11 09:08:07 -0600389 {
Ben Tyner29651ef2021-02-08 10:51:03 -0600390 tiInfoSize = 1024; // assume hypervisor max
Ben Tynerb6401ed2021-01-11 09:08:07 -0600391
Ben Tyner29651ef2021-02-08 10:51:03 -0600392 // hypervisor may just want some of the data
393 if (0 == (*(i_tiInfoData + 0x09) & 0x01))
394 {
395 uint32_t* additionalLength = (uint32_t*)(i_tiInfoData + 0x50);
396 uint32_t tiAdditional = be32toh(*additionalLength);
397 tiInfoSize = std::min(tiInfoSize, (84 + tiAdditional));
398 }
Ben Tynerb6401ed2021-01-11 09:08:07 -0600399 }
400 }
401
402 std::string traceMsg = "TI info size = " + std::to_string(tiInfoSize);
403 trace<level::INFO>(traceMsg.c_str());
404
Ben Tynerf5210bb2021-01-05 12:58:10 -0600405 event(EventType::Terminate, i_additionalData,
Ben Tynerb6401ed2021-01-11 09:08:07 -0600406 createFFDCFiles(i_tiInfoData, tiInfoSize));
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500407}
408
Ben Tynerf5210bb2021-01-05 12:58:10 -0600409/** @brief Commit SBE vital event to log */
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500410void eventVital()
411{
Ben Tynerf5210bb2021-01-05 12:58:10 -0600412 // Additional data for log
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500413 std::map<std::string, std::string> additionalData;
414
Ben Tynerf5210bb2021-01-05 12:58:10 -0600415 // Create log event with additional data and FFDC data
416 event(EventType::Vital, additionalData, createFFDCFiles(nullptr, 0));
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500417}
418
Ben Tynerf5210bb2021-01-05 12:58:10 -0600419/**
Ben Tynerf5210bb2021-01-05 12:58:10 -0600420 * Commit attention handler failure event to log
421 *
422 * Create an event log containing the specified error code.
423 *
424 * @param i_error - Error code
425 */
426void eventAttentionFail(int i_error)
427{
428 // Additional data for log
429 std::map<std::string, std::string> additionalData;
430 additionalData["ERROR_CODE"] = std::to_string(i_error);
431
432 // Create log event with additional data and FFDC data
433 event(EventType::AttentionFail, additionalData,
434 createFFDCFiles(nullptr, 0));
435}
436
437/**
438 * Parse systemd journal message field
439 *
440 * Parse the journal looking for the specified field and return the journal
441 * data for that field.
442 *
443 * @param journal - The journal to parse
444 * @param field - Field containing the data to retrieve
445 * @return Data for the speciefied field
446 */
Ben Tyner1b1915e2020-10-23 15:13:38 -0500447std::string sdjGetFieldValue(sd_journal* journal, const char* field)
448{
449 const char* data{nullptr};
450 size_t length{0};
Ben Tyner1b1915e2020-10-23 15:13:38 -0500451
452 // get field value
453 if (0 == sd_journal_get_data(journal, field, (const void**)&data, &length))
454 {
Zane Shelley9fb657f2021-01-12 15:30:58 -0600455 size_t prefix{0};
456
Ben Tyner1b1915e2020-10-23 15:13:38 -0500457 // The data returned by sd_journal_get_data will be prefixed with the
458 // field name and "="
459 const void* eq = memchr(data, '=', length);
460 if (nullptr != eq)
461 {
462 // get just data following the "="
463 prefix = (const char*)eq - data + 1;
464 }
465 else
466 {
467 // all the data (should not happen)
468 prefix = 0;
469 std::string value{}; // empty string
470 }
471
472 return std::string{data + prefix, length - prefix};
473 }
474 else
475 {
476 return std::string{}; // empty string
477 }
478}
479
Ben Tynerf5210bb2021-01-05 12:58:10 -0600480/**
481 * Gather messages from the journal
482 *
483 * Fetch journal entry data for all entries with the specified field equal to
484 * the specified value.
485 *
486 * @param field - Field to search on
487 * @param fieldValue - Value to search for
488 * @param max - Maximum number of messages fetch
489 * @return Vector of journal entry data
490 */
Ben Tyner1b1915e2020-10-23 15:13:38 -0500491std::vector<std::string> sdjGetMessages(const std::string& field,
492 const std::string& fieldValue,
493 unsigned int max)
494{
495 sd_journal* journal;
496 std::vector<std::string> messages;
497
498 if (0 == sd_journal_open(&journal, SD_JOURNAL_LOCAL_ONLY))
499 {
500 SD_JOURNAL_FOREACH_BACKWARDS(journal)
501 {
502 // Get input field
503 std::string value = sdjGetFieldValue(journal, field.c_str());
504
505 // Compare field value and read data
506 if (value == fieldValue)
507 {
508 // Get SYSLOG_IDENTIFIER field (process that logged message)
509 std::string syslog =
510 sdjGetFieldValue(journal, "SYSLOG_IDENTIFIER");
511
512 // Get _PID field
513 std::string pid = sdjGetFieldValue(journal, "_PID");
514
515 // Get MESSAGE field
516 std::string message = sdjGetFieldValue(journal, "MESSAGE");
517
518 // Get timestamp
519 uint64_t usec{0};
520 if (0 == sd_journal_get_realtime_usec(journal, &usec))
521 {
522
523 // Convert realtime microseconds to date format
524 char dateBuffer[80];
525 std::string date;
526 std::time_t timeInSecs = usec / 1000000;
527 strftime(dateBuffer, sizeof(dateBuffer), "%b %d %H:%M:%S",
528 std::localtime(&timeInSecs));
529 date = dateBuffer;
530
531 // Store value to messages
532 value = date + " " + syslog + "[" + pid + "]: " + message;
533 messages.insert(messages.begin(), value);
534 }
535 }
536
537 // limit maximum number of messages
538 if (messages.size() >= max)
539 {
540 break;
541 }
542 }
543
544 sd_journal_close(journal); // close journal when done
545 }
546
547 return messages;
548}
549
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500550} // namespace attn