/*
 * gdb helper commands and functions for Linux kernel debugging
 *
 *  Kernel constants derived from include files.
 *
 * Copyright (c) 2016 Linaro Ltd
 *
 * Authors:
 *  Kieran Bingham <kieran.bingham@linaro.org>
 *
 * This work is licensed under the terms of the GNU GPL version 2.
 *
 */

#include <linux/clk-provider.h>
#include <linux/fs.h>
#include <linux/hrtimer.h>
#include <linux/irq.h>
#include <linux/mount.h>
#include <linux/of_fdt.h>
#include <linux/page_ext.h>
#include <linux/radix-tree.h>
#include <linux/slab.h>
#include <linux/threads.h>
#include <linux/vmalloc.h>

/* We need to stringify expanded macros so that they can be parsed */

#define STRING(x) #x
#define XSTRING(x) STRING(x)

#define LX_VALUE(x) LX_##x = x
#define LX_GDBPARSED(x) LX_##x = gdb.parse_and_eval(XSTRING(x))

/*
 * IS_ENABLED generates (a || b) which is not compatible with python
 * We can only switch on configuration items we know are available
 * Therefore - IS_BUILTIN() is more appropriate
 */
#define LX_CONFIG(x) LX_##x = IS_BUILTIN(x)

/* The build system will take care of deleting everything above this marker */
<!-- end-c-headers -->

import gdb

LX_CONFIG(CONFIG_DEBUG_INFO_REDUCED)

/* linux/clk-provider.h */
if IS_BUILTIN(CONFIG_COMMON_CLK):
    LX_GDBPARSED(CLK_GET_RATE_NOCACHE)

/* linux/fs.h */
LX_GDBPARSED(SB_RDONLY)
LX_GDBPARSED(SB_SYNCHRONOUS)
LX_GDBPARSED(SB_MANDLOCK)
LX_GDBPARSED(SB_DIRSYNC)
LX_GDBPARSED(SB_NOATIME)
LX_GDBPARSED(SB_NODIRATIME)

/* linux/htimer.h */
LX_GDBPARSED(hrtimer_resolution)

/* linux/irq.h */
LX_GDBPARSED(IRQD_LEVEL)
LX_GDBPARSED(IRQ_HIDDEN)

/* linux/module.h */
LX_GDBPARSED(MOD_TEXT)
LX_GDBPARSED(MOD_DATA)
LX_GDBPARSED(MOD_RODATA)
LX_GDBPARSED(MOD_RO_AFTER_INIT)

/* linux/mount.h */
LX_VALUE(MNT_NOSUID)
LX_VALUE(MNT_NODEV)
LX_VALUE(MNT_NOEXEC)
LX_VALUE(MNT_NOATIME)
LX_VALUE(MNT_NODIRATIME)
LX_VALUE(MNT_RELATIME)

/* linux/threads.h */
LX_VALUE(NR_CPUS)

/* linux/of_fdt.h> */
LX_VALUE(OF_DT_HEADER)

/* linux/radix-tree.h */
LX_GDBPARSED(RADIX_TREE_ENTRY_MASK)
LX_GDBPARSED(RADIX_TREE_INTERNAL_NODE)
LX_GDBPARSED(RADIX_TREE_MAP_SIZE)
LX_GDBPARSED(RADIX_TREE_MAP_SHIFT)
LX_GDBPARSED(RADIX_TREE_MAP_MASK)

/* linux/vmalloc.h */
LX_VALUE(VM_IOREMAP)
LX_VALUE(VM_ALLOC)
LX_VALUE(VM_MAP)
LX_VALUE(VM_USERMAP)
LX_VALUE(VM_DMA_COHERENT)

/* linux/page_ext.h */
if IS_BUILTIN(CONFIG_PAGE_OWNER):
    LX_GDBPARSED(PAGE_EXT_OWNER)
    LX_GDBPARSED(PAGE_EXT_OWNER_ALLOCATED)

/* linux/slab.h */
LX_GDBPARSED(SLAB_RED_ZONE)
LX_GDBPARSED(SLAB_POISON)
LX_GDBPARSED(SLAB_KMALLOC)
LX_GDBPARSED(SLAB_HWCACHE_ALIGN)
LX_GDBPARSED(SLAB_CACHE_DMA)
LX_GDBPARSED(SLAB_CACHE_DMA32)
LX_GDBPARSED(SLAB_STORE_USER)
LX_GDBPARSED(SLAB_PANIC)

/* Kernel Configs */
LX_CONFIG(CONFIG_GENERIC_CLOCKEVENTS)
LX_CONFIG(CONFIG_GENERIC_CLOCKEVENTS_BROADCAST)
LX_CONFIG(CONFIG_HIGH_RES_TIMERS)
LX_CONFIG(CONFIG_NR_CPUS)
LX_CONFIG(CONFIG_OF)
LX_CONFIG(CONFIG_TICK_ONESHOT)
LX_CONFIG(CONFIG_GENERIC_IRQ_SHOW_LEVEL)
LX_CONFIG(CONFIG_X86_LOCAL_APIC)
LX_CONFIG(CONFIG_SMP)
LX_CONFIG(CONFIG_X86_THERMAL_VECTOR)
LX_CONFIG(CONFIG_X86_MCE_THRESHOLD)
LX_CONFIG(CONFIG_X86_MCE_AMD)
LX_CONFIG(CONFIG_X86_MCE)
LX_CONFIG(CONFIG_X86_IO_APIC)
LX_CONFIG(CONFIG_HAVE_KVM)
LX_CONFIG(CONFIG_NUMA)
LX_CONFIG(CONFIG_ARM64)
LX_CONFIG(CONFIG_ARM64_4K_PAGES)
LX_CONFIG(CONFIG_ARM64_16K_PAGES)
LX_CONFIG(CONFIG_ARM64_64K_PAGES)
if IS_BUILTIN(CONFIG_ARM64):
    LX_VALUE(CONFIG_ARM64_PA_BITS)
    LX_VALUE(CONFIG_ARM64_VA_BITS)
    LX_VALUE(CONFIG_ARM64_PAGE_SHIFT)
    LX_VALUE(CONFIG_ARCH_FORCE_MAX_ORDER)
LX_CONFIG(CONFIG_SPARSEMEM)
LX_CONFIG(CONFIG_SPARSEMEM_EXTREME)
LX_CONFIG(CONFIG_SPARSEMEM_VMEMMAP)
LX_CONFIG(CONFIG_KASAN)
LX_CONFIG(CONFIG_KASAN_GENERIC)
LX_CONFIG(CONFIG_KASAN_SW_TAGS)
LX_CONFIG(CONFIG_KASAN_HW_TAGS)
if IS_BUILTIN(CONFIG_KASAN_GENERIC) or IS_BUILTIN(CONFIG_KASAN_SW_TAGS):
    LX_VALUE(CONFIG_KASAN_SHADOW_OFFSET)
LX_CONFIG(CONFIG_VMAP_STACK)
if IS_BUILTIN(CONFIG_NUMA):
    LX_VALUE(CONFIG_NODES_SHIFT)
LX_CONFIG(CONFIG_DEBUG_VIRTUAL)
LX_CONFIG(CONFIG_STACKDEPOT)
LX_CONFIG(CONFIG_PAGE_OWNER)
LX_CONFIG(CONFIG_SLUB_DEBUG)
LX_CONFIG(CONFIG_SLAB_FREELIST_HARDENED)