targets/plc_debug.c
author Laurent Bessard
Thu, 21 Feb 2013 20:08:06 +0100
changeset 937 bd6fdbb61784
parent 923 6ef6e0b3a908
child 944 52a17be9c4d1
permissions -rw-r--r--
Added support for changing graph size using handles like sash window
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
     1
/*
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
     2
 * DEBUGGER code
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
     3
 * 
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
     4
 * On "publish", when buffer is free, debugger stores arbitrary variables 
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
     5
 * content into, and mark this buffer as filled
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
     6
 * 
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
     7
 * 
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
     8
 * Buffer content is read asynchronously, (from non real time part), 
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
     9
 * and then buffer marked free again.
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    10
 *  
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    11
 * 
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    12
 * */
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    13
#include "iec_types_all.h"
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    14
#include "POUS.h"
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    15
/*for memcpy*/
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    16
#include <string.h>
335
c5f3f71e7260 fixed bug : buffer overflow when debugging PLC with python blocks
greg
parents: 280
diff changeset
    17
#include <stdio.h>
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    18
335
c5f3f71e7260 fixed bug : buffer overflow when debugging PLC with python blocks
greg
parents: 280
diff changeset
    19
#define BUFFER_SIZE %(buffer_size)d
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    20
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    21
/* Atomically accessed variable for buffer state */
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    22
#define BUFFER_FREE 0
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    23
#define BUFFER_BUSY 1
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    24
static long buffer_state = BUFFER_FREE;
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    25
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    26
/* The buffer itself */
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    27
char debug_buffer[BUFFER_SIZE];
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    28
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    29
/* Buffer's cursor*/
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    30
static char* buffer_cursor = debug_buffer;
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
    31
static unsigned int retain_offset = 0;
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    32
/***
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    33
 * Declare programs 
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    34
 **/
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    35
%(programs_declarations)s
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    36
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    37
/***
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    38
 * Declare global variables from resources and conf 
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    39
 **/
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    40
%(extern_variables_declarations)s
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    41
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
    42
typedef void(*__for_each_variable_do_fp)(void*, __IEC_types_enum);
452
2d0718a05cc7 Reflect changes in iec type definitions in matiec/lib
edouard
parents: 450
diff changeset
    43
void __for_each_variable_do(__for_each_variable_do_fp fp)
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
    44
{
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
    45
%(for_each_variable_do_code)s
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
    46
}
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    47
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
    48
__IEC_types_enum __find_variable(unsigned int varindex, void ** varp)
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
    49
{
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
    50
    switch(varindex){
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
    51
%(find_variable_case_code)s
452
2d0718a05cc7 Reflect changes in iec type definitions in matiec/lib
edouard
parents: 450
diff changeset
    52
     default:
2d0718a05cc7 Reflect changes in iec type definitions in matiec/lib
edouard
parents: 450
diff changeset
    53
      *varp = NULL;
2d0718a05cc7 Reflect changes in iec type definitions in matiec/lib
edouard
parents: 450
diff changeset
    54
      return UNKNOWN_ENUM;
2d0718a05cc7 Reflect changes in iec type definitions in matiec/lib
edouard
parents: 450
diff changeset
    55
    }
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
    56
}
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    57
605
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
    58
#define __Unpack_case_t(TYPENAME) \
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
    59
        case TYPENAME##_ENUM :\
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
    60
            *flags = ((__IEC_##TYPENAME##_t *)varp)->flags;\
605
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
    61
            forced_value_p = *real_value_p = &((__IEC_##TYPENAME##_t *)varp)->value;\
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
    62
            break;
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
    63
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
    64
#define __Unpack_case_p(TYPENAME)\
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
    65
        case TYPENAME##_O_ENUM :\
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
    66
            *flags = __IEC_OUTPUT_FLAG;\
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
    67
        case TYPENAME##_P_ENUM :\
605
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
    68
            *flags |= ((__IEC_##TYPENAME##_p *)varp)->flags;\
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
    69
            *real_value_p = ((__IEC_##TYPENAME##_p *)varp)->value;\
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
    70
            forced_value_p = &((__IEC_##TYPENAME##_p *)varp)->fvalue;\
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
    71
            break;
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
    72
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
    73
void* UnpackVar(void* varp, __IEC_types_enum vartype, void **real_value_p, char *flags)
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
    74
{
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
    75
    void *forced_value_p = NULL;
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
    76
    *flags = 0;
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
    77
    /* find data to copy*/
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
    78
    switch(vartype){
611
b665a9001451 Fix debugger bug after merging matiec repositories
laurent
parents: 605
diff changeset
    79
        __ANY(__Unpack_case_t)
b665a9001451 Fix debugger bug after merging matiec repositories
laurent
parents: 605
diff changeset
    80
        __ANY(__Unpack_case_p)
452
2d0718a05cc7 Reflect changes in iec type definitions in matiec/lib
edouard
parents: 450
diff changeset
    81
    default:
2d0718a05cc7 Reflect changes in iec type definitions in matiec/lib
edouard
parents: 450
diff changeset
    82
        break;
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
    83
    }
605
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
    84
    if (*flags & __IEC_FORCE_FLAG)
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
    85
        return forced_value_p;
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
    86
    return *real_value_p;
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
    87
}
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
    88
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
    89
void Remind(unsigned int offset, unsigned int count, void * p);
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
    90
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
    91
void RemindIterator(void* varp, __IEC_types_enum vartype)
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
    92
{
605
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
    93
    void *real_value_p = NULL;
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
    94
    char flags = 0;
605
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
    95
    UnpackVar(varp, vartype, &real_value_p, &flags);
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
    96
507
bf6f623d7450 Bug on testing Retain and Force flags fixed
laurent
parents: 504
diff changeset
    97
    if(flags & __IEC_RETAIN_FLAG){
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
    98
        USINT size = __get_type_enum_size(vartype);
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
    99
        /* compute next cursor positon*/
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   100
        unsigned int next_retain_offset = retain_offset + size;
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   101
        /* if buffer not full */
605
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   102
        Remind(retain_offset, size, real_value_p);
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   103
        /* increment cursor according size*/
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   104
        retain_offset = next_retain_offset;
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   105
    }
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   106
}
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   107
580
9dd978e6537c More robust retain buffer validity management
edouard
parents: 579
diff changeset
   108
extern int CheckRetainBuffer(void);
521
02cb9e5fb6f6 LPC transfer tested, added PLCInfo along MD5 checksum while invoking makefile
edouard
parents: 511
diff changeset
   109
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   110
void __init_debug(void)
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   111
{
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   112
    /* init local static vars */
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   113
    buffer_cursor = debug_buffer;
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   114
    retain_offset = 0;
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   115
    buffer_state = BUFFER_FREE;
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   116
    /* Iterate over all variables to fill debug buffer */
521
02cb9e5fb6f6 LPC transfer tested, added PLCInfo along MD5 checksum while invoking makefile
edouard
parents: 511
diff changeset
   117
    if(CheckRetainBuffer())
02cb9e5fb6f6 LPC transfer tested, added PLCInfo along MD5 checksum while invoking makefile
edouard
parents: 511
diff changeset
   118
    	__for_each_variable_do(RemindIterator);
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   119
    retain_offset = 0;
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   120
}
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   121
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   122
extern void InitiateDebugTransfer(void);
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   123
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   124
extern unsigned long __tick;
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   125
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   126
void __cleanup_debug(void)
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   127
{
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   128
    buffer_cursor = debug_buffer;
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   129
    InitiateDebugTransfer();
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   130
}
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   131
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   132
void __retrieve_debug(void)
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   133
{
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   134
}
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   135
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   136
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   137
void Retain(unsigned int offset, unsigned int count, void * p);
605
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   138
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   139
inline void BufferIterator(void* varp, __IEC_types_enum vartype, int do_debug)
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   140
{
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   141
    void *real_value_p = NULL;
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   142
    void *visible_value_p = NULL;
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   143
    char flags = 0;
605
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   144
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   145
    visible_value_p = UnpackVar(varp, vartype, &real_value_p, &flags);
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   146
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   147
    if(flags & ( __IEC_DEBUG_FLAG | __IEC_RETAIN_FLAG)){
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   148
        USINT size = __get_type_enum_size(vartype);
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   149
        if(flags & __IEC_DEBUG_FLAG){
605
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   150
            /* copy visible variable to buffer */;
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   151
            if(do_debug){
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   152
                /* compute next cursor positon.
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   153
                   No need to check overflow, as BUFFER_SIZE
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   154
                   is computed large enough */
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   155
                char* next_cursor = buffer_cursor + size;
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   156
                /* copy data to the buffer */
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   157
                memcpy(buffer_cursor, visible_value_p, size);
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   158
                /* increment cursor according size*/
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   159
                buffer_cursor = next_cursor;
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   160
            }
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   161
            /* re-force real value of outputs (M and Q)*/
649
c48023b6f0ec Fixing bug in forcing variables in runtime debug thread
laurent
parents: 611
diff changeset
   162
            if((flags & __IEC_FORCE_FLAG) && (flags & __IEC_OUTPUT_FLAG)){
605
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   163
                memcpy(real_value_p, visible_value_p, size);
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   164
            }
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   165
        }
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   166
        if(flags & __IEC_RETAIN_FLAG){
605
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   167
            /* compute next cursor positon*/
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   168
            unsigned int next_retain_offset = retain_offset + size;
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   169
            /* if buffer not full */
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   170
            Retain(retain_offset, size, real_value_p);
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   171
            /* increment cursor according size*/
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   172
            retain_offset = next_retain_offset;
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   173
        }
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   174
    }
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   175
}
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   176
605
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   177
void DebugIterator(void* varp, __IEC_types_enum vartype){
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   178
    BufferIterator(varp, vartype, 1);
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   179
}
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   180
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   181
void RetainIterator(void* varp, __IEC_types_enum vartype){
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   182
    BufferIterator(varp, vartype, 0);
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   183
}
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   184
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   185
extern int TryEnterDebugSection(void);
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   186
extern long AtomicCompareExchange(long*, long, long);
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   187
extern void LeaveDebugSection(void);
580
9dd978e6537c More robust retain buffer validity management
edouard
parents: 579
diff changeset
   188
extern void ValidateRetainBuffer(void);
9dd978e6537c More robust retain buffer validity management
edouard
parents: 579
diff changeset
   189
extern void InValidateRetainBuffer(void);
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   190
423
4d7ac355701d Fix some warnings during compilation
greg
parents: 397
diff changeset
   191
void __publish_debug(void)
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   192
{
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   193
    retain_offset = 0;
580
9dd978e6537c More robust retain buffer validity management
edouard
parents: 579
diff changeset
   194
    InValidateRetainBuffer();
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   195
    /* Check there is no running debugger re-configuration */
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   196
    if(TryEnterDebugSection()){
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   197
        /* Lock buffer */
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   198
        long latest_state = AtomicCompareExchange(
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   199
            &buffer_state,
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   200
            BUFFER_FREE,
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   201
            BUFFER_BUSY);
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   202
            
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   203
        /* If buffer was free */
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   204
        if(latest_state == BUFFER_FREE)
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   205
        {
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   206
            /* Reset buffer cursor */
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   207
            buffer_cursor = debug_buffer;
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   208
            /* Iterate over all variables to fill debug buffer */
605
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   209
            __for_each_variable_do(DebugIterator);
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   210
            
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   211
            /* Leave debug section,
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   212
             * Trigger asynchronous transmission 
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   213
             * (returns immediately) */
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   214
            InitiateDebugTransfer(); /* size */
581
57605e8e89f0 Also retain when debugger is stalled
edouard
parents: 580
diff changeset
   215
        }else{
57605e8e89f0 Also retain when debugger is stalled
edouard
parents: 580
diff changeset
   216
            /* when not debugging, do only retain */
57605e8e89f0 Also retain when debugger is stalled
edouard
parents: 580
diff changeset
   217
            __for_each_variable_do(RetainIterator);
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   218
        }
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   219
        LeaveDebugSection();
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   220
    }else{
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   221
        /* when not debugging, do only retain */
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   222
        __for_each_variable_do(RetainIterator);
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   223
    }
580
9dd978e6537c More robust retain buffer validity management
edouard
parents: 579
diff changeset
   224
    ValidateRetainBuffer();
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   225
}
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   226
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   227
#define __RegisterDebugVariable_case_t(TYPENAME) \
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   228
        case TYPENAME##_ENUM :\
477
f66a092b6e74 Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 458
diff changeset
   229
            ((__IEC_##TYPENAME##_t *)varp)->flags |= flags;\
f66a092b6e74 Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 458
diff changeset
   230
            if(force)\
f66a092b6e74 Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 458
diff changeset
   231
             ((__IEC_##TYPENAME##_t *)varp)->value = *((TYPENAME *)force);\
458
dfc6164e4022 Debugger still doesn't work crash less...
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 452
diff changeset
   232
            break;
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   233
#define __RegisterDebugVariable_case_p(TYPENAME)\
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   234
        case TYPENAME##_P_ENUM :\
477
f66a092b6e74 Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 458
diff changeset
   235
            ((__IEC_##TYPENAME##_p *)varp)->flags |= flags;\
f66a092b6e74 Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 458
diff changeset
   236
            if(force)\
f66a092b6e74 Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 458
diff changeset
   237
             ((__IEC_##TYPENAME##_p *)varp)->fvalue = *((TYPENAME *)force);\
511
518c30f2a507 Adding support for hard forcing located output
laurent
parents: 507
diff changeset
   238
            break;\
518c30f2a507 Adding support for hard forcing located output
laurent
parents: 507
diff changeset
   239
        case TYPENAME##_O_ENUM :\
518c30f2a507 Adding support for hard forcing located output
laurent
parents: 507
diff changeset
   240
            ((__IEC_##TYPENAME##_p *)varp)->flags |= flags;\
582
bb5d0367bf32 Fix typo in plc_debug.c
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 581
diff changeset
   241
            if(force){\
511
518c30f2a507 Adding support for hard forcing located output
laurent
parents: 507
diff changeset
   242
             ((__IEC_##TYPENAME##_p *)varp)->fvalue = *((TYPENAME *)force);\
518c30f2a507 Adding support for hard forcing located output
laurent
parents: 507
diff changeset
   243
             *(((__IEC_##TYPENAME##_p *)varp)->value) = *((TYPENAME *)force);\
582
bb5d0367bf32 Fix typo in plc_debug.c
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 581
diff changeset
   244
            }\
477
f66a092b6e74 Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 458
diff changeset
   245
            break;
f66a092b6e74 Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 458
diff changeset
   246
void RegisterDebugVariable(int idx, void* force)
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   247
{
452
2d0718a05cc7 Reflect changes in iec type definitions in matiec/lib
edouard
parents: 450
diff changeset
   248
    void *varp = NULL;
477
f66a092b6e74 Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 458
diff changeset
   249
    unsigned char flags = force ? __IEC_DEBUG_FLAG | __IEC_FORCE_FLAG : __IEC_DEBUG_FLAG;
458
dfc6164e4022 Debugger still doesn't work crash less...
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 452
diff changeset
   250
    switch(__find_variable(idx, &varp)){
611
b665a9001451 Fix debugger bug after merging matiec repositories
laurent
parents: 605
diff changeset
   251
        __ANY(__RegisterDebugVariable_case_t)
b665a9001451 Fix debugger bug after merging matiec repositories
laurent
parents: 605
diff changeset
   252
        __ANY(__RegisterDebugVariable_case_p)
452
2d0718a05cc7 Reflect changes in iec type definitions in matiec/lib
edouard
parents: 450
diff changeset
   253
    default:
2d0718a05cc7 Reflect changes in iec type definitions in matiec/lib
edouard
parents: 450
diff changeset
   254
        break;
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   255
    }
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   256
}
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   257
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   258
#define __ResetDebugVariablesIterator_case_t(TYPENAME) \
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   259
        case TYPENAME##_ENUM :\
477
f66a092b6e74 Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 458
diff changeset
   260
            ((__IEC_##TYPENAME##_t *)varp)->flags &= ~(__IEC_DEBUG_FLAG|__IEC_FORCE_FLAG);\
458
dfc6164e4022 Debugger still doesn't work crash less...
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 452
diff changeset
   261
            break;
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   262
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   263
#define __ResetDebugVariablesIterator_case_p(TYPENAME)\
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   264
        case TYPENAME##_P_ENUM :\
582
bb5d0367bf32 Fix typo in plc_debug.c
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 581
diff changeset
   265
        case TYPENAME##_O_ENUM :\
477
f66a092b6e74 Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 458
diff changeset
   266
            ((__IEC_##TYPENAME##_p *)varp)->flags &= ~(__IEC_DEBUG_FLAG|__IEC_FORCE_FLAG);\
458
dfc6164e4022 Debugger still doesn't work crash less...
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 452
diff changeset
   267
            break;
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   268
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   269
void ResetDebugVariablesIterator(void* varp, __IEC_types_enum vartype)
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   270
{
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   271
    /* force debug flag to 0*/
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   272
    switch(vartype){
611
b665a9001451 Fix debugger bug after merging matiec repositories
laurent
parents: 605
diff changeset
   273
        __ANY(__ResetDebugVariablesIterator_case_t)
b665a9001451 Fix debugger bug after merging matiec repositories
laurent
parents: 605
diff changeset
   274
        __ANY(__ResetDebugVariablesIterator_case_p)
452
2d0718a05cc7 Reflect changes in iec type definitions in matiec/lib
edouard
parents: 450
diff changeset
   275
    default:
2d0718a05cc7 Reflect changes in iec type definitions in matiec/lib
edouard
parents: 450
diff changeset
   276
        break;
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   277
    }
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   278
}
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   279
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   280
void ResetDebugVariables(void)
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   281
{
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   282
    __for_each_variable_do(ResetDebugVariablesIterator);
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   283
}
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   284
423
4d7ac355701d Fix some warnings during compilation
greg
parents: 397
diff changeset
   285
void FreeDebugData(void)
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   286
{
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   287
    /* atomically mark buffer as free */
423
4d7ac355701d Fix some warnings during compilation
greg
parents: 397
diff changeset
   288
    long latest_state;
4d7ac355701d Fix some warnings during compilation
greg
parents: 397
diff changeset
   289
    latest_state = AtomicCompareExchange(
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   290
        &buffer_state,
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   291
        BUFFER_BUSY,
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   292
        BUFFER_FREE);
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   293
}
452
2d0718a05cc7 Reflect changes in iec type definitions in matiec/lib
edouard
parents: 450
diff changeset
   294
int WaitDebugData(unsigned long *tick);
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   295
/* Wait until debug data ready and return pointer to it */
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   296
int GetDebugData(unsigned long *tick, unsigned long *size, void **buffer){
504
688e84df3408 Fixed debug again, did some code tidying
edouard
parents: 502
diff changeset
   297
    int wait_error = WaitDebugData(tick);
688e84df3408 Fixed debug again, did some code tidying
edouard
parents: 502
diff changeset
   298
    if(!wait_error){
502
5343ae43f6d0 LPC connector - one step further
edouard
parents: 491
diff changeset
   299
        *size = buffer_cursor - debug_buffer;
5343ae43f6d0 LPC connector - one step further
edouard
parents: 491
diff changeset
   300
        *buffer = debug_buffer;
5343ae43f6d0 LPC connector - one step further
edouard
parents: 491
diff changeset
   301
    }
504
688e84df3408 Fixed debug again, did some code tidying
edouard
parents: 502
diff changeset
   302
    return wait_error;
688e84df3408 Fixed debug again, did some code tidying
edouard
parents: 502
diff changeset
   303
}
688e84df3408 Fixed debug again, did some code tidying
edouard
parents: 502
diff changeset
   304
906
de452d65865c Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents: 649
diff changeset
   305
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   306
/* LOGGING
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   307
*/
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   308
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   309
#define LOG_LEVELS 4
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   310
#define LOG_CRITICAL 0
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   311
#define LOG_WARNING 1
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   312
#define LOG_INFO 2
923
6ef6e0b3a908 Fixed crash (segfault) when logging debug messages
Edouard Tisserant
parents: 921
diff changeset
   313
#define LOG_DEBUG 3
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   314
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   315
#define LOG_BUFFER_SIZE (1<<14) /*16Ko*/
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   316
#define LOG_BUFFER_MASK (LOG_BUFFER_SIZE-1)
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   317
static char LogBuff[LOG_LEVELS][LOG_BUFFER_SIZE];
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   318
void inline copy_to_log(uint8_t level, uint32_t buffpos, void* buf, uint32_t size){
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   319
    if(buffpos + size < LOG_BUFFER_SIZE){
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   320
        memcpy(&LogBuff[level][buffpos], buf, size);
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   321
    }else{
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   322
        uint32_t remaining = LOG_BUFFER_SIZE - buffpos - 1; 
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   323
        memcpy(&LogBuff[level][buffpos], buf, remaining);
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   324
        memcpy(LogBuff[level], buf + remaining, size - remaining);
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   325
    }
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   326
}
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   327
void inline copy_from_log(uint8_t level, uint32_t buffpos, void* buf, uint32_t size){
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   328
    if(buffpos + size < LOG_BUFFER_SIZE){
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   329
        memcpy(buf, &LogBuff[level][buffpos], size);
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   330
    }else{
911
ffa24427396a Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents: 910
diff changeset
   331
        uint32_t remaining = LOG_BUFFER_SIZE - buffpos; 
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   332
        memcpy(buf, &LogBuff[level][buffpos], remaining);
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   333
        memcpy(buf + remaining, LogBuff[level], size - remaining);
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   334
    }
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   335
}
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   336
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   337
/* Log buffer structure
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   338
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   339
 |<-Tail1.msgsize->|<-sizeof(mTail)->|<--Tail2.msgsize-->|<-sizeof(mTail)->|...
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   340
 |  Message1 Body  |      Tail1      |   Message2 Body   |      Tail2      |
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   341
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   342
*/
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   343
typedef struct {
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   344
    uint32_t msgidx;
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   345
    uint32_t msgsize;
921
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
   346
    unsigned long tick;
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
   347
    IEC_TIME time;
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   348
} mTail;
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   349
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   350
/* Log cursor : 64b
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   351
   |63 ... 32|31 ... 0|
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   352
   | Message | Buffer |
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   353
   | counter | Index  | */
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   354
static uint64_t LogCursor[LOG_LEVELS] = {0x0,0x0,0x0,0x0};
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   355
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   356
/* Store one log message of give size */
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   357
int LogMessage(uint8_t level, char* buf, uint32_t size){
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   358
    if(size < LOG_BUFFER_SIZE - sizeof(mTail)){
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   359
        uint32_t buffpos;
921
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
   360
        uint64_t new_cursor, old_cursor;
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
   361
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   362
        mTail tail;
921
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
   363
        tail.msgsize = size;
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
   364
        tail.tick = __tick;
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
   365
        PLC_GetTime(&tail.time);
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
   366
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   367
        /* We cannot increment both msg index and string pointer 
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   368
           in a single atomic operation but we can detect having been interrupted.
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   369
           So we can try with atomic compare and swap in a loop until operation
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   370
           succeeds non interrupted */
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   371
        do{
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   372
            old_cursor = LogCursor[level];
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   373
            buffpos = (uint32_t)old_cursor;
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   374
            tail.msgidx = (old_cursor >> 32); 
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   375
            new_cursor = ((uint64_t)(tail.msgidx + 1)<<32) 
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   376
                         | (uint64_t)((buffpos + size + sizeof(mTail)) & LOG_BUFFER_MASK);
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   377
        }while(!__sync_bool_compare_and_swap(&LogCursor[level],old_cursor,new_cursor));
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   378
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   379
        copy_to_log(level, buffpos, buf, size);
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   380
        copy_to_log(level, (buffpos + size) & LOG_BUFFER_MASK, &tail, sizeof(mTail));
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   381
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   382
        return 1; /* Success */
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   383
    }else{
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   384
        char mstr[] = "Logging error : message too big";
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   385
        LogMessage(LOG_CRITICAL, mstr, sizeof(mstr));
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   386
    }
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   387
    return 0;
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   388
}
906
de452d65865c Python runtime now dlopens shared library immediatly after transfer, and release it only immediately before reloading a new one. This is probably going to reveal lot of dirty cleanups during start/stop cycles.
Edouard Tisserant
parents: 649
diff changeset
   389
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   390
uint32_t GetLogCount(uint8_t level){
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   391
    return (uint64_t)LogCursor[level] >> 32;
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   392
}
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   393
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   394
/* Return message size and content */
921
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
   395
uint32_t GetLogMessage(uint8_t level, uint32_t msgidx, char* buf, uint32_t max_size, uint32_t* tick, uint32_t* tv_sec, uint32_t* tv_nsec){
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   396
    uint64_t cursor = LogCursor[level];
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   397
    if(cursor){
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   398
        /* seach cursor */
911
ffa24427396a Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents: 910
diff changeset
   399
        uint32_t stailpos = (uint32_t)cursor; 
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   400
        uint32_t smsgidx;
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   401
        mTail tail;
911
ffa24427396a Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents: 910
diff changeset
   402
        tail.msgidx = cursor >> 32;
ffa24427396a Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents: 910
diff changeset
   403
        tail.msgsize = 0;
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   404
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   405
        /* Message search loop */
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   406
        do {
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   407
            smsgidx = tail.msgidx;
911
ffa24427396a Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents: 910
diff changeset
   408
            stailpos = (stailpos - sizeof(mTail) - tail.msgsize ) & LOG_BUFFER_MASK;
917
401e44bae7c0 Now logging have 4 levels
Edouard Tisserant
parents: 911
diff changeset
   409
            copy_from_log(level, stailpos, &tail, sizeof(mTail));
911
ffa24427396a Log redirected to console, dump of all available log to console when connecting to PLC
Edouard Tisserant
parents: 910
diff changeset
   410
        }while((tail.msgidx == smsgidx - 1) && (tail.msgidx > msgidx));
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   411
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   412
        if(tail.msgidx == msgidx){
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   413
            uint32_t sbuffpos = (stailpos - tail.msgsize ) & LOG_BUFFER_MASK; 
921
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
   414
            uint32_t totalsize = tail.msgsize;
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
   415
            *tick = tail.tick; 
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
   416
            *tv_sec = tail.time.tv_sec; 
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
   417
            *tv_nsec = tail.time.tv_nsec; 
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
   418
            copy_from_log(level, sbuffpos, buf, 
a8db48ec2c31 Added log messages time-stamping
Edouard Tisserant
parents: 917
diff changeset
   419
                          totalsize > max_size ? max_size : totalsize);
910
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   420
            return totalsize;
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   421
        }
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   422
    }
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   423
    return 0;
f6d06bdd31e8 Experimental logging feature in PLC debug
Edouard Tisserant
parents: 906
diff changeset
   424
}