* On "publish", when buffer is free, debugger stores arbitrary variables
* content into, and mark this buffer as filled
* Buffer content is read asynchronously, (from non real time part),
* and then buffer marked free again.
#ifdef TARGET_DEBUG_AND_RETAIN_DISABLE
void __init_debug (void){}
void __cleanup_debug (void){}
void __retrieve_debug(void){}
void __publish_debug (void){}
#include "iec_types_all.h"
#ifndef TARGET_ONLINE_DEBUG_DISABLE
#define BUFFER_SIZE %(buffer_size)d
/* Atomically accessed variable for buffer state */
static long buffer_state = BUFFER_FREE;
char debug_buffer[BUFFER_SIZE];
static char* buffer_cursor = debug_buffer;
static unsigned int retain_offset = 0;
%(programs_declarations)s
* Declare global variables from resources and conf
%(extern_variables_declarations)s
static dbgvardsc_t dbgvardsc[] = {
typedef void(*__for_each_variable_do_fp)(dbgvardsc_t*);
void __for_each_variable_do(__for_each_variable_do_fp fp)
for(i = 0; i < sizeof(dbgvardsc)/sizeof(dbgvardsc_t); i++){
dbgvardsc_t *dsc = &dbgvardsc[i];
if(dsc->type != UNKNOWN_ENUM)
#define __Unpack_desc_type dbgvardsc_t
void Remind(unsigned int offset, unsigned int count, void * p);
void RemindIterator(dbgvardsc_t *dsc)
void *real_value_p = NULL;
UnpackVar(dsc, &real_value_p, &flags);
if(flags & __IEC_RETAIN_FLAG){
USINT size = __get_type_enum_size(dsc->type);
/* compute next cursor positon*/
unsigned int next_retain_offset = retain_offset + size;
Remind(retain_offset, size, real_value_p);
/* increment cursor according size*/
retain_offset = next_retain_offset;
extern int CheckRetainBuffer(void);
extern void InitRetain(void);
/* init local static vars */
#ifndef TARGET_ONLINE_DEBUG_DISABLE
buffer_cursor = debug_buffer;
buffer_state = BUFFER_FREE;
/* Iterate over all variables to fill debug buffer */
__for_each_variable_do(RemindIterator);
char mstr[] = "RETAIN memory invalid - defaults used";
LogMessage(LOG_WARNING, mstr, sizeof(mstr));
extern void InitiateDebugTransfer(void);
extern void CleanupRetain(void);
extern unsigned long __tick;
void __cleanup_debug(void)
#ifndef TARGET_ONLINE_DEBUG_DISABLE
buffer_cursor = debug_buffer;
void __retrieve_debug(void)
void Retain(unsigned int offset, unsigned int count, void * p);
static inline void BufferIterator(dbgvardsc_t *dsc, int do_debug)
void *real_value_p = NULL;
void *visible_value_p = NULL;
visible_value_p = UnpackVar(dsc, &real_value_p, &flags);
if(flags & ( __IEC_DEBUG_FLAG | __IEC_RETAIN_FLAG)){
USINT size = __get_type_enum_size(dsc->type);
#ifndef TARGET_ONLINE_DEBUG_DISABLE
if(flags & __IEC_DEBUG_FLAG){
/* copy visible variable to buffer */;
/* compute next cursor positon.
No need to check overflow, as BUFFER_SIZE
is computed large enough */
if((dsc->type == STRING_ENUM) ||
(dsc->type == STRING_P_ENUM) ||
(dsc->type == STRING_O_ENUM)){
/* optimization for strings */
size = ((STRING*)visible_value_p)->len + 1;
char* next_cursor = buffer_cursor + size;
/* copy data to the buffer */
memcpy(buffer_cursor, visible_value_p, size);
/* increment cursor according size*/
buffer_cursor = next_cursor;
/* re-force real value of outputs (M and Q)*/
if((flags & __IEC_FORCE_FLAG) && (flags & __IEC_OUTPUT_FLAG)){
memcpy(real_value_p, visible_value_p, size);
if(flags & __IEC_RETAIN_FLAG){
/* compute next cursor positon*/
unsigned int next_retain_offset = retain_offset + size;
Retain(retain_offset, size, real_value_p);
/* increment cursor according size*/
retain_offset = next_retain_offset;
void DebugIterator(dbgvardsc_t *dsc){
void RetainIterator(dbgvardsc_t *dsc){
unsigned int retain_size = 0;
/* GetRetainSizeIterator */
void GetRetainSizeIterator(dbgvardsc_t *dsc)
void *real_value_p = NULL;
UnpackVar(dsc, &real_value_p, &flags);
if(flags & __IEC_RETAIN_FLAG){
USINT size = __get_type_enum_size(dsc->type);
/* Calc retain buffer size */
/* Return size of all retain variables */
unsigned int GetRetainSize(void)
__for_each_variable_do(GetRetainSizeIterator);
extern void PLC_GetTime(IEC_TIME*);
extern int TryEnterDebugSection(void);
extern long AtomicCompareExchange(long*, long, long);
extern long long AtomicCompareExchange64(long long* , long long , long long);
extern void LeaveDebugSection(void);
extern void ValidateRetainBuffer(void);
extern void InValidateRetainBuffer(void);
void __publish_debug(void)
InValidateRetainBuffer();
#ifndef TARGET_ONLINE_DEBUG_DISABLE
/* Check there is no running debugger re-configuration */
if(TryEnterDebugSection()){
long latest_state = AtomicCompareExchange(
if(latest_state == BUFFER_FREE)
/* Reset buffer cursor */
buffer_cursor = debug_buffer;
/* Iterate over all variables to fill debug buffer */
__for_each_variable_do(DebugIterator);
* Trigger asynchronous transmission
* (returns immediately) */
InitiateDebugTransfer(); /* size */
/* when not debugging, do only retain */
__for_each_variable_do(RetainIterator);
/* when not debugging, do only retain */
__for_each_variable_do(RetainIterator);
#ifndef TARGET_ONLINE_DEBUG_DISABLE
#define __RegisterDebugVariable_case_t(TYPENAME) \
((__IEC_##TYPENAME##_t *)varp)->flags |= flags;\
((__IEC_##TYPENAME##_t *)varp)->value = *((TYPENAME *)force);\
#define __RegisterDebugVariable_case_p(TYPENAME)\
case TYPENAME##_P_ENUM :\
((__IEC_##TYPENAME##_p *)varp)->flags |= flags;\
((__IEC_##TYPENAME##_p *)varp)->fvalue = *((TYPENAME *)force);\
case TYPENAME##_O_ENUM :\
((__IEC_##TYPENAME##_p *)varp)->flags |= flags;\
((__IEC_##TYPENAME##_p *)varp)->fvalue = *((TYPENAME *)force);\
*(((__IEC_##TYPENAME##_p *)varp)->value) = *((TYPENAME *)force);\
void RegisterDebugVariable(unsigned int idx, void* force)
if(idx < sizeof(dbgvardsc)/sizeof(dbgvardsc_t)){
unsigned char flags = force ?
__IEC_DEBUG_FLAG | __IEC_FORCE_FLAG :
dbgvardsc_t *dsc = &dbgvardsc[idx];
__ANY(__RegisterDebugVariable_case_t)
__ANY(__RegisterDebugVariable_case_p)
#define __ResetDebugVariablesIterator_case_t(TYPENAME) \
((__IEC_##TYPENAME##_t *)varp)->flags &= ~(__IEC_DEBUG_FLAG|__IEC_FORCE_FLAG);\
#define __ResetDebugVariablesIterator_case_p(TYPENAME)\
case TYPENAME##_P_ENUM :\
case TYPENAME##_O_ENUM :\
((__IEC_##TYPENAME##_p *)varp)->flags &= ~(__IEC_DEBUG_FLAG|__IEC_FORCE_FLAG);\
void ResetDebugVariablesIterator(dbgvardsc_t *dsc)
/* force debug flag to 0*/
__ANY(__ResetDebugVariablesIterator_case_t)
__ANY(__ResetDebugVariablesIterator_case_p)
void ResetDebugVariables(void)
__for_each_variable_do(ResetDebugVariablesIterator);
/* atomically mark buffer as free */
int WaitDebugData(unsigned long *tick);
/* Wait until debug data ready and return pointer to it */
int GetDebugData(unsigned long *tick, unsigned long *size, void **buffer){
int wait_error = WaitDebugData(tick);
*size = buffer_cursor - debug_buffer;