targets/plc_debug.c
author Edouard Tisserant
Tue, 18 Jun 2019 13:58:45 +0200
branchfix_fb_resize_wire_bug
changeset 2616 7a434c8d3f3c
parent 2501 eba2bbb2dd9a
child 2632 534387caf43d
permissions -rw-r--r--
GraphicCommons.py : Wire.SetPoints() move filtering before computation of Start and End points to avoid setting them with arbitrary directions.
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
 * */
2501
eba2bbb2dd9a Make online debug optional
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2190
diff changeset
    13
#ifdef TARGET_DEBUG_AND_RETAIN_DISABLE
1800
1711339585ce make possible to turn off at compile time online debugging, logging
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1677
diff changeset
    14
1711339585ce make possible to turn off at compile time online debugging, logging
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1677
diff changeset
    15
void __init_debug    (void){}
1711339585ce make possible to turn off at compile time online debugging, logging
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1677
diff changeset
    16
void __cleanup_debug (void){}
1711339585ce make possible to turn off at compile time online debugging, logging
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1677
diff changeset
    17
void __retrieve_debug(void){}
1711339585ce make possible to turn off at compile time online debugging, logging
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1677
diff changeset
    18
void __publish_debug (void){}
1711339585ce make possible to turn off at compile time online debugging, logging
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1677
diff changeset
    19
1711339585ce make possible to turn off at compile time online debugging, logging
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1677
diff changeset
    20
#else
1711339585ce make possible to turn off at compile time online debugging, logging
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1677
diff changeset
    21
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    22
#include "iec_types_all.h"
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    23
#include "POUS.h"
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    24
/*for memcpy*/
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    25
#include <string.h>
335
c5f3f71e7260 fixed bug : buffer overflow when debugging PLC with python blocks
greg
parents: 280
diff changeset
    26
#include <stdio.h>
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    27
2501
eba2bbb2dd9a Make online debug optional
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2190
diff changeset
    28
#ifndef TARGET_ONLINE_DEBUG_DISABLE
335
c5f3f71e7260 fixed bug : buffer overflow when debugging PLC with python blocks
greg
parents: 280
diff changeset
    29
#define BUFFER_SIZE %(buffer_size)d
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    30
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    31
/* Atomically accessed variable for buffer state */
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    32
#define BUFFER_FREE 0
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    33
#define BUFFER_BUSY 1
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    34
static long buffer_state = BUFFER_FREE;
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    35
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    36
/* The buffer itself */
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    37
char debug_buffer[BUFFER_SIZE];
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    38
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    39
/* Buffer's cursor*/
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    40
static char* buffer_cursor = debug_buffer;
2501
eba2bbb2dd9a Make online debug optional
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2190
diff changeset
    41
#endif
eba2bbb2dd9a Make online debug optional
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2190
diff changeset
    42
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
    43
static unsigned int retain_offset = 0;
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    44
/***
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    45
 * Declare programs 
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    46
 **/
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    47
%(programs_declarations)s
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    48
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    49
/***
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    50
 * Declare global variables from resources and conf 
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    51
 **/
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    52
%(extern_variables_declarations)s
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    53
1432
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
    54
typedef const struct {
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
    55
    void *ptr;
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
    56
    __IEC_types_enum type;
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
    57
} dbgvardsc_t;
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
    58
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
    59
static dbgvardsc_t dbgvardsc[] = {
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
    60
%(variable_decl_array)s
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
    61
};
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
    62
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
    63
typedef void(*__for_each_variable_do_fp)(dbgvardsc_t*);
452
2d0718a05cc7 Reflect changes in iec type definitions in matiec/lib
edouard
parents: 450
diff changeset
    64
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
    65
{
2190
b7d803fc44db Fix compilation warning/error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2172
diff changeset
    66
    unsigned int i;
1432
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
    67
    for(i = 0; i < sizeof(dbgvardsc)/sizeof(dbgvardsc_t); i++){
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
    68
        dbgvardsc_t *dsc = &dbgvardsc[i];
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
    69
        if(dsc->type != UNKNOWN_ENUM) 
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
    70
            (*fp)(dsc);
452
2d0718a05cc7 Reflect changes in iec type definitions in matiec/lib
edouard
parents: 450
diff changeset
    71
    }
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
    72
}
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
    73
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
    74
#define __Unpack_case_t(TYPENAME) \
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
    75
        case TYPENAME##_ENUM :\
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
    76
            *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
    77
            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
    78
            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
    79
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
    80
#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
    81
        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
    82
            *flags = __IEC_OUTPUT_FLAG;\
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
    83
        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
    84
            *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
    85
            *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
    86
            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
    87
            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
    88
1432
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
    89
void* UnpackVar(dbgvardsc_t *dsc, void **real_value_p, char *flags)
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
    90
{
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
    91
    void *varp = dsc->ptr;
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
    92
    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
    93
    *flags = 0;
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
    94
    /* find data to copy*/
1432
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
    95
    switch(dsc->type){
611
b665a9001451 Fix debugger bug after merging matiec repositories
laurent
parents: 605
diff changeset
    96
        __ANY(__Unpack_case_t)
b665a9001451 Fix debugger bug after merging matiec repositories
laurent
parents: 605
diff changeset
    97
        __ANY(__Unpack_case_p)
452
2d0718a05cc7 Reflect changes in iec type definitions in matiec/lib
edouard
parents: 450
diff changeset
    98
    default:
2d0718a05cc7 Reflect changes in iec type definitions in matiec/lib
edouard
parents: 450
diff changeset
    99
        break;
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   100
    }
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
   101
    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
   102
        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
   103
    return *real_value_p;
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   104
}
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
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
   107
1432
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
   108
void RemindIterator(dbgvardsc_t *dsc)
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   109
{
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
   110
    void *real_value_p = NULL;
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   111
    char flags = 0;
1432
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
   112
    UnpackVar(dsc, &real_value_p, &flags);
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   113
507
bf6f623d7450 Bug on testing Retain and Force flags fixed
laurent
parents: 504
diff changeset
   114
    if(flags & __IEC_RETAIN_FLAG){
1432
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
   115
        USINT size = __get_type_enum_size(dsc->type);
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   116
        /* compute next cursor positon*/
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   117
        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
   118
        /* 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
   119
        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
   120
        /* increment cursor according size*/
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   121
        retain_offset = next_retain_offset;
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   122
    }
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
580
9dd978e6537c More robust retain buffer validity management
edouard
parents: 579
diff changeset
   125
extern int CheckRetainBuffer(void);
1457
ff7cfce737ca Added PLCID variable accessible from C side, set with binarie's MD5. Added retain init and cleanup calls. Extended tests/python to test PLCID
Edouard Tisserant
parents: 1432
diff changeset
   126
extern void InitRetain(void);
521
02cb9e5fb6f6 LPC transfer tested, added PLCInfo along MD5 checksum while invoking makefile
edouard
parents: 511
diff changeset
   127
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   128
void __init_debug(void)
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   129
{
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   130
    /* init local static vars */
2501
eba2bbb2dd9a Make online debug optional
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2190
diff changeset
   131
#ifndef TARGET_ONLINE_DEBUG_DISABLE	
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   132
    buffer_cursor = debug_buffer;
2501
eba2bbb2dd9a Make online debug optional
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2190
diff changeset
   133
    buffer_state = BUFFER_FREE;
eba2bbb2dd9a Make online debug optional
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2190
diff changeset
   134
#endif
eba2bbb2dd9a Make online debug optional
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2190
diff changeset
   135
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   136
    retain_offset = 0;
1457
ff7cfce737ca Added PLCID variable accessible from C side, set with binarie's MD5. Added retain init and cleanup calls. Extended tests/python to test PLCID
Edouard Tisserant
parents: 1432
diff changeset
   137
    InitRetain();
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   138
    /* Iterate over all variables to fill debug buffer */
1459
c9065fb5de0a Added log message when RETAIN memory not valid
Edouard Tisserant
parents: 1457
diff changeset
   139
    if(CheckRetainBuffer()){
521
02cb9e5fb6f6 LPC transfer tested, added PLCInfo along MD5 checksum while invoking makefile
edouard
parents: 511
diff changeset
   140
    	__for_each_variable_do(RemindIterator);
1459
c9065fb5de0a Added log message when RETAIN memory not valid
Edouard Tisserant
parents: 1457
diff changeset
   141
    }else{
c9065fb5de0a Added log message when RETAIN memory not valid
Edouard Tisserant
parents: 1457
diff changeset
   142
    	char mstr[] = "RETAIN memory invalid - defaults used";
c9065fb5de0a Added log message when RETAIN memory not valid
Edouard Tisserant
parents: 1457
diff changeset
   143
        LogMessage(LOG_WARNING, mstr, sizeof(mstr));
c9065fb5de0a Added log message when RETAIN memory not valid
Edouard Tisserant
parents: 1457
diff changeset
   144
    }
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   145
    retain_offset = 0;
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   146
}
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   147
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   148
extern void InitiateDebugTransfer(void);
1457
ff7cfce737ca Added PLCID variable accessible from C side, set with binarie's MD5. Added retain init and cleanup calls. Extended tests/python to test PLCID
Edouard Tisserant
parents: 1432
diff changeset
   149
extern void CleanupRetain(void);
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   150
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   151
extern unsigned long __tick;
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   152
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   153
void __cleanup_debug(void)
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   154
{
2501
eba2bbb2dd9a Make online debug optional
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2190
diff changeset
   155
#ifndef TARGET_ONLINE_DEBUG_DISABLE	
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   156
    buffer_cursor = debug_buffer;
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   157
    InitiateDebugTransfer();
2501
eba2bbb2dd9a Make online debug optional
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2190
diff changeset
   158
#endif    
eba2bbb2dd9a Make online debug optional
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2190
diff changeset
   159
1457
ff7cfce737ca Added PLCID variable accessible from C side, set with binarie's MD5. Added retain init and cleanup calls. Extended tests/python to test PLCID
Edouard Tisserant
parents: 1432
diff changeset
   160
    CleanupRetain();
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   161
}
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   162
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   163
void __retrieve_debug(void)
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   164
{
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   165
}
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   166
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   167
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   168
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
   169
1479
8f41aa88aa46 fix many compilation warnings about static variables used by non-static functions
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1459
diff changeset
   170
static inline void BufferIterator(dbgvardsc_t *dsc, int do_debug)
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
   171
{
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
    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
   173
    void *visible_value_p = NULL;
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   174
    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
   175
1432
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
   176
    visible_value_p = UnpackVar(dsc, &real_value_p, &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
   177
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
    if(flags & ( __IEC_DEBUG_FLAG | __IEC_RETAIN_FLAG)){
1432
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
   179
        USINT size = __get_type_enum_size(dsc->type);
2501
eba2bbb2dd9a Make online debug optional
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2190
diff changeset
   180
eba2bbb2dd9a Make online debug optional
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2190
diff changeset
   181
#ifndef TARGET_ONLINE_DEBUG_DISABLE	
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   182
        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
   183
            /* 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
   184
            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
   185
                /* 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
   186
                   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
   187
                   is computed large enough */
1677
db05cb9e0439 fix problem with debugging external string variables
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1479
diff changeset
   188
		if((dsc->type == STRING_ENUM)   ||
db05cb9e0439 fix problem with debugging external string variables
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1479
diff changeset
   189
		   (dsc->type == STRING_P_ENUM) ||
db05cb9e0439 fix problem with debugging external string variables
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1479
diff changeset
   190
		   (dsc->type == STRING_O_ENUM)){
1074
92a009dc5826 Optimzed tracing of STRING type variables value. Now, only used size is transfered in debug buffer.
Edouard Tisserant
parents: 985
diff changeset
   191
                    /* optimization for strings */
92a009dc5826 Optimzed tracing of STRING type variables value. Now, only used size is transfered in debug buffer.
Edouard Tisserant
parents: 985
diff changeset
   192
                    size = ((STRING*)visible_value_p)->len + 1;
92a009dc5826 Optimzed tracing of STRING type variables value. Now, only used size is transfered in debug buffer.
Edouard Tisserant
parents: 985
diff changeset
   193
                }
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
   194
                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
   195
                /* 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
   196
                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
   197
                /* 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
   198
                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
   199
            }
2250ed42e306 debugger : forcing %M and %Q variables on each cycle, and rework of code for optimization and concision
Edouard Tisserant
parents: 582
diff changeset
   200
            /* 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
   201
            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
   202
                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
   203
            }
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   204
        }
2501
eba2bbb2dd9a Make online debug optional
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2190
diff changeset
   205
#endif	
eba2bbb2dd9a Make online debug optional
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2190
diff changeset
   206
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   207
        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
   208
            /* 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
   209
            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
   210
            /* 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
   211
            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
   212
            /* 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
   213
            retain_offset = next_retain_offset;
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   214
        }
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   215
    }
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   216
}
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   217
1432
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
   218
void DebugIterator(dbgvardsc_t *dsc){
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
   219
    BufferIterator(dsc, 1);
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
   220
}
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
   221
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
   222
void RetainIterator(dbgvardsc_t *dsc){
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
   223
    BufferIterator(dsc, 0);
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   224
}
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   225
2172
9fa5be79bb77 Add helper function GetRetainSize()
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1800
diff changeset
   226
9fa5be79bb77 Add helper function GetRetainSize()
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1800
diff changeset
   227
unsigned int retain_size = 0;
9fa5be79bb77 Add helper function GetRetainSize()
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1800
diff changeset
   228
9fa5be79bb77 Add helper function GetRetainSize()
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1800
diff changeset
   229
/* GetRetainSizeIterator */
9fa5be79bb77 Add helper function GetRetainSize()
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1800
diff changeset
   230
void GetRetainSizeIterator(dbgvardsc_t *dsc)
9fa5be79bb77 Add helper function GetRetainSize()
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1800
diff changeset
   231
{
9fa5be79bb77 Add helper function GetRetainSize()
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1800
diff changeset
   232
    void *real_value_p = NULL;
9fa5be79bb77 Add helper function GetRetainSize()
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1800
diff changeset
   233
    char flags = 0;
9fa5be79bb77 Add helper function GetRetainSize()
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1800
diff changeset
   234
    UnpackVar(dsc, &real_value_p, &flags);
9fa5be79bb77 Add helper function GetRetainSize()
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1800
diff changeset
   235
9fa5be79bb77 Add helper function GetRetainSize()
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1800
diff changeset
   236
    if(flags & __IEC_RETAIN_FLAG){
9fa5be79bb77 Add helper function GetRetainSize()
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1800
diff changeset
   237
        USINT size = __get_type_enum_size(dsc->type);
9fa5be79bb77 Add helper function GetRetainSize()
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1800
diff changeset
   238
        /* Calc retain buffer size */
9fa5be79bb77 Add helper function GetRetainSize()
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1800
diff changeset
   239
        retain_size += size;
9fa5be79bb77 Add helper function GetRetainSize()
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1800
diff changeset
   240
    }
9fa5be79bb77 Add helper function GetRetainSize()
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1800
diff changeset
   241
}
9fa5be79bb77 Add helper function GetRetainSize()
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1800
diff changeset
   242
9fa5be79bb77 Add helper function GetRetainSize()
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1800
diff changeset
   243
/* Return size of all retain variables */
9fa5be79bb77 Add helper function GetRetainSize()
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1800
diff changeset
   244
unsigned int GetRetainSize(void)
9fa5be79bb77 Add helper function GetRetainSize()
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1800
diff changeset
   245
{
9fa5be79bb77 Add helper function GetRetainSize()
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1800
diff changeset
   246
    __for_each_variable_do(GetRetainSizeIterator);
9fa5be79bb77 Add helper function GetRetainSize()
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1800
diff changeset
   247
    return retain_size;
9fa5be79bb77 Add helper function GetRetainSize()
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1800
diff changeset
   248
}
9fa5be79bb77 Add helper function GetRetainSize()
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1800
diff changeset
   249
9fa5be79bb77 Add helper function GetRetainSize()
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 1800
diff changeset
   250
969
1950fe687dde Fix warning with LogMessage function
Laurent Bessard
parents: 954
diff changeset
   251
extern void PLC_GetTime(IEC_TIME*);
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   252
extern int TryEnterDebugSection(void);
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   253
extern long AtomicCompareExchange(long*, long, long);
954
ab487d32ce9a Made logging compatible with windows API
Edouard Tisserant
parents: 944
diff changeset
   254
extern long long AtomicCompareExchange64(long long* , long long , long long);
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   255
extern void LeaveDebugSection(void);
580
9dd978e6537c More robust retain buffer validity management
edouard
parents: 579
diff changeset
   256
extern void ValidateRetainBuffer(void);
9dd978e6537c More robust retain buffer validity management
edouard
parents: 579
diff changeset
   257
extern void InValidateRetainBuffer(void);
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   258
423
4d7ac355701d Fix some warnings during compilation
greg
parents: 397
diff changeset
   259
void __publish_debug(void)
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   260
{
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   261
    retain_offset = 0;
580
9dd978e6537c More robust retain buffer validity management
edouard
parents: 579
diff changeset
   262
    InValidateRetainBuffer();
2501
eba2bbb2dd9a Make online debug optional
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2190
diff changeset
   263
    
eba2bbb2dd9a Make online debug optional
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2190
diff changeset
   264
#ifndef TARGET_ONLINE_DEBUG_DISABLE 
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   265
    /* Check there is no running debugger re-configuration */
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   266
    if(TryEnterDebugSection()){
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   267
        /* Lock buffer */
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   268
        long latest_state = AtomicCompareExchange(
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   269
            &buffer_state,
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   270
            BUFFER_FREE,
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   271
            BUFFER_BUSY);
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   272
            
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   273
        /* If buffer was free */
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   274
        if(latest_state == BUFFER_FREE)
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   275
        {
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   276
            /* Reset buffer cursor */
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   277
            buffer_cursor = debug_buffer;
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   278
            /* 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
   279
            __for_each_variable_do(DebugIterator);
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   280
            
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   281
            /* Leave debug section,
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   282
             * Trigger asynchronous transmission 
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   283
             * (returns immediately) */
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   284
            InitiateDebugTransfer(); /* size */
581
57605e8e89f0 Also retain when debugger is stalled
edouard
parents: 580
diff changeset
   285
        }else{
57605e8e89f0 Also retain when debugger is stalled
edouard
parents: 580
diff changeset
   286
            /* when not debugging, do only retain */
57605e8e89f0 Also retain when debugger is stalled
edouard
parents: 580
diff changeset
   287
            __for_each_variable_do(RetainIterator);
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   288
        }
239
112b4bc523b3 Fixed bad IPC choice for debugger/PLC/control thread collaboration
etisserant
parents: 236
diff changeset
   289
        LeaveDebugSection();
2501
eba2bbb2dd9a Make online debug optional
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2190
diff changeset
   290
    }else
eba2bbb2dd9a Make online debug optional
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2190
diff changeset
   291
#endif
eba2bbb2dd9a Make online debug optional
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2190
diff changeset
   292
    {
483
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   293
        /* when not debugging, do only retain */
bc26c42d2eec fixed greg's crap in win32, enhanced debug stability, implemented preliminary retain
edouard
parents: 477
diff changeset
   294
        __for_each_variable_do(RetainIterator);
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   295
    }
580
9dd978e6537c More robust retain buffer validity management
edouard
parents: 579
diff changeset
   296
    ValidateRetainBuffer();
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   297
}
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   298
2501
eba2bbb2dd9a Make online debug optional
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2190
diff changeset
   299
#ifndef TARGET_ONLINE_DEBUG_DISABLE
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   300
#define __RegisterDebugVariable_case_t(TYPENAME) \
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   301
        case TYPENAME##_ENUM :\
477
f66a092b6e74 Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 458
diff changeset
   302
            ((__IEC_##TYPENAME##_t *)varp)->flags |= flags;\
f66a092b6e74 Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 458
diff changeset
   303
            if(force)\
f66a092b6e74 Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 458
diff changeset
   304
             ((__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
   305
            break;
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   306
#define __RegisterDebugVariable_case_p(TYPENAME)\
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   307
        case TYPENAME##_P_ENUM :\
477
f66a092b6e74 Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 458
diff changeset
   308
            ((__IEC_##TYPENAME##_p *)varp)->flags |= flags;\
f66a092b6e74 Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 458
diff changeset
   309
            if(force)\
f66a092b6e74 Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 458
diff changeset
   310
             ((__IEC_##TYPENAME##_p *)varp)->fvalue = *((TYPENAME *)force);\
511
518c30f2a507 Adding support for hard forcing located output
laurent
parents: 507
diff changeset
   311
            break;\
518c30f2a507 Adding support for hard forcing located output
laurent
parents: 507
diff changeset
   312
        case TYPENAME##_O_ENUM :\
518c30f2a507 Adding support for hard forcing located output
laurent
parents: 507
diff changeset
   313
            ((__IEC_##TYPENAME##_p *)varp)->flags |= flags;\
582
bb5d0367bf32 Fix typo in plc_debug.c
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 581
diff changeset
   314
            if(force){\
511
518c30f2a507 Adding support for hard forcing located output
laurent
parents: 507
diff changeset
   315
             ((__IEC_##TYPENAME##_p *)varp)->fvalue = *((TYPENAME *)force);\
518c30f2a507 Adding support for hard forcing located output
laurent
parents: 507
diff changeset
   316
             *(((__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
   317
            }\
477
f66a092b6e74 Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 458
diff changeset
   318
            break;
2190
b7d803fc44db Fix compilation warning/error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2172
diff changeset
   319
void RegisterDebugVariable(unsigned int idx, void* force)
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   320
{
1432
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
   321
    if(idx  < sizeof(dbgvardsc)/sizeof(dbgvardsc_t)){
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
   322
        unsigned char flags = force ?
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
   323
            __IEC_DEBUG_FLAG | __IEC_FORCE_FLAG :
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
   324
            __IEC_DEBUG_FLAG;
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
   325
        dbgvardsc_t *dsc = &dbgvardsc[idx];
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
   326
        void *varp = dsc->ptr;
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
   327
        switch(dsc->type){
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
   328
            __ANY(__RegisterDebugVariable_case_t)
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
   329
            __ANY(__RegisterDebugVariable_case_p)
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
   330
        default:
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
   331
            break;
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
   332
        }
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   333
    }
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   334
}
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   335
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   336
#define __ResetDebugVariablesIterator_case_t(TYPENAME) \
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   337
        case TYPENAME##_ENUM :\
477
f66a092b6e74 Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 458
diff changeset
   338
            ((__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
   339
            break;
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   340
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   341
#define __ResetDebugVariablesIterator_case_p(TYPENAME)\
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   342
        case TYPENAME##_P_ENUM :\
582
bb5d0367bf32 Fix typo in plc_debug.c
Edouqrd Tisserant <edouard.tisserant@gmail.com>
parents: 581
diff changeset
   343
        case TYPENAME##_O_ENUM :\
477
f66a092b6e74 Arbitrary variable forcing
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 458
diff changeset
   344
            ((__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
   345
            break;
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   346
1432
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
   347
void ResetDebugVariablesIterator(dbgvardsc_t *dsc)
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   348
{
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   349
    /* force debug flag to 0*/
1432
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
   350
    void *varp = dsc->ptr;
8872223a675b Optimized plc_debug.c generated code. Should produce smaller code size. Added statically initialized array for PLC tracable variable description.
Edouard Tisserant
parents: 1403
diff changeset
   351
    switch(dsc->type){
611
b665a9001451 Fix debugger bug after merging matiec repositories
laurent
parents: 605
diff changeset
   352
        __ANY(__ResetDebugVariablesIterator_case_t)
b665a9001451 Fix debugger bug after merging matiec repositories
laurent
parents: 605
diff changeset
   353
        __ANY(__ResetDebugVariablesIterator_case_p)
452
2d0718a05cc7 Reflect changes in iec type definitions in matiec/lib
edouard
parents: 450
diff changeset
   354
    default:
2d0718a05cc7 Reflect changes in iec type definitions in matiec/lib
edouard
parents: 450
diff changeset
   355
        break;
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   356
    }
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   357
}
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   358
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   359
void ResetDebugVariables(void)
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   360
{
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   361
    __for_each_variable_do(ResetDebugVariablesIterator);
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   362
}
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   363
423
4d7ac355701d Fix some warnings during compilation
greg
parents: 397
diff changeset
   364
void FreeDebugData(void)
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   365
{
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   366
    /* atomically mark buffer as free */
1403
dd6d40094782 Fixed unused variable in plc_debug.c
Edouard Tisserant
parents: 1074
diff changeset
   367
    AtomicCompareExchange(
209
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   368
        &buffer_state,
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   369
        BUFFER_BUSY,
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   370
        BUFFER_FREE);
08dc3d064cb5 Moved template C code to targets dir. Cleaned up some forgotten print.
etisserant
parents:
diff changeset
   371
}
452
2d0718a05cc7 Reflect changes in iec type definitions in matiec/lib
edouard
parents: 450
diff changeset
   372
int WaitDebugData(unsigned long *tick);
450
18583d13f0fa Preliminary accessor support for debug
Edouard TISSERANT <edouard.tisserant@gmail.com>
parents: 423
diff changeset
   373
/* 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
   374
int GetDebugData(unsigned long *tick, unsigned long *size, void **buffer){
504
688e84df3408 Fixed debug again, did some code tidying
edouard
parents: 502
diff changeset
   375
    int wait_error = WaitDebugData(tick);
688e84df3408 Fixed debug again, did some code tidying
edouard
parents: 502
diff changeset
   376
    if(!wait_error){
502
5343ae43f6d0 LPC connector - one step further
edouard
parents: 491
diff changeset
   377
        *size = buffer_cursor - debug_buffer;
5343ae43f6d0 LPC connector - one step further
edouard
parents: 491
diff changeset
   378
        *buffer = debug_buffer;
5343ae43f6d0 LPC connector - one step further
edouard
parents: 491
diff changeset
   379
    }
504
688e84df3408 Fixed debug again, did some code tidying
edouard
parents: 502
diff changeset
   380
    return wait_error;
688e84df3408 Fixed debug again, did some code tidying
edouard
parents: 502
diff changeset
   381
}
2501
eba2bbb2dd9a Make online debug optional
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2190
diff changeset
   382
#endif
eba2bbb2dd9a Make online debug optional
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2190
diff changeset
   383
#endif
eba2bbb2dd9a Make online debug optional
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents: 2190
diff changeset
   384