Process Info on Linux

#!/usr/bin/tclsh
# cprt: C2009 Uwe Klein Habertwedt
# what: present the numbers from linux /proc/pid/stat

 # $statinfo content is ripped directly from "man 5 proc"
 set statinfo {
 pid %d
        The process id.

 comm %s
        The filename of the executable, in parentheses.  This  is
        visible whether or not the executable is swapped out.

 state %c
        One  character  from  the string "RSDZTW" where R is run‐
        ning, S is sleeping in an interruptible wait, D is  wait‐
        ing  in  uninterruptible  disk  sleep,  Z is zombie, T is
        traced or stopped (on a signal), and W is paging.

 ppid %d
        The PID of the parent.

 pgrp %d
        The process group ID of the process.

 session %d
        The session ID of the process.

 tty_nr %d
        The tty the process uses.

 tpgid %d
        The process group ID of the process which currently  owns
        the tty that the process is connected to.

 flags %lu
        The  kernel  flags word of the process. For bit meanings,
        see the PF_* defines in <linux/sched.h>.  Details  depend
        on the kernel version.

 minflt %lu
        The  number  of  minor  faults the process has made which
        have not required loading a memory page from disk.


 nflt %lu
        The number of minor faults that the process's  waited-for
        children have made.

 majflt %lu
        The  number  of  major  faults the process has made which
        have required loading a memory page from disk.

 cmajflt %lu
        The number of major faults that the process's  waited-for
        children have made.

 utime %lu
        The  number  of jiffies that this process has been sched‐
        uled in user mode.

 stime %lu
        The number of jiffies that this process has  been  sched‐
        uled in kernel mode.

 cutime %ld
        The  number  of  jiffies  that  this process's waited-for
        children have been scheduled  in  user  mode.  (See  also
        times(2).)

 cstime %ld
        The  number  of  jiffies  that  this process's waited-for
        children have been scheduled in kernel mode.

 priority %ld
        The standard nice value,  plus  fifteen.   The  value  is
        never negative in the kernel.

 nice %ld
        The  nice  value ranges from 19 (nicest) to -19 (not nice
        to others).

 dummy1 %ld
        This value is hard coded to 0  as  a  placeholder  for  a
        removed field.

 itrealvalue %ld
        The  time  in  jiffies before the next SIGALRM is sent to
        the process due to an interval timer.

 starttime %lu
         children have been scheduled  in  user  mode.  (See  also
        times(2).)

 cstime %ld
        The  number  of  jiffies  that  this process's waited-for
        children have been scheduled in kernel mode.

 priority %ld
        The standard nice value,  plus  fifteen.   The  value  is
        never negative in the kernel.

 nice %ld
        The  nice  value ranges from 19 (nicest) to -19 (not nice
        to others).

 dummy2 %ld
        This value is hard coded to 0  as  a  placeholder  for  a
        removed field.

 itrealvalue %ld
        The  time  in  jiffies before the next SIGALRM is sent to
        the process due to an interval timer.

 starttime %lu
         The time in jiffies  the  process  started  after  system
        boot.
        boot.

 vsize %lu
        Virtual memory size in bytes.

 rss %ld
        Resident  Set  Size:  number  of pages the process has in
        real memory, minus 3 for administrative purposes. This is
        just  the  pages which count towards text, data, or stack
        space.  This does not include pages which have  not  been
        demand-loaded in, or which are swapped out.

 rlim %lu
        Current limit in bytes on the rss of the process (usually
        4294967295 on i386).

 startcode %lu
        The address above which program text can run.

 endcode %lu
        The address below which program text can run.

 startstack %lu
        The address of the start of the stack.

 kstkesp %lu
        The current value of esp (stack pointer), as found in the
        kernel stack page for the process.

 kstkeip %lu
        The current EIP (instruction pointer).

 signal %lu
        The bitmap of pending signals (usually 0).

 blocked %lu
        The  bitmap of blocked signals (usually 0, 2 for shells).

 sigignore %lu
        The bitmap of ignored signals.

 sigcatch %lu
        The bitmap of catched signals.

 wchan %lu
        This is the "channel" in which the  process  is  waiting.
        It  is the address of a system call, and can be looked up
        in a namelist if you need a textual name.  (If  you  have
        an  up-to-date /etc/psdatabase, then try ps -l to see the
        WCHAN field in action.)

 nswap %lu
        Number of pages swapped - not maintained.

 cnswap %lu
        Cumulative nswap for child processes.

 exit_signal %d
        Signal to be sent to parent when we die.

 processor %d
        CPU number last executed on.
 }

 set lines [ split  $statinfo \n ]
 set desc ""
 set flag ""
 foreach line $lines {
        switch -regexp -- $line {
                  "^\ [^ \]" {
                        # [PWQ] 17 jun 2009 Fixed re above to parse this text correctly
                        # a bit fragile if the leading spaces from 
                        # this script are removed fix the re for this
                        # case too!
                        if {[string length $desc]} {
                                lappend ::INFO $flag $desc
                                set desc ""
                                set flag ""
                        }
                        foreach tok $line {
                                lappend ::INFO $tok
                        }
                } "^ .*\$" {
                        if {[string first jiffies $line ]>=0} {
                                set flag jiffies
                        }
                        append desc $line
                        append desc \n
                } default  {
                }
        }
 }
 if {[string length $desc]} {
        lappend ::INFO $flag $desc
 }

 proc < {filename} {
        set fd [ open $filename r ]
        set cont [ read $fd ]
        close $fd
        return $cont
 }
 puts $::INFO
 if {$argc > 1} {
        set pid [ lindex $argv 0 ]
 } else {
        set pid self
 }
 set cont [< /proc/$pid/stat]
 puts stderr $cont
 foreach val $cont {field format flag desc} $::INFO {
        puts stderr "field:$field val:$val format:$format"
        if { "$format" == "hard" } {
                set format %d
        }
        scan $val $format temp
        if { "$flag" == "jiffies"} {
                set temp [ expr {$val * 0.01}]
        }
        set ::STAT($field) $temp
 }
 parray ::STAT

2009 Jun 16 - I tried the above using a tclkit (8.5) on linux, but the 'scan' generated an error 'different numbers of variable names and field specifiers'. ::INFO only has 2 list elements at this point, 'jiffies' and the contents of $statinfo in braces. What am I missing? ... thanks to whoever made changes on Jun 17 - it works for me now.