#!/bin/sh
#
# prints commands to flag the process sections into radare
#
# pancake <youterm.com>

P=$1
[ -z "$P" ] && \
  echo "Usage: proc-map [pid]" && exit

parse() {
	b=0 # old value for a
	inc=0

	while : ; do
	read A
	[ -z "$A" ] && break
	i=0
	for a in $A ; do
		i=$(($i+1))
		[ $i = 1 ] && addr=`echo $a|sed -e s/-/,/ |cut -d , -f 1`
		if [ $i = 6 ]; then
			if [ $a = $b ]; then
				let inc=$(($inc+1))
			else
				b="$a"
				inc=0
			fi
			a=`basename $a`
			echo "f section_${a}_${inc} @ 0x$addr" | sed -e s,/,_,g -e s,-,_,g
		fi
	done
	done
}

if [ -e "/proc/$P/maps" ]; then
	column -t /proc/$P/maps | parse
fi
