blob: 3fb0572fb94c16585ced0e4f42d64a43735b5786 [file] [log] [blame]
Ben Tynerb1ebfcb2020-05-08 18:52:48 -05001#include <unistd.h>
2
Ben Tyner135793a2021-10-27 09:18:41 -05003#include <analyzer/analyzer_main.hpp>
Ben Tyner5c5db652021-02-22 18:22:35 -06004#include <attn/attn_common.hpp>
Ben Tyner188f1092021-02-01 09:33:06 -06005#include <attn/attn_dbus.hpp>
Ben Tyner7029e522021-08-09 19:18:24 -05006#include <attn/attn_dump.hpp>
Ben Tynerb797b3e2020-06-29 10:12:05 -05007#include <attn/attn_logging.hpp>
Ben Tynerf5210bb2021-01-05 12:58:10 -06008#include <attn/pel/pel_minimal.hpp>
Ben Tynerb1ebfcb2020-05-08 18:52:48 -05009#include <phosphor-logging/log.hpp>
Ben Tynerf5210bb2021-01-05 12:58:10 -060010
Ben Tynerb1ebfcb2020-05-08 18:52:48 -050011namespace attn
12{
13
Ben Tynerf5210bb2021-01-05 12:58:10 -060014/** @brief Journal entry of type INFO using phosphor logging */
Ben Tynerb1ebfcb2020-05-08 18:52:48 -050015template <>
16void trace<INFO>(const char* i_message)
17{
18 phosphor::logging::log<phosphor::logging::level::INFO>(i_message);
19}
20
Ben Tynerd7006092021-02-05 14:55:52 -060021template <>
22void trace<ERROR>(const char* i_message)
23{
24 phosphor::logging::log<phosphor::logging::level::ERR>(i_message);
25}
26
Ben Tynerf5210bb2021-01-05 12:58:10 -060027/** @brief Tuple containing information about ffdc files */
28using FFDCTuple =
29 std::tuple<util::FFDCFormat, uint8_t, uint8_t, sdbusplus::message::unix_fd>;
30
31/** @brief Gather messages from the journal */
32std::vector<std::string> sdjGetMessages(const std::string& field,
33 const std::string& fieldValue,
34 unsigned int max);
35
36/**
37 * Create FFDCTuple objects corresponding to the specified FFDC files.
38 *
39 * The D-Bus method to create an error log requires a vector of tuples to
40 * pass in the FFDC file information.
41 *
42 * @param files - FFDC files
43 * @return vector of FFDCTuple objects
44 */
45std::vector<FFDCTuple>
46 createFFDCTuples(const std::vector<util::FFDCFile>& files)
47{
48 std::vector<FFDCTuple> ffdcTuples{};
Zane Shelleya79f6c82021-01-12 16:38:49 -060049 util::transformFFDC(files, ffdcTuples);
Ben Tynerf5210bb2021-01-05 12:58:10 -060050
51 return ffdcTuples;
52}
53
54/**
55 * @brief Create an FFDCFile object containing raw data
56 *
57 * Throws an exception if an error occurs.
58 *
59 * @param i_buffer - raw data to add to ffdc faw data file
60 * @param i_size - size of the raw data
61 * @return FFDCFile object
62 */
63util::FFDCFile createFFDCRawFile(void* i_buffer, size_t i_size)
64{
65 util::FFDCFile file{util::FFDCFormat::Custom};
66
67 // Write buffer to file and then reset file description file offset
Ben Tynerd7006092021-02-05 14:55:52 -060068 int fd = file.getFileDescriptor();
69 size_t numBytes = write(fd, static_cast<char*>(i_buffer), i_size);
70 if (i_size != numBytes)
71 {
72 std::stringstream traceMsg;
73 traceMsg << file.getPath().c_str() << " only " << (int)numBytes
74 << " of " << (int)i_size << " bytes written";
75 auto strobj = traceMsg.str();
76 trace<level::ERROR>(strobj.c_str());
77 }
78
Ben Tynerf5210bb2021-01-05 12:58:10 -060079 lseek(fd, 0, SEEK_SET);
80
81 return file;
82}
83
84/**
85 * @brief Create an FFDCFile object containing the specified lines of text data
86 *
87 * Throws an exception if an error occurs.
88 *
89 * @param lines - lines of text data to write to file
90 * @return FFDCFile object
91 */
92util::FFDCFile createFFDCTraceFile(const std::vector<std::string>& lines)
93{
94 // Create FFDC file of type Text
95 util::FFDCFile file{util::FFDCFormat::Text};
96 int fd = file.getFileDescriptor();
97
98 // Write FFDC lines to file
99 std::string buffer;
100 for (const std::string& line : lines)
101 {
102 // Copy line to buffer. Add newline if necessary.
103 buffer = line;
104 if (line.empty() || (line.back() != '\n'))
105 {
106 buffer += '\n';
107 }
108
109 // write buffer to file
Ben Tynerd7006092021-02-05 14:55:52 -0600110 size_t numBytes = write(fd, buffer.c_str(), buffer.size());
111 if (buffer.size() != numBytes)
112 {
113 std::stringstream traceMsg;
114 traceMsg << file.getPath().c_str() << " only " << (int)numBytes
115 << " of " << (int)buffer.size() << " bytes written";
116 auto strobj = traceMsg.str();
117 trace<level::ERROR>(strobj.c_str());
118 }
Ben Tynerf5210bb2021-01-05 12:58:10 -0600119 }
120
121 // Seek to beginning of file so error logging system can read data
122 lseek(fd, 0, SEEK_SET);
123
124 return file;
125}
126
127/**
128 * Create FDDC files from journal messages of relevant executables
129 *
130 * Parse the system journal looking for log entries created by the executables
131 * of interest for logging. For each of these entries create a ffdc trace file
132 * that will be used to create ffdc log entries. These files will be pushed
133 * onto the stack of ffdc files.
134 *
135 * @param i_files - vector of ffdc files that will become log entries
136 */
137void createFFDCTraceFiles(std::vector<util::FFDCFile>& i_files)
138{
139 // Executables of interest
140 std::vector<std::string> executables{"openpower-hw-diags"};
141
142 for (const std::string& executable : executables)
143 {
144 try
145 {
146 // get journal messages
147 std::vector<std::string> messages =
148 sdjGetMessages("SYSLOG_IDENTIFIER", executable, 30);
149
150 // Create FFDC file containing the journal messages
151 if (!messages.empty())
152 {
153 i_files.emplace_back(createFFDCTraceFile(messages));
154 }
155 }
156 catch (const std::exception& e)
157 {
Ben Tyner6764d702021-02-12 09:17:23 -0600158 trace<level::INFO>("createFFDCTraceFiles exception");
159 std::string traceMsg = std::string(e.what(), maxTraceLen);
160 trace<level::INFO>(traceMsg.c_str());
Ben Tynerf5210bb2021-01-05 12:58:10 -0600161 }
162 }
163}
164
165/**
166 * Create FFDCFile objects containing debug data to store in the error log.
167 *
168 * If an error occurs, the error is written to the journal but an exception
169 * is not thrown.
170 *
171 * @param i_buffer - raw data (if creating raw dump ffdc entry in log)
172 * @return vector of FFDCFile objects
173 */
174std::vector<util::FFDCFile> createFFDCFiles(char* i_buffer = nullptr,
175 size_t i_size = 0)
176{
177 std::vector<util::FFDCFile> files{};
178
179 // Create raw dump file
180 if ((nullptr != i_buffer) && (0 != i_size))
181 {
182 files.emplace_back(createFFDCRawFile(i_buffer, i_size));
183 }
184
185 // Create trace dump file
186 createFFDCTraceFiles(files);
187
188 return files;
189}
190
191/**
Ben Tynerf5210bb2021-01-05 12:58:10 -0600192 * Create a PEL from an existing PEL
193 *
194 * Create a new PEL based on the specified raw PEL and submit the new PEL
195 * to the backend logging code as a raw PEL. Note that additional data map
196 * here contains data to be committed to the PEL and it can also be used to
197 * create the PEL as it contains needed information.
198 *
Ben Tyner135793a2021-10-27 09:18:41 -0500199 * @param i_rawPel - buffer containing a raw PEL
Ben Tynerf5210bb2021-01-05 12:58:10 -0600200 * @param i_additional - additional data to be added to the new PEL
201 */
202void createPelCustom(std::vector<uint8_t>& i_rawPel,
203 std::map<std::string, std::string> i_additional)
204{
205 // create PEL object from buffer
206 auto tiPel = std::make_unique<pel::PelMinimal>(i_rawPel);
207
208 // The additional data contains the TI info as well as the value for the
209 // subystem that provided the TI info. Get the subystem from additional
210 // data and then populate the prmary SRC and SRC words for the custom PEL
211 // based on the sybsystem's TI info.
212 uint8_t subsystem = std::stoi(i_additional["Subsystem"]);
213 tiPel->setSubsystem(subsystem);
214
Ben Tyner135793a2021-10-27 09:18:41 -0500215 // If recoverable attentions are active we will call the analyzer and
216 // then link the custom pel to analyzer pel.
217 std::map<std::string, std::string>::iterator it;
218 it = i_additional.find("recoverables");
219 if (it != i_additional.end() && "true" == it->second)
220 {
221 DumpParameters dumpParameters;
222 if (analyzer::analyzeHardware(dumpParameters))
223 {
224 tiPel->setPlid(dumpParameters.logId);
225 }
226 }
227
Ben Tynerf5210bb2021-01-05 12:58:10 -0600228 if (static_cast<uint8_t>(pel::SubsystemID::hypervisor) == subsystem)
229 {
230 // populate hypervisor SRC words
231 tiPel->setSrcWords(std::array<uint32_t, pel::numSrcWords>{
232 (uint32_t)std::stoul(i_additional["0x10 SRC Word 12"], 0, 16),
233 (uint32_t)std::stoul(i_additional["0x14 SRC Word 13"], 0, 16),
234 (uint32_t)std::stoul(i_additional["0x18 SRC Word 14"], 0, 16),
235 (uint32_t)std::stoul(i_additional["0x1c SRC Word 15"], 0, 16),
236 (uint32_t)std::stoul(i_additional["0x20 SRC Word 16"], 0, 16),
237 (uint32_t)std::stoul(i_additional["0x24 SRC Word 17"], 0, 16),
238 (uint32_t)std::stoul(i_additional["0x28 SRC Word 18"], 0, 16),
239 (uint32_t)std::stoul(i_additional["0x2c SRC Word 19"], 0, 16)});
240
Ben Tyner9d4f91c2021-02-09 08:27:58 -0600241 // Populate phyp primary SRC
242
243 // char array for raw pel src
Ben Tynerf5210bb2021-01-05 12:58:10 -0600244 std::array<char, pel::asciiStringSize> srcChars{'0'};
Ben Tyner9d4f91c2021-02-09 08:27:58 -0600245
246 // src from TI info
Ben Tynerf5210bb2021-01-05 12:58:10 -0600247 std::string srcString = i_additional["SrcAscii"];
Ben Tyner9d4f91c2021-02-09 08:27:58 -0600248
249 // copy from string to char array
Ben Tynerf5210bb2021-01-05 12:58:10 -0600250 srcString.copy(srcChars.data(),
251 std::min(srcString.size(), pel::asciiStringSize), 0);
Ben Tyner9d4f91c2021-02-09 08:27:58 -0600252
253 tiPel->setAsciiString(srcChars); // pel object src is char array
Ben Tynerfeeea832021-04-06 10:08:11 -0500254
255 // set symptom-id
256 auto symptomId = (i_additional["SrcAscii"].substr(0, 8) + '_');
257
258 symptomId += (i_additional["0x10 SRC Word 12"]);
259 symptomId += (i_additional["0x14 SRC Word 13"] + '_');
260 symptomId += (i_additional["0x18 SRC Word 14"]);
261 symptomId += (i_additional["0x1c SRC Word 15"] + '_');
262 symptomId += (i_additional["0x20 SRC Word 16"]);
263 symptomId += (i_additional["0x24 SRC Word 17"] + '_');
264 symptomId += (i_additional["0x28 SRC Word 18"]);
265 symptomId += (i_additional["0x2c SRC Word 19"]);
266
267 // setSymptomId will take care of required null-terminate and padding
268 tiPel->setSymptomId(symptomId);
Ben Tynerf5210bb2021-01-05 12:58:10 -0600269 }
270 else
271 {
272 // Populate hostboot SRC words - note HB word 0 from the shared info
273 // data (additional data "0x10 HB Word") is reflected in the PEL as
274 // "reason code" so we zero it here. Also note that the first word
275 // in this group of words starts at word 0 and word 1 does not exits.
276 tiPel->setSrcWords(std::array<uint32_t, pel::numSrcWords>{
277 (uint32_t)0x00000000,
278 (uint32_t)std::stoul(i_additional["0x14 HB Word 2"], 0, 16),
279 (uint32_t)std::stoul(i_additional["0x18 HB Word 3"], 0, 16),
280 (uint32_t)std::stoul(i_additional["0x1c HB Word 4"], 0, 16),
281 (uint32_t)std::stoul(i_additional["0x20 HB Word 5"], 0, 16),
282 (uint32_t)std::stoul(i_additional["0x24 HB Word 6"], 0, 16),
283 (uint32_t)std::stoul(i_additional["0x28 HB Word 7"], 0, 16),
284 (uint32_t)std::stoul(i_additional["0x2c HB Word 8"], 0, 16)});
285
Ben Tyner9d4f91c2021-02-09 08:27:58 -0600286 // Populate hostboot primary SRC
287
288 // char array for raw pel src
Ben Tynerf5210bb2021-01-05 12:58:10 -0600289 std::array<char, pel::asciiStringSize> srcChars{'0'};
Ben Tyner9d4f91c2021-02-09 08:27:58 -0600290
291 // src from TI info
292 std::string srcString = i_additional["SrcAscii"];
293
294 // copy from string to char array
Ben Tynerf5210bb2021-01-05 12:58:10 -0600295 srcString.copy(srcChars.data(),
296 std::min(srcString.size(), pel::asciiStringSize), 0);
Ben Tyner9d4f91c2021-02-09 08:27:58 -0600297
298 tiPel->setAsciiString(srcChars); // pel object src is char array
Ben Tynerfeeea832021-04-06 10:08:11 -0500299
300 // set symptom-id
301 auto symptomId = (i_additional["SrcAscii"].substr(0, 8) + '_');
302
303 symptomId += (i_additional["0x10 HB Word 0"]); // note: word 1
304 symptomId += (i_additional["0x14 HB Word 2"] + '_'); // does not exist
305 symptomId += (i_additional["0x18 HB Word 3"]);
306 symptomId += (i_additional["0x1c HB Word 4"] + '_');
307 symptomId += (i_additional["0x20 HB Word 5"]);
308 symptomId += (i_additional["0x24 HB Word 6"] + '_');
309 symptomId += (i_additional["0x28 HB Word 7"]);
310 symptomId += (i_additional["0x2c HB Word 8"]);
311
312 // setSymptomId will take care of required null-terminate and padding
313 tiPel->setSymptomId(symptomId);
Ben Tynerf5210bb2021-01-05 12:58:10 -0600314 }
315
316 // set severity, event type and action flags
317 tiPel->setSeverity(static_cast<uint8_t>(pel::Severity::termination));
318 tiPel->setType(static_cast<uint8_t>(pel::EventType::na));
319 tiPel->setAction(static_cast<uint16_t>(pel::ActionFlags::service |
320 pel::ActionFlags::report |
321 pel::ActionFlags::call));
322
323 // The raw PEL that we used as the basis for this custom PEL contains the
324 // attention handler trace data and does not needed to be in this PEL so
325 // we remove it here.
326 tiPel->setSectionCount(tiPel->getSectionCount() - 1);
327
328 // Update the raw PEL with the new custom PEL data
329 tiPel->raw(i_rawPel);
330
331 // create PEL from raw data
332 createPelRaw(i_rawPel);
333}
334
335/**
336 * Log an event handled by the attention handler
337 *
338 * Basic (non TI) events will generate a standard message-registry based PEL
339 *
340 * TI events will create two PEL's. One PEL will be informational and will
341 * contain trace information relevent to attention handler. The second PEL
342 * will be specific to the TI type (including the primary SRC) and will be
343 * based off of the TI information provided to the attention handler through
344 * shared TI info data area.
345 *
346 * @param i_event - The event type
347 * @param i_additional - Additional PEL data
348 * @param i_ffdc - FFDC PEL data
Ben Tyner7f6ce6a2021-08-17 19:40:00 -0500349 * @return Event log Id (0 if no event log generated)
Ben Tynerf5210bb2021-01-05 12:58:10 -0600350 */
Ben Tyner7f6ce6a2021-08-17 19:40:00 -0500351uint32_t event(EventType i_event,
352 std::map<std::string, std::string>& i_additional,
353 const std::vector<util::FFDCFile>& i_ffdc)
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500354{
Ben Tyner7f6ce6a2021-08-17 19:40:00 -0500355 uint32_t pelId = 0; // assume no event log generated
356
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500357 bool eventValid = false; // assume no event created
Ben Tynerf5210bb2021-01-05 12:58:10 -0600358 bool tiEvent = false; // assume not a terminate event
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500359
360 std::string eventName;
361
362 switch (i_event)
363 {
364 case EventType::Checkstop:
365 eventName = "org.open_power.HwDiags.Error.Checkstop";
366 eventValid = true;
367 break;
368 case EventType::Terminate:
369 eventName = "org.open_power.Attn.Error.Terminate";
370 eventValid = true;
Ben Tynerf5210bb2021-01-05 12:58:10 -0600371 tiEvent = true;
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500372 break;
373 case EventType::Vital:
374 eventName = "org.open_power.Attn.Error.Vital";
375 eventValid = true;
376 break;
377 case EventType::HwDiagsFail:
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500378 case EventType::AttentionFail:
379 eventName = "org.open_power.Attn.Error.Fail";
380 eventValid = true;
381 break;
Ben Tynerb9715172021-09-29 08:46:19 -0500382 case EventType::PhalSbeChipop:
383 eventName = "org.open_power.Processor.Error.SbeChipOpFailure";
384 eventValid = true;
385 break;
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500386 default:
387 eventValid = false;
388 break;
389 }
390
391 if (true == eventValid)
392 {
Ben Tynerf5210bb2021-01-05 12:58:10 -0600393 // Create PEL with additional data and FFDC data. The newly created
394 // PEL's platform log-id will be returned.
Ben Tyner7f6ce6a2021-08-17 19:40:00 -0500395 pelId = createPel(eventName, i_additional, createFFDCTuples(i_ffdc));
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500396
Ben Tynerf5210bb2021-01-05 12:58:10 -0600397 // If this is a TI event we will create an additional PEL that is
398 // specific to the subsystem that generated the TI.
Ben Tyner135793a2021-10-27 09:18:41 -0500399 if ((0 != pelId) && (true == tiEvent))
Ben Tynerf5210bb2021-01-05 12:58:10 -0600400 {
401 // get file descriptor and size of information PEL
Ben Tyner188f1092021-02-01 09:33:06 -0600402 int pelFd = getPel(pelId);
Ben Tyner1b1915e2020-10-23 15:13:38 -0500403
Ben Tynerf5210bb2021-01-05 12:58:10 -0600404 // if PEL found, read into buffer
405 if (-1 != pelFd)
406 {
407 auto pelSize = lseek(pelFd, 0, SEEK_END);
408 lseek(pelFd, 0, SEEK_SET);
Ben Tyner1b1915e2020-10-23 15:13:38 -0500409
Ben Tynerf5210bb2021-01-05 12:58:10 -0600410 // read information PEL into buffer
411 std::vector<uint8_t> buffer(pelSize);
Ben Tynerd7006092021-02-05 14:55:52 -0600412 size_t numBytes = read(pelFd, buffer.data(), buffer.size());
413 if (buffer.size() != numBytes)
414 {
415 std::stringstream traceMsg;
416 traceMsg << "Error reading event log: " << (int)numBytes
417 << " of " << (int)buffer.size() << " bytes read";
418 auto strobj = traceMsg.str();
419 trace<level::ERROR>(strobj.c_str());
420 }
421 else
422 {
423 // create PEL from buffer
424 createPelCustom(buffer, i_additional);
425 }
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500426
Ben Tynerd7006092021-02-05 14:55:52 -0600427 close(pelFd);
Ben Tynerf5210bb2021-01-05 12:58:10 -0600428 }
Ben Tyner5c5db652021-02-22 18:22:35 -0600429
Ben Tyner6bc43c92021-05-27 15:08:02 -0500430 uint8_t subsystem = std::stoi(i_additional["Subsystem"]);
Ben Tyner5c5db652021-02-22 18:22:35 -0600431
Ben Tyner6bc43c92021-05-27 15:08:02 -0500432 // If not hypervisor TI
433 if (static_cast<uint8_t>(pel::SubsystemID::hypervisor) != subsystem)
Ben Tyner5c5db652021-02-22 18:22:35 -0600434 {
Ben Tyner6bc43c92021-05-27 15:08:02 -0500435 // Request a dump and transition the host
436 if ("true" == i_additional["Dump"])
437 {
438 // will not return until dump is complete
Ben Tyner7029e522021-08-09 19:18:24 -0500439 requestDump(DumpParameters{pelId, 0, DumpType::Hostboot});
Ben Tyner6bc43c92021-05-27 15:08:02 -0500440 }
Ben Tyner5c5db652021-02-22 18:22:35 -0600441 }
442 }
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500443 }
Ben Tyner7f6ce6a2021-08-17 19:40:00 -0500444 return pelId;
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500445}
446
Ben Tynerf5210bb2021-01-05 12:58:10 -0600447/**
448 * Commit special attention TI event to log
449 *
450 * Create a event log with provided additional information and standard
451 * FFDC data plus TI FFDC data
452 *
453 * @param i_additional - Additional log data
454 * @param i_ti_InfoData - TI FFDC data
455 */
456void eventTerminate(std::map<std::string, std::string> i_additionalData,
457 char* i_tiInfoData)
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500458{
Ben Tynerb6401ed2021-01-11 09:08:07 -0600459
Ben Tyner29651ef2021-02-08 10:51:03 -0600460 uint32_t tiInfoSize = 0; // assume TI info was not available
Ben Tynerb6401ed2021-01-11 09:08:07 -0600461
Ben Tyner29651ef2021-02-08 10:51:03 -0600462 if (nullptr != i_tiInfoData)
Ben Tynerb6401ed2021-01-11 09:08:07 -0600463 {
Ben Tyner29651ef2021-02-08 10:51:03 -0600464 tiInfoSize = 56; // assume not hypervisor TI
Ben Tynerb6401ed2021-01-11 09:08:07 -0600465
Ben Tyner29651ef2021-02-08 10:51:03 -0600466 uint8_t subsystem = std::stoi(i_additionalData["Subsystem"]);
467
468 // If hypervisor
469 if (static_cast<uint8_t>(pel::SubsystemID::hypervisor) == subsystem)
Ben Tynerb6401ed2021-01-11 09:08:07 -0600470 {
Ben Tyner29651ef2021-02-08 10:51:03 -0600471 tiInfoSize = 1024; // assume hypervisor max
Ben Tynerb6401ed2021-01-11 09:08:07 -0600472
Ben Tyner29651ef2021-02-08 10:51:03 -0600473 // hypervisor may just want some of the data
474 if (0 == (*(i_tiInfoData + 0x09) & 0x01))
475 {
476 uint32_t* additionalLength = (uint32_t*)(i_tiInfoData + 0x50);
477 uint32_t tiAdditional = be32toh(*additionalLength);
478 tiInfoSize = std::min(tiInfoSize, (84 + tiAdditional));
479 }
Ben Tynerb6401ed2021-01-11 09:08:07 -0600480 }
481 }
482
483 std::string traceMsg = "TI info size = " + std::to_string(tiInfoSize);
484 trace<level::INFO>(traceMsg.c_str());
485
Ben Tynerf5210bb2021-01-05 12:58:10 -0600486 event(EventType::Terminate, i_additionalData,
Ben Tynerb6401ed2021-01-11 09:08:07 -0600487 createFFDCFiles(i_tiInfoData, tiInfoSize));
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500488}
489
Ben Tyner7f6ce6a2021-08-17 19:40:00 -0500490/** @brief Commit SBE vital event to log, returns event log ID */
491uint32_t eventVital()
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500492{
Ben Tynerf5210bb2021-01-05 12:58:10 -0600493 // Additional data for log
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500494 std::map<std::string, std::string> additionalData;
495
Ben Tynerf5210bb2021-01-05 12:58:10 -0600496 // Create log event with additional data and FFDC data
Ben Tyner7f6ce6a2021-08-17 19:40:00 -0500497 return event(EventType::Vital, additionalData, createFFDCFiles(nullptr, 0));
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500498}
499
Ben Tynerf5210bb2021-01-05 12:58:10 -0600500/**
Ben Tynerf5210bb2021-01-05 12:58:10 -0600501 * Commit attention handler failure event to log
502 *
503 * Create an event log containing the specified error code.
504 *
505 * @param i_error - Error code
506 */
507void eventAttentionFail(int i_error)
508{
509 // Additional data for log
510 std::map<std::string, std::string> additionalData;
511 additionalData["ERROR_CODE"] = std::to_string(i_error);
512
513 // Create log event with additional data and FFDC data
514 event(EventType::AttentionFail, additionalData,
515 createFFDCFiles(nullptr, 0));
516}
517
518/**
Ben Tynerb9715172021-09-29 08:46:19 -0500519 * Commit SBE timeout event to log
520 *
521 * Create an event log indicating an SBE operation timed out.
522 *
523 * @param proc - processor that encountered the error
524 */
525void eventPhalSbeChipop(uint32_t proc)
526{
527 trace<level::ERROR>("SBE error while getting TI info");
528
529 // report proc number in event log entry
530 std::map<std::string, std::string> additionalData;
531 additionalData.emplace("SRC6", std::to_string(proc << 16));
532
533 // create event with additional data and no ffdc
534 event(EventType::PhalSbeChipop, additionalData,
535 createFFDCFiles(nullptr, 0));
536}
537
538/**
Ben Tynerf5210bb2021-01-05 12:58:10 -0600539 * Parse systemd journal message field
540 *
541 * Parse the journal looking for the specified field and return the journal
542 * data for that field.
543 *
544 * @param journal - The journal to parse
545 * @param field - Field containing the data to retrieve
546 * @return Data for the speciefied field
547 */
Ben Tyner1b1915e2020-10-23 15:13:38 -0500548std::string sdjGetFieldValue(sd_journal* journal, const char* field)
549{
550 const char* data{nullptr};
551 size_t length{0};
Ben Tyner1b1915e2020-10-23 15:13:38 -0500552
553 // get field value
554 if (0 == sd_journal_get_data(journal, field, (const void**)&data, &length))
555 {
Zane Shelley9fb657f2021-01-12 15:30:58 -0600556 size_t prefix{0};
557
Ben Tyner1b1915e2020-10-23 15:13:38 -0500558 // The data returned by sd_journal_get_data will be prefixed with the
559 // field name and "="
560 const void* eq = memchr(data, '=', length);
561 if (nullptr != eq)
562 {
563 // get just data following the "="
564 prefix = (const char*)eq - data + 1;
565 }
566 else
567 {
568 // all the data (should not happen)
569 prefix = 0;
570 std::string value{}; // empty string
571 }
572
573 return std::string{data + prefix, length - prefix};
574 }
575 else
576 {
577 return std::string{}; // empty string
578 }
579}
580
Ben Tynerf5210bb2021-01-05 12:58:10 -0600581/**
582 * Gather messages from the journal
583 *
584 * Fetch journal entry data for all entries with the specified field equal to
585 * the specified value.
586 *
587 * @param field - Field to search on
588 * @param fieldValue - Value to search for
589 * @param max - Maximum number of messages fetch
590 * @return Vector of journal entry data
591 */
Ben Tyner1b1915e2020-10-23 15:13:38 -0500592std::vector<std::string> sdjGetMessages(const std::string& field,
593 const std::string& fieldValue,
594 unsigned int max)
595{
596 sd_journal* journal;
597 std::vector<std::string> messages;
598
599 if (0 == sd_journal_open(&journal, SD_JOURNAL_LOCAL_ONLY))
600 {
601 SD_JOURNAL_FOREACH_BACKWARDS(journal)
602 {
603 // Get input field
604 std::string value = sdjGetFieldValue(journal, field.c_str());
605
606 // Compare field value and read data
607 if (value == fieldValue)
608 {
609 // Get SYSLOG_IDENTIFIER field (process that logged message)
610 std::string syslog =
611 sdjGetFieldValue(journal, "SYSLOG_IDENTIFIER");
612
613 // Get _PID field
614 std::string pid = sdjGetFieldValue(journal, "_PID");
615
616 // Get MESSAGE field
617 std::string message = sdjGetFieldValue(journal, "MESSAGE");
618
619 // Get timestamp
620 uint64_t usec{0};
621 if (0 == sd_journal_get_realtime_usec(journal, &usec))
622 {
623
624 // Convert realtime microseconds to date format
625 char dateBuffer[80];
626 std::string date;
627 std::time_t timeInSecs = usec / 1000000;
628 strftime(dateBuffer, sizeof(dateBuffer), "%b %d %H:%M:%S",
629 std::localtime(&timeInSecs));
630 date = dateBuffer;
631
632 // Store value to messages
633 value = date + " " + syslog + "[" + pid + "]: " + message;
634 messages.insert(messages.begin(), value);
635 }
636 }
637
638 // limit maximum number of messages
639 if (messages.size() >= max)
640 {
641 break;
642 }
643 }
644
645 sd_journal_close(journal); // close journal when done
646 }
647
648 return messages;
649}
650
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500651} // namespace attn