o
    i                     @   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)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   
GDB_BINARY_gdb_binarysys
executable_python_debug
_file_name_timeout
_init_cmds_drive)self	file_namedebugtimeout	init_cmdsmsg r   T/home/ubuntu/veenaModal/venv/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python)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' from)r1   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   r8   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(   afterr5   )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@   rA   N)r(   rB   r5   
splitlinesrematch)r   rC   rD   rE   done_strfoundr   r   r   assert_regex_outputd   s
   
zGdbMIDriver.assert_regex_output c                 C   s   | j | | | d S )N)r(   sendliner=   )r   commandr8   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r8   NrP   r0   r   r   r   runq      zGdbMIDriver.runc                 C   rQ   )zgdb command ~= 'continue'z-exec-continuerR   rS   NrT   r0   r   r   r   contu   rV   zGdbMIDriver.contc                 C   s   | j ddd | j  dS )zgdb command ~= 'quit'z	-gdb-exitrS   N)rP   r(   	terminater0   r   r   r   quity   s   zGdbMIDriver.quitc                 C   rQ   )zgdb command ~= 'next'z
-exec-next\*stopped,.*\r\nrS   NrT   r0   r   r   r   next~   rV   zGdbMIDriver.nextc                 C   rQ   )zgdb command ~= 'step'z
-exec-steprZ   rS   NrT   r0   r   r   r   step   rV   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\^donerS   )r;   
isinstanceintr   strrP   )r   linesymbol	condition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 hitrZ   z!*stopped,reason="breakpoint-hit",Nzbkptno=""zline=")r=   rF   r^   r_   )r   numberra   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   ri      z-stack-list-arguments r    \^done,.*\r\nrS   Nr^   r_   rP   )r   print_values	low_frame
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'rj   z-stack-list-variables rl   rS   Nrm   )r   rn   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 "rf   z\^(done|error).*\r\nrS   )r;   rP   )r   interpreterrO   r-   r   r   r   r3      s   zGdbMIDriver.interpreter_execc                 C   s   d}| j |dd d S )Nz-list-featuresrl   rS   rT   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 stringri   z$Invalid number of match groups foundr   rf   rM   ,)
ru   r(   rB   r5   rH   rI   groupslenreplacesplit)r   rD   rE   mgr   r   r   r/      s   
zGdbMIDriver.list_features)Fr   N)rM   )NNN)NN)ri   r   r   )ri   )__name__
__module____qualname____doc__r   r   r1   r6   r=   rF   rL   rP   rU   rW   rY   r[   r\   re   rh   rr   rs   r3   ru   r/   r   r   r   r   r   &   s,    

	




	r   )r   r!   rH   r   unittest
numba.corer   numba.misc.gdb_hookr   numba.misc.numba_gdbinfor   r   	_gdb_infopy_ver
startswith_GDB_HAS_PY3	Exception_msg
skipUnless	needs_gdbneeds_gdb_py3r%   r	   ImportErrorskip_unless_pexpectobjectr   r   r   r   r   <module>   s<    
