dot

packages and services management
Log | Files | Refs | README

commit 532437baf529bda3a21b422fbefaf7018f70fccd
parent 17ee0dca1540d93aa3db989dbe44bf6f65e57007
Author: Josuah Demangeon <mail@josuah.net>
Date:   Sat, 15 Dec 2018 14:30:34 +0100

bin/{pt,ptree}: make ptree not use any exec, so that it works on saved output too

Diffstat:
Abin/pt | 5+++++
Mbin/ptree | 109++++++++++++++++++++++++++++++++++++-------------------------------------------
2 files changed, 54 insertions(+), 60 deletions(-)

diff --git a/bin/pt b/bin/pt @@ -0,0 +1,5 @@ +#!/usr/bin/env execlineb + +pipeline { ps ax -o ppid,user,pid,stat,comm,args } +pipeline { ptree } +less -S diff --git a/bin/ptree b/bin/ptree @@ -1,7 +1,38 @@ #!/usr/bin/awk -f +# Use ps(1) to generate a list of pid, ppid and other properties with +# the command name, displayed as a tree built from the pid-ppid pairs: +# +# USER TT NI PID STAT COMMAND +# root ? 0 1 Ss runit +# josuah ? 0 22437 S ├─ startx +# josuah ? 0 22451 S │ └─ xinit +# root tty7 0 22452 Rsl+ │ ├─ Xorg +# josuah ? 0 22457 S │ └─ dwm +# josuah ? 0 24882 S └─ runsvdir +# josuah ? 0 24884 S ├─ runsv +# josuah ? 0 24887 S │ ├─ svlogd +# josuah ? 0 24890 S │ └─ ratox +# josuah ? 0 24885 S └─ runsv +# josuah ? 0 24405 S ├─ tor +# josuah ? 0 24889 S └─ svlogd + +BEGIN { + LINE = "│ "; + NODE = "├─ "; + TAIL = "└─ "; + VOID = " "; + + if (ARGC == 1) PID = 1; else PID = ARGV[1]; +} + # pstree implementation in awk +NR == 1 { + sub(" *[^ ]+", ""); + print $0; +} + # Build a relational database in <entries> from the output of ps: The # parent pid (ppid) -> pid pairs are used to accumulate a list of child # pid (serialized into a csv: ",234,532,454") later used for building @@ -9,32 +40,17 @@ # # For each pid, "info" and "comm" are saved as well. -function list(entries) -{ - opt = "-o ppid,user,pid,stat,comm,args" - cmd = "exec ps -ax " opt " 2>/dev/null"; - if (!(cmd | getline)) { - cmd = "exec ps " opt - cmd | getline; - } +NR > 1 { + ppid = $1; pid = $3; + entries[ppid"cpid"] = entries[ppid"cpid"] "," pid; sub(" *[^ ]+", ""); - print $0; - - for (num = 0; cmd | getline; num++) { - ppid = $1; pid = $3; - entries[ppid"cpid"] = entries[ppid"cpid"] "," pid; - sub(" *[^ ]+", ""); - sub(" *[^ ]+ + *[^ ]+ + *[^ ]+ +", "&\t"); - sub("[^\t]+ [^ ]+ +", "&\t"); - split($0, info, "\t"); - sub(" *$" , "", info[2]); - entries[pid"info"] = info[1]; - entries[pid"comm"] = info[2]; - entries[pid"args"] = info[3]; - } - close(cmd); - - return num - 1; + sub(" *[^ ]+ + *[^ ]+ + *[^ ]+ +", "&\t"); + sub("[^\t]+ [^ ]+ +", "&\t"); + split($0, info, "\t"); + sub(" *$" , "", info[2]); + entries[pid"info"] = info[1]; + entries[pid"comm"] = info[2]; + entries[pid"args"] = info[3]; } # Using the informations from the child pid in entries, build the absolute @@ -68,15 +84,17 @@ function fill(entries, pid, lvl) } } -# Transform entries into a tree by replacing some LINE by VOID when needed. -# The tree is walked from the bottom to the top, and column by column -# toward the right until an empty column is met. +END { + NUM = 0; + fill(entries, PID, 0); + + # Transform entries into a tree by replacing some LINE by VOID when needed. + # The tree is walked from the bottom to the top, and column by column + # toward the right until an empty column is met. -function tree(entries, num) -{ for (j = 0; !stop; j++) { stop = tail = 1; - for (i = num; i > 0; i--) { + for (i = NR; i > 0; i--) { if (entries[i":"j] == LINE && tail) { entries[i":"j] = VOID; stop = 0; @@ -88,35 +106,6 @@ function tree(entries, num) } } } -} - -# Use ps(1) to generate a list of pid, ppid and other properties with -# the command name, displayed as a tree built from the pid-ppid pairs: -# -# USER TT NI PID STAT COMMAND -# root ? 0 1 Ss runit -# josuah ? 0 22437 S ├─ startx -# josuah ? 0 22451 S │ └─ xinit -# root tty7 0 22452 Rsl+ │ ├─ Xorg -# josuah ? 0 22457 S │ └─ dwm -# josuah ? 0 24882 S └─ runsvdir -# josuah ? 0 24884 S ├─ runsv -# josuah ? 0 24887 S │ ├─ svlogd -# josuah ? 0 24890 S │ └─ ratox -# josuah ? 0 24885 S └─ runsv -# josuah ? 0 24405 S ├─ tor -# josuah ? 0 24889 S └─ svlogd - -BEGIN { - LINE = "│ "; - NODE = "├─ "; - TAIL = "└─ "; - VOID = " "; - - list(entries); - if (ARGC == 1) PID = 1; else PID = ARGV[1]; - NUM = 0; fill(entries, PID, 0); - tree(entries, NUM); for (i = 0; i < NUM; i++) { printf("%s", entries[i":info"]);