- 论坛徽章:
- 0
|
我想改写malloc,重新定义一个malloc hook函数,在该函数中,将调用malloc的函数打印出来,代码如下:- static void *my_malloc_hook(size_t size, const void *caller)
- {
- void *array[10];
- size_t size1 = 0;
- void *result;
- __malloc_hook = old_malloc_hook;
- result = malloc(size);
- old_malloc_hook = __malloc_hook;
- __malloc_hook = my_malloc_hook;
- size1 = backtrace(array, 5);
- switch(size1)
- {
- case 1:
- printf("@@@ %p + %p 0x%x %p\n",
- caller, result, (unsigned long int)size, array[0]);
- break;
- case 2:
- printf("@@@ %p + %p 0x%x %p %p\n",
- caller, result, (unsigned long int)size, array[0], array[1]);
- break;
- case 3:
- printf("@@@ %p + %p 0x%x %p %p %p\n",
- caller, result, (unsigned long int)size, array[0], array[1], array[2]);
- break;
- case 4:
- printf("@@@ %p + %p 0x%x %p %p %p %p\n",
- caller, result, (unsigned long int)size, array[0], array[1], array[2],
- array[3]);
- break;
- case 5:
- printf("@@@ %p + %p 0x%x %p %p %p %p %p\n",
- caller, result, (unsigned long int)size, array[0], array[1], array[2],
- array[3], array[4]);
- break;
- default:
- break;
- }
- return result;
- }
复制代码 修改成这样之后,程序中一调用malloc就会segment fault,去掉backtrace就没问题。
有大侠知道这是怎么回事吗?难道在malloc hook中不能调用backtrace??? |
|