| 1 |
from ptrace.os_tools import RUNNING_LINUX |
| 2 |
if RUNNING_LINUX: |
| 3 |
from ptrace.linux_proc import ( |
| 4 |
ProcError, |
| 5 |
openProc, |
| 6 |
readProcessLink, |
| 7 |
readProcessProcList, |
| 8 |
readProcessStat, |
| 9 |
) |
| 10 |
from ptrace.signames import signalName # noqa |
| 11 |
from os import ( |
| 12 |
WCOREDUMP, |
| 13 |
WEXITSTATUS, |
| 14 |
WIFSIGNALED, |
| 15 |
WIFSTOPPED, |
| 16 |
WSTOPSIG, |
| 17 |
WTERMSIG, |
| 18 |
) |
| 19 |
|
| 20 |
|
| 21 |
def dumpProcessInfo(log, pid, max_length=None): |
| 22 |
""" |
| 23 |
Dump all information about a process: |
| 24 |
- log: callback to write display one line |
| 25 |
- pid: process identifier |
| 26 |
- max_length (default: None): maximum number of environment variables |
| 27 |
""" |
| 28 |
if not RUNNING_LINUX: |
| 29 |
log("Process ID: %s" % pid) |
| 30 |
return |
| 31 |
try: |
| 32 |
stat = readProcessStat(pid) |
| 33 |
except ProcError: |
| 34 |
# Permission denied |
| 35 |
stat = None |
| 36 |
text = "Process ID: %s" % pid |
| 37 |
if stat: |
| 38 |
text += " (parent: %s)" % stat.ppid |
| 39 |
log(text) |
| 40 |
if stat: |
| 41 |
state = stat.state |
| 42 |
try: |
| 43 |
state = "%s (%s)" % (state, stat.STATE_NAMES[state]) |
| 44 |
except KeyError: |
| 45 |
pass |
| 46 |
log("Process state: %s" % state) |
| 47 |
try: |
| 48 |
log("Process command line: %r" % readProcessProcList(pid, 'cmdline')) |
| 49 |
except ProcError: |
| 50 |
# Permission denied |
| 51 |
pass |
| 52 |
try: |
| 53 |
env = readProcessProcList(pid, 'environ') |
| 54 |
if max_length: |
| 55 |
# Truncate environment if it's too long |
| 56 |
length = 0 |
| 57 |
removed = 0 |
| 58 |
index = 0 |
| 59 |
while index < len(env): |
| 60 |
var = env[index] |
| 61 |
if max_length < length + len(var): |
| 62 |
del env[index] |
| 63 |
removed += 1 |
| 64 |
else: |
| 65 |
length += len(var) |
| 66 |
index += 1 |
| 67 |
env = ', '.join("%s=%r" % tuple(item.split("=", 1)) |
| 68 |
for item in env) |
| 69 |
if removed: |
| 70 |
env += ', ... (skip %s vars)' % removed |
| 71 |
log("Process environment: %s" % env) |
| 72 |
except ProcError: |
| 73 |
# Permission denied |
| 74 |
pass |
| 75 |
try: |
| 76 |
log("Process working directory: %s" % readProcessLink(pid, 'cwd')) |
| 77 |
except ProcError: |
| 78 |
# Permission denied |
| 79 |
pass |
| 80 |
|
| 81 |
try: |
| 82 |
user = None |
| 83 |
group = None |
| 84 |
status_file = openProc("%s/status" % pid) |
| 85 |
for line in status_file: |
| 86 |
if line.startswith("Uid:"): |
| 87 |
user = [int(id) for id in line[5:].split("\t")] |
| 88 |
if line.startswith("Gid:"): |
| 89 |
group = [int(id) for id in line[5:].split("\t")] |
| 90 |
status_file.close() |
| 91 |
if user: |
| 92 |
text = "User identifier: %s" % user[0] |
| 93 |
if user[0] != user[1]: |
| 94 |
text += " (effective: %s)" % user[1] |
| 95 |
log(text) |
| 96 |
if group: |
| 97 |
text = "Group identifier: %s" % group[0] |
| 98 |
if group[0] != group[1]: |
| 99 |
text += " (effective: %s)" % group[1] |
| 100 |
log(text) |
| 101 |
except ProcError: |
| 102 |
# Permission denied |
| 103 |
pass |
| 104 |
|
| 105 |
|
| 106 |
def formatProcessStatus(status, title="Process"): |
| 107 |
""" |
| 108 |
Format a process status (integer) as a string. |
| 109 |
""" |
| 110 |
if WIFSTOPPED(status): |
| 111 |
signum = WSTOPSIG(status) |
| 112 |
text = "%s stopped by signal %s" % (title, signalName(signum)) |
| 113 |
elif WIFSIGNALED(status): |
| 114 |
signum = WTERMSIG(status) |
| 115 |
text = "%s killed by signal %s" % (title, signalName(signum)) |
| 116 |
else: |
| 117 |
exitcode = WEXITSTATUS(status) |
| 118 |
if exitcode: |
| 119 |
text = "%s exited with code %s" % (title, exitcode) |
| 120 |
else: |
| 121 |
text = "%s exited normally" % title |
| 122 |
if WCOREDUMP(status): |
| 123 |
text += " (core dumped)" |
| 124 |
return text |