#!/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"

#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.3.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|aligned_alloc|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 
}
