U
    iâËd  ã                   @   sø   d Z ddlZddlZddlZddlZddlmZ ddlmZ ddl	m
Z
 z$edd� dZe
ƒ Zej d	¡ZW n ek
rˆ   dZdZY nX d
Ze ee¡ZdZe ee¡ZzddlZdZW n ek
rÒ   dZY nX dZe ee¡ZG dd„ deƒZdS )z'Helpers for running gdb related testingé    N)Úconfig)Ú_confirm_gdb)Úcollect_gdbinfoF)Zneed_ptrace_attachTÚ3z;functioning gdb with correct ptrace permissions is requiredz gdb with python 3 support neededzpexpect module needed for testc                   @   s¾   e Zd ZdZd0dd„Zdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Z	dd„ Z
d1dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zd2d d!„Zd3d"d#„Zd4d&d'„Zd5d(d)„Zd6d*d+„Zd,d-„ Zd.d/„ ZdS )7ÚGdbMIDriverzs
    Driver class for the GDB machine interface:
    https://sourceware.org/gdb/onlinedocs/gdb/GDB_002fMI.html
    Féx   Nc                 C   sT   t sd}t|ƒ‚ts d}t|ƒ‚tj| _tj| _|| _	|| _
|| _|| _|  ¡  d S )Nz[This driver requires the pexpect module. This can be obtained via:

$ conda install pexpectzWThis driver requires a gdb binary. This can be obtained via the system package manager.)Ú_HAVE_PEXPECTÚRuntimeErrorÚ	_HAVE_GDBr   Z
GDB_BINARYÚ_gdb_binaryÚsysÚ
executableÚ_pythonÚ_debugÚ
_file_nameÚ_timeoutÚ
_init_cmdsÚ_drive)ÚselfÚ	file_nameÚdebugÚtimeoutZ	init_cmdsÚmsg© r   úP/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/numba/tests/gdb_support.pyÚ__init__+   s    zGdbMIDriver.__init__c                 C   sp   t j | j¡st‚| jddg}| jdk	r6|t| jƒ7 }|d| j| jg7 }t	 
d |¡¡| _| jrltjj| j_dS )z0This function sets up the caputured gdb instancez--interpreterÚmiNz--argsú )ÚosÚpathÚisfiler   ÚAssertionErrorr   r   Úlistr   ÚpexpectÚspawnÚjoinÚ	_capturedr   r   ÚstdoutÚbufferZlogfile©r   Úcmdr   r   r   r   <   s    
zGdbMIDriver._drivec                 C   s   d|   ¡ kS )z_Returns True if the underlying gdb implementation has python support
           False otherwiseÚpython)Úlist_features©r   r   r   r   Úsupports_pythonG   s    zGdbMIDriver.supports_pythonc                 C   s,   |   ¡ sdS d}|  d|¡ d| jj ¡ kS )z€Returns True if the underlying gdb implementation has NumPy support
           (and by extension Python support) False otherwiseFzGpython from __future__ import print_function;import numpy; print(numpy)Úconsolezmodule 'numpy' from)r.   Úinterpreter_execr&   ÚbeforeÚdecoder)   r   r   r   Úsupports_numpyL   s
    zGdbMIDriver.supports_numpyc              
   C   sX   z| j j|| jd� W n< tjjk
rR } zd|› d�}t|ƒ|‚W 5 d }~X Y nX d S )N)r   zExpected value did not arrive: Ú.)r&   Úexpectr   r#   Ú
exceptionsÚTIMEOUTÚ
ValueError)r   r5   Úer   r   r   r   Ú_captured_expectW   s
    zGdbMIDriver._captured_expectc                 C   s4   | j j}| d¡}||ks0td|› d|› d�ƒ‚dS )z=Asserts that the current output string contains the expected.úutf-8údecoded=ú

expected=ú)N)r&   Úafterr2   r!   )r   ÚexpectedÚoutputÚdecodedr   r   r   Úassert_output^   s    
zGdbMIDriver.assert_outputc                 C   sH   | j j}| d¡}| ¡ d }t ||¡}|sDtd|› d|› d�ƒ‚dS )zKAsserts that the current output string contains the expected
        regex.r;   r   r<   r=   r>   N)r&   r?   r2   Ú
splitlinesÚreÚmatchr!   )r   r@   rA   rB   Zdone_strÚfoundr   r   r   Úassert_regex_outputd   s
    
zGdbMIDriver.assert_regex_outputÚ c                 C   s   | j  |¡ |  |¡ d S )N)r&   Zsendliner:   )r   Úcommandr5   r   r   r   Ú_run_commandm   s    zGdbMIDriver._run_commandc                 C   s   | j ddd� dS )zgdb command ~= 'run'z	-exec-runú\^running.*\r\n©r5   N©rK   r-   r   r   r   Úrunq   s    zGdbMIDriver.runc                 C   s   | j ddd� dS )zgdb command ~= 'continue'z-exec-continuerL   rM   NrN   r-   r   r   r   Úcontu   s    zGdbMIDriver.contc                 C   s   | j ddd� | j ¡  dS )zgdb command ~= 'quit'z	-gdb-exitrM   N)rK   r&   Ú	terminater-   r   r   r   Úquity   s    zGdbMIDriver.quitc                 C   s   | j ddd� dS )zgdb command ~= 'next'z
-exec-nextú\*stopped,.*\r\nrM   NrN   r-   r   r   r   Únext~   s    zGdbMIDriver.nextc                 C   s   | j ddd� dS )zgdb command ~= 'step'z
-exec-steprS   rM   NrN   r-   r   r   r   Ústep‚   s    zGdbMIDriver.stepc                 C   sš   |dk	r|dk	rt dƒ‚d}|dk	r4|d|› d�7 }|dk	rbt|tƒsJt‚|d| j› d|› d�7 }|dk	rˆt|tƒsxt‚|d|› d�7 }| j|d	d
� dS )zgdb command ~= 'break'Nz%Can only supply one of line or symbolz-break-insert z-c "z" z-f ú:r   z\^donerM   )r8   Ú
isinstanceÚintr!   r   ÚstrrK   )r   ÚlineÚsymbolÚ	conditionÚbpr   r   r   Úset_breakpoint†   s    zGdbMIDriver.set_breakpointc                 C   sh   |   d¡ |  d¡ |dk	r<t|tƒs*t‚|  d|› d�¡ |dk	rdt|tƒsRt‚|  d|› d�¡ dS )z%Checks that a breakpoint has been hitrS   z!*stopped,reason="breakpoint-hit",Nzbkptno="ú"zline=")r:   rC   rW   rX   r!   )r   ÚnumberrZ   r   r   r   Úcheck_hit_breakpoint•   s    

z GdbMIDriver.check_hit_breakpointé   r   c                 C   sN   |||fD ]}t |tƒr |dks
t‚q
d|› d|› d|› �}| j|dd� dS )zgdb command ~= 'info args'©r   rb   é   z-stack-list-arguments r   ú\^done,.*\r\nrM   N©rW   rX   r!   rK   )r   Úprint_valuesZ	low_frameZ
high_frameÚxr*   r   r   r   Ústack_list_arguments    s    z GdbMIDriver.stack_list_argumentsc                 C   s2   t |tƒr|dkst‚d|› �}| j|dd� dS )zgdb command ~= 'info locals'rc   z-stack-list-variables re   rM   Nrf   )r   rg   r*   r   r   r   Ústack_list_variables§   s    
z GdbMIDriver.stack_list_variablesc                 C   sD   |dkrt dƒ‚|dkr t dƒ‚d|› d|› d�}| j|dd� dS )	z!gdb command ~= 'interpreter-exec'Nzinterpreter cannot be Nonezcommand cannot be Nonez-interpreter-exec z "r_   z\^(done|error).*\r\nrM   )r8   rK   )r   ÚinterpreterrJ   r*   r   r   r   r0   ­   s    zGdbMIDriver.interpreter_execc                 C   s   d}| j |dd� d S )Nz-list-featuresre   rM   rN   r)   r   r   r   Ú_list_features_raw¶   s    zGdbMIDriver._list_features_rawc                 C   sh   |   ¡  | jj}| d¡}t d|¡}|dk	s6tdƒ‚| ¡ }t|ƒdksRtdƒ‚|d  	dd	¡ 
d
¡S )zXNo equivalent gdb command? Returns a list of supported gdb
           features.
        r;   z.*features=\[(.*)\].*Nz"No match found for features stringrb   z$Invalid number of match groups foundr   r_   rI   ú,)rl   r&   r?   r2   rE   rF   r!   ÚgroupsÚlenÚreplaceÚsplit)r   rA   rB   ÚmÚgr   r   r   r,   º   s    
zGdbMIDriver.list_features)Fr   N)rI   )NNN)NN)rb   r   r   )rb   )NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r.   r3   r:   rC   rH   rK   rO   rP   rR   rT   rU   r^   ra   ri   rj   r0   rl   r,   r   r   r   r   r   &   s*   
	





	r   )rw   r   rE   r   ZunittestZ
numba.corer   Znumba.misc.gdb_hookr   Znumba.misc.numba_gdbinfor   r
   Z	_gdb_infoZpy_verÚ
startswithZ_GDB_HAS_PY3Ú	ExceptionZ_msgZ
skipUnlessZ	needs_gdbZneeds_gdb_py3r#   r   ÚImportErrorZskip_unless_pexpectÚobjectr   r   r   r   r   Ú<module>   s6   


