U
    Ô¾|ew!  ã                   @   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¡pˆe d¡pˆd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 |¡}|r¤t|dƒ�}| ¡  ¡ }W 5 Q R X | r¤|d	kr¤d
}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 systemsz¸Is 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   Ú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%   úP/var/www/website-v5/atlas_env/lib/python3.8/site-packages/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    
r0   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    
r1   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 ƒƒrÊt |¡‚| 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   ÚStringLiteral©Ú.0Úxr%   r%   r&   Ú
<listcomp>m   s     z$init_gdb_codegen.<locals>.<listcomp>c                 S   s   g | ]
}|j ‘qS r%   )Úliteral_valuer;   r%   r%   r&   r>   o   s     c                    s   g | ]}ˆ   ˆ|¡‘qS r%   )Úinsert_const_stringr;   ©ÚcgctxÚmodr%   r&   r>   p   s     ÚgetpidT)Úvar_argÚsnprintfÚforkÚexeclÚsleepÚnumba_gdb_breakpoint)Úinboundsú>F)Úlikelyz8Internal error: `snprintf` buffer would have overflowed.z==éÿÿÿÿzInternal error: `fork` failed.zAttaching to PID: %s
é
   )#r   ÚIntTyper   ÚMACHINE_BITSÚPointerTypeÚmoduler   Úalloca_oncer@   r   r   Úextendr   r   r   Ú_pathÚanyr   ÚRequireLiteralValueÚFunctionTypeÚtupleÚget_or_insert_functionÚVoidTypeÚcallÚstoreÚgepÚloadÚicmp_signedÚif_thenÚ	call_convÚreturn_user_excr   Úif_elseÚConstantÚprintf)(rB   ÚbuilderÚ	signaturer*   Ú
const_argsÚdo_breakÚint8_tÚint32_tÚintp_tZchar_ptrZ	zero_i32tÚpidZpidstrZintfmtZgdb_strZ
attach_strÚnew_argsZcmdlangÚfntyrD   rF   rG   rH   rI   Ú
breakpointZ
parent_pidZ
pidstr_ptrZpid_valÚstatZinvalid_writer"   Z	child_pidZfork_failedZis_childÚthenÚorelseZnullptrZgdb_str_ptrZattach_str_ptrÚbufr%   rA   r&   Úinit_gdb_codegenP   s¦    


  ÿÿ
 ÿ  ÿ  ÿ  ÿü
rw   c                    s   t ‡ ‡fdd„ƒ}|S )Nc                    s   t  ¡ }‡ ‡fdd„}||fS )Nc                    s"   t | |||ˆ ˆd� |  tjd ¡S )N)rk   )rw   Úget_constantr   Únone)rB   rh   ri   r*   ©rj   rk   r%   r&   ÚcodegenÁ   s    ÿz3gen_gdb_impl.<locals>.gdb_internal.<locals>.codegen©r   Úvoid©ÚtyctxZfunction_sigr{   rz   r%   r&   Úgdb_internal½   s    z"gen_gdb_impl.<locals>.gdb_internal©r   )rj   rk   r€   r%   rz   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%   r‚   r&   Úhook_gdb_breakpointÉ   s
    r†   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 )NrJ   )rS   r   rY   r\   rZ   r   r[   r]   rx   r   ry   )rB   rh   ri   r*   rC   rq   rr   r%   r%   r&   r{   Ü   s    ÿz1gen_bp_impl.<locals>.bp_internal.<locals>.codegenr|   r~   r%   r%   r&   Úbp_internalØ   s    z gen_bp_impl.<locals>.bp_internalr�   )r‡   r%   r%   r&   r…   ×   s    
r…   )T)F) r   r   Úllvmliter   Ú
numba.corer   r   r   r   r   Únumbar   r	   r
   Únumba.core.extendingr   r   r   ÚdirnameÚ__file__rV   rƒ   Ú	_platformr„   r   r'   r0   r1   rw   r.   r†   r…   r%   r%   r%   r&   Ú<module>   s.   
ÿþ
*
	

 ÿ
l
