U
    ‡ª/e&;  ã                   @   s  d dl mZmZmZmZmZmZmZmZm	Z	m
Z
 d dlmZmZ d dlmZmZ d dlZd dlZd dlZd dlmZ d dlZd dlZejZG dd„ dƒZdd	„ Zd
d„ Zdd„ Ze ¡ Z dd„ Z!dd„ Z"dd„ Z#dd„ Z$e
ƒ Z%dd„ Z&dd„ Z'dd„ Z(ddd„Z)dS ) é    )
Ú	get_frameÚ
IS_CPYTHONÚIS_64BIT_PROCESSÚ
IS_WINDOWSÚIS_LINUXÚIS_MACÚDebugInfoHolderÚLOAD_NATIVE_LIB_FLAGÚENV_FALSE_LOWER_VALUESÚForkSafeLock)ÚthreadÚ	threading)Ú	pydev_logÚpydev_monkeyN)ÚStringIOc                   @   s    e Zd ZdZdZdZdZi ZdS )ÚTracingFunctionHolderzhThis class exists just to keep some variables (so that we don't keep them in the global namespace).
    NTé   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú_original_tracingÚ_warnÚ_traceback_limitÚ_warnings_shown© r   r   úK/tmp/pip-unpacked-wheel-dg3irmqq/debugpy/_vendored/pydevd/pydevd_tracing.pyr      s
   r   c                  C   s4   t  ¡ } tƒ }tj| d | d | d |d | ¡ S )Nr   r   é   )Úfile)ÚsysÚexc_infor   Ú	tracebackÚprint_exceptionÚgetvalue)r    Úsr   r   r   Úget_exception_traceback_str   s    r%   c                 C   s<   d}t jr8tƒ }| d¡ tj| t j|d || ¡  }|S )Nz¤
If this is needed, please check: 
http://pydev.blogspot.com/2007/06/why-cant-pydev-debugger-work-with.html
to see how to restore the debug tracing back correctly.
zCall Location:
)ÚfÚlimitr   )r   r   r   Úwriter!   Úprint_stackr#   )ÚframeÚmsgr$   r   r   r   Ú_get_stack_str    s    
r,   c                 C   sÂ   t jr®tƒ }|d k	r®|jd k	r®tj |jjj 	¡ ¡d }| 
d¡rbd|krb| d kr®t d¡ d S nL| 
d¡s®ddt|jƒ  }|t jkr®dt j|< tj d	|f ¡ tj ¡  t jr¾t  | ¡ d S )
Nr   Z
threadpoolZgeventz6Disabled internal sys.settrace from gevent threadpool.)r   Zpydevd_tracingz’
PYDEV DEBUGGER WARNING:
sys.settrace() should not be used when the debugger is being used.
This may cause the debugger to stop working correctly.z%sr   z%s
)r   r   r   Úf_backÚosÚpathÚsplitextÚf_codeÚco_filenameÚlowerÚendswithr   Údebugr,   r   r   Ústderrr(   Úflushr   )Útracing_funcr*   ÚfilenameÚmessager   r   r   Ú_internal_set_trace/   s*    
ÿýÿ


r;   c                 C   sB   | t _| d k	r*t| t ¡ gdddkr*d S tjp4tj}|| ƒ d S )NF)Úthread_identsÚcreate_dummy_threadr   )	Ú_last_tracing_func_thread_localr8   Úset_trace_to_threadsr   Ú	get_identr   r   r   Úsettrace)r8   Ú	set_tracer   r   r   ÚSetTraceS   s    rC   c                  C   s.   z
t j} W n tk
r    Y d S X t| ƒ d S ©N)r>   r8   ÚAttributeErrorrC   )r8   r   r   r   Úreapply_settracee   s
    
rF   c                   C   s   t jd krtjt _tt_d S rD   )r   r   r   rA   r;   r   r   r   r   Úreplace_sys_set_trace_funcn   s    
rG   c                   C   s   t jd k	rt jt_d t _d S rD   )r   r   r   rA   r   r   r   r   Úrestore_sys_set_trace_funct   s    
rH   c               
   C   st   zt jW S  tk
r   Y nX tH zt jW W  5 Q R £ S  tk
rL   Y nX tƒ } | t _| W  5 Q R £ S Q R X d S rD   )Ú_load_python_helper_libZ__lib__rE   Ú_lockÚ _load_python_helper_lib_uncached)Úlibr   r   r   rI   }   s    rI   c            	         sB  t j t j t¡d¡} d}tr6t j dt j dd¡¡}|sTt 	¡ }|sTt
 d¡ d S trfd‰ d‰d‰n6trxd	‰ d‰d‰n$trŠd
‰ d‰d‰nt
 dtj¡ d S | ¡ dkrÈz‡ fdd„t  | ¡D ƒ}W n$   tjdkrät
 d| ¡ Y d S X d| ˆ  }d| ˆ  }d }||kr"t j | |¡}nxtrB||krBt j | |¡}nXt|ƒdkrdt j | |d ¡}n6‡ ‡‡fdd„|D ƒ}t|ƒdkršt j | |d ¡}|d kr¸t
 d||| ¡ d S t
 d||¡ nXtrÔˆ}nˆ}tsätrêd}ntröd}nt
 dtj¡ d S t j | d||ˆ f ¡}t j |¡s>t
 d|¡ d S |S )NZpydevd_attach_to_processÚ ÚPROCESSOR_ARCHITEW6432ÚPROCESSOR_ARCHITECTUREz.platform.machine() did not return valid value.z.dllÚamd64Úx86z.soz.dylibÚx86_64z2Unable to set trace to all threads in platform: %s)rP   rQ   rR   Úi386rQ   c                    s$   g | ]}|  d ¡r| ˆ ¡r|‘qS )Úattach_)Ú
startswithr4   ©Ú.0Úname)Ú	extensionr   r   Ú
<listcomp>Á   s     
 
 z2get_python_helper_lib_filename.<locals>.<listcomp>r   zError listing dir: %srT   Zattach_linux_r   c                    s&   g | ]}|  ˆˆ  ˆˆ  f¡s|‘qS r   )r4   rV   ©rY   Z	suffix_32Z	suffix_64r   r   rZ   Ö   s      zMUnable to set trace to all threads in arch: %s (did not find a %s lib in %s).zUsing %s lib in arch: %s.z%s%s%szExpected: %s to exist.)r.   r/   ÚjoinÚdirnameÚ__file__r   ÚenvironÚgetÚplatformÚmachiner   Úinfor   r   r   r3   Úlistdirr   ÚDEBUG_TRACE_LEVELÚ	exceptionÚlenr   ÚexistsÚcritical)	ZlibdirÚarchÚfoundZexpected_nameZexpected_name_linuxr9   ÚfilteredÚsuffixÚprefixr   r[   r   Úget_python_helper_lib_filenameŽ   s‚    




  þro   c                  C   s   t r(tjd d… dks(ttdƒs(ttkr6t d¡ d S z0tƒ } | d krLW d S t	j
 | ¡}t d¡ |W S    tjdkr„t d| ¡ Y d S X d S )Nr   )é   é   Úgettotalrefcountz4Helper lib to set tracing to all threads not loaded.z=Successfully Loaded helper lib to set tracing to all threads.r   zError loading: %s)r   r   Úversion_infoÚhasattrr	   r
   r   rc   ro   ÚctypesÚpydllÚLoadLibraryr   re   rf   )r9   rL   r   r   r   rK   ü   s$    ÿÿ


rK   Tc              
      s   | d k	st ‚d}|d kr\tt ¡  ¡ ƒ}t ¡ D ]*}t|ddƒrN| |j	¡ q0| 
|j	¡ q0t ¡ }tj |¡}||kr¢t|ƒdkr¢t|ƒ}| |¡ | d|¡ |D ]r‰|r*ˆtjkr*G ‡fdd„dtjƒ}|ƒ }tj|_ˆ|krî|}tj0 |tjˆ< |tj|< |j	ˆkr t d¡ W 5 Q R X d}t ¡ ‰ˆ ¡  ‡ fdd	„‰ ‡ ‡fd
d„}	t t¡}
|
|	dƒ ˆ ¡  d ‰tjp„tj}tƒ }|d kr¦t  d¡ d}q¦z4| !t" #|¡t" $|¡t" $| ¡t" %ˆ¡t" $d ¡¡}W n&   t&j'dkrøt (d¡ d}Y q¦X |dkr¦t  d|¡ |}q¦|S )Nr   Zpydev_do_not_traceFr   c                       s   e Zd Z‡ fdd„ZdS )z*set_trace_to_threads.<locals>._DummyThreadc                    s
   ˆ | _ d S rD   )Ú_ident)Úself©Úthread_identr   r   Ú
_set_ident:  s    z5set_trace_to_threads.<locals>._DummyThread._set_identN)r   r   r   r|   r   rz   r   r   Ú_DummyThread8  s   r}   zIpydevd: creation of _DummyThread with fixed thread ident did not succeed.c                    s   ˆ S rD   r   )r*   ÚeventÚarg)Údummy_tracer   r   r€   Y  s    z)set_trace_to_threads.<locals>.dummy_tracec                     s    t jp
tj} | ˆ ƒ ˆ ¡  d S rD   )r   r   r   rA   Úrelease)rB   )r€   Úproceedr   r   Úincrease_tracing_count\  s    z4set_trace_to_threads.<locals>.increase_tracing_countr   zPUnable to load helper lib to set tracing to all threads (unsupported python vm).éÿÿÿÿz Error attaching debugger tracingz5Unable to set tracing for existing thread. Result: %s))ÚAssertionErrorÚsetr   Ú_current_framesÚkeysr   Ú	enumerateÚgetattrÚdiscardÚidentÚaddr   r@   Ú_activer`   rg   ÚlistÚremoveÚinsertr}   Ú	__class__Ú_active_limbo_lockr   ri   Úallocate_lockÚacquirer   Zget_original_start_new_threadr   r   rA   rI   rc   ZAttachDebuggerTracingru   Úc_intÚ	py_objectÚc_uintr   re   rf   )r8   r<   r=   ÚretÚtZ
curr_identZcurr_threadr}   Zshow_debug_inforƒ   Ústart_new_threadZset_trace_funcrL   Úresultr   )r€   r‚   r{   r   r?     sr    







û

r?   )NT)*Z_pydevd_bundle.pydevd_constantsr   r   r   r   r   r   r   r	   r
   r   Z"_pydev_bundle._pydev_saved_modulesr   r   Z_pydev_bundler   r   Úos.pathr.   ra   ru   Úior   r   r!   rA   Z_original_settracer   r%   r,   r;   Úlocalr>   rC   rF   rG   rH   rJ   rI   ro   rK   r?   r   r   r   r   Ú<module>   s.   0	!	n