Changeset 6444e03
- Timestamp:
- 06/02/15 18:23:28 (10 years ago)
- Branches:
- master
- Children:
- ed8b809
- Parents:
- 21d3542
- git-author:
- Hal Finkel <hfinkel@…> (06/02/15 18:23:28)
- git-committer:
- Hal Finkel <hfinkel@…> (06/02/15 18:23:28)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
memlog.cpp
r21d3542 r6444e03 16 16 #include <malloc.h> 17 17 #include <execinfo.h> 18 #include <sys/mman.h> 18 19 #include <sys/syscall.h> 19 20 #include <sys/time.h> … … 52 53 53 54 void *initial_brk = 0; 55 56 #ifdef __PIC__ 57 typedef int (*__real_posix_memalign_t)(void **memptr, size_t alignment, 58 size_t size); 59 __real_posix_memalign_t __real_posix_memalign = 0; 60 #else 61 extern "C" { 62 extern int __real_posix_memalign(void **memptr, size_t alignment, size_t size); 63 } 64 #endif 54 65 55 66 __attribute__((__constructor__)) … … 248 259 extern "C" { 249 260 extern void *__libc_malloc(size_t size); 261 extern void *__libc_valloc(size_t size); 250 262 extern void *__libc_realloc(void *ptr, size_t size); 251 263 extern void *__libc_calloc(size_t nmemb, size_t size); … … 253 265 extern void __libc_free(void *ptr); 254 266 267 extern void *__mmap(void *addr, size_t length, int prot, int flags, 268 int fd, off_t offset); 269 extern void *__mmap64(void *addr, size_t length, int prot, int flags, 270 int fd, off64_t offset); 271 extern int __munmap(void *addr, size_t length); 272 255 273 #ifdef __PIC__ 256 274 #define FUNC(x) x … … 269 287 270 288 void *ptr = __libc_malloc(size); 271 272 record_malloc(size, ptr, caller); 289 if (ptr) 290 record_malloc(size, ptr, caller); 291 292 in_malloc = 0; 293 return ptr; 294 } 295 296 void *FUNC(valloc)(size_t size) { 297 const void *caller = 298 __builtin_extract_return_addr(__builtin_return_address(0)); 299 300 if (in_malloc) 301 return __libc_valloc(size); 302 303 in_malloc = 1; 304 305 void *ptr = __libc_valloc(size); 306 if (ptr) 307 record_malloc(size, ptr, caller); 273 308 274 309 in_malloc = 0; … … 289 324 if (ptr) 290 325 record_free(ptr, caller); 291 record_malloc(size, nptr, caller); 326 if (nptr) 327 record_malloc(size, nptr, caller); 292 328 293 329 in_malloc = 0; … … 307 343 void *ptr = __libc_calloc(nmemb, size); 308 344 309 record_malloc(nmemb*size, ptr, caller); 345 if (ptr) 346 record_malloc(nmemb*size, ptr, caller); 310 347 311 348 in_malloc = 0; … … 325 362 void *ptr = __libc_memalign(boundary, size); 326 363 327 record_malloc(size, ptr, caller); 364 if (ptr) 365 record_malloc(size, ptr, caller); 328 366 329 367 in_malloc = 0; … … 348 386 } 349 387 388 int FUNC(posix_memalign)(void **memptr, size_t alignment, size_t size) { 389 const void *caller = 390 __builtin_extract_return_addr(__builtin_return_address(0)); 391 392 #ifdef __PIC__ 393 if (!__real_posix_memalign) 394 if (!(__real_posix_memalign = 395 (__real_posix_memalign_t) dlsym(RTLD_NEXT, "posix_memalign"))) 396 return -1; 397 #endif 398 399 if (in_malloc) 400 return __real_posix_memalign(memptr, alignment, size); 401 402 in_malloc = 1; 403 404 int r = __real_posix_memalign(memptr, alignment, size); 405 406 if (!r) 407 record_malloc(size, *memptr, caller); 408 409 in_malloc = 0; 410 411 return r; 412 } 413 414 void *FUNC(mmap)(void *addr, size_t length, int prot, int flags, 415 int fd, off_t offset) { 416 const void *caller = 417 __builtin_extract_return_addr(__builtin_return_address(0)); 418 419 if (in_malloc) 420 return __mmap(addr, length, prot, flags, fd, offset); 421 422 in_malloc = 1; 423 424 void *ptr = __mmap(addr, length, prot, flags, fd, offset); 425 426 if (ptr != MAP_FAILED) 427 record_malloc(length, ptr, caller); 428 429 in_malloc = 0; 430 431 return ptr; 432 } 433 434 void *FUNC(mmap64)(void *addr, size_t length, int prot, int flags, 435 int fd, off64_t offset) { 436 const void *caller = 437 __builtin_extract_return_addr(__builtin_return_address(0)); 438 439 if (in_malloc) 440 return __mmap64(addr, length, prot, flags, fd, offset); 441 442 in_malloc = 1; 443 444 void *ptr = __mmap64(addr, length, prot, flags, fd, offset); 445 446 if (ptr != MAP_FAILED) 447 record_malloc(length, ptr, caller); 448 449 in_malloc = 0; 450 451 return ptr; 452 } 453 454 int FUNC(munmap)(void *addr, size_t length) { 455 const void *caller = 456 __builtin_extract_return_addr(__builtin_return_address(0)); 457 458 if (in_malloc) 459 return __munmap(addr, length); 460 461 in_malloc = 1; 462 463 record_free(addr, caller); 464 465 int r = __munmap(addr, length); 466 467 in_malloc = 0; 468 469 return r; 470 } 471 350 472 } // extern "C" 351 473
Note: See TracChangeset
for help on using the changeset viewer.