Zephyr API Documentation 4.5.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
coredump.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Intel Corporation.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_DEBUG_COREDUMP_H_
8#define ZEPHYR_INCLUDE_DEBUG_COREDUMP_H_
9
10#include <stddef.h>
11#include <stdint.h>
12#include <sys/types.h>
13
14#ifdef __cplusplus
15extern "C" {
16#endif
17
18/*
19 * Define COREDUMP_*_STR as public to allow coredump_backend_other to re-use
20 * these strings if necessary
21 */
22#define COREDUMP_BEGIN_STR "BEGIN#"
23#define COREDUMP_END_STR "END#"
24#define COREDUMP_ERROR_STR "ERROR CANNOT DUMP#"
25
26/*
27 * Need to prefix coredump strings to make it easier to parse
28 * as log module adds its own prefixes.
29 */
30#define COREDUMP_PREFIX_STR "#CD:"
31
42
43
74
132
144
145#ifdef CONFIG_DEBUG_COREDUMP
146
147#include <zephyr/toolchain.h>
148#include <zephyr/arch/cpu.h>
149#include <zephyr/sys/byteorder.h>
150
151#define COREDUMP_HDR_VER 2
152
153#define COREDUMP_ARCH_HDR_ID 'A'
154
155#define THREADS_META_HDR_ID 'T'
156#define THREADS_META_HDR_VER 1
157
158#define COREDUMP_MEM_HDR_ID 'M'
159#define COREDUMP_MEM_HDR_VER 1
160
161#define COREDUMP_CPU_SNAPSHOT_HDR_ID 'F'
162#define COREDUMP_CPU_SNAPSHOT_HDR_VER 1
163
164#define COREDUMP_CRC_HDR_ID 'C'
165#define COREDUMP_CRC_HDR_VER 1
166
167/*
168 * coredump_hdr_t.flag bits.
169 *
170 * COREDUMP_HDR_FLAG_CRC advertises that the dump is terminated by a CRC-32
171 * integrity trailer (CONFIG_DEBUG_COREDUMP_CRC). Host tooling uses it to tell a
172 * legacy (no-CRC) dump apart from a CRC dump that was truncated before its
173 * trailer, so the latter can be rejected instead of silently handed to GDB.
174 */
175#define COREDUMP_HDR_FLAG_CRC 0x01U
176
177/* Target code */
178enum coredump_tgt_code {
179 COREDUMP_TGT_UNKNOWN = 0,
180 COREDUMP_TGT_X86,
181 COREDUMP_TGT_X86_64,
182 COREDUMP_TGT_ARM_CORTEX_M,
183 COREDUMP_TGT_RISC_V,
184 COREDUMP_TGT_XTENSA,
185 COREDUMP_TGT_ARM64,
186};
187
188/* Coredump header */
189struct coredump_hdr_t {
190 /* 'Z', 'E' */
191 char id[2];
192
193 /* Header version */
194 uint16_t hdr_version;
195
196 /* Target code */
197 uint16_t tgt_code;
198
199 /* Pointer size in Log2 */
200 uint8_t ptr_size_bits;
201
202 uint8_t flag;
203
204 /* Coredump Reason given */
205 unsigned int reason;
206} __packed;
207
208/* Architecture-specific block header */
209struct coredump_arch_hdr_t {
210 /* COREDUMP_ARCH_HDR_ID */
211 char id;
212
213 /* Header version */
214 uint16_t hdr_version;
215
216 /* Number of bytes in this block (excluding header) */
217 uint16_t num_bytes;
218} __packed;
219
220/* Threads metadata header */
221struct coredump_threads_meta_hdr_t {
222 /* THREADS_META_HDR_ID */
223 char id;
224
225 /* Header version */
226 uint16_t hdr_version;
227
228 /* Number of bytes in this block (excluding header) */
229 uint16_t num_bytes;
230} __packed;
231
232/* Memory block header */
233struct coredump_mem_hdr_t {
234 /* COREDUMP_MEM_HDR_ID */
235 char id;
236
237 /* Header version */
238 uint16_t hdr_version;
239
240 /* Address of start of memory region */
241 uintptr_t start;
242
243 /* Address of end of memory region */
244 uintptr_t end;
245} __packed;
246
247/*
248 * Live per-CPU register snapshot header (CONFIG_DEBUG_COREDUMP_SMP_FREEZE_CPUS).
249 *
250 * One of these precedes each frozen CPU's register block (same arch-specific
251 * payload layout as the COREDUMP_ARCH_HDR_ID block for the panicking thread,
252 * e.g. arm64_arch_block on ARM64), letting tooling show a correct backtrace
253 * for a thread that was actively running on another CPU at panic time,
254 * instead of falling back to its stale last-swapped-out saved context.
255 */
256struct coredump_cpu_snapshot_hdr_t {
257 /* COREDUMP_CPU_SNAPSHOT_HDR_ID */
258 char id;
259
260 /* Header version */
261 uint16_t hdr_version;
262
263 /* Number of bytes in the arch-specific payload that follows */
264 uint16_t num_bytes;
265
266 /* CPU index (0..CONFIG_MP_MAX_NUM_CPUS-1) this snapshot came from */
267 uint32_t cpu_id;
268
269 /* The k_thread that was .current on that CPU when it was frozen */
270 uintptr_t thread_ptr;
271} __packed;
272
273/*
274 * Integrity trailer (CONFIG_DEBUG_COREDUMP_CRC).
275 *
276 * When enabled, this is emitted as the very last block of the coredump. It
277 * carries a CRC-32 (IEEE 802.3, as computed by crc32_ieee()) over every byte
278 * of the dump that precedes it - i.e. the main header and all arch / threads
279 * metadata / per-CPU snapshot / memory blocks, but not the trailer itself.
280 * Host tooling recomputes the CRC over the same range and compares, so a
281 * dump corrupted or truncated in transit (logging/UDP) or in storage (flash)
282 * can be detected before it is fed to GDB.
283 */
284struct coredump_crc_hdr_t {
285 /* COREDUMP_CRC_HDR_ID */
286 char id;
287
288 /* Header version */
289 uint16_t hdr_version;
290
291 /* CRC-32 (IEEE) of all preceding coredump bytes */
292 uint32_t crc;
293} __packed;
294
295typedef void (*coredump_backend_start_t)(void);
296typedef void (*coredump_backend_end_t)(void);
297typedef void (*coredump_backend_buffer_output_t)(uint8_t *buf, size_t buflen);
298typedef int (*coredump_backend_query_t)(enum coredump_query_id query_id,
299 void *arg);
300typedef int (*coredump_backend_cmd_t)(enum coredump_cmd_id cmd_id,
301 void *arg);
302
303struct coredump_backend_api {
304 /* Signal to backend of the start of coredump. */
305 coredump_backend_start_t start;
306
307 /* Signal to backend of the end of coredump. */
308 coredump_backend_end_t end;
309
310 /* Raw buffer output */
311 coredump_backend_buffer_output_t buffer_output;
312
313 /* Perform query on backend */
314 coredump_backend_query_t query;
315
316 /* Perform command on backend */
317 coredump_backend_cmd_t cmd;
318};
319
320void coredump(unsigned int reason, const struct arch_esf *esf,
321 struct k_thread *thread);
322void coredump_memory_dump(uintptr_t start_addr, uintptr_t end_addr);
323void coredump_buffer_output(uint8_t *buf, size_t buflen);
324
325int coredump_query(enum coredump_query_id query_id, void *arg);
326int coredump_cmd(enum coredump_cmd_id cmd_id, void *arg);
327
328#else
329
330static inline void coredump(unsigned int reason, const struct arch_esf *esf,
331 struct k_thread *thread)
332{
333 ARG_UNUSED(reason);
334 ARG_UNUSED(esf);
335 ARG_UNUSED(thread);
336}
337
338static inline void coredump_memory_dump(uintptr_t start_addr, uintptr_t end_addr)
339{
340 ARG_UNUSED(start_addr);
341 ARG_UNUSED(end_addr);
342}
343
344static inline void coredump_buffer_output(uint8_t *buf, size_t buflen)
345{
346 ARG_UNUSED(buf);
347 ARG_UNUSED(buflen);
348}
349
350static inline int coredump_query(enum coredump_query_id query_id, void *arg)
351{
352 ARG_UNUSED(query_id);
353 ARG_UNUSED(arg);
354 return -ENOTSUP;
355}
356
357static inline int coredump_cmd(enum coredump_cmd_id query_id, void *arg)
358{
359 ARG_UNUSED(query_id);
360 ARG_UNUSED(arg);
361 return -ENOTSUP;
362}
363
364#endif /* CONFIG_DEBUG_COREDUMP */
365
378
386
398
410
422
426
427#ifdef __cplusplus
428}
429#endif
430
431#endif /* ZEPHYR_INCLUDE_DEBUG_COREDUMP_H_ */
coredump_query_id
Query ID.
Definition coredump.h:45
coredump_cmd_id
Command ID.
Definition coredump.h:76
static int coredump_cmd(enum coredump_cmd_id query_id, void *arg)
Perform command on coredump subsystem.
Definition coredump.h:357
static int coredump_query(enum coredump_query_id query_id, void *arg)
Perform query on coredump subsystem.
Definition coredump.h:350
static void coredump_memory_dump(uintptr_t start_addr, uintptr_t end_addr)
Dump memory region.
Definition coredump.h:338
static void coredump(unsigned int reason, const struct arch_esf *esf, struct k_thread *thread)
Perform coredump.
Definition coredump.h:330
static void coredump_buffer_output(uint8_t *buf, size_t buflen)
Output the buffer via coredump.
Definition coredump.h:344
@ COREDUMP_QUERY_HAS_STORED_DUMP
Check if there is a stored coredump from backend.
Definition coredump.h:59
@ COREDUMP_QUERY_GET_STORED_DUMP_SIZE
Returns:
Definition coredump.h:67
@ COREDUMP_QUERY_MAX
Max value for query ID.
Definition coredump.h:72
@ COREDUMP_QUERY_GET_ERROR
Returns error code from backend.
Definition coredump.h:49
@ COREDUMP_CMD_COPY_STORED_DUMP
Copy the raw stored coredump.
Definition coredump.h:114
@ COREDUMP_CMD_MAX
Max value for command ID.
Definition coredump.h:130
@ COREDUMP_CMD_INVALIDATE_STORED_DUMP
Invalidate the stored coredump.
Definition coredump.h:125
@ COREDUMP_CMD_ERASE_STORED_DUMP
Erase the stored coredump.
Definition coredump.h:103
@ COREDUMP_CMD_VERIFY_STORED_DUMP
Verify that the stored coredump is valid.
Definition coredump.h:93
@ COREDUMP_CMD_CLEAR_ERROR
Clear error code from backend.
Definition coredump.h:82
static void cmd(uint32_t command)
Execute a display list command by co-processor engine.
Definition ft8xx_reference_api.h:153
#define ENOTSUP
Unsupported value.
Definition errno.h:115
__INTPTR_TYPE__ off_t
Definition types.h:36
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:105
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
Coredump copy command (COREDUMP_CMD_COPY_STORED_DUMP) argument definition.
Definition coredump.h:134
off_t offset
Copy offset.
Definition coredump.h:136
uint8_t * buffer
Copy destination buffer.
Definition coredump.h:139
size_t length
Copy length.
Definition coredump.h:142
Thread Structure.
Definition thread.h:258
Byte order helpers.
Macros to abstract toolchain specific capabilities.