forked from mchehab/rasdaemon
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ras-non-standard-handler.c
275 lines (235 loc) · 5.97 KB
/
ras-non-standard-handler.c
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
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2016, The Linux Foundation. All rights reserved.
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <traceevent/kbuffer.h>
#include <unistd.h>
#include "ras-logger.h"
#include "ras-non-standard-handler.h"
#include "ras-report.h"
#include "types.h"
static struct ras_ns_ev_decoder *ras_ns_ev_dec_list;
void print_le_hex(struct trace_seq *s, const uint8_t *buf, int index)
{
trace_seq_printf(s, "%02x%02x%02x%02x",
buf[index + 3], buf[index + 2],
buf[index + 1], buf[index]);
}
static char *uuid_le(const char *uu)
{
static char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
char *p = uuid;
int i;
static const unsigned char le[16] = {3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15};
for (i = 0; i < 16; i++) {
p += snprintf(p, sizeof(uuid), "%.2x", (unsigned char)uu[le[i]]);
switch (i) {
case 3:
case 5:
case 7:
case 9:
*p++ = '-';
break;
}
}
*p = 0;
return uuid;
}
int register_ns_ev_decoder(struct ras_ns_ev_decoder *ns_ev_decoder)
{
struct ras_ns_ev_decoder *list;
if (!ns_ev_decoder)
return -1;
ns_ev_decoder->next = NULL;
#ifdef HAVE_SQLITE3
ns_ev_decoder->stmt_dec_record = NULL;
#endif
if (!ras_ns_ev_dec_list) {
ras_ns_ev_dec_list = ns_ev_decoder;
ras_ns_ev_dec_list->ref_count = 0;
} else {
list = ras_ns_ev_dec_list;
while (list->next)
list = list->next;
list->next = ns_ev_decoder;
}
return 0;
}
int ras_ns_add_vendor_tables(struct ras_events *ras)
{
struct ras_ns_ev_decoder *ns_ev_decoder;
int error = 0;
#ifdef HAVE_SQLITE3
if (!ras)
return -1;
ns_ev_decoder = ras_ns_ev_dec_list;
if (ras_ns_ev_dec_list)
ras_ns_ev_dec_list->ref_count++;
while (ns_ev_decoder) {
if (ns_ev_decoder->add_table && !ns_ev_decoder->stmt_dec_record) {
error = ns_ev_decoder->add_table(ras, ns_ev_decoder);
if (error)
break;
}
ns_ev_decoder = ns_ev_decoder->next;
}
if (error)
return -1;
#endif
return 0;
}
static int find_ns_ev_decoder(const char *sec_type, struct ras_ns_ev_decoder **p_ns_ev_dec)
{
struct ras_ns_ev_decoder *ns_ev_decoder;
int match = 0;
ns_ev_decoder = ras_ns_ev_dec_list;
while (ns_ev_decoder) {
if (strcmp(uuid_le(sec_type), ns_ev_decoder->sec_type) == 0) {
*p_ns_ev_dec = ns_ev_decoder;
match = 1;
break;
}
ns_ev_decoder = ns_ev_decoder->next;
}
if (!match)
return -1;
return 0;
}
void ras_ns_finalize_vendor_tables(void)
{
#ifdef HAVE_SQLITE3
struct ras_ns_ev_decoder *ns_ev_decoder = ras_ns_ev_dec_list;
if (!ras_ns_ev_dec_list)
return;
if (ras_ns_ev_dec_list->ref_count > 0)
ras_ns_ev_dec_list->ref_count--;
else
return;
if (ras_ns_ev_dec_list->ref_count > 0)
return;
while (ns_ev_decoder) {
if (ns_ev_decoder->stmt_dec_record) {
ras_mc_finalize_vendor_table(ns_ev_decoder->stmt_dec_record);
ns_ev_decoder->stmt_dec_record = NULL;
}
ns_ev_decoder = ns_ev_decoder->next;
}
#endif
}
static void unregister_ns_ev_decoder(void)
{
#ifdef HAVE_SQLITE3
if (!ras_ns_ev_dec_list)
return;
ras_ns_ev_dec_list->ref_count = 1;
ras_ns_finalize_vendor_tables();
#endif
ras_ns_ev_dec_list = NULL;
}
int ras_non_standard_event_handler(struct trace_seq *s,
struct tep_record *record,
struct tep_event *event, void *context)
{
int len, i, line_count;
unsigned long long val;
struct ras_events *ras = context;
time_t now;
struct tm *tm;
struct ras_non_standard_event ev;
struct ras_ns_ev_decoder *ns_ev_decoder;
/*
* Newer kernels (3.10-rc1 or upper) provide an uptime clock.
* On previous kernels, the way to properly generate an event would
* be to inject a fake one, measure its timestamp and diff it against
* gettimeofday. We won't do it here. Instead, let's use uptime,
* falling-back to the event report's time, if "uptime" clock is
* not available (legacy kernels).
*/
if (ras->use_uptime)
now = record->ts / user_hz + ras->uptime_diff;
else
now = time(NULL);
tm = localtime(&now);
if (tm)
strftime(ev.timestamp, sizeof(ev.timestamp),
"%Y-%m-%d %H:%M:%S %z", tm);
trace_seq_printf(s, "%s ", ev.timestamp);
if (tep_get_field_val(s, event, "sev", record, &val, 1) < 0)
return -1;
switch (val) {
case GHES_SEV_NO:
ev.severity = "Informational";
break;
case GHES_SEV_CORRECTED:
ev.severity = "Corrected";
break;
case GHES_SEV_RECOVERABLE:
ev.severity = "Recoverable";
break;
default:
case GHES_SEV_PANIC:
ev.severity = "Fatal";
}
trace_seq_printf(s, " %s", ev.severity);
ev.sec_type = tep_get_field_raw(s, event, "sec_type",
record, &len, 1);
if (!ev.sec_type)
return -1;
if (strcmp(uuid_le(ev.sec_type),
"e8ed898d-df16-43cc-8ecc-54f060ef157f") == 0)
trace_seq_printf(s, " section type: %s",
"Ampere Specific Error");
else
trace_seq_printf(s, " section type: %s",
uuid_le(ev.sec_type));
ev.fru_text = tep_get_field_raw(s, event, "fru_text",
record, &len, 1);
ev.fru_id = tep_get_field_raw(s, event, "fru_id",
record, &len, 1);
trace_seq_printf(s, " fru text: %s fru id: %s ",
ev.fru_text, uuid_le(ev.fru_id));
if (tep_get_field_val(s, event, "len", record, &val, 1) < 0)
return -1;
ev.length = val;
trace_seq_printf(s, " length: %d", ev.length);
ev.error = tep_get_field_raw(s, event, "buf", record, &len, 1);
if (!ev.error)
return -1;
if (!find_ns_ev_decoder(ev.sec_type, &ns_ev_decoder)) {
ns_ev_decoder->decode(ras, ns_ev_decoder, s, &ev);
} else {
len = ev.length;
i = 0;
line_count = 0;
trace_seq_printf(s, " error:\n %08x: ", i);
while (len >= 4) {
print_le_hex(s, ev.error, i);
i += 4;
len -= 4;
if (++line_count == 4) {
trace_seq_printf(s, "\n %08x: ", i);
line_count = 0;
} else {
trace_seq_printf(s, " ");
}
}
}
/* Insert data into the SGBD */
#ifdef HAVE_SQLITE3
ras_store_non_standard_record(ras, &ev);
#endif
#ifdef HAVE_ABRT_REPORT
/* Report event to ABRT */
ras_report_non_standard_event(ras, &ev);
#endif
return 0;
}
__attribute__((destructor))
static void ns_exit(void)
{
unregister_ns_ev_decoder();
}