a
    –§»aK  ã                   @   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 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v Z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)ÚgdbÚgdb_initÚgdb_breakpoint)ÚoverloadÚ	intrinsicÚlinuxÚdarwinZbsdc                  C   s²   t stdƒ‚tj} tj | ¡r*tj | ¡s<d}t|tj ƒ‚tj tj	ddddd¡}tj |¡}|r®t
|dƒ�}| ¡  ¡ }W d   ƒ n1 s�0    Y  |d	kr®d
}t|ƒ‚d S )Nz2gdb 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.pydata.org/numba-doc/latest/reference/envvars.htmlÚprocÚsysÚkernelZyamaZptrace_scopeÚrtÚ0a  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.)Ú
_unix_likeÚRuntimeErrorr   Ú
GDB_BINARYÚosÚpathÚexistsÚisfileÚjoinÚsepÚopenÚreadlineÚstrip)ZgdblocÚmsgZptrace_scope_fileZhas_ptrace_scopeÚfÚvalue© r"   úM/home/yuhw/wc/pochoir/venv/lib/python3.9/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#   Úimpl9   s    zhook_gdb.<locals>.impl©r$   Úgen_gdb_impl©r'   r)   r"   r(   r#   Úhook_gdb4   s    
r-   c                     s    t ƒ  t| dƒ‰ ‡ fdd„}|S )NFc                     s
   ˆ ƒ  d S r%   r"   r&   r(   r"   r#   r)   C   s    zhook_gdb_init.<locals>.implr*   r,   r"   r(   r#   Úhook_gdb_init>   s    
r.   Fc           (   	      s¼  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¡ | 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 d   ƒ n1 �s,0    Y  | |tƒ ¡}| d||dƒ¡} |j| dd��& d}ˆ j |t|f¡ W d   ƒ n1 �s’0    Y  | d||
¡}!| |!¡�ò\}"}#|"�x t  |	d ¡}$|j||
gdd�}%|j||
gdd�}&t |d |¡ |%|%|&|f}'|'t|ƒ |$f }'| ||'¡ W d   ƒ n1 �s>0    Y  |#�: | ||d!ƒf¡ |du �rx| |tƒ ¡ W d   ƒ n1 �sŽ0    Y  W d   ƒ n1 �s®0    Y  d S )"Né   é    r   é   )Úsizeé   z%dÚattachz-xzcmdlang.gdbz-exÚcc                 S   s   g | ]
}|j ‘qS r"   )Zliteral_value©Ú.0Úxr"   r"   r#   Ú
<listcomp>e   ó    z$init_gdb_codegen.<locals>.<listcomp>c                    s   g | ]}ˆ   ˆ|¡‘qS r"   )Úinsert_const_stringr6   ©ÚcgctxÚmodr"   r#   r9   f   r:   Ú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Ú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Úfntyr?   r@   rA   rB   rC   Ú
breakpointZ
parent_pidZ
pidstr_ptrZpid_valÚstatZinvalid_writer   Z	child_pidZfork_failedZis_childZthenÚorelseZnullptrZgdb_str_ptrZattach_str_ptrÚbufr"   r<   r#   Úinit_gdb_codegenH   s�    

ÿÿ
ÿ22ÿÿÿü,
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)rV   )r^   Úget_constantr   Únone)r=   rS   rT   r'   ©rU   rV   r"   r#   Úcodegen·   s    ÿz3gen_gdb_impl.<locals>.gdb_internal.<locals>.codegen©r   Úvoid©ZtyctxZfunction_sigrb   ra   r"   r#   Úgdb_internal³   s    z"gen_gdb_impl.<locals>.gdb_internal©r   )rU   rV   rf   r"   ra   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"   rh   r#   Úhook_gdb_breakpoint¿   s
    rl   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 )NrD   )rH   r   rK   rN   rL   r   rM   rO   r_   r   r`   )r=   rS   rT   r'   r>   rY   rZ   r"   r"   r#   rb   Ò   s    ÿz1gen_bp_impl.<locals>.bp_internal.<locals>.codegenrc   re   r"   r"   r#   Úbp_internalÎ   s    z gen_bp_impl.<locals>.bp_internalrg   )rm   r"   r"   r#   rk   Í   s    
rk   )F)r   r   Zllvmliter   Z
numba.corer   r   r   r   Znumbar   r   r	   Znumba.core.extendingr
   r   r   ÚdirnameÚ__file__rJ   ri   Ú	_platformrj   r   r$   r-   r.   r^   r+   rl   rk   r"   r"   r"   r#   Ú<module>   s.   
ÿþ"
	

 ÿ
j
