-
Notifications
You must be signed in to change notification settings - Fork 0
/
pargs
executable file
·55 lines (50 loc) · 1.47 KB
/
pargs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python
from __future__ import print_function
def pargs(pdir):
with open(pdir+'/cmdline') as f:
cmdline = f.read()
args = cmdline.split('\0')
assert(args[-1] == '') # Terminating NUL
for i, arg in enumerate(args[:-1]):
print('argv[%d]: %r' % (i, arg))
def penv(pdir):
with open(pdir+'/environ') as f:
environ = f.read()
env = environ.split('\0')
assert(env[-1] == '') # Terminating NUL
for ev in env[:-1]:
print(ev)
def pstack(pdir):
# with open(pdir+'/stack') as f:
# print f.read()
taskdir = os.path.join(pdir, 'task')
for task in os.listdir(taskdir):
print('%s:' % task)
stack = os.path.join(taskdir, task, 'stack')
try:
with open(stack) as f:
print(f.read(), end='')
except Exception as e:
print(str(e))
if __name__=='__main__':
import sys
import os
import argparse
ap = argparse.ArgumentParser()
ap.add_argument('pid', type=int,
help='Process identifier')
args = ap.parse_args()
myname = ap.prog
pdir = '/proc/%d' % args.pid
if not os.path.isdir(pdir):
print('%s: Unknown pid %d' % (ap.prog, args.pid),
file=sys.stderr)
sys.exit(1)
if myname == 'pargs':
pargs(pdir)
elif myname == 'penv':
penv(pdir)
elif myname == 'pstack':
pstack(pdir)
else:
ap.error('Invoked as unknown program')