blob: 9de91f257b22e3e1709b6c9df6cf8b75be36d004 [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>
austinfcuibfa831a2022-01-26 15:37:07 -060010#include <util/trace.hpp>
Ben Tynerf5210bb2021-01-05 12:58:10 -060011
Ben Tynerb1ebfcb2020-05-08 18:52:48 -050012namespace attn
13{
14
Ben Tynerf5210bb2021-01-05 12:58:10 -060015/** @brief Tuple containing information about ffdc files */
16using FFDCTuple =
17 std::tuple<util::FFDCFormat, uint8_t, uint8_t, sdbusplus::message::unix_fd>;
18
19/** @brief Gather messages from the journal */
20std::vector<std::string> sdjGetMessages(const std::string& field,
21 const std::string& fieldValue,
22 unsigned int max);
23
24/**
25 * Create FFDCTuple objects corresponding to the specified FFDC files.
26 *
27 * The D-Bus method to create an error log requires a vector of tuples to
28 * pass in the FFDC file information.
29 *
30 * @param files - FFDC files
31 * @return vector of FFDCTuple objects
32 */
33std::vector<FFDCTuple>
34 createFFDCTuples(const std::vector<util::FFDCFile>& files)
35{
36 std::vector<FFDCTuple> ffdcTuples{};
Zane Shelleya79f6c82021-01-12 16:38:49 -060037 util::transformFFDC(files, ffdcTuples);
Ben Tynerf5210bb2021-01-05 12:58:10 -060038
39 return ffdcTuples;
40}
41
42/**
43 * @brief Create an FFDCFile object containing raw data
44 *
45 * Throws an exception if an error occurs.
46 *
47 * @param i_buffer - raw data to add to ffdc faw data file
48 * @param i_size - size of the raw data
49 * @return FFDCFile object
50 */
51util::FFDCFile createFFDCRawFile(void* i_buffer, size_t i_size)
52{
53 util::FFDCFile file{util::FFDCFormat::Custom};
54
55 // Write buffer to file and then reset file description file offset
Ben Tynerd7006092021-02-05 14:55:52 -060056 int fd = file.getFileDescriptor();
57 size_t numBytes = write(fd, static_cast<char*>(i_buffer), i_size);
58 if (i_size != numBytes)
59 {
austinfcuibfa831a2022-01-26 15:37:07 -060060 trace::err("%s only %u of %u bytes written", file.getPath().c_str(),
61 numBytes, i_size);
Ben Tynerd7006092021-02-05 14:55:52 -060062 }
63
Ben Tynerf5210bb2021-01-05 12:58:10 -060064 lseek(fd, 0, SEEK_SET);
65
66 return file;
67}
68
69/**
70 * @brief Create an FFDCFile object containing the specified lines of text data
71 *
72 * Throws an exception if an error occurs.
73 *
74 * @param lines - lines of text data to write to file
75 * @return FFDCFile object
76 */
77util::FFDCFile createFFDCTraceFile(const std::vector<std::string>& lines)
78{
79 // Create FFDC file of type Text
80 util::FFDCFile file{util::FFDCFormat::Text};
81 int fd = file.getFileDescriptor();
82
83 // Write FFDC lines to file
84 std::string buffer;
85 for (const std::string& line : lines)
86 {
87 // Copy line to buffer. Add newline if necessary.
88 buffer = line;
89 if (line.empty() || (line.back() != '\n'))
90 {
91 buffer += '\n';
92 }
93
94 // write buffer to file
Ben Tynerd7006092021-02-05 14:55:52 -060095 size_t numBytes = write(fd, buffer.c_str(), buffer.size());
96 if (buffer.size() != numBytes)
97 {
austinfcuibfa831a2022-01-26 15:37:07 -060098 trace::err("%s only %u of %u bytes written", file.getPath().c_str(),
99 numBytes, buffer.size());
Ben Tynerd7006092021-02-05 14:55:52 -0600100 }
Ben Tynerf5210bb2021-01-05 12:58:10 -0600101 }
102
103 // Seek to beginning of file so error logging system can read data
104 lseek(fd, 0, SEEK_SET);
105
106 return file;
107}
108
109/**
110 * Create FDDC files from journal messages of relevant executables
111 *
112 * Parse the system journal looking for log entries created by the executables
113 * of interest for logging. For each of these entries create a ffdc trace file
114 * that will be used to create ffdc log entries. These files will be pushed
115 * onto the stack of ffdc files.
116 *
117 * @param i_files - vector of ffdc files that will become log entries
118 */
119void createFFDCTraceFiles(std::vector<util::FFDCFile>& i_files)
120{
121 // Executables of interest
122 std::vector<std::string> executables{"openpower-hw-diags"};
123
124 for (const std::string& executable : executables)
125 {
126 try
127 {
128 // get journal messages
129 std::vector<std::string> messages =
130 sdjGetMessages("SYSLOG_IDENTIFIER", executable, 30);
131
132 // Create FFDC file containing the journal messages
133 if (!messages.empty())
134 {
135 i_files.emplace_back(createFFDCTraceFile(messages));
136 }
137 }
138 catch (const std::exception& e)
139 {
austinfcuibfa831a2022-01-26 15:37:07 -0600140 trace::inf("createFFDCTraceFiles exception");
141 trace::inf(e.what());
Ben Tynerf5210bb2021-01-05 12:58:10 -0600142 }
143 }
144}
145
146/**
147 * Create FFDCFile objects containing debug data to store in the error log.
148 *
149 * If an error occurs, the error is written to the journal but an exception
150 * is not thrown.
151 *
152 * @param i_buffer - raw data (if creating raw dump ffdc entry in log)
153 * @return vector of FFDCFile objects
154 */
155std::vector<util::FFDCFile> createFFDCFiles(char* i_buffer = nullptr,
156 size_t i_size = 0)
157{
158 std::vector<util::FFDCFile> files{};
159
160 // Create raw dump file
161 if ((nullptr != i_buffer) && (0 != i_size))
162 {
163 files.emplace_back(createFFDCRawFile(i_buffer, i_size));
164 }
165
166 // Create trace dump file
167 createFFDCTraceFiles(files);
168
169 return files;
170}
171
172/**
Ben Tynerf5210bb2021-01-05 12:58:10 -0600173 * Create a PEL from an existing PEL
174 *
175 * Create a new PEL based on the specified raw PEL and submit the new PEL
176 * to the backend logging code as a raw PEL. Note that additional data map
177 * here contains data to be committed to the PEL and it can also be used to
178 * create the PEL as it contains needed information.
179 *
Ben Tyner135793a2021-10-27 09:18:41 -0500180 * @param i_rawPel - buffer containing a raw PEL
Ben Tynerf5210bb2021-01-05 12:58:10 -0600181 * @param i_additional - additional data to be added to the new PEL
182 */
183void createPelCustom(std::vector<uint8_t>& i_rawPel,
184 std::map<std::string, std::string> i_additional)
185{
186 // create PEL object from buffer
187 auto tiPel = std::make_unique<pel::PelMinimal>(i_rawPel);
188
189 // The additional data contains the TI info as well as the value for the
190 // subystem that provided the TI info. Get the subystem from additional
191 // data and then populate the prmary SRC and SRC words for the custom PEL
192 // based on the sybsystem's TI info.
193 uint8_t subsystem = std::stoi(i_additional["Subsystem"]);
194 tiPel->setSubsystem(subsystem);
195
Ben Tyner135793a2021-10-27 09:18:41 -0500196 // If recoverable attentions are active we will call the analyzer and
197 // then link the custom pel to analyzer pel.
198 std::map<std::string, std::string>::iterator it;
199 it = i_additional.find("recoverables");
200 if (it != i_additional.end() && "true" == it->second)
201 {
202 DumpParameters dumpParameters;
Zane Shelleyebff0d32021-11-21 10:52:07 -0600203 auto plid = analyzer::analyzeHardware(
204 analyzer::AnalysisType::TERMINATE_IMMEDIATE, dumpParameters);
Zane Shelley611b3442021-11-19 16:02:01 -0600205 if (0 != plid)
Ben Tyner135793a2021-10-27 09:18:41 -0500206 {
Zane Shelley611b3442021-11-19 16:02:01 -0600207 // Link the PLID if an attention was found and a PEL was generated.
208 tiPel->setPlid(plid);
Ben Tyner135793a2021-10-27 09:18:41 -0500209 }
210 }
211
Ben Tynerf5210bb2021-01-05 12:58:10 -0600212 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
Ben Tyner9d4f91c2021-02-09 08:27:58 -0600225 // Populate phyp primary SRC
226
227 // char array for raw pel src
Ben Tynerf5210bb2021-01-05 12:58:10 -0600228 std::array<char, pel::asciiStringSize> srcChars{'0'};
Ben Tyner9d4f91c2021-02-09 08:27:58 -0600229
230 // src from TI info
Ben Tynerf5210bb2021-01-05 12:58:10 -0600231 std::string srcString = i_additional["SrcAscii"];
Ben Tyner9d4f91c2021-02-09 08:27:58 -0600232
233 // copy from string to char array
Ben Tynerf5210bb2021-01-05 12:58:10 -0600234 srcString.copy(srcChars.data(),
235 std::min(srcString.size(), pel::asciiStringSize), 0);
Ben Tyner9d4f91c2021-02-09 08:27:58 -0600236
237 tiPel->setAsciiString(srcChars); // pel object src is char array
Ben Tynerfeeea832021-04-06 10:08:11 -0500238
239 // set symptom-id
240 auto symptomId = (i_additional["SrcAscii"].substr(0, 8) + '_');
241
242 symptomId += (i_additional["0x10 SRC Word 12"]);
243 symptomId += (i_additional["0x14 SRC Word 13"] + '_');
244 symptomId += (i_additional["0x18 SRC Word 14"]);
245 symptomId += (i_additional["0x1c SRC Word 15"] + '_');
246 symptomId += (i_additional["0x20 SRC Word 16"]);
247 symptomId += (i_additional["0x24 SRC Word 17"] + '_');
248 symptomId += (i_additional["0x28 SRC Word 18"]);
249 symptomId += (i_additional["0x2c SRC Word 19"]);
250
251 // setSymptomId will take care of required null-terminate and padding
252 tiPel->setSymptomId(symptomId);
Ben Tynerf5210bb2021-01-05 12:58:10 -0600253 }
254 else
255 {
256 // Populate hostboot SRC words - note HB word 0 from the shared info
257 // data (additional data "0x10 HB Word") is reflected in the PEL as
258 // "reason code" so we zero it here. Also note that the first word
259 // in this group of words starts at word 0 and word 1 does not exits.
260 tiPel->setSrcWords(std::array<uint32_t, pel::numSrcWords>{
261 (uint32_t)0x00000000,
262 (uint32_t)std::stoul(i_additional["0x14 HB Word 2"], 0, 16),
263 (uint32_t)std::stoul(i_additional["0x18 HB Word 3"], 0, 16),
264 (uint32_t)std::stoul(i_additional["0x1c HB Word 4"], 0, 16),
265 (uint32_t)std::stoul(i_additional["0x20 HB Word 5"], 0, 16),
266 (uint32_t)std::stoul(i_additional["0x24 HB Word 6"], 0, 16),
267 (uint32_t)std::stoul(i_additional["0x28 HB Word 7"], 0, 16),
268 (uint32_t)std::stoul(i_additional["0x2c HB Word 8"], 0, 16)});
269
Ben Tyner9d4f91c2021-02-09 08:27:58 -0600270 // Populate hostboot primary SRC
271
272 // char array for raw pel src
Ben Tynerf5210bb2021-01-05 12:58:10 -0600273 std::array<char, pel::asciiStringSize> srcChars{'0'};
Ben Tyner9d4f91c2021-02-09 08:27:58 -0600274
275 // src from TI info
276 std::string srcString = i_additional["SrcAscii"];
277
278 // copy from string to char array
Ben Tynerf5210bb2021-01-05 12:58:10 -0600279 srcString.copy(srcChars.data(),
280 std::min(srcString.size(), pel::asciiStringSize), 0);
Ben Tyner9d4f91c2021-02-09 08:27:58 -0600281
282 tiPel->setAsciiString(srcChars); // pel object src is char array
Ben Tynerfeeea832021-04-06 10:08:11 -0500283
284 // set symptom-id
285 auto symptomId = (i_additional["SrcAscii"].substr(0, 8) + '_');
286
287 symptomId += (i_additional["0x10 HB Word 0"]); // note: word 1
288 symptomId += (i_additional["0x14 HB Word 2"] + '_'); // does not exist
289 symptomId += (i_additional["0x18 HB Word 3"]);
290 symptomId += (i_additional["0x1c HB Word 4"] + '_');
291 symptomId += (i_additional["0x20 HB Word 5"]);
292 symptomId += (i_additional["0x24 HB Word 6"] + '_');
293 symptomId += (i_additional["0x28 HB Word 7"]);
294 symptomId += (i_additional["0x2c HB Word 8"]);
295
296 // setSymptomId will take care of required null-terminate and padding
297 tiPel->setSymptomId(symptomId);
Ben Tynerf5210bb2021-01-05 12:58:10 -0600298 }
299
300 // set severity, event type and action flags
301 tiPel->setSeverity(static_cast<uint8_t>(pel::Severity::termination));
302 tiPel->setType(static_cast<uint8_t>(pel::EventType::na));
303 tiPel->setAction(static_cast<uint16_t>(pel::ActionFlags::service |
304 pel::ActionFlags::report |
305 pel::ActionFlags::call));
306
307 // The raw PEL that we used as the basis for this custom PEL contains the
308 // attention handler trace data and does not needed to be in this PEL so
309 // we remove it here.
310 tiPel->setSectionCount(tiPel->getSectionCount() - 1);
311
312 // Update the raw PEL with the new custom PEL data
313 tiPel->raw(i_rawPel);
314
315 // create PEL from raw data
316 createPelRaw(i_rawPel);
317}
318
319/**
320 * Log an event handled by the attention handler
321 *
322 * Basic (non TI) events will generate a standard message-registry based PEL
323 *
324 * TI events will create two PEL's. One PEL will be informational and will
325 * contain trace information relevent to attention handler. The second PEL
326 * will be specific to the TI type (including the primary SRC) and will be
327 * based off of the TI information provided to the attention handler through
328 * shared TI info data area.
329 *
330 * @param i_event - The event type
331 * @param i_additional - Additional PEL data
332 * @param i_ffdc - FFDC PEL data
Ben Tyner7f6ce6a2021-08-17 19:40:00 -0500333 * @return Event log Id (0 if no event log generated)
Ben Tynerf5210bb2021-01-05 12:58:10 -0600334 */
Ben Tyner7f6ce6a2021-08-17 19:40:00 -0500335uint32_t event(EventType i_event,
336 std::map<std::string, std::string>& i_additional,
337 const std::vector<util::FFDCFile>& i_ffdc)
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500338{
Ben Tyner7f6ce6a2021-08-17 19:40:00 -0500339 uint32_t pelId = 0; // assume no event log generated
340
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500341 bool eventValid = false; // assume no event created
Ben Tynerf5210bb2021-01-05 12:58:10 -0600342 bool tiEvent = false; // assume not a terminate event
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500343
344 std::string eventName;
345
346 switch (i_event)
347 {
348 case EventType::Checkstop:
349 eventName = "org.open_power.HwDiags.Error.Checkstop";
350 eventValid = true;
351 break;
352 case EventType::Terminate:
353 eventName = "org.open_power.Attn.Error.Terminate";
354 eventValid = true;
Ben Tynerf5210bb2021-01-05 12:58:10 -0600355 tiEvent = true;
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500356 break;
357 case EventType::Vital:
358 eventName = "org.open_power.Attn.Error.Vital";
359 eventValid = true;
360 break;
361 case EventType::HwDiagsFail:
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500362 case EventType::AttentionFail:
363 eventName = "org.open_power.Attn.Error.Fail";
364 eventValid = true;
365 break;
366 default:
367 eventValid = false;
368 break;
369 }
370
371 if (true == eventValid)
372 {
Ben Tynerf5210bb2021-01-05 12:58:10 -0600373 // Create PEL with additional data and FFDC data. The newly created
374 // PEL's platform log-id will be returned.
Ben Tyner7f6ce6a2021-08-17 19:40:00 -0500375 pelId = createPel(eventName, i_additional, createFFDCTuples(i_ffdc));
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500376
Ben Tynerf5210bb2021-01-05 12:58:10 -0600377 // If this is a TI event we will create an additional PEL that is
378 // specific to the subsystem that generated the TI.
Ben Tyner135793a2021-10-27 09:18:41 -0500379 if ((0 != pelId) && (true == tiEvent))
Ben Tynerf5210bb2021-01-05 12:58:10 -0600380 {
381 // get file descriptor and size of information PEL
Ben Tyner188f1092021-02-01 09:33:06 -0600382 int pelFd = getPel(pelId);
Ben Tyner1b1915e2020-10-23 15:13:38 -0500383
Ben Tynerf5210bb2021-01-05 12:58:10 -0600384 // if PEL found, read into buffer
385 if (-1 != pelFd)
386 {
387 auto pelSize = lseek(pelFd, 0, SEEK_END);
388 lseek(pelFd, 0, SEEK_SET);
Ben Tyner1b1915e2020-10-23 15:13:38 -0500389
Ben Tynerf5210bb2021-01-05 12:58:10 -0600390 // read information PEL into buffer
391 std::vector<uint8_t> buffer(pelSize);
Ben Tynerd7006092021-02-05 14:55:52 -0600392 size_t numBytes = read(pelFd, buffer.data(), buffer.size());
393 if (buffer.size() != numBytes)
394 {
austinfcuibfa831a2022-01-26 15:37:07 -0600395 trace::err("Error reading event log: %u of %u bytes read",
396 numBytes, buffer.size());
Ben Tynerd7006092021-02-05 14:55:52 -0600397 }
398 else
399 {
400 // create PEL from buffer
401 createPelCustom(buffer, i_additional);
402 }
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500403
Ben Tynerd7006092021-02-05 14:55:52 -0600404 close(pelFd);
Ben Tynerf5210bb2021-01-05 12:58:10 -0600405 }
Ben Tyner5c5db652021-02-22 18:22:35 -0600406
Ben Tyner6bc43c92021-05-27 15:08:02 -0500407 uint8_t subsystem = std::stoi(i_additional["Subsystem"]);
Ben Tyner5c5db652021-02-22 18:22:35 -0600408
Ben Tyner6bc43c92021-05-27 15:08:02 -0500409 // If not hypervisor TI
410 if (static_cast<uint8_t>(pel::SubsystemID::hypervisor) != subsystem)
Ben Tyner5c5db652021-02-22 18:22:35 -0600411 {
Ben Tyner6bc43c92021-05-27 15:08:02 -0500412 // Request a dump and transition the host
413 if ("true" == i_additional["Dump"])
414 {
415 // will not return until dump is complete
Zane Shelley611b3442021-11-19 16:02:01 -0600416 requestDump(pelId, DumpParameters{0, DumpType::Hostboot});
Ben Tyner6bc43c92021-05-27 15:08:02 -0500417 }
Ben Tyner5c5db652021-02-22 18:22:35 -0600418 }
419 }
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500420 }
Ben Tyner7f6ce6a2021-08-17 19:40:00 -0500421 return pelId;
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500422}
423
Ben Tynerf5210bb2021-01-05 12:58:10 -0600424/**
425 * Commit special attention TI event to log
426 *
427 * Create a event log with provided additional information and standard
428 * FFDC data plus TI FFDC data
429 *
430 * @param i_additional - Additional log data
431 * @param i_ti_InfoData - TI FFDC data
432 */
433void eventTerminate(std::map<std::string, std::string> i_additionalData,
434 char* i_tiInfoData)
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500435{
Ben Tynerb6401ed2021-01-11 09:08:07 -0600436
Ben Tyner29651ef2021-02-08 10:51:03 -0600437 uint32_t tiInfoSize = 0; // assume TI info was not available
Ben Tynerb6401ed2021-01-11 09:08:07 -0600438
Ben Tyner29651ef2021-02-08 10:51:03 -0600439 if (nullptr != i_tiInfoData)
Ben Tynerb6401ed2021-01-11 09:08:07 -0600440 {
Ben Tyner29651ef2021-02-08 10:51:03 -0600441 tiInfoSize = 56; // assume not hypervisor TI
Ben Tynerb6401ed2021-01-11 09:08:07 -0600442
Ben Tyner29651ef2021-02-08 10:51:03 -0600443 uint8_t subsystem = std::stoi(i_additionalData["Subsystem"]);
444
445 // If hypervisor
446 if (static_cast<uint8_t>(pel::SubsystemID::hypervisor) == subsystem)
Ben Tynerb6401ed2021-01-11 09:08:07 -0600447 {
Ben Tyner29651ef2021-02-08 10:51:03 -0600448 tiInfoSize = 1024; // assume hypervisor max
Ben Tynerb6401ed2021-01-11 09:08:07 -0600449
Ben Tyner29651ef2021-02-08 10:51:03 -0600450 // hypervisor may just want some of the data
451 if (0 == (*(i_tiInfoData + 0x09) & 0x01))
452 {
453 uint32_t* additionalLength = (uint32_t*)(i_tiInfoData + 0x50);
454 uint32_t tiAdditional = be32toh(*additionalLength);
455 tiInfoSize = std::min(tiInfoSize, (84 + tiAdditional));
456 }
Ben Tynerb6401ed2021-01-11 09:08:07 -0600457 }
458 }
459
austinfcuibfa831a2022-01-26 15:37:07 -0600460 trace::inf("TI info size = %u", tiInfoSize);
Ben Tynerb6401ed2021-01-11 09:08:07 -0600461
Ben Tynerf5210bb2021-01-05 12:58:10 -0600462 event(EventType::Terminate, i_additionalData,
Ben Tynerb6401ed2021-01-11 09:08:07 -0600463 createFFDCFiles(i_tiInfoData, tiInfoSize));
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500464}
465
Ben Tyner7f6ce6a2021-08-17 19:40:00 -0500466/** @brief Commit SBE vital event to log, returns event log ID */
467uint32_t eventVital()
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500468{
Ben Tynerf5210bb2021-01-05 12:58:10 -0600469 // Additional data for log
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500470 std::map<std::string, std::string> additionalData;
471
Ben Tynerf5210bb2021-01-05 12:58:10 -0600472 // Create log event with additional data and FFDC data
Ben Tyner7f6ce6a2021-08-17 19:40:00 -0500473 return event(EventType::Vital, additionalData, createFFDCFiles(nullptr, 0));
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500474}
475
Ben Tynerf5210bb2021-01-05 12:58:10 -0600476/**
Ben Tynerf5210bb2021-01-05 12:58:10 -0600477 * Commit attention handler failure event to log
478 *
479 * Create an event log containing the specified error code.
480 *
481 * @param i_error - Error code
482 */
483void eventAttentionFail(int i_error)
484{
485 // Additional data for log
486 std::map<std::string, std::string> additionalData;
487 additionalData["ERROR_CODE"] = std::to_string(i_error);
488
489 // Create log event with additional data and FFDC data
490 event(EventType::AttentionFail, additionalData,
491 createFFDCFiles(nullptr, 0));
492}
493
494/**
495 * Parse systemd journal message field
496 *
497 * Parse the journal looking for the specified field and return the journal
498 * data for that field.
499 *
500 * @param journal - The journal to parse
501 * @param field - Field containing the data to retrieve
502 * @return Data for the speciefied field
503 */
Ben Tyner1b1915e2020-10-23 15:13:38 -0500504std::string sdjGetFieldValue(sd_journal* journal, const char* field)
505{
506 const char* data{nullptr};
507 size_t length{0};
Ben Tyner1b1915e2020-10-23 15:13:38 -0500508
509 // get field value
510 if (0 == sd_journal_get_data(journal, field, (const void**)&data, &length))
511 {
Zane Shelley9fb657f2021-01-12 15:30:58 -0600512 size_t prefix{0};
513
Ben Tyner1b1915e2020-10-23 15:13:38 -0500514 // The data returned by sd_journal_get_data will be prefixed with the
515 // field name and "="
516 const void* eq = memchr(data, '=', length);
517 if (nullptr != eq)
518 {
519 // get just data following the "="
520 prefix = (const char*)eq - data + 1;
521 }
522 else
523 {
524 // all the data (should not happen)
525 prefix = 0;
526 std::string value{}; // empty string
527 }
528
529 return std::string{data + prefix, length - prefix};
530 }
531 else
532 {
533 return std::string{}; // empty string
534 }
535}
536
Ben Tynerf5210bb2021-01-05 12:58:10 -0600537/**
538 * Gather messages from the journal
539 *
540 * Fetch journal entry data for all entries with the specified field equal to
541 * the specified value.
542 *
543 * @param field - Field to search on
544 * @param fieldValue - Value to search for
545 * @param max - Maximum number of messages fetch
546 * @return Vector of journal entry data
547 */
Ben Tyner1b1915e2020-10-23 15:13:38 -0500548std::vector<std::string> sdjGetMessages(const std::string& field,
549 const std::string& fieldValue,
550 unsigned int max)
551{
552 sd_journal* journal;
553 std::vector<std::string> messages;
554
555 if (0 == sd_journal_open(&journal, SD_JOURNAL_LOCAL_ONLY))
556 {
557 SD_JOURNAL_FOREACH_BACKWARDS(journal)
558 {
559 // Get input field
560 std::string value = sdjGetFieldValue(journal, field.c_str());
561
562 // Compare field value and read data
563 if (value == fieldValue)
564 {
565 // Get SYSLOG_IDENTIFIER field (process that logged message)
566 std::string syslog =
567 sdjGetFieldValue(journal, "SYSLOG_IDENTIFIER");
568
569 // Get _PID field
570 std::string pid = sdjGetFieldValue(journal, "_PID");
571
572 // Get MESSAGE field
573 std::string message = sdjGetFieldValue(journal, "MESSAGE");
574
575 // Get timestamp
576 uint64_t usec{0};
577 if (0 == sd_journal_get_realtime_usec(journal, &usec))
578 {
579
580 // Convert realtime microseconds to date format
581 char dateBuffer[80];
582 std::string date;
583 std::time_t timeInSecs = usec / 1000000;
584 strftime(dateBuffer, sizeof(dateBuffer), "%b %d %H:%M:%S",
585 std::localtime(&timeInSecs));
586 date = dateBuffer;
587
588 // Store value to messages
589 value = date + " " + syslog + "[" + pid + "]: " + message;
590 messages.insert(messages.begin(), value);
591 }
592 }
593
594 // limit maximum number of messages
595 if (messages.size() >= max)
596 {
597 break;
598 }
599 }
600
601 sd_journal_close(journal); // close journal when done
602 }
603
604 return messages;
605}
606
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500607} // namespace attn