PEL: Save process name in a UserData section

When creating a new PEL, add a UserData section that contains various
pieces of system information that will be saved in every error log.

In this first commit, just save the process name of the creator,
provided they passed in the '_PID' AdditionalData item containing their
PID.

Future commits will add other items like certain D-Bus properties.

Signed-off-by: Matt Spinler <spinler@us.ibm.com>
Change-Id: I7139b4056e494277ff3388bfa8a00002c9c89dc1
diff --git a/extensions/openpower-pels/pel.cpp b/extensions/openpower-pels/pel.cpp
index 625d177..0c05ac1 100644
--- a/extensions/openpower-pels/pel.cpp
+++ b/extensions/openpower-pels/pel.cpp
@@ -56,9 +56,12 @@
     auto mtms = std::make_unique<FailingMTMS>(dataIface);
     _optionalSections.push_back(std::move(mtms));
 
+    auto ud = util::makeSysInfoUserDataSection(additionalData, dataIface);
+    _optionalSections.push_back(std::move(ud));
+
     if (!additionalData.empty())
     {
-        auto ud = util::makeADUserDataSection(additionalData);
+        ud = util::makeADUserDataSection(additionalData);
 
         // To be safe, check there isn't too much data
         if (size() + ud->header().size <= _maxPELSize)
@@ -299,6 +302,23 @@
 namespace util
 {
 
+std::unique_ptr<UserData> makeJSONUserDataSection(const nlohmann::json& json)
+{
+    auto jsonString = json.dump();
+    std::vector<uint8_t> jsonData(jsonString.begin(), jsonString.end());
+
+    // Pad to a 4 byte boundary
+    while ((jsonData.size() % 4) != 0)
+    {
+        jsonData.push_back(0);
+    }
+
+    return std::make_unique<UserData>(
+        static_cast<uint16_t>(ComponentID::phosphorLogging),
+        static_cast<uint8_t>(UserDataFormat::json),
+        static_cast<uint8_t>(UserDataFormatVersion::json), jsonData);
+}
+
 std::unique_ptr<UserData> makeADUserDataSection(const AdditionalData& ad)
 {
     assert(!ad.empty());
@@ -316,19 +336,42 @@
         json = ad.toJSON();
     }
 
-    auto jsonString = json.dump();
-    std::vector<uint8_t> jsonData(jsonString.begin(), jsonString.end());
+    return makeJSONUserDataSection(json);
+}
 
-    // Pad to a 4 byte boundary
-    while ((jsonData.size() % 4) != 0)
+void addProcessNameToJSON(nlohmann::json& json,
+                          const std::optional<std::string>& pid,
+                          const DataInterfaceBase& dataIface)
+{
+    std::string name = "Unknown";
+
+    try
     {
-        jsonData.push_back(0);
+        if (pid)
+        {
+            auto n = dataIface.getProcessName(*pid);
+            if (n)
+            {
+                name = *n;
+            }
+        }
+    }
+    catch (std::exception& e)
+    {
     }
 
-    return std::make_unique<UserData>(
-        static_cast<uint16_t>(ComponentID::phosphorLogging),
-        static_cast<uint8_t>(UserDataFormat::json),
-        static_cast<uint8_t>(UserDataFormatVersion::json), jsonData);
+    json["Process Name"] = std::move(name);
+}
+
+std::unique_ptr<UserData>
+    makeSysInfoUserDataSection(const AdditionalData& ad,
+                               const DataInterfaceBase& dataIface)
+{
+    nlohmann::json json;
+
+    addProcessNameToJSON(json, ad.getValue("_PID"), dataIface);
+
+    return makeJSONUserDataSection(json);
 }
 
 } // namespace util