Current File : //usr/local/lib/python3.6/site-packages/ptrace/debugger/__pycache__/debugger.cpython-36.pyc
3

�d~&�@s�ddlmZmZmZddlmZddlmZmZddl	m
Z
mZddlm
Z
ddlmZmZddlmZddlmZer�dd	lmZmZmZmZmZmZGd
d�de�ZGdd
�d
e�ZdS)�)�info�warning�error)�PtraceError)�waitpid�WNOHANG)�SIGTRAP�SIGSTOP)�ECHILD)�
PtraceProcess�
ProcessSignal)�HAS_PTRACE_EVENTS)�sleep)�PTRACE_O_TRACEFORK�PTRACE_O_TRACEVFORK�PTRACE_O_TRACEEXEC�PTRACE_O_TRACESYSGOOD�PTRACE_O_TRACECLONE�THREAD_TRACE_FLAGSc@seZdZdS)�
DebuggerErrorN)�__name__�
__module__�__qualname__�rr�A/tmp/pip-build-in2aja_b/python-ptrace/ptrace/debugger/debugger.pyrsrc@s�eZdZdZdd�Zd'dd�Zdd	�Zd(dd�Zd)d
d�Zd*dd�Z	d+dd�Z
dd�Zd,dd�Zd-dd�Z
dd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�ZdS).�PtraceDebuggera�
    Debugger managing one or multiple processes at the same time.

    Methods
    =======

     * Process list:
       - addProcess(): add a new process
       - deleteProcess(): remove a process from the debugger

     * Wait for an event:
       - waitProcessEvent(): wait for a process event
       - waitSignals(): wait for a signal
       - waitSyscall(): wait for the next syscall event

     * Options:
      - traceFork(): enable fork() tracing
      - traceExec(): enable exec() tracing
      - traceClone(): enable clone() tracing
      - enableSysgood(): enable sysgood option

     * Other:
       - quit(): quit the debugger, terminate all processes

    Operations
    ==========

     - iterate on all processes: "for process in debugger: ..."
     - get a process by its identifier: "process = debugger[pid]"
     - get the number of processes: len(debugger)

    Attributes
    ==========

     - dict: processes dictionary (pid -> PtraceProcess)
     - list: processes list
     - options: ptrace options
     - trace_fork (bool): fork() tracing is enabled?
     - trace_exec (bool): exec() tracing is enabled?
     - trace_clone (bool): clone() tracing is enabled?
     - use_sysgood (bool): sysgood option is enabled?
    cCs6i|_g|_d|_d|_d|_d|_d|_|j�dS)NrF)�dict�list�options�
trace_fork�
trace_exec�trace_clone�use_sysgood�
enableSysgood)�selfrrr�__init__@szPtraceDebugger.__init__NFcCs�||jkrtd|��t|||||d�}td|�||j|<|jj|�y|jtt�Wnbt	k
rzt
d|�YnDtk
r�}z|j�WYdd}~Xnd|_
|j��YnXtr�|jr�|j|j�|S)z�
        Add a new process using its identifier. Use is_attached=False to
        attach an existing (running) process, and is_attached=True to trace
        a new (stopped) process.
        z%The process %s is already registered!)�parent�	is_threadzAttach %s to debuggerzEUser interrupt! Force the process %s attach (don't wait for signals).NF)r�KeyErrorrrr�append�waitSignalsrr	�KeyboardInterruptrr�display�is_attached�detachr
rZ
setoptions)r$�pidr-r&r'�process�eventrrr�
addProcessJs,



zPtraceDebugger.addProcesscCs8td�t|j�}x t|�D]}|j�|j�qWdS)zN
        Quit the debugger: terminate all processes in reverse order.
        z
Quit debuggerN)rr�reversed�	terminater.)r$Z	processesr0rrr�quiths

zPtraceDebugger.quitTcCs�d}|s|tO}|rV||jkr.td||d��|j|}|jrF|tO}t||�\}}ntd|�\}}|sl|r�|r�||kr�td||f|d��||fS)a'
        Wait for a process event from a specific process (if wanted_pid is
        set) or any process (wanted_pid=None). The call is blocking is
        blocking option is True. Return the tuple (pid, status).

        See os.waitpid() documentation for explanations about the result.
        rzUnknown PID: %r)r/�z Unwanted PID: %r (instead of %s)���)rrrr'rr)r$�
wanted_pid�blocking�flagsr0r/�statusrrr�_waitpidts 

zPtraceDebugger._waitpidcCs�d}x�|s�y|j||�\}}WnBtk
r`}z&|jtkrL|j|}|j�S|�WYdd}~XnX|rr|rrdSy|j|}Wqtk
r�td|�YqXqW|j|�S)z�
        Wait for a process event from the specified process identifier. If
        blocking=False, return None if there is no new event, otherwise return
        an object based on ProcessEvent.
        Nz!waitpid() warning: Unknown PID %r)	r<�OSError�errnor
rZprocessTerminatedr(rZ
processStatus)r$r8r9r0r/r;�errrrr�_wait_event_pid�s 

zPtraceDebugger._wait_event_pidcCs�|dk	r|j||�Sd}xpt|j�}t|�dkrvx$|D]}|j|d�}|dk	r6|Sq6W|s^dSt|dd�}t|�q|j|d|�SqWdS)Ng����MbP?r6F�g�?r)r@�tupler�len�minr)r$r8r9�pauseZpidsr/r0rrr�_wait_event�s


zPtraceDebugger._wait_eventcCs|j||�S)z�
        Wait for a process event from a specific process (if pid option is
        set) or any process (default). If blocking=False, return None if there
        is no new event, otherwise return an object based on ProcessEvent.
        )rF)r$r/r9rrr�waitProcessEvent�szPtraceDebugger.waitProcessEventcOsJ|jdd�}x8|j|�}|jtkr&|�|j}||ks:|r>|S|�qWdS)z�
        Wait for any signal or some specific signals (if specified) from a
        specific process (if pid keyword is set) or any process (default).
        Return a ProcessSignal object or raise an unexpected ProcessEvent.
        r/N)�getrF�	__class__r�signum)r$Zsignals�kwr/r1rJrrrr*�s

zPtraceDebugger.waitSignalscCs4t}|jr|dO}|r&|j||jd�S|j|�SdS)z�
        Wait for the next syscall event (enter or exit) for a specific process
        (if specified) or any process (default). Return a ProcessSignal object
        or raise an unexpected ProcessEvent.
        �)r/N)rr"r*r/)r$r0rJrrr�waitSyscall�szPtraceDebugger.waitSyscallcCsv|s(y|j|}Wntk
r&dSXy|j|j=Wntk
rJYnXy|jj|�Wntk
rpYnXdS)z9
        Delete a process from the process list.
        N)rr(r/r�remove�
ValueError)r$r0r/rrr�
deleteProcess�szPtraceDebugger.deleteProcesscCs6tstd��|jttBO_d|_td|j�dS)zJ
        Enable fork() tracing. Do nothing if it's not supported.
        zMTracing fork events is not supported on this architecture or operating systemTz!Debugger trace forks (options=%s)N)r
rrrrrr)r$rrr�	traceFork�szPtraceDebugger.traceForkcCs tsdSd|_|jtO_dS)zJ
        Enable exec() tracing. Do nothing if it's not supported.
        NT)r
r rr)r$rrr�	traceExec�szPtraceDebugger.traceExeccCs tsdSd|_|jtO_dS)zK
        Enable clone() tracing. Do nothing if it's not supported.
        NT)r
r!rr)r$rrr�
traceCloneszPtraceDebugger.traceClonecCs tsdSd|_|jtO_dS)z�
        Enable sysgood option: ask the kernel to set bit #7 of the signal
        number if the signal comes from the kernel space. If the signal comes
        from the user space, the bit is unset.
        NT)r
r"rr)r$rrrr#szPtraceDebugger.enableSysgoodcCs
|j|S)N)r)r$r/rrr�__getitem__szPtraceDebugger.__getitem__cCs
t|j�S)N)�iterr)r$rrr�__iter__!szPtraceDebugger.__iter__cCs
t|j�S)N)rCr)r$rrr�__len__$szPtraceDebugger.__len__)NF)T)T)T)NT)N)NN)rrr�__doc__r%r2r5r<r@rFrGr*rMrPrQrRrSr#rTrVrWrrrrrs$*









rN)�loggingrrrZptracer�osrr�signalrr	r>r
Zptrace.debuggerrrZptrace.bindingr
�timerZptrace.binding.funcrrrrrrr�objectrrrrr�<module>s