def main(stack_file, root_dir, name):
    with open(stack_file) as fd:
        stacks = json.load(fd)

        # collect modules and set color per modules
    modules_colors = {}
    for stack in stacks:
        for entry in stack:
            mod = entry[1]
            if mod in modules_colors:
                continue
            modules_colors[mod] = graphcolors.get()

            # generate dot files
    dst_dir = os.path.join(root_dir, name)
    if os.path.exists(dst_dir) == False:
        os.path.mkdir(dst_dir)
    dot_files = []
    no = 0
    for stack in stacks:
        dst_file = os.path.join(dst_dir, str(no)) + ".dot"
        with open(dst_file, "wb") as fd:
            fd.write(b"digraph callstack {")
            fd.write(b"graph [rankdir = LR];")

            # define node color
            nodes = []
            for node in stack:
                node_str = conv_str(node)

                if node_str in nodes:
                    continue
                nodes.append(node_str)

                fd.write('"{0}" [ color = "{1}" ]'.format(node_str, modules_colors[node[1]]).encode())

                # write node direction
            for pair in read_pairs(stack):
                (caller, callee) = pair
                k = (caller[0], callee[0])
                fd.write('"{0}" -> "{1}"'.format(conv_str(caller), conv_str(callee)).encode())
            fd.write("}".encode())
        no += 1
        dot_files.append(dst_file)

        # generate images
    img_files = []
    for f in dot_files:
        img_file = f.replace("dot", "gif")
        os.system("dot -Tgif -o {0} {1}".format(img_file, f))
        img_files.append(img_file)

        # generate html file
    html_file = os.path.join(dst_dir, "index.html")
    with open(html_file, "wb") as fd:
        fd.write(b"<html><body>")
        for f in img_files:
            fd.write("<img src='{0}' />".format(f).encode())
            fd.write(b"<hr>")
        fd.write(b"</body></html>")
def main( stack_file ):
	with open( stack_file ) as fd:
		stacks = json.load( fd );

	# collect call pair count
	call_count = {};
	for stack in stacks:
		for pair in read_pairs( stack ):
			k = ( pair[0][0], pair[1][0] ); # caller and callee addr
			if k in call_count:
				call_count[ k ] += 1;
			else:
				call_count[ k ] = 1;

	# collect modules and set color per modules
	modules_colors = {};
	for stack in stacks:
		for entry in stack:
			mod = entry[1];
			if mod in modules_colors:
				continue;
			modules_colors[ mod ] = graphcolors.get();
	
	# print out
	print( "digraph callstack {" );
	nodes = [];
	for stack in stacks:
		for node in stack:
			node_str = conv_str( node );
			if node_str in nodes:
				continue;
			nodes.append( node_str );
			print(
				"\"{0}\" [ color = \"{1}\" ]".format(
					node_str,
					modules_colors[ node[1] ] ) );
	
	pairs = [];
	for stack in stacks:
		for pair in read_pairs( stack ):
			( caller, callee ) = pair;
			k = ( caller[0], callee[0] );
			
			if k in pairs:
				continue;
			pairs.append( k );
			
			print( "\"{0}\" -> \"{1}\" [label=\"{2}\"]".format(
					conv_str( caller ),
					conv_str( callee ),
					call_count[ k ],
					) );
	print( "}" );