Changeset 430548b
- Timestamp:
- 05/28/15 21:24:13 (9 years ago)
- Branches:
- master
- Children:
- 3105f50
- Parents:
- fc0b9bf
- git-author:
- Hal Finkel <hfinkel@…> (05/28/15 21:24:13)
- git-committer:
- Hal Finkel <hfinkel@…> (05/28/15 21:24:13)
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
Makefile
rfc0b9bf r430548b 5 5 LDFLAGS = -lpthread -ldl 6 6 7 all: libmemlog.so libmemlog.a7 all: libmemlog.so memlog.o 8 8 9 libmemlog.a: memlog.c 10 rm -f memlog.o 9 memlog.o: memlog.c 11 10 $(CC) $(CPPFLAGS) $(CFLAGS) -c -o memlog.o memlog.c 12 ar cr libmemlog.a memlog.o13 ranlib libmemlog.a14 11 15 12 libmemlog.so: memlog.c … … 17 14 18 15 clean: 19 rm -f memlog.o libmemlog. a libmemlog.so16 rm -f memlog.o libmemlog.so 20 17 -
memlog.c
r625d5f9 r430548b 8 8 #include <string.h> 9 9 10 #include <malloc.h> 10 11 #include <execinfo.h> 11 12 #include <sys/syscall.h> … … 54 55 } 55 56 56 static void print_context( void *caller) {57 static void print_context(const void *caller) { 57 58 struct rusage usage; 58 59 if (getrusage(RUSAGE_SELF, &usage)) { … … 68 69 69 70 int found_caller = 0; 70 caller = __builtin_extract_return_addr( caller);71 caller = __builtin_extract_return_addr((void*) caller); 71 72 for (int pci = 0; pci < num_pcs; ++pci) { 72 73 intptr_t pc = (intptr_t) pcs[pci]; … … 120 121 } 121 122 122 static void record_malloc(size_t size, void *ptr, void *caller) {123 static void record_malloc(size_t size, void *ptr, const void *caller) { 123 124 if (!log_file) 124 125 return; … … 135 136 } 136 137 137 static void record_free(void *ptr, void *caller) {138 static void record_free(void *ptr, const void *caller) { 138 139 if (!log_file) 139 140 return; … … 158 159 extern void __libc_free(void *ptr); 159 160 160 void *malloc(size_t size) { 161 #ifdef __PIC__ 162 #define FUNC(x) x 163 #else 164 #define FUNC(x) __wrap_ ## x 165 #endif 166 167 void *FUNC(malloc)(size_t size) { 168 const void *caller = __builtin_return_address(0); 169 161 170 if (in_malloc) 162 171 return __libc_malloc(size); … … 166 175 void *ptr = __libc_malloc(size); 167 176 168 void *caller = __builtin_return_address(0);169 177 record_malloc(size, ptr, caller); 170 178 … … 173 181 } 174 182 175 void *realloc(void *ptr, size_t size) { 183 void *FUNC(realloc)(void *ptr, size_t size) { 184 const void *caller = __builtin_return_address(0); 185 176 186 if (in_malloc) 177 187 return __libc_realloc(ptr, size); … … 181 191 void *nptr = __libc_realloc(ptr, size); 182 192 183 void *caller = __builtin_return_address(0);184 193 if (ptr) 185 194 record_free(ptr, caller); … … 191 200 } 192 201 193 void *calloc(size_t nmemb, size_t size) { 202 void *FUNC(calloc)(size_t nmemb, size_t size) { 203 const void *caller = __builtin_return_address(0); 204 194 205 if (in_malloc) 195 206 return __libc_calloc(nmemb, size); … … 199 210 void *ptr = __libc_calloc(nmemb, size); 200 211 201 void *caller = __builtin_return_address(0);202 212 record_malloc(nmemb*size, ptr, caller); 203 213 … … 207 217 } 208 218 209 void *memalign(size_t boundary, size_t size) { 219 void *FUNC(memalign)(size_t boundary, size_t size) { 220 const void *caller = __builtin_return_address(0); 221 210 222 if (in_malloc) 211 223 return __libc_memalign(boundary, size); … … 215 227 void *ptr = __libc_memalign(boundary, size); 216 228 217 void *caller = __builtin_return_address(0);218 229 record_malloc(size, ptr, caller); 219 230 … … 223 234 } 224 235 225 void free(void *ptr) { 236 void FUNC(free)(void *ptr) { 237 const void *caller = __builtin_return_address(0); 238 226 239 if (in_malloc || !ptr) 227 240 return __libc_free(ptr); … … 229 242 in_malloc = 1; 230 243 231 void *caller = __builtin_return_address(0);232 244 record_free(ptr, caller); 233 245
Note: See TracChangeset
for help on using the changeset viewer.