#!/usr/bin/env runawk

#use "power_getopt.awk"

#env "LC_ALL=C"

#interp-var "LMDBG_STRIP_AWK"

#.begin-str help
# lmdbg-strip - strip stacktraces given on input
# usage: lmdbg-strip [OPTIONS] [files...]
# OPTIONS:
#   -h      display this screen
#   -V      display version
#   -a      remove stacktrace addresses
#   -r      replace return and input addresses with fake value
#   -l      remove line number from sourcefile.c:<linenumber>
#   -s      remove source, i.e. sourcefile.c:<linenumber>
#   -n      remove "num: <number>" from input
#.end-str help

BEGIN {
	# processing options
	if (getarg("V")){
		print "lmdbg-strip 1.1.0"
		exitnow(0)
	}
	if (getarg("h")){
		print_help()
		exitnow(0)
	}

	mode_a = getarg("a")
	mode_r = getarg("r")
	mode_l = getarg("l")
	mode_s = getarg("s")
	mode_n = getarg("n")

	if (!mode_a && !mode_l && !mode_r && !mode_s && !mode_n){
		print "At least of the following options must be specified: -a, -l, -r, -n or -s" > "/dev/stderr"
		exitnow(1)
	}

	# initializing
	FS = OFS = "\t"
}

mode_a && /^ / {
	$1 = " "
}

mode_r && /^[^ ]/ {
	if (/^(malloc|calloc|memalign|posix_memalign|free) /){
		sub(/0x[0-9A-Fa-f]+/, "0xXYZ")
	}else if (/^realloc /){
		sub(/0x[0-9A-Fa-f]+/, "0xXYZ")
		sub(/0x[0-9A-Fa-f]+/, "0xXYZ")
	}
}

mode_l && NF >= 2 {
	sub(/:[^: \t]+$/, "", $2)
}

mode_s && /^ / {
	$2 = ""
}

mode_n && /^[^ ]/ {
	sub(/ +num: [0-9]+/, " ")
}

{
	if ($0 != " " && $0 != " \t" && $0 != " \t\t")
		print 
}
