
    d                         d Z ddlmZ ddlZddlZddlZddlZddlmZm	Z	 ddl
mZmZmZ  G d de      Z G d	 d
e	      Zy)z$
Debugging utilities for constructs
    )print_functionN   )	ConstructSubconstruct)	HexString	ContainerListContainerc                   H    e Zd ZdZg dZdZ	 	 	 ddZd Zd Zd Z	d	 Z
d
 Zy)Probea  
    A probe: dumps the context, stack frames, and stream content to the screen
    to aid the debugging process.
    See also Debugger.

    Parameters:
    * name - the display name
    * show_stream - whether or not to show stream contents. default is True.
      the stream must be seekable.
    * show_context - whether or not to show the context. default is True.
    * show_stack - whether or not to show the upper stack frames. default
      is True.
    * stream_lookahead - the number of bytes to dump when show_stack is set.
      default is 100.

    Example:
    Struct("foo",
        UBInt8("a"),
        Probe("between a and b"),
        UBInt8("b"),
    )
    )	printnameshow_streamshow_context
show_stackstream_lookaheadr   Nc                     t        j                  | d        |-t        xj                  dz  c_        dt        j                  fz  }|| _        || _        || _        || _        || _        y )Nr   z<unnamed %d>)	r   __init__r   counterr   r   r   r   r   )selfnamer   r   r   r   s         :/usr/lib/python3/dist-packages/elftools/construct/debug.pyr   zProbe.__init__*   s^     	4&<MMQM!U]]$44D&($ 0    c                 N    | j                   j                  d| j                  dS )N())	__class____name__r   )r   s    r   __repr__zProbe.__repr__6   s    >>22DNNCCr   c                 (    | j                  ||       y Nprintoutr   streamcontexts      r   _parsezProbe._parse8       fg&r   c                 (    | j                  ||       y r   r    r   objr#   r$   s       r   _buildzProbe._build:   r&   r   c                      y)Nr    )r   r$   s     r   _sizeofzProbe._sizeof<   s    r   c                    t               }| j                  rl|j                         |_        |j	                  | j
                        }|sd|_        n,|j                  t        |       d       t        |      |_        t         | j                  r||_        | j                  rt               |_        t!        j                         D cg c]  }|d   	 c}dd }|j#                          |D ]B  }t               }|j%                  |j&                         |j                  j)                  |       D t        d       t        d| j*                         t        |       t        d       y c c}w )NzEOF reachedr   r   P================================================================================r   )r   r   tellstream_positionreadr   following_stream_dataseeklenr   printr   r$   r   r	   stackinspectreverse
__update__f_localsappendr   )	r   r#   r$   r)   followssframesfas	            r   r!   zProbe.printout?   s   k"(++-Ckk$"7"78G,9)S\M1-,5g,>)!CK??%CI$+MMO4qad4Qr:FNN $KQZZ(		  #$
 	hgt~~&c
h 5s   E()NTTTd   )r   
__module____qualname____doc__	__slots__r   r   r   r%   r*   r-   r!   r,   r   r   r   r      s=    ,I G2637$'1D''r   r   c                   *    e Zd ZdZdgZd Zd ZddZy)DebuggeraR  
    A pdb-based debugger. When an exception occurs in the subcon, a debugger
    will appear and allow you to debug the error (and even fix on-the-fly).

    Parameters:
    * subcon - the subcon to debug

    Example:
    Debugger(
        Enum(UBInt8("foo"),
            a = 1,
            b = 2,
            c = 3
        )
    )
    retvalc                     	 | j                   j                  ||      S # t        $ r> t        | _        | j                  d       | j                  t        u r | j                  cY S w xY w)Nz@(you can set the value of 'self.retval', which will be returned))subconr%   	ExceptionNotImplementedrJ   
handle_excr"   s      r   r%   zDebugger._parsen   s^    		#;;%%fg66 	#(DKOO * +{{n,{{"	#s    AA%$A%c                 ~    	 | j                   j                  |||       y # t        $ r | j                          Y y w xY wr   )rL   r*   rM   rO   r(   s       r   r*   zDebugger._buildy   s6    	KKsFG4 	OO	s     <<Nc                 N   t        d       t        d| j                  d       t        dj                  t        j                  t        j                          dd               |rt        |       t        j                  t        j                         d          t        d       y )Nr0   zDebugging exception of : r      )	r7   rL   join	tracebackformat_exceptionsysexc_infopdbpost_mortem)r   msgs     r   rO   zDebugger.handle_exc~   si    hdkk;<bggi00#,,.A!"EFG#Jq)*hr   r   )r   rD   rE   rF   rG   r%   r*   rO   r,   r   r   rI   rI   \   s      
I
#
r   rI   )rF   
__future__r   rX   rV   rZ   r9   corer   r   libr   r   r	   r   rI   r,   r   r   <module>r`      s?    & 
  
  ) 4 4MI M^)| )r   