NULLに文字列を書き込みcore dumpをはかせてそのログを解析する。
#include <stdio.h>
#include <string.h>
int
main()
{
char *ptr=NULL;
strcpy(ptr, "hello");
puts(ptr);
return(0);
}
$ gcc -o hello hello.c -g $ ulimit -c # core fileのサイズを確認 0 # おそらくlinuxの場合何もしていないと0が表示される $ ulimit -c unlimited # サイズをunlimited変更 $ ./hello # Segmentation faultで落ちる Segmentation fault (core dumped) $ ls # coreというfileができている core hello hello.c $ gdb hello core # core fileを解析開始 GNU gdb 6.6-debian Copyright (C) 2006 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i486-linux-gnu"... Using host libthread_db library "/lib/tls/i686/cmov/libthread_db.so.1". warning: Can't read pathname for load map: Input/output error. Reading symbols from /lib/tls/i686/cmov/libc.so.6...done. Loaded symbols for /lib/tls/i686/cmov/libc.so.6 Reading symbols from /lib/ld-linux.so.2...done. Loaded symbols for /lib/ld-linux.so.2 Core was generated by `./hello'. Program terminated with signal 11, Segmentation fault. #0 0x0804838f in main () at hello.c:9 9 strcpy(ptr, "hello"); (gdb) print ptr $1 = 0x0 (gdb) quit
以上からstrcpyでNULLアドレスにwriteしようとしたため落ちたことがわかった。