check format strings on eDebug-style functions
[enigma2.git] / lib / base / eerror.h
1 #ifndef __E_ERROR__
2 #define __E_ERROR__
3
4 #include <string>
5 #include <map>       
6 #include <new>
7 #include <libsig_comp.h>
8
9 // to use memleak check change the following in configure.ac
10 // * add -rdynamic to LD_FLAGS
11 // * add -DMEMLEAK_CHECK to CPP_FLAGS
12
13 #ifdef MEMLEAK_CHECK
14 #define BACKTRACE_DEPTH 5
15 // when you have c++filt and corresponding libs on your platform
16 // then add -DHAVE_CPP_FILT to CPP_FLAGS in configure.ac
17 #include <map>
18 #include <lib/base/elock.h>
19 #include <execinfo.h>
20 #include <string>
21 #include <new>
22 #endif // MEMLEAK_CHECK
23
24 #ifndef NULL
25 #define NULL 0
26 #endif
27
28 #ifndef SWIG
29 #define CHECKFORMAT __attribute__ ((__format__(__printf__, 1, 2)))
30 #else
31 #define CHECKFORMAT
32 #endif
33
34 void CHECKFORMAT eFatal(const char*, ...);
35
36 enum { lvlDebug=1, lvlWarning=2, lvlFatal=4 };
37
38 #ifndef SWIG
39 extern Signal2<void, int, const std::string&> logOutput;
40 extern int logOutputConsole;
41 #endif
42
43 #ifdef ASSERT
44 #undef ASSERT
45 #endif
46
47 #ifdef DEBUG
48     void CHECKFORMAT eDebug(const char*, ...);
49     void CHECKFORMAT eDebugNoNewLine(const char*, ...);
50     void CHECKFORMAT eWarning(const char*, ...);
51 #ifndef SWIG
52     #define ASSERT(x) { if (!(x)) eFatal("%s:%d ASSERTION %s FAILED!", __FILE__, __LINE__, #x); }
53 #endif
54
55 #ifdef MEMLEAK_CHECK
56 typedef struct
57 {
58         unsigned int address;
59         unsigned int size;
60         char *file;
61         void *backtrace[BACKTRACE_DEPTH];
62         unsigned char btcount;
63         unsigned short line;
64         unsigned char type;
65 } ALLOC_INFO;
66
67 typedef std::map<unsigned int, ALLOC_INFO> AllocList;
68
69 extern AllocList *allocList;
70 extern pthread_mutex_t memLock;
71
72 static inline void AddTrack(unsigned int addr,  unsigned int asize,  const char *fname, unsigned int lnum, unsigned int type)
73 {
74         ALLOC_INFO info;
75
76         if(!allocList)
77                 allocList = new(AllocList);
78
79         info.address = addr;
80         info.file = strdup(fname);
81         info.line = lnum;
82         info.size = asize;
83         info.type = type;
84         info.btcount = backtrace( info.backtrace, BACKTRACE_DEPTH );
85         singleLock s(memLock);
86         (*allocList)[addr]=info;
87 };
88
89 static inline void RemoveTrack(unsigned int addr, unsigned int type)
90 {
91         if(!allocList)
92                 return;
93         AllocList::iterator i;
94         singleLock s(memLock);
95         i = allocList->find(addr);
96         if ( i != allocList->end() )
97         {
98                 if ( i->second.type != type )
99                         i->second.type=3;
100                 else
101                 {
102                         free(i->second.file);
103                         allocList->erase(i);
104                 }
105         }
106 };
107
108 inline void * operator new(unsigned int size, const char *file, int line)
109 {
110         void *ptr = (void *)malloc(size);
111         AddTrack((unsigned int)ptr, size, file, line, 1);
112         return(ptr);
113 };
114
115 inline void operator delete(void *p)
116 {
117         RemoveTrack((unsigned int)p,1);
118         free(p);
119 };
120
121 inline void * operator new[](unsigned int size, const char *file, int line)
122 {
123         void *ptr = (void *)malloc(size);
124         AddTrack((unsigned int)ptr, size, file, line, 2);
125         return(ptr);
126 };
127
128 inline void operator delete[](void *p)
129 {
130         RemoveTrack((unsigned int)p, 2);
131         free(p);
132 };
133
134 inline void DumpUnfreed()
135 {
136         AllocList::iterator i;
137         unsigned int totalSize = 0;
138
139         if(!allocList)
140                 return;
141
142         for(i = allocList->begin(); i != allocList->end(); i++)
143         {
144                 unsigned int tmp;
145                 printf("%s\tLINE %d\tADDRESS %p\t%d unfreed\ttype %d\n",
146                         i->second.file, i->second.line, (void*)i->second.address, i->second.size, i->second.type);
147                 totalSize += i->second.size;
148                 char **bt_string = backtrace_symbols( i->second.backtrace, i->second.btcount );
149                 for ( tmp=0; tmp < i->second.btcount; tmp++ )
150                 {
151                         if ( bt_string[tmp] )
152                         {
153 #ifdef HAVE_CPP_FILT
154                                 char *beg = strchr(bt_string[tmp], '(');
155                                 if ( beg )
156                                 {
157                                         std::string tmp1(beg+1);
158                                         int pos1=tmp1.find('+'), pos2=tmp1.find(')');
159                                         std::string tmp2(tmp1.substr(pos1,(pos2-pos1)-1));
160                                         std::string cmd="c++filt ";
161                                         cmd+=tmp1.substr(0,pos1);
162                                         FILE *f = popen(cmd.c_str(), "r");
163                                         char buf[256];
164                                         if (f)
165                                         {
166                                                 size_t rd = fread(buf, 1, 255, f);
167                                                 if ( rd > 0 )
168                                                 {
169                                                         buf[rd-1]=0;
170                                                         printf("%s %s\n", buf, tmp2.c_str() );
171                                                 }
172                                                 else
173                                                         printf("%s\n", tmp1.substr(0,pos1).c_str());
174                                                 fclose(f);
175                                         }
176                                 }
177                                 else
178 #endif // HAVE_CPP_FILT
179                                         printf("%s\n", bt_string[tmp]);
180                         }
181                 }
182                 free(bt_string);
183                 printf("\n");
184         }
185
186         printf("-----------------------------------------------------------\n");
187         printf("Total Unfreed: %d bytes\n", totalSize);
188         fflush(stdout);
189 };
190 #define new new(__FILE__, __LINE__)
191
192 #endif // MEMLEAK_CHECK
193
194
195 #else
196     inline void eDebug(const char* fmt, ...)
197     {
198     }
199
200     inline void eDebugNoNewLine(const char* fmt, ...)
201     {
202     }
203
204     inline void eWarning(const char* fmt, ...)
205     {
206     }
207     #define ASSERT(x) do { } while (0)
208 #endif //DEBUG
209
210 #endif // __E_ERROR__