# Copyright (c) 2020 Intel Corporation.
# SPDX-License-Identifier: Apache-2.0

menuconfig DEBUG_COREDUMP
	bool "Core Dump"
	depends on ARCH_SUPPORTS_COREDUMP
	help
	  Enable core dump so it can be used for offline debugging.

if DEBUG_COREDUMP

choice DEBUG_COREDUMP_BACKEND
	prompt "Coredump backend"
	default DEBUG_COREDUMP_BACKEND_LOGGING

config DEBUG_COREDUMP_BACKEND_LOGGING
	bool "Use Logging subsystem for coredump"
	select LOG
	help
	  Core dump is done via logging subsystem.

config DEBUG_COREDUMP_BACKEND_FLASH_PARTITION
	bool "Use flash partition for coredump"
	depends on FLASH
	select FLASH_MAP
	select STREAM_FLASH
	help
	  Core dump is saved to a flash partition with DTS alias
	  "coredump-partition".

config DEBUG_COREDUMP_BACKEND_IN_MEMORY
	bool "Use memory to store the coredump"
	help
	  Core dump (or part of it) is saved in a dedicated area in
	  memory. Such memory is protected at boot time so it could be
	  read after a warm reboot. This is obviously NOT a valid solution
	  when a full hardware reboot cycle occurs (on/off etc..) where
	  the RAM will most likely loose its data.
	  See DEBUG_COREDUMP_BACKEND_IN_MEMORY_SIZE on how much space to
	  reserve.

config DEBUG_COREDUMP_BACKEND_INTEL_ADSP_MEM_WINDOW
	bool "Use memory window for coredump on Intel ADSP"
	depends on DT_HAS_INTEL_ADSP_MEM_WINDOW_ENABLED
	help
	  Core dump is done via memory window slot[1].
	  It is Intel ADSP memory region shared with xtensa DSP.
	  Window 2 slot [1] is reserved for debugging information.

config DEBUG_COREDUMP_BACKEND_OTHER
	bool "Backend subsystem for coredump defined out of tree"
	help
	  Core dump is done via custom mechanism defined out of tree

endchoice

choice DEBUG_COREDUMP_MEMORY_DUMP
	prompt "Memory dump"
	default DEBUG_COREDUMP_MEMORY_DUMP_LINKER_RAM

config DEBUG_COREDUMP_MEMORY_DUMP_MIN
	bool "Minimal"
	select THREAD_STACK_INFO
	help
	  Only dumps the bare minimum memory content.
	  For example, the thread struct and stack of
	  the exception thread will be dumped.

	  Don't use this unless you want absolutely
	  minimum core dump.

config DEBUG_COREDUMP_MEMORY_DUMP_THREADS
	bool "Threads"
	depends on !SMP
	depends on ARCH_SUPPORTS_COREDUMP_THREADS
	select THREAD_STACK_INFO
	select DEBUG_THREAD_INFO
	select DEBUG_COREDUMP_THREADS_METADATA
	help
	  Dumps the thread struct and stack of all
	  threads and all data required to debug threads.

config DEBUG_COREDUMP_MEMORY_DUMP_LINKER_RAM
	bool "RAM defined by linker section"
	help
	  Dumps the memory region between _image_ram_start[]
	  and _image_ram_end[]. This includes at least data,
	  noinit, and BSS sections.

	  This is the default.

endchoice

if DEBUG_COREDUMP_BACKEND_FLASH_PARTITION

config DEBUG_COREDUMP_FLASH_CHUNK_SIZE
	int "Chunk size for flash write operations"
	default 64
	help
	  Larger values can speed up writing due to fewer write operations
	  being performed in total, but consume more memory.


endif # DEBUG_COREDUMP_BACKEND_FLASH_PARTITION

config DEBUG_COREDUMP_SHELL
	bool "Coredump shell"
	depends on SHELL
	help
	  This shell provides access to coredump and its backends.

config DEBUG_COREDUMP_THREADS_METADATA
	bool "Threads metadata"
	depends on !SMP
	depends on ARCH_SUPPORTS_COREDUMP_THREADS
	select DEBUG_THREAD_INFO
	help
	  Core dump will contain the threads metadata section containing
	  any necessary data to enable debugging threads

config DEBUG_COREDUMP_DUMP_THREAD_PRIV_STACK
	bool "Dump privilege stack of user threads"
	default y
	depends on ARCH_SUPPORTS_COREDUMP_PRIV_STACKS
	depends on USERSPACE
	help
	  Dump the privilege stack of user threads.

	  Say n to conserve space on coredump backend or if you will never
	  need to look into the privilege stacks.

config DEBUG_COREDUMP_BACKEND_IN_MEMORY_SIZE
	int "In-memory coredump size"
	default 128
	depends on DEBUG_COREDUMP_BACKEND_IN_MEMORY
	help
	  Sets the dedicated memory area where a coredump can be stored
	  and accessed after a warm reboot. It has to be able to hold at
	  least the info dumped by arch_coredump_info_dump().
	  Extra memory region will silently be ignored if there isn't
	  enough space.

module = DEBUG_COREDUMP
module-str = coredump
source "subsys/logging/Kconfig.template.log_config"

config DEBUG_COREDUMP_THREAD_STACK_TOP
	bool "Dump top of stack only"
	default y if DEBUG_COREDUMP_MEMORY_DUMP_MIN
	depends on DEBUG_COREDUMP_MEMORY_DUMP_MIN || \
		   DEBUG_COREDUMP_MEMORY_DUMP_THREADS
	depends on ARCH_SUPPORTS_COREDUMP_STACK_PTR
	help
	  Dump only the top part of each thread's stack instead of the entire
	  stack region. The top of the stack is defined as the area starting
	  from the stack pointer and extending up to the smaller of the stack
	  end or the limit configured by either
	  DEBUG_COREDUMP_THREAD_STACK_TOP_LIMIT_FOR_CURRENT (for the current
	  thread) or DEBUG_COREDUMP_THREAD_STACK_TOP_LIMIT (for remaining ones).

if DEBUG_COREDUMP_THREAD_STACK_TOP

config DEBUG_COREDUMP_THREAD_STACK_TOP_LIMIT_FOR_CURRENT
	int "Stack top size limit for current thread"
	default DEBUG_COREDUMP_THREAD_STACK_TOP_LIMIT
	help
	  The maximum number of stack bytes to be dumped for the thread running
	  when the exception occurred.
	  A negative value indicates that there is no limit, meaning that the
	  stack is dumped till the end of its region.

config DEBUG_COREDUMP_THREAD_STACK_TOP_LIMIT
	int "Stack top size limit for non-current threads"
	default -1
	help
	  The maximum number of stack bytes to be dumped for threads that were
	  not running when the exception occurred.
	  A negative value indicates that there is no limit, meaning that the
	  stack is dumped till the end of its region.

endif # DEBUG_COREDUMP_THREAD_STACK_TOP

endif # DEBUG_COREDUMP
