#!/usr/bin/perl # # ptree - print the Linux process list as a tree # Copyright (C) 2002 - Anthony Tonns # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # # # initially written # ATonns Sun Oct 24 21:27:56 EDT 1999 # # updated to sort by starttime of process # ATonns Mon Jun 24 17:54:12 EDT 2002 # # fixed bug where children processes were dropped from the list if # they started on the same jiffie as the parent and appeared earlier # in the list # ATonns Mon Jul 22 22:03:36 EDT 2002 # # use strict; my %foundprocs; my %starttime; # truncate width of cmdline # (length 0 means unlimited) my $termwidth = "132"; # read /proc my @procs; opendir(D,"/proc"); @procs=readdir(D); closedir(D); # filter out everything that's not digits @procs=grep(/\d+/,@procs); # get the starttime for all procs my $p; foreach $p (@procs) { open(F,"/proc/$p/stat"); my $stat = ; close F; chomp $stat; my ( $pid, $comm, $state, $ppid, $pgrp, $session, $tty, $tpgid, $flags, $minflt, $cminflt, $majflt, $cmajflt, $utime, $stime, $cutime, $cstime, $counter, $priority, $timeout, $itrealvalue, $starttime, $vsize, $rss, $rlim, $startcode, $endcode, $startstack, $kstkesp, $kstkeip, $signal, $blocked, $sigignore, $sigcatch, $wchan) = split(" ",$stat); $starttime{$p}=$starttime; } # get data for printing ptree my $pid; foreach $pid (sort bystarttime (keys(%starttime))) { # create data structure and add pid if (defined $foundprocs{$pid}) { # print "!!! $pid !!!\n"; } else { $foundprocs{$pid}->{"children"}=[ ]; } $foundprocs{$pid}->{"pid"}=$pid; # get ppid and public name from status file my $ppid=0; my $name=""; open(P,"/proc/$pid/status"); while(

) { my $trash; if($_ =~ /^PPid:/) { chomp; ($trash,$ppid)=split(/\t/); } if($_ =~ /^Name:/) { chomp; ($trash,$name)=split(/\t/); } } close(P); $foundprocs{$pid}->{"ppid"}=$ppid; $foundprocs{$pid}->{"name"}=$name; # get the command line (if we can) my $cmdline=""; open(C,"/proc/$pid/cmdline"); $cmdline=; close(C); # # replace the NULLs in the cmdline with spaces here # $cmdline=join ' ', split chr(0), $cmdline; if( $termwidth > 0 ) { $cmdline = substr($cmdline,0,$termwidth); } $foundprocs{$pid}->{"cmdline"}=$cmdline; # print qq!pid: "$foundprocs{$pid}->{"pid"}" !; # print qq!ppid: "$foundprocs{$pid}->{"ppid"}" !; # print qq!name: "$foundprocs{$pid}->{"name"}" !; # print qq!cmdline: "$foundprocs{$pid}->{"cmdline"}" !; # print "\n"; push(@{$foundprocs{$ppid}->{"children"}},$pid); } # recurse the tree printnode(0,$foundprocs{1}); sub printnode { my ($level,$node) = @_; print " "x$level; print $node->{"pid"} . " "; if ( $node->{"cmdline"} ne "" ) { print $node->{"cmdline"}; } else { print "[" . $node->{"name"} . "]"; } print "\n"; my $child; foreach $child (@{$node->{"children"}}) { printnode($level+1,$foundprocs{$child}); } } sub bystarttime { $starttime{$a} <=> $starttime{$b}; }