./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
109 / 112
97.3%
Branches:
56 / 60
93.3%
Functions:
13 / 13
100.0%

kfsw-services/src/log_history.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <string.h>
3
4 #include <zephyr/kernel.h>
5 #include <zephyr/sys/cbprintf.h>
6 #include <zephyr/sys/crc.h>
7 #include <zephyr/sys/util.h>
8
9 #include "log_history_internal.h"
10
11 #if CONFIG_KFSW_LOG_HISTORY_RETAINED
12 /* Out of .bss, so start-up does not clear it. */
13 #define KFSW_LOG_RETAINED __noinit
14 #else
15 #define KFSW_LOG_RETAINED
16 #endif
17
18 BUILD_ASSERT(CBPRINTF_PACKAGE_ALIGNMENT <= 8, "a held package must be aligned for cbprintf");
19
20 static KFSW_LOG_RETAINED struct kfsw_log_encoded records[CONFIG_KFSW_LOG_HISTORY_DEPTH];
21 static KFSW_LOG_RETAINED struct kfsw_log_retained_header header;
22 static struct k_spinlock history_lock;
23 /* In .bss either way, so it is false once per boot. */
24 static bool restored;
25
26 1610 static uint32_t header_crc(const struct kfsw_log_retained_header *value)
27 {
28 /* Over everything except the CRC itself. */
29 1610 return crc32_ieee((const uint8_t *)value, offsetof(struct kfsw_log_retained_header, crc));
30 }
31
32 /*
33 * Changes with the revisions and with where the image keeps its read-only
34 * data, which is where package format strings point.
35 */
36 212 static uint32_t image_identity(void)
37 {
38 static const char revisions[] = KFSW_BUILD_REVISIONS;
39 212 const uintptr_t anchor = (uintptr_t)revisions;
40
41 212 return crc32_ieee_update(crc32_ieee((const uint8_t *)revisions, sizeof(revisions)),
42 (const uint8_t *)&anchor, sizeof(anchor));
43 }
44
45 /* Caller holds the lock. */
46 1506 static void publish_header(void)
47 {
48 1506 header.crc = header_crc(&header);
49 1506 }
50
51 /*
52 * Decide once per boot whether the retained ring belongs to this image. A
53 * mismatch starts clean; individual records are checked as they are read.
54 * Caller holds the lock.
55 */
56 9110 static void restore_once(void)
57 {
58 bool valid;
59
60
2/2
✓ Branch 0 taken 8994 times.
✓ Branch 1 taken 116 times.
9110 if (restored) {
61 8994 return;
62 }
63 116 restored = true;
64
65 296 valid = (header.magic == KFSW_LOG_RETAINED_MAGIC) &&
66
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 8 times.
64 (header.version == KFSW_LOG_RETAINED_VERSION) &&
67
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 8 times.
56 (header.depth == ARRAY_SIZE(records)) &&
68
4/4
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 8 times.
✓ Branch 3 taken 32 times.
✓ Branch 4 taken 8 times.
48 (header.record_size == sizeof(records[0])) && (header.image == image_identity()) &&
69
6/6
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 52 times.
✓ Branch 2 taken 24 times.
✓ Branch 3 taken 8 times.
✓ Branch 5 taken 16 times.
✓ Branch 6 taken 8 times.
180 (header.next_sequence > 0U) && (header.crc == header_crc(&header));
70
71
2/2
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 16 times.
116 if (!valid) {
72 100 header = (struct kfsw_log_retained_header){
73 .magic = KFSW_LOG_RETAINED_MAGIC,
74 .version = KFSW_LOG_RETAINED_VERSION,
75 .depth = ARRAY_SIZE(records),
76 .record_size = sizeof(records[0]),
77 100 .image = image_identity(),
78 .next_sequence = 1U,
79 };
80 100 publish_header();
81 }
82 }
83
84 1406 void kfsw_log_history_append(uint8_t module, uint8_t severity, const char *format, va_list args,
85 const char *text, bool truncated)
86 {
87 1406 k_spinlock_key_t key = k_spin_lock(&history_lock);
88 struct kfsw_log_encoded *record;
89 va_list copy;
90 int size;
91
92 1406 restore_once();
93 1406 record = &records[(header.next_sequence - 1U) % ARRAY_SIZE(records)];
94 1406 *record = (struct kfsw_log_encoded){
95 1406 .sequence = header.next_sequence++,
96 1406 .uptime_ms = (uint64_t)k_uptime_get(),
97 .module = module,
98 .severity = severity,
99 .truncated = truncated,
100 };
101
102 /* Packaged in place, so the record costs no stack in the caller. */
103 1406 va_copy(copy, args);
104 1406 size = cbvprintf_package(record->data, sizeof(record->data), 0U, format, copy);
105 1406 va_end(copy);
106
2/2
✓ Branch 0 taken 1364 times.
✓ Branch 1 taken 42 times.
1406 if (size > 0) {
107 1364 record->package = true;
108 1364 record->size = (uint16_t)size;
109 } else {
110 42 size_t length = 0U;
111
112
4/4
✓ Branch 0 taken 5214 times.
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 5208 times.
✓ Branch 3 taken 6 times.
5250 while (length < sizeof(record->data) && text[length] != '\0') {
113 5208 length++;
114 }
115
116
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 6 times.
42 if (length == sizeof(record->data)) {
117 36 length--;
118 36 record->truncated = true;
119 }
120 42 memcpy(record->data, text, length);
121 42 record->size = (uint16_t)length;
122 }
123 1406 publish_header();
124 1406 k_spin_unlock(&history_lock, key);
125 1406 }
126
127 1166 int kfsw_log_history_window(uint16_t count, struct kfsw_log_history_window *window)
128 {
129 k_spinlock_key_t key;
130 uint64_t held;
131
132
6/6
✓ Branch 0 taken 1158 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 1150 times.
✓ Branch 3 taken 8 times.
✓ Branch 4 taken 8 times.
✓ Branch 5 taken 1142 times.
1166 if ((window == NULL) || (count == 0U) || (count > KFSW_LOG_HISTORY_MAX_READ)) {
133 24 return -EINVAL;
134 }
135 1142 key = k_spin_lock(&history_lock);
136 1142 restore_once();
137 1142 held = MIN(header.next_sequence - 1U, ARRAY_SIZE(records));
138 1142 window->end = header.next_sequence;
139 1142 window->first = header.next_sequence - MIN(held, count);
140 1142 window->overwritten = header.next_sequence - 1U - held;
141 1142 k_spin_unlock(&history_lock, key);
142 1142 return 0;
143 }
144
145 6562 int kfsw_log_history_get_encoded(uint64_t sequence, struct kfsw_log_encoded *record)
146 {
147 k_spinlock_key_t key;
148 const struct kfsw_log_encoded *held;
149
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6562 times.
6562 if (record == NULL) {
151 ✗ return -EINVAL;
152 }
153 6562 key = k_spin_lock(&history_lock);
154 6562 restore_once();
155
4/4
✓ Branch 0 taken 6554 times.
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 6498 times.
✓ Branch 3 taken 56 times.
6562 if ((sequence == 0U) || (sequence >= header.next_sequence) ||
156
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 6490 times.
6498 ((header.next_sequence - sequence) > ARRAY_SIZE(records))) {
157 72 k_spin_unlock(&history_lock, key);
158 72 return -ENOENT;
159 }
160 6490 held = &records[(sequence - 1U) % ARRAY_SIZE(records)];
161 /* A retained slot that disagrees with its own index did not survive. */
162
3/4
✓ Branch 0 taken 6426 times.
✓ Branch 1 taken 64 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6426 times.
6490 if ((held->sequence != sequence) || (held->size > sizeof(held->data))) {
163 64 k_spin_unlock(&history_lock, key);
164 64 return -ENOENT;
165 }
166 6426 *record = *held;
167 6426 k_spin_unlock(&history_lock, key);
168 6426 return 0;
169 }
170
171 struct text_sink {
172 char *text;
173 size_t length;
174 bool full;
175 };
176
177 83706 static int put_char(int c, void *context)
178 {
179 83706 struct text_sink *sink = context;
180
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 83706 times.
83706 if (sink->length + 1U >= KFSW_LOG_TEXT_SIZE) {
182 ✗ sink->full = true;
183 ✗ return c;
184 }
185
4/4
✓ Branch 0 taken 83626 times.
✓ Branch 1 taken 80 times.
✓ Branch 2 taken 83546 times.
✓ Branch 3 taken 80 times.
83706 sink->text[sink->length++] = ((c == '\n') || (c == '\r')) ? ' ' : (char)c;
186 83706 return c;
187 }
188
189 6418 void kfsw_log_history_format(const struct kfsw_log_encoded *encoded, struct kfsw_log_record *record)
190 {
191 6418 struct text_sink sink = {0};
192
193 6418 *record = (struct kfsw_log_record){
194 6418 .sequence = encoded->sequence,
195 6418 .uptime_ms = encoded->uptime_ms,
196 6418 .module = encoded->module,
197 6418 .severity = encoded->severity,
198 6418 .truncated = encoded->truncated,
199 };
200 6418 sink.text = record->text;
201
2/2
✓ Branch 0 taken 6382 times.
✓ Branch 1 taken 36 times.
6418 if (encoded->package) {
202 6382 (void)cbpprintf(put_char, &sink, (void *)encoded->data);
203 } else {
204 36 sink.length = MIN(encoded->size, sizeof(record->text) - 1U);
205 36 memcpy(record->text, encoded->data, sink.length);
206 }
207 6418 record->text[sink.length] = '\0';
208
3/4
✓ Branch 0 taken 6388 times.
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6388 times.
6418 record->truncated = record->truncated || sink.full;
209 6418 }
210
211 6508 int kfsw_log_history_get(uint64_t sequence, struct kfsw_log_record *record)
212 {
213 struct kfsw_log_encoded encoded;
214 int result;
215
216
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 6500 times.
6508 if (record == NULL) {
217 8 return -EINVAL;
218 }
219 6500 result = kfsw_log_history_get_encoded(sequence, &encoded);
220
2/2
✓ Branch 0 taken 6364 times.
✓ Branch 1 taken 136 times.
6500 if (result == 0) {
221 6364 kfsw_log_history_format(&encoded, record);
222 }
223 6500 return result;
224 }
225
226 #if CONFIG_ZTEST
227 72 uint32_t kfsw_log_history_image(void)
228 {
229 72 return image_identity();
230 }
231
232 80 uint32_t kfsw_log_history_header_crc(const struct kfsw_log_retained_header *retained)
233 {
234 80 return header_crc(retained);
235 }
236
237 96 void kfsw_log_history_install_retained(const struct kfsw_log_retained_header *retained)
238 {
239 96 k_spinlock_key_t key = k_spin_lock(&history_lock);
240
241 96 header = *retained;
242 96 restored = false;
243 96 k_spin_unlock(&history_lock, key);
244 96 }
245 #endif
246