o
    i4[                     @  s  d dl m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
mZmZmZ d dlmZ d dlZd dlZdZ	 da	 e Zg Z	 e ZG dd deZG dd	 d	ejZdddZdd Zdd Zdd Z dddZ!dd Z"dd Z#dd Z$dS )    )annotationsN)adapter)jsonlog	messagingsockets)
componentsc                   @  s   e Zd ZU dZded< ded< 	 ded< 	 ded< ded	< d
ed< dd Zdd Zdd Zdd Zdd Z	dd Z
dd Zdd ZdS )
Connectionaw  A debug server that is connected to the adapter.

    Servers that are not participating in a debug session are managed directly by the
    corresponding Connection instance.

    Servers that are participating in a debug session are managed by that sessions's
    Server component instance, but Connection object remains, and takes over again
    once the session ends.
    booldisconnectedprocess_replacedzServer | Noneserverz
int | Nonepidppidzmessaging.JsonMessageChannelchannelc           	        s  ddl m} d _d _d  _d  _tj|t	 }t
|  _ j  zr    jd}|dt }|dt _|dtdd	 _ jd
krOd  _t	   j_|_t6  jrh	 W d    W d S t fddtD rzt  dttdk}t  t  W d    n1 sw   Y  W n ty   td  j  t   Y d S w |! j}|d u r|! j}|d u rt"d  n6 j|jkr|j}|r|j#jst$d|  j  d S z	|j%&  W d S  ty   td  Y nw |rd S t"d  z" jdddi  jdd ji  jd  jd W d S  tyD   td  j  Y d S w )Nr   )sessionsFpydevdSystemInfoprocessr   r   Toptional c                 3  s$    | ]}|j  j ko|j V  qd S N)r   r   .0connselfr   R/var/www/edux/Edux_v2/venv/lib/python3.10/site-packages/debugpy/adapter/servers.py	<genexpr>c   s
    
z&Connection.__init__.<locals>.<genexpr>z% is already connected to this adapterz,Failed to accept incoming server connection:z2No active debug session for parent process of {0}.z!{0} is not expecting replacement.z*Failed to notify parent session about {0}:z(No clients to wait for - unblocking {0}.
initialize	adapterIDdebugpyattachsubProcessIdconfigurationDone
disconnectz&Failed to unblock orphaned subprocess:)'debugpy.adapterr   r   r   r   r   r   JsonIOStreamfrom_socketstrJsonMessageChannelr   startauthenticaterequestr   objectintr   name_lockany_connectionsKeyErrorlenappend_connections_changedset	Exceptionr   swallow_exceptionclosedont_wait_for_first_connectiongetinfo
connectionerrorclientnotify_of_subprocess)	r   sockr   streamr>   process_infois_first_serverparent_sessionparent_serverr   r   r   __init__?   s   







zConnection.__init__c                 C  s"   d| j d u r	d S d| j  d S )NServerz[?]z[pid=]r   r   r   r   r   __str__   s   "zConnection.__str__c                 C  sL   t d u rtj d u rd S | jddt i}|d tj kr$| j  tdd S )NpydevdAuthorizedebugServerAccessTokenclientAccessTokenz6Mismatched "clientAccessToken"; server not authorized.)access_tokenr   r   r-   r;   RuntimeError)r   authr   r   r   r,      s   
zConnection.authenticatec                 C  
   | dNz=Requests from the debug server to the client are not allowed.
isnt_validr   r-   r   r   r   r-      s   zConnection.requestc                 C     d S r   r   r   eventr   r   r   r[      s   zConnection.eventc                 C     | j   d S r   r   r;   rZ   r   r   r   terminated_event   s   zConnection.terminated_eventc                 C  s   t 4 d| _| jd ur| j  n| tv r&t|  t  W d    d S W d    d S W d    d S 1 s9w   Y  d S NT)r1   r   r   r%   r3   remover7   r8   r   r   r   r   r%      s   


"zConnection.disconnectc                 C  sR   t  | jdur
ttd| | t|| | _W d   dS 1 s"w   Y  dS )zAttaches this server to the specified Session as a Server component.

        Raises ValueError if the server already belongs to some session.
        NzAttaching {0} to {1})r1   r   
ValueErrorr   r>   rJ   r   sessionr   r   r   attach_to_session   s   
"zConnection.attach_to_sessionN)__name__
__module____qualname____doc____annotations__rI   rM   r,   r-   r[   r^   r%   rd   r   r   r   r   r	   #   s$   
 
f
r	   c                      s   e Zd ZU dZejjZded< G dd dejZ fddZ	e
dd	 Ze
d
d Zdd Zedd Zedd Zedd Zedd Zedd Zed"ddZedd Zdd Z fd d!Z  ZS )#rJ   z1Handles the debug server side of a debug session.r	   r?   c                   @  s   e Zd Zi ddddddddddddddd	dd
dddddddddddddddddddddddddg g g dZdS )zServer.CapabilitiessupportsCompletionsRequestFsupportsConditionalBreakpoints supportsConfigurationDoneRequestsupportsDataBreakpoints supportsDelayedStackTraceLoadingsupportsDisassembleRequestsupportsEvaluateForHoverssupportsExceptionInfoRequestsupportsExceptionOptionssupportsFunctionBreakpointssupportsGotoTargetsRequest!supportsHitConditionalBreakpointssupportsLoadedSourcesRequestsupportsLogPointssupportsModulesRequestsupportsReadMemoryRequestsupportsRestartFrameT)supportsRestartRequestsupportsSetExpressionsupportsSetVariablesupportsStepBacksupportsStepInTargetsRequestsupportsTerminateRequestsupportsTerminateThreadsRequestsupportsValueFormattingOptionsexceptionBreakpointFiltersadditionalModuleColumnssupportedChecksumAlgorithmsN)re   rf   rg   
PROPERTIESr   r   r   r   Capabilities   s`    	
r   c                   s   |j d u sJ |A |j rJ t j||jd || _| jjd u s#J | jjr:| jjj| jkr:t	d| jjj| j | j| j_| |_ W d    d S 1 sMw   Y  d S )N)r   z6Launcher reported PID={0}, but server reported PID={1})
r   superrI   r   r?   rc   r   launcherr   r>   )r   rc   r?   	__class__r   r   rI      s   

"zServer.__init__c                 C     | j jS )z>Process ID of the debuggee process, as reported by the server.)r?   r   r   r   r   r   r        z
Server.pidc                 C  r   )zEParent process ID of the debuggee process, as reported by the server.)r?   r   r   r   r   r   r     r   zServer.ppidc                 C  s@   | dsJ | j  | j|}|  | | |j| _d S )Nr   )	
is_requestr?   r,   r   	propagatewait_for_responser   responsecapabilitiesrX   r   r   r   r     s
   
zServer.initializec                 C  rT   rU   rV   rX   r   r   r   r-   "  s   zServer.requestc                 C  s   | j | d S r   )rA   propagate_after_startrZ   r   r   r   r[   .  s   zServer.eventc                 C  rY   r   r   rZ   r   r   r   initialized_event2  s   zServer.initialized_eventc                 C  s   | j s| j| d S d S r   )r   rA   r   rZ   r   r   r   process_event7  s   zServer.process_eventc                 C  s    | j jdvr| j | d S d S )N)visualstudiovsformac)rA   	client_idr   rZ   r   r   r   continued_event=  s   zServer.continued_eventr[   messaging.Eventc                 C  s8   |dt dddkrd| j_d S | js| j| d S d S )NpydevdReasonTr   processReplaced)r)   r?   r   r   rA   r   rZ   r   r   r   exited_eventU  s
   zServer.exited_eventc                 C  r\   r   r]   rZ   r   r   r   r^   b  s   zServer.terminated_eventc                 C  sZ   t ! d| _| j| j_t| j | j_| jj_d | j_W d    d S 1 s&w   Y  d S )NF)	r1   is_connectedr?   r   handlersr)   r0   rD   r   r   r   r   r   detach_from_sessiong  s   

"zServer.detach_from_sessionc                   s    j jr,td   jjrjjs t fdddd n j fdddd t	 t
 j  t  W d    n1 sCw   Y  t   d S )Nz*{0} is waiting for replacement subprocess.c                   s   | j  j kS r   rL   )r   r   r   r   <lambda>v  s    z#Server.disconnect.<locals>.<lambda>   )timeoutc                     s,   j  pj j pt fddj jD S )Nc                 3  s    | ]	}|j  j kV  qd S r   rL   r   r   r   r   r   }  s
    

z6Server.disconnect.<locals>.<lambda>.<locals>.<genexpr>)rA   r   r2   known_subprocessesr   rb   r   r   r   z  s   )r?   r   r   r>   rc   rA   r   wait_for_connectionwait_forr1   r3   r`   r7   r8   r   r%   r   r   rb   r   r%   n  s    
zServer.disconnect)r[   r   )re   rf   rg   rh   r   	Componentmessage_handlerri   r   rI   propertyr   r   r   r-   r[   r   r   r   r   r^   r   r%   __classcell__r   r   r   r   rJ      s6   
  







rJ   	127.0.0.1c                 C  s   t dt| |at S )NrJ   )r   server	   listenergetsockname)hostportr   r   r   r     s   r   c                   C  s   t d uS r   )r   r   r   r   r   
is_serving  s   r   c                   C  sB   zt d urt   d a W d S W d S  ty    tjdd Y d S w )Nwarning)level)r   r;   r9   r   r:   r   r   r   r   stop_serving  s   
r   c                   C  s0   t  ttW  d    S 1 sw   Y  d S r   )r1   listr3   r   r   r   r   connections  s   $r   c                   s   fdddk_ rtjdd}d|_|  dkr&td|  	 t* t	   fdd	t
D }t|d
}|d
usCj rL|W  d
   S W d
   n1 sVw   Y  t  q')zWaits until there is a server matching the specified predicate connected to
    this adapter, and returns the corresponding Connection.

    If there is more than one server connection already available, returns the oldest
    one.
    c                     sB   t   d_t t  W d    d S 1 sw   Y  d S r_   )timesleep	timed_outr1   r7   r8   r   )r   wait_for_timeoutr   r   r     s
   

"z-wait_for_connection.<locals>.wait_for_timeoutr   z%servers.wait_for_connection() timeout)targetr0   Tz/{0} waiting for connection from debug server...c                 3  s    | ]	} |r|V  qd S r   r   r   )	predicater   r   r     s    z&wait_for_connection.<locals>.<genexpr>N)r   	threadingThreaddaemonr+   r   r>   r1   r7   clearr3   nextwait)rc   r   r   threadconnsr   r   )r   r   r   r   r     s,   

r   c                   C  sR   	 t   t t   tts	 W d   dS W d   n1 s#w   Y  q)zBlocks until all debug servers disconnect from the adapter.

    If there are no server connections, waits until at least one is established first,
    before waiting for it to disconnect.
    TN)r7   r   r1   r   r5   r3   r   r   r   r   wait_until_disconnected  s   r   c                   C  s2   t  t  W d   dS 1 sw   Y  dS )zlUnblocks any pending wait_until_disconnected() call that is waiting on the
    first server to connect.
    N)r1   r7   r8   r   r   r   r   r<     s   
"r<   c           	   
     s:  t  \}}tjtjtjd|d t	| g}t
jd ur$|dt
jg7 }||7 }|dt	g7 }td| ztj|dtjtjtjd W n ty` } ztd td	|d }~ww g d
 fdd}tj|d d jfdd   fdd}tj|d ddd  d S )Nz	--connect:z--adapter-access-tokenz--pidz/Spawning attach-to-PID debugger injector: {0!r}r   )bufsizestdinstdoutstderrz7Failed to inject debug server into process with PID={0}z<Failed to inject debug server into process with PID={0}: {1}z$--- Starting attach to pid: {0} ---
c                   s   z&	 |   }|s	n|dd}| td|  qtd W d S  tyA   t }t	j
|d  d|  Y d S w )NTzutf-8replacezInjector[PID={0}] output: {1}zInjector[PID={0}] exited.)filer   )readlinedecoder6   r   r>   rstripr9   ioStringIO	traceback	print_excgetvalue)rD   lines)	on_outputoutput_collectedr   r   r   capture  s   
zinject.<locals>.capturezInjector[PID=z] stdoutT)r   r0   argsr   c                    s"  d} t   }	 t d   }|d ur0|dkr.dd }g d|}dd| d S t   | }dd	|f  | s~|d
kr~d} tjdv r~dd dd dd dd dd dd dd dd dd dd | r}g d|}|rd| q)NFT   r   r   zAttach to PID failed.

 r   z'Attaching to PID: %s (elapsed: %.2fs).

   )linuxlinux2z3
The attach to PID is taking longer than expected.
z1On Linux it's possible to customize the value of
z<`PYDEVD_GDB_SCAN_SHARED_LIBRARIES` so that fewer libraries.
z4are scanned when searching for the needed symbols.

zHi.e.: set in your environment variables (and restart your editor/client
z>so that it picks up the updated environment variable value):

zCPYDEVD_GDB_SCAN_SHARED_LIBRARIES=libdl, libltdl, libc, libfreebl3

zA-- the actual library may be different (the gdb output typically
zE-- writes the libraries that will be used, so, it should be possible
z8-- to test other libraries if the above doesn't work).

)r   r   polljoinsysplatform)taking_longer_than_expectedinitial_time
returncodeoldcontentselapsedinjectorr   r   r   r   r   info_on_timeout  s   




zinject.<locals>.info_on_timeoutz] info on timeout)r   r0   r   )r   r   r   
executableospathdirnamer!   __file__r)   r   rQ   r   r>   
subprocessPopenPIPESTDOUTr9   r:   r   MessageHandlingErrorformatr6   r   r   r   r+   )	r   debugpy_argsr   r   r   cmdlineexcr   r   r   r   r   inject  sZ   


N
r   )r   r   r   )%
__future__r   r   r   r   r   r   r!   r   debugpy.commonr   r   r   r   r&   r   r   r   rQ   r   RLockr1   r3   Eventr7   r.   r	   r   rJ   r   r   r   r   r   r   r<   r   r   r   r   r   <module>   s@    4 
5

"