| 1 | /**************************************************************************** |
|---|
| 2 | * This file is part of the v2lin Library. |
|---|
| 3 | * VxWorks is a registered trademark of Wind River Systems, Inc. |
|---|
| 4 | * |
|---|
| 5 | * Copyright (C) 2006 Constantine Shulyupin, conan.sh@gmail.com |
|---|
| 6 | * |
|---|
| 7 | * The v2lin library is free software; you can redistribute it and/or |
|---|
| 8 | * modify it under the terms of the GNU Lesser General Public |
|---|
| 9 | * License as published by the Free Software Foundation; either |
|---|
| 10 | * version 2.1 of the License, or (at your option) any later version. |
|---|
| 11 | * |
|---|
| 12 | * The v2lin Library is distributed in the hope that it will be useful, |
|---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 15 | * Lesser General Public License for more details. |
|---|
| 16 | * |
|---|
| 17 | ****************************************************************************/ |
|---|
| 18 | |
|---|
| 19 | #ifndef __V2LIN_DEBUG__ |
|---|
| 20 | #define __V2LIN_DEBUG__ |
|---|
| 21 | |
|---|
| 22 | #include <time.h> |
|---|
| 23 | #include <string.h> |
|---|
| 24 | #include <stdio.h> |
|---|
| 25 | #include <sys/time.h> |
|---|
| 26 | #include <errno.h> |
|---|
| 27 | |
|---|
| 28 | #include <sys/types.h> |
|---|
| 29 | #ifdef USE_CYGWIN |
|---|
| 30 | #include <unistd.h> |
|---|
| 31 | #else |
|---|
| 32 | #include <linux/unistd.h> |
|---|
| 33 | #endif |
|---|
| 34 | pid_t gettid(void); |
|---|
| 35 | |
|---|
| 36 | #include "internal.h" |
|---|
| 37 | |
|---|
| 38 | extern int trace; |
|---|
| 39 | extern int trace_cnt; |
|---|
| 40 | extern int tid; |
|---|
| 41 | extern char trace_fn[100]; |
|---|
| 42 | |
|---|
| 43 | extern struct timeval prev_tv; |
|---|
| 44 | extern struct timeval start_tv; |
|---|
| 45 | |
|---|
| 46 | //#define DEBUG |
|---|
| 47 | |
|---|
| 48 | void trace_time(); |
|---|
| 49 | #ifdef DEBUG |
|---|
| 50 | |
|---|
| 51 | // TRACEV is for printing variable name and value |
|---|
| 52 | #define TRACEV(F,V) do { if (trace) { \ |
|---|
| 53 | if ( tid != gettid() ) { \ |
|---|
| 54 | tid = gettid();\ |
|---|
| 55 | fprintf(stderr,"thread=%i\n",tid);}; \ |
|---|
| 56 | fprintf(stderr,#V"="F"\n",V); \ |
|---|
| 57 | } } while (0) |
|---|
| 58 | |
|---|
| 59 | #define TRACEF(args ... ) do { if (trace) { \ |
|---|
| 60 | trace_time(); \ |
|---|
| 61 | fprintf(stderr,"%s:%i", __FILE__, __LINE__); \ |
|---|
| 62 | if ( strcmp(trace_fn,__FUNCTION__) ) \ |
|---|
| 63 | strcpy(trace_fn,__FUNCTION__) && fprintf(stderr," %s()",trace_fn); \ |
|---|
| 64 | fprintf(stderr," "args); \ |
|---|
| 65 | fprintf(stderr,"\n"); \ |
|---|
| 66 | } } while (0) |
|---|
| 67 | |
|---|
| 68 | // if ( tid != gettid() ) { |
|---|
| 69 | // tid = gettid(); |
|---|
| 70 | // fprintf(stderr,"\nthread=%i task=%x %s\n",(int)tid,(int)my_task(),my_task()?my_task()->taskname:NULL);}; |
|---|
| 71 | // |
|---|
| 72 | |
|---|
| 73 | #else |
|---|
| 74 | #define TRACEV(F,V) |
|---|
| 75 | #define TRACEF(args ... ) |
|---|
| 76 | #endif |
|---|
| 77 | |
|---|
| 78 | #define FNSTART int status = OK; |
|---|
| 79 | #define FNFINISH exit: return status; |
|---|
| 80 | |
|---|
| 81 | #ifdef TRACE_IN_OUT |
|---|
| 82 | #define FN_IN(s) TRACEF("%s {",s) |
|---|
| 83 | #define FN_OUT(s) TRACEF("%s }",s) |
|---|
| 84 | #else |
|---|
| 85 | #define FN_IN(s) |
|---|
| 86 | #define FN_OUT(s) |
|---|
| 87 | #endif |
|---|
| 88 | |
|---|
| 89 | // ON_ERR defines common error processing behaveur: |
|---|
| 90 | // a) ignore error and continue code exection |
|---|
| 91 | // b) goto to finalization sections of a functions |
|---|
| 92 | |
|---|
| 93 | #define ON_ERR // ignore |
|---|
| 94 | //#define ON_ERR goto exit |
|---|
| 95 | |
|---|
| 96 | // CHK and CHK0 - common macros for error processing |
|---|
| 97 | // and optional code tracing. This marcos prints error messages and |
|---|
| 98 | // optionally processes errors in case of subfunction failures. |
|---|
| 99 | #define TRACE_errno() fprintf(stderr,"\t|%s:%d| errno=%i(%x) %s \n",__FUNCTION__, __LINE__, errno, errno, VxWorksError(errno)) |
|---|
| 100 | |
|---|
| 101 | // CHK supposes !0 is success, suitable for checking pointers |
|---|
| 102 | // and functions in form: CHK(0<read(fd,buff,count)); |
|---|
| 103 | #define CHK(command) \ |
|---|
| 104 | do { int ret;\ |
|---|
| 105 | FN_IN(#command); \ |
|---|
| 106 | if ( ! (ret=(int) (command)) ) { \ |
|---|
| 107 | TRACEF("ERROR: %s \n\treturned 0",#command); \ |
|---|
| 108 | TRACE_errno(); \ |
|---|
| 109 | ON_ERR ;\ |
|---|
| 110 | } \ |
|---|
| 111 | FN_OUT(#command); \ |
|---|
| 112 | } while (0) |
|---|
| 113 | |
|---|
| 114 | // CHK0 supposes 0 is success, sutable for checking most of libc functions |
|---|
| 115 | #define CHK0(command) \ |
|---|
| 116 | do { \ |
|---|
| 117 | int status; \ |
|---|
| 118 | FN_IN(#command); \ |
|---|
| 119 | if ((status = (command)) != 0 ) { \ |
|---|
| 120 | TRACEF("ERROR: %s \n\treturned status: #%i (%x):\"%s\"",\ |
|---|
| 121 | #command,status,status, VxWorksError(status)); \ |
|---|
| 122 | TRACE_errno(); \ |
|---|
| 123 | ON_ERR;\ |
|---|
| 124 | } \ |
|---|
| 125 | FN_OUT(#command); \ |
|---|
| 126 | } while (0) |
|---|
| 127 | |
|---|
| 128 | //#define TRACE_PTHREAD |
|---|
| 129 | #ifdef TRACE_PTHREAD |
|---|
| 130 | |
|---|
| 131 | #define pthread_mutex_lock(m) \ |
|---|
| 132 | do { \ |
|---|
| 133 | if ( EBUSY == pthread_mutex_trylock(m)) { TRACEF("pthread_mutex_lock %s %x EBUSY",#m,m); \ |
|---|
| 134 | pthread_mutex_lock(m); } \ |
|---|
| 135 | TRACEF("pthread_mutex_lock %s %x OK",#m,m); \ |
|---|
| 136 | } while ( 0 ) |
|---|
| 137 | |
|---|
| 138 | #define pthread_mutex_unlock(m) \ |
|---|
| 139 | do { \ |
|---|
| 140 | TRACEF("pthread_mutex_unlock %s %x OK",#m,m); \ |
|---|
| 141 | pthread_mutex_unlock(m); \ |
|---|
| 142 | } while ( 0 ) |
|---|
| 143 | |
|---|
| 144 | #define pthread_cond_timedwait(args ... ) \ |
|---|
| 145 | ( fprintf(stderr,"%s:%i %s", __FILE__, __LINE__, __FUNCTION__), \ |
|---|
| 146 | fprintf(stderr,"pthread_cond_timedwait %x\n",#args), pthread_cond_timedwait(args)) |
|---|
| 147 | |
|---|
| 148 | #define thread_cleanup_pop(i) do { \ |
|---|
| 149 | TRACEF("thread_cleanup_pop %s OK",#i); \ |
|---|
| 150 | thread_cleanup_pop(m); \ |
|---|
| 151 | } while ( 0 |
|---|
| 152 | #endif |
|---|
| 153 | |
|---|
| 154 | #endif |
|---|