a
    ze                     @   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	 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ZG dd deejZG dd deejZG dd deejZG dd deejZedkre  dS )    N)findfilec                 C   s   t jt| ddS )NZ
dtracedata)Zsubdir)ospathabspathr   )filename r   &/usr/lib/python3.9/test/test_dtrace.pyr      s    r   c              	   C   st   t dd| } z<dd |  D }|jdd d dd |D }d	|W S  ttfyn   td
| Y n0 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   
z%tracer produced unparsable 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   sp   t | jt|| j t|d |d}tt|| j d }|  }W d    n1 s^0    Y  ||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stderruniversal_newlines)r6   
subprocessPopenPIPESTDOUT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   sx   z |  td| j }| }W n2 tttfyR } zt|}W Y d }~n
d }~0 0 |dkrtt	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   rE   N   s    zTraceBackend.assert_usable)N)N)N)N)__name__
__module____qualname__r)   r3   ZCOMMAND_ARGSr2   r6   r@   r(   rE   r   r   r   r   r$   )   s   



r$   c                   @   s   e Zd ZdZg dZdS )DTraceBackendz.d)Zdtracez-qz-sNrL   rM   rN   r)   r3   r   r   r   r   rO   Z   s   rO   c                   @   s   e Zd ZdZddgZdS )SystemTapBackendz.stpZstapz-gNrP   r   r   r   r   rQ   _   s   rQ   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)backendrE   r-   r   r   r   
setUpClassl   s    zTraceTests.setUpClassc                 C   s&   | j j|| jd\}}| || d S )N)r'   )rS   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   rT   r   r   r   test_function_entry_returnu   s    z%TraceTests.test_function_entry_returnc                    sz   t g d}ttd}|  W d   n1 s60    Y   fdd}|dD ]}||j qVt  | dS )z9Ensure our call stack test hits all function call opcodes)ZCALL_FUNCTIONZCALL_FUNCTION_EXZCALL_FUNCTION_KWzcall_stack.pyNc                    sF   t  ddjd}|jD ](}t|tjr|j| krt|  S qg S )Nz<string>exec)sourcer   modeoptimize)	compiler'   	co_consts
isinstancetypesCodeTypeco_namedisZget_instructions)funcnamecodecZcode_stringr-   r   r   get_function_instructions   s    
zFTraceTests.test_verify_call_opcodes.<locals>.get_function_instructionsstart)setr*   r   r+   discardopnamerV   )r-   Zopcodesr0   rh   Zinstructionr   rg   r   test_verify_call_opcodesx   s    &z#TraceTests.test_verify_call_opcodesc                 C   s   |  d d S )NgcrW   rT   r   r   r   test_gc   s    zTraceTests.test_gcc                 C   s   |  d d S )NlinerW   rT   r   r   r   	test_line   s    zTraceTests.test_line)rL   rM   rN   ZmaxDiffrS   r'   classmethodrU   r2   rX   rm   ro   rq   r   r   r   r   rR   d   s   
rR   c                   @   s   e Zd Ze ZdZdS )DTraceNormalTestsr   NrL   rM   rN   rO   rS   r'   r   r   r   r   rs      s   rs   c                   @   s   e Zd Ze ZdZdS )DTraceOptimizedTests   Nrt   r   r   r   r   ru      s   ru   c                   @   s   e Zd Ze ZdZdS )SystemTapNormalTestsr   NrL   rM   rN   rQ   rS   r'   r   r   r   r   rw      s   rw   c                   @   s   e Zd Ze ZdZdS )SystemTapOptimizedTestsrv   Nrx   r   r   r   r   ry      s   ry   __main__)rc   Zos.pathr   r   r:   rC   r`   rK   Ztest.supportr   r   r#   r$   rO   rQ   rR   ZTestCasers   ru   rw   ry   rL   mainr   r   r   r   <module>   s&   14