Study from open source code -- malloc trace (1)
2010-08-19 17:47
441 查看
for working on fsck the disk mounted on board, i read the the code of the e2fsck, study some useful skills, such like this:
malloc_trace.
we malloc or free the memory in programes, some skills will be useful for us to debug
if want get more information, please 'man malloc_hook'
a example follw:
malloc_trace.
we malloc or free the memory in programes, some skills will be useful for us to debug
if want get more information, please 'man malloc_hook'
a example follw:
/* * ===================================================================================== * * Filename: mtrace.c * * Description: trace the malloc in heap, that may be helpful for our debugging * * Version: 1.0 * Created: 2010年08月19日 15时48分15秒 * Revision: none * Compiler: gcc * * Author: StrongMouse (), inch.zheng@besovideo.com * Company: Besovideo * * ===================================================================================== */ #include <stdio.h> #include <malloc.h> typedef void * _ptr_t; //global variable for storage the old hook static void (*tr_old_free_hook)(_ptr_t ptr, const void *caller); static _ptr_t (*tr_old_malloc_hook)(size_t size, const void *caller); static _ptr_t (*tr_old_realloc_hook)(_ptr_t ptr, size_t size, const void *caller); //just as extern, decalare only static void my_free_hook(_ptr_t ptr, const void *caller); static _ptr_t my_malloc_hook(size_t size, const void *caller); static _ptr_t my_realloc_hook(_ptr_t ptr, size_t size, const void *caller); static void my_init_hook(void); //malloc will call this function, once when implement of malloc func void (*__malloc_initialize_hook)(void) = my_init_hook; static void (my_init_hook)(void) { tr_old_free_hook = __free_hook; __free_hook = my_free_hook; tr_old_malloc_hook = __malloc_hook; __malloc_hook = my_malloc_hook; tr_old_realloc_hook = __realloc_hook; __realloc_hook = my_realloc_hook; printf("in func %s/n", __func__); }; static void (my_free_hook)(_ptr_t ptr, const void *caller) { __free_hook = tr_old_free_hook; printf("free the %p/n", ptr); free(ptr); __free_hook = my_free_hook; } static _ptr_t my_malloc_hook(size_t size, const void *caller) { _ptr_t hdr = NULL; __malloc_hook = tr_old_malloc_hook; hdr = malloc(size); printf("malloc size %d %p/n", size, hdr); __malloc_hook = my_malloc_hook; return hdr; } static _ptr_t my_realloc_hook(_ptr_t ptr, size_t size, const void *caller) { _ptr_t hdr = NULL; __realloc_hook = tr_old_realloc_hook; hdr = realloc(ptr, size); printf("realloc <%p> %p,%d/n", ptr, hdr, size); __realloc_hook = my_realloc_hook; return hdr; } int main(int argc, const char *argv[]) { int i = 0; _ptr_t ptr = NULL; for(i = 0; i < 10; i++) { ptr = malloc(i); free(ptr); } return 0; }
相关文章推荐
- Study from open source code -- brk() & sbrk() (2)
- Smart Client Case Study Source Code Download from MSDN China
- Build OpenSolaris from source code
- Smart Client Case Study Source Code Download from MSDN China
- Smart Client Case Study Source Code Download from MSDN China
- Smart Client Case Study Source Code Download from MSDN China
- Learning English From Android Source Code:1
- Install CyanogenMod from source code for ZTE V880
- trace openjdk from systemtap
- C# 二维码的生成 Open Source QRCode Library
- wierd issue - just marked it when I am free to trace the source code
- VS 下 Open Cascade Source Code 编译及自定义工程设置()
- Decompiled .class file bytecode version:49.0(java5.0) load or open source file.
- The right method to compile android SDK from source code
- /bin/sh: autoreconf: command not found (centos 6.5 install qemu from source code)
- How to get source code from APK
- vs2005_the breakpoint will not currently be hit. The source code is different from the original verison.
- The Ultimate List of Open Source Static Code Analysis Security Tools
- Generate C interface from C++ source code using Clang libtooling
- openBMC source code