| 1 |
from optparse import OptionGroup |
| 2 |
from logging import (getLogger, StreamHandler, |
| 3 |
DEBUG, INFO, WARNING, ERROR) |
| 4 |
from sys import exit |
| 5 |
from ptrace import PtraceError |
| 6 |
from logging import error |
| 7 |
from ptrace.tools import locateProgram |
| 8 |
from ptrace.debugger import ProcessExit, DebuggerError |
| 9 |
from errno import EPERM |
| 10 |
from ptrace.debugger.child import createChild |
| 11 |
|
| 12 |
|
| 13 |
class Application(object): |
| 14 |
|
| 15 |
def __init__(self): |
| 16 |
pass |
| 17 |
|
| 18 |
def _setupLog(self, fd): |
| 19 |
logger = getLogger() |
| 20 |
handler = StreamHandler(fd) |
| 21 |
logger.addHandler(handler) |
| 22 |
if self.options.debug: |
| 23 |
level = DEBUG |
| 24 |
elif self.options.verbose: |
| 25 |
level = INFO |
| 26 |
elif self.options.quiet: |
| 27 |
level = ERROR |
| 28 |
else: |
| 29 |
level = WARNING |
| 30 |
logger.setLevel(level) |
| 31 |
|
| 32 |
def processOptions(self): |
| 33 |
if self.program: |
| 34 |
self.program[0] = locateProgram(self.program[0]) |
| 35 |
|
| 36 |
def createLogOptions(self, parser): |
| 37 |
log = OptionGroup(parser, "Logging") |
| 38 |
log.add_option("--quiet", "-q", help="Be quiet (set log level to ERROR)", |
| 39 |
action="store_true", default=False) |
| 40 |
log.add_option("--verbose", "-v", help="Debug mode (set log level to INFO)", |
| 41 |
action="store_true", default=False) |
| 42 |
log.add_option("--debug", help="Debug mode (set log level to DEBUG)", |
| 43 |
action="store_true", default=False) |
| 44 |
parser.add_option_group(log) |
| 45 |
|
| 46 |
def createChild(self, arguments, env=None): |
| 47 |
return createChild(arguments, self.options.no_stdout, env) |
| 48 |
|
| 49 |
def setupDebugger(self): |
| 50 |
# Set ptrace options |
| 51 |
if self.options.fork: |
| 52 |
try: |
| 53 |
self.debugger.traceFork() |
| 54 |
except DebuggerError: |
| 55 |
error("--fork option is not supported by your OS, sorry!") |
| 56 |
exit(1) |
| 57 |
if self.options.trace_exec: |
| 58 |
self.debugger.traceExec() |
| 59 |
if self.options.trace_clone: |
| 60 |
self.debugger.traceClone() |
| 61 |
|
| 62 |
def createProcess(self): |
| 63 |
if self.options.pid: |
| 64 |
pid = self.options.pid |
| 65 |
is_attached = False |
| 66 |
error("Attach process %s" % pid) |
| 67 |
else: |
| 68 |
pid = self.createChild(self.program) |
| 69 |
is_attached = True |
| 70 |
try: |
| 71 |
return self.debugger.addProcess(pid, is_attached=is_attached) |
| 72 |
except (ProcessExit, PtraceError) as err: |
| 73 |
if isinstance(err, PtraceError) \ |
| 74 |
and err.errno == EPERM: |
| 75 |
error( |
| 76 |
"ERROR: You are not allowed to trace process %s (permission denied or process already traced)" % pid) |
| 77 |
else: |
| 78 |
error("ERROR: Process can no be attached! %s" % err) |
| 79 |
return None |
| 80 |
|
| 81 |
def createCommonOptions(self, parser): |
| 82 |
parser.add_option("--pid", "-p", help="Attach running process specified by its identifier", |
| 83 |
type="int", default=None) |
| 84 |
parser.add_option("--fork", "-f", help="Trace fork() event and child processes", |
| 85 |
action="store_true", default=False) |
| 86 |
parser.add_option("--trace-exec", help="Trace execve() event", |
| 87 |
action="store_true", default=False) |
| 88 |
parser.add_option("--trace-clone", help="Trace clone() event", |
| 89 |
action="store_true", default=False) |
| 90 |
parser.add_option("--no-stdout", help="Use /dev/null as stdout/stderr, or close stdout and stderr if /dev/null doesn't exist", |
| 91 |
action="store_true", default=False) |