shithub: riscv

ref: 51f76ae999813bdbf33b06bb4e6c6c8b00f4aec8
dir: /rc/bin/tap/

View raw version
#!/bin/rc
rfork e

if(~ $#* 0){
	echo 'Usage: ' $0 '[ pid ... ]' >[1=2]
	exit usage
}

f=/proc/*/fd

fn otherqid {
	x=`{echo $1 | sed 's/[12]$//'}
	switch($1){
	case $x^1
		echo $x^2
	case $x^2
		echo $x^1
	}
}

fn traceqid {
	while(! ~ $#* 0){
		echo $t '['$2']' $3 $1
		t=('	' $t)
		tracepid `{grep -n `{otherqid $1} $f >[2]/dev/null | sed 's!^/proc/([^/]+)/.*!\1!g'}
		t=$t(2-)
		shift
		shift
		shift
	}
}

fn tracepid {
	while(! ~ $#* 0){
		a=ap$1
		v=av$1
		if(~ $$v $1){
			echo $t $1 $$a '	...'
		}
		if not {
			$v=$1
			$a=`{cat /proc/$1/args >[2]/dev/null}
			if(~ $$a ''){
				s=`{cat /proc/$1/status >[2]/dev/null}
				$a=$s(1)
			}
			echo $t $1 $$a
			t=('	' $t)
			traceqid `{awk '/\|/{q=substr($5,2);print q" "$1" "$10}' /proc/$1/fd >[2]/dev/null}
			t=$t(2-)
		}
		shift
	}
}

tracepid $*