o
    'ѹg                     @   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yC   dZdZY nw d
ZeeeZdZeeeZzddlZdZW n eyg   dZY nw 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d3d*d+Zd,d- Zd.d/ ZdS )6GdbMIDriverzs
    Driver class for the GDB machine interface:
    https://sourceware.org/gdb/onlinedocs/gdb/GDB_002fMI.html
    Fx   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   [/Users/admin/.pyenv/versions/3.10.0/lib/python3.10/site-packages/numba/tests/gdb_support.py__init__+   s   zGdbMIDriver.__init__c                 C   st   t j| js	J | jddg}| jdur|t| j7 }|d| j| jg7 }t	d
|| _| jr8tjj| j_dS dS )z0This function sets up the caputured gdb instancez--interpretermiNz--args )ospathisfiler   r   r   listr   pexpectspawnjoin	_capturedr   r   stdoutbufferlogfiler   cmdr   r   r   r   <   s   
zGdbMIDriver._drivec                 C   s   d|   v S )z_Returns True if the underlying gdb implementation has python support
           False otherwisepythonN)list_featuresr   r   r   r   supports_pythonG   s   zGdbMIDriver.supports_pythonc                 C   s,   |   sdS d}| d| d| jj v S )zReturns 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' fromN)r.   interpreter_execr%   beforedecoder)   r   r   r   supports_numpyL   s
   zGdbMIDriver.supports_numpyc              
   C   sL   z| j j|| jd W d S  tjjy% } zd| d}t||d }~ww )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}||v sJ d| d| ddS )z=Asserts that the current output string contains the expected.utf-8decoded=

expected=)N)r%   afterr2   )r   expectedoutputdecodedr   r   r   assert_output^   s   
"zGdbMIDriver.assert_outputc                 C   sH   | j j}|d}| d }t||}|s"J 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match)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      | j ddd dS )zgdb command ~= 'run'z	-exec-run\^running.*\r\nr5   NrK   r-   r   r   r   runq      zGdbMIDriver.runc                 C   rL   )zgdb command ~= 'continue'z-exec-continuerM   rN   NrO   r-   r   r   r   contu   rQ   zGdbMIDriver.contc                 C   s   | j ddd | j  dS )zgdb command ~= 'quit'z	-gdb-exitrN   N)rK   r%   	terminater-   r   r   r   quity   s   zGdbMIDriver.quitc                 C   rL   )zgdb command ~= 'next'z
-exec-next\*stopped,.*\r\nrN   NrO   r-   r   r   r   next~   rQ   zGdbMIDriver.nextc                 C   rL   )zgdb command ~= 'step'z
-exec-steprU   rN   NrO   r-   r   r   r   step   rQ   zGdbMIDriver.stepc                 C   s   |dur|durt dd}|dur|d| d7 }|dur1t|ts%J |d| j d| d7 }|durDt|ts<J |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\^donerN   )r8   
isinstanceintr   strrK   )r   linesymbol	conditionZbpr   r   r   set_breakpoint   s   zGdbMIDriver.set_breakpointc                 C   sl   |  d | d |durt|tsJ | d| d |dur4t|ts)J | d| d dS dS )z%Checks that a breakpoint has been hitrU   z!*stopped,reason="breakpoint-hit",Nzbkptno=""zline=")r:   rC   rY   rZ   )r   numberr\   r   r   r   check_hit_breakpoint   s   

z GdbMIDriver.check_hit_breakpoint   r   c                 C   sN   |||fD ]}t |tr|dv sJ qd| d| d| }| j|dd dS )zgdb command ~= 'info args'r   rc      z-stack-list-arguments r   \^done,.*\r\nrN   NrY   rZ   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v sJ d| }| j|dd dS )zgdb command ~= 'info locals'rd   z-stack-list-variables rf   rN   Nrg   )r   rh   r*   r   r   r   stack_list_variables   s   
z GdbMIDriver.stack_list_variablesc                 C   sD   |du rt d|du 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\nrN   )r8   rK   )r   interpreterrJ   r*   r   r   r   r0      s   zGdbMIDriver.interpreter_execc                 C   s   d}| j |dd d S )Nz-list-featuresrf   rN   rO   r)   r   r   r   _list_features_raw   s   zGdbMIDriver._list_features_rawc                 C   sh   |    | jj}|d}td|}|dusJ d| }t|dks)J 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 stringrc   z$Invalid number of match groups foundr   r`   rI   ,)
rm   r%   r?   r2   rE   rF   groupslenreplacesplit)r   rA   rB   mgr   r   r   r,      s   
zGdbMIDriver.list_features)Fr   N)rI   )NNN)NN)rc   r   r   )rc   )__name__
__module____qualname____doc__r   r   r.   r3   r:   rC   rH   rK   rP   rR   rT   rV   rW   r_   rb   rj   rk   r0   rm   r,   r   r   r   r   r   &   s,    

	




	r   )rx   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	Exception_msgZ
skipUnlessZ	needs_gdbZneeds_gdb_py3r"   r   ImportErrorZskip_unless_pexpectobjectr   r   r   r   r   <module>   s<    
