Edit File: process.cpython-36.pyc
3 �e�d � @ s| d dl mZmZmZmZmZmZmZmZm Z m Z mZmZm Z mZ d dlmZmZmZ d dlmZ d dlmZ d dlmZmZmZmZ d dlmZmZmZ d dlm Z m!Z! d dl"m#Z#m$Z$ d d l%m&Z&m'Z'm(Z( d d l)m*Z* d dl+m,Z,m-Z- d dl.m/Z/m0Z0m1Z1m2Z2m3Z3 d d l4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z; d dl<m=Z= d dl>m?Z? d dl@mAZA d dlBmCZC d dlDmEZEmFZFmGZGmHZH d dlImJZJ e�r�d dl mKZK e�r�d dl mLZL e�r�d dl"mMZMmNZN d dl mOZOmPZPmQZQmRZR e�rd dl mSZSmTZTmUZUmVZVmWZWmXZXmYZY eVeWeXfZZe�se�rd dl m[Z[ nd dl m\Z\m]Z] e=�rFd dl<m^Z^m_Z_m`Z` e�rXd dlambZb dZcdZddZed ZfG d!d"� d"eg�Zhd#S )$� )�HAS_PTRACE_SINGLESTEP�HAS_PTRACE_EVENTS�HAS_PTRACE_SIGINFO� HAS_PTRACE_IO�HAS_PTRACE_GETREGS�HAS_PTRACE_GETREGSET� ptrace_attach� ptrace_detach�ptrace_cont�ptrace_syscall�ptrace_setregs�ptrace_peektext�ptrace_poketext�REGISTER_NAMES)�HAS_PROC�RUNNING_BSD�RUNNING_PYTHON3)�dumpRegs)� CPU_WORD_SIZE)� bytes2word� word2bytes� bytes2type�bytes2array)�SIGTRAP�SIGSTOP�SIGKILL)� formatAddress� formatWordHex)�sizeof�c_char_p)�info�warning�error)�PtraceError)�ESRCH�EACCES)� Breakpoint�ProcessExit� ProcessSignal�NewProcessEvent�ProcessExecution)�kill� WIFSTOPPED�WSTOPSIG�WIFSIGNALED�WTERMSIG� WIFEXITED�WEXITSTATUS)�HAS_DISASSEMBLER)�getBacktrace)�ProcessError)�readProcessMappings)�CPU_INSTR_POINTER�CPU_STACK_POINTER�CPU_FRAME_POINTER�CPU_SUB_REGISTERS)�SyscallState)�ptrace_singlestep)�ptrace_getsiginfo)�create_string_buffer� addressof)� ptrace_io�ptrace_io_desc�PIOD_READ_D�PIOD_WRITE_D)�ptrace_setoptions�ptrace_geteventmsg�WPTRACEEVENT�PTRACE_EVENT_FORK�PTRACE_EVENT_VFORK�PTRACE_EVENT_CLONE�PTRACE_EVENT_EXEC)�ptrace_getregs)�ptrace_peekuser�ptrace_registers_t)�disassemble�disassembleOne�MAX_INSTR_SIZE)�readProcessStat� i � � c @ s e Zd ZdZddd�Zdd� Zdd � Zd�d d�Zdd � Zd�dd�Z d�dd�Z dd� Zdd� Zdd� Z dd� Zd�dd�Zdd� Zdd � Zd!d"� Zd#d$� Zd%d&� Zd'd(� Zd)d*� Zd+d,� Zd-d.� Zd/d0� Zd1d2� Zd3d4� Zd5d6� Zd�d8d9�Zd:d;� Zd<d=� Zd>d?� Z d@dA� Z!dBdC� Z"dDdE� Z#e$�r(dFdG� Z%ne&�r8dHdG� Z%ne"Z%dIdJ� Z'dKdL� Z(dMdN� Z)dOdP� Z*d�dRdS�Z+d�dTdU�Z,dVdW� Z-dXdY� Z.d�dZd[�Z/d\d]� Z0d�d^d_�Z1d�d`da�Z2dbdc� Z3ddde� Z4dfdg� Z5dhdi� Z6djdk� Z7d�dmdn�Z8d�dqdr�Z9dsdt� Z:dudv� Z;dwdx� Z<dydz� Z=d{d|� Z>d}d~� Z?dS )�� PtraceProcessa� Process traced by a PtraceDebugger. Methods ======= * control execution: - singleStep(): execute one instruction - cont(): continue the execution - syscall(): break at next syscall - setInstrPointer(): change the instruction pointer - kill(): send a signal to the process - terminate(): kill the process * wait an event: - waitEvent(): wait next process event - waitSignals(): wait a signal * get status - getreg(): get a register - getInstrPointer(): get the instruction pointer - getStackPointer(): get the stack pointer - getFramePointer(): get the stack pointer - getregs(): get all registers, e.g. regs=getregs(); print regs.eax - disassemble(): assembler code of the next instructions - disassembleOne(): assembler code of the next instruction - findStack(): get stack memory mapping - getsiginfo(): get signal information - getBacktrace(): get the current backtrace * set status - setreg(): set a register - setregs(): set all registers * memory access: - readWord(): read a memory word - readBytes(): read some bytes - readStruct(): read a structure - readArray(): read an array - readCString(): read a C string - readMappings(): get all memory mappings - writeWord(): write a memory word - writeBytes(): write some bytes * display status: - dumpCode(): display the next instructions - dumpStack(): display some memory words around the stack pointer - dumpMaps(): display memory mappings - dumpRegs(): display all registers * breakpoint: - createBreakpoint(): set a breakpoint - findBreakpoint(): find a breakpoint - removeBreakpoint(): remove a breakpoint * other: - setoptions(): set ptrace options See each method to get better documentation. You are responsible to manage the process state: some methods may fail or crash your processus if they are called when the process is in the wrong state. Attributes ========== * main attributes: - pid: identifier of the process - debugger: PtraceDebugger instance - breakpoints: dictionary of active breakpoints - parent: parent PtraceProcess (None if process has no parent) * state: - running: if True, the process is alive, otherwise the process doesn't exist anymore - exited: if True, the process has exited (attributed only used on BSD operation systems) - is_attached: if True, the process is attached by ptrace - was_attached: if True, the process will be detached at exit - is_stopped: if True, the process is stopped, otherwise it's running - syscall_state: control syscall tracing Sometimes, is_stopped value is wrong. You might use isTraced() to make sure that the process is stopped. NFc C sh || _ i | _|| _d| _d| _|| _|| _d| _d| _|| _ |sJ| j � nd| _trZd | _t | �| _d S )NTF)�debugger�breakpoints�pid�running�exited�parent�was_attached�is_attached� is_stopped� is_thread�attachr � read_mem_filer: Z syscall_state)�selfrU rW r\ rZ r^ � rb �@/tmp/pip-build-uv1wutoy/python-ptrace/ptrace/debugger/process.py�__init__� s zPtraceProcess.__init__c C s t s| j� t| j�}|jdkS )N�T)r �notImplementedErrorrP rW �state)ra �statrb rb rc �isTraced� s zPtraceProcess.isTracedc C s, | j r d S td| j � t| j� d| _ d S )NzAttach process %sT)r\ r rW r )ra rb rb rc r_ � s zPtraceProcess.attachc C s� |st }y| j� }W n> tk rR } z"|d kr>|d| � d S d }W Y d d }~X nX |d kr`|}y| j|||||� W n6 tk r� } z|dt|�|f � W Y d d }~X nX d S )Nz&Unable to read instruction pointer: %szUnable to dump code at %s: %s)r"