
    Mhz[                    J   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mZ d dlZd dlZdZ	 da	  ej                    Zg Z	  ej                    Z G d de          Z G d d	ej                  ZddZd Zd Z d Z!ddZ"d Z#d Z$d Z%dS )    )annotationsN)adapter)jsonlog	messagingsockets)
componentssessionsc                      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 Zd Zd Zd Zd Z	d Z
d Z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                    ddl m} d _        d _        d  _        d  _        t          j                            |t                               }t          j
        |            _         j                                         	                                    j                            d          } |dt          j                              } |dt"                     _         |dt"          d	           _         j        d
k    rd  _        t                     x j        _        |_        t(          5   j        r	 d d d            d S t+           fdt,          D                       rt/            d          t1          t,                    dk    }t,                                          t4                                           d d d            n# 1 swxY w Y   nL# t8          $ r? t;          j        d            j                                         tA                       Y d S w xY w |j!         j                  }| |j!         j                  }|t;          j"        d            n j        |j        k    rE|j        }|r|j#        j        s0t;          j$        d|            j                                         d S 	 |j%        &                                d S # t8          $ r t;          j        d            Y nw xY w|rd S t;          j"        d            	  j                            dddi            j                            dd j        i            j                            d            j                            d           d S # t8          $ r1 t;          j        d            j                                         Y d S w xY w)Nr   )r
   FpydevdSystemInfoprocessr   r   Toptional c              3  H   K   | ]}|j         j         k    o|j         V  d S N)r   r   .0connselfs     W/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/debugpy/adapter/servers.py	<genexpr>z&Connection.__init__.<locals>.<genexpr>c   sK         H(F1F-F         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
   streamrB   process_infois_first_serverparent_sessionparent_servers	   `        r    __init__zConnection.__init__?   sp   ,,,,,,! %'33D#d))DD 3FDAA*	<''(:;;D4	4;==99L#|E3//DH$VS4@@@DIyB 	.1$ii7DL + +
 $ + + + + + + + +      ,     S #d#Q#Q#QRRR"%l"3"3q"8##D)))$((***'+ + + + + + + + + + + + + + +*  	 	 	!"PQQQL    +,,,
 FF	 &di00!)X\$(33N!HI4PPPPx>--- . 5% -*B*S IA>RRRL&&(((F
%::4@@@   
 %@$       	F;TBBB	!L  Y/GHHHL  NDH+EFFFL  !4555L  ..... 	! 	! 	!!"JKKKL      	!sj   B2G" 7	G G" A=G
G" GG" GG" "AH+*H+K$ $LL#A3N 7OOc                0    d| j         dn
d| j          dz   S )NServerz[?]z[pid=]r   r   s    r    __str__zConnection.__str__   s&    DH$455:M$(:M:M:MNNr"   c                    t           t          j         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   r1   r?   RuntimeError)r   auths     r    r0   zConnection.authenticate   sx    G$8$@F|## 8,G
 
 #$(<<<L   WXXX =<r"   c                ,    |                     d          Nz=Requests from the debug server to the client are not allowed.
isnt_validr   r1   s     r    r1   zConnection.request   s      K
 
 	
r"   c                    d S r   r   r   events     r    rb   zConnection.event   s    r"   c                8    | j                                          d S r   r   r?   ra   s     r    terminated_eventzConnection.terminated_event   s    r"   c                   t           5  d| _        | j        | j                                         n<| t          v r3t                              |            t                                           d d d            d S # 1 swxY w Y   d S NT)r5   r   r   r)   r7   remover;   r<   rR   s    r    r)   zConnection.disconnect   s     		+ 		+ $D{& &&((((%%##D)))$((***		+ 		+ 		+ 		+ 		+ 		+ 		+ 		+ 		+ 		+ 		+ 		+ 		+ 		+ 		+ 		+ 		+ 		+s   A%A::A>A>c                    t           5  | j        t          t          j        d| |           t          ||           | _        ddd           dS # 1 swxY 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})r5   r   
ValueErrorr   rB   rO   r   sessions     r    attach_to_sessionzConnection.attach_to_session   s      	0 	0{&  H+T7;;; $//DK		0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0s   :AAAN)__name__
__module____qualname____doc____annotations__rM   rS   r0   r1   rb   re   r)   rm   r   r"   r    r   r   #   s             OOO))))d! d! d!LO O OY Y Y
 
 

    
+ 
+ 
+
0 
0 
0 
0 
0r"   r   c                  J    e Zd ZU dZej        j        Zded<    G d dej                  Z fdZ	e
d             Ze
d             Zd	 Zed
             Zed             Zed             Zed             Zed             Zedd            Zed             Zd Z fdZ xZS )rO   z1Handles the debug server side of a debug session.r   rC   c                      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 )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)rn   ro   rp   
PROPERTIESr   r"   r    Capabilitiesru      s       
(%
,e
 /
 &u	

 /
 )%
 (
 +E
 '
 *5
 )%
 0
 +E
  
 %e
  (!
" #E#
$ ',%*#( %,1(,/4.3*,')+-9
 
 



r"   r   c                   |j         J |5  |j         rJ t                                          ||j                   || _        | j        j        J | j        j        rD| j        j        j        | j        k    r*t          j	        d| j        j        j        | j                   | j        | j        _        | |_         d d d            d S # 1 swxY w Y   d S )N)r   z6Launcher reported PID={0}, but server reported PID={1})
r   superrM   r   rC   rl   r   launcherr   rB   )r   rl   rC   	__class__s      r    rM   zServer.__init__   s    ((( 	" 	"~%%%GGWj.@AAA(DO<#+++|$ )>)Bdh)N)NLL)-H  
  $xDL!GN	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	" 	"s   B/C		CCc                    | j         j        S )z>Process ID of the debuggee process, as reported by the server.)rC   r   rR   s    r    r   z
Server.pid  s     ""r"   c                    | j         j        S )zEParent process ID of the debuggee process, as reported by the server.)rC   r   rR   s    r    r   zServer.ppid  s     ##r"   c                   |                     d          sJ | j                                         | j                            |          }|                                 |                     | |j                  | _        d S )Nr#   )	
is_requestrC   r0   r   	propagatewait_for_responser   responsecapabilitiesr_   s     r    r#   zServer.initialize  sx    !!,/////$$&&&,((11!!### --dG4DEEr"   c                ,    |                     d          r\   r]   r_   s     r    r1   zServer.request"  s       K
 
 	
r"   c                :    | j                             |           d S r   )rE   propagate_after_startra   s     r    rb   zServer.event.  s    ))%00000r"   c                    d S r   r   ra   s     r    initialized_eventzServer.initialized_event2  s	     	r"   c                L    | j         s| j                            |           d S d S r   )r   rE   r   ra   s     r    process_eventzServer.process_event7  s4     } 	5K--e44444	5 	5r"   c                Z    | j         j        dvr| j                             |           d S d S )N)visualstudiovsformac)rE   	client_idr   ra   s     r    continued_eventzServer.continued_event=  s:    * ; (DDDK--e44444 EDr"   rb   messaging.Eventc                     |dt           d          dk    rd| j        _        d S | j        s| j                            |           d S d S )NpydevdReasonTr   processReplaced)r-   rC   r   r   rE   r   ra   s     r    exited_eventzServer.exited_eventU  sc    5t4448III
 04DO,,, = 911%888889 9r"   c                8    | j                                          d S r   rd   ra   s     r    re   zServer.terminated_eventb  s     	r"   c                    t           5  d| _        | j        | j        _        t          | j                  x| j        _        | j        j        _        d | j        _        d d d            d S # 1 swxY w Y   d S )NF)	r5   is_connectedrC   r   handlersr-   r4   rH   r   rR   s    r    detach_from_sessionzServer.detach_from_sessiong  s     	* 	* %D$(ODL!;>t;O;OODL 3 8%)DO"		* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	* 	*s   AA))A-0A-c                     j         j        r`t          j        d             j        j        rj        j        st           fdd           n                      fdd           t          5  t                               j                    t                                           d d d            n# 1 swxY w Y   t                                                       d S )Nz*{0} is waiting for replacement subprocess.c                $    | j         j         k    S r   rQ   )r   r   s    r    <lambda>z#Server.disconnect.<locals>.<lambda>v  s    $(dh*> r"   <   )timeoutc                 x    j          p1j         j         p$t           fdj         j        D                       S )Nc              3  8   K   | ]}|j         j         k    V  d S r   rQ   r   s     r    r!   z6Server.disconnect.<locals>.<lambda>.<locals>.<genexpr>}  s@         $ !H0     r"   )rE   r   r6   known_subprocessesrk   s   r    r   z#Server.disconnect.<locals>.<lambda>z  s]    #N* &~::    (/(I     r"   )rC   r   r   rB   rl   rE   r   wait_for_connectionwait_forr5   r7   rh   r;   r<   r   r)   )r   rl   r   s   `@r    r)   zServer.disconnectn  sO   ?+ 	 HA4HHHlG> )D #>>>>            
 
 
  	' 	'000 $$&&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	s   79B<<C C )rb   r   )rn   ro   rp   rq   r	   	Componentmessage_handlerrr   r   rM   propertyr   r   r#   r1   rb   r   r   r   r   re   r   r)   __classcell__)r   s   @r    rO   rO      s        ;; *:O
 
 
 
 
z. 
 
 
@" " " " "& # # X# $ $ X$F F F 
 
 _
 1 1 _1   _ 5 5 _5
 5 5 _5. 
9 
9 
9 _
9   _* * *        r"   rO   	127.0.0.1c                    t          j        dt          | |          at	          j                     t                                          S )NrO   )r   server   listenerr
   report_socketsgetsockname)hostports     r    r   r     s8    }Xz4>>H!!!r"   c                     t           d uS r   )r   r   r"   r    
is_servingr     s    4r"   c                     	 t           t                                            d a n%# t          $ r t          j        d           Y nw xY wt          j                     d S )Nwarning)level)r   r?   r=   r   r>   r
   r   r   r"   r    stop_servingr     sm    /NNH / / /I....../s   "% AAc                 l    t           5  t          t                    cd d d            S # 1 swxY w Y   d S r   )r5   listr7   r   r"   r    connectionsr     sw    	 " "L!!" " " " " " " " " " " " " " " " " "s   )--c                   fddk    _         r1t          j        d          }d|_        |                                 dk    rt          j        d|            	 t          5  t          	                                 fdt          D             }t          |d          }|j         r|cddd           S 	 ddd           n# 1 swxY w Y   t                                           )	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                     t          j                    d_        t          5  t                                           d d d            d S # 1 swxY w Y   d S rg   )timesleep	timed_outr5   r;   r<   )r   wait_for_timeouts   r    r   z-wait_for_connection.<locals>.wait_for_timeout  s    
7%)" 	' 	' $$&&&	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	' 	's   AAAr   z%servers.wait_for_connection() timeout)targetr4   Tz/{0} waiting for connection from debug server...c              3  2   K   | ]} |          |V  d S r   r   )r   r   	predicates     r    r!   z&wait_for_connection.<locals>.<genexpr>  s0      FFdiiooFTFFFFFFr"   N)r   	threadingThreaddaemonr/   r   rB   r5   r;   clearr7   nextwait)rl   r   r   threadconnsr   r   s    ``   @r    r   r     s{   ' ' ' ' ' ' ")A !#*Q
 
 
 !||BGLLL$ 	 	 &&(((FFFFlFFFEt$$D#3#=	 	 	 	 	 	 	 	  		 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	!!###$s   +ACCCc                     	 t                                            t          5  t                                            t	          t
                    s	 ddd           dS 	 ddd           n# 1 swxY w Y   u)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)r;   r   r5   r   r9   r7   r   r"   r    wait_until_disconnectedr     s    !!### 	 	 &&(((|$$ 	 	 	 	 	 	 	 		 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   /A++A/2A/c                 x    t           5  t                                           ddd           dS # 1 swxY w Y   dS )zlUnblocks any pending wait_until_disconnected() call that is waiting on the
    first server to connect.
    N)r5   r;   r<   r   r"   r    r@   r@     s     
 # #  """# # # # # # # # # # # # # # # # # #s   /33c                   	
 t                                           \  }}t          j        t          j                            t          j                  d|dz   t          |          z   g}t          j        |dt          j        gz  }||z  }|dt                     gz  }t          j        d|           	 t          j        |dt          j        t          j        t          j                  	nO# t$          $ rB}t          j        d            t)          j        d	                     |                    d }~ww xY wg 

                    d
                                          
 fd}t1          j        |d  d	j        fd                                           	
 fd}t1          j        |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nT|                    dd          }                    |           t          j        d|                                           kt          j        d           d S # t          $ rJ t          j                    }t          j
        |            d|                                           Y d S w xY w)NTzutf-8replacezInjector[PID={0}] output: {1}zInjector[PID={0}] exited.)filer   )readlinedecoder:   r   rB   rstripr=   ioStringIO	traceback	print_excgetvalue)rH   lines	on_outputoutput_collectedr   s      r    capturezinject.<locals>.capture  s    	.N(( {{7I66 ''---8#t{{}}MMMN H0#66666 	. 	. 	.AQ''''Ih

------	.s   BB ACCzInjector[PID=z] stdoutT)r   r4   argsr   c                    d} t          j                     }	 t          j        d                                           }|L|dk    rD dd           }g d                    |          } dd                    |                     d S t          j                     |z
  } dd		|fz             | s|d
k    rd} t          j        dv rx 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|           X)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   s
         r    info_on_timeoutzinject.<locals>.info_on_timeout  sQ   &+#y{{H	2JqMMM!J%??I 3  
 +C')$!wws||HIh(9(9:::ikkL0GIDW~U   / +R<<26/|':::!	$S   "	$P   "	$[   "	$T   "	$g   "	$^   "	$c   "	$`   "	$d   "	$X   + 2&#% 773<< 2Ih111QH	2r"   z] info on timeout)r   r4   r   )r   r   r  
executableospathdirnamer%   __file__r-   r   rX   r   rB   
subprocessPopenPIPESTDOUTr=   r>   r   MessageHandlingErrorformatr:   r   r   r   r/   )r   debugpy_argsr   r   r   cmdlineexcr   r  r  r   s   ` `      @@r    injectr     s9   %%''JD$ 	
())s
SYY	G ',g.BCC|GS""GH>HHH
#/?$
 
 
  
 
 
Es	
 	
 	
 ,JQQS 
 
 	
	
 CJJ3OOPPP. . . . . . .  *S***o	  
 egggL2 L2 L2 L2 L2 L2 L2 L2\ %KS%K%K%KTX  egggggs   77C/ /
D;9=D66D;)r   r   r   )&
__future__r   r  r  r  r   r   r%   r   debugpy.commonr   r   r   r   r*   r	   r
   r   r   rX   r   RLockr5   r7   Eventr;   r2   r   r   rO   r   r   r   r   r   r   r@   r   r   r"   r    <module>r%     s  
 # " " " " " 				     



             8 8 8 8 8 8 8 8 8 8 8 8 0 0 0 0 0 0 0 0     				 9 6	 'y(( p0 p0 p0 p0 p0 p0 p0 p0fq q q q qZ! q q qh" " " "       " " "
$ $ $ $D  # # #O O O O Or"   