C/alloc Debugging

From Attie's Wiki
Jump to: navigation, search

Header

#define myFree(a) _myFree(__FILE__,__LINE__,(a))
void _myFree(const char *fn, int ln, void *p);
#define myMalloc(a) _myMalloc(__FILE__,__LINE__,(a))
void *_myMalloc(const char *fn, int ln, int s);
#define myCalloc(a,b) _myCalloc(__FILE__,__LINE__,(a),(b))
void *_myCalloc(const char *fn, int ln, int x, int s);
#define myRealloc(a,b) _myRealloc(__FILE__,__LINE__,(a),(b))
void *_myRealloc(const char *fn, int ln, void *op, int s);

Source

void _myFree(const char *fn, int ln, void *p) {
	fprintf(stderr, "*** %s(%p) *** (from %s:%d)\n", __FUNCTION__, p, fn,ln);
	fflush(stdout);
	free(p);
}
void *_myMalloc(const char *fn, int ln, int s) {
	void *p;
	p = malloc(s);
	fprintf(stderr, "*** %s(%d) *** %p (from %s:%d)\n", __FUNCTION__, s,p, fn,ln);
	fflush(stdout);
	return p;
}
void *_myCalloc(const char *fn, int ln, int x, int s) {
	void *p;
	p = calloc(x,s);
	fprintf(stderr, "*** %s(%d,%d) *** %p (from %s:%d)\n", __FUNCTION__, x,s,p, fn,ln);
	fflush(stdout);
	return p;
}
void *_myRealloc(const char *fn, int ln, void *op, int s) {
	void *p;
	p = realloc(op,s);
	fprintf(stderr, "*** %s(%p) *** %p (from %s:%d)\n", __FUNCTION__, op, p, fn,ln);
	fflush(stdout);
	return p;
}

Post-mortem

Catch the output, and then pipe through grep:

grep -P '^\*\*\* (_my(M|Re|C)alloc|_myFree)\([^\)]*\) \*\*\*'
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox