B
     \                 @   s   d dl mZmZ 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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
 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 )    )print_functionabsolute_importN)ir)gdbgdb_initgdb_breakpointtypesutilscgutilsconfig)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|}|rt
|d}|  }W d Q R X |d	krd
}t|d S )Nz2gdb 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: http://numba.pydata.org/numba-doc/latest/reference/envvars.htmlprocsysZkernelZyamaZptrace_scopeZ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"   5lib/python3.7/site-packages/numba/targets/gdb_hook.py_confirm_gdb   s    r$   c                 s    t   t| d  fdd}|S )NTc                 s
      d S )Nr"   )args)gdbimplr"   r#   impl:   s    zhook_gdb.<locals>.impl)r$   gen_gdb_impl)r%   r'   r"   )r&   r#   hook_gdb5   s    
r)   c                 s    t   t| d  fdd}|S )NFc                 s
      d S )Nr"   )r%   )r&   r"   r#   r'   D   s    zhook_gdb_init.<locals>.impl)r$   r(   )r%   r'   r"   )r&   r#   hook_gdb_init?   s    
r*   Fc       (   
      sB  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 }|d}t j||	||	fdd}|d}t |t }|d}t j||	|	fdd}|d}t ||f}|d}t t  t }|d}||t }||| |j||
gdd}||}||||d||f}|d||d}|j|dd d} j|t|f W d Q R X ||t }|d||d} |j| dd d} j|t|f W d Q R X |d||
}!||!\}"}#|"n t |	d }$|j||
gdd}%|j||
gdd}&t|d | |%|%|&|f}'|'t| |$f }'|||' W d Q R X |#0 |||d!f |dkr*||t  W d Q R X W d Q R X d S )"N       r      )size   z%dZattachz-xzcmdlang.gdbz-excc             S   s   g | ]
}|j qS r"   )Zliteral_value).0xr"   r"   r#   
<listcomp>f   s    z$init_gdb_codegen.<locals>.<listcomp>c                s   g | ]}  |qS r"   )insert_const_string)r1   r2   )cgctxmodr"   r#   r3   g   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_oncer4   r   r   extendr   r   r   _pathFunctionTypetupleget_or_insert_functionVoidTypecallZstoreZgeploadZicmp_signedZif_thenZ	call_convZreturn_user_excr   Zif_elseZConstantZprintf)(r5   builder	signaturer%   
const_argsdo_breakZint8_tZint32_tZintp_tZchar_ptrZ	zero_i32tpidZpidstrZintfmtZgdb_strZ
attach_strZnew_argsZcmdlangfntyr7   r8   r9   r:   r;   
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_ptrZbufr"   )r5   r6   r#   init_gdb_codegenI   s    




rQ   c                s   t  fdd}|S )Nc                s   t  } fdd}||fS )Nc                s"   t | ||| d | tjd S )N)rL   )rQ   get_constantr   none)r5   rI   rJ   r%   )rK   rL   r"   r#   codegen   s    z3gen_gdb_impl.<locals>.gdb_internal.<locals>.codegen)r   void)tyctxfunction_sigrT   )rK   rL   r"   r#   gdb_internal   s    z"gen_gdb_impl.<locals>.gdb_internal)r   )rK   rL   rX   r"   )rK   rL   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 )Nr"   r"   )bp_implr"   r#   r'      s    z!hook_gdb_breakpoint.<locals>.impl)r   platform
startswithr   gen_bp_impl)r'   r"   )rY   r#   hook_gdb_breakpoint   s
    r]   c              C   s   t dd } | S )Nc             S   s   t  }dd }||fS )Nc             S   s@   |j }tt t }||d}||t  | tj	d S )Nr<   )
r@   r   rC   rF   rD   rE   rG   rR   r   rS   )r5   rI   rJ   r%   r6   rN   rO   r"   r"   r#   rT      s    z1gen_bp_impl.<locals>.bp_internal.<locals>.codegen)r   rU   )rV   rW   rT   r"   r"   r#   bp_internal   s    z gen_bp_impl.<locals>.bp_internal)r   )r^   r"   r"   r#   r\      s    r\   )F)!Z
__future__r   r   r   r   Zllvmliter   Znumbar   r   r   r   r	   r
   r   Znumba.extendingr   r   r   dirname__file__rB   rZ   Z	_platformr[   r   r$   r)   r*   rQ   r(   r]   r\   r"   r"   r"   r#   <module>   s"   $

"

i