%%Title: ccmallocを使ってみる

%%Created: Fri Aug 27 11:51:48 JST 2004
%%Updated:

o インストール

	- コンパイルは gmake(1)を使う

	# gmake install
	./util/install bin/ccmalloc /usr/local/bin
	./util/install lib/libccmalloc.a /usr/local/lib
	./util/install ccmalloc.cfg /usr/local/share/ccmalloc
	./util/install obj/ccmalloc-g++.o /usr/local/lib
	./util/install obj/ccmalloc-gcc.o /usr/local/lib
	./util/install obj/ccmalloc-CC.o /usr/local/lib

o .ccmalloc
	ccmallocの環境設定ファイル
	実行ディレクトリ→ホームディレクトリの順で検索される
	なくても初期設定で動く

file FILE
	実行ファイル名を指定する。例えば a.outならば
		file a.out
	と書いとく。
	指定しないと実行ディレクトリの実行ビットが立ったファイルを適当に選ぶ。

set continue 1
	ccmalloc内の様々なチェックで abortしなくなる。
		- malloc(0)
		- malloc()してないアドレスをfree()した
		- 2回フリーした
	等があるり(他にもあるかも)、設定すると
		*** malloc(0)
	等とメッセージを出力するだけになる。

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 'check-overwrites' detect overwrites [0]
% ----------------------------------------------------------------------
% If you want to detect 'off by n bytes' errors you should set
% 'checking-overwrites' to n/4 (on 32-Bit machines).
%
% ccmalloc inserts a boundary above allocated data. This boundary
% consists of 'check-overwrites' words. If your program writes to
% this area then ccmalloc can detect this (see also check-start
% and check-interval). 'ccmalloc' also does checking for overwrites
% at non word boundaries (e.g. strcpy(malloc(strlen("hello")),"hello");)

set check-overwrites 1

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 'check-interval' can be used to speed up checks [0]
% ----------------------------------------------------------------------
% If check-overwrite, check-underwrites or check-free-space is set then
% the default is to do 'write checks' when data is deallocated and
% to do 'free space checks' when reporting together with
% 'write checks' for garbage. When you want these checks to be
% performed more often then you should set 'check-interval' to a
% positive number. This number is the interval between the number of
% calls to free or malloc without performing the checks.

set check-interval 1

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 'check-start' can be used to speed up checks [0]
% ----------------------------------------------------------------------
% The flag 'check-start' delays the start of checks until the given
% number of calls to free and malloc have occured. Together with
% 'check-interval' you can use a binary search to find an aproximation
% when a corruption occured! If you simply set check-interval to 1 and
% check-start to 0 then this will slow done your program too much.

set check-start 0

set check-underwrites 1

log FILE
	ccmalloc.logにしたいならば
		log ccmalloc.log
	と書く

o 試しに使ってみる
	- 

	#include 
	#include 

	int
	main()
	{
		char *x, *y = malloc(1);

		x = y;

		exit(0);
	}

	% cc -Werror -Wall -g -c y.c ; cc -o y y.o /var/tmp/libccmalloc.a ; ./y
	file-name=a.out does not contain valid symbols
	trying to find executable in current directory ...
	(to speed up this search specify `file y'
	 in the startup file `.ccmalloc (but not found)')
	.--------------------------------------------------------------------------.
	|================ ccmalloc-0.4.0 (C) 1997-2003 Armin Biere ================|
	+--------------------------------------------------------------------------+
	| executable       = y                                                     |
	| startup file     = .ccmalloc (but not found)                             |
	| log file         = stderr                                                |
	| start time       = Fri Aug 27 12:33:54 2004                              |
	| operating system = FreeBSD 4.10-RELEASE i386 on shoichi.tanu.org         |
	+--------------------------------------------------------------------------+
	| only-count        = 0            keep-deallocated-data = 0               |
	| check-interval    = 0            check-free-space      = 0               |
	| check-start       = 0            file-info             = 1               |
	| chain-length      = 0            additional-line       = 1               |
	| check-underwrites = 0            print-addresses       = 0               |
	| check-overwrites  = 0            print-on-one-line     = 0               |
	| sort-by-wasted    = 1            sort-by-size          = 1               |
	| # only-log-chain  = 0            continue              = 0               |
	| # dont-log-chain  = 0            statistics            = 0               |
	| debug             = 0            library-chains        = 0               |
	| load-dynlibs      = 0            align-8-byte          = 0               |
	| only-wasting-alloc= 1                                                    |
	`--------------------------------------------------------------------------'

	.---------------.
	|ccmalloc report|
	=======================================================
	| total # of|   allocated | deallocated |     garbage |
	+-----------+-------------+-------------+-------------+
	|      bytes|           1 |           0 |           1 |
	+-----------+-------------+-------------+-------------+
	|allocations|           1 |           0 |           1 |
	+-----------------------------------------------------+
	| number of checks: 1                                 |
	| number of counts: 1                                 |
	| retrieving function names for addresses ... done.   |
	| reading file info from gdb ... done.                |
	| sorting by number of not reclaimed bytes ... done.  |
	| number of call chains: 1                            |
	| number of ignored call chains: 0                    |
	| number of reported call chains: 1                   |
	| number of internal call chains: 1                   |
	| number of library call chains: 0                    |
	=======================================================
	|
	*100.0% = 1 Bytes of garbage allocated in 1 allocation
	|       |
	|       |       0x08048e3c in 
| | at y.c:7 | | | `-----> 0x08049182 in | at src/wrapper.c:318 | `------------------------------------------------------ o tips - malloc()を使ってないと統計が出ない。 下の様なコードをどっかにこっそり置いとく。 char *dummy_to_use_malloc = malloc(1); free(dummy_to_use_malloc); - printf()がgarbage allocatedになる場合がある