看板 LinuxDev 關於我們 聯絡資訊
#include <stdio.h> #include <string.h> #include <stddef.h> #include <unistd.h> #include <dlfcn.h> void* malloc(size_t size) { char buf[32]; static void* (*real_malloc)(size_t) = NULL; printf("ggggggggggg"); if (real_malloc == NULL) { *(void**)(&real_malloc) = dlsym(RTLD_NEXT, "malloc"); } sprintf(buf, "malloc called, size = %zu\n", size); write(2, buf, strlen(buf)); return real_malloc(size); } void free(void* ptr) { char buf[32]; static void* (*real_free)(void* ptr) = NULL; if (real_free == NULL) { *(void**)(&real_free) = dlsym(RTLD_NEXT, "free"); } sprintf(buf, "free called, ptr address = %p\n", ptr); write(2, buf, strlen(buf)); real_free(ptr); } gcc -D_GNU_SOURCE -shared -fPIC -o libmcount.so malloc_count.c -ldl LD_PRELOAD=./libmcount.so ls 那悶在malloc函數裡面 p rintf 函數會造成 crash ? 看網路上dlsym hook一些例子只用 write 而不用 printf --------------------------------------------------------------------- 我又測試一版本是 hook strlen 在裡面用 printf 沒問題 https://gist.github.com/anonymous/348a8102ddccb295b6ef573c68955748 編譯指令: gcc -shared -fPIC hook.c -o hook.so -ldl LD_PRELOAD=$PWD/hook.so ./test 這樣是 printf 跟 malloc 之間有什麼關係嘛? printf 底層是會call到 malloc 所以造成 crash? 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 223.136.83.2 ※ 文章網址: https://www.ptt.cc/bbs/LinuxDev/M.1499759097.A.3B9.html ※ 編輯: gigigigi (123.204.140.54), 07/12/2017 00:26:21
Bencrie: 你先把 sprintf 改成 snprintf 再說 XD 07/12 01:13
gigigigi: coredump gdb ls core 結果看起來是死在printf 裡面 07/12 14:16
gigigigi: http://0rz.tw/t54mf 07/12 14:26
Bencrie: 還是掛個 strace / ltrace 跑看看堆疊是不是壞了 07/13 00:26
CP64: 這 trace 結果 一臉堆疊過深的樣子 07/13 01:14
x000032001: printf 又呼叫malloc 造成無限循環 07/13 07:38
gigigigi: 目前結論也是遞迴無限循環造成! 07/13 16:47