Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | #!/usr/bin/env python |
michael@0 | 2 | |
michael@0 | 3 | # Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. |
michael@0 | 4 | # Use of this source code is governed by a BSD-style license that can be |
michael@0 | 5 | # found in the LICENSE file. |
michael@0 | 6 | |
michael@0 | 7 | """ |
michael@0 | 8 | Print detailed information about a process. |
michael@0 | 9 | |
michael@0 | 10 | Author: Giampaolo Rodola' <g.rodola@gmail.com> |
michael@0 | 11 | """ |
michael@0 | 12 | |
michael@0 | 13 | import os |
michael@0 | 14 | import datetime |
michael@0 | 15 | import socket |
michael@0 | 16 | import sys |
michael@0 | 17 | |
michael@0 | 18 | import psutil |
michael@0 | 19 | |
michael@0 | 20 | |
michael@0 | 21 | def convert_bytes(n): |
michael@0 | 22 | symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y') |
michael@0 | 23 | prefix = {} |
michael@0 | 24 | for i, s in enumerate(symbols): |
michael@0 | 25 | prefix[s] = 1 << (i+1)*10 |
michael@0 | 26 | for s in reversed(symbols): |
michael@0 | 27 | if n >= prefix[s]: |
michael@0 | 28 | value = float(n) / prefix[s] |
michael@0 | 29 | return '%.1f%s' % (value, s) |
michael@0 | 30 | return "%sB" % n |
michael@0 | 31 | |
michael@0 | 32 | def print_(a, b): |
michael@0 | 33 | if sys.stdout.isatty() and os.name == 'posix': |
michael@0 | 34 | fmt = '\x1b[1;32m%-17s\x1b[0m %s' %(a, b) |
michael@0 | 35 | else: |
michael@0 | 36 | fmt = '%-15s %s' %(a, b) |
michael@0 | 37 | # python 2/3 compatibility layer |
michael@0 | 38 | sys.stdout.write(fmt + '\n') |
michael@0 | 39 | sys.stdout.flush() |
michael@0 | 40 | |
michael@0 | 41 | def run(pid): |
michael@0 | 42 | ACCESS_DENIED = '' |
michael@0 | 43 | try: |
michael@0 | 44 | p = psutil.Process(pid) |
michael@0 | 45 | pinfo = p.as_dict(ad_value=ACCESS_DENIED) |
michael@0 | 46 | except psutil.NoSuchProcess: |
michael@0 | 47 | sys.exit(str(sys.exc_info()[1])) |
michael@0 | 48 | |
michael@0 | 49 | try: |
michael@0 | 50 | if p.parent: |
michael@0 | 51 | parent = '(%s)' % p.parent.name |
michael@0 | 52 | else: |
michael@0 | 53 | parent = '' |
michael@0 | 54 | except psutil.Error: |
michael@0 | 55 | parent = '' |
michael@0 | 56 | started = datetime.datetime.fromtimestamp(pinfo['create_time'] |
michael@0 | 57 | ).strftime('%Y-%M-%d %H:%M') |
michael@0 | 58 | io = pinfo.get('io_counters', ACCESS_DENIED) |
michael@0 | 59 | mem = '%s%% (resident=%s, virtual=%s) ' % ( |
michael@0 | 60 | round(pinfo['memory_percent'], 1), |
michael@0 | 61 | convert_bytes(pinfo['memory_info'].rss), |
michael@0 | 62 | convert_bytes(pinfo['memory_info'].vms)) |
michael@0 | 63 | children = p.get_children() |
michael@0 | 64 | |
michael@0 | 65 | print_('pid', pinfo['pid']) |
michael@0 | 66 | print_('name', pinfo['name']) |
michael@0 | 67 | print_('exe', pinfo['exe']) |
michael@0 | 68 | print_('parent', '%s %s' % (pinfo['ppid'], parent)) |
michael@0 | 69 | print_('cmdline', ' '.join(pinfo['cmdline'])) |
michael@0 | 70 | print_('started', started) |
michael@0 | 71 | print_('user', pinfo['username']) |
michael@0 | 72 | if os.name == 'posix' and pinfo['uids'] and pinfo['gids']: |
michael@0 | 73 | print_('uids', 'real=%s, effective=%s, saved=%s' % pinfo['uids']) |
michael@0 | 74 | if os.name == 'posix' and pinfo['gids']: |
michael@0 | 75 | print_('gids', 'real=%s, effective=%s, saved=%s' % pinfo['gids']) |
michael@0 | 76 | if os.name == 'posix': |
michael@0 | 77 | print_('terminal', pinfo['terminal'] or '') |
michael@0 | 78 | if hasattr(p, 'getcwd'): |
michael@0 | 79 | print_('cwd', pinfo['cwd']) |
michael@0 | 80 | print_('memory', mem) |
michael@0 | 81 | print_('cpu', '%s%% (user=%s, system=%s)' % (pinfo['cpu_percent'], |
michael@0 | 82 | getattr(pinfo['cpu_times'], 'user', '?'), |
michael@0 | 83 | getattr(pinfo['cpu_times'], 'system', '?'))) |
michael@0 | 84 | print_('status', pinfo['status']) |
michael@0 | 85 | print_('niceness', pinfo['nice']) |
michael@0 | 86 | print_('num threads', pinfo['num_threads']) |
michael@0 | 87 | if io != ACCESS_DENIED: |
michael@0 | 88 | print_('I/O', 'bytes-read=%s, bytes-written=%s' % \ |
michael@0 | 89 | (convert_bytes(io.read_bytes), |
michael@0 | 90 | convert_bytes(io.write_bytes))) |
michael@0 | 91 | if children: |
michael@0 | 92 | print_('children', '') |
michael@0 | 93 | for child in children: |
michael@0 | 94 | print_('', 'pid=%s name=%s' % (child.pid, child.name)) |
michael@0 | 95 | |
michael@0 | 96 | if pinfo['open_files'] != ACCESS_DENIED: |
michael@0 | 97 | print_('open files', '') |
michael@0 | 98 | for file in pinfo['open_files']: |
michael@0 | 99 | print_('', 'fd=%s %s ' % (file.fd, file.path)) |
michael@0 | 100 | |
michael@0 | 101 | if pinfo['threads']: |
michael@0 | 102 | print_('running threads', '') |
michael@0 | 103 | for thread in pinfo['threads']: |
michael@0 | 104 | print_('', 'id=%s, user-time=%s, sys-time=%s' \ |
michael@0 | 105 | % (thread.id, thread.user_time, thread.system_time)) |
michael@0 | 106 | if pinfo['connections'] != ACCESS_DENIED: |
michael@0 | 107 | print_('open connections', '') |
michael@0 | 108 | for conn in pinfo['connections']: |
michael@0 | 109 | if conn.type == socket.SOCK_STREAM: |
michael@0 | 110 | type = 'TCP' |
michael@0 | 111 | elif conn.type == socket.SOCK_DGRAM: |
michael@0 | 112 | type = 'UDP' |
michael@0 | 113 | else: |
michael@0 | 114 | type = 'UNIX' |
michael@0 | 115 | lip, lport = conn.laddr |
michael@0 | 116 | if not conn.raddr: |
michael@0 | 117 | rip, rport = '*', '*' |
michael@0 | 118 | else: |
michael@0 | 119 | rip, rport = conn.raddr |
michael@0 | 120 | print_('', '%s:%s -> %s:%s type=%s status=%s' \ |
michael@0 | 121 | % (lip, lport, rip, rport, type, conn.status)) |
michael@0 | 122 | |
michael@0 | 123 | def main(argv=None): |
michael@0 | 124 | if argv is None: |
michael@0 | 125 | argv = sys.argv |
michael@0 | 126 | if len(argv) == 1: |
michael@0 | 127 | sys.exit(run(os.getpid())) |
michael@0 | 128 | elif len(argv) == 2: |
michael@0 | 129 | sys.exit(run(int(argv[1]))) |
michael@0 | 130 | else: |
michael@0 | 131 | sys.exit('usage: %s [pid]' % __file__) |
michael@0 | 132 | |
michael@0 | 133 | if __name__ == '__main__': |
michael@0 | 134 | sys.exit(main()) |