o
    `۷ir/                     @   sH  d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlZd dlmZ d dlmZ d dlZd dlmZmZ d dlmZ d dlmZmZ d dlmZ eeZejfdd	ZG d
d de Z!G dd deZ"G dd deZ#						dddZ$edddZ%dd Z&dd Z'dd Z(dd Z)dS )    N)Pdb)Callable)build_addressis_ipv6)ray_constants)_internal_kv_del_internal_kv_put)DeveloperAPIc                 C   s   t | |d |  d S )N)file)printflush)messagestderr r   C/home/ubuntu/vllm_env/lib/python3.10/site-packages/ray/util/rpdb.py_cry   s   r   c                   @   sL   e Zd Zdd Zedd Zdd Zedfdd	Z	edfd
dZ
dS )_LF2CRLF_FileWrapperc                    sp    | _  d | _j| _j| _j| _j| _j| _j| _t	dr2 fdd| _
d S  j| _
d S )Nrwencodingc                    s     | jjddS )Nreplace)errors)sendallencoder   )data
connectionfhr   r   <lambda>-   s    z/_LF2CRLF_FileWrapper.__init__.<locals>.<lambda>)r   makefilestreamreadreadline	readlinescloser   filenohasattr_sendr   )selfr   r   r   r   __init__#   s   
z_LF2CRLF_FileWrapper.__init__c                 C   s   | j jS N)r   r   r'   r   r   r   r   3   s   z_LF2CRLF_FileWrapper.encodingc                 C   s
   | j  S r)   )r   __iter__r*   r   r   r   r+   7   s   
z_LF2CRLF_FileWrapper.__iter__z?
c                 C   s   | d|}| | d S )Nz
)subr&   )r'   r   nl_rexr   r   r   write:   s   z_LF2CRLF_FileWrapper.writec                 C   s   |D ]}|  || qd S r)   )r.   )r'   linesr-   liner   r   r   
writelines>   s   z_LF2CRLF_FileWrapper.writelinesN)__name__
__module____qualname__r(   propertyr   r+   recompiler.   r1   r   r   r   r   r   "   s    
r   c                   @   s6   e Zd ZdZdeg df fddZdd Ze ZZdS )_PdbWrapz/Wrap PDB to run a custom exit hook on continue.	exit_hookNc                 C   s   || _ t|  d S r)   )
_exit_hookr   r(   )r'   r9   r   r   r   r(   F   s   z_PdbWrap.__init__c                 C      |    t| |S r)   )r:   r   do_continuer'   argr   r   r   r<   J      z_PdbWrap.do_continue)	r2   r3   r4   __doc__r   r(   r<   do_cdo_contr   r   r   r   r8   C   s
    r8   c                   @   sv   e Zd ZdZdZ		dddZdd Zdd	 Zd
d Ze Z	Z
dd Ze ZZdddZdddZdd Zdd ZdS )
_RemotePdbac  
    This will run pdb as a ephemeral telnet service. Once you connect no one
    else can connect. On construction this object will block execution till a
    client has connected.
    Based on https://github.com/tamentis/rpdb I think ...
    To use this::
        RemotePdb(host="0.0.0.0", port=4444).set_trace()
    Then run: telnet 127.0.0.1 4444
    NFc                 C   s`   || _ || _|| _tt|rtjntjtj| _| j	tj
tjd | j||f || _d S )NT)_breakpoint_uuid_quiet_patch_stdstreamssocketr   AF_INET6AF_INETSOCK_STREAM_listen_socket
setsockopt
SOL_SOCKETSO_REUSEADDRbind_ip_address)r'   breakpoint_uuidhostport
ip_addresspatch_stdstreamsquietr   r   r   r(   ^   s   	
z_RemotePdb.__init__c                 C   s   | j stdt| j| j d   | jd | j \}}| j s)td|  t|| _	t
j| d| j	| j	dgd g | _| jrXdD ]}| j|tt|f tt|| j	 qC| t_d S )Nz;RemotePdb session open at %s, use 'ray debug' to connect...   z#RemotePdb accepted connection from tabzray.*)completekeystdinstdoutskip)r   r[   
__stderr__
__stdout__rZ   	__stdin__)rE   r   r   rP   rK   getsocknamelistenacceptr   handler   r(   backuprF   appendgetattrsyssetattrrC   active_instance)r'   r   addressnamer   r   r   ra   q   s0   

z_RemotePdb.listenc                 C   sJ   | j r| jstd| j   | j D ]
\}}tt|| q| j  d t_d S )NzRestoring streams: %s ...)	rd   rE   r   rh   rg   rc   r#   rC   ri   )r'   rk   r   r   r   r   	__restore   s   

z_RemotePdb.__restorec                 C   r;   r)   )_RemotePdb__restorer   do_quitr=   r   r   r   rn      r?   z_RemotePdb.do_quitc                 C   s    |    | jj  t| |S r)   )rm   rc   r   r#   r   r<   r=   r   r   r   r<      s   z_RemotePdb.do_continuec              
   C   sZ   |d u r	t  j}z	t| | W d S  ty, } z|jtjkr! W Y d }~d S d }~ww r)   )rg   	_getframef_backr   	set_traceIOErrorerrno
ECONNRESET)r'   frameexcr   r   r   rq      s   
z_RemotePdb.set_tracec              
   C   s^   zt  d }|   t| d | W d S  ty. } z|jtjkr# W Y d }~d S d }~ww )N   )rg   exc_inforesetr   interactionrr   rs   rt   )r'   	tracebacktrv   r   r   r   post_mortem   s   z_RemotePdb.post_mortemc                 C   s\   | j tjjj_tdt 	 i}t
d| j |tjd |   | jj  t| |S )z7remote
        Skip into the next remote call.
        job_idzRAY_PDB_CONTINUE_{}	namespace)rD   ray_privateworkerglobal_workerdebugger_breakpointjsondumpsget_runtime_context
get_job_idr   formatr   KV_NAMESPACE_PDBrm   rc   r   r#   r   r<   )r'   r>   r   r   r   r   	do_remote   s   
z_RemotePdb.do_remotec                 C   s.   | j tjjj_|   | jj	  t
| |S )z?get
        Skip to where the current task returns to.
        )rD   r   r   r   r   debugger_get_breakpointrm   rc   r   r#   r   r<   r=   r   r   r   do_get   s
   
z_RemotePdb.do_get)FFr)   )r2   r3   r4   r@   ri   r(   ra   rm   rn   do_qdo_exitr<   rA   rB   rq   r}   r   r   r   r   r   r   rC   Q   s     

!

	rC   Fc                 C   sb  |r| rJ dd} n| du rt jdd} |du r#tt jdd}|du r0tt jdd	}|s7t j}|r@tj	j
jj}nd
}t|| ||||d}|j }t||d }	tt d }
tj |	|
j|
jdtjt  t t  t   t ! t " t # d}t$d%|t&'|dt(j)d |*  t+d%|t(j)d |S )z5
    Opens a remote PDB on first available port.
    z.Cannot specify both host and debugger_externalz0.0.0.0NREMOTE_PDB_HOSTz	127.0.0.1REMOTE_PDB_PORT0REMOTE_PDB_QUIET 	localhost)rQ   rR   rS   rT   rU   rV   rW   rw   
)	proctitlepdb_addressfilenamelinenor{   	timestampr~   node_id	worker_idactor_idtask_idz
RAY_PDB_{}T)	overwriter   r   ),osenvirongetintbooluuiduuid4hexr   r   r   r   node_ip_addressrC   rK   r`   r   inspectgetouterframescurrentframe_rayletgetproctitler   r   joinr{   format_exceptionrg   rx   timer   r   get_node_idget_worker_idget_actor_idget_task_idr   r   r   r   r   r   ra   r   )rR   rS   rU   rV   rQ   debugger_externalrT   rdbsocknamer   parentframeinfor   r   r   r   _connect_ray_pdb   s`   






r   c                 C   s   t jdddkrtjj| S t jdddkrBtjjj	j
dkrDt j}tdddd| r1|  ndtjjj	jd}|j|d dS dS dS )	zsInterrupt the flow of the program and drop into the Ray debugger.

    Can be used within a Ray task or actor.
    	RAY_DEBUG1legacy    NF)rR   rS   rU   rV   rQ   r   )ru   )r   r   r   r   utilray_debugpyrq   r   r   r   r   rg   ro   rp   r   decoderay_debugger_external)rQ   ru   r   r   r   r   rq     s    

rq   c                  C   sn   t jdddkrtjj S t jdddkr5td dtjj	_
dd } t| }t j}|| dS dS )	zThe breakpoint hook to use for the driver.

    This disables Ray driver logs temporarily so that the PDB console is not
    spammed: https://github.com/ray-project/ray/issues/18172
    r   r   r   z-*** Temporarily disabling Ray worker logs ***Fc                   S   s   t d dtjj_d S )Nz#*** Re-enabling Ray worker logs ***T)r   r   r   r   _worker_logs_enabledr   r   r   r   enable_logging@  s   z)_driver_set_trace.<locals>.enable_loggingN)r   r   r   r   r   r   rq   r   r   r   r   r8   rg   ro   rp   )r   pdbru   r   r   r   _driver_set_trace4  s   

r   c                   C   s   t jdddkS )NRAY_DEBUG_POST_MORTEMr   r   )r   r   r   r   r   r   r   $_is_ray_debugger_post_mortem_enabledI  s   r   c                  C   sD   t jdddkrtjj S td d dd tjj	j
jd} |   d S )Nr   r   F)rR   rS   rU   rV   r   )r   r   r   r   r   r   _post_mortemr   r   r   r   r   r}   )r   r   r   r   r   M  s   
r   c           
      C   s  t jdkr	dd l}tt| rtjntjtj}|| |f 	 t jdkrHt		|gg g dd }|
 r:|t j |sGt j sG|t j nt		t j|gg g \}}}|D ]*}||kru|d}|sg d S t j|  t j  qWt j }	||	  qWq )Nwin32r   TrW   i   )rg   platformmsvcrtrG   r   rH   rI   rJ   connectselectkbhitre   rZ   isattyrecvr[   r.   r   r   r!   sendr   )
rR   rS   r   sready_to_readwrite_socketserror_socketssockr   msgr   r   r   _connect_pdb_client[  s6   




r   )NNFNNFr)   )*rs   r   r   loggingr   r6   r   rG   rg   r   r{   r   r   r   typingr   r   ray._common.network_utilsr   r   ray._privater   ray.experimental.internal_kvr   r   ray.util.annotationsr	   	getLoggerr2   logr]   r   objectr   r8   rC   r   rq   r   r   r   r   r   r   r   r   <module>   sL   
! 
@