blob: 279d2121f708d4cad4ec30c670b21488f87fe57c [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;
Zane Shelley611b3442021-11-19 16:02:01 -0600222 auto plid = analyzer::analyzeHardware(dumpParameters);
223 if (0 != plid)
Ben Tyner135793a2021-10-27 09:18:41 -0500224 {
Zane Shelley611b3442021-11-19 16:02:01 -0600225 // Link the PLID if an attention was found and a PEL was generated.
226 tiPel->setPlid(plid);
Ben Tyner135793a2021-10-27 09:18:41 -0500227 }
228 }
229
Ben Tynerf5210bb2021-01-05 12:58:10 -0600230 if (static_cast<uint8_t>(pel::SubsystemID::hypervisor) == subsystem)
231 {
232 // populate hypervisor SRC words
233 tiPel->setSrcWords(std::array<uint32_t, pel::numSrcWords>{
234 (uint32_t)std::stoul(i_additional["0x10 SRC Word 12"], 0, 16),
235 (uint32_t)std::stoul(i_additional["0x14 SRC Word 13"], 0, 16),
236 (uint32_t)std::stoul(i_additional["0x18 SRC Word 14"], 0, 16),
237 (uint32_t)std::stoul(i_additional["0x1c SRC Word 15"], 0, 16),
238 (uint32_t)std::stoul(i_additional["0x20 SRC Word 16"], 0, 16),
239 (uint32_t)std::stoul(i_additional["0x24 SRC Word 17"], 0, 16),
240 (uint32_t)std::stoul(i_additional["0x28 SRC Word 18"], 0, 16),
241 (uint32_t)std::stoul(i_additional["0x2c SRC Word 19"], 0, 16)});
242
Ben Tyner9d4f91c2021-02-09 08:27:58 -0600243 // Populate phyp primary SRC
244
245 // char array for raw pel src
Ben Tynerf5210bb2021-01-05 12:58:10 -0600246 std::array<char, pel::asciiStringSize> srcChars{'0'};
Ben Tyner9d4f91c2021-02-09 08:27:58 -0600247
248 // src from TI info
Ben Tynerf5210bb2021-01-05 12:58:10 -0600249 std::string srcString = i_additional["SrcAscii"];
Ben Tyner9d4f91c2021-02-09 08:27:58 -0600250
251 // copy from string to char array
Ben Tynerf5210bb2021-01-05 12:58:10 -0600252 srcString.copy(srcChars.data(),
253 std::min(srcString.size(), pel::asciiStringSize), 0);
Ben Tyner9d4f91c2021-02-09 08:27:58 -0600254
255 tiPel->setAsciiString(srcChars); // pel object src is char array
Ben Tynerfeeea832021-04-06 10:08:11 -0500256
257 // set symptom-id
258 auto symptomId = (i_additional["SrcAscii"].substr(0, 8) + '_');
259
260 symptomId += (i_additional["0x10 SRC Word 12"]);
261 symptomId += (i_additional["0x14 SRC Word 13"] + '_');
262 symptomId += (i_additional["0x18 SRC Word 14"]);
263 symptomId += (i_additional["0x1c SRC Word 15"] + '_');
264 symptomId += (i_additional["0x20 SRC Word 16"]);
265 symptomId += (i_additional["0x24 SRC Word 17"] + '_');
266 symptomId += (i_additional["0x28 SRC Word 18"]);
267 symptomId += (i_additional["0x2c SRC Word 19"]);
268
269 // setSymptomId will take care of required null-terminate and padding
270 tiPel->setSymptomId(symptomId);
Ben Tynerf5210bb2021-01-05 12:58:10 -0600271 }
272 else
273 {
274 // Populate hostboot SRC words - note HB word 0 from the shared info
275 // data (additional data "0x10 HB Word") is reflected in the PEL as
276 // "reason code" so we zero it here. Also note that the first word
277 // in this group of words starts at word 0 and word 1 does not exits.
278 tiPel->setSrcWords(std::array<uint32_t, pel::numSrcWords>{
279 (uint32_t)0x00000000,
280 (uint32_t)std::stoul(i_additional["0x14 HB Word 2"], 0, 16),
281 (uint32_t)std::stoul(i_additional["0x18 HB Word 3"], 0, 16),
282 (uint32_t)std::stoul(i_additional["0x1c HB Word 4"], 0, 16),
283 (uint32_t)std::stoul(i_additional["0x20 HB Word 5"], 0, 16),
284 (uint32_t)std::stoul(i_additional["0x24 HB Word 6"], 0, 16),
285 (uint32_t)std::stoul(i_additional["0x28 HB Word 7"], 0, 16),
286 (uint32_t)std::stoul(i_additional["0x2c HB Word 8"], 0, 16)});
287
Ben Tyner9d4f91c2021-02-09 08:27:58 -0600288 // Populate hostboot primary SRC
289
290 // char array for raw pel src
Ben Tynerf5210bb2021-01-05 12:58:10 -0600291 std::array<char, pel::asciiStringSize> srcChars{'0'};
Ben Tyner9d4f91c2021-02-09 08:27:58 -0600292
293 // src from TI info
294 std::string srcString = i_additional["SrcAscii"];
295
296 // copy from string to char array
Ben Tynerf5210bb2021-01-05 12:58:10 -0600297 srcString.copy(srcChars.data(),
298 std::min(srcString.size(), pel::asciiStringSize), 0);
Ben Tyner9d4f91c2021-02-09 08:27:58 -0600299
300 tiPel->setAsciiString(srcChars); // pel object src is char array
Ben Tynerfeeea832021-04-06 10:08:11 -0500301
302 // set symptom-id
303 auto symptomId = (i_additional["SrcAscii"].substr(0, 8) + '_');
304
305 symptomId += (i_additional["0x10 HB Word 0"]); // note: word 1
306 symptomId += (i_additional["0x14 HB Word 2"] + '_'); // does not exist
307 symptomId += (i_additional["0x18 HB Word 3"]);
308 symptomId += (i_additional["0x1c HB Word 4"] + '_');
309 symptomId += (i_additional["0x20 HB Word 5"]);
310 symptomId += (i_additional["0x24 HB Word 6"] + '_');
311 symptomId += (i_additional["0x28 HB Word 7"]);
312 symptomId += (i_additional["0x2c HB Word 8"]);
313
314 // setSymptomId will take care of required null-terminate and padding
315 tiPel->setSymptomId(symptomId);
Ben Tynerf5210bb2021-01-05 12:58:10 -0600316 }
317
318 // set severity, event type and action flags
319 tiPel->setSeverity(static_cast<uint8_t>(pel::Severity::termination));
320 tiPel->setType(static_cast<uint8_t>(pel::EventType::na));
321 tiPel->setAction(static_cast<uint16_t>(pel::ActionFlags::service |
322 pel::ActionFlags::report |
323 pel::ActionFlags::call));
324
325 // The raw PEL that we used as the basis for this custom PEL contains the
326 // attention handler trace data and does not needed to be in this PEL so
327 // we remove it here.
328 tiPel->setSectionCount(tiPel->getSectionCount() - 1);
329
330 // Update the raw PEL with the new custom PEL data
331 tiPel->raw(i_rawPel);
332
333 // create PEL from raw data
334 createPelRaw(i_rawPel);
335}
336
337/**
338 * Log an event handled by the attention handler
339 *
340 * Basic (non TI) events will generate a standard message-registry based PEL
341 *
342 * TI events will create two PEL's. One PEL will be informational and will
343 * contain trace information relevent to attention handler. The second PEL
344 * will be specific to the TI type (including the primary SRC) and will be
345 * based off of the TI information provided to the attention handler through
346 * shared TI info data area.
347 *
348 * @param i_event - The event type
349 * @param i_additional - Additional PEL data
350 * @param i_ffdc - FFDC PEL data
Ben Tyner7f6ce6a2021-08-17 19:40:00 -0500351 * @return Event log Id (0 if no event log generated)
Ben Tynerf5210bb2021-01-05 12:58:10 -0600352 */
Ben Tyner7f6ce6a2021-08-17 19:40:00 -0500353uint32_t event(EventType i_event,
354 std::map<std::string, std::string>& i_additional,
355 const std::vector<util::FFDCFile>& i_ffdc)
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500356{
Ben Tyner7f6ce6a2021-08-17 19:40:00 -0500357 uint32_t pelId = 0; // assume no event log generated
358
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500359 bool eventValid = false; // assume no event created
Ben Tynerf5210bb2021-01-05 12:58:10 -0600360 bool tiEvent = false; // assume not a terminate event
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500361
362 std::string eventName;
363
364 switch (i_event)
365 {
366 case EventType::Checkstop:
367 eventName = "org.open_power.HwDiags.Error.Checkstop";
368 eventValid = true;
369 break;
370 case EventType::Terminate:
371 eventName = "org.open_power.Attn.Error.Terminate";
372 eventValid = true;
Ben Tynerf5210bb2021-01-05 12:58:10 -0600373 tiEvent = true;
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500374 break;
375 case EventType::Vital:
376 eventName = "org.open_power.Attn.Error.Vital";
377 eventValid = true;
378 break;
379 case EventType::HwDiagsFail:
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500380 case EventType::AttentionFail:
381 eventName = "org.open_power.Attn.Error.Fail";
382 eventValid = true;
383 break;
Ben Tynerb9715172021-09-29 08:46:19 -0500384 case EventType::PhalSbeChipop:
385 eventName = "org.open_power.Processor.Error.SbeChipOpFailure";
386 eventValid = true;
387 break;
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500388 default:
389 eventValid = false;
390 break;
391 }
392
393 if (true == eventValid)
394 {
Ben Tynerf5210bb2021-01-05 12:58:10 -0600395 // Create PEL with additional data and FFDC data. The newly created
396 // PEL's platform log-id will be returned.
Ben Tyner7f6ce6a2021-08-17 19:40:00 -0500397 pelId = createPel(eventName, i_additional, createFFDCTuples(i_ffdc));
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500398
Ben Tynerf5210bb2021-01-05 12:58:10 -0600399 // If this is a TI event we will create an additional PEL that is
400 // specific to the subsystem that generated the TI.
Ben Tyner135793a2021-10-27 09:18:41 -0500401 if ((0 != pelId) && (true == tiEvent))
Ben Tynerf5210bb2021-01-05 12:58:10 -0600402 {
403 // get file descriptor and size of information PEL
Ben Tyner188f1092021-02-01 09:33:06 -0600404 int pelFd = getPel(pelId);
Ben Tyner1b1915e2020-10-23 15:13:38 -0500405
Ben Tynerf5210bb2021-01-05 12:58:10 -0600406 // if PEL found, read into buffer
407 if (-1 != pelFd)
408 {
409 auto pelSize = lseek(pelFd, 0, SEEK_END);
410 lseek(pelFd, 0, SEEK_SET);
Ben Tyner1b1915e2020-10-23 15:13:38 -0500411
Ben Tynerf5210bb2021-01-05 12:58:10 -0600412 // read information PEL into buffer
413 std::vector<uint8_t> buffer(pelSize);
Ben Tynerd7006092021-02-05 14:55:52 -0600414 size_t numBytes = read(pelFd, buffer.data(), buffer.size());
415 if (buffer.size() != numBytes)
416 {
417 std::stringstream traceMsg;
418 traceMsg << "Error reading event log: " << (int)numBytes
419 << " of " << (int)buffer.size() << " bytes read";
420 auto strobj = traceMsg.str();
421 trace<level::ERROR>(strobj.c_str());
422 }
423 else
424 {
425 // create PEL from buffer
426 createPelCustom(buffer, i_additional);
427 }
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500428
Ben Tynerd7006092021-02-05 14:55:52 -0600429 close(pelFd);
Ben Tynerf5210bb2021-01-05 12:58:10 -0600430 }
Ben Tyner5c5db652021-02-22 18:22:35 -0600431
Ben Tyner6bc43c92021-05-27 15:08:02 -0500432 uint8_t subsystem = std::stoi(i_additional["Subsystem"]);
Ben Tyner5c5db652021-02-22 18:22:35 -0600433
Ben Tyner6bc43c92021-05-27 15:08:02 -0500434 // If not hypervisor TI
435 if (static_cast<uint8_t>(pel::SubsystemID::hypervisor) != subsystem)
Ben Tyner5c5db652021-02-22 18:22:35 -0600436 {
Ben Tyner6bc43c92021-05-27 15:08:02 -0500437 // Request a dump and transition the host
438 if ("true" == i_additional["Dump"])
439 {
440 // will not return until dump is complete
Zane Shelley611b3442021-11-19 16:02:01 -0600441 requestDump(pelId, DumpParameters{0, DumpType::Hostboot});
Ben Tyner6bc43c92021-05-27 15:08:02 -0500442 }
Ben Tyner5c5db652021-02-22 18:22:35 -0600443 }
444 }
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500445 }
Ben Tyner7f6ce6a2021-08-17 19:40:00 -0500446 return pelId;
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500447}
448
Ben Tynerf5210bb2021-01-05 12:58:10 -0600449/**
450 * Commit special attention TI event to log
451 *
452 * Create a event log with provided additional information and standard
453 * FFDC data plus TI FFDC data
454 *
455 * @param i_additional - Additional log data
456 * @param i_ti_InfoData - TI FFDC data
457 */
458void eventTerminate(std::map<std::string, std::string> i_additionalData,
459 char* i_tiInfoData)
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500460{
Ben Tynerb6401ed2021-01-11 09:08:07 -0600461
Ben Tyner29651ef2021-02-08 10:51:03 -0600462 uint32_t tiInfoSize = 0; // assume TI info was not available
Ben Tynerb6401ed2021-01-11 09:08:07 -0600463
Ben Tyner29651ef2021-02-08 10:51:03 -0600464 if (nullptr != i_tiInfoData)
Ben Tynerb6401ed2021-01-11 09:08:07 -0600465 {
Ben Tyner29651ef2021-02-08 10:51:03 -0600466 tiInfoSize = 56; // assume not hypervisor TI
Ben Tynerb6401ed2021-01-11 09:08:07 -0600467
Ben Tyner29651ef2021-02-08 10:51:03 -0600468 uint8_t subsystem = std::stoi(i_additionalData["Subsystem"]);
469
470 // If hypervisor
471 if (static_cast<uint8_t>(pel::SubsystemID::hypervisor) == subsystem)
Ben Tynerb6401ed2021-01-11 09:08:07 -0600472 {
Ben Tyner29651ef2021-02-08 10:51:03 -0600473 tiInfoSize = 1024; // assume hypervisor max
Ben Tynerb6401ed2021-01-11 09:08:07 -0600474
Ben Tyner29651ef2021-02-08 10:51:03 -0600475 // hypervisor may just want some of the data
476 if (0 == (*(i_tiInfoData + 0x09) & 0x01))
477 {
478 uint32_t* additionalLength = (uint32_t*)(i_tiInfoData + 0x50);
479 uint32_t tiAdditional = be32toh(*additionalLength);
480 tiInfoSize = std::min(tiInfoSize, (84 + tiAdditional));
481 }
Ben Tynerb6401ed2021-01-11 09:08:07 -0600482 }
483 }
484
485 std::string traceMsg = "TI info size = " + std::to_string(tiInfoSize);
486 trace<level::INFO>(traceMsg.c_str());
487
Ben Tynerf5210bb2021-01-05 12:58:10 -0600488 event(EventType::Terminate, i_additionalData,
Ben Tynerb6401ed2021-01-11 09:08:07 -0600489 createFFDCFiles(i_tiInfoData, tiInfoSize));
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500490}
491
Ben Tyner7f6ce6a2021-08-17 19:40:00 -0500492/** @brief Commit SBE vital event to log, returns event log ID */
493uint32_t eventVital()
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500494{
Ben Tynerf5210bb2021-01-05 12:58:10 -0600495 // Additional data for log
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500496 std::map<std::string, std::string> additionalData;
497
Ben Tynerf5210bb2021-01-05 12:58:10 -0600498 // Create log event with additional data and FFDC data
Ben Tyner7f6ce6a2021-08-17 19:40:00 -0500499 return event(EventType::Vital, additionalData, createFFDCFiles(nullptr, 0));
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500500}
501
Ben Tynerf5210bb2021-01-05 12:58:10 -0600502/**
Ben Tynerf5210bb2021-01-05 12:58:10 -0600503 * Commit attention handler failure event to log
504 *
505 * Create an event log containing the specified error code.
506 *
507 * @param i_error - Error code
508 */
509void eventAttentionFail(int i_error)
510{
511 // Additional data for log
512 std::map<std::string, std::string> additionalData;
513 additionalData["ERROR_CODE"] = std::to_string(i_error);
514
515 // Create log event with additional data and FFDC data
516 event(EventType::AttentionFail, additionalData,
517 createFFDCFiles(nullptr, 0));
518}
519
520/**
Ben Tynerb9715172021-09-29 08:46:19 -0500521 * Commit SBE timeout event to log
522 *
523 * Create an event log indicating an SBE operation timed out.
524 *
525 * @param proc - processor that encountered the error
526 */
527void eventPhalSbeChipop(uint32_t proc)
528{
529 trace<level::ERROR>("SBE error while getting TI info");
530
531 // report proc number in event log entry
532 std::map<std::string, std::string> additionalData;
533 additionalData.emplace("SRC6", std::to_string(proc << 16));
534
535 // create event with additional data and no ffdc
536 event(EventType::PhalSbeChipop, additionalData,
537 createFFDCFiles(nullptr, 0));
538}
539
540/**
Ben Tynerf5210bb2021-01-05 12:58:10 -0600541 * Parse systemd journal message field
542 *
543 * Parse the journal looking for the specified field and return the journal
544 * data for that field.
545 *
546 * @param journal - The journal to parse
547 * @param field - Field containing the data to retrieve
548 * @return Data for the speciefied field
549 */
Ben Tyner1b1915e2020-10-23 15:13:38 -0500550std::string sdjGetFieldValue(sd_journal* journal, const char* field)
551{
552 const char* data{nullptr};
553 size_t length{0};
Ben Tyner1b1915e2020-10-23 15:13:38 -0500554
555 // get field value
556 if (0 == sd_journal_get_data(journal, field, (const void**)&data, &length))
557 {
Zane Shelley9fb657f2021-01-12 15:30:58 -0600558 size_t prefix{0};
559
Ben Tyner1b1915e2020-10-23 15:13:38 -0500560 // The data returned by sd_journal_get_data will be prefixed with the
561 // field name and "="
562 const void* eq = memchr(data, '=', length);
563 if (nullptr != eq)
564 {
565 // get just data following the "="
566 prefix = (const char*)eq - data + 1;
567 }
568 else
569 {
570 // all the data (should not happen)
571 prefix = 0;
572 std::string value{}; // empty string
573 }
574
575 return std::string{data + prefix, length - prefix};
576 }
577 else
578 {
579 return std::string{}; // empty string
580 }
581}
582
Ben Tynerf5210bb2021-01-05 12:58:10 -0600583/**
584 * Gather messages from the journal
585 *
586 * Fetch journal entry data for all entries with the specified field equal to
587 * the specified value.
588 *
589 * @param field - Field to search on
590 * @param fieldValue - Value to search for
591 * @param max - Maximum number of messages fetch
592 * @return Vector of journal entry data
593 */
Ben Tyner1b1915e2020-10-23 15:13:38 -0500594std::vector<std::string> sdjGetMessages(const std::string& field,
595 const std::string& fieldValue,
596 unsigned int max)
597{
598 sd_journal* journal;
599 std::vector<std::string> messages;
600
601 if (0 == sd_journal_open(&journal, SD_JOURNAL_LOCAL_ONLY))
602 {
603 SD_JOURNAL_FOREACH_BACKWARDS(journal)
604 {
605 // Get input field
606 std::string value = sdjGetFieldValue(journal, field.c_str());
607
608 // Compare field value and read data
609 if (value == fieldValue)
610 {
611 // Get SYSLOG_IDENTIFIER field (process that logged message)
612 std::string syslog =
613 sdjGetFieldValue(journal, "SYSLOG_IDENTIFIER");
614
615 // Get _PID field
616 std::string pid = sdjGetFieldValue(journal, "_PID");
617
618 // Get MESSAGE field
619 std::string message = sdjGetFieldValue(journal, "MESSAGE");
620
621 // Get timestamp
622 uint64_t usec{0};
623 if (0 == sd_journal_get_realtime_usec(journal, &usec))
624 {
625
626 // Convert realtime microseconds to date format
627 char dateBuffer[80];
628 std::string date;
629 std::time_t timeInSecs = usec / 1000000;
630 strftime(dateBuffer, sizeof(dateBuffer), "%b %d %H:%M:%S",
631 std::localtime(&timeInSecs));
632 date = dateBuffer;
633
634 // Store value to messages
635 value = date + " " + syslog + "[" + pid + "]: " + message;
636 messages.insert(messages.begin(), value);
637 }
638 }
639
640 // limit maximum number of messages
641 if (messages.size() >= max)
642 {
643 break;
644 }
645 }
646
647 sd_journal_close(journal); // close journal when done
648 }
649
650 return messages;
651}
652
Ben Tynerb1ebfcb2020-05-08 18:52:48 -0500653} // namespace attn