Attn: Add FFDC logs to PEL

Gather trace messages from systemd journal and add them as a user data
secion to the PEL.

Signed-off-by: Ben Tyner <ben.tyner@ibm.com>
Change-Id: I806915281d9aefd194f90e752c27a89ad375fb13
diff --git a/attn/attn_logging.cpp b/attn/attn_logging.cpp
index cbe3afd..d802437 100644
--- a/attn/attn_logging.cpp
+++ b/attn/attn_logging.cpp
@@ -2,7 +2,6 @@
 
 #include <attn/attn_logging.hpp>
 #include <phosphor-logging/log.hpp>
-
 namespace attn
 {
 
@@ -55,12 +54,18 @@
         // using direct create method (for additional data)
         auto method = bus.new_method_call(
             "xyz.openbmc_project.Logging", "/xyz/openbmc_project/logging",
-            "xyz.openbmc_project.Logging.Create", "Create");
+            "xyz.openbmc_project.Logging.Create", "CreateWithFFDCFiles");
+
+        // Create FFDC files containing debug data to store in error log
+        std::vector<util::FFDCFile> files{createFFDCFiles()};
+
+        // Create FFDC tuples used to pass FFDC files to D-Bus method
+        std::vector<FFDCTuple> ffdcTuples{createFFDCTuples(files)};
 
         // attach additional data
         method.append(eventName,
                       "xyz.openbmc_project.Logging.Entry.Level.Error",
-                      i_additional);
+                      i_additional, ffdcTuples);
 
         // log the event
         auto reply = bus.call(method);
@@ -129,4 +134,171 @@
     event(EventType::AttentionFail, additionalData);
 }
 
+/** @brief parse systemd journal message field */
+std::string sdjGetFieldValue(sd_journal* journal, const char* field)
+{
+    const char* data{nullptr};
+    size_t length{0};
+    size_t prefix{0};
+
+    // get field value
+    if (0 == sd_journal_get_data(journal, field, (const void**)&data, &length))
+    {
+        // The data returned  by sd_journal_get_data will be prefixed with the
+        // field name and "="
+        const void* eq = memchr(data, '=', length);
+        if (nullptr != eq)
+        {
+            // get just data following the "="
+            prefix = (const char*)eq - data + 1;
+        }
+        else
+        {
+            // all the data (should not happen)
+            prefix = 0;
+            std::string value{}; // empty string
+        }
+
+        return std::string{data + prefix, length - prefix};
+    }
+    else
+    {
+        return std::string{}; // empty string
+    }
+}
+
+/** @brief get messages from systemd journal */
+std::vector<std::string> sdjGetMessages(const std::string& field,
+                                        const std::string& fieldValue,
+                                        unsigned int max)
+{
+    sd_journal* journal;
+    std::vector<std::string> messages;
+
+    if (0 == sd_journal_open(&journal, SD_JOURNAL_LOCAL_ONLY))
+    {
+        SD_JOURNAL_FOREACH_BACKWARDS(journal)
+        {
+            // Get input field
+            std::string value = sdjGetFieldValue(journal, field.c_str());
+
+            // Compare field value and read data
+            if (value == fieldValue)
+            {
+                // Get SYSLOG_IDENTIFIER field (process that logged message)
+                std::string syslog =
+                    sdjGetFieldValue(journal, "SYSLOG_IDENTIFIER");
+
+                // Get _PID field
+                std::string pid = sdjGetFieldValue(journal, "_PID");
+
+                // Get MESSAGE field
+                std::string message = sdjGetFieldValue(journal, "MESSAGE");
+
+                // Get timestamp
+                uint64_t usec{0};
+                if (0 == sd_journal_get_realtime_usec(journal, &usec))
+                {
+
+                    // Convert realtime microseconds to date format
+                    char dateBuffer[80];
+                    std::string date;
+                    std::time_t timeInSecs = usec / 1000000;
+                    strftime(dateBuffer, sizeof(dateBuffer), "%b %d %H:%M:%S",
+                             std::localtime(&timeInSecs));
+                    date = dateBuffer;
+
+                    // Store value to messages
+                    value = date + " " + syslog + "[" + pid + "]: " + message;
+                    messages.insert(messages.begin(), value);
+                }
+            }
+
+            // limit maximum number of messages
+            if (messages.size() >= max)
+            {
+                break;
+            }
+        }
+
+        sd_journal_close(journal); // close journal when done
+    }
+
+    return messages;
+}
+
+/** @brief create a file containing FFDC data */
+util::FFDCFile createFFDCFile(const std::vector<std::string>& lines)
+{
+    // Create FFDC file of type Text
+    util::FFDCFile file{util::FFDCFormat::Text};
+    int fd = file.getFileDescriptor();
+
+    // Write FFDC lines to file
+    std::string buffer;
+    for (const std::string& line : lines)
+    {
+        // Copy line to buffer.  Add newline if necessary.
+        buffer = line;
+        if (line.empty() || (line.back() != '\n'))
+        {
+            buffer += '\n';
+        }
+
+        // write buffer to file
+        write(fd, buffer.c_str(), buffer.size());
+    }
+
+    // Seek to beginning of file so error logging system can read data
+    lseek(fd, 0, SEEK_SET);
+
+    return file;
+}
+
+/** @brief Create FDDC files from journal messages of relevant executables */
+std::vector<util::FFDCFile> createFFDCFiles()
+{
+    std::vector<util::FFDCFile> files{};
+
+    // Executables of interest
+    std::vector<std::string> executables{"openpower-hw-diags"};
+
+    for (const std::string& executable : executables)
+    {
+        try
+        {
+            // get journal messages
+            std::vector<std::string> messages =
+                sdjGetMessages("SYSLOG_IDENTIFIER", executable, 30);
+
+            // Create FFDC file containing the journal messages
+            if (!messages.empty())
+            {
+                files.emplace_back(createFFDCFile(messages));
+            }
+        }
+        catch (const std::exception& e)
+        {
+            std::stringstream ss;
+            ss << "createFFDCFiles: " << e.what();
+            trace<level::INFO>(ss.str().c_str());
+        }
+    }
+
+    return files;
+}
+
+/** create tuples of FFDC files */
+std::vector<FFDCTuple> createFFDCTuples(std::vector<util::FFDCFile>& files)
+{
+    std::vector<FFDCTuple> ffdcTuples{};
+    for (util::FFDCFile& file : files)
+    {
+        ffdcTuples.emplace_back(
+            file.getFormat(), file.getSubType(), file.getVersion(),
+            sdbusplus::message::unix_fd(file.getFileDescriptor()));
+    }
+    return ffdcTuples;
+}
+
 } // namespace attn
diff --git a/attn/attn_logging.hpp b/attn/attn_logging.hpp
index e21418a..8b5feba 100644
--- a/attn/attn_logging.hpp
+++ b/attn/attn_logging.hpp
@@ -1,8 +1,11 @@
 #pragma once
 
+#include <util/ffdc_file.hpp>
+
 #include <cstddef> // for size_t
 #include <map>
 #include <string>
+#include <vector>
 
 namespace attn
 {
@@ -45,4 +48,39 @@
 /** @brief commit attention handler failure event to log */
 void eventAttentionFail(int i_error);
 
+using FFDCTuple =
+    std::tuple<util::FFDCFormat, uint8_t, uint8_t, sdbusplus::message::unix_fd>;
+
+/**
+ * Create an FFDCFile object containing the specified lines of text data.
+ *
+ * Throws an exception if an error occurs.
+ *
+ * @param lines lines of text data to write to file
+ * @return FFDCFile object
+ */
+util::FFDCFile createFFDCFile(const std::vector<std::string>& lines);
+
+/**
+ * Create FFDCFile objects containing debug data to store in the error log.
+ *
+ * If an error occurs, the error is written to the journal but an exception
+ * is not thrown.
+ *
+ * @param journal system journal
+ * @return vector of FFDCFile objects
+ */
+std::vector<util::FFDCFile> createFFDCFiles();
+
+/**
+ * Create FFDCTuple objects corresponding to the specified FFDC files.
+ *
+ * The D-Bus method to create an error log requires a vector of tuples to
+ * pass in the FFDC file information.
+ *
+ * @param files FFDC files
+ * @return vector of FFDCTuple objects
+ */
+std::vector<FFDCTuple> createFFDCTuples(std::vector<util::FFDCFile>& files);
+
 } // namespace attn