Re: [dev] pids for surf, webkit instances (+ lsw.c bug)

2017-09-07 Thread Greg Minshall
just for completeness, below is a bash script that uses lsw recursively
to walk the tree.  it's a bit overkill (feeling guilty for having missed
the hackathon?).  "once a Fortran programmer..."

#!/usr/bin/env bash

# this script walks the tree of known X windows on the system and
# prints out those that have PIDs (process ids; see ps(1)) associated
# with them.

# usage: "$0", or "$0 windowid ..."; in the former, the windows are
# traversed from the root window; in the latter, from the designated
# window id(s).

# the output is: pid windowid name [secondary]\n...

# program we use to list windowids
LSW=lsw

# recursive part of getwinids
function rgetwinids() {
local windowid=$1 a i
echo ${windowid}
a=$(${LSW} $windowid | awk '{print $1}')
for i in $a; do
rgetwinids $i
done
}

# output the windowids that seem to be on the system.
function getwinids() {
local arg
if [[ $# -eq 0 ]] ; then
echo $(rgetwinids);
else
for arg in $*; do echo $(rgetwinids ${arg}); done
fi
}

# do some processing on grep output
function gnhelper() {
grep -w "$1" |
head -1 |
tr -d '\n' |
awk '{$1=""; $2=""; print; exit}' |
sed 's/^[[:blank:]]*"//' |
sed 's/"$//';
 }

# now, get the name (and maybe surf(1) uri) of the window
function getname() {
local winid=$1 wim su
wim=$(xprop -id ${winid} | gnhelper ${pname});
if [[ ${args} == 0 ]]; then
   echo ${wim}
else
su=$(xprop -id ${winid} | gnhelper ${sname});
if [[ -z $su ]]; then
echo ${wim};
else
echo "${wim} [$su]"
fi
fi
}


# how many digits might be in a PID?  will give one greater (newline
# counted by wc(1))
function pidndigits() {
local i
echo $(
(for i in $(ps haxo pid); do
 echo $i |
 wc |
 awk '{print $3}';
 done) |
sort -nu |
tail -1
)
}

# get the process id of the process that "owns" a window
function getpid() {
xprop -id $1 | grep PID | awk '{print $3}';
}

# make sure ${LSW} is installed
function prereq() {
a=$(${LSW} $windowid);
if [[ $? -ge 126 ]] ; then
printf "required program ${LSW} not found; see 
https://suckless.org\n(or install package suckless-tools)\n" > /dev/stderr
exit 1;
fi
}


function help() {
echo "\

$(basename $0) lists all the windows open on the system, along with
their process IDs (pids), optionally ([--winid|-w]) the window ids, as
well as the name property of each window (\"WM_ICON_NAME\" is the
default property here), optionally ([-s]) a secondary property of the
window (\"_SURF_URI\" is the default) is listed after the primary
(enclosed in square brackets).

the width of the (combined) primary and secondary properties can be
set with [--cmdn|-c]; the width of the PID field set with [--pidn|-p].
"
echo
usage
}

function usage() {
if [[ ! -z $1 ]] ; then echo "invalid argument \"$1\"" > /dev/stderr; fi
echo "usage: $(basename $0) 
[-s]
[--winid|-w]
[--help|-h]
[[--cmdn|-c] NUM (${DEF_argnc})]
[[--pidn|-p] NUM (${DEF_argnp})]
[--pname STRING (\"${DEF_pname}\")]
[--sname STRING (\"${DEF_sname}\")]" > /dev/stderr
exit 1;
}


function parseargs() {
local optlist="-l cmdn:,help,pidn:,pname:,sname:,winid -o c:hp:sw" optresult

# 
http://bahmanm.com/blogs/command-line-options-how-to-parse-in-bash-using-getopt
# of help here
DEF_pname=WM_ICON_NAME
DEF_sname=_SURF_URI
DEF_argnc=200   # digits reserved for name field
DEF_argnp=$(pidndigits) # how many digits should we reserve for pid?
DEF_args=0
DEF_argwinid=0

cn=${DEF_argnc}
pn=${DEF_argnp}
argwinid=${DEF_pwinid}
pname=${DEF_pname}
sname=${DEF_sname}
args=${DEF_args}
optresult=$(getopt ${optlist} -- "$@")
if [[ $? != 0 ]]; then usage; fi
eval set -- ${optresult}
while true ; do
case "$1" in
-c|--cmdn) cn=$2; shift 2 ;;
-h|--help) help; shift ;;
-p|--pidn) pn=$2; shift 2 ;;
-s) args=1; shift ;;
-w|--winid) argwinid=true ; shift ;;
--pname) pname=$2; shift 2 ;;
--sname) sname=$2; shift 2 ;;
--) shift; desired=$*; break ;;
*) usage $1; ;; # NOTREACHED
esac
done
}

function main() {
local winid a b winidpart
parseargs $*
prereq
for winid in $(getwinids ${desired} | tr '[[:blank:]]' '\n' | sort -u | tr 
'\n' " "); do
a=$(getpid ${winid})
if [[ ! -z $a ]]; then
b=$(getname ${winid})  # get name string for this window
if [[ ${argwinid} ]]; then
winidpart=" ${winid} ";
else
winidpart=" ";
fi
printf "%${pn}d%s%-.${cn}s\n" $a "${winidpart}" "$b"
fi
done | sort -n -k1
}

main $*



Re: [dev] pids for surf, webkit instances (+ lsw.c bug)

2017-09-06 Thread Greg Minshall
s...@mailless.org wrote:

> or use lsw for a specific window:
> 
> lsw | grep "some webpage title" | cut -f1 -d' ' | xargs xprop -id | grep PID

ah, thanks.

btw, i ran into a bug in, i assume, lsw.

bash minshall-apollo: {1339} lsw 0x164
Segmentation fault (core dumped)

at the time, xwininfo around this window id was

bash minshall-apollo: {1350} xwininfo -root -children -tree | grep --text 
0x16
0x169 (has no name): ()  1x1+-1+-1  +-1+-1
0x161 "darktable": ("darktable" "Darktable")  10x10+10+10  +10+10
0x163 "darktable": ("darktable" "Darktable")  1918x1061+-3840+17  +-3840+17
   0x164 (has no name): ()  1x1+-1+-1  +-3840+17


anyway, here's a patch for lsw that seems to keep it from crashing.
(but, maybe one would want to report such a window somehow?)

diff --git a/lsw.c b/lsw.c
index fc40fef..b3f473b 100644
--- a/lsw.c
+++ b/lsw.c
@@ -36,7 +36,8 @@ lsw(Window win) {
Window *wins, *w, dw;
XWindowAttributes wa;
 
-   if(!XQueryTree(dpy, win, &dw, &dw, &wins, &n))
+   if((!XQueryTree(dpy, win, &dw, &dw, &wins, &n)) ||
+   (n == 0))
return;
for(w = &wins[n-1]; w >= &wins[0]; w--)
if(XGetWindowAttributes(dpy, *w, &wa)

(please let me know if some other way of contributing patches is more
happiness-inducing.)

cheers, Greg



Re: [dev] pids for surf, webkit instances

2017-09-06 Thread ssd
or use lsw for a specific window:

lsw | grep "some webpage title" | cut -f1 -d' ' | xargs xprop -id | grep PID



Re: [dev] pids for surf, webkit instances

2017-09-05 Thread Greg Minshall
S. Gilles,

> xprop | grep PID

yes, nice.  i hadn't known.  this

for i in $(xwininfo -children | tr '[:blank:]' \\n | grep '^0x'); do
xprop -id $i | grep PID;
done

i guess, gives me PIDs for all the windows in a tabbed set.

thanks!  Greg



Re: [dev] pids for surf, webkit instances

2017-09-05 Thread S. Gilles
On 2017-09-06T07:14:21+0300, Greg Minshall wrote:
> hello.
> 
> every now and then some browser instance goes resource wild.  top(1) or
> ps(1) show the PID consuming all the resources (normally a webkit
> instance), but i haven't figured out a way to go from that to knowing
> which instance to kill off, given that most often i start surf from
> dwm(1), which starts surf on a generic search engine page, and then i go
> from there.  is there an obvious method for correlating a PID to an
> actual instance?

xprop | grep PID

?

-- 
S. Gilles



[dev] pids for surf, webkit instances

2017-09-05 Thread Greg Minshall
hello.

every now and then some browser instance goes resource wild.  top(1) or
ps(1) show the PID consuming all the resources (normally a webkit
instance), but i haven't figured out a way to go from that to knowing
which instance to kill off, given that most often i start surf from
dwm(1), which starts surf on a generic search engine page, and then i go
from there.  is there an obvious method for correlating a PID to an
actual instance?

if not, two questions/suggestions:

1.  would it make sense to have some key combination in surf(1) that
would show *its* PID (in the status bar or somewhere)?  (and, possibly
less palatable, maybe a key for tabbed(1) to list its cilents along with
their PIDs?)

2.  i'm using the new webkit2gtk surf, and i notice that the spawned
webkit processes don't have the surf instance as their parent; e.g., "ps
axo pid,ppid,command" shows:

32676 32672 tabbed -c -r2 surf -e x -z1.3 -a @a duckduckgo.com
32677 32676 surf -e 46137347 -z1.3 -a @a duckduckgo.com
32688 1 /usr/local/libexec/webkit2gtk-4.0/WebKitNetworkProcess 18
32691 1 /usr/local/libexec/webkit2gtk-4.0/WebKitWebProcess 21

rather, they are children of process 1.  does anyone know why that is?

cheers, Greg Minshall