U
    ,dw!                     @   s   d dl Z d dlZd dlmZ d dlmZmZmZmZm	Z	 d dl
mZmZmZ d dlmZmZ e jeZejZedpedpdekZdd
dZeedd Zeedd ZdddZdd Zeedd Zdd ZdS )    N)ir)typesutilsconfigcgutilserrors)gdbgdb_initgdb_breakpoint)overload	intrinsiclinuxdarwinZbsdTc              	   C   s   t sd}t|tj}tj|r0tj|sBd}t	|tj tj
tjddddd}tj|}|rt|d}|  }W 5 Q R X | r|d	krd
}t	|dS )aF  
    Set need_ptrace_attach to True/False to indicate whether the ptrace attach
    permission is needed for this gdb use case. Mode 0 (classic) or 1
    (restricted ptrace) is required if need_ptrace_attach is True. See:
    https://www.kernel.org/doc/Documentation/admin-guide/LSM/Yama.rst
    for details on the modes.
    z2gdb support is only available on unix-like systemszIs gdb present? Location specified (%s) does not exist. The gdb binary location can be set using Numba configuration, see: https://numba.readthedocs.io/en/stable/reference/envvars.htmlprocsyskernelZyamaZptrace_scopert)01a  gdb can launch but cannot attach to the executing program because ptrace permissions have been restricted at the system level by the Linux security module 'Yama'.

Documentation for this module and the security implications of making changes to its behaviour can be found in the Linux Kernel documentation https://www.kernel.org/doc/Documentation/admin-guide/LSM/Yama.rst

Documentation on how to adjust the behaviour of Yama on Ubuntu Linux with regards to 'ptrace_scope' can be found here https://wiki.ubuntu.com/Security/Features#ptrace.N)
_unix_liker   ZNumbaRuntimeErrorr   
GDB_BINARYospathexistsisfileRuntimeErrorjoinsepopenreadlinestrip)Zneed_ptrace_attachmsgZgdblocZptrace_scope_fileZhas_ptrace_scopefvalue r$   7/tmp/pip-unpacked-wheel-eu7e0c37/numba/misc/gdb_hook.py_confirm_gdb   s"    
r&   c                     s    t   t| d  fdd}|S )NTc                     s
      d S Nr$   argsZgdbimplr$   r%   implA   s    zhook_gdb.<locals>.implr&   gen_gdb_implr)   r+   r$   r*   r%   hook_gdb<   s    
r/   c                     s    t   t| d  fdd}|S )NFc                     s
      d S r'   r$   r(   r*   r$   r%   r+   K   s    zhook_gdb_init.<locals>.implr,   r.   r$   r*   r%   hook_gdb_initF   s    
r0   Fc           (   
      sj  t d}t d}t tj}t t d}	|d}
|jtj||dd}tj||dd} d} t	j
} d}g }|d	tjtd
g |ddg tdd |D rt||dd |D   fdd|D }t |t }t|d}t j||	||	fdd}t|d}t |t }t|d}t j||	|	fdd}t|d}t ||f}t|d}t t  t }t|d}||t }||| |j||
gdd}||}||||d||f}|d||d}|j|dd d} j|t|f W 5 Q R X ||t }|d||d} |j| dd d } j|t|f W 5 Q R X |d||
}!| |!\}"}#|"n t !|	d }$|j||
gdd}%|j||
gdd}&t"|d!| |%|%|&|f}'|'t| |$f }'|||' W 5 Q R X |#0 |||d"f |dkrR||t  W 5 Q R X W 5 Q R X d S )#N       r      )size   z%dattachz-xzcmdlang.gdbz-excc                 S   s   g | ]}t |tj qS r$   )
isinstancer   ZStringLiteral.0xr$   r$   r%   
<listcomp>m   s     z$init_gdb_codegen.<locals>.<listcomp>c                 S   s   g | ]
}|j qS r$   )Zliteral_valuer9   r$   r$   r%   r<   o   s     c                    s   g | ]}  |qS r$   )insert_const_stringr9   cgctxmodr$   r%   r<   p   s     getpidT)Zvar_argsnprintfforkexeclsleepnumba_gdb_breakpoint)Zinbounds>F)Zlikelyz8Internal error: `snprintf` buffer would have overflowed.z==zInternal error: `fork` failed.zAttaching to PID: %s

   )#r   ZIntTyper   ZMACHINE_BITSZPointerTypemoduler   Zalloca_oncer=   r   r   extendr   r   r   _pathanyr   ZRequireLiteralValueFunctionTypetupleget_or_insert_functionVoidTypecallstoreZgeploadZicmp_signedZif_thenZ	call_convZreturn_user_excr   Zif_elseConstantZprintf)(r?   builder	signaturer)   
const_argsdo_breakZint8_tZint32_tZintp_tZchar_ptrZ	zero_i32tpidZpidstrZintfmtZgdb_strZ
attach_strnew_argsZcmdlangfntyrA   rB   rC   rD   rE   
breakpointZ
parent_pidZ
pidstr_ptrZpid_valstatZinvalid_writer!   Z	child_pidZfork_failedZis_childZthenZorelseZnullptrZgdb_str_ptrZattach_str_ptrbufr$   r>   r%   init_gdb_codegenP   s    


  
       
r`   c                    s   t  fdd}|S )Nc                    s   t  } fdd}||fS )Nc                    s"   t | ||| d | tjd S )N)rY   )r`   get_constantr   none)r?   rV   rW   r)   rX   rY   r$   r%   codegen   s    z3gen_gdb_impl.<locals>.gdb_internal.<locals>.codegenr   voidZtyctxZfunction_sigrd   rc   r$   r%   gdb_internal   s    z"gen_gdb_impl.<locals>.gdb_internalr   )rX   rY   rh   r$   rc   r%   r-      s    r-   c                     s*   t jdstdt   fdd} | S )z4
    Adds the Numba break point into the source
    r   zgdb is only available on linuxc                      s
      d S r'   r$   r$   Zbp_implr$   r%   r+      s    z!hook_gdb_breakpoint.<locals>.impl)r   platform
startswithr   gen_bp_impl)r+   r$   rj   r%   hook_gdb_breakpoint   s
    rn   c                  C   s   t dd } | S )Nc                 S   s   t  }dd }||fS )Nc                 S   sB   |j }tt t }t||d}||t  | t	j
d S )NrF   )rJ   r   rN   rQ   rO   r   rP   rR   ra   r   rb   )r?   rV   rW   r)   r@   r\   r]   r$   r$   r%   rd      s    z1gen_bp_impl.<locals>.bp_internal.<locals>.codegenre   rg   r$   r$   r%   bp_internal   s    z gen_bp_impl.<locals>.bp_internalri   )ro   r$   r$   r%   rm      s    
rm   )T)F) r   r   Zllvmliter   Z
numba.corer   r   r   r   r   Znumbar   r	   r
   Znumba.core.extendingr   r   r   dirname__file__rL   rk   	_platformrl   r   r&   r/   r0   r`   r-   rn   rm   r$   r$   r$   r%   <module>   s.   

*
	

 
l
