aboutsummaryrefslogtreecommitdiff
path: root/lib/base/eerror.h
blob: 2ae89e2f47d312b532625758e4c5082581e08582 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#ifndef __E_ERROR__
#define __E_ERROR__

#include "config.h"
#include <string>
#include <map>       
#include <new>
#include <libsig_comp.h>

// to use memleak check change the following in configure.ac
// * add -rdynamic to LD_FLAGS
// * add -DMEMLEAK_CHECK to CPP_FLAGS

#ifdef MEMLEAK_CHECK
#define BACKTRACE_DEPTH 5
// when you have c++filt and corresponding libs on your platform
// then add -DHAVE_CPP_FILT to CPP_FLAGS in configure.ac
#include <map>
#include <lib/base/elock.h>
#include <execinfo.h>
#include <string>
#include <new>
#endif // MEMLEAK_CHECK

#ifndef NULL
#define NULL 0
#endif

void eFatal(const char* fmt, ...);

enum { lvlDebug=1, lvlWarning=2, lvlFatal=4 };

#ifndef SWIG
extern Signal2<void, int, const std::string&> logOutput;
extern int logOutputConsole;
#endif

#ifdef ASSERT
#undef ASSERT
#endif

#ifdef DEBUG
    void eDebug(const char* fmt, ...);
    void eDebugNoNewLine(const char* fmt, ...);
    void eWarning(const char* fmt, ...);
#ifndef SWIG
    #define ASSERT(x) { if (!(x)) eFatal("%s:%d ASSERTION %s FAILED!", __FILE__, __LINE__, #x); }
#endif

#ifdef MEMLEAK_CHECK
typedef struct
{
	unsigned int address;
	unsigned int size;
	char *file;
	void *backtrace[BACKTRACE_DEPTH];
	unsigned char btcount;
	unsigned short line;
	unsigned char type;
} ALLOC_INFO;

typedef std::map<unsigned int, ALLOC_INFO> AllocList;

extern AllocList *allocList;
extern pthread_mutex_t memLock;

static inline void AddTrack(unsigned int addr,  unsigned int asize,  const char *fname, unsigned int lnum, unsigned int type)
{
	ALLOC_INFO info;

	if(!allocList)
		allocList = new(AllocList);

	info.address = addr;
	info.file = strdup(fname);
	info.line = lnum;
	info.size = asize;
	info.type = type;
	info.btcount = backtrace( info.backtrace, BACKTRACE_DEPTH );
	singleLock s(memLock);
	(*allocList)[addr]=info;
};

static inline void RemoveTrack(unsigned int addr, unsigned int type)
{
	if(!allocList)
		return;
	AllocList::iterator i;
	singleLock s(memLock);
	i = allocList->find(addr);
	if ( i != allocList->end() )
	{
		if ( i->second.type != type )
			i->second.type=3;
		else
		{
			free(i->second.file);
			allocList->erase(i);
		}
	}
};

inline void * operator new(unsigned int size, const char *file, int line)
{
	void *ptr = (void *)malloc(size);
	AddTrack((unsigned int)ptr, size, file, line, 1);
	return(ptr);
};

inline void operator delete(void *p)
{
	RemoveTrack((unsigned int)p,1);
	free(p);
};

inline void * operator new[](unsigned int size, const char *file, int line)
{
	void *ptr = (void *)malloc(size);
	AddTrack((unsigned int)ptr, size, file, line, 2);
	return(ptr);
};

inline void operator delete[](void *p)
{
	RemoveTrack((unsigned int)p, 2);
	free(p);
};

inline void DumpUnfreed()
{
	AllocList::iterator i;
	unsigned int totalSize = 0;

	if(!allocList)
		return;

	for(i = allocList->begin(); i != allocList->end(); i++)
	{
		unsigned int tmp;
		printf("%s\tLINE %d\tADDRESS %p\t%d unfreed\ttype %d\n",
			i->second.file, i->second.line, (void*)i->second.address, i->second.size, i->second.type);
		totalSize += i->second.size;
		char **bt_string = backtrace_symbols( i->second.backtrace, i->second.btcount );
		for ( tmp=0; tmp < i->second.btcount; tmp++ )
		{
			if ( bt_string[tmp] )
			{
#ifdef HAVE_CPP_FILT
				char *beg = strchr(bt_string[tmp], '(');
				if ( beg )
				{
					std::string tmp1(beg+1);
					int pos1=tmp1.find('+'), pos2=tmp1.find(')');
					std::string tmp2(tmp1.substr(pos1,(pos2-pos1)-1));
					std::string cmd="c++filt ";
					cmd+=tmp1.substr(0,pos1);
					FILE *f = popen(cmd.c_str(), "r");
					char buf[256];
					if (f)
					{
						size_t rd = fread(buf, 1, 255, f);
						if ( rd > 0 )
						{
							buf[rd-1]=0;
							printf("%s %s\n", buf, tmp2.c_str() );
						}
						else
							printf("%s\n", tmp1.substr(0,pos1).c_str());
						fclose(f);
					}
				}
				else
#endif // HAVE_CPP_FILT
					printf("%s\n", bt_string[tmp]);
			}
		}
		free(bt_string);
		printf("\n");
	}

	printf("-----------------------------------------------------------\n");
	printf("Total Unfreed: %d bytes\n", totalSize);
	fflush(stdout);
};
#define new new(__FILE__, __LINE__)

#endif // MEMLEAK_CHECK


#else
    inline void eDebug(const char* fmt, ...)
    {
    }

    inline void eDebugNoNewLine(const char* fmt, ...)
    {
    }

    inline void eWarning(const char* fmt, ...)
    {
    }
    #define ASSERT(x) do { } while (0)
#endif //DEBUG

#endif // __E_ERROR__