B
    u9a                 @   s   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	m
Z
 dd Zdd ZG dd dZG d	d
 d
eZG dd deZG dd dejZG dd deZG dd deZG dd deZG dd deZdd Zedkre  dS )    N)findfilerun_unittestc             C   s   t jt| ddS )NZ
dtracedata)Zsubdir)ospathabspathr   )filename r   !/usr/lib/python3.7/test_dtrace.pyr      s    r   c          	   C   st   t dd| } y:dd |  D }|jdd d dd |D }d	|S  ttfk
rn   td
| Y nX dS )aM  Normalize DTrace output for comparison.

    DTrace keeps a per-CPU buffer, and when showing the fired probes, buffers
    are concatenated. So if the operating system moves our thread around, the
    straight result can be "non-causal". So we add timestamps to the probe
    firing, sort by that field, then strip it from the outputz\[[0-9]+ refs\] c             S   s$   g | ]}|r| d s|dqS )#	)
startswithsplit).0rowr   r   r	   
<listcomp>   s   z*normalize_trace_output.<locals>.<listcomp>c             S   s   t | d S )Nr   )int)r   r   r   r	   <lambda>        z(normalize_trace_output.<locals>.<lambda>)keyc             S   s   g | ]}|d  qS )   r   )r   r   r   r   r	   r   !   s    
z&tracer produced unparseable output:
{}N)	resub
splitlinessortjoin
IndexError
ValueErrorAssertionErrorformat)outputresultr   r   r	   normalize_trace_output   s    	
r#   c               @   sH   e Zd ZdZdZg ZdddZdddZdddZddd	Z	d
d Z
dS )TraceBackendNc          	   C   s\   t | jt|| j t|d |d}tt|| j d }|  }W d Q R X ||fS )Nz.py)script_filepython_fileoptimize_pythonz	.expected)r#   trace_pythonr   	EXTENSIONopenreadrstrip)selfnamer'   actual_outputfexpected_outputr   r   r	   run_case.   s    

zTraceBackend.run_casec             C   s    | j |g }|r|d|g7 }|S )Nz-c)COMMAND)r-   r%   
subcommandcommandr   r   r	   generate_trace_command9   s    z#TraceBackend.generate_trace_commandc             C   s.   |  ||}tj|tjtjdd \}}|S )NT)stdoutstderrZuniversal_newlines)r6   
subprocessPopenPIPEZSTDOUTZcommunicate)r-   r%   r4   r5   r7   _r   r   r	   trace?   s    zTraceBackend.tracec             C   s<   g }|r| dg|  dtjg| |g }| ||S )Nz-O )extendr   sys
executabler=   )r-   r%   r&   r'   Zpython_flagsr4   r   r   r	   r(   G   s
    zTraceBackend.trace_pythonc          
   C   sv   y |  td| j }| }W n0 tttfk
rP } zt|}W d d }~X Y nX |dkrrt	d
| jd |d S )Nassert_usablezprobe: successz{}(1) failed: {}r   )r=   r   r)   stripFileNotFoundErrorNotADirectoryErrorPermissionErrorstrunittestZSkipTestr    r3   )r-   r!   Zfnfer   r   r	   rB   N   s    zTraceBackend.assert_usable)N)N)N)N)__name__
__module____qualname__r)   r3   ZCOMMAND_ARGSr2   r6   r=   r(   rB   r   r   r   r	   r$   )   s   



r$   c               @   s   e Zd ZdZdddgZdS )DTraceBackendz.dZdtracez-qz-sN)rI   rJ   rK   r)   r3   r   r   r   r	   rL   Z   s   rL   c               @   s   e Zd ZdZddgZdS )SystemTapBackendz.stpZstapz-gN)rI   rJ   rK   r)   r3   r   r   r   r	   rM   _   s   rM   c               @   sL   e Zd ZdZdZdZedd Zdd Zdd Z	d	d
 Z
dd Zdd ZdS )
TraceTestsNr   c             C   s   | j   d S )N)backendrB   )r-   r   r   r	   
setUpClassl   s    zTraceTests.setUpClassc             C   s&   | j j|| jd\}}| || d S )N)r'   )rO   r2   r'   assertEqual)r-   r.   r/   r1   r   r   r	   r2   p   s    zTraceTests.run_casec             C   s   |  d d S )NZ
call_stack)r2   )r-   r   r   r	   test_function_entry_returnu   s    z%TraceTests.test_function_entry_returnc          	      sl   t dddg}ttd}|  W dQ R X  fdd}x|dD ]}||j qFW t  | dS )	z9Ensure our call stack test hits all function call opcodesZCALL_FUNCTIONZCALL_FUNCTION_EXZCALL_FUNCTION_KWzcall_stack.pyNc                sF   t  ddjd}x.|jD ]$}t|tjr|j| krt|S qW g S )Nz<string>exec)sourcer   modeoptimize)	compiler'   	co_consts
isinstancetypesCodeTypeco_namedisZget_instructions)funcnamecodec)code_stringr-   r   r	   get_function_instructions   s    
zFTraceTests.test_verify_call_opcodes.<locals>.get_function_instructionsstart)setr*   r   r+   discardopnamerQ   )r-   Zopcodesr0   rb   Zinstructionr   )ra   r-   r	   test_verify_call_opcodesx   s    z#TraceTests.test_verify_call_opcodesc             C   s   |  d d S )NZgc)r2   )r-   r   r   r	   test_gc   s    zTraceTests.test_gcc             C   s   |  d d S )Nline)r2   )r-   r   r   r	   	test_line   s    zTraceTests.test_line)rI   rJ   rK   ZmaxDiffrO   r'   classmethodrP   r2   rR   rg   rh   rj   r   r   r   r	   rN   d   s   rN   c               @   s   e Zd Ze ZdZdS )DTraceNormalTestsr   N)rI   rJ   rK   rL   rO   r'   r   r   r   r	   rl      s   rl   c               @   s   e Zd Ze ZdZdS )DTraceOptimizedTests   N)rI   rJ   rK   rL   rO   r'   r   r   r   r	   rm      s   rm   c               @   s   e Zd Ze ZdZdS )SystemTapNormalTestsr   N)rI   rJ   rK   rM   rO   r'   r   r   r   r	   ro      s   ro   c               @   s   e Zd Ze ZdZdS )SystemTapOptimizedTestsrn   N)rI   rJ   rK   rM   rO   r'   r   r   r   r	   rp      s   rp   c               C   s   t tttt d S )N)r   rl   rm   ro   rp   r   r   r   r	   	test_main   s    rq   __main__)r]   Zos.pathr   r   r9   r@   rZ   rH   Ztest.supportr   r   r   r#   r$   rL   rM   ZTestCaserN   rl   rm   ro   rp   rq   rI   r   r   r   r	   <module>   s(   14