text: /****************************************************************************
   * arch/avr/src/avr32/up_dumpstate.c
   *
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.  The
   * ASF licenses this file to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance with the
   * License.  You may obtain a copy of the License at
   *
   *   http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
   * License for the specific language governing permissions and limitations
   * under the License.
   *
   ****************************************************************************/
  
  /****************************************************************************
   * Included Files
   ****************************************************************************/
  
  #include <nuttx/config.h>
  
  #include <stdint.h>
  #include <stdlib.h>
  #include <assert.h>
  #include <debug.h>
  
  #include <nuttx/irq.h>
  #include <nuttx/arch.h>
  #include <nuttx/syslog/syslog.h>
  #include <arch/board/board.h>
  
  #include "up_arch.h"
  #include "sched/sched.h"
  #include "up_internal.h"
  
  #ifdef CONFIG_ARCH_STACKDUMP
  
  /****************************************************************************
   * Private Functions
   ****************************************************************************/
  
  /****************************************************************************
   * Name: avr_stackdump
   ****************************************************************************/
  
  static void avr_stackdump(uint32_t sp, uint32_t stack_top)
  {
    uint32_t stack;
  
    /* Flush any buffered SYSLOG data to avoid overwrite */
  
    syslog_flush();
  
    for (stack = sp & ~0x1f; stack < (stack_top & ~0x1f); stack += 32)
      {
        uint32_t *ptr = (uint32_t *)stack;
        _alert("%08x: %08x %08x %08x %08x %08x %08x %08x %08x\n",
               stack, ptr[0], ptr[1], ptr[2], ptr[3],
               ptr[4], ptr[5], ptr[6], ptr[7]);
      }
  }
  
  /****************************************************************************
   * Name: avr_registerdump
   ****************************************************************************/
  
  static inline void avr_registerdump(void)
  {
    /* Are user registers available from interrupt processing? */
  
    if (g_current_regs)
      {
        _alert("R%d: %08x %08x %08x %08x %08x %08x %08x %08x\n",
               0,
               g_current_regs[REG_R0], g_current_regs[REG_R1],
               g_current_regs[REG_R2], g_current_regs[REG_R3],
               g_current_regs[REG_R4], g_current_regs[REG_R5],
               g_current_regs[REG_R6], g_current_regs[REG_R7]);
  
        _alert("R%d: %08x %08x %08x %08x %08x %08x %08x %08x\n",
               8,
               g_current_regs[REG_R8],  g_current_regs[REG_R9],
               g_current_regs[REG_R10], g_current_regs[REG_R11],
               g_current_regs[REG_R12], g_current_regs[REG_R13],
               g_current_regs[REG_R14], g_current_regs[REG_R15]);
  
        _alert("SR: %08x\n", g_current_regs[REG_SR]);
      }
  }
  
  /****************************************************************************
   * Name: up_dumpstate
   ****************************************************************************/
  
  void up_dumpstate(void)
  {
    FAR struct tcb_s *rtcb = running_task();
    uint32_t sp = up_getsp();
    uint32_t ustackbase;
    uint32_t ustacksize;
  #if CONFIG_ARCH_INTERRUPTSTACK > 3
    uint32_t istackbase;
    uint32_t istacksize;
  #endif
  
    /* Dump the registers (if available) */
  
    avr_registerdump();
  
    /* Get the limits on the user stack memory */
  
    ustackbase = (uint32_t)rtcb->stack_base_ptr;
    ustacksize = (uint32_t)rtcb->adj_stack_size;
  
    /* Get the limits on the interrupt stack memory */
  
  #if CONFIG_ARCH_INTERRUPTSTACK > 3
    istackbase = (uint32_t)&g_intstackalloc;
    istacksize = (CONFIG_ARCH_INTERRUPTSTACK & ~3);
  
    /* Show interrupt stack info */
  
    _alert("sp:     %08x\n", sp);
    _alert("IRQ stack:\n");
    _alert("  base: %08x\n", istackbase);
    _alert("  size: %08x\n", istacksize);
  #ifdef CONFIG_STACK_COLORATION
    _alert("  used: %08x\n", up_check_intstack());
  #endif
  
    /* Does the current stack pointer lie within the interrupt
     * stack?
     */
  
    if (sp >= istackbase && sp < istackbase + istacksize)
      {
        /* Yes.. dump the interrupt stack */
  
        avr_stackdump(sp, istackbase + istacksize);
      }
    else if (g_current_regs)
      {
        _alert("ERROR: Stack pointer is not within the interrupt stack\n");
        avr_stackdump(istackbase, istackbase + istacksize);
      }
  
    /* Extract the user stack pointer if we are in an interrupt handler.
     * If we are not in an interrupt handler.  Then sp is the user stack
     * pointer (and the above range check should have failed).
     */
  
    if (g_current_regs)
      {
        sp = g_current_regs[REG_R13];
        _alert("sp:     %08x\n", sp);
      }
  
    _alert("User stack:\n");
    _alert("  base: %08x\n", ustackbase);
    _alert("  size: %08x\n", ustacksize);
  #ifdef CONFIG_STACK_COLORATION
    _alert("  used: %08x\n", up_check_tcbstack(rtcb));
  #endif
  #else
    _alert("sp:         %08x\n", sp);
    _alert("stack base: %08x\n", ustackbase);
    _alert("stack size: %08x\n", ustacksize);
  #ifdef CONFIG_STACK_COLORATION
    _alert("stack used: %08x\n", up_check_tcbstack(rtcb));
  #endif
  #endif
  
    /* Dump the user stack if the stack pointer lies within the allocated user
     * stack memory.
     */
  
    if (sp >= ustackbase && sp < ustackbase + ustacksize)
      {
        avr_stackdump(sp, ustackbase + ustacksize);
      }
    else
      {
        _alert("ERROR: Stack pointer is not within allocated stack\n");
        avr_stackdump(ustackbase, ustackbase + ustacksize);
      }
  }
  #endif
  