targets/plc_debug.c
changeset 1432 8872223a675b
parent 1403 dd6d40094782
child 1457 ff7cfce737ca
equal deleted inserted replaced
1431:df59be5afb08 1432:8872223a675b
    37 /***
    37 /***
    38  * Declare global variables from resources and conf 
    38  * Declare global variables from resources and conf 
    39  **/
    39  **/
    40 %(extern_variables_declarations)s
    40 %(extern_variables_declarations)s
    41 
    41 
    42 typedef void(*__for_each_variable_do_fp)(void*, __IEC_types_enum);
    42 typedef const struct {
       
    43     void *ptr;
       
    44     __IEC_types_enum type;
       
    45 } dbgvardsc_t;
       
    46 
       
    47 static dbgvardsc_t dbgvardsc[] = {
       
    48 %(variable_decl_array)s
       
    49 };
       
    50 
       
    51 typedef void(*__for_each_variable_do_fp)(dbgvardsc_t*);
    43 void __for_each_variable_do(__for_each_variable_do_fp fp)
    52 void __for_each_variable_do(__for_each_variable_do_fp fp)
    44 {
    53 {
    45 %(for_each_variable_do_code)s
    54     int i;
    46 }
    55     for(i = 0; i < sizeof(dbgvardsc)/sizeof(dbgvardsc_t); i++){
    47 
    56         dbgvardsc_t *dsc = &dbgvardsc[i];
    48 __IEC_types_enum __find_variable(unsigned int varindex, void ** varp)
    57         if(dsc->type != UNKNOWN_ENUM) 
    49 {
    58             (*fp)(dsc);
    50     switch(varindex){
       
    51 %(find_variable_case_code)s
       
    52      default:
       
    53       *varp = NULL;
       
    54       return UNKNOWN_ENUM;
       
    55     }
    59     }
    56 }
    60 }
    57 
    61 
    58 #define __Unpack_case_t(TYPENAME) \
    62 #define __Unpack_case_t(TYPENAME) \
    59         case TYPENAME##_ENUM :\
    63         case TYPENAME##_ENUM :\
    68             *flags |= ((__IEC_##TYPENAME##_p *)varp)->flags;\
    72             *flags |= ((__IEC_##TYPENAME##_p *)varp)->flags;\
    69             *real_value_p = ((__IEC_##TYPENAME##_p *)varp)->value;\
    73             *real_value_p = ((__IEC_##TYPENAME##_p *)varp)->value;\
    70             forced_value_p = &((__IEC_##TYPENAME##_p *)varp)->fvalue;\
    74             forced_value_p = &((__IEC_##TYPENAME##_p *)varp)->fvalue;\
    71             break;
    75             break;
    72 
    76 
    73 void* UnpackVar(void* varp, __IEC_types_enum vartype, void **real_value_p, char *flags)
    77 void* UnpackVar(dbgvardsc_t *dsc, void **real_value_p, char *flags)
    74 {
    78 {
       
    79     void *varp = dsc->ptr;
    75     void *forced_value_p = NULL;
    80     void *forced_value_p = NULL;
    76     *flags = 0;
    81     *flags = 0;
    77     /* find data to copy*/
    82     /* find data to copy*/
    78     switch(vartype){
    83     switch(dsc->type){
    79         __ANY(__Unpack_case_t)
    84         __ANY(__Unpack_case_t)
    80         __ANY(__Unpack_case_p)
    85         __ANY(__Unpack_case_p)
    81     default:
    86     default:
    82         break;
    87         break;
    83     }
    88     }
    86     return *real_value_p;
    91     return *real_value_p;
    87 }
    92 }
    88 
    93 
    89 void Remind(unsigned int offset, unsigned int count, void * p);
    94 void Remind(unsigned int offset, unsigned int count, void * p);
    90 
    95 
    91 void RemindIterator(void* varp, __IEC_types_enum vartype)
    96 void RemindIterator(dbgvardsc_t *dsc)
    92 {
    97 {
    93     void *real_value_p = NULL;
    98     void *real_value_p = NULL;
    94     char flags = 0;
    99     char flags = 0;
    95     UnpackVar(varp, vartype, &real_value_p, &flags);
   100     UnpackVar(dsc, &real_value_p, &flags);
    96 
   101 
    97     if(flags & __IEC_RETAIN_FLAG){
   102     if(flags & __IEC_RETAIN_FLAG){
    98         USINT size = __get_type_enum_size(vartype);
   103         USINT size = __get_type_enum_size(dsc->type);
    99         /* compute next cursor positon*/
   104         /* compute next cursor positon*/
   100         unsigned int next_retain_offset = retain_offset + size;
   105         unsigned int next_retain_offset = retain_offset + size;
   101         /* if buffer not full */
   106         /* if buffer not full */
   102         Remind(retain_offset, size, real_value_p);
   107         Remind(retain_offset, size, real_value_p);
   103         /* increment cursor according size*/
   108         /* increment cursor according size*/
   134 }
   139 }
   135 
   140 
   136 
   141 
   137 void Retain(unsigned int offset, unsigned int count, void * p);
   142 void Retain(unsigned int offset, unsigned int count, void * p);
   138 
   143 
   139 inline void BufferIterator(void* varp, __IEC_types_enum vartype, int do_debug)
   144 inline void BufferIterator(dbgvardsc_t *dsc, int do_debug)
   140 {
   145 {
   141     void *real_value_p = NULL;
   146     void *real_value_p = NULL;
   142     void *visible_value_p = NULL;
   147     void *visible_value_p = NULL;
   143     char flags = 0;
   148     char flags = 0;
   144 
   149 
   145     visible_value_p = UnpackVar(varp, vartype, &real_value_p, &flags);
   150     visible_value_p = UnpackVar(dsc, &real_value_p, &flags);
   146 
   151 
   147     if(flags & ( __IEC_DEBUG_FLAG | __IEC_RETAIN_FLAG)){
   152     if(flags & ( __IEC_DEBUG_FLAG | __IEC_RETAIN_FLAG)){
   148         USINT size = __get_type_enum_size(vartype);
   153         USINT size = __get_type_enum_size(dsc->type);
   149         if(flags & __IEC_DEBUG_FLAG){
   154         if(flags & __IEC_DEBUG_FLAG){
   150             /* copy visible variable to buffer */;
   155             /* copy visible variable to buffer */;
   151             if(do_debug){
   156             if(do_debug){
   152                 /* compute next cursor positon.
   157                 /* compute next cursor positon.
   153                    No need to check overflow, as BUFFER_SIZE
   158                    No need to check overflow, as BUFFER_SIZE
   154                    is computed large enough */
   159                    is computed large enough */
   155                 if(vartype == STRING_ENUM){
   160                 if(dsc->type == STRING_ENUM){
   156                     /* optimization for strings */
   161                     /* optimization for strings */
   157                     size = ((STRING*)visible_value_p)->len + 1;
   162                     size = ((STRING*)visible_value_p)->len + 1;
   158                 }
   163                 }
   159                 char* next_cursor = buffer_cursor + size;
   164                 char* next_cursor = buffer_cursor + size;
   160                 /* copy data to the buffer */
   165                 /* copy data to the buffer */
   176             retain_offset = next_retain_offset;
   181             retain_offset = next_retain_offset;
   177         }
   182         }
   178     }
   183     }
   179 }
   184 }
   180 
   185 
   181 void DebugIterator(void* varp, __IEC_types_enum vartype){
   186 void DebugIterator(dbgvardsc_t *dsc){
   182     BufferIterator(varp, vartype, 1);
   187     BufferIterator(dsc, 1);
   183 }
   188 }
   184 
   189 
   185 void RetainIterator(void* varp, __IEC_types_enum vartype){
   190 void RetainIterator(dbgvardsc_t *dsc){
   186     BufferIterator(varp, vartype, 0);
   191     BufferIterator(dsc, 0);
   187 }
   192 }
   188 
   193 
   189 extern void PLC_GetTime(IEC_TIME*);
   194 extern void PLC_GetTime(IEC_TIME*);
   190 extern int TryEnterDebugSection(void);
   195 extern int TryEnterDebugSection(void);
   191 extern long AtomicCompareExchange(long*, long, long);
   196 extern long AtomicCompareExchange(long*, long, long);
   249              *(((__IEC_##TYPENAME##_p *)varp)->value) = *((TYPENAME *)force);\
   254              *(((__IEC_##TYPENAME##_p *)varp)->value) = *((TYPENAME *)force);\
   250             }\
   255             }\
   251             break;
   256             break;
   252 void RegisterDebugVariable(int idx, void* force)
   257 void RegisterDebugVariable(int idx, void* force)
   253 {
   258 {
   254     void *varp = NULL;
   259     if(idx  < sizeof(dbgvardsc)/sizeof(dbgvardsc_t)){
   255     unsigned char flags = force ? __IEC_DEBUG_FLAG | __IEC_FORCE_FLAG : __IEC_DEBUG_FLAG;
   260         unsigned char flags = force ?
   256     switch(__find_variable(idx, &varp)){
   261             __IEC_DEBUG_FLAG | __IEC_FORCE_FLAG :
   257         __ANY(__RegisterDebugVariable_case_t)
   262             __IEC_DEBUG_FLAG;
   258         __ANY(__RegisterDebugVariable_case_p)
   263         dbgvardsc_t *dsc = &dbgvardsc[idx];
   259     default:
   264         void *varp = dsc->ptr;
   260         break;
   265         switch(dsc->type){
       
   266             __ANY(__RegisterDebugVariable_case_t)
       
   267             __ANY(__RegisterDebugVariable_case_p)
       
   268         default:
       
   269             break;
       
   270         }
   261     }
   271     }
   262 }
   272 }
   263 
   273 
   264 #define __ResetDebugVariablesIterator_case_t(TYPENAME) \
   274 #define __ResetDebugVariablesIterator_case_t(TYPENAME) \
   265         case TYPENAME##_ENUM :\
   275         case TYPENAME##_ENUM :\
   270         case TYPENAME##_P_ENUM :\
   280         case TYPENAME##_P_ENUM :\
   271         case TYPENAME##_O_ENUM :\
   281         case TYPENAME##_O_ENUM :\
   272             ((__IEC_##TYPENAME##_p *)varp)->flags &= ~(__IEC_DEBUG_FLAG|__IEC_FORCE_FLAG);\
   282             ((__IEC_##TYPENAME##_p *)varp)->flags &= ~(__IEC_DEBUG_FLAG|__IEC_FORCE_FLAG);\
   273             break;
   283             break;
   274 
   284 
   275 void ResetDebugVariablesIterator(void* varp, __IEC_types_enum vartype)
   285 void ResetDebugVariablesIterator(dbgvardsc_t *dsc)
   276 {
   286 {
   277     /* force debug flag to 0*/
   287     /* force debug flag to 0*/
   278     switch(vartype){
   288     void *varp = dsc->ptr;
       
   289     switch(dsc->type){
   279         __ANY(__ResetDebugVariablesIterator_case_t)
   290         __ANY(__ResetDebugVariablesIterator_case_t)
   280         __ANY(__ResetDebugVariablesIterator_case_p)
   291         __ANY(__ResetDebugVariablesIterator_case_p)
   281     default:
   292     default:
   282         break;
   293         break;
   283     }
   294     }