
    -Phj                    
   d Z ddlZddlZddlZddlZd Zd Zd Zd Zd Z	da
dZd	Zd
ZdZdZdZdZdZdZdZdZ ej                    ZdZdZdZ G d de          Zd Zd Zd Z	 ej         Z!n# e"$ r d Z!Y nw xY w G d de          Z# G d d          Z$ G d  d!          Z% G d" d#e%          Z& G d$ d%          Z'd& Z( G d' d(          Z)d) Z*de*_+         G d* d+e%          Z, G d, d-e-          Z. G d. d/e%          Z/ G d0 d1e%          Z0 G d2 d3          Z1 G d4 d5          Z2 G d6 d7e%          Z3 G d8 d9e%          Z4 G d: d;e%          Z5 G d< d=e%          Z6 G d> d?e%          Z7 G d@ dAe7          Z8 G dB dCe%          Z9 G dD dEe%          Z: G dF dGe%          Z; G dH dIe%          Z< G dJ dKe%          Z= G dL dMe%          Z>dN Z? G dO dPe%          Z@ G dQ dRe%          ZAdS ZBdT ZC G dU dV          ZDdW ZE	 dX ZF eF ejG                                G dY dZ          ZH G d[ d\ejI                  ZJ eJ             d] ZK G d^ d_ejI                  ZL G d` daejI                  ZM eNejH        db          r eL              eM              G dc ddejI                  ZO eO              G de dfejI                  ZP eP              G dg dhejI                  ZQ eQ              G di djejI                  ZR eR             ddlSZSddlTZTddlUZUddlVZVddlWZWddlXZXddlYZYdk ZZ G dl dmejI                  Z[ e[             dn Z\ G do dpej]                  Z^ e^dq            G dr dse^          Z_ e_dt            G du dvejI                  Z` e`dweja        ejb                    G dx dy          Zcdd{Zdejd        Zeede_d         ec            Zfd| Zgdd}Zhd~ Zid Zj G d dejI                  Zk G d d          Zl G d del          Zm G d d          Zn G d deken          Zo G d deo          Zp G d dek          Zq G d dek          Zr G d dek          Zsd Ztd Zud Zv G d d          Zw G d dew          Zx G d dejI                  Zyd Zz G d dejI                  Z{ ejd        d            eNed          r eod em                      Z| epd em                      Z} ei              eqd em                      Z~ erd em                      Z esd em                      Z eydd          Z e{dej        ejb                  ZdS  eTj        d           dS )a-  
From gdb 7 onwards, gdb's build can be configured --with-python, allowing gdb
to be extended with Python code e.g. for library-specific data visualizations,
such as for the C++ STL types.  Documentation on this API can be seen at:
http://sourceware.org/gdb/current/onlinedocs/gdb/Python-API.html


This python module deals with the case when the process being debugged (the
"inferior process" in gdb parlance) is itself python, or more specifically,
linked against libpython.  In this situation, almost every item of data is a
(PyObject*), and having the debugger merely print their addresses is not very
enlightening.

This module embeds knowledge about the implementation details of libpython so
that we can emit useful visualizations e.g. a string, a list, a dict, a frame
giving file/line information and the state of local variables

In particular, given a gdb.Value corresponding to a PyObject* in the inferior
process, we can generate a "proxy value" within the gdb process.  For example,
given a PyObject* in the inferior process that is in fact a PyListObject*
holding three PyObject* that turn out to be PyBytesObject* instances, we can
generate a proxy value within the gdb process that is a list of bytes
instances:
  [b"foo", b"bar", b"baz"]

Doing so can be expensive for complicated graphs of objects, and could take
some time, so we also have a "write_repr" method that writes a representation
of the data to a file-like object.  This allows us to stop the traversal by
having the file-like object raise an exception if it gets too much data.

With both "proxyval" and "write_repr" we keep track of the set of all addresses
visited so far in the traversal, to avoid infinite recursion due to cycles in
the graph of object references.

We try to defer gdb.lookup_type() invocations for python types until as late as
possible: for a dynamically linked python binary, when the process starts in
the debugger, the libpython.so hasn't been dynamically loaded yet, so none of
the type names are known to the debugger

The module also extends gdb with some python-specific commands.
    Nc                  N    t          j        d                                          S )Nchargdblookup_typepointer     Y/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/Cython/Debugger/libpython.py_type_char_ptrr   ?   s    ?6""**,,,r
   c                  N    t          j        d                                          S )Nzunsigned charr   r	   r
   r   _type_unsigned_char_ptrr   C   s    ??++33555r
   c                  N    t          j        d                                          S )Nzunsigned shortr   r	   r
   r   _type_unsigned_short_ptrr   G   s    ?+,,44666r
   c                  N    t          j        d                                          S )Nzunsigned intr   r	   r
   r   _type_unsigned_int_ptrr   K   s    ?>**22444r
   c                  X    t          j        d                                          j        S )Nvoid)r   r   r   sizeofr	   r
   r   _sizeof_void_pr   O   s!    ?6""**,,33r
   i   i   i   i   i   i   i    i   @l        i   0123456789abcdefz!(frame information optimized out)z*Unable to read information on python frame_PyEval_EvalFrameDefaultc                       e Zd ZdS )NullPyObjectPtrN__name__
__module____qualname__r	   r
   r   r   r   k           Dr
   r   c                 "    t          | d          S )Ni  )minvals    r   safety_limitr$   o   s     sD>>r
   c                 T    t          t          t          |                               S N)ranger$   intr"   s    r   
safe_ranger)   v   s      c#hh''(((r
   c                 0    |                      |           d S r&   )write)filetexts     r   write_unicoder.   {   s    JJtr
   c                    t          | t                    s| S t          j                    }|dk    r|                     |          S g }| D ]i}dt          |          cxk    rdk    r#n n t          t          |          dz
            }n|                    |          }|                    |           jd                    |          S )Nmbcsi  i      )	
isinstanceunicodesysgetfilesystemencodingencodeordchrappendjoin)filenameencodingencodedr   bytes        r   os_fsencoder@      s    (G,, 	O,..v??8,,, 	! 	!DT,,,,f,,,,,3t99v-..{{8,,NN4    wwwr
   c                       e Zd ZdS )StringTruncatedNr   r	   r
   r   rB   rB      r   r
   rB   c                   &    e Zd ZdZddZd Zd ZdS )TruncatedStringIOz^Similar to io.StringIO, but can truncate the output by raising a
    StringTruncated exceptionNc                 "    d| _         || _        d S )Nr2   )_valmaxlen)selfrG   s     r   __init__zTruncatedStringIO.__init__   s    	r
   c                    | j         rmt          |          t          | j                  z   | j         k    r@| xj        |d| j         t          | j                  z
           z  c_        t                      | xj        |z  c_        d S Nr   )rG   lenrF   rB   )rH   datas     r   r+   zTruncatedStringIO.write   su    ; 	(4yy3ty>>)DK77		T!DK#di..$@"@AA		%'''		T				r
   c                     | j         S r&   )rF   rH   s    r   getvaluezTruncatedStringIO.getvalue   
    yr
   r&   )r   r   r   __doc__rI   r+   rP   r	   r
   r   rD   rD      sP        ! !         r
   rD   c                       e Zd ZdZdZddZd Zd Zd Zd Z	d	 Z
d
 Zd Zd Zd Zd Zed             Zed             Zed             Zd ZdS )PyObjectPtrak  
    Class wrapping a gdb.Value that's either a (PyObject*) within the
    inferior process, or some subclass pointer e.g. (PyBytesObject*)

    There will be a subclass for every refined PyObject type that we care
    about.

    Note that at every stage the underlying pointer could be NULL, point
    to corrupt data, etc; this is the debugger, after all.
    PyObjectNc                 P    |r|                     |          | _        d S || _        d S r&   )cast_gdbvalrH   gdbvalcast_tos      r   rI   zPyObjectPtr.__init__   s,     	"!;;w//DLLL!DLLLr
   c                    |                                  rt          |           |dk    rK| j                            t                                                    }|                                |         S |dk    rK| j                            t                                                    }|                                |         S | j                                        |         S )a  
        Get the gdb.Value for the given field within the PyObject, coping with
        some python 2 versus python 3 differences.

        Various libpython types are defined using the "PyObject_HEAD" and
        "PyObject_VAR_HEAD" macros.

        In Python 2, this these are defined so that "ob_type" and (for a var
        object) "ob_size" are fields of the type in question.

        In Python 3, this is defined as an embedded PyVarObject type thus:
           PyVarObject ob_base;
        so that the "ob_size" field is located insize the "ob_base" field, and
        the "ob_type" is most easily accessed by casting back to a (PyObject*).
        ob_typeob_size)is_nullr   rX   rW   rT   get_gdb_typedereferencePyVarObjectPtr)rH   namepyo_ptrs      r   fieldzPyObjectPtr.field   s      <<>> 	(!$'''9l''(@(@(B(BCCG&&((..9l''(C(C(E(EFFG&&((.. |''))$//r
   c                 \    t                               |                     |                    S )z
        Get a PyObjectPtr for the given PyObject* field within this PyObject,
        coping with some python 2 versus python 3 differences.
        )rT   from_pyobject_ptrre   rH   rc   s     r   
pyop_fieldzPyObjectPtr.pyop_field   s$    
 ,,TZZ-=-=>>>r
   c                 \    |                      |          }|                    ||           dS )zz
        Extract the PyObject* field named "name", and write its representation
        to file-like object "out"
        N)ri   
write_repr)rH   rc   outvisited	field_objs        r   write_field_reprzPyObjectPtr.write_field_repr   s1    
 OOD))	S'*****r
   c                     t          |          }	 |                     |t                                 n'# t          $ r |                                dz   cY S w xY w|                                S )z
        Get a repr-like string for the data, but truncate it at "maxlen" bytes
        (ending the object graph traversal as soon as you do)
        z...(truncated))rD   rk   setrB   rP   )rH   rG   rl   s      r   get_truncated_reprzPyObjectPtr.get_truncated_repr   sv    
  ''	5OOC'''' 	5 	5 	5<<>>$44444	5
 ||~~s   "4 !AAc                 F    t          |                     d                    S )Nr]   )PyTypeObjectPtrre   rO   s    r   typezPyObjectPtr.type   s    tzz)44555r
   c                 2    dt          | j                  k    S rK   r(   rX   rO   s    r   r_   zPyObjectPtr.is_null   s    C%%%%r
   c                     | j         j        S )a<  
        Is the value of the underlying PyObject* visible to the debugger?

        This can vary with the precise version of the compiler used to build
        Python, and the precise version of gdb.

        See e.g. https://bugzilla.redhat.com/show_bug.cgi?id=556975 with
        PyEval_EvalFrameEx's "f"
        )rX   is_optimized_outrO   s    r   ry   zPyObjectPtr.is_optimized_out   s     |,,r
   c                     	 |                                  }|                    d          }|                                S # t          t          t
          f$ r Y dS w xY w)Ntp_nameunknown)ru   re   stringr   RuntimeErrorUnicodeDecodeError)rH   r]   r{   s      r   safe_tp_namezPyObjectPtr.safe_tp_name	  sa    	iikkGmmI..G>>###  /AB 	 	 	99	s   <? AAc                 |     G d d          } ||                                  t          | j                            S )ai  
        Scrape a value from the inferior process, and try to represent it
        within the gdb process, whilst (hopefully) avoiding crashes when
        the remote data is corrupt.

        Derived classes will override this.

        For example, a PyIntObject* with ob_ival 42 in the inferior process
        should result in an int(42) in this process.

        visited: a set of all gdb.Value pyobject pointers already visited
        whilst generating this value (to guard against infinite recursion when
        visiting object graphs with loops).  Analogous to Py_ReprEnter and
        Py_ReprLeave
        c                       e Zd ZdZd Zd ZdS )&PyObjectPtr.proxyval.<locals>.FakeReprz
            Class representing a non-descript PyObject* value in the inferior
            process for when we don't have a custom scraper, intended to have
            a sane repr().
            c                 "    || _         || _        d S r&   )r{   address)rH   r{   r   s      r   rI   z/PyObjectPtr.proxyval.<locals>.FakeRepr.__init__,  s    &&r
   c                 >    | j         dk    rdS d| j        | j         fz  S )Nr   0x0<%s at remote 0x%x>)r   r{   rO   s    r   __repr__z/PyObjectPtr.proxyval.<locals>.FakeRepr.__repr__0  s*     <1$$ 5,dl/KKKr
   Nr   r   r   rR   rI   r   r	   r
   r   FakeReprr   %  sA         ' ' 'L L L L Lr
   r   )r   r(   rX   )rH   rm   r   s      r   proxyvalzPyObjectPtr.proxyval  sc    "	L 	L 	L 	L 	L 	L 	L 	L& x))++DL))+ + 	+r
   c                 l    |                     t          |                     |                              S )z
        Write a string representation of the value scraped from the inferior
        process to "out", a file-like object.
        )r+   reprr   rH   rl   rm   s      r   rk   zPyObjectPtr.write_repr;  s*     yydmmG4455666r
   c           	      \   	 |                     d                                          }t          |                     d                    }n# t          t          f$ r | cY S w xY wt
          t          t          t          t          t          t          t          d}||v r||         S |t          z  rt          S |t          z  rt          S |t           z  rt"          S |t$          z  rt&          S |t(          z  rt*          S |t,          z  rt.          S |t0          z  rt2          S |t4          z  rt6          S | S )a  
        Given a PyTypeObjectPtr instance wrapping a gdb.Value that's a
        (PyTypeObject*), determine the corresponding subclass of PyObjectPtr
        to use

        Ideally, we would look up the symbols for the global types, but that
        isn't working yet:
          (gdb) python print gdb.lookup_symbol('PyList_Type')[0].value
          Traceback (most recent call last):
            File "<string>", line 1, in <module>
          NotImplementedError: Symbol type not yet supported in Python scripts.
          Error while executing Python code.

        For now, we use tp_flags, after doing some string comparisons on the
        tp_name for some special-cases that don't seem to be visible through
        flags
        r{   tp_flags)boolclassobjNoneTypeframerq   	frozensetbuiltin_function_or_methodzmethod-wrapper)re   r}   r(   r~   r   PyBoolObjectPtrPyClassObjectPtrPyNoneStructPtrPyFrameObjectPtrPySetObjectPtrPyCFunctionObjectPtrwrapperobjectPy_TPFLAGS_HEAPTYPEHeapTypeObjectPtrPy_TPFLAGS_LONG_SUBCLASSPyLongObjectPtrPy_TPFLAGS_LIST_SUBCLASSPyListObjectPtrPy_TPFLAGS_TUPLE_SUBCLASSPyTupleObjectPtrPy_TPFLAGS_BYTES_SUBCLASSPyBytesObjectPtrPy_TPFLAGS_UNICODE_SUBCLASSPyUnicodeObjectPtrPy_TPFLAGS_DICT_SUBCLASSPyDictObjectPtrPy_TPFLAGS_BASE_EXC_SUBCLASSPyBaseExceptionObjectPtr)clstr{   r   name_maps        r   subclass_from_typezPyObjectPtr.subclass_from_typeE  sR   &	ggi((//11G177:..//HH 01 	 	 	 JJJ	 , 0 /-*"03G&3  hG$$)) 	%$$.. 	#"".. 	#""// 	$##// 	$##11 	&%%.. 	#""22 	,++
 
s   A	A A"!A"c                     	 t          |          }|                     |                                          }  | ||                                           S # t          $ r Y nw xY w | |          S )zt
        Try to locate the appropriate derived class dynamically, and cast
        the pointer accordingly.
        )r[   )rT   r   ru   r`   r~   )r   rZ   ps      r   rg   zPyObjectPtr.from_pyobject_ptr  s    	F##A((22C3vs'7'7'9'9:::: 	 	 	 D	 s6{{s   AA 
A$#A$c                 X    t          j        | j                                                  S r&   )r   r   	_typenamer   )r   s    r   r`   zPyObjectPtr.get_gdb_type  s     s}--55777r
   c                 *    t          | j                  S r&   rw   rO   s    r   
as_addresszPyObjectPtr.as_address  s    4<   r
   r&   )r   r   r   rR   r   rI   re   ri   ro   rr   ru   r_   ry   r   r   rk   classmethodr   rg   r`   r   r	   r
   r   rT   rT      s4       	 	 I" " " "0 0 0<? ? ?+ + +  6 6 6& & &
- 
- 
-	 	 	%+ %+ %+N7 7 7 @ @ [@D   [ 8 8 [8! ! ! ! !r
   rT   c                       e Zd ZdZdS )rb   PyVarObjectNr   r   r   r   r	   r
   r   rb   rb     s        IIIr
   rb   c                       e Zd ZdZd Zd ZdS )ProxyAlreadyVisitedz
    Placeholder proxy to use when protecting against infinite recursion due to
    loops in the object graph.

    Analogous to the values emitted by the users of Py_ReprEnter and Py_ReprLeave
    c                     || _         d S r&   _rep)rH   reps     r   rI   zProxyAlreadyVisited.__init__  s    			r
   c                     | j         S r&   r   rO   s    r   r   zProxyAlreadyVisited.__repr__  rQ   r
   Nr   r	   r
   r   r   r     s<               r
   r   c                    |                      d           |                      |           t          |t                    r|                      d           d}|                                D ]q\  }}|s|                      d           d}|                      |                    |                     |                      d           |                    | |           r|                      d           |                      d|z             d	S )
zXShared code for use by all classes:
    write a representation to file-like object "out"<(T, F=)z at remote 0x%x>N)r+   r3   r   	iteritemsr   rk   )rl   rm   rc   pyop_attrdictr   firstpyop_argpyop_vals           r   _write_instance_reprr     s     IIcNNNIIdOOO -11 
		#"/"9"9";"; 	. 	.Hh  		$EIIh''00111IIcNNNW----		#II 7*+++++r
   c                       e Zd Zd Zd ZdS )InstanceProxyc                 0    || _         || _        || _        d S r&   )cl_nameattrdictr   )rH   r   r   r   s       r   rI   zInstanceProxy.__init__  s     r
   c                     t          | j        t                    rHd                    d | j                                        D                       }d| j        || j        fz  S d| j        | j        fz  S )Nr   c                 "    g | ]\  }}|d |S )r   r	   ).0argr#   s      r   
<listcomp>z*InstanceProxy.__repr__.<locals>.<listcomp>  s=      K  K  K$,C ,/33 4  K  K  Kr
   z<%s(%s) at remote 0x%x>r   )r3   r   dictr;   r   r   r   )rH   kwargss     r   r   zInstanceProxy.__repr__  s    dmT** 	:YY  K  K040G0G0I0I K  K  K L LF,060F F F )DL,0L,: : :r
   Nr   r   r   rI   r   r	   r
   r   r   r     s2          
: : : : :r
   r   c                 6   t           j        t          j        d          t           _        |                     d          ||                     d          z  z   t                      dz
  z   t                      dz
   z                      t           j                  S )Nsize_ttp_basicsizetp_itemsize   )_PyObject_VAR_SIZE_type_size_tr   r   re   r   rW   )typeobjnitemss     r   r   r     s    &.*-/(*C*C'}}^,,m4445  1$& "##a'() T$122	3r
   c                   $    e Zd ZdZd Zd Zd ZdS )r   rU   c                    	 |                                  }t          |                    d                    }|dk    r+|dk     rt          j        d                                          }t          | j                            |          d                   }|dk     r| }t          ||          }||z  }|dk    sJ |t                      z  dk    sJ | j                            t                                |z   }t                                                                          }|                    |          }t                              |                                          S n# t          $ r Y nw xY wdS )zw
        Get the PyDictObject ptr representing the attribute dictionary
        (or None if there's a problem)
        tp_dictoffsetr   r   r^   N)ru   int_from_intre   r   r   r   rX   rW   r   r   r   rT   r`   rg   ra   r~   )rH   r   
dictoffsettype_PyVarObject_ptrtsizesizedictptrPyObjectPtrPtrs           r   get_attr_dictzHeapTypeObjectPtr.get_attr_dict  sm   
	iikkG%gmmO&D&DEEJQ>>+.?=+I+I+Q+Q+S+S(():):;O)P)PQZ)[\\Eqyy!&-gu==D$&J%>>>>%(8(88A====,++N,<,<==
J!,!9!9!;!;!C!C!E!E!,,~66"44W5H5H5J5JKKK   	 	 	D	
 ts   E'E+ +
E87E8c                 d   |                                  |v rt          d          S |                    |                                             |                                 }|r|                    |          }ni }|                                 }t          ||t          | j                            S )z
        Support for classes.

        Currently we just locate the dictionary using a transliteration to
        python of _PyObject_GetDictPtr, ignoring descriptors
        <...>)	r   r   addr   r   r   r   r(   rX   )rH   rm   pyop_attr_dict	attr_dictr{   s        r   r   zHeapTypeObjectPtr.proxyval  s     ??''&w///DOO%%&&&++-- 	&//88III##%% WiT\1B1BCCCr
   c                 D   |                                  |v r|                    d           d S |                    |                                             |                                 }t	          |||                                 ||                                             d S )Nr   )r   r+   r   r   r   r   )rH   rl   rm   r   s       r   rk   zHeapTypeObjectPtr.write_repr  s    ??''IIgFDOO%%&&&**,,S'!..00-ARAR	T 	T 	T 	T 	Tr
   N)r   r   r   r   r   r   rk   r	   r
   r   r   r     sN        I  <D D D,	T 	T 	T 	T 	Tr
   r   c                       e Zd Zd Zd ZdS )ProxyExceptionc                 "    || _         || _        d S r&   r{   args)rH   r{   r   s      r   rI   zProxyException.__init__$  s    			r
   c                 "    | j         | j        S r&   r   rO   s    r   r   zProxyException.__repr__(  s    tyy11r
   Nr   r	   r
   r   r   r   #  s2          2 2 2 2 2r
   r   c                   "    e Zd ZdZdZd Zd ZdS )r   z}
    Class wrapping a gdb.Value that's a PyBaseExceptionObject* i.e. an exception
    within the process being debugged.
    PyBaseExceptionObjectc                 .   |                                  |v rt          d          S |                    |                                             |                     d                              |          }t          |                                 |          S N(...)r   )r   r   r   ri   r   r   r   )rH   rm   	arg_proxys      r   r   z!PyBaseExceptionObjectPtr.proxyval2  s    ??''&w///DOO%%&&&OOF++44W==	d//11') ) 	)r
   c                 *   |                                  |v r|                    d           d S |                    |                                             |                    |                                            |                     d||           d S r   )r   r+   r   r   ro   r   s      r   rk   z#PyBaseExceptionObjectPtr.write_repr;  s    ??''IIgFDOO%%&&&		$##%%&&&fc733333r
   N)r   r   r   rR   r   r   rk   r	   r
   r   r   r   +  sC          (I) ) )4 4 4 4 4r
   r   c                       e Zd ZdZdZdS )r   z~
    Class wrapping a gdb.Value that's a PyClassObject* i.e. a <classobj>
    instance within the process being debugged.
    PyClassObjectN)r   r   r   rR   r   r	   r
   r   r   r   E  s           IIIr
   r   c                       e Zd Zd Zd ZdS )BuiltInFunctionProxyc                     || _         d S r&   ml_name)rH   r
  s     r   rI   zBuiltInFunctionProxy.__init__N  s    r
   c                     d| j         z  S )Nz<built-in function %s>r	  rO   s    r   r   zBuiltInFunctionProxy.__repr__Q  s    '$,66r
   Nr   r	   r
   r   r  r  M  s2          7 7 7 7 7r
   r  c                       e Zd Zd Zd ZdS )BuiltInMethodProxyc                 "    || _         || _        d S r&   )r
  pyop_m_self)rH   r
  r  s      r   rI   zBuiltInMethodProxy.__init__U  s    &r
   c                 x    d| j         | j                                        | j                                        fz  S )Nz0<built-in method %s of %s object at remote 0x%x>)r
  r  r   r   rO   s    r   r   zBuiltInMethodProxy.__repr__Y  s=    B<#0022#..0022 	r
   Nr   r	   r
   r   r  r  T  s2        ' ' '    r
   r  c                       e Zd ZdZdZd ZdS )r   z|
    Class wrapping a gdb.Value that's a PyCFunctionObject*
    (see Include/methodobject.h and Objects/methodobject.c)
    PyCFunctionObjectc                    |                      d          }	 |d                                         }n# t          $ r d}Y nw xY w|                     d          }|                                rt          |          S t          ||          S )Nm_mlr
  z<ml_name:UnicodeDecodeError>m_self)re   r}   r   ri   r_   r  r  )rH   rm   r  r
  r  s        r   r   zPyCFunctionObjectPtr.proxyvalg  s    zz&!!	59o,,..GG! 	5 	5 	54GGG	5 ooh//   	<'000%g{;;;s   2 A ANr   r   r   rR   r   r   r	   r
   r   r   r   `  s4          $I< < < < <r
   r   c                       e Zd ZdZdZd ZdS )PyCodeObjectPtrzy
    Class wrapping a gdb.Value that's a PyCodeObject* i.e. a <code> instance
    within the process being debugged.
    PyCodeObjectc                 b   |                      d                              t                                }t          |                     d                    }d}t          |ddd         |ddd                   D ]3\  }}|t          |          z  }||k    r|c S |t          |          z  }4|S )z
        Get the line number for a given bytecode offset

        Analogous to PyCode_Addr2Line; translated from pseudocode in
        Objects/lnotab_notes.txt
        	co_lnotabco_firstlinenor   N   r   )ri   r   rq   r   re   zipr8   )rH   addrqr  linenoaddr	addr_incr	line_incrs          r   	addr2linezPyCodeObjectPtr.addr2line|  s     OOK0099#%%@@	 djj)9::;;$'	##A#	!$Q$$H$H 	% 	% IyC	NN"De||c)nn$FFr
   N)r   r   r   rR   r   r$  r	   r
   r   r  r  u  s4          I    r
   r  c                   .    e Zd ZdZdZd Zd Zd Zd ZdS )r   zw
    Class wrapping a gdb.Value that's a PyDictObject* i.e. a dict instance
    within the process being debugged.
    PyDictObjectc              #     K   |                      d          }|                      d          }|                     |          \  }}t          |          D ]}||         }t          |          r!t                              ||                   }n t                              |d                   }|                                s&t                              |d                   }||fV  dS )zx
        Yields a sequence of (PyObjectPtr key, PyObjectPtr value) pairs,
        analogous to dict.iteritems()
        ma_keys	ma_valuesme_valueme_keyN)re   _get_entriesr)   r(   rT   rg   r_   )	rH   keysvaluesentriesnentriesiep
pyop_valuepyop_keys	            r   r   zPyDictObjectPtr.iteritems  s      
 zz)$$K(( --d33H%% 	- 	-AB6{{ K(::6!9EE

(::2j>JJ
%%'' -&88HFF,,,,	- 	-r
   c                 4   |                                  |v rt          d          S |                    |                                             i }|                                 D ]4\  }}|                    |          }|                    |          }|||<   5|S )N{...})r   r   r   r   r   )rH   rm   resultr4  r3  	proxy_keyproxy_values          r   r   zPyDictObjectPtr.proxyval  s    ??''&w///DOO%%&&&$(NN$4$4 	, 	, Hj ))'22I$--g66K +F9r
   c                    |                                  |v r|                    d           d S |                    |                                             |                    d           d}|                                 D ]_\  }}|s|                    d           d}|                    ||           |                    d           |                    ||           `|                    d           d S )Nr6  {Tr   F: })r   r+   r   r   rk   )rH   rl   rm   r   r4  r3  s         r   rk   zPyDictObjectPtr.write_repr  s    ??''IIgFDOO%%&&&		#$(NN$4$4 	0 	0 Hj  		$EW---IIdOOO!!#w////		#r
   c                    t          |d                   }t          |d                   }	 |d         |fS # t          $ r Y nw xY w|dk    r|}n|dk    rd|z  }n|dk    rd|z  }nd	|z  }|d
         j        }|                    t	                                |z   }t          j        d                                          }|                    |          }||fS )Ndk_nentriesdk_size
dk_entries   i  r  l          
dk_indicesPyDictKeyEntry)r(   r~   r   rW   r   r   r   r   )rH   r-  r?  r@  offsetent_addr	ent_ptr_ts          r   r,  zPyDictObjectPtr._get_entries  s   $}-..d9o&&	%w.. 	 	 	D	 d??FF[FF
""[FF[F%-==!8!:!:;;fDO$455==??	==++$$s   	6 
AAN)	r   r   r   rR   r   r   r   rk   r,  r	   r
   r   r   r     sa          I- - -$    $% % % % %r
   r   c                   $    e Zd ZdZd Zd Zd ZdS )r   PyListObjectc                 <    |                      d          }||         S Nob_itemre   rH   r1  field_ob_items      r   __getitem__zPyListObjectPtr.__getitem__      

9--Qr
   c                                                       v rt          d          S                                                                   fdt          t	                               d                              D             }|S )N[...]c                 v    g | ]5}t                               |                                                 6S r	   rT   rg   r   r   r1  rH   rm   s     r   r   z,PyListObjectPtr.proxyval.<locals>.<listcomp>  sO     L L L //Q88AA'JJ L L Lr
   r^   )r   r   r   r)   r   re   rH   rm   r7  s   `` r   r   zPyListObjectPtr.proxyval  s    ??''&w///DOO%%&&&L L L L L%l4::i3H3H&I&IJJL L Lr
   c                    |                                  |v r|                    d           d S |                    |                                             |                    d           t          t	          |                     d                              D ]S}|dk    r|                    d           t                              | |                   }|                    ||           T|                    d           d S )NrU  [r^   r   r   ]	r   r+   r   r)   r   re   rT   rg   rk   rH   rl   rm   r1  elements        r   rk   zPyListObjectPtr.write_repr  s    ??''IIgFDOO%%&&&		#LI)>)>??@@ 	- 	-A1uu		$!33DG<<GsG,,,,		#r
   Nr   r   r   r   rR  r   rk   r	   r
   r   r   r     sF        I     
      r
   r   c                       e Zd ZdZd Zd ZdS )r   PyLongObjectc                 N   t          |                     d                    }|dk    rdS |                     d          t          j        d          j        dk    rdndfdt          t          |                    D             }t          |          }|dk     r| }|S )	aG  
        Python's Include/longobjrep.h has this declaration:
           struct _longobject {
               PyObject_VAR_HEAD
               digit ob_digit[1];
           };

        with this description:
            The absolute value of a number is equal to
                 SUM(for i=0 through abs(ob_size)-1) ob_digit[i] * 2**(SHIFT*i)
            Negative numbers are represented with ob_size < 0;
            zero is represented by ob_size == 0.

        where SHIFT can be either:
            #define PyLong_SHIFT        30
            #define PyLong_SHIFT        15
        r^   r   ob_digitdigitr        c                 L    g | ] }t          |                   d |z  z  z  !S )r  r(   )r   r1  SHIFTrd  s     r   r   z,PyLongObjectPtr.proxyval.<locals>.<listcomp>&  sB     5 5 5 hqk""Qq\1 5 5 5r
   )r(   re   r   r   r   r)   abssum)rH   rm   r^   digitsr7  rj  rd  s        @@r   r   zPyLongObjectPtr.proxyval	  s    $ djj++,,a<<1::j))?7##*a//EEE5 5 5 5 5%c'll335 5 5VQ;;WFr
   c                 `    |                      |          }|                    d|z             d S )Nz%sr   r+   rH   rl   rm   proxys       r   rk   zPyLongObjectPtr.write_repr-  s/    g&&		$,r
   N)r   r   r   r   r   rk   r	   r
   r   r   r     s8        I" " "H         r
   r   c                       e Zd ZdZd ZdS )r   z
    Class wrapping a gdb.Value that's a PyBoolObject* i.e. one of the two
    <bool> instances (Py_True/Py_False) within the process being debugged.
    c                 @    t                               | |          rdS dS )NTF)r   r   rH   rm   s     r   r   zPyBoolObjectPtr.proxyval8  s$    ##D'22 	45r
   N)r   r   r   rR   r   r	   r
   r   r   r   3  s-             r
   r   c                       e Zd ZdZdZd ZdS )r   z
    Class wrapping a gdb.Value that's a PyObject* pointing to the
    singleton (we hope) _Py_NoneStruct with ob_type PyNone_Type
    rU   c                     d S r&   r	   rt  s     r   r   zPyNoneStructPtr.proxyvalE  s    tr
   Nr  r	   r
   r   r   r   >  s4          I    r
   r   c                   P    e Zd ZdZddZd Zd Zd Zd Zd Z	d	 Z
d
 Zd Zd ZdS )r   PyFrameObjectNc                    t                               | ||           |                                 s#t                              |                     d                    | _        | j                            d          | _        | j                            d          | _	        t          |                     d                    | _        t          |                     d                    | _        t          | j                            d                    | _        t                              | j                            d                    | _        d S d S )Nf_codeco_nameco_filenamef_linenof_lasti
co_nlocalsco_varnames)rT   rI   ry   r  rg   re   cori   r{  r|  r   r}  r~  r  r   r  rY   s      r   rI   zPyFrameObjectPtr.__init__L  s	   T67333$$&& 	`%77

88L8LMMDG7--i88DL#w11-@@D(J)?)?@@DM'

9(=(=>>DL*47==+F+FGGDO/AA$'--P]B^B^__D	` 	`r
   c              #   L  K   |                                  rdS |                     d          }t          | j                  D ]a}t                              ||                   }|                                s+t                              | j        |                   }||fV  bdS )z
        Yield a sequence of (name,value) pairs of PyObjectPtr instances, for
        the local variables of this frame
        Nf_localsplus)ry   re   r)   r  rT   rg   r_   r  )rH   r  r1  r3  	pyop_names        r   iter_localszPyFrameObjectPtr.iter_localsY  s      
   "" 	Fzz.11DO,, 	. 	.A$66|AGGJ%%'' .'99$:J1:MNN	 *----		. 	.r
   c                     |                                  rdS |                     d          }|                                S )z
        Yield a sequence of (name,value) pairs of PyObjectPtr instances, for
        the global variables of this frame
        r	   	f_globalsry   ri   r   )rH   pyop_globalss     r   iter_globalszPyFrameObjectPtr.iter_globalsh  s@    
   "" 	2{33%%'''r
   c                     |                                  rdS |                     d          }|                                S )zt
        Yield a sequence of (name,value) pairs of PyObjectPtr instances, for
        the builtin variables
        r	   
f_builtinsr  )rH   pyop_builtinss     r   iter_builtinszPyFrameObjectPtr.iter_builtinss  s@    
   "" 	255&&(((r
   c                    |                                  D ]0\  }}||                    t                                k    r|dfc S 1|                                 D ]0\  }}||                    t                                k    r|dfc S 1|                                 D ]0\  }}||                    t                                k    r|dfc S 1dS )z
        Look for the named local variable, returning a (PyObjectPtr, scope) pair
        where scope is a string 'local', 'global', 'builtin'

        If not found, return (None, None)
        localglobalbuiltinNN)r  r   rq   r  r  )rH   rc   r  r3  s       r   get_var_by_namez PyFrameObjectPtr.get_var_by_name~  s    &*%5%5%7%7 	+ 	+!Izy))#%%0000!7**** 1%)%6%6%8%8 	, 	,!Izy))#%%0000!8++++ 1%)%7%7%9%9 	- 	-!Izy))#%%0000!9,,,, 1zr
   c                     |                                  rt          S | j                            t	                                S )z;Get the path of the current Python source file, as a string)ry   FRAME_INFO_OPTIMIZED_OUTr|  r   rq   rO   s    r   r<   zPyFrameObjectPtr.filename  s7      "" 	,++((///r
   c                     |                                  rdS |                     d          }t          |          dk    r| j        S 	 | j                            | j                  S # t          $ r Y dS w xY w)zGet current line number as an integer (1-based)

        Translated from PyFrame_GetLineNumber and PyCode_Addr2Line

        See Objects/lnotab_notes.txt
        Nf_tracer   )ry   re   r(   r}  r  r$  r~  	Exception)rH   r  s     r   current_line_numz!PyFrameObjectPtr.current_line_num  s       "" 	4**Y''w<<1= 	7$$T\222 	 	 	 44	s   A& &
A43A4c                    |                                  rt          S |                                 }|dS |                                 }	 t	          t          |                    5 }|                                }ddd           n# 1 swxY w Y   n# t          $ r Y dS w xY w	 ||dz
           S # t          $ r Y dS w xY w)z^Get the text of the current source line as a string, with a trailing
        newline characterNz!(failed to get frame line number)r   )	ry   r  r  r<   openr@   	readlinesOSError
IndexError)rH   r   r<   fpliness        r   current_linezPyFrameObjectPtr.current_line  s      "" 	,++&&((>66==??	k(++,, '' ' ' ' ' ' ' ' ' ' ' ' ' ' ' 	 	 	44		!$$ 	 	 	44	sH   	B %B:B B

B B
B 
B B $
B/ /
B=<B=c           	         |                                  r|                    t                     d S |                                 }|t	          |          nd}|                    d|                                 | j                            |          || j                            |          fz             d}| 	                                D ]q\  }}|s|                    d           d}|                    |                    |                     |                    d           |
                    ||           r|                    d           d S )N?z)Frame 0x%x, for file %s, line %s, in %s (Tr   Fr   r   )ry   r+   r  r  strr   r|  r   r{  r  rk   )rH   rl   rm   r   r   r  r3  s          r   rk   zPyFrameObjectPtr.write_repr  sK     "" 	II.///F&&(( & 2V		=__&&%..w77\**73355 	6 	6 	6
 %)%5%5%7%7 	0 	0!Iz  		$EIIi((11222IIcNNN!!#w////		#r
   c           
         |                                  r)t          j                            dt          z             d S t                      }|                                 }|t          |          nd}t          j                            d| j        	                    |          d|d| j
        	                    |          d           d S )N  %s
r  z  File "z", line z, in 
)ry   r5   stdoutr+   r  rq   r  r  r|  r   r{  )rH   rm   r   s      r   print_tracebackz PyFrameObjectPtr.print_traceback  s      "" 	JX(@@AAAF%%&&(( & 2V
%..w7777VV\**733335 	6 	6 	6 	6 	6r
   r&   )r   r   r   r   rI   r  r  r  r  r<   r  r  rk   r  r	   r
   r   r   r   I  s        I` ` ` `. . .	( 	( 	(	) 	) 	)  $0 0 0  0  .  .
6 
6 
6 
6 
6r
   r   c                   :    e Zd ZdZed             Zd Zd Zd ZdS )r   PySetObjectc                 N    t          j        d                                          S )N_PySet_Dummy)r   lookup_global_symbolvaluerO   s    r   
_dummy_keyzPySetObjectPtr._dummy_key  s    '77==???r
   c              #     K   |                                  }|                     d          }t          |                     d          dz             D ]:}||         }|d         }|dk    r"||k    rt                              |          V  ;d S )Ntablemaskr   keyr   )r  re   r)   rT   rg   )rH   	dummy_ptrr  r1  setentryr  s         r   __iter__zPySetObjectPtr.__iter__  s      OO%%	

7##DJJv..233 	9 	9AQxH5/CaxxC9,,!33C88888		9 	9r
   c                 N   |                                  v r$t          d|                                 z            S                     |                                             fd| D             }|                                 dk    rt	          |          S t          |          S )Nz%s(...)c              3   B   K   | ]}|                               V  d S r&   )r   )r   r  rm   s     r   	<genexpr>z*PySetObjectPtr.proxyval.<locals>.<genexpr>  s/      99S3<<((999999r
   r   )r   r   r   r   r   rq   )rH   rm   memberss    ` r   r   zPySetObjectPtr.proxyval  s    ??''&y43D3D3F3F'FGGGDOO%%&&&9999D999+--W%%%w<<r
   c                    |                                  }|                                 |v r|                    d           d S |                    |                                            |                     d          s,|                    |           |                    d           d S |dk    r*|                    |           |                    d           |                    d           d}| D ]1}|s|                    d           d	}|                    ||           2|                    d
           |dk    r|                    d           d S d S )Nr  usedz()rq   r   r;  Tr   Fr=  r   )r   r   r+   r   re   rk   )rH   rl   rm   r{   r   r  s         r   rk   zPySetObjectPtr.write_repr  sR   ##%% ??''IIgFDOO%%&&& zz&!! 	IIgIIdOOOF eIIgIIcNNN		# 	) 	)C  		$ENN3((((		#eIIcNNNNN r
   N)	r   r   r   r   r   r  r  r   rk   r	   r
   r   r   r     sb        I@ @ [@9 9 9
  
  
     r
   r   c                   $    e Zd ZdZd Zd Zd ZdS )r   PyBytesObjectc                     |                      d          }|                      d          }|j                            t                                d                    fdt          |          D                       S )Nr^   ob_svalr2   c                 :    g | ]}t          |                   S r	   r9   )r   r1  char_ptrs     r   r   z,PyBytesObjectPtr.__str__.<locals>.<listcomp>-  s%    LLLQHQK((LLLr
   )re   r   rW   r   r;   r)   )rH   field_ob_sizefield_ob_svalr  s      @r   __str__zPyBytesObjectPtr.__str__)  sq    

9--

9-- (--.E.G.GHHwwLLLL*]2K2KLLLMMMr
   c                      t          |           S r&   )r  rt  s     r   r   zPyBytesObjectPtr.proxyval/  s    4yyr
   c                 :   |                      |          }d}d|v rd|vrd}|                    d           |                    |           |D ]6}||k    s|dk    r+|                    d           |                    |           :|dk    r|                    d           V|dk    r|                    d           r|d	k    r|                    d
           |dk     st          |          dk    rz|                    d           |                    t          t          |          dz  dz	                      |                    t          t          |          dz                      !|                    |           8|                    |           d S )N'"b\	\tr  \n\r    \x   rC  rf  )r   r+   r8   	hexdigits)rH   rl   rm   rq  quoter?   s         r   rk   zPyBytesObjectPtr.write_repr2  s    g&& %<<uE		#		% 	  	 Du}}		$		$		%    		%    		%    s4yyD00		%   		)SYY%5!$;<===		)CIIO45555		$		%r
   N)r   r   r   r   r  r   rk   r	   r
   r   r   r   &  sI        IN N N      r
   r   c                   $    e Zd ZdZd Zd Zd ZdS )r   PyTupleObjectc                 <    |                      d          }||         S rM  rO  rP  s      r   rR  zPyTupleObjectPtr.__getitem__T  rS  r
   c           
      4                                      v rt          d          S                                                                  t           fdt	          t                               d                              D                       }|S )Nr  c              3   ~   K   | ]7}t                               |                                                 V  8d S r&   rW  rX  s     r   r  z,PyTupleObjectPtr.proxyval.<locals>.<genexpr>_  s_       Q Q #44T!W==FFwOO Q Q Q Q Q Qr
   r^   )r   r   r   tupler)   r   re   rY  s   `` r   r   zPyTupleObjectPtr.proxyvalY  s    ??''&w///DOO%%&&& Q Q Q Q Q *<

98M8M+N+N O OQ Q Q Q Qr
   c                 h   |                                  |v r|                    d           d S |                    |                                             |                    d           t          t	          |                     d                              D ]S}|dk    r|                    d           t                              | |                   }|                    ||           T|                     d          dk    r|                    d           d S |                    d           d S )	Nr  r   r^   r   r   r   z,)r   r]  r^  s        r   rk   zPyTupleObjectPtr.write_reprc  s	   ??''IIgFDOO%%&&&		#LI)>)>??@@ 	- 	-A1uu		$!33DG<<GsG,,,,::i  A%%IIdOOOOOIIcNNNNNr
   Nr`  r	   r
   r   r   r   Q  sF        I     
      r
   r   c                       e Zd ZdZdS )rt   PyTypeObjectNr   r	   r
   r   rt   rt   u  s        IIIr
   rt   c                 H    | dk    rdS dd l }|                    |           dvS )Nr  Tr   )CZ)unicodedatacategory)r   r  s     r   _unichr_is_printabler  y  s5    s{{t%%Z77r
   c                   $    e Zd ZdZd Zd Zd ZdS )r   PyUnicodeObjectc                 8    t          j        d          }|j        S )N
Py_UNICODE)r   r   r   )rH   _type_Py_UNICODEs     r   
char_widthzPyUnicodeObjectPtr.char_width  s    ?<88&&r
   c                    t           4t          j        d                                          }dd |D             v a t           rqd}|                     d          }|d         }|d         }t          |d                   ot          |d                   }t          |d	                   s!t          |d
                   }d}|d         n9t          |d                   }|r|j        dz   n;t          |d                   r|j        dz   n|                     d          d         t          |d                   }	|	dk    r"                    t                                n|	dk    r"                    t                                nw|	dk    r!                    t                                nOt          |                     d                    }|                     d          |                                 dk    }|sfdt          |          D             }
ng }
d}t          |          }||k     rt          |                   }|dz  }|dk     s|dk    s||k    r|
                    |           Ht          |                   }|dk     s|dk    rj|dz  dz  }||dz  z  }|dz  }|
                    |           |dz  }||k     d                    d |
D                       }|S )Nr  rM   c                     g | ]	}|j         
S r	   )rc   )r   fs     r   r   z/PyUnicodeObjectPtr.proxyval.<locals>.<listcomp>  s    #;#;#;qAF#;#;#;r
   F_basestateasciicompactreadywstr_lengthTwstrlengthr   anykindr  rC  r  c                 :    g | ]}t          |                   S r	   ri  )r   r1  	field_strs     r   r   z/PyUnicodeObjectPtr.proxyval.<locals>.<listcomp>  s%    OOO3y|,,OOOr
   r      r1       
      r2   c                 <    g | ]}|d k    rt          |          ndS )i u   �r  )r   ucss     r   r   z/PyUnicodeObjectPtr.proxyval.<locals>.<listcomp>  s:     $ $ $ SXXXh$ $ $r
   )
_is_pep393r   r   fieldsre   r(   r   rW   r   r   r   r  r)   r$   r:   r;   )rH   rm   r  may_have_surrogatesr  r  r  is_compact_asciifield_length	repr_kindPy_UNICODEsr1  limitr  ucs2coder7  r  s                    @r   r   zPyUnicodeObjectPtr.proxyval  s,   _%677>>@@F#;#;F#;#;#;;J 	9"'jj))GG$E'NE #E'N 3 3 ME)<L8M8MuW~&& I"7=#9::&*#!&M		"5?33# : % 1IIy)** : '! 3II $

6 2 25 9If..	>> )/F/H/H I III!^^ )/G/I/I J JII!^^ )/E/G/G H HI tzz(3344L

5))I"&//"3"3q"8 # 	OOOOj6N6NOOOKK KA ..Ee)))A,''Q<<3&==A4E4E&&s+++9Q<((&==D6MMf+v%
"""4(((Q e))&  $ $"$ $ $ % % r
   c                 
   |                      |          }d|v rd|vrd}nd}|                    |           d}|t          |          k     r||         }|dz  }||k    s|dk    r,|                    d           |                    |           n|dk    r|                    d           nm|dk    r|                    d	           nP|d
k    r|                    d           n3|dk     s|dk    rz|                    d           |                    t          t	          |          dz	  dz                      |                    t          t	          |          dz                      nt	          |          dk     r|                    |           n|}d }t
          j        dk     re|t          |          k     rRdt	          |          cxk    rdk     r8n n5dt	          ||                   cxk    rdk    rn n||         }||z   }|dz  }t          |          }	|	r.	 |                    t                     n# t          $ r d}	Y nw xY w|	s|0t	          |          dz  dz  }
|
t	          |          dz  z  }
|
dz  }
nt	          |          }
|
dk    r`|                    d           |                    t          |
dz	  dz                      |                    t          |
dz                      n!|
dk    rC|                    d           |                    t          |
dz	  dz                      |                    t          |
dz	  dz                      |                    t          |
dz	  dz                      |                    t          |
dz	  dz                      |                    t          |
dz	  dz                      |                    t          |
dz	  dz                      |                    t          |
dz	  dz                      |                    t          |
dz                      n|                    d            |                    t          |
dz	  dz                      |                    t          |
dz	  dz                      |                    t          |
dz	  dz                      |                    t          |
dz                      n,|                    |           ||                    |           |t          |          k     |                    |           d S )!Nr  r  r   r   r  r  r  r  r  r  r  r  r  r  rC  rf  r  r  r1   r  Fr   r  rB  z\U               rD  z\u)r   r+   rL   r  r8   r5   
maxunicoder  r7   ENCODINGUnicodeEncodeError)rH   rl   rm   rq  r  r1  chr  ch2	printabler  s              r   rk   zPyUnicodeObjectPtr.write_repr  s(    g&& %<<Cu,,EEE		%#e**nnqBFA U{{bDjj		$		" t		%    t		%    t		%     cR4ZZ		%   		)SWW\V$;<===		)CGGf$456666 R4		" >G++ CJJ &#b'' : : : :F : : : : : &#eAh-- A A A A6 A A A A A#Ah 3hQ 155	 **

8,,,,- * * *$)			*
 ! %' !$B& 0R7C6 11
*"3xx t||		%(((		)TQY&,@"ABBB		)D6M":;;;;		%(((		)TRZ:,E"FGGG		)TRZ:,E"FGGG		)TRZ:,E"FGGG		)TRZ:,E"FGGG		)TRZ:,E"FGGG		)TQY*,D"EFFF		)TQY*,D"EFFF		)D:,=">???? 		%(((		)TRZ6,A"BCCC		)TQY&,@"ABBB		)TQY&,@"ABBB		)D6M":;;;; IIbMMM		#s #e**nnv 			%s   %I   IIN)r   r   r   r   r  r   rk   r	   r
   r   r   r     sO        !I' ' 'F F FPj j j j jr
   r   c                   0    e Zd Zd Zd Zd Zd Zd Zd ZdS )r   c                     	 |                      d          d         d                                         }t          |          S # t          t          t
          f$ r Y dS w xY w)Ndescrd_baserc   z<unknown name>)re   r}   r   r   r~   r   rh   s     r   	safe_namezwrapperobject.safe_name@  sf    	$::g&&x08??AAD::/AB 	$ 	$ 	$###	$s   AA AAc                     	 |                      d          d         d                                         S # t          t          t          f$ r Y dS w xY w)NrH   r]   r{   z<unknown tp_name>)re   r}   r   r~   r   rO   s    r   r   zwrapperobject.safe_tp_nameG  s[    	'::f%%i0;BBDDD/AB 	' 	' 	'&&&	's   25 AAc                     	 t          |                     d                    }d|z  S # t          t          f$ r Y dS w xY w)NrH   z%#xz<failed to get self address>)r(   re   r   r~   )rH   r   s     r   safe_self_addresssz wrapperobject.safe_self_addresssM  sS    	2$**V,,--G7?". 	2 	2 	2111	2s   &) >>c                     |                                  }|                                 }|                                 }d|d|d|dS )Nz<method-wrapper z of z object at >)r  r   r"  )rH   rm   rc   r{   self_addresss        r   r   zwrapperobject.proxyvalT  sR    ~~##%%..0044,,,0 	1r
   c                 Z    |                      |          }|                    |           d S r&   ro  rp  s       r   rk   zwrapperobject.write_repr[  s+    g&&		%r
   N)	r   r   r   r   r  r   r"  r   rk   r	   r
   r   r   r   =  sd        I$ $ $' ' '2 2 21 1 1    r
   r   c                      t          |           S r&   ri  rZ   s    r   r   r   `  s    v;;r
   c                 "    	 t          |           S r&   )r   pprintpformat)r#   r+  s     r   	stringifyr,  d  s    Cyyr
   c                       e Zd ZdZd Zd ZdS )PyObjectPtrPrinterzPrints a (PyObject*)c                     || _         d S r&   r(  )rH   rZ   s     r   rI   zPyObjectPtrPrinter.__init__q  s    r
   c                 v    t                               | j                  }	 |                    t                    S r&   )rT   rg   rZ   rr   MAX_OUTPUT_LENr   rq   r,  )rH   pyopr   s      r   	to_stringzPyObjectPtrPrinter.to_stringt  s0    ,,T[99	'**>:::r
   N)r   r   r   rR   rI   r3  r	   r
   r   r.  r.  n  s8          ' ' ' ' 'r
   r.  c                     | j                                         }|j        t          j        k    rd S |                                                                }t          |          }|dv rt          |           S d S )N)rU   rx  r  r   )ru   unqualifiedr  r   TYPE_CODE_PTRtargetr  r.  )rZ   ru   r   s      r   pretty_printer_lookupr8  ~  sq    ;""$$DyC%%%t;;==$$&&DD		AMMM!&))) NMr
   c                 V    | t           } | j                            t                     d S r&   )r   pretty_printersr:   r8  )objs    r   registerr<    s,    
{ 455555r
   c                       e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zed             Zed             Zed             Zd Zd ZdS )Framez7
    Wrapper for gdb.Frame, adding various methods
    c                     || _         d S r&   )	_gdbframe)rH   gdbframes     r   rI   zFrame.__init__  s    !r
   c                 Z    | j                                         }|rt          |          S d S r&   )r@  olderr>  )rH   rC  s     r   rC  zFrame.older  .    $$&& 	<<4r
   c                 Z    | j                                         }|rt          |          S d S r&   )r@  newerr>  )rH   rF  s     r   rF  zFrame.newer  rD  r
   c                     t          | j        d          st          d           dS | j                                         dS )zIf supported, select this frame and return True; return False if unsupported

        Not all builds have a gdb.Frame.select method; seems to be present on Fedora 12
        onwards, but absent on Ubuntu buildbotselectzSUnable to select frame: this build of gdb does not expose a gdb.Frame.select methodFT)hasattrr@  printrH  rO   s    r   rH  zFrame.select  sQ    
 t~x00 	 Q R R R5tr
   c                     d}| }|                                 r-|dz  }|                                 }|                                 -|S )zWCalculate index of frame, starting at 0 for the newest frame within
        this threadr   r   )rF  )rH   index
iter_frames      r   	get_indexzFrame.get_index  sY     
   	,QJE#))++J    	, r
   c                 ^    |                                  rdS |                                 rdS dS )zIs this a _PyEval_EvalFrameDefault frame, or some other important
        frame? (see is_other_python_frame for what "important" means in this
        context)TF)is_evalframeis_other_python_framerO   s    r   is_python_framezFrame.is_python_frame  s;      	4%%'' 	4ur
   c                     | j                                         t          k    r*	 | j                                         t          j        k    rdS dS )z)Is this a _PyEval_EvalFrameDefault frame?TF)r@  rc   	EVALFRAMEru   r   NORMAL_FRAMErO   s    r   rP  zFrame.is_evalframe  sH    >  I-- ~""$$(888tur
   c                    |                                  rdS |                                 rdS | j        }|                                }|sdS |                    d          s|dk    rLd}	 |                    |          }t          |          S # t          $ r d|z  cY S t          $ r d|z  cY S w xY w|d	k    rLd
}	 |                    |          }t          |          S # t          $ r d|z  cY S t          $ r d|z  cY S w xY wdS )a  Is this frame worth displaying in python backtraces?
        Examples:
          - waiting on the GIL
          - garbage-collecting
          - within a CFunction
         If it is, return a descriptive string
         For other frames, return False
         zWaiting for the GILzGarbage-collectingFcfunction_vectorcall_cfunction_callfuncz?PyCFunction invocation (unable to read %s: missing debuginfos?)z*PyCFunction invocation (unable to read %s)wrapper_callwpzB<wrapper_call invocation (unable to read %s: missing debuginfos?)>z-<wrapper_call invocation (unable to read %s)>)	is_waiting_for_gilis_gc_collectr@  rc   
startswithread_varr  
ValueErrorr~   )rH   r   callerarg_namerY  s        r   rQ  zFrame.is_other_python_frame  s    ""$$ 	)(( 	(''  	5566 	O&&&HO~~h//4yy  ; ; ;/19: ; ; ; O O OChNNNNO ^##HR~~h//4yy  < < <02:; < < < R R RFQQQQR us0   *#B B0B0/B0<#C   D1DDc                 H    | j                                         }|r|dk    S dS )z!Is this frame waiting on the GIL?take_gilNr@  rc   rh   s     r   r\  zFrame.is_waiting_for_gil,  s4     ~""$$ 	(J&'	( 	(r
   c                 <    | j                                         dk    S )z5Is this frame "collect" within the garbage-collector?collectre  rO   s    r   r]  zFrame.is_gc_collect3  s    ~""$$	11r
   c                    	 | j                             d          }t                              |          }|                                s|S |}| j                                         }|rE|                    d          }t                              |          }|                                s|S |S # t          $ r Y d S w xY w)Nr  )r@  r_  r   rg   ry   rC  r`  )rH   r  r   
orig_framera  s        r   get_pyopzFrame.get_pyop7  s    	'',,A$66q99E))++  J^))++F !OOC(((::1==--// ! L 	 	 	44	s   A	B0 A!B0 .B0 0
B>=B>c                 N    t          j                    }|rt          |          S d S r&   )r   selected_framer>  )r   r@  s     r   get_selected_framezFrame.get_selected_frameK  s+    &((	 	$###tr
   c                     	 |                                  }n# t          j        $ r Y dS w xY w|r,|                                r|S |                                }|,dS )zZTry to obtain the Frame for the python-related code in the selected
        frame, or NoneN)rm  r   errorrR  rC  r   r   s     r   get_selected_python_framezFrame.get_selected_python_frameR  s    	**,,EEy 	 	 	44	  	"$$&& KKMME  	" ts    **c                     |                                  }|r,|                                r|S |                                }|,dS )zfTry to obtain the Frame for the python bytecode interpreter in the
        selected GDB frame, or NoneN)rm  rP  rC  rp  s     r   get_selected_bytecode_framez!Frame.get_selected_bytecode_framed  sT     &&(( 	"!!## KKMME  	" tr
   c                 (   |                                  r|                                 }|r|                    t                    }t	          t
          j        d|                                 |fz             |                                sL|	                                }|8t
          j        
                    d|                                z             d S d S d S t
          j        
                    d|                                 z             d S |                                 }|r8t
          j        
                    d|                                 |fz             d S t
          j        
                    d|                                 z             d S )Nz#%i %s
    %s
z.#%i (unable to read python frame information)
z#%i
)rP  rj  rr   r1  r.   r5   r  rN  ry   r  r+   striprQ  rH   r2  lineinfos       r   print_summaryzFrame.print_summaryr  sq    	===??D g..~>>cj*8H8H$7O*OPPP,,.. D,,..D'
((djjll)BCCCCCD D'' 
  !RUYUcUcUeUe!efffff--//D =
  t~~/?/?.F!FGGGGG
  4>>+;+;!;<<<<<r
   c                 >   |                                  r|                                 }|rx|                                 |                                sL|                                }|8t
          j                            d|                                z             d S d S d S t
          j                            d           d S | 	                                }|r$t
          j                            d|z             d S t
          j                            d           d S )Nru  z,  (unable to read python frame information)
r  z  (not a python frame)
)
rP  rj  r  ry   r  r5   r  r+   rv  rQ  rw  s       r   r  zFrame.print_traceback  s    	===??D R$$&&&,,.. D,,..D'
((djjll)BCCCCCD D'' 
  !PQQQQQ--//D =
  D11111
  !;<<<<<r
   N)r   r   r   rR   rI   rC  rF  rH  rN  rR  rP  rQ  r\  r]  rj  r   rm  rq  rs  rz  r  r	   r
   r   r>  r>    s3        " " "    
 
 
	 	 	$     3 3 3j( ( (2 2 2  (   [   ["   [= = =&= = = = =r
   r>  c                       e Zd ZdZd Zd ZdS )PyListzList the current Python source code, if any

    Use
       py-list START
    to list at a different line number within the python source.

    Use
       py-list START, END
    to list a specific range of lines within the python source.
    c                 r    t           j                            | dt           j        t           j                   d S )Nzpy-list)r   CommandrI   COMMAND_FILESCOMPLETE_NONErO   s    r   rI   zPyList.__init__  7    d'!/!/	1 	1 	1 	1 	1r
   c                 \   dd l }d }d } |j        d|          }|r't          |                    d                    }|dz   } |j        d|          }|r*t	          t          |                                          \  }}t                                          }|st          d           d S |	                                }|r|
                                rt          t                     d S |                                }	|                                }
|
t          d           d S |
|
dz
  }|
dz   }|dk     rd}	 t          t          |	                    }n># t           $ r1}t"          j                            d	|	d
|d           Y d }~d S d }~ww xY w|5  |                                }t+          ||dz
  |                   D ]J\  }}t-          ||z             }||z   |
k    rd|z   }t"          j                            |dd|           K	 d d d            d S # 1 swxY w Y   d S )Nr   z\s*(\d+)\s*r  z\s*(\d+)\s*,\s*(\d+)\s*z:Unable to locate gdb frame for python bytecode interpreterz'Unable to read python frame line number   r   zUnable to open r<  r  r$  z>4z    )rematchr(   groupmapgroupsr>  rs  rJ  rj  ry   UNABLE_READ_INFO_PYTHON_FRAMEr<   r  r  r@   r  r5   r  r+   r  	enumerater  )rH   r   from_ttyr  startendmr   r2  r<   r   r  err	all_linesr1  rx  linestrs                    r   invokezPyList.invoke  s   			BH^T** 	

OOE"*CBH/66 	.S!((**--JE3 1133 	NOOOF~~ 	t,,.. 	/000F==??&&((>;<<<F=QJE1*C77E	[**++AA 	 	 	J (###/ 0 0 0FFFFF	  
	@ 
	@I %YuQws{%;<< @ @4ag,,u9&&!GmG
  !>????@
	@ 
	@ 
	@ 
	@ 
	@ 
	@ 
	@ 
	@ 
	@ 
	@ 
	@ 
	@ 
	@ 
	@ 
	@ 
	@ 
	@ 
	@s+   <E 
F#&FFA:H!!H%(H%Nr   r   r   rR   rI   r  r	   r
   r   r}  r}    sA        	 	1 1 17@ 7@ 7@ 7@ 7@r
   r}  c                    t                                           }|st          d           dS |rp| r|                                }n|                                }|snB|                                r*|                                r|                                 dS |}|p| rt          d           dS t          d           dS )z9Move up or down the stack (for the py-up/py-down command)Unable to locate python frameNz$Unable to find an older python framez#Unable to find a newer python frame)r>  rq  rJ  rC  rF  rR  rH  rz  )move_upr   rM  s      r   move_in_stackr    s    ++--E -...
  	'JJJ 	%%'' 	  "" +((***F  "  5455555344444r
   c                       e Zd ZdZd Zd ZdS )PyUpzESelect and print the python stack frame that called this one (if any)c                 r    t           j                            | dt           j        t           j                   d S )Nzpy-upr   r  rI   COMMAND_STACKr  rO   s    r   rI   zPyUp.__init__  7    d%!/!/	1 	1 	1 	1 	1r
   c                 &    t          d           d S )NTr  r  rH   r   r  s      r   r  zPyUp.invoke  s    d######r
   Nr  r	   r
   r   r  r    s8        KK1 1 1$ $ $ $ $r
   r  c                       e Zd ZdZd Zd ZdS )PyDownzCSelect and print the python stack frame called by this one (if any)c                 r    t           j                            | dt           j        t           j                   d S )Nzpy-downr  rO   s    r   rI   zPyDown.__init__  r  r
   c                 &    t          d           d S )NFr  r  r  s      r   r  zPyDown.invoke  s    e$$$$$$r
   Nr  r	   r
   r   r  r    s8        II1 1 1% % % % %r
   r  rH  c                       e Zd ZdZd Zd ZdS )PyBacktraceFullRDisplay the current python frame and all the frames within its call stack (if any)c                 r    t           j                            | dt           j        t           j                   d S )Nz
py-bt-fullr  rO   s    r   rI   zPyBacktraceFull.__init__#  s7    d*!/!/	1 	1 	1 	1 	1r
   c                     t                                           }|st          d           d S |r@|                                r|                                 |                                }|>d S d S )Nr  )r>  rq  rJ  rR  rz  rC  rH   r   r  r   s       r   r  zPyBacktraceFull.invoke*  s    //11 	1222F 	"$$&& &##%%%KKMME  	" 	" 	" 	" 	"r
   Nr  r	   r
   r   r  r  !  s8        XX1 1 1	" 	" 	" 	" 	"r
   r  c                       e Zd ZdZd Zd ZdS )PyBacktracer  c                 r    t           j                            | dt           j        t           j                   d S )Nzpy-btr  rO   s    r   rI   zPyBacktrace.__init__9  r  r
   c                     t                                           }|st          d           d S t          j                            d           |r@|                                r|                                 |                                }|>d S d S )Nr  z$Traceback (most recent call first):
)	r>  rq  rJ  r5   r  r+   rR  r  rC  r  s       r   r  zPyBacktrace.invoke@  s    //11 	1222F
@AAA 	"$$&& (%%'''KKMME  	" 	" 	" 	" 	"r
   Nr  r	   r
   r   r  r  7  s8        XX1 1 1
" 
" 
" 
" 
"r
   r  c                       e Zd ZdZd Zd ZdS )PyPrint4Look up the given python variable name, and print itc                 r    t           j                            | dt           j        t           j                   d S )Nzpy-printr   r  rI   COMMAND_DATAr  rO   s    r   rI   zPyPrint.__init__P  s7    d(!.!/	1 	1 	1 	1 	1r
   c           	         t          |          }t                                          }|st          d           d S |                                }|st          t
                     d S |                    |          \  }}|r1t          |d|d|                    t                               d S t          d|z             d S )Nr  r   = z%r not found)	r  r>  rq  rJ  rj  r  r  rr   r1  )rH   r   r  rc   r   
pyop_framepyop_varscopes           r   r  zPyPrint.invokeW  s    4yy//11 	1222F^^%%
 	/000F$44T::% 	)eedd11.AAAC D D D D D
 .4'(((((r
   Nr  r	   r
   r   r  r  N  s8        ::1 1 1) ) ) ) )r
   r  c                       e Zd ZdZd Zd ZdS )PyLocalsr  c                 r    t           j                            | dt           j        t           j                   d S )Nz	py-localsr  rO   s    r   rI   zPyLocals.__init__r  s7    d)!.!/	1 	1 	1 	1 	1r
   c                    t          |          }t                                          }|st          d           d S |                                }|st          t
                     d S |                                D ]P\  }}t          |                    t                                d|	                    t                               Qd S Nr  r  r  r>  rq  rJ  rj  r  r  r   rq   rr   r1  rH   r   r  rc   r   r  r  r3  s           r   r  zPyLocals.invokey  s    4yy//11 	1222F^^%%
 	/000F%/%;%;%=%= 	 	!Iz""355))))--n===    	 	r
   Nr  r	   r
   r   r  r  p  s8        ::1 1 1    r
   r  c                 F     t          j                    fd            }|S )z*sigh*, readlinec                  Z    	  | i |S # t           $ r t          j                      w xY wr&   )r  	traceback	print_exc)r   r   functions     r   wrapperz%dont_suppress_errors.<locals>.wrapper  sJ    	8T,V,,, 	 	 	!!!	s    *)	functoolswraps)r  r  s   ` r   dont_suppress_errorsr    s8    _X     Nr
   c                   4    e Zd ZdZd Zed             Zd ZdS )	PyGlobalsz9List all the globals in the currently select Python framec                 r    t           j                            | dt           j        t           j                   d S )Nz
py-globalsr  rO   s    r   rI   zPyGlobals.__init__  s7    d*!.!/	1 	1 	1 	1 	1r
   c                    t          |          }t                                          }|st          d           d S |                                }|st          t
                     d S |                                D ]P\  }}t          |                    t                                d|	                    t                               Qd S r  r  r  s           r   r  zPyGlobals.invoke  s    4yy//11 	1222F^^%%
 	/000F%/%;%;%=%= 	F 	F!Iz((//// 33NCCCE F F F F	F 	Fr
   c                 *    |                                 S r&   )r  )rH   r  s     r   get_namespacezPyGlobals.get_namespace  s    &&(((r
   N)r   r   r   rR   rI   r  r  r  r	   r
   r   r  r    sW        ??1 1 1 F F F$) ) ) ) )r
   r  c                     | j                                         dk    r*	 | j                                         t          j        k    rdS dS )z#Is this a PyEval_EvalFrameEx frame?PyEval_EvalFrameExTF)r@  rc   ru   r   rU  )r   s    r   is_evalframeexr    sI    !555	 ?!!S%55545r
   c                   *    e Zd Zd Zed             ZdS )PyNameEqualsc                    t          t          j                              }t          |          r[|                                }|t          j        d           d S t          ||                              t                                S d S )NzBUse a Python debug build, Python breakpoints won't work otherwise.)
r>  r   rl  r  rj  warningswarngetattrr   rq   )rH   attrr   pyframes       r   _get_pycurframe_attrz!PyNameEquals._get_pycurframe_attr  s    c(**++%   	:nn&&G 6 7 7 7t7D))22355999tr
   c                 d    |                      d          }|d uo||                                k    S )Nr{  )r  r}   )rH   funcnamer  s      r   r  zPyNameEquals.invoke  s4    ((334=DHOO,=,=$==r
   N)r   r   r   r  r  r  r	   r
   r   r  r    s?           > > > > >r
   r  pyname_equalsc                   $    e Zd Zed             ZdS )PyModEqualsc                     |                      d          }|Wt          j                            t          j                            |                    \  }}||                                k    S dS )Nr|  F)r  ospathsplitextbasenamer}   )rH   modnamer  r<   exts        r   r  zPyModEquals.invoke  s^    ((77G,,RW-=-=d-C-CDDMHcw~~////ur
   N)r   r   r   r  r  r	   r
   r   r  r    s.            r
   r  pymod_equalsc                   (    e Zd ZdZed             ZdS )PyBreakz
    Set a Python breakpoint. Examples:

    Break on any function or method named 'func' in module 'modname'

        py-break modname.func

    Break on any function or method named 'func'

        py-break func
    c                     d|v r#|                     d          \  }}}d|d|d}nd|z  }t          j        d|z              d S )N.z$pyname_equals("z") && $pymod_equals("z")z$pyname_equals("%s")zbreak PyEval_EvalFrameEx if )
rpartitionr   execute)rH   r  r  r  dotconds         r   r  zPyBreak.invoke  sg    (??%-%8%8%=%="GS((DLHHDKGGMDD *H4D2T9:::::r
   N)r   r   r   rR   r  r  r	   r
   r   r  r    s:        
 
 ; ; ; ; ;r
   r  zpy-breakc                   *    e Zd ZdZd Zd Zd Zd ZdS )_LoggingStatezI
    State that helps to provide a reentrant gdb.execute() function.
    c                     t          j        d          }|| _        |j        | _        |                                | _        t          d| j        z             g | _        d S )Nzr+zset logging file %s)	tempfileNamedTemporaryFiler,   rc   r<   filenofd_executefile_position_stack)rH   r  s     r   rI   z_LoggingState.__init__  sU    '--	((**&6777#%   r
   c                     | j         s-t          d           t          d           t          d           | j                             t          j        | j                  j                   | S )Nzset logging redirect onzset logging onzset pagination off)r  r  r:   r  fstatr  st_sizerO   s    r   	__enter__z_LoggingState.__enter__'  se    ' 	+.///%&&&)*** ''(9(9(ABBBr
   c                     t          j                     | j                            | j        d                    | j                                        }|S )N)r   flushr,   seekr  read)rH   r7  s     r   	getoutputz_LoggingState.getoutput0  s?    		t/3444!!r
   c                 
   | j                                         }| j                            |           | j                                         | j         s/t          d           t          d           t          d           d S d S )Nzset logging offzset logging redirect offzset pagination on)r  popr,   r  truncater  )rH   exc_typeexc_valtbstartposs        r   __exit__z_LoggingState.__exit__6  s    +//11	x   	' 	*&'''/000()))))	* 	*r
   N)r   r   r   rR   rI   r  r  r  r	   r
   r   r  r    sZ         & & &    * * * * *r
   r  Fc                     |rEt           5 }t          | |           |                                cddd           S # 1 swxY w Y   dS t          | |           dS )z
    Replace gdb.execute() with this function and have it accept a 'to_string'
    argument (new in 7.2). Have it properly capture stderr also. Ensure
    reentrancy.
    N)_logging_stater  r  )commandr  r3  r  s       r   r  r  @  s      $ 	%uWh'''??$$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	(#####s   $;??c                  4    t          j                    d         S )z.
    Return the selected inferior in gdb.
    r   )r   	inferiorsselected_threadthreads)r  inferiorthreads      r   get_selected_inferiorr  S  s     =??1r
   c                    t          j                    \  }}t          j        |d          }|                    |            |                                 t          j        d|z  |           t          j        |           dS )a  
    Source a gdb script with script_contents passed as a string. This is useful
    to provide defines for py-step and py-next to make them repeatable (this is
    not possible with gdb.execute()). See
    http://sourceware.org/bugzilla/show_bug.cgi?id=12216
    wz	source %sr3  N)	r  mkstempr  fdopenr+   closer   r  remove)script_contentsr3  r  r<   r  s        r   source_gdb_scriptr   c  sv     #%%LB
	"cAGGOGGIIIKh&)<<<<Ihr
   c                  z    t          t          j        d          t          j        t
          j        fz             d S )Nz        define py-step
        -py-step
        end

        define py-next
        -py-next
        end

        document py-step
        %s
        end

        document py-next
        %s
        end
    )r   textwrapdedentPySteprR   PyNextr	   r
   r   register_definesr&  r  sD    ho ' 	 	  NFN+!, - - - - -r
   c                 D    d}| r|                                  } |dz  }| |S )z$Tells the stackdepth of a gdb frame.r   r   )rC  )r   depths     r   
stackdepthr)    s6    E
 
   Lr
   c                   f     e Zd ZdZ fdZd Zd Zd Zd Zd Z	d Z
d	 Zd
 ZddZd Zd Z xZS )ExecutionControlCommandBasez
    Superclass for language specific execution control. Language specific
    features should be implemented by lang_info using the LanguageInfo
    interface. 'name' is the name of the command.
    c                     t                                          |t          j        t          j                   || _        d S r&   )superrI   r   COMMAND_RUNNINGr  	lang_info)rH   rc   r/  	__class__s      r   rI   z$ExecutionControlCommandBase.__init__  s:     $c&93;L	N 	N 	N"r
   c              #      K   t          j        | j                                        | j                                                  }|D ]E}t          j        d|z  d          }t          j        d|          	                    d          V  Fd S )Nzbreak %sTr  Breakpoint (\d+)r   )
	itertoolschainr/  static_break_functionsruntime_break_functionsr   r  r  searchr  )rH   all_locationslocationr7  s       r   install_breakpointsz/ExecutionControlCommandBase.install_breakpoints  s      !N1133N22446 6 & 	B 	BH[h!6$GGGF)/88>>qAAAAAA	B 	Br
   c                 >    |D ]}t          j        d|z             d S )N	delete %s)r   r  )rH   breakpoint_listbps      r   delete_breakpointsz.ExecutionControlCommandBase.delete_breakpoints  s3    ! 	* 	*BKb())))	* 	*r
   c                    t           j        }d|t           j        z  fdd|fg}d|fd|fg}fd}t          j        dt           j                  }|rd|                    d	          z  }nd
} ||          | ||          z   fS )Nz^Program received signal .*)z.*[Ww]arning.*r   z^Program exited .*z^(Old|New) value = .*z^\d+: \w+ = .*c                     g }| D ]F\  }}t          j        ||          D ]*}|                    |                    d                     +Gd                    |          S )Nr   r  )r  finditerr:   r  r;   )regexesoutputregexflagsr  r7  s        r   filter_outputz@ExecutionControlCommandBase.filter_output.<locals>.filter_output  sp    F ' 2 2u[>> 2 2EMM%++a..11112 99V$$$r
   z^Value returned is \$\d+ = (.*)zValue returned: %s
r   r2   )r  	MULTILINEDOTALLr7  r  )rH   r7  reflagsoutput_on_haltoutput_alwaysrG  match_finishfinish_outputs    `      r   rG  z)ExecutionControlCommandBase.filter_output  s    , ,WRY->?""G,
 &w/(	
	% 	% 	% 	% 	% y!CV!#/ / 	2\5G5G5J5JJMMMn--m < <<> 	>r
   c                 0    t                      j        dk    S rK   )r  pidrO   s    r   stoppedz#ExecutionControlCommandBase.stopped  s    $&&*a//r
   c                 0   |                      |          \  }}|                                 r t          |           t          |           dS t          j                    }| j                            |          }| j                            |          r+| j                            |          }|rt          |           |rF|	                                r!t          |	                                           t          |           dS t          |           dS )z
        After doing some kind of code running in the inferior, print the line
        of source code or the result of the last executed gdb command (passed
        in as the `result` argument).
        N)
rG  rQ  rJ  r   rl  r/  get_source_lineis_relevant_functionexc_inforstrip)rH   r7  rK  rL  r   source_lineraised_exceptions          r   finish_executingz,ExecutionControlCommandBase.finish_executing  s    )-(:(:6(B(B%<<>> 	-   .!!!!!&((E.88??K~22599 ,#'>#:#:5#A#A # ,*+++  '')) 2-..00111k"""""fr
   c                     t          j                                                    t          j        dd          S t          j        dd          S )zd
        Execute until the function returns (or until something else makes it
        stop)
        NfinishTr  cont)r   rl  rC  r  rO   s    r   _finishz#ExecutionControlCommandBase._finish  sK    
 %%''3;x48888 ;v6666r
   c                    	 |                                  }	 t          j                    }n# t          $ r Y nMw xY wt	          j        d|          }| j                            |          }|s|s|                                 rn|S )zJ
        Execute until the function returns to a relevant caller.
        Tr2  )	r]  r   rl  r~   r  r7  r/  rT  rQ  )rH   r7  r   hitbpis_relevants        r   _finish_framez)ExecutionControlCommandBase._finish_frame  s    	\\^^F*,,    I16::E.==eDDK  t||~~ 	 s   + 
88c                 X    |                                  }|                     |           dS )zImplements the finish command.N)ra  rY  )rH   r   r7  s      r   r[  z"ExecutionControlCommandBase.finish	  s-    ##%%f%%%%%r
   nextc                    |r!t          |                                           }t          j                    }| j                            |          r+| j                            |          }|st          |          }|}	 | j                            |          rt          j        |d          }n| 	                                }| 
                                rnt          j                    }| j                            |          }		 |                                }
n# t          $ r d}
Y nw xY wt          j        d|          }|r|	r|                    d          |v rnH||k    r|st          |          }||k     o|	}	|	rn&n#| j                            |          }|r||k    rn(|r|                     |           |                     |           dS )a  
        Do a single step or step-over. Returns the result of the last gdb
        command that made execution stop.

        This implementation, for stepping, sets (conditional) breakpoints for
        all functions that are deemed relevant. It then does a step over until
        either something halts execution, or until the next line is reached.

        If, however, stepover_command is given, it should be a string gdb
        command that continues execution in some way. The idea is that the
        caller has set a (conditional) breakpoint or watchpoint that can work
        more efficiently than the step-over loop. For Python this means setting
        a watchpoint for f->f_lasti, which means we can then subsequently
        "finish" frames.
        We want f->f_lasti instead of f->f_lineno, because the latter only
        works properly with local trace functions, see
        PyFrameObjectPtr.current_line_num and PyFrameObjectPtr.addr2line.
        Tr  Nr2  r   )listr:  r   rl  r/  rT  r   r)  r  ra  rQ  rc   r~   r  r7  r  r?  rY  )rH   stepintostepover_commandr=  
beginframe	beginliner(  newframer7  rT  	framenamer  newdepthr   s                 r   stepz ExecutionControlCommandBase.step	  s#   &  	?"4#;#;#=#=>>O'))
>..z:: 	/
 --j99I /":..(	~228<< .%5FFF++--||~~ )++H#'>#F#Fx#P#P !$MMOO		 ! ! ! 			! 	-v66A ' AGGAJJ/,I,I :%%   B)(33H,4u,< -A,@ ) ( 
 ..x88 f	11Q(	T  	5##O444f%%%%%s   D D+*D+c                 ^    |                      t          j        d|z   d                     d S )Nzrun Tr  rY  r   r  r  s      r   runzExecutionControlCommandBase.run_	  s/    ck&4-4HHHIIIIIr
   c                 X    |                      t          j        dd                     d S )Nr\  Tr  ro  )rH   r   s     r   r\  z ExecutionControlCommandBase.contb	  s+    ck&DAAABBBBBr
   )rc  )r   r   r   rR   rI   r:  r?  rG  rQ  rY  r]  ra  r[  rm  rp  r\  __classcell__r0  s   @r   r+  r+    s         # # # # #
B B B* * *!> !> !>F0 0 0  4	7 	7 	7  && & &
Q& Q& Q& Q&fJ J JC C C C C C Cr
   r+  c                       e Zd ZdZd Zd ZdS )LanguageInfoa  
    This class defines the interface that ExecutionControlCommandBase needs to
    provide language-specific execution control.

    Classes that implement this interface should implement:

        lineno(frame)
            Tells the current line number (only called for a relevant frame).
            If lineno is a false value it is not checked for a difference.

        is_relevant_function(frame)
            tells whether we care about frame 'frame'

        get_source_line(frame)
            get the line of source code for the current line (only called for a
            relevant frame). If the source code cannot be retrieved this
            function should return None

        exc_info(frame) -- optional
            tells whether an exception was raised, if so, it should return a
            string representation of the exception value, None otherwise.

        static_break_functions()
            returns an iterable of function names that are considered relevant
            and should halt step-into execution. This is needed to provide a
            performing step-into

        runtime_break_functions() -- optional
            list of functions that we should break into depending on the
            context
    c                     dS )zSee this class' docstring.Nr	   rH   r   s     r   rU  zLanguageInfo.exc_info	  s      r
   c                     dS )zc
        Implement this if the list of step-into functions depends on the
        context.
        r	   r	   rO   s    r   r6  z$LanguageInfo.runtime_break_functions	  s	    
 rr
   N)r   r   r   rR   rU  r6  r	   r
   r   ru  ru  f	  s=         @% % %    r
   ru  c                   2    e Zd Zd Zd Zd Zd Zd Zd ZdS )
PythonInfoc                 t    t          |                                          }|r|S t          j        d          )NzuUnable to find the Python frame, run your code with a debug build (configure with --with-pydebug or compile with -g).)r>  rj  r   r~   rH   r   r  s      r   r  zPythonInfo.pyframe	  sE    ,,'')) 	MN"LM M Mr
   c                 P    |                      |                                          S r&   )r  r  rw  s     r   r   zPythonInfo.lineno	  s     ||E""33555r
   c                 D    t          |                                          S r&   )r>  r  rw  s     r   rT  zPythonInfo.is_relevant_function	  s    U||**,,,r
   c                     	 |                      |          }d|                                |                                                                fz  S # t          $ r Y d S w xY w)Nz	%4d    %s)r  r  r  rV  r  r|  s      r   rS  zPythonInfo.get_source_line	  sx    	ll5))G'":":"<"<")"6"6"8"8"?"?"A"A"C C C 	 	 	44	s   AA 
A"!A"c                    	 |                     d                                          }t          j        d          r2t          j        dk    r|d         x}}n|d         }|d         }|rd|S d S d S # t          t          f$ r Y d S w xY w)Ntstateztstate->frame == f)   r  r   alpha   current_exceptioncurexc_typecurexc_valuezAn exception was raised: )r_  ra   r   parse_and_evalr5   version_infor`  r~   )rH   r   r  inf_type	inf_values        r   rU  zPythonInfo.exc_info	  s    	^^H--99;;F!"677 	H#'===+12E+FFHyy%m4H &~ 6I H H<EIGG	H 	HH HL) 	 	 	DD	s   A,A3 3BBc              #      K   dV  d S )Nr  r	   rO   s    r   r5  z!PythonInfo.static_break_functions	  s      """"""r
   N)	r   r   r   r  r   rT  rS  rU  r5  r	   r
   r   rz  rz  	  sq        M M M6 6 6- - -    "# # # # #r
   rz  c                       e Zd ZdZd ZdS )PythonStepperMixinzr
    Make this a mixin so CyStep can also inherit from this and use a
    CythonCodeStepper at the same time.
    c                     t          j        dd          }t          t          j        d|                              d                    }|                     |d           t          j        d|z             d	S )
zq
        Set a watchpoint on the Python bytecode instruction pointer and try
        to finish the frame
        zwatch f->f_lastiTr  z[Ww]atchpoint (\d+):r   r[  )rf  rg  r<  N)r   r  r(   r  r7  r  rm  )rH   rf  rD  
watchpoints       r   python_stepzPythonStepperMixin.python_step	  sr    
 /4@@@#:FCCII!LLMM
		8h	???K*,-----r
   N)r   r   r   rR   r  r	   r
   r   r  r  	  s-         
. . . . .r
   r  c                   ,    e Zd ZdZdZed             ZdS )r$  zStep through Python code.Tc                 <    |                      | j                   d S )N)rf  )r  rf  r  s      r   r  zPyStep.invoke	  s!    $-00000r
   N)r   r   r   rR   rf  r  r  r	   r
   r   r$  r$  	  s9        H1 1 1 1 1r
   r$  c                       e Zd ZdZdZdS )r%  zStep-over Python code.FN)r   r   r   rR   rf  r	   r
   r   r%  r%  	  s        HHHr
   r%  c                   2    e Zd ZdZ eej                  ZdS )PyFinishz+Execute until function returns to a caller.N)r   r   r   rR   r  r+  r[  r  r	   r
   r   r  r  	  s(        11!!"="DEEFFFr
   r  c                   2    e Zd ZdZ eej                  ZdS )PyRunzRun the program.N)r   r   r   rR   r  r+  rp  r  r	   r
   r   r  r  	  s(        !!"="ABBFFFr
   r  c                   .    e Zd Z eej                  ZdS )PyContN)r   r   r   r  r+  r\  r  r	   r
   r   r  r  	  s"        !!"="BCCFFFr
   r  c                 V    | j         t          | j                   S t          |           S )zb
    Return the value of the pointer as a Python int.

    gdbval.type must be a pointer type
    )r   r(   r(  s    r   _pointervaluer  	  s*     ~!6>""" 6{{r
   c                 |    t          |           }	 |dk     rt          j        d          n# t          $ r Y nw xY w|S )Nr   z:Negative pointer value, presumably a bug in gdb, aborting.)r  r   GdbErrorr~   )rZ   r   s     r   pointervaluer  
  sf    F##GQ;;,  3 4 4 4      	
 Ns   , 
99c                      	 t          j        d           dS # t          $ r+ 	 t          j        d           Y dS # t          $ r Y Y dS w xY ww xY w)NPyUnicode_FromEncodedObjectr2   PyUnicodeUCS2_FromEncodedObjectUCS2UCS4)r   r  r~   r	   r
   r   get_inferior_unicode_postfixr  
  s    
8999 r    	@AAA 66  	 	 	666	s$    
A:
A	AA		Ac                   F    e Zd ZdZdZdZd Zd Zd Zd Z	d Z
d	 ZddZd
S )PythonCodeExecutor   i  i  c                     t          j        d|z            }t          |          }|dk    rt          j        d          |S )Nz(void *) malloc((size_t) %d)r   z-No memory could be allocated in the inferior.)r   r  r  r  )rH   r   chunkr   s       r   malloczPythonCodeExecutor.malloc#
  sD    #$BT$IJJu%%a<<,NOOOr
   c                     |                      t          |                    }t                                          ||           |S r&   )r  rL   r  write_memory)rH   r}   r   s      r   alloc_stringzPythonCodeExecutor.alloc_string,
  s:    ++c&kk**,,Wf===r
   c                    |                      |          }d}	 t          j        |           n"# t          $ r dt	                      d}Y nw xY w	 t          j        d||t          |          fz            }|                     |           n# |                     |           w xY wt          |          }|dk    rt          j        d          |S )NPyString_FromStringAndSize	PyUnicode_FromStringAndSizez)(PyObject *) %s((char *) %d, (size_t) %d)r   z1Unable to allocate Python string in the inferior.)	r  r   r  r~   r  rL   freer  r  )rH   r}   stringpr  r7  r   s         r   alloc_pystringz!PythonCodeExecutor.alloc_pystring2
  s   ##F++%A"	R9:::: 	R 	R 	R 	R 1M0N0N0N0N+Q&&&	R
	';6V?N NO OF IIgDIIgv&&a<<,  / 0 0 0 s   . AA'B B%c                 4    t          j        d|z             d S )Nz(void) free((void *) %d)r   r  rH   r   s     r   r  zPythonCodeExecutor.freeK
  s    5?@@@@@r
   c                 4    t          j        d|z             dS )zAIncrement the reference count of a Python object in the inferior.zPy_IncRef((PyObject *) %d)Nr  r  s     r   increfzPythonCodeExecutor.increfN
  s    7'ABBBBBr
   c                 4    t          j        d|z             dS )zADecrement the reference count of a Python object in the inferior.zPy_DecRef((PyObject *) %d)Nr  r  s     r   xdecrefzPythonCodeExecutor.xdecrefR
  s!    
 	7'ABBBBBr
   Nc                    d|v rt          j        d          |dz  }|                     |          }t          |          }t          |          }|dk    s|dk    rt          j        d          dt	          ||||          z  }t                      5  	 t          j        |          }|                     |           n# |                     |           w xY w	 ddd           n# 1 swxY w Y   |S )aH  
        Evaluate python code `code` given as a string in the inferior and
        return the result as a gdb.Value. Returns a new reference in the
        inferior.

        Of course, executing any code in the inferior may be dangerous and may
        leave the debuggee in an unsafe state or terminate it altogether.
         zString contains NUL byte.r   z-Unable to obtain or create locals or globals.z
            PyRun_String(
                (char *) %(code)d,
                (int) %(start)d,
                (PyObject *) %(globals)s,
                (PyObject *) %(locals)d)
        )r  r  globalslocalsN)r   r  r  r  r   FetchAndRestoreErrorr  r  )	rH   r  
input_typeglobal_dict
local_dictr   globalsplocalsppyobject_return_values	            r   evalcodezPythonCodeExecutor.evalcodeY
  sP    4<<,:;;;##D)),,z**q==GqLL,NOOO z#G5 5 55 "## 	# 	##(+(:4(@(@%		'""""		'"""""		# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# 	# %$s*   C(C+C(CC((C,/C,r  )r   r   r   Py_single_inputPy_file_inputPy_eval_inputr  r  r  r  r  r  r  r	   r
   r   r  r  
  s        OMM      2A A AC C CC C C%% %% %% %% %% %%r
   r  c                   $    e Zd ZdZd Zd Zd ZdS )r  zg
    Context manager that fetches the error indicator in the inferior and
    restores it on exit.
    c                    t          j        d                                          j        | _        |                     | j        dz            | _        | j        }| j        | j        z   }| j        | j        dz  z   }|||f| _        d S )NrU   r  r  )r   r   r   r   sizeof_PyObjectPtrr  errstate)rH   ru   r  r  s       r   rI   zFetchAndRestoreError.__init__
  sz    "%/*"="="E"E"G"G"N{{4#:Q#>??|t66L4#:Q#>>	eY.r
   c                 >    t          j        d| j        z             d S )NzPyErr_Fetch(%d, %d, %d))r   r  r  rO   s    r   r  zFetchAndRestoreError.__enter__
  s!    4t}DEEEEEr
   c                    t          j        d          rt          j        d           d}	 t          j        || j        z             |                     | j                   d S # |                     | j                   w xY w)Nz(int) PyErr_Occurred()zPyErr_Print()zAPyErr_Restore((PyObject *) *%d,(PyObject *) *%d,(PyObject *) *%d))r   r  r  r  r   )rH   r   pyerr_restores      r   r  zFetchAndRestoreError.__exit__
  s    677 	0///0
	$}t}<===IIdl#####DIIdl####s   A$ $B N)r   r   r   rR   rI   r  r  r	   r
   r   r  r  
  sN         
/ / /F F F$ $ $ $ $r
   r  c                   :     e Zd Z fdZd Zed             Z xZS )FixGdbCommandc                     t                                          |t          j        t          j                   || _        d S r&   )r-  rI   r   r  r  actual_command)rH   r  r  r0  s      r   rI   zFixGdbCommand.__init__
  s:    #"2,/,=	? 	? 	?,r
   c                     t          j        ddt          t          j        t
                               	 t          t          j        d                    dk     dS # t          $ r Y dS w xY w)a  
        It seems that invoking either 'cy exec' and 'py-exec' work perfectly
        fine, but after this gdb's python API is entirely broken.
        Maybe some uncleared exception value is still set?
        sys.exc_clear() didn't help. A demonstration:

        (gdb) cy exec 'hello'
        'hello'
        (gdb) python gdb.execute('cont')
        RuntimeError: Cannot convert value to int.
        Error while executing Python code.
        (gdb) python gdb.execute('cont')
        [15148 refs]

        Program exited normally.
        ignorez.*z
(void *) 0r   N)
r  filterwarningsRuntimeWarningr  escaper   r(   r   r  r~   rO   s    r   fix_gdbzFixGdbCommand.fix_gdb
  sw    " 	% "	( 3 3	5 	5 	5	"<0011Q6666 	 	 	DD	s   %A 
A*)A*c                     |                                   	 t          j        | j        d|           n3# t          $ r&}t          j        t          |                    d }~ww xY w|                                   d S )Nr  )r  r   r  r  r~   r  r  )rH   r   r  es       r   r  zFixGdbCommand.invoke
  s}    	'K4#6#6#6=>>>> 	' 	' 	',s1vv&&&	's   5 
A%!A  A%)r   r   r   rI   r  r  r  rr  rs  s   @r   r  r  
  sc        - - - - -
  2       r
   r  c                     t          j        d          }t          j        d          }t          |          dk    st          |          dk    rt          j        d          |                     ||||          S )z=
    Execute Python code in the most recent stack frame.
    zPyEval_GetGlobals()zPyEval_GetLocals()r   ziUnable to find the locals or globals of the most recent Python function (relative to the selected frame).)r   r  r  r  r  )executorr  r  r  r  s        r   _evalcode_pythonr  
  s     $%:;;K#$899J[!!Q&&,z*B*Ba*G*Gl . / / 	/ T:{JGGGr
   c                   *    e Zd Zd Zed             ZdS )PyExecc                 
   |r|t           j        fS g }	 	 t          d          }|                                dk    rn'|                    |           n# t
          $ r Y nw xY wPd                    |          t           j        fS )NTr$  r  r  )r  r  inputrV  r:   EOFErrorr;   r  )rH   exprr  rx  s       r   readcodezPyExec.readcode
  s     	F+;;;E	'' ::D {{}}--LL&&&&     E	' 99U##%7%EEEs   A 
A A c                     |                      |          \  }}t                      }|                    t          ||t          t
                               d S r&   )r  r  r  r  r  r  )rH   r  r  r  r  s        r   r  zPyExec.invoke
  sL    ==..j%'')(JZXXYYYYYr
   N)r   r   r   r  r  r  r	   r
   r   r  r  
  sG        F F F$ Z Z Z Z Zr
   r  zset breakpoint pending onr  z-py-stepz-py-nextz	py-finishzpy-runzpy-contzpy-execz-py-execz1Use gdb 7.2 or higher to use the py-exec command.)FF)F)rR   r   r  localer5   r   r   r   r   r   r  r   r   r   r   r   r   r   r   Py_TPFLAGS_TYPE_SUBCLASSr1  r  getpreferredencodingr  r  r  rT  r~   r   r$   r)   r.   fsencoder@   AttributeErrorrB   rD   rT   rb   r   r   r   r   r   r   r  r   r   r   r  r  r   r  r   r   r   r   r   r   r   r   r   rt   r  r   r   r   r,  r.  r8  r<  current_objfiler>  r  r}  r  r  r  rI  r  r  r  r  r  r  r  r  r"  r3  r  r  r  r  Functionr  r  r  r.  r  r  r  r  r  r  r   r&  r)  r+  ru  rz  r  r$  r%  r  r  r  r  r  r  r  r  r  r  r  py_steppy_next	py_finishpy_runpy_contpy_execr  _py_execr  r	   r
   r   <module>r     sD  ( (Z 


 				  



- - -6 6 67 7 75 5 54 4 4
 
  '  '  '  '  '  '  '  '  	&6&((>  L &		 	 	 	 	l 	 	 	  ) ) )
   +KK                $	 	 	 	 	l 	 	 	       &u! u! u! u! u! u! u! u!n    [          , , ,*: : : : : : : :"3 3 3 #'  @T @T @T @T @T @T @T @TD2 2 2 2 2Y 2 2 24 4 4 4 4{ 4 4 44         {      7 7 7 7 7 7 7 7
 
 
 
 
 
 
 
< < < < <; < < <*    k   :P% P% P% P% P%k P% P% P%f    k   B*  *  *  *  * k *  *  * Z	 	 	 	 	o 	 	 	    k   ]6 ]6 ]6 ]6 ]6{ ]6 ]6 ]6~; ; ; ; ;[ ; ; ;|) ) ) ) ){ ) ) )V" " " " "{ " " "H    k   8 8 8y y y y y y y yx         K      F    ' ' ' ' ' ' ' ' * * *&6 6 6 	
#

 
  ! ! !k= k= k= k= k= k= k= k=ZJ@ J@ J@ J@ J@S[ J@ J@ J@\ 5 5 5:
$ 
$ 
$ 
$ 
$3; 
$ 
$ 
$
% 
% 
% 
% 
%S[ 
% 
% 
% 739h DFFF
FHHH" " " " "ck " " "(    " " " " "#+ " " "* ) ) ) ) )ck ) ) )@ 			    s{   8 	


 
			               
 
 
) ) ) ) ) ) ) )> 
	   > > > > >3< > > >( _       ,    N   ; ; ; ; ;ck ; ; ;0 
C'): ; ; ;#* #* #* #* #* #* #* #*L$ $ $ $ ;         - - -(  SC SC SC SC SC#+ SC SC SCl) ) ) ) ) ) ) )X+# +# +# +# +# +# +# +#\. . . . . . . ."1 1 1 1 1(*< 1 1 1    V   F F F F F* F F FC C C C C' C C CD D D D D( D D D
      a% a% a% a% a% a% a% a%H$ $ $ $ $- $ $ $D' ' ' ' 'CK ' ' 'TH H HZ Z Z Z ZS[ Z Z Z6 ' ( ( (
73
 GfZ..GfZ..Gjjll33IU8ZZ\\**FfY

--GmIz22Gvj#"2C4EFFHHHHMEFFFFFs   'A/ /A:9A: