#!/usr/pkg/bin/runawk

# Copyright (c) 2010-2011 Aleksey Cheusov <vle@gmx.net>
# 
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#use "power_getopt.awk"
#use "xgetline.awk"
#use "trim.awk"
#use "has_prefix.awk"

#interp-var "LMDBG_MODULES_AWK"

#.begin-str help
# lmdbg-module - detects module by analysing stacktraces with a help of rules
#                specified by user.
# usage: lmdbg-module [OPTIONS] [files...]
# OPTIONS:
#   -h                  display this screen
#   -V                  display version
#   =c <config_file>    configuration file
#   -s                  calculate and output sum of counters
# Option -c is mandatory.
#
# Config file format:
#   [modulename1]
#     <op1.1> <field1.1> <string1.1> [+]
#     <op1.2> <field1.2> <string1.2> [+]
#     ...
#   [modulename2]
#     <op2.1> <field2.1> <string2.1> [+]
#     ...
#  where <op> is either "substr" or "match",
#       <field> is either "funcname" or "source" and
#       <string> is either string (op = "string")
#                or regular expression (op = "match").
# Example of configuration file:
#   [wbd]
#      match       funcname    ^ENWBD +
#   [lemmatizer]
#      substring   source      ENLMZ  +
#   [prosao1]
#      substring   source      ENLingui
#
#.end-str help

BEGIN {
	# processing arguments
	if (getarg("V")){
		print "lmdbg-modules 1.3.0"
		exitnow(0)
	}
	if (getarg("h")){
		print_help()
		exitnow(0)
	}

	sum = getarg("s")

	config_file = getarg("c")
	if (!config_file){
		print_help()
		exitnow(1)
	}

	# reading a config file
	rules_match_count  = 0
	rules_substr_count = 0
	while (xgetline0(config_file)){
		$0=trim_lrc($0)

		if (NF == 0){
		}else if ($1 ~ /^\[.*\]$/){
			module = substr($1, 2, length($1)-2)
			op     = "+"
		}else if ($1 == "substr"){
			++rules_substr_count
			rules_substr_field [rules_substr_count] = $2
			rules_substr_str [rules_substr_count] = $3
			rules_substr_module [rules_substr_count] = module
			rules_substr_op [rules_substr_count] = $4
		}else if ($1 == "match"){
			++rules_match_count
			rules_match_field [rules_match_count] = $2
			rules_match_str [rules_match_count] = $3
			rules_match_module [rules_match_count] = module
			rules_match_op [rules_match_count] = $4
		}else{
			abort("bad line in config file")
		}
	}

	# initialization
	fields [1] = "peak:"
	fields [2] = "max:"
	fields [3] = "allocs:"
	fields [4] = "leaks:"
	fields [5] = "modules:"

	module = ""
	op     = "+"
}

function printout (                i){
	if (!cnt)
		return

	if (module && op != "i")
		printf "%s module: %s\n", lines [1], module
	else
		print lines [1]

	for (i=2; i <= cnt; ++i){
		print lines [i]
	}
}

function update_summary (            i){
#	print module
	modules [module] = 1
	if ("allocs:" in cnts)
		global_cnts [module, "allocs:"] += cnts ["allocs:"]
	if ("leaks:" in cnts)
		global_cnts [module, "leaks:"] += cnts ["leaks:"]

	if ("peak:" in cnts)
		if (cnts ["peak:"] > global_cnts [module, "peak:"])
			global_cnts [module, "peak:"] = cnts ["peak:"]
	if ("max:" in cnts)
		if (cnts ["max:"] > global_cnts [module, "max:"])
			global_cnts [module, "max:"] = cnts ["max:"]
}

function process_stacktrace (){
	if (sum){
		update_summary()
	}else{
		printout()
	}

	# cleanup
	delete cnts
	cnt = 0
	module = ""
	op     = "+"
}

/^(stacktrace|malloc|calloc|realloc|(posix_)?memalign|aligned_alloc|free) / {
	process_stacktrace()

	lines [++cnt] = $0
	for (i=2; i <= NF; ++i){
		if ($i ~ /:$/){
			cnts [$i] = $(i+1)
		}
	}

	if ("module:" in cnts){
		module = cnts ["module:"]
		op = "i" # do not add yet another module: <module>
	}

	next
}

/^[^ ]/ {
	print
	next
}

#module != "" && op == "" {
#	if (!sum)
#		print

#	next
#}

function get_f_and_s (){
	if (field == "funcname")
		str0 = arr [3]
	else if (field == "source")
		str0 = arr [2]
	else
		abort("this should not happen")
}

{
#	print "$0:", $0

	lines [++cnt] = $0
	if (op == "+"){
		split($0, arr, /\t/)

		modules_addon = ""

		for (i=1; i <= rules_substr_count; ++i){
			field = rules_substr_field [i]
			str = rules_substr_str [i]

			get_f_and_s()

			if (index(str0, str) > 0){
				modules_addon = rules_substr_module [i]

				if (module == modules_addon || has_prefix(module, modules_addon ":")){
				}else if (module){
					module = modules_addon ":" module
				}else{
					module = modules_addon
				}

				op     = rules_substr_op [i]
				break
			}
		}

		for (i=1; modules_addon == "" && i <= rules_match_count; ++i){
			field = rules_match_field [i]
			str = rules_match_str [i]

			get_f_and_s()

			if (match(str0, str) > 0){
				modules_addon = rules_match_module [i]

				if (module == modules_addon || has_prefix(module, modules_addon ":")){
				}else if (module){
					module = modules_addon ":" module
				}else{
					module = modules_addon
				}

				op     = rules_match_op [i]
				break
			}
		}
	}
}

function printout_global_summary (         i,f,m)
{
	for (m in modules){
		printed = 0
		for (i=1; i in fields; ++i){
			f = fields [i]
			if ((m SUBSEP f) in global_cnts) {
				if (!printed){
					printf "info modulestat"
					printed = 1
				}
				printf " %s %s", f, global_cnts [m, f]
			}
		}
		if (printed){
			if (m)
				print " module:", m
			else
				print ""
		}
	}
}

END {
	process_stacktrace()

	if (sum)
		printout_global_summary()
}
