-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathocc_ffdc.cpp
433 lines (375 loc) · 13.3 KB
/
occ_ffdc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#include "occ_ffdc.hpp"
#include "utils.hpp"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <nlohmann/json.hpp>
#include <org/open_power/OCC/Device/error.hpp>
#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/elog.hpp>
#include <phosphor-logging/lg2.hpp>
#include <phosphor-logging/log.hpp>
#include <xyz/openbmc_project/Common/error.hpp>
#include <xyz/openbmc_project/Logging/Create/server.hpp>
namespace open_power
{
namespace occ
{
static constexpr size_t max_ffdc_size = 8192;
static constexpr size_t sbe_status_header_size = 8;
static constexpr auto loggingObjectPath = "/xyz/openbmc_project/logging";
static constexpr auto opLoggingInterface = "org.open_power.Logging.PEL";
using namespace phosphor::logging;
using namespace sdbusplus::org::open_power::OCC::Device::Error;
using InternalFailure =
sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
uint32_t FFDC::createPEL(const char* path, uint32_t src6, const char* msg,
int fd)
{
uint32_t plid = 0;
std::vector<std::tuple<
sdbusplus::xyz::openbmc_project::Logging::server::Create::FFDCFormat,
uint8_t, uint8_t, sdbusplus::message::unix_fd>>
pelFFDCInfo;
lg2::info("Creating PEL for OCC{INST} with SBE FFDC: {PATH} - SRC6: {SRC}",
"INST", src6 >> 16, "PATH", path, "SRC", lg2::hex, src6);
if (fd > 0)
{
pelFFDCInfo.push_back(std::make_tuple(
sdbusplus::xyz::openbmc_project::Logging::server::Create::
FFDCFormat::Custom,
static_cast<uint8_t>(0xCB), static_cast<uint8_t>(0x01), fd));
}
// Add journal traces to PEL FFDC
auto occJournalFile =
addJournalEntries(pelFFDCInfo, "openpower-occ-control", 25);
std::map<std::string, std::string> additionalData;
additionalData.emplace("SRC6", std::to_string(src6));
additionalData.emplace("_PID", std::to_string(getpid()));
additionalData.emplace("SBE_ERR_MSG", msg);
auto& bus = utils::getBus();
try
{
std::string service =
utils::getService(loggingObjectPath, opLoggingInterface);
auto method =
bus.new_method_call(service.c_str(), loggingObjectPath,
opLoggingInterface, "CreatePELWithFFDCFiles");
// Set level to Notice (Informational). Error should trigger an OCC
// reset and if it does not recover, HTMGT/HBRT will create an
// unrecoverable error.
auto level =
sdbusplus::xyz::openbmc_project::Logging::server::convertForMessage(
sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level::
Notice);
method.append(path, level, additionalData, pelFFDCInfo);
auto response = bus.call(method);
std::tuple<uint32_t, uint32_t> reply = {0, 0};
response.read(reply);
plid = std::get<1>(reply);
}
catch (const sdbusplus::exception_t& e)
{
lg2::error("Failed to create PEL: {ERR}", "ERR", e.what());
}
return plid;
}
void FFDC::createOCCResetPEL(unsigned int instance, const char* path, int err,
const char* callout, const bool isInventoryCallout)
{
std::map<std::string, std::string> additionalData;
additionalData.emplace("_PID", std::to_string(getpid()));
if (err)
{
additionalData.emplace("CALLOUT_ERRNO", std::to_string(-err));
}
lg2::info("Creating OCC Reset PEL for OCC{INST}: {PATH}", "INST", instance,
"PATH", path);
if (callout)
{
if (isInventoryCallout)
{
lg2::info("adding inventory callout path {COPATH}", "COPATH",
std::string(callout));
additionalData.emplace("CALLOUT_INVENTORY_PATH",
std::string(callout));
}
else
{
lg2::info("adding device callout path {COPATH}, errno:{ERRNO}",
"COPATH", std::string(callout), "ERRNO", err);
additionalData.emplace("CALLOUT_DEVICE_PATH", std::string(callout));
}
}
additionalData.emplace("OCC", std::to_string(instance));
auto& bus = utils::getBus();
try
{
FFDCFiles ffdc;
// Add journal traces to PEL FFDC
auto occJournalFile =
addJournalEntries(ffdc, "openpower-occ-control", 25);
std::string service =
utils::getService(loggingObjectPath, opLoggingInterface);
auto method =
bus.new_method_call(service.c_str(), loggingObjectPath,
opLoggingInterface, "CreatePELWithFFDCFiles");
// Set level to Notice (Informational). Error should trigger an OCC
// reset and if it does not recover, HTMGT/HBRT will create an
// unrecoverable error.
auto level =
sdbusplus::xyz::openbmc_project::Logging::server::convertForMessage(
sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level::
Notice);
method.append(path, level, additionalData, ffdc);
bus.call(method);
}
catch (const sdbusplus::exception_t& e)
{
lg2::error("Failed to create OCC Reset PEL: {ERR}", "ERR", e.what());
}
}
// Reads the SBE FFDC file and create an error log
void FFDC::analyzeEvent()
{
int tfd = -1;
size_t total = 0;
auto data = std::make_unique<unsigned char[]>(max_ffdc_size);
while (total < max_ffdc_size)
{
auto r = read(fd, data.get() + total, max_ffdc_size - total);
if (r < 0)
{
elog<ReadFailure>(
phosphor::logging::org::open_power::OCC::Device::ReadFailure::
CALLOUT_ERRNO(errno),
phosphor::logging::org::open_power::OCC::Device::ReadFailure::
CALLOUT_DEVICE_PATH(file.c_str()));
return;
}
if (!r)
{
break;
}
total += r;
}
lseek(fd, 0, SEEK_SET);
if (!total)
{
// no error
return;
}
uint32_t src6 = instance << 16;
src6 |= *(data.get() + 2) << 8;
src6 |= *(data.get() + 3);
if (total > sbe_status_header_size)
{
std::string templateString =
fs::temp_directory_path() / "OCC_FFDC_XXXXXX";
tfd = mkostemp(templateString.data(), O_RDWR);
if (tfd < 0)
{
lg2::error("Couldn't create temporary FFDC file");
}
else
{
temporaryFiles.emplace_back(templateString, tfd);
size_t written = sbe_status_header_size;
while (written < total)
{
auto r = write(tfd, data.get() + written, total - written);
if (r < 0)
{
close(temporaryFiles.back().second);
fs::remove(temporaryFiles.back().first);
temporaryFiles.pop_back();
tfd = -1;
lg2::error("Couldn't write temporary FFDC file");
break;
}
if (!r)
{
break;
}
written += r;
}
}
}
createPEL("org.open_power.Processor.Error.SbeChipOpFailure", src6,
"SBE command reported error", tfd);
}
// Create file with the latest journal entries for specified executable
std::unique_ptr<FFDCFile> FFDC::addJournalEntries(
FFDCFiles& fileList, const std::string& executable, unsigned int lines)
{
auto journalFile = makeJsonFFDCFile(getJournalEntries(lines, executable));
if (journalFile && journalFile->fd() != -1)
{
lg2::debug(
"addJournalEntries: Added up to {NUM} journal entries for {APP}",
"NUM", lines, "APP", executable);
fileList.emplace_back(FFDCFormat::JSON, 0x01, 0x01, journalFile->fd());
}
else
{
lg2::error("addJournalEntries: Failed to add journal entries for {APP}",
"APP", executable);
}
return journalFile;
}
// Write JSON data into FFDC file and return the file
std::unique_ptr<FFDCFile> FFDC::makeJsonFFDCFile(const nlohmann::json& ffdcData)
{
std::string tmpFile = fs::temp_directory_path() / "OCC_JOURNAL_XXXXXX";
auto fd = mkostemp(tmpFile.data(), O_RDWR);
if (fd != -1)
{
auto jsonString = ffdcData.dump();
auto rc = write(fd, jsonString.data(), jsonString.size());
close(fd);
if (rc != -1)
{
fs::path jsonFile{tmpFile};
return std::make_unique<FFDCFile>(jsonFile);
}
else
{
auto e = errno;
lg2::error(
"makeJsonFFDCFile: Failed call to write JSON FFDC file, errno={ERR}",
"ERR", e);
}
}
else
{
auto e = errno;
lg2::error("makeJsonFFDCFile: Failed called to mkostemp, errno={ERR}",
"ERR", e);
}
return nullptr;
}
// Collect the latest journal entries for a specified executable
nlohmann::json FFDC::getJournalEntries(int numLines, std::string executable)
{
// Sleep 100ms; otherwise recent journal entries sometimes not available
using namespace std::chrono_literals;
std::this_thread::sleep_for(100ms);
std::vector<std::string> entries;
// Open the journal
sd_journal* journal;
int rc = sd_journal_open(&journal, SD_JOURNAL_LOCAL_ONLY);
if (rc < 0)
{
// Build one line string containing field values
entries.push_back("[Internal error: sd_journal_open(), rc=" +
std::string(strerror(rc)) + "]");
return nlohmann::json(entries);
}
// Create object to automatically close journal
JournalCloser closer{journal};
// Add match so we only loop over entries with specified field value
std::string field{"SYSLOG_IDENTIFIER"};
std::string match{field + '=' + executable};
rc = sd_journal_add_match(journal, match.c_str(), 0);
if (rc < 0)
{
// Build one line string containing field values
entries.push_back("[Internal error: sd_journal_add_match(), rc=" +
std::string(strerror(rc)) + "]");
}
else
{
int count{1};
entries.reserve(numLines);
std::string syslogID, pid, message, timeStamp;
// Loop through journal entries from newest to oldest
SD_JOURNAL_FOREACH_BACKWARDS(journal)
{
// Get relevant journal entry fields
timeStamp = getTimeStamp(journal);
syslogID = getFieldValue(journal, "SYSLOG_IDENTIFIER");
pid = getFieldValue(journal, "_PID");
message = getFieldValue(journal, "MESSAGE");
// Build one line string containing field values
entries.push_back(
timeStamp + " " + syslogID + "[" + pid + "]: " + message);
// Stop after number of lines was read
if (count++ >= numLines)
{
break;
}
}
}
// put the journal entries in chronological order
std::reverse(entries.begin(), entries.end());
return nlohmann::json(entries);
}
std::string FFDC::getTimeStamp(sd_journal* journal)
{
// Get realtime (wallclock) timestamp of current journal entry. The
// timestamp is in microseconds since the epoch.
uint64_t usec{0};
int rc = sd_journal_get_realtime_usec(journal, &usec);
if (rc < 0)
{
return "[Internal error: sd_journal_get_realtime_usec(), rc=" +
std::string(strerror(rc)) + "]";
}
// Convert to number of seconds since the epoch
time_t secs = usec / 1000000;
// Convert seconds to tm struct required by strftime()
struct tm* timeStruct = localtime(&secs);
if (timeStruct == nullptr)
{
return "[Internal error: localtime() returned nullptr]";
}
// Convert tm struct into a date/time string
char timeStamp[80];
strftime(timeStamp, sizeof(timeStamp), "%b %d %H:%M:%S", timeStruct);
return timeStamp;
}
std::string FFDC::getFieldValue(sd_journal* journal, const std::string& field)
{
std::string value{};
// Get field data from current journal entry
const void* data{nullptr};
size_t length{0};
int rc = sd_journal_get_data(journal, field.c_str(), &data, &length);
if (rc < 0)
{
if (-rc == ENOENT)
{
// Current entry does not include this field; return empty value
return value;
}
else
{
return "[Internal error: sd_journal_get_data() rc=" +
std::string(strerror(rc)) + "]";
}
}
// Get value from field data. Field data in format "FIELD=value".
std::string dataString{static_cast<const char*>(data), length};
std::string::size_type pos = dataString.find('=');
if ((pos != std::string::npos) && ((pos + 1) < dataString.size()))
{
// Value is substring after the '='
value = dataString.substr(pos + 1);
}
return value;
}
// Create temporary file that will automatically get removed when destructed
FFDCFile::FFDCFile(const fs::path& name) :
_fd(open(name.c_str(), O_RDONLY)), _name(name)
{
if (_fd() == -1)
{
auto e = errno;
lg2::error("FFDCFile: Could not open FFDC file {FILE}. errno {ERR}",
"FILE", _name.string(), "ERR", e);
}
}
} // namespace occ
} // namespace open_power