home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / waiting < prev    next >
Encoding:
Korn shell script  |  1997-08-26  |  4.8 KB  |  192 lines

  1. #!/bin/ksh
  2. # @(#) waiting.ksh 2.0 95/12/24
  3. # Under XENIX, the wait channel address printed by ps is assumed to be in 
  4. # segment 18h (data) unless it is 6000000h, in which case it is assumed to 
  5. # be in segment 0.
  6. # 91/11/04 john h. dubois iii (johnd@armory.com)
  7. # 92/02/16 added help
  8. # 92/02/17 added PATH setting to get real awk
  9. #          added code to make it also work under UNIX
  10. # 93/06/29 belal -- added "-" to adb call, to ignore stray core files
  11. # 95/12/24 Ported to 5.0.  Less dependence on ps output format.
  12.  
  13. # This could be written to be much faster on UNIX systems by using crash,
  14. # but then unprivileged users wouln't be able to run it.
  15.  
  16. name=${0##*/}
  17.  
  18. if [ "$1" = -h ]; then
  19.     echo \
  20. "$name: show what processes are waiting for.
  21. Usage: $name [-h] [ps-options]
  22. $name prints one line for each process that is waiting,
  23. listing the uid, pid, ppid, tty, wait channel, and command.
  24. The wait channel is printed as a kernel symbol name and offset.
  25. If the offset is 0 it is not printed."
  26.     exit 0
  27. fi
  28.  
  29. Debug=0
  30. if [ "$1" = -x ]; then
  31.     Debug=1
  32.     shift
  33. fi
  34.  
  35. kernels="/xenix /unix"
  36. unset kernel
  37.  
  38. for file in $kernels; do
  39.     if [ -f $file ]; then
  40.     if [ -r $file ]; then
  41.         kernel=$file
  42.     else
  43.         print -u2 "Cannot open $file."
  44.         exit 1
  45.     fi
  46.     break
  47.     fi
  48. done
  49.  
  50. if [ -n "$kernel" ]; then
  51.     adb $file - |&
  52. else
  53.     print -u2 "Can't find a kernel."
  54.     exit 1
  55. fi
  56.  
  57. # To make sure we get the real awk
  58. PATH=/bin:/usr/bin:$PATH
  59. ttyAdj=0
  60. [[ "$(uname -X)" = *"Release = 3.2v4"* ]] && ttyAdj=1
  61.  
  62. awk -v "args=$*" -v Debug=$Debug -v ttyAdj=$ttyAdj '
  63. BEGIN {
  64.     if (args == "")
  65.     args = "-e"
  66.     Cmd = "ps -lf " args " < /dev/null"
  67.     Cmd | getline Header
  68.     if (Debug)
  69.     printf "Header line:\n%s\n",Header
  70.     f = "CMD=0 COMMAND=0"
  71.     if (ttyAdj == 1)
  72.     f = "TTY=7 " f
  73.     FWidths = SetFields(Header,0,f, Fields,Gutter)
  74.     split("UID PID TTY TIME WCHAN CMD",Format)
  75.     split(FWidths,Widths)
  76.     if (Debug) {
  77.     printf "fieldwidths: %s\n",FWidths > "/dev/stderr"
  78.     for (Elem in Fields)
  79.         printf "Field %s is %s; width %d\n",Elem,Fields[Elem],
  80.         Widths[Elem] > "/dev/stderr"
  81.     }
  82.  
  83.     while ((Cmd | getline) == 1) {
  84.     FieldsByName(Fields,FieldVals,$0,Widths)
  85.     wchan = FieldVals["WCHAN"]
  86.     if (Debug)
  87.         printf "wchan: %s; uid: %s; pid: %s; tty: %s; time: %s; cmd: %s\n",
  88.         FieldVals["WCHAN"],FieldVals["UID"],FieldVals["PID"],
  89.         FieldVals["TTY"],FieldVals["TIME"],FieldVals["CMD"] > "/dev/stderr"
  90.     if (wchan !~ "[0-9a-z]")
  91.         continue
  92.     else {
  93.         gsub(" ","0",wchan)
  94.         if (xenix == 1) {
  95.         if (wchan == "06000000")
  96.             printf "0x0:0x%s =a\n",wchan
  97.         else
  98.             printf "0x18:0x%s =a\n",wchan
  99.         }
  100.         else {
  101.         printf "0x1f:0x%s =a\n",wchan
  102.         getline    sym # unix adb echoes back command
  103.         }
  104.         getline sym
  105.         if (sym ~ "^0x")
  106.         sym = wchan
  107.         sub(/^\* /,"",sym)
  108.         sub(/^_/,"",sym)
  109.         sub(/:$/,"",sym)
  110.     }
  111.     if (!xenix && (sym == "e0000000"))
  112.         sym = "<sleep>"
  113.     gsub(" ","",FieldVals["UID"])
  114.     gsub(" ","",FieldVals["PID"])
  115.     gsub(" ","",FieldVals["TTY"])
  116.     printf "%-8s %5s %-7s %-17s %s\n",FieldVals["UID"],FieldVals["PID"],
  117.     FieldVals["TTY"], sym,FieldVals["CMD"] | "cat 1>&2"
  118.     }
  119. }
  120.  
  121. function SetFields(Header,LeftAdj,AltAdjFields,Fields,Gutter,
  122. Offset,NFields,HFields,Pos,Alt,i,Name,Adj,Lengths,PreSpace,Columns,
  123. OtherField,FieldNum,Widths) {
  124.     Offset = 1-LeftAdj*2
  125.     NFields = split(Header,HFields)
  126.     Pos = 0
  127.     Assign(Alt,AltAdjFields," +","=")
  128.     for (i = 1; i <= NFields; i++) {
  129.     Name = HFields[i]
  130.     if (Name in Alt)
  131.         Adj[i] = !LeftAdj
  132.     else
  133.         Adj[i] = LeftAdj
  134.     Lengths[i] = length(Name)
  135.     PreSpace[i] = index(Header,Name) - 1
  136.     Header = substr(Header,Lengths[i] + PreSpace[i] + 1)
  137.     }
  138.     for (i = 1; i <= NFields; i++) {
  139.     Columns = PreSpace[i+Adj[i]] 
  140.     OtherField = i + Adj[i]*2-1
  141.     if (Adj[i] == Adj[OtherField])
  142.         Lengths[i] += Columns
  143.     else {
  144.         Name = HFields[i]
  145.         if (Name in Alt) {
  146.         if (Alt[Name] == 0)
  147.             Alt[Name] = 1000000
  148.         Lengths[OtherField] += Lengths[i] + Columns - Alt[Name]
  149.         Lengths[i] = Alt[Name]
  150.         }
  151.     }
  152.     }
  153.     FieldNum = 0
  154.     for (i = 1; i <= NFields; i++) {
  155.     Name = HFields[i]
  156.     Fields[++FieldNum] = Name
  157.     if (!Adj[i] && Adj[i+1]) {
  158.         Widths = Widths Lengths[i]+1 " "
  159.         Gutter[Name] = 1
  160.         if (PreSpace[i+1] > 1) {
  161.         Widths = Widths PreSpace[i+1]-1 " "
  162.         ++FieldNum
  163.         }
  164.     }
  165.     else
  166.         Widths = Widths Lengths[i] " "
  167.     }
  168.     return Widths
  169. }
  170.  
  171. function Assign(Arr,Elements,Sep,AssignOp,
  172. Num,Names,Elem,Assignments,Assignment) {
  173.     Num = split(Elements,Assignments,Sep)
  174.     for (; Num; Num--) {
  175.     Assignment = Assignments[Num]
  176.     Ind = index(Assignment,AssignOp)
  177.     Arr[substr(Assignment,1,Ind - 1)] = substr(Assignment,Ind + 1)
  178.     }
  179.     return Num
  180. }
  181.  
  182. function FieldsByName(FieldNames,FieldVals,Line,Widths,  i,Pos) {
  183.     Pos = 1
  184.     for (i = 1; i in Widths; i++) {
  185.     if (i in FieldNames)
  186.         FieldVals[FieldNames[i]] = substr(Line,Pos,Widths[i])
  187.     Pos += Widths[i]
  188.     }
  189. }
  190.  
  191. ' 3>&1 <&p >&p 2>&3
  192.