
    f#$                         d dl mZ d dlZd dlZddlmZmZmZm	Z	m
Z
mZ  G d de      Z G d de      Z G d	 d
e      Z G d de      Z G d de      Zy)    )print_functionN   )lib_USDT_CB_USDT_PROBE_CBbcc_usdt_locationbcc_usdt_argumentBCC_USDT_ARGUMENT_FLAGSc                       e Zd Zy)USDTExceptionN)__name__
__module____qualname__     */usr/lib/python3/dist-packages/bcc/usdt.pyr   r      s    r   r   c                   $    e Zd Zd Zd Zd Zd Zy)USDTProbeArgumentc                    |j                   dk  | _        t        |j                         | _         |j                  | _        | j                  t        j
                  z  dk7  r|j                  | _        | j                  t        j                  z  dk7  r|j                  | _        | j                  t        j                  z  dk7  r|j                  | _
        | j                  t        j                  z  dk7  r|j                  | _        | j                  t        j                  z  dk7  r|j                  | _        | j                  t        j                  z  dk7  r|j                   | _        y y )Nr   )sizesignedabsvalidr
   CONSTANTconstantDEREF_OFFSETderef_offsetDEREF_IDENTderef_identBASE_REGISTER_NAMEbase_register_nameINDEX_REGISTER_NAMEindex_register_nameSCALEscale)selfarguments     r   __init__zUSDTProbeArgument.__init__   s   mma'&	^^
::/888A=$--DM::/<<<A ( 5 5D::/;;;q@'33D::/BBBaG&.&A&AD#::/CCCqH'/'C'CD$::/555:!DJ ;r   c                 F    d| j                   | j                  rdfz  S dfz  S )Nz%d %s byteszsigned  unsigned)r   r   r&   s    r   _size_prefixzUSDTProbeArgument._size_prefix+   s5    $++JFG 	G;EFG 	Gr   c                 T   | j                   t        j                  z  dk7  rd| j                  z  S | j                   t        j                  z  dk(  rd| j
                  j                         z  S | j                   t        j                  z  dk7  r| j                   t        j                  z  dk(  r| j                   t        j                  z  dk7  rPd| j                  j                         z  }| j                   t        j                  z  dk7  r|d| j                  z  z  }nd}| j                  dk\  rdnd}d	| j
                  j                         |t        | j                        |fz  S | j                   t        j                  z  dk7  r| j                   t        j                  z  dk7  ru| j                   t        j                  z  dk7  rU| j
                  d
k(  rF| j                  dk\  rdnd}d| j                  j                         |t        | j                        fz  S d| j                   z  S )Nr   z%dz%sz + %sz * %d +-z*(%s %s %d%s)ipz*(&%s %s %d)z&unrecognized argument format, flags %d)r   r
   r   r   r   r!   decoder   r"   r#   r$   r%   r   r   r    r   )r&   index_offsetsigns      r   _formatzUSDTProbeArgument._format/   s    ::/888A=$--''::/<<<A$1188:::::/<<<A::/;;;q@zz3GGG1L&)A)A)H)H)JJ:: 7 = ==B Gdjj$88L!++q03cD"d&=&=&D&D&F&*C0A0A,BL&R R R::/<<<A::/;;;q@::/BBBaG""d*++q03cD!T%5%5%<%<%>%)3t/@/@+A%C C C
 8$**DDr   c                 H    | j                         d| j                         S )Nz @ )r,   r5   r+   s    r   __str__zUSDTProbeArgument.__str__M   s     --/@@r   N)r   r   r   r(   r,   r5   r7   r   r   r   r   r      s    ("GE<Ar   r   c                       e Zd Zd Zd Zd Zy)USDTProbeLocationc                     || _         || _        |j                  | _        |j                  | _        |j                  | _        y N)probeindexnum_argumentsaddressbin_path)r&   r<   r=   locations       r   r(   zUSDTProbeLocation.__init__Q   s8    

"00'' ))r   c                 T    d| j                   j                         | j                  fz  S )Nz%s 0x%x)r@   r2   r?   r+   s    r   r7   zUSDTProbeLocation.__str__X   s#    DMM002DLLAAAr   c           
      P   t               }t        j                  | j                  j                  | j                  j
                  | j                  j                  | j                  |t        j                  |            }|dk7  rt        d|| j                  fz        t        |      S )Nr   z.error retrieving probe argument %d location %d)r	   r   bcc_usdt_get_argumentr<   contextprovidernamer=   ctbyrefr   r   )r&   r=   argress       r   get_argumentzUSDTProbeLocation.get_argument[   s    !''

(:(:DJJ<O<O(,

(,

E288C=J !8DDJJ'() ) !%%r   N)r   r   r   r(   r7   rL   r   r   r   r9   r9   P   s    *B	&r   r9   c                   $    e Zd Zd Zd Zd Zd Zy)	USDTProbec                     || _         |j                  | _        |j                  | _        |j                  | _        |j                  | _        |j
                  | _        |j                  | _        y r;   )rE   rF   rG   r@   	semaphorenum_locationsr>   )r&   rE   r<   s      r   r(   zUSDTProbe.__init__g   sQ    JJ	"00"00r   c                     d| j                   j                         | j                  j                         | j                  fz  S )Nz%s:%s [sema 0x%x])rF   r2   rG   rP   r+   s    r   r7   zUSDTProbe.__str__p   s9    "$$&		(8(8(:DNNKL 	Lr   c                 p    | j                   j                         d| j                  j                         S )N:)rF   r2   rG   r+   s    r   
short_namezUSDTProbe.short_namet   s'    --..0$))2B2B2DEEr   c           	          t               }t        j                  | j                  | j                  | j
                  |t        j                  |            }|dk7  rt        d|z        t        | ||      S )Nr   z"error retrieving probe location %d)
r   r   bcc_usdt_get_locationrE   rF   rG   rH   rI   r   r9   )r&   r=   locrK   s       r   get_locationzUSDTProbe.get_locationw   s`    !''dmmTYY(-rxx}>!8 Du LMM uc22r   N)r   r   r   r(   r7   rU   rY   r   r   r   rN   rN   f   s    1LF3r   rN   c                   J    e Zd ZddZd Zd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zy)USDTNc                    |r|dk7  r|| _         |r+t        j                  ||j                  d            | _        n.t        j                  |t        j                  d            | _        | j                  d k(  rt        d|z        y |rN|| _        t        j                  |j                  d            | _        | j                  d k(  rt        d|z        y t        d      )Nasciir   z USDT failed to instrument PID %dz!USDT failed to instrument path %sz/either a pid or a binary path must be specified)
pidr   bcc_usdt_new_frompidencoderE   rH   c_char_pr   pathbcc_usdt_new_frompath)r&   r_   rc   s      r   r(   zUSDT.__init__   s    3"9DH"77T[[=QR"77R[[^L||t##$F$LMM $DI44T[[5IJDL||t##$G$$NOO $  EG Gr   c                 B    t        j                  | j                         y r;   )r   bcc_usdt_closerE   r+   s    r   __del__zUSDT.__del__   s    4<<(r   c                    |j                  dd      }t        |      dk(  r@t        j                  | j                  |j                  d      |j                  d            }nT|\  }}t        j                  | j                  |j                  d      |j                  d      |j                  d            }|dk7  rt        d|z        y )NrT   r   r^   r   a.  Failed to enable USDT probe '%s':
the specified pid might not contain the given language's runtime,
or the runtime was not built with the required USDT probes. Look
for a configure flag similar to --with-dtrace or --enable-dtrace.
To check which probes are present in the process, use the tplist tool.
)splitlenr   bcc_usdt_enable_proberE   ra   %bcc_usdt_enable_fully_specified_prober   )r&   r<   fn_nameprobe_partsretprovider_name
probe_names          r   enable_probezUSDT.enable_probe   s    kk#q){q ++ell73W^^G5LNC +6']J;;m227;Z=N=Nw=Ww')C !8
   r   c                     	 | j                  ||       y # t        $ r:}t        |t        j                         t        j
                  d       Y d }~y d }~ww xY w)N)filer   )rr   r   printsysstderrexit)r&   r<   rm   es       r   enable_probe_or_bailzUSDT.enable_probe_or_bail   sB    	eW- 	!#**%HHQKK	s    	A0AAc                     | j                   S r;   )rE   r+   s    r   get_contextzUSDT.get_context   s    ||r   c                     t        j                  dz         }t        j                  | j                        |d<   t        j                  |d      j                         S )Nr   r   )rH   c_void_prE   r   bcc_usdt_genargsr2   )r&   	ctx_arrays     r   get_textzUSDT.get_text   sD    [[1_'	{{4<<0	!##Iq188::r   c                 d   |j                  dd      }t        |      dk(  r>t        j                  | j                  |j                  d      |      j                         S |\  }}t        j                  | j                  |j                  d      |j                  d      |      j                         S )NrT   r   r^   )ri   rj   r   bcc_usdt_get_probe_argctyperE   ra   r2   +bcc_usdt_get_fully_specified_probe_argctype)r&   rq   	arg_indexrn   rp   r<   s         r   get_probe_arg_ctypezUSDT.get_probe_arg_ctype   s     &&sA.{q 22j//8)EEKVXN &1"]EBBm227;U\\'=RT]__e_e_ghr   c                 l     g  fd}t        j                   j                  t        |             S )Nc                 d    j                  t        j                  | j                               y r;   )appendrN   rE   contents)r<   probesr&   s    r   
_add_probez)USDT.enumerate_probes.<locals>._add_probe   s    MM)DLL%..ABr   )r   bcc_usdt_foreachrE   r   r&   r   r   s   ` @r   enumerate_probeszUSDT.enumerate_probes   s.    	C 	T\\8J+?@r   c                 n    | j                         }|D ]   \  }}}}|rd}|j                  ||||       " y )Nr]   )rG   rm   addrr_   )enumerate_active_probesattach_uprobe)r&   bpfattach_usdt_ignore_pidr   binpathrm   r   r_   s           r   attach_uprobeszUSDT.attach_uprobes   sL    --/-3 	2)WgtS%7G#'S  2	2r   c                 h    g fd}t        j                  | j                  t        |             S )Nc                 0    j                  | |||f       y r;   )r   )r   rm   r   r_   r   s       r   r   z0USDT.enumerate_active_probes.<locals>._add_probe   s    MM7GT378r   )r   bcc_usdt_foreach_uproberE   r   r   s     @r   r   zUSDT.enumerate_active_probes   s-    	9 	##DLL.2LMr   )NN)r   r   r   r(   rg   rr   rz   r|   r   r   r   r   r   r   r   r   r[   r[      s6    G$)(;
h2r   r[   )
__future__r   ctypesrH   rv   libbccr   r   r   r   r	   r
   	Exceptionr   objectr   r9   rN   r[   r   r   r   <module>r      s[    &  
, ,	I 	5A 5An& &,3 32[6 [r   