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