Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:14

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup libmisc_stackchk
0007  *
0008  * @brief This header file provides the Stack Checker API.
0009  */
0010 
0011 /*
0012  * COPYRIGHT (c) 1989-2024 On-Line Applications Research Corporation (OAR).
0013  * COPYRIGHT (c) 2024 Mohamed Hassan <muhammad.hamdy.hassan@gmail.com>
0014  *
0015  * Redistribution and use in source and binary forms, with or without
0016  * modification, are permitted provided that the following conditions
0017  * are met:
0018  * 1. Redistributions of source code must retain the above copyright
0019  *    notice, this list of conditions and the following disclaimer.
0020  * 2. Redistributions in binary form must reproduce the above copyright
0021  *    notice, this list of conditions and the following disclaimer in the
0022  *    documentation and/or other materials provided with the distribution.
0023  *
0024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0027  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0028  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0029  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0030  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0031  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0032  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0033  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0034  * POSSIBILITY OF SUCH DAMAGE.
0035  */
0036 
0037 #ifndef _RTEMS_STACKCHK_H
0038 #define _RTEMS_STACKCHK_H
0039 
0040 #include <rtems.h>
0041 #include <rtems/print.h>
0042 
0043 /**
0044  *  @defgroup libmisc_stackchk Stack Checker Mechanism
0045  *
0046  *  @ingroup RTEMSAPIClassic
0047  */
0048 /**@{*/
0049 #ifdef __cplusplus
0050 extern "C" {
0051 #endif
0052 
0053 /**
0054  * @brief Checks if current task is blown its stack.
0055  *
0056  * This method is used to determine if the current stack pointer
0057  * of the currently executing task is within bounds.
0058  *
0059  * @retval This method returns true if the currently executing task
0060  *         has blown its stack.
0061  */
0062 bool rtems_stack_checker_is_blown( void );
0063 
0064 /**
0065  * @brief Print the stack usage report using printk.
0066  *
0067  * This method prints a stack usage report for the curently executing
0068  * task.
0069  *
0070  * @note It uses printk to print the report.
0071  */
0072 void rtems_stack_checker_report_usage( void );
0073 
0074 /**
0075  * @brief Print the stack usage report using caller's routine.
0076  *
0077  * This method prints a stack usage report for the curently executing
0078  * task.
0079  *
0080  * @param[in] context is the context to pass to the print handler
0081  * 
0082  * @param[in] print is the print handler
0083  *
0084  * @note It uses the caller's routine to print the report.
0085  */
0086 void rtems_stack_checker_report_usage_with_plugin(
0087   const rtems_printer *printer
0088 );
0089 
0090 /**
0091  * @brief Stack information provided by the stack checker.
0092  */
0093 typedef struct {
0094   /**
0095    * This member contains the object identifier associated with the
0096    * object using the stack.
0097    * 
0098    * For interrupt stacks, the object identifier is the processor index.
0099    */
0100   rtems_id id;
0101 
0102   /**
0103    * This member provides the object name associated with the
0104    * object using the stack.
0105    * 
0106    * For interrupt stacks, the object name is "Interrupt Stack".
0107    */
0108   const char *name;
0109 
0110   /**
0111    * This member provides the begin address of the stack area.
0112    */
0113   const void *begin;
0114 
0115   /**
0116    *  This member contains the size in bytes of the stack area.
0117    */
0118   uintptr_t size;
0119 
0120   /**
0121    * This member provides the current stack pointer of the stack.
0122    * 
0123    * If the current stack pointer is not available, then the value is 
0124    * set to NULL.
0125    */
0126   const void *current;
0127 
0128   /**
0129    * This member contains the size in bytes of the used stack area.
0130    *
0131    * If the stack checker is not initialized, then the value is set to
0132    * UINTPTR_MAX.
0133    */
0134   uintptr_t used;
0135 } rtems_stack_checker_info;
0136 
0137 /**
0138  * @brief Visitor routines invoked by rtems_stack_checker_iterate() shall 
0139  *        have this type.
0140  *
0141  * @param[in] info is the stack information.
0142  *
0143  * @param[in] arg is the argument passed to rtems_stack_checker_iterate().
0144  */
0145 typedef void ( *rtems_stack_checker_visitor )(
0146   const rtems_stack_checker_info *info,
0147   void                           *arg
0148 );
0149 
0150 /**
0151  * @brief Iterates over all stacks used by the system and invokes the visitor
0152  *        routine for each stack.
0153  *
0154  * This method prints a stack usage report for the curently executing
0155  * task.
0156  *
0157  * @param[in] visitor is the visitor routine invoked for each stack.
0158  *
0159  * @param[in] arg is the argument passed to each visitor routine invocation 
0160  *                during the iteration.
0161  */
0162 void rtems_stack_checker_iterate( rtems_stack_checker_visitor visit, void *arg );
0163 
0164 /*************************************************************
0165  *************************************************************
0166  **  Prototyped only so the user extension can be installed **
0167  *************************************************************
0168  *************************************************************/
0169 
0170 /**
0171  * @brief Stack Checker Task Create Extension
0172  *
0173  * This method is the task create extension for the stack checker.
0174  *
0175  * @param[in] running points to the currently executing task
0176  * 
0177  * @param[in] the_thread points to the newly created task
0178  *
0179  * @note If this this the first task created, the stack checker
0180  *       will automatically intialize itself.
0181  */
0182 bool rtems_stack_checker_create_extension(
0183   rtems_tcb *running,
0184   rtems_tcb *the_thread
0185 );
0186 
0187 void rtems_stack_checker_begin_extension( rtems_tcb *executing );
0188 
0189 /**
0190  * @brief Stack Checker Task Context Switch Extension
0191  *
0192  * This method is the task context switch extension for the stack checker.
0193  *
0194  * @param[in] running points to the currently executing task which
0195  *                    is being context switched out
0196  * 
0197  * @param[in] heir points to the heir task which we are switching to
0198  *
0199  * @note This is called from the internal method _Thread_Dispatch.
0200  */
0201 void rtems_stack_checker_switch_extension(
0202   rtems_tcb *running,
0203   rtems_tcb *heir
0204 );
0205 
0206 /**
0207  * @brief A Quiet Version of Stack Checker Reporter.
0208  * 
0209  * @param[in] running running points to the currently executing thread which
0210  *                    is being context switched out.
0211  * 
0212  * @param[in] pattern_ok bool variable to check if the pattern is
0213  *                       still valid or not
0214  */
0215  
0216 void rtems_stack_checker_reporter_quiet(
0217   const rtems_tcb *running,
0218   bool pattern_ok
0219 );
0220 
0221 /**
0222  * @brief The Default Function to Report a Blown Stack.
0223  * 
0224  * @param[in] running running points to the currently executing thread which
0225  *                    is being context switched out.
0226  * 
0227  * @param[in] pattern_ok bool variable to check if the pattern is
0228  *                       still valid or not
0229  */
0230 void rtems_stack_checker_reporter_print_details(
0231   const rtems_tcb *running,
0232   bool pattern_ok
0233 );
0234 
0235 /**
0236  *  @brief Stack Checker Extension Set Definition
0237  *
0238  *  This macro defines the user extension handler set for the stack
0239  *  checker. This macro is normally only used by confdefs.h.
0240  */
0241 #define RTEMS_STACK_CHECKER_EXTENSION \
0242 { \
0243   rtems_stack_checker_create_extension,        /* rtems_task_create  */ \
0244   0,                                           /* rtems_task_start   */ \
0245   0,                                           /* rtems_task_restart */ \
0246   0,                                           /* rtems_task_delete  */ \
0247   rtems_stack_checker_switch_extension,        /* task_switch  */ \
0248   rtems_stack_checker_begin_extension,         /* task_begin   */ \
0249   0,                                           /* task_exitted */ \
0250   0,                                           /* fatal        */ \
0251   0                                            /* terminate    */ \
0252 }
0253 
0254 /**
0255  * @brief The Stack Checker Reporter Initialization Handler.
0256  * 
0257  * @param[in] running running points to the currently executing thread which
0258  *                    is being context switched out.
0259  * 
0260  * @param[in] pattern_ok bool variable to check if the pattern is
0261  *                       still valid or not.
0262  */
0263 typedef void (*Stack_checker_Reporter_handler)(
0264   const rtems_tcb *running,
0265   bool pattern_ok
0266 );
0267 
0268 /**
0269  * @brief The Stack Checker Reporter Initialization Handler.
0270  * 
0271  * Application provided via <rtems/confdefs.h>
0272  */
0273 extern const Stack_checker_Reporter_handler Stack_checker_Reporter;
0274 
0275 #ifdef __cplusplus
0276 }
0277 #endif
0278 /**@}*/
0279 #endif
0280 /* end of include file */