
    hj                    	   S r SSKrSSKrSSKrSSKrS rS rS rS rS r	Sq
SrS	rS
rSrSrSrSrSrSrSrSr\R,                  " 5       rSrSrSr " S S\5      rS rS rS r \R@                  r! " S S\5      r# " S S5      r$ " S  S!5      r% " S" S#\%5      r& " S$ S%5      r'S& r( " S' S(5      r)S) r*S\*l+         " S* S+\%5      r, " S, S-\-5      r. " S. S/\%5      r/ " S0 S1\%5      r0 " S2 S35      r1 " S4 S55      r2 " S6 S7\%5      r3 " S8 S9\%5      r4 " S: S;\%5      r5 " S< S=\%5      r6 " S> S?\%5      r7 " S@ SA\75      r8 " SB SC\%5      r9 " SD SE\%5      r: " SF SG\%5      r; " SH SI\%5      r< " SJ SK\%5      r= " SL SM\%5      r>SN r? " SO SP\%5      r@ " SQ SR\%5      rASS rBST rC " SU SV5      rDSW rE SX rF\F" \R                  " 5       5         " SY SZ5      rH " S[ S\\R                  5      rJ\J" 5         S] rK " S^ S_\R                  5      rL " S` Sa\R                  5      rM\N" \R                  Sb5      (       a  \L" 5         \M" 5          " Sc Sd\R                  5      rO\O" 5          " Se Sf\R                  5      rP\P" 5          " Sg Sh\R                  5      rQ\Q" 5          " Si Sj\R                  5      rR\R" 5         SSKSrSSSKTrTSSKUrUSSKVrVSSKWrWSSKXrXSSKYrYSk rZ " Sl Sm\R                  5      r[\[" 5         Sn r\ " So Sp\R                  5      r^\^" Sq5         " Sr Ss\^5      r_\_" St5         " Su Sv\R                  5      r`\`" Sw\R                  \R                  5         " Sx Sy5      rcSSz jrd\R                  re\d\ld        \c" 5       rfS{ rgSS| jrhS} riS~ rj " S S\R                  5      rk " S S5      rl " S S\l5      rm " S S5      rn " S S\k\n5      ro " S S\o5      rp " S S\k5      rq " S S\k5      rr " S S\k5      rsS rtS ruS rv " S S5      rw " S S\w5      rx " S S\R                  5      ryS rz " S S\R                  5      r{\R                  " S5        \N" \S5      (       av  \o" S\m" 5       5      r|\p" S\m" 5       5      r}\i" 5         \q" S\m" 5       5      r~\r" S\m" 5       5      r\s" S\m" 5       5      r\y" SS5      r\{" S\GR                  \R                  5      rg\TGR                  " S5        g! \" a    S r! GNf = f)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                  J    [         R                  " S5      R                  5       $ )Nchargdblookup_typepointer     S/var/www/fran/franai/venv/lib/python3.13/site-packages/Cython/Debugger/libpython.py_type_char_ptrr   ?   s    ??6"**,,r
   c                  J    [         R                  " S5      R                  5       $ )Nzunsigned charr   r	   r
   r   _type_unsigned_char_ptrr   C   s    ???+3355r
   c                  J    [         R                  " S5      R                  5       $ )Nzunsigned shortr   r	   r
   r   _type_unsigned_short_ptrr   G   s    ??+,4466r
   c                  J    [         R                  " S5      R                  5       $ )Nzunsigned intr   r	   r
   r   _type_unsigned_int_ptrr   K   s    ??>*2244r
   c                  ^    [         R                  " S5      R                  5       R                  $ )Nvoid)r   r   r   sizeofr	   r
   r   _sizeof_void_pr   O   s     ??6"**,3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                       \ rS rSrSrg)NullPyObjectPtrk   r	   N__name__
__module____qualname____firstlineno____static_attributes__r	   r
   r   r   r   k       r
   r   c                     [        U S5      $ )N  )minvals    r   safety_limitr(   o   s     sD>r
   c                 <    [        [        [        U 5      5      5      $ N)ranger(   intr&   s    r   
safe_ranger-   v   s     c#h'((r
   c                 &    U R                  U5        g r*   )write)filetexts     r   write_unicoder2   {   s    JJtr
   c                 f   [        U [        5      (       d  U $ [        R                  " 5       nUS:X  a  U R	                  U5      $ / nU  HV  nS[        U5      s=::  a  S::  a  O  O[        [        U5      S-
  5      nOUR	                  U5      nUR                  U5        MX     SR                  U5      $ )Nmbcsi  i      )	
isinstanceunicodesysgetfilesystemencodingencodeordchrappendjoin)filenameencodingencodedr   bytes        r   os_fsencoderD      s    (G,,O,,.v??8,,DT,f,3t9v-.{{8,NN4   wwwr
   c                       \ rS rSrSrg)StringTruncated   r	   Nr   r	   r
   r   rF   rF      r"   r
   rF   c                   .    \ rS rSrSrSS jrS rS rSrg)	TruncatedStringIO   zZSimilar to io.StringIO, but can truncate the output by raising a
StringTruncated exceptionNc                     SU l         Xl        g )Nr6   _valmaxlen)selfrN   s     r   __init__TruncatedStringIO.__init__   s    	r
   c                 2   U R                   (       aq  [        U5      [        U R                  5      -   U R                   :  aB  U =R                  USU R                   [        U R                  5      -
   -  sl        [        5       eU =R                  U-  sl        g Nr   )rN   lenrM   rF   )rO   datas     r   r/   TruncatedStringIO.write   sc    ;;4y3tyy>)DKK7		T!DKK#dii.$@AA	%''		T	r
   c                     U R                   $ r*   )rM   rO   s    r   getvalueTruncatedStringIO.getvalue       yyr
   rL   r*   )	r   r   r   r    __doc__rP   r/   rY   r!   r	   r
   r   rI   rI      s    !r
   rI   c                       \ rS rSrSrSrSS jrS rS rS r	S	 r
S
 rS rS rS rS rS r\S 5       r\S 5       r\S 5       rS rSrg)PyObjectPtr   aO  
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                 L    U(       a  UR                  U5      U l        g Xl        g r*   )cast_gdbvalrO   gdbvalcast_tos      r   rP   PyObjectPtr.__init__   s    !;;w/DL!Lr
   c                    U R                  5       (       a  [        U 5      eUS:X  a@  U R                  R                  [        R                  5       5      nUR                  5       U   $ US:X  a@  U R                  R                  [        R                  5       5      nUR                  5       U   $ U R                  R                  5       U   $ )a9  
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   rc   rb   r^   get_gdb_typedereferencePyVarObjectPtr)rO   namepyo_ptrs      r   fieldPyObjectPtr.field   s      <<>>!$''9ll''(@(@(BCG&&(..9ll''(C(C(EFG&&(.. ||'')$//r
   c                 J    [         R                  U R                  U5      5      $ )z~
Get a PyObjectPtr for the given PyObject* field within this PyObject,
coping with some python 2 versus python 3 differences.
)r^   from_pyobject_ptrrq   rO   ro   s     r   
pyop_fieldPyObjectPtr.pyop_field   s    
 ,,TZZ-=>>r
   c                 H    U R                  U5      nUR                  X#5        g)zb
Extract the PyObject* field named "name", and write its representation
to file-like object "out"
N)rv   
write_repr)rO   ro   outvisited	field_objs        r   write_field_reprPyObjectPtr.write_field_repr   s     
 OOD)	S*r
   c                     [        U5      n U R                  U[        5       5        UR	                  5       $ ! [         a    UR	                  5       S-   s $ f = f)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))rI   ry   setrF   rY   )rO   rN   rz   s      r   get_truncated_reprPyObjectPtr.get_truncated_repr   sT    
  '	5OOC' ||~  	5<<>$444	5s   7 AAc                 6    [        U R                  S5      5      $ )Nri   )PyTypeObjectPtrrq   rX   s    r   typePyObjectPtr.type   s    tzz)455r
   c                 2    S[        U R                  5      :H  $ rS   r,   rc   rX   s    r   rk   PyObjectPtr.is_null   s    C%%%r
   c                 .    U R                   R                  $ )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"
)rc   is_optimized_outrX   s    r   r   PyObjectPtr.is_optimized_out   s     ||,,,r
   c                      U R                  5       nUR                  S5      nUR                  5       $ ! [        [        [
        4 a     gf = f)Ntp_nameunknown)r   rq   stringr   RuntimeErrorUnicodeDecodeError)rO   ri   r   s      r   safe_tp_namePyObjectPtr.safe_tp_name	  sI    	iikGmmI.G>>##  /AB 		s   03 A
Ac                 j     " S S5      nU" U R                  5       [        U R                  5      5      $ )a  
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                   $    \ rS rSrSrS rS rSrg)&PyObjectPtr.proxyval.<locals>.FakeRepri%  z
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                     Xl         X l        g r*   )r   address)rO   r   r   s      r   rP   /PyObjectPtr.proxyval.<locals>.FakeRepr.__init__,  s    &&r
   c                 Z    U R                   S:X  a  gSU R                  U R                   4-  $ )Nr   0x0<%s at remote 0x%x>r   r   rX   s    r   __repr__/PyObjectPtr.proxyval.<locals>.FakeRepr.__repr__0  s+     <<1$ ,dll/KKKr
   r   Nr   r   r   r    r\   rP   r   r!   r	   r
   r   FakeReprr   %  s    'Lr
   r   )r   r,   rc   )rO   r{   r   s      r   proxyvalPyObjectPtr.proxyval  s4    "	L 	L& ))+DLL)+ 	+r
   c                 T    UR                  [        U R                  U5      5      5      $ )zl
Write a string representation of the value scraped from the inferior
process to "out", a file-like object.
)r/   reprr   rO   rz   r{   s      r   ry   PyObjectPtr.write_repr;  s!     yydmmG4566r
   c           	      R    UR                  S5      R                  5       n[        UR                  S5      5      n[
        [        [        [        [        [        [        [        S.nX$;   a  XB   $ U[        -  (       a  [        $ U[        -  (       a  [        $ U[         -  (       a  ["        $ U[$        -  (       a  [&        $ U[(        -  (       a  [*        $ U[,        -  (       a  [.        $ U[0        -  (       a  [2        $ U[4        -  (       a  [6        $ U $ ! [        [        4 a    U s $ f = f)ap  
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framer   	frozensetbuiltin_function_or_methodzmethod-wrapper)rq   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_typePyObjectPtr.subclass_from_typeE  s   &	ggi(//1G177:./H , 0 /-*"03G&3 $$))$$..""..""//##//##11%%..""22++
 
S 01 	 J	s   9D D&%D&c                      [        U5      nU R                  UR                  5       5      n U " XR                  5       S9$ ! [         a     Of = fU " U5      $ )z\
Try to locate the appropriate derived class dynamically, and cast
the pointer accordingly.
)rf   )r^   r   r   rl   r   )r   re   ps      r   rt   PyObjectPtr.from_pyobject_ptr  s[    	F#A((2Cv'7'7'9:: 	 	 6{s   =A   
AAc                 ^    [         R                  " U R                  5      R                  5       $ r*   )r   r   	_typenamer   )r   s    r   rl   PyObjectPtr.get_gdb_type  s    s}}-5577r
   c                 ,    [        U R                  5      $ r*   r   rX   s    r   
as_addressPyObjectPtr.as_address  s    4<<  r
   )rc   r*   )r   r   r   r    r\   r   rP   rq   rv   r}   r   r   rk   r   r   r   ry   classmethodr   rt   rl   r   r!   r	   r
   r   r^   r^      s    	 I"0<?+6&
-	%+N7 @ @D   8 8!r
   r^   c                       \ rS rSrSrSrg)rn   i  PyVarObjectr	   Nr   r   r   r    r   r!   r	   r
   r   rn   rn     s    Ir
   rn   c                   $    \ rS rSrSrS rS rSrg)ProxyAlreadyVisitedi  z
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                     Xl         g r*   _rep)rO   reps     r   rP   ProxyAlreadyVisited.__init__  s    	r
   c                     U R                   $ r*   r   rX   s    r   r   ProxyAlreadyVisited.__repr__  r[   r
   r   Nr   r	   r
   r   r   r     s    r
   r   c                    U R                  S5        U R                  U5        [        U[        5      (       a  U R                  S5        SnUR                  5        Ha  u  pgU(       d  U R                  S5        SnU R                  UR	                  U5      5        U R                  S5        UR                  X5        Mc     U R                  S5        U R                  SU-  5        g	)
zTShared code for use by all classes:
write a representation to file-like object "out"<(T, F=)z at remote 0x%x>N)r/   r7   r   	iteritemsr   ry   )rz   r{   ro   pyop_attrdictr   firstpyop_argpyop_vals           r   _write_instance_reprr     s     IIcNIIdO -11		#"/"9"9";H		$EIIh''01IIcN- #< 			#II 7*+r
   c                        \ rS rSrS rS rSrg)InstanceProxyi  c                 (    Xl         X l        X0l        g r*   )cl_nameattrdictr   )rO   r   r   r   s       r   rP   InstanceProxy.__init__  s     r
   c           	      B   [        U R                  [        5      (       a`  SR                  U R                  R	                  5        VVs/ s H  u  pU< SU< 3PM     snn5      nSU R
                  X0R                  4-  $ SU R
                  U R                  4-  $ s  snnf )Nr   r   z<%s(%s) at remote 0x%x>r   )r7   r   dictr?   r   r   r   )rO   argr'   kwargss       r   r   InstanceProxy.__repr__  s    dmmT**YY040G0G0I K0IHC ,/ 40I K LF,060F F F )DLL,0LL,: : : Ks   B
)r   r   r   Nr   r   r   r    rP   r   r!   r	   r
   r   r   r     s    
:r
   r   c                     [         R                  c  [        R                  " S5      [         l        U R	                  S5      XR	                  S5      -  -   [        5       S-
  -   [        5       S-
  ) -  R                  [         R                  5      $ )Nsize_ttp_basicsizetp_itemsize   )_PyObject_VAR_SIZE_type_size_tr   r   rq   r   rb   )typeobjnitemss     r   r   r     s}    &&.*-//(*C'}}^,m445 1$& "#a'() T$112	3r
   c                   *    \ rS rSrSrS rS rS rSrg)r   i  r`   c                     U R                  5       n[        UR                  S5      5      nUS:w  Ga   US:  a~  [        R                  " S5      R                  5       n[        U R                  R                  U5      S   5      nUS:  a  U* n[        X5      nX%-  nUS:  d   eU[        5       -  S:X  d   eU R                  R                  [        5       5      U-   n[        R                  5       R                  5       nUR                  U5      n[        R                  UR                  5       5      $  g! [         a     gf = f)z_
Get the PyDictObject ptr representing the attribute dictionary
(or None if there's a problem)
tp_dictoffsetr   r   rj   N)r   int_from_intrq   r   r   r   rc   rb   r   r   r   r^   rl   rt   rm   r   )rO   r  
dictoffsettype_PyVarObject_ptrtsizesizedictptrPyObjectPtrPtrs           r   get_attr_dictHeapTypeObjectPtr.get_attr_dict  s+   
	iikG%gmmO&DEJQ>+.??=+I+Q+Q+S(():):;O)PQZ)[\Eqy!&-g=D&J%>)>%(88A===,,++N,<=
J!,!9!9!;!C!C!E!,,~6"44W5H5H5JKK (   	 	s   D0D5 5
EEc                 2   U R                  5       U;   a  [        S5      $ UR                  U R                  5       5        U R                  5       nU(       a  UR	                  U5      nO0 nU R                  5       n[        XC[        U R                  5      5      $ )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,   rc   )rO   r{   pyop_attr_dict	attr_dictr   s        r   r   HeapTypeObjectPtr.proxyval  s}     ??'&w//DOO%&++-&//8II##% WT\\1BCCr
   c                     U R                  5       U;   a  UR                  S5        g UR                  U R                  5       5        U R                  5       n[	        XU R                  5       X0R                  5       5        g )Nr  )r   r/   r  r  r   r   )rO   rz   r{   r   s       r   ry   HeapTypeObjectPtr.write_repr  sa    ??'IIgDOO%&**,S!..0-AR	Tr
   r	   N)	r   r   r   r    r   r  r   ry   r!   r	   r
   r   r   r     s    I<D,	Tr
   r   c                        \ rS rSrS rS rSrg)ProxyExceptioni#  c                     Xl         X l        g r*   r   args)rO   r   r  s      r   rP   ProxyException.__init__$  s    	r
   c                 :    U R                   < U R                  < 3$ r*   r  rX   s    r   r   ProxyException.__repr__(  s    tyy11r
   )r  r   Nr   r	   r
   r   r  r  #  s    2r
   r  c                   (    \ rS rSrSrSrS rS rSrg)r   i+  zq
Class wrapping a gdb.Value that's a PyBaseExceptionObject* i.e. an exception
within the process being debugged.
PyBaseExceptionObjectc                     U R                  5       U;   a  [        S5      $ UR                  U R                  5       5        U R                  S5      R	                  U5      n[        U R                  5       U5      $ N(...)r  )r   r   r  rv   r   r  r   )rO   r{   	arg_proxys      r   r   !PyBaseExceptionObjectPtr.proxyval2  sc    ??'&w//DOO%&OOF+44W=	d//1') 	)r
   c                     U R                  5       U;   a  UR                  S5        g UR                  U R                  5       5        UR                  U R                  5       5        U R	                  SX5        g r#  )r   r/   r  r   r}   r   s      r   ry   #PyBaseExceptionObjectPtr.write_repr;  sX    ??'IIgDOO%&		$##%&fc3r
   r	   N)	r   r   r   r    r\   r   r   ry   r!   r	   r
   r   r   r   +  s     (I)4r
   r   c                       \ rS rSrSrSrSrg)r   iE  zr
Class wrapping a gdb.Value that's a PyClassObject* i.e. a <classobj>
instance within the process being debugged.
PyClassObjectr	   N)r   r   r   r    r\   r   r!   r	   r
   r   r   r   E  s      Ir
   r   c                        \ rS rSrS rS rSrg)BuiltInFunctionProxyiM  c                     Xl         g r*   ml_name)rO   r/  s     r   rP   BuiltInFunctionProxy.__init__N  s    r
   c                      SU R                   -  $ )Nz<built-in function %s>r.  rX   s    r   r   BuiltInFunctionProxy.__repr__Q  s    '$,,66r
   r.  Nr   r	   r
   r   r,  r,  M  s    7r
   r,  c                        \ rS rSrS rS rSrg)BuiltInMethodProxyiT  c                     Xl         X l        g r*   r/  pyop_m_self)rO   r/  r7  s      r   rP   BuiltInMethodProxy.__init__U  s    &r
   c                     SU R                   U R                  R                  5       U R                  R                  5       4-  $ )Nz0<built-in method %s of %s object at remote 0x%x>)r/  r7  r   r   rX   s    r   r   BuiltInMethodProxy.__repr__Y  s?    B<<##002##..022 	r
   r6  Nr   r	   r
   r   r4  r4  T  s    'r
   r4  c                   "    \ rS rSrSrSrS rSrg)r   i`  zp
Class wrapping a gdb.Value that's a PyCFunctionObject*
(see Include/methodobject.h and Objects/methodobject.c)
PyCFunctionObjectc                     U R                  S5      n US   R                  5       nU R                  S5      nUR	                  5       (       a  [        U5      $ [        X45      $ ! [         a    Sn NJf = f)Nm_mlr/  z<ml_name:UnicodeDecodeError>m_self)rq   r   r   rv   rk   r,  r4  )rO   r{   r>  r/  r7  s        r   r   PyCFunctionObjectPtr.proxyvalg  sr    zz&!	59o,,.G ooh/  '00%g;; " 	54G	5s   A" "A10A1r	   Nr   r   r   r    r\   r   r   r!   r	   r
   r   r   r   `  s     $I<r
   r   c                   "    \ rS rSrSrSrS rSrg)PyCodeObjectPtriu  zm
Class wrapping a gdb.Value that's a PyCodeObject* i.e. a <code> instance
within the process being debugged.
PyCodeObjectc                    U R                  S5      R                  [        5       5      n[        U R	                  S5      5      nSn[        USSS2   USSS2   5       H*  u  pVU[        U5      -  nXA:  a  Us  $ U[        U5      -  nM,     U$ )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   )rv   r   r   r  rq   zipr<   )rO   addrqrF  linenoaddr	addr_incr	line_incrs          r   	addr2linePyCodeObjectPtr.addr2line|  s     OOK099#%@	 djj)9:;$'	#A#	!$Q$$H IC	N"D|c)n$F	 %I
 r
   r	   N)r   r   r   r    r\   r   rO  r!   r	   r
   r   rC  rC  u  s     Ir
   rC  c                   4    \ rS rSrSrSrS rS rS rS r	Sr
g	)
r   i  zk
Class wrapping a gdb.Value that's a PyDictObject* i.e. a dict instance
within the process being debugged.
PyDictObjectc              #     #    U R                  S5      nU R                  S5      nU R                  U5      u  p4[        U5       H{  nX5   n[        U5      (       a  [        R                  X%   5      nO[        R                  US   5      nUR                  5       (       a  M^  [        R                  US   5      nX4v   M}     g7f)z`
Yields a sequence of (PyObjectPtr key, PyObjectPtr value) pairs,
analogous to dict.iteritems()
ma_keys	ma_valuesme_valueme_keyN)rq   _get_entriesr-   r,   r^   rt   rk   )	rO   keysvaluesentriesnentriesiep
pyop_valuepyop_keys	            r   r   PyDictObjectPtr.iteritems  s     
 zz)$K( --d3H%AB6{{(::69E
(::2j>J
%%''&88HF,, &s   BC!"Cc                    U R                  5       U;   a  [        S5      $ UR                  U R                  5       5        0 nU R                  5        H+  u  p4UR	                  U5      nUR	                  U5      nXbU'   M-     U$ )N{...})r   r   r  r   r   )rO   r{   resultr`  r_  	proxy_keyproxy_values          r   r   PyDictObjectPtr.proxyval  sw    ??'&w//DOO%&$(NN$4 H ))'2I$--g6K +9 %5 r
   c                    U R                  5       U;   a  UR                  S5        g UR                  U R                  5       5        UR                  S5        SnU R                  5        HR  u  pEU(       d  UR                  S5        SnUR	                  X5        UR                  S5        UR	                  X5        MT     UR                  S5        g )Nrc  {Tr   F: })r   r/   r  r   ry   )rO   rz   r{   r   r`  r_  s         r   ry   PyDictObjectPtr.write_repr  s    ??'IIgDOO%&		#$(NN$4 H		$E-IIdO!!#/ %5 			#r
   c                 |   [        US   5      n[        US   5      n US   U4$ ! [         a     Of = fUS::  a  UnOUS::  a  SU-  nOUS::  a  SU-  nOS	U-  nUS
   R                  nUR                  [	        5       5      U-   n[
        R                  " S5      R                  5       nUR                  U5      nXR4$ )Ndk_nentriesdk_size
dk_entries   i  rH  l          
dk_indicesPyDictKeyEntry)r,   r   r   rb   r   r   r   r   )rO   rY  rn  ro  offsetent_addr	ent_ptr_ts          r   rX  PyDictObjectPtr._get_entries  s    $}-.d9o&	%w.. 		 d?F[F
"[F[F%--==!8!:;fDOO$45==?	==+$$s   % 
22r	   N)r   r   r   r    r\   r   r   r   ry   rX  r!   r	   r
   r   r   r     s"     I-$$%r
   r   c                   *    \ rS rSrSrS rS rS rSrg)r   i  PyListObjectc                 ,    U R                  S5      nX!   $ Nob_itemrq   rO   r]  field_ob_items      r   __getitem__PyListObjectPtr.__getitem__      

9-r
   c                 :   U R                  5       U;   a  [        S5      $ UR                  U R                  5       5        [        [	        U R                  S5      5      5       Vs/ s H)  n[        R                  X   5      R                  U5      PM+     nnU$ s  snf )N[...]rj   )	r   r   r  r-   r  rq   r^   rt   r   )rO   r{   r]  rd  s       r   r   PyListObjectPtr.proxyval  s    ??'&w//DOO%& &l4::i3H&IJLJa //8AA'JJ 	 LLs   $0Bc                    U R                  5       U;   a  UR                  S5        g UR                  U R                  5       5        UR                  S5        [        [	        U R                  S5      5      5       HB  nUS:  a  UR                  S5        [        R                  X   5      nUR                  X5        MD     UR                  S5        g )Nr  [rj   r   r   ]	r   r/   r  r-   r  rq   r^   rt   ry   rO   rz   r{   r]  elements        r   ry   PyListObjectPtr.write_repr  s    ??'IIgDOO%&		#LI)>?@A1u		$!33DG<Gs,	 A
 			#r
   r	   N	r   r   r   r    r   r  r   ry   r!   r	   r
   r   r   r     s    I 
r
   r   c                   $    \ rS rSrSrS rS rSrg)r   i  PyLongObjectc                 \   [        U R                  S5      5      nUS:X  a  gU R                  S5      n[        R                  " S5      R                  S:X  a  SnOSn[        [        U5      5       Vs/ s H  n[        X5   5      SXE-  -  -  PM     nn[        U5      nUS:  a  U* nU$ s  snf )a  
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
rj   r   ob_digitdigitrH        )r,   rq   r   r   r   r-   abssum)rO   r{   rj   r  SHIFTr]  digitsrd  s           r   r   PyLongObjectPtr.proxyval	  s    $ djj+,a<::j)??7#**a/EE &c'l353a hk"Q\13 	 5VQ;WF5s   2B)c                 N    U R                  U5      nUR                  SU-  5        g )Nz%sr   r/   rO   rz   r{   proxys       r   ry   PyLongObjectPtr.write_repr-  s     g&		$,r
   r	   N)r   r   r   r    r   r   ry   r!   r	   r
   r   r   r     s    I"H r
   r   c                       \ rS rSrSrS rSrg)r   i3  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                 :    [         R                  X5      (       a  gg)NTF)r   r   rO   r{   s     r   r   PyBoolObjectPtr.proxyval8  s    ##D22r
   r	   N)r   r   r   r    r\   r   r!   r	   r
   r   r   r   3  s    r
   r   c                   "    \ rS rSrSrSrS rSrg)r   i>  z{
Class wrapping a gdb.Value that's a PyObject* pointing to the
singleton (we hope) _Py_NoneStruct with ob_type PyNone_Type
r`   c                     g r*   r	   r  s     r   r   PyNoneStructPtr.proxyvalE  s    r
   r	   NrA  r	   r
   r   r   r   >  s     Ir
   r   c                   X    \ rS rSrSrSS jrS rS rS rS r	S	 r
S
 rS rS rS rSrg)r   iI  PyFrameObjectNc                 d   [         R                  XU5        U R                  5       (       Gd  [        R	                  U R                  S5      5      U l        U R                  R                  S5      U l        U R                  R                  S5      U l	        [        U R                  S5      5      U l        [        U R                  S5      5      U l        [        U R                  R                  S5      5      U l        [        R	                  U R                  R                  S5      5      U l        g g )Nf_codeco_nameco_filenamef_linenof_lasti
co_nlocalsco_varnames)r^   rP   r   rC  rt   rq   corv   r  r  r  r  r  r  r   r  rd   s      r   rP   PyFrameObjectPtr.__init__L  s    T73$$&&%77

88LMDG77--i8DL#ww11-@D(J)?@DM'

9(=>DL*477==+FGDO/AA$''--P]B^_D 'r
   c              #   <  #    U R                  5       (       a  gU R                  S5      n[        U R                  5       HX  n[        R                  X   5      nUR                  5       (       a  M1  [        R                  U R                  U   5      nXC4v   MZ     g7f)zh
Yield a sequence of (name,value) pairs of PyObjectPtr instances, for
the local variables of this frame
Nf_localsplus)r   rq   r-   r  r^   rt   rk   r  )rO   r  r]  r_  	pyop_names        r   iter_localsPyFrameObjectPtr.iter_localsY  s     
   ""zz.1DOO,A$66|GJ%%'''99$:J:J1:MN	 --	 -s   A*B0,Bc                 p    U R                  5       (       a  gU R                  S5      nUR                  5       $ )zi
Yield a sequence of (name,value) pairs of PyObjectPtr instances, for
the global variables of this frame
r	   	f_globalsr   rv   r   )rO   pyop_globalss     r   iter_globalsPyFrameObjectPtr.iter_globalsh  s3    
   ""{3%%''r
   c                 p    U R                  5       (       a  gU R                  S5      nUR                  5       $ )z\
Yield a sequence of (name,value) pairs of PyObjectPtr instances, for
the builtin variables
r	   
f_builtinsr  )rO   pyop_builtinss     r   iter_builtinsPyFrameObjectPtr.iter_builtinss  s3    
   ""5&&((r
   c                 f   U R                  5        H'  u  p#XR                  [        5       5      :X  d  M#  US4s  $    U R                  5        H'  u  p#XR                  [        5       5      :X  d  M#  US4s  $    U R	                  5        H'  u  p#XR                  [        5       5      :X  d  M#  US4s  $    g)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   r   r  r  )rO   ro   r  r_  s       r   get_var_by_name PyFrameObjectPtr.get_var_by_name~  s     &*%5%5%7!I))#%00!7** &8 &*%6%6%8!I))#%00!8++ &9 &*%7%7%9!I))#%00!9,, &: r
   c                 ~    U R                  5       (       a  [        $ U R                  R                  [	        5       5      $ )z;Get the path of the current Python source file, as a string)r   FRAME_INFO_OPTIMIZED_OUTr  r   r   rX   s    r   r@   PyFrameObjectPtr.filename  s0      ""++((//r
   c                     U R                  5       (       a  gU R                  S5      n[        U5      S:w  a  U R                  $  U R                  R                  U R                  5      $ ! [         a     gf = f)zGet current line number as an integer (1-based)

Translated from PyFrame_GetLineNumber and PyCode_Addr2Line

See Objects/lnotab_notes.txt
Nf_tracer   )r   rq   r,   r  r  rO  r  	Exception)rO   r  s     r   current_line_num!PyFrameObjectPtr.current_line_num  sl       ""**Y'w<1== 	77$$T\\22 	 	s   $A) )
A65A6c                 P   U R                  5       (       a  [        $ U R                  5       nUc  gU R                  5       n [	        [        U5      5       nUR                  5       nSSS5         WUS-
     $ ! , (       d  f       N= f! [         a     gf = f! [         a     gf = f)zVGet the text of the current source line as a string, with a trailing
newline characterNz!(failed to get frame line number)r   )	r   r  r  r@   openrD   	readlinesOSError
IndexError)rO   rK  r@   fpliness        r   current_linePyFrameObjectPtr.current_line  s       ""++&&(>6==?	k(+, -
	!$$ -, 		  		sB   B A7&B /B 7
BB B 
BB
B%$B%c           	      Z   U R                  5       (       a  UR                  [        5        g U R                  5       nUb  [	        U5      OSnUR                  SU R                  5       U R                  R                  U5      UU R                  R                  U5      4-  5        SnU R                  5        Ha  u  pVU(       d  UR                  S5        SnUR                  UR                  U5      5        UR                  S5        UR                  X5        Mc     UR                  S5        g )N?z)Frame 0x%x, for file %s, line %s, in %s (Tr   Fr   r   )r   r/   r  r  strr   r  r   r  r  ry   )rO   rz   r{   rK  r   r  r_  s          r   ry   PyFrameObjectPtr.write_repr  s      ""II./&&( & 2V		=__&%%..w7\\**7355 	6
 %)%5%5%7!I		$EIIi((12IIcN!!#/ &8 			#r
   c           
         U R                  5       (       a'  [        R                  R                  S[        -  5        g [        5       nU R                  5       nUb  [        U5      OSn[        R                  R                  SU R                  R                  U5      < SU< SU R                  R                  U5      < S35        g )N  %s
r  z  File "z", line z, in 
)r   r9   stdoutr/   r  r   r  r  r  r   r  )rO   r{   rK  s      r   print_traceback PyFrameObjectPtr.print_traceback  s      ""JJX(@@A%&&( & 2V

%%..w7\\**735 	6r
   )r  r  r  r  r  r  r  r*   )r   r   r   r    r   rP   r  r  r  r  r@   r  r  ry   r  r!   r	   r
   r   r   r   I  s:    I`.	(	)$00..
6r
   r   c                   :    \ rS rSrSr\S 5       rS rS rS r	Sr
g)	r   r$   PySetObjectc                 J    [         R                  " S5      R                  5       $ )N_PySet_Dummy)r   lookup_global_symbolvaluerX   s    r   
_dummy_keyPySetObjectPtr._dummy_key  s    ''7==??r
   c              #      #    U R                  5       nU R                  S5      n[        U R                  S5      S-   5       H2  nX#   nUS   nUS:w  d  M  XQ:w  d  M  [        R	                  U5      v   M4     g 7f)Ntablemaskr   keyr   )r  rq   r-   r^   rt   )rO   	dummy_ptrr  r]  setentryr  s         r   __iter__PySetObjectPtr.__iter__  si     OO%	

7#DJJv.23AxH5/CaxC,!33C88	 4s   AA8A8A8c                   ^ U R                  5       T;   a  [        SU R                  5       -  5      $ TR                  U R                  5       5        U4S jU  5       nU R                  5       S:X  a  [	        U5      $ [        U5      $ )Nz%s(...)c              3   D   >#    U  H  oR                  T5      v   M     g 7fr*   )r   ).0r  r{   s     r   	<genexpr>*PySetObjectPtr.proxyval.<locals>.<genexpr>  s     9DS<<((Ds    r   )r   r   r   r  r   r   )rO   r{   memberss    ` r   r   PySetObjectPtr.proxyval  sn    ??'&y43D3D3F'FGGDOO%&9D9+-W%%w<r
   c                 P   U R                  5       nU R                  5       U;   a  UR                  S5        g UR                  U R                  5       5        U R	                  S5      (       d#  UR                  U5        UR                  S5        g US:w  a"  UR                  U5        UR                  S5        UR                  S5        SnU  H.  nU(       d  UR                  S5        S	nUR                  X5        M0     UR                  S
5        US:w  a  UR                  S5        g g )Nr$  usedz()r   r   ri  Tr   Frk  r   )r   r   r/   r  rq   ry   )rO   rz   r{   r   r   r  s         r   ry   PySetObjectPtr.write_repr  s    ##% ??'IIgDOO%& zz&!!IIgIIdO eIIgIIcN		#C		$ENN3(	 
 			#eIIcN r
   r	   N)r   r   r   r    r   r   r  r  r   ry   r!   r	   r
   r   r   r     s*    I@ @9
 r
   r   c                   *    \ rS rSrSrS rS rS rSrg)r   i&  PyBytesObjectc           	         U R                  S5      nU R                  S5      nUR                  R                  [        5       5      nSR	                  [        U5       Vs/ s H  n[        X4   5      PM     sn5      $ s  snf )Nrj   ob_svalr6   )rq   r   rb   r   r?   r-   r=   )rO   field_ob_sizefield_ob_svalchar_ptrr]  s        r   __str__PyBytesObjectPtr.__str__)  sg    

9-

9- ((--.E.GHww*]2KL2KQHK(2KLMMLs   A<c                     [        U 5      $ r*   )r  r  s     r   r   PyBytesObjectPtr.proxyval/  s    4yr
   c                    U R                  U5      nSnSU;   a  SU;  a  SnUR                  S5        UR                  U5        U GH  nXT:X  d  US:X  a$  UR                  S5        UR                  U5        M3  US:X  a  UR                  S5        ML  US:X  a  UR                  S5        Me  US	:X  a  UR                  S
5        M~  US:  d  [        U5      S:  a^  UR                  S5        UR                  [        [        U5      S-  S-	     5        UR                  [        [        U5      S-     5        M  UR                  U5        GM     UR                  U5        g )N'"b\	\tr  \n\r    \x   rr  r  )r   r/   r<   	hexdigits)rO   rz   r{   r  quoterC   s         r   ry   PyBytesObjectPtr.write_repr2  s    g& %<uE		#		%D}		$		$		% 		% 		% s4yD0		% 		)SY%5!$;<=		)CIO45		$   			%r
   r	   N)	r   r   r   r    r   r  r   ry   r!   r	   r
   r   r   r   &  s    INr
   r   c                   *    \ rS rSrSrS rS rS rSrg)r   iQ  PyTupleObjectc                 ,    U R                  S5      nX!   $ r}  r  r  s      r   r  PyTupleObjectPtr.__getitem__T  r  r
   c           
         ^ ^ T R                  5       T;   a  [        S5      $ TR                  T R                  5       5        [        U U4S j[	        [        T R                  S5      5      5       5       5      nU$ )Nr$  c              3   r   >#    U  H,  n[         R                  TU   5      R                  T5      v   M.     g 7fr*   )r^   rt   r   )r  r]  rO   r{   s     r   r  ,PyTupleObjectPtr.proxyval.<locals>.<genexpr>_  s6      Q O1 #44T!W=FFwOO Os   47rj   )r   r   r  tupler-   r  rq   )rO   r{   rd  s   `` r   r   PyTupleObjectPtr.proxyvalY  sb    ??'&w//DOO%& Q *<

98M+N OQ Qr
   c                    U R                  5       U;   a  UR                  S5        g UR                  U R                  5       5        UR                  S5        [        [	        U R                  S5      5      5       HB  nUS:  a  UR                  S5        [        R                  X   5      nUR                  X5        MD     U R                  S5      S:X  a  UR                  S5        g UR                  S5        g )	Nr$  r   rj   r   r   r   z,)r   r  r  s        r   ry   PyTupleObjectPtr.write_reprc  s    ??'IIgDOO%&		#LI)>?@A1u		$!33DG<Gs,	 A
 ::i A%IIdOIIcNr
   r	   Nr  r	   r
   r   r   r   Q  s    I 
r
   r   c                       \ rS rSrSrSrg)r   iu  PyTypeObjectr	   Nr   r	   r
   r   r   r   u  s    Ir
   r   c                 @    U S:X  a  gSS K nUR                  U 5      S;  $ )Nr  Tr   )CZ)unicodedatacategory)r   r,  s     r   _unichr_is_printabler.  y  s%    s{%Z77r
   c                   *    \ rS rSrSrS rS rS rSrg)r   i  PyUnicodeObjectc                 F    [         R                  " S5      nUR                  $ )N
Py_UNICODE)r   r   r   )rO   _type_Py_UNICODEs     r   
char_widthPyUnicodeObjectPtr.char_width  s    ??<8&&&r
   c                    [         cC  [        R                  " S5      R                  5       nSU Vs/ s H  o3R                  PM     sn;   q [         (       Ga4  SnU R                  S5      nUS   nUS   n[        US   5      =(       a    [        US   5      n[        US   5      (       d  [        US	   5      n	S
nUS   n
GO[        US   5      n	U(       a  UR                  S-   n
O7[        US   5      (       a  UR                  S-   n
OU R                  S5      S   n
[        US   5      nUS:X  a  U
R                  [        5       5      n
O~US:X  a  U
R                  [        5       5      n
O^US:X  a  U
R                  [        5       5      n
O>[        U R                  S5      5      n	U R                  S5      n
U R                  5       S:H  nU(       d'  [        U	5       Vs/ s H  n[        X   5      PM     nnO/ nSn[        U	5      nX:  a  [        X   5      nUS-  nUS:  d  US:  d  X:X  a  UR                  U5        M;  [        X   5      nUS:  d  US:  a  MV  US-  S-  nUUS-  -  nUS-  nUR                  U5        US-  nX:  a  M  SR!                  U Vs/ s H  nUS::  a  [#        U5      OSPM     sn5      nU$ s  snf s  snf s  snf )Nr0  rU   F_basestateasciicompactreadywstr_lengthTwstrlengthr   anykindrH  rr  r  r      r5       
      r6   i u   �)
_is_pep393r   r   fieldsro   rq   r,   r   rb   r   r   r   r4  r-   r(   r>   r?   r=   )rO   r{   rG  fmay_have_surrogatesr:  r9  r8  is_compact_asciifield_length	field_str	repr_kindr]  Py_UNICODEslimitucsucs2coderd  s                      r   r   PyUnicodeObjectPtr.proxyval  s   __%67>>@FF#;FqFFF#;;J:"'jj)GG$E'NE #E'N 3 ME)<L8MuW~&&"7=#9:&*#!&M	"5?3# % 1Iy)** '! 3I $

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

5)I"&//"3q"8 #6@6NO6N3y|,6NKOK KA .E)),'Q<3&=A4E&&s+9<(&=D6Mf+v%
"""4(Q )& "$" SXh6"$ % E $<J P4$s   KKKc                    U R                  U5      nSU;   a	  SU;  a  SnOSnUR                  U5        SnU[        U5      :  Ga  X5   nUS-  nXd:X  d  US:X  a$  UR                  S5        UR                  U5        GOUS:X  a  UR                  S5        GOUS:X  a  UR                  S	5        GOvUS
:X  a  UR                  S5        GO]US:  d  US:X  a^  UR                  S5        UR                  [        [	        U5      S-	  S-     5        UR                  [        [	        U5      S-     5        GO[	        U5      S:  a  UR                  U5        GOUnS n[
        R                  S:  aP  U[        U5      :  aA  S[	        U5      s=::  a  S:  a+  O  O(S[	        X5   5      s=::  a  S::  a  O  OX5   nXh-   nUS-  n[        U5      n	U	(       a   UR                  [        5        U	(       Gd  Ub(  [	        U5      S-  S-  n
U
[	        U5      S-  -  n
U
S-  n
O[	        U5      n
U
S::  aL  UR                  S5        UR                  [        U
S-	  S-     5        UR                  [        U
S-     5        GOU
S:  a  UR                  S5        UR                  [        U
S-	  S-     5        UR                  [        U
S-	  S-     5        UR                  [        U
S-	  S-     5        UR                  [        U
S-	  S-     5        UR                  [        U
S-	  S-     5        UR                  [        U
S-	  S-     5        UR                  [        U
S-	  S-     5        UR                  [        U
S-     5        OUR                  S 5        UR                  [        U
S-	  S-     5        UR                  [        U
S-	  S-     5        UR                  [        U
S-	  S-     5        UR                  [        U
S-     5        O%UR                  U5        Ub  UR                  U5        U[        U5      :  a  GM  UR                  U5        g ! [         a    Sn	 GNtf = f)!Nr  r  r   r   r  r  r  r  r  r  r  r  r  r  rr  r  rE  rA  r5   rB  FrC  rD  rq  z\U               rs  z\u)r   r/   rT   r  r<   r9   
maxunicoder.  r;   ENCODINGUnicodeEncodeError)rO   rz   r{   r  r  r]  chrP  ch2	printablerR  s              r   ry   PyUnicodeObjectPtr.write_repr  s    g& %<Cu,EE		%#e*nBFA {bDj		$		" t		% t		% t		%  cR4Z		% 		)SW\V$;<=		)CGf$456 R4		" >>G+ CJ &#b' :F : &#eh- A6 A#h hQ 15	*

8, ! !$B& 0R7C6 11
*"3x t|		%(		)TQY&,@"AB		)D6M":;		%(		)TRZ:,E"FG		)TRZ:,E"FG		)TRZ:,E"FG		)TRZ:,E"FG		)TRZ:,E"FG		)TQY*,D"EF		)TQY*,D"EF		)D:,=">? 		%(		)TRZ6,A"BC		)TQY&,@"AB		)TQY&,@"AB		)D6M":; IIbM		#s #e*nv 			%Y . *$)	*s   Q
 
QQr	   N)	r   r   r   r    r   r4  r   ry   r!   r	   r
   r   r   r     s    !I'FPjr
   r   c                   6    \ rS rSrS rS rS rS rS rS r	Sr
g)	r   i=  c                      U R                  S5      S   S   R                  5       n[        U5      $ ! [        [        [
        4 a     gf = f)Ndescrd_basero   z<unknown name>)rq   r   r   r   r   r   ru   s     r   	safe_namewrapperobject.safe_name@  sL    	$::g&x08??AD:/AB 	$#	$s   /2 A
	A
c                      U R                  S5      S   S   R                  5       $ ! [        [        [        4 a     gf = f)NrO   ri   r   z<unknown tp_name>)rq   r   r   r   r   rX   s    r   r   wrapperobject.safe_tp_nameG  sC    	'::f%i0;BBDD/AB 	'&	's   $' ??c                 n     [        U R                  S5      5      nSU-  $ ! [        [        4 a     gf = f)NrO   z%#xz<failed to get self address>)r,   rq   r   r   )rO   r   s     r   safe_self_addresss wrapperobject.safe_self_addresssM  s;    	2$**V,-G7?". 	21	2s   ! 44c                     U R                  5       nU R                  5       nU R                  5       nSU< SU< SU< S3$ )Nz<method-wrapper z of z object at >)re  r   rj  )rO   r{   ro   r   self_addresss        r   r   wrapperobject.proxyvalT  s;    ~~##%..0,0 	1r
   c                 H    U R                  U5      nUR                  U5        g r*   r  r  s       r   ry   wrapperobject.write_repr[  s    g&		%r
   r	   N)r   r   r   r    r   re  r   rj  r   ry   r!   r	   r
   r   r   r   =  s     I$'21r
   r   c                     [        U 5      $ r*   )r,   re   s    r   r  r  `  s    v;r
   c                      [        U 5      $ r*   )r   pprintpformat)r'   rv  s     r   	stringifyrw  d  s     Cyr
   c                   $    \ rS rSrSrS rS rSrg)PyObjectPtrPrinterin  zPrints a (PyObject*)c                     Xl         g r*   rs  )rO   re   s     r   rP   PyObjectPtrPrinter.__init__q  s    r
   c                 l    [         R                  U R                  5      n UR                  [        5      $ r*   )r^   rt   re   r   MAX_OUTPUT_LENr   r   rw  )rO   pyopr   s      r   	to_stringPyObjectPtrPrinter.to_stringt  s*    ,,T[[9**>::r
   rs  N)r   r   r   r    r\   rP   r  r!   r	   r
   r   ry  ry  n  s    'r
   ry  c                     U R                   R                  5       nUR                  [        R                  :w  a  g UR                  5       R                  5       n[        U5      nUS;   a  [        U 5      $ g )N)r`   r  r0  r   )r   unqualifiedrR  r   TYPE_CODE_PTRtargetr  ry  )re   r   r   s      r   pretty_printer_lookupr  ~  s`    ;;""$DyyC%%%;;=$$&DD	AMM!&)) Nr
   c                 T    U c  [         n U R                  R                  [        5        g r*   )r   pretty_printersr>   r  )objs    r   registerr    s#    
{ 45r
   c                       \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rS rS rS r\S 5       r\S 5       r\S 5       rS rS rSrg)Framei  z/
Wrapper for gdb.Frame, adding various methods
c                     Xl         g r*   	_gdbframe)rO   gdbframes     r   rP   Frame.__init__  s    !r
   c                 \    U R                   R                  5       nU(       a  [        U5      $ g r*   )r  olderr  )rO   r  s     r   r  Frame.older  $    $$&<r
   c                 \    U R                   R                  5       nU(       a  [        U5      $ g r*   )r  newerr  )rO   r  s     r   r  Frame.newer  r  r
   c                     [        U R                  S5      (       d  [        S5        gU R                  R                  5         g)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  printr  rX   s    r   r  Frame.select  s:    
 t~~x00 Q Rr
   c                     SnU nUR                  5       (       a,  US-  nUR                  5       nUR                  5       (       a  M,  U$ )zOCalculate index of frame, starting at 0 for the newest frame within
this threadr   r   )r  )rO   index
iter_frames      r   	get_indexFrame.get_index  sL     
  QJE#))+J    r
   c                 \    U R                  5       (       a  gU R                  5       (       a  gg)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_framerX   s    r   is_python_frameFrame.is_python_frame  s+     %%''r
   c                     U R                   R                  5       [        :X  a.   U R                   R                  5       [        R
                  :X  a  gg)z)Is this a _PyEval_EvalFrameDefault frame?TF)r  ro   	EVALFRAMEr   r   NORMAL_FRAMErX   s    r   r  Frame.is_evalframe  s?    >> I- ~~""$(8(88r
   c                    U R                  5       (       a  gU R                  5       (       a  gU R                  nUR                  5       nU(       d  gUR	                  S5      (       d  US:X  a  Sn UR                  U5      n[        U5      $ US	:X  a  S
n UR                  U5      n[        U5      $ g! [         a    SU-  s $ [         a    SU-  s $ f = f! [         a    SU-  s $ [         a    SU-  s $ f = f)zIs 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  ro   
startswithread_varr  
ValueErrorr   )rO   r   callerarg_namer  s        r   r  Frame.is_other_python_frame  s#    ""$$(' 566&&HO~~h/4y  ^#HR~~h/4y  %  ;/19: ; OChNNO  <02:; < RFQQRs0   0B2 C 2CCCC9(C98C9c                 P    U R                   R                  5       nU(       a  US:H  $ g)z!Is this frame waiting on the GIL?take_gilNr  ro   ru   s     r   r  Frame.is_waiting_for_gil,  s)     ~~""$J&' r
   c                 <    U R                   R                  5       S:H  $ )z5Is this frame "collect" within the garbage-collector?collectr  rX   s    r   r  Frame.is_gc_collect3  s    ~~""$	11r
   c                 v    U R                   R                  S5      n[        R                  U5      nUR	                  5       (       d  U$ UnU R                   R                  5       nU(       a=  UR                  S5      n[        R                  U5      nUR	                  5       (       d  U$ U$ ! [         a     g f = f)NrH  )r  r  r   rt   r   r  r  )rO   rH  r   
orig_framer  s        r   get_pyopFrame.get_pyop7  s    	'',A$66q9E))++ J^^))+FOOC((::1=--// L 		s   AB+ 	AB+ )B+ +
B87B8c                 R    [         R                  " 5       nU(       a  [        U5      $ g r*   )r   selected_framer  )r   r  s     r   get_selected_frameFrame.get_selected_frameK  s!    &&(	##r
   c                      U R                  5       nU(       a0  UR                  5       (       a  U$ UR	                  5       nU(       a  M0  g! [        R                   a     gf = f)zRTry to obtain the Frame for the python-related code in the selected
frame, or NoneN)r  r   errorr  r  r   r   s     r   get_selected_python_frameFrame.get_selected_python_frameR  s]    	**,E
 $$&&KKME e  yy 		s   A
 
A! A!c                     U R                  5       nU(       a0  UR                  5       (       a  U$ UR                  5       nU(       a  M0  g)z^Try to obtain the Frame for the python bytecode interpreter in the
selected GDB frame, or NoneN)r  r  r  r  s     r   get_selected_bytecode_frame!Frame.get_selected_bytecode_framed  s@     &&(!!##KKME e r
   c                    U R                  5       (       a  U R                  5       nU(       a  UR                  [        5      n[	        [
        R                  SU R                  5       U4-  5        UR                  5       (       dE  UR                  5       nUb1  [
        R                  R                  SUR                  5       -  5        g g g [
        R                  R                  SU R                  5       -  5        g U R                  5       nU(       a3  [
        R                  R                  SU R                  5       U4-  5        g [
        R                  R                  SU R                  5       -  5        g )Nz#%i %s
    %s
z.#%i (unable to read python frame information)
z#%i
)r  r  r   r}  r2   r9   r  r  r   r  r/   stripr  rO   r~  lineinfos       r   print_summaryFrame.print_summaryr  s   ==?D..~>cjj*8H$7O*OP,,..,,.D'

((djjl)BC ( /
 

  !RUYUcUcUe!ef--/D

  t~~/?.F!FG

  4>>+;!;<r
   c                 $   U R                  5       (       a  U R                  5       nU(       ak  UR                  5         UR                  5       (       dE  UR	                  5       nUb1  [
        R                  R                  SUR                  5       -  5        g g g [
        R                  R                  S5        g U R                  5       nU(       a#  [
        R                  R                  SU-  5        g [
        R                  R                  S5        g )Nr  z,  (unable to read python frame information)
r  z  (not a python frame)
)
r  r  r  r   r  r9   r  r/   r  r  r  s       r   r  Frame.print_traceback  s    ==?D$$&,,..,,.D'

((djjl)BC ( /
 

  !PQ--/D

  D1

  !;<r
   r  N)r   r   r   r    r\   rP   r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r  r!   r	   r
   r   r  r    s    "
	$ 3j(2(    "  =&=r
   r  c                   $    \ rS rSrSrS rS rSrg)PyListi  zList 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                  R                  U S[         R                  [         R                  5        g )Nzpy-list)r   CommandrP   COMMAND_FILESCOMPLETE_NONErX   s    r   rP   PyList.__init__  +    d'!//!//	1r
   c                    SS K nS nS nUR                  " SU5      nU(       a  [        UR                  S5      5      nUS-   nUR                  " SU5      nU(       a   [	        [        UR                  5       5      u  pE[        R                  5       nU(       d  [        S5        g UR                  5       nU(       a  UR                  5       (       a  [        [        5        g UR                  5       n	UR                  5       n
U
c  [        S5        g Uc
  U
S-
  nU
S-   nUS:  a  Sn [        [        U	5      5      nU   UR)                  5       n[+        XS-
  U 5       HF  u  p[-        X-   5      nX-   U
:X  a  SU-   n["        R$                  R'                  U<S SU< 35        MH     S S S 5        g ! [          a2  n["        R$                  R'                  S	U	< S
U< S35         S nAg S nAff = f! , (       d  f       g = f)Nr   z\s*(\d+)\s*rD  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 rj  r  rm  z>4z    )rematchr,   groupmapgroupsr  r  r  r  r   UNABLE_READ_INFO_PYTHON_FRAMEr@   r  r  rD   r  r9   r  r/   r  	enumerater  )rO   r  from_ttyr  startendmr   r~  r@   rK  rH  err	all_linesr]  r  linestrs                    r   invokePyList.invoke  s   HH^T*
OE"*CHH/6S!((*-JE 113NO~~t,,../0==?&&(>;<=QJE1*C7E	[*+A
 I %YQws%;<ag,9&!GmG

  !>? = Q	  	JJ (#/ 0	 Qs%   $F. :A+G-.
G*8(G%%G*-
G;r	   Nr   r   r   r    r\   rP   r  r!   r	   r
   r   r  r    s    	17@r
   r  c                    [         R                  5       nU(       d  [        S5        gU(       av  U (       a  UR                  5       nOUR	                  5       nU(       d  OFUR                  5       (       a&  UR                  5       (       a  UR                  5         gUnU(       a  Mv  U (       a  [        S5        g[        S5        g)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  r  r  r  r  r  r  r  )move_upr   r  s      r   move_in_stackr    s    ++-E-.
JJ%%''  ""((* %" 4534r
   c                   $    \ rS rSrSrS rS rSrg)PyUpi  zESelect and print the python stack frame that called this one (if any)c                     [         R                  R                  U S[         R                  [         R                  5        g )Nzpy-upr   r  rP   COMMAND_STACKr  rX   s    r   rP   PyUp.__init__  +    d%!//!//	1r
   c                     [        SS9  g )NTr  r  rO   r  r  s      r   r  PyUp.invoke  s
    d#r
   r	   Nr  r	   r
   r   r  r    s    K1$r
   r  c                   $    \ rS rSrSrS rS rSrg)PyDowni  zCSelect and print the python stack frame called by this one (if any)c                     [         R                  R                  U S[         R                  [         R                  5        g )Nzpy-downr  rX   s    r   rP   PyDown.__init__  r  r
   c                     [        SS9  g )NFr  r  r  s      r   r  PyDown.invoke  s
    e$r
   r	   Nr  r	   r
   r   r  r    s    I1%r
   r  r  c                   $    \ rS rSrSrS rS rSrg)PyBacktraceFulli!  RDisplay the current python frame and all the frames within its call stack (if any)c                     [         R                  R                  U S[         R                  [         R                  5        g )Nz
py-bt-fullr  rX   s    r   rP   PyBacktraceFull.__init__#  s+    d*!//!//	1r
   c                     [         R                  5       nU(       d  [        S5        g U(       a?  UR                  5       (       a  UR	                  5         UR                  5       nU(       a  M>  g g )Nr  )r  r  r  r  r  r  rO   r  r  r   s       r   r  PyBacktraceFull.invoke*  sO    //112$$&&##%KKME er
   r	   Nr  r	   r
   r   r  r  !  s    X1	"r
   r  c                   $    \ rS rSrSrS rS rSrg)PyBacktracei7  r  c                     [         R                  R                  U S[         R                  [         R                  5        g )Nzpy-btr  rX   s    r   rP   PyBacktrace.__init__9  r  r
   c                    [         R                  5       nU(       d  [        S5        g [        R                  R                  S5        U(       a?  UR                  5       (       a  UR                  5         UR                  5       nU(       a  M>  g g )Nr  z$Traceback (most recent call first):
)	r  r  r  r9   r  r/   r  r  r  r  s       r   r  PyBacktrace.invoke@  sb    //112

@A$$&&%%'KKME er
   r	   Nr  r	   r
   r   r  r  7  s    X1
"r
   r  c                   $    \ rS rSrSrS rS rSrg)PyPrintiN  4Look up the given python variable name, and print itc                     [         R                  R                  U S[         R                  [         R                  5        g )Nzpy-printr   r  rP   COMMAND_DATAr  rX   s    r   rP   PyPrint.__init__P  s+    d(!..!//	1r
   c           	      Z   [        U5      n[        R                  5       nU(       d  [        S5        g UR	                  5       nU(       d  [        [
        5        g UR                  U5      u  pgU(       a*  [        U< SU< SUR                  [        5      < 35        g [        SU-  5        g )Nr  r   = z%r not found)	r  r  r  r  r  r  r  r   r}  )rO   r  r  ro   r   
pyop_framepyop_varscopes           r   r  PyPrint.invokeW  s    4y//112^^%
/0$44T:11.AC D
 .4'(r
   r	   Nr  r	   r
   r   r  r  N  s    :1)r
   r  c                   $    \ rS rSrSrS rS rSrg)PyLocalsip  r  c                     [         R                  R                  U S[         R                  [         R                  5        g )Nz	py-localsr  rX   s    r   rP   PyLocals.__init__r  s+    d)!..!//	1r
   c                 `   [        U5      n[        R                  5       nU(       d  [        S5        g UR	                  5       nU(       d  [        [
        5        g UR                  5        HA  u  pg[        UR                  [        5       5      < SUR                  [        5      < 35        MC     g Nr  r!  r  r  r  r  r  r  r  r   r   r   r}  rO   r  r  ro   r   r"  r  r_  s           r   r  PyLocals.invokey  s    4y//112^^%
/0%/%;%;%=!I""35)--n=  &>r
   r	   Nr  r	   r
   r   r'  r'  p  s    :1r
   r'  c                 F   ^  [         R                  " T 5      U 4S j5       nU$ )z*sigh*, readlinec                  ^   >  T" U 0 UD6$ ! [          a    [        R                  " 5         e f = fr*   )r  	traceback	print_exc)r  r   functions     r   wrapper%dont_suppress_errors.<locals>.wrapper  s6    	T,V,, 	!	s    !,)	functoolswraps)r3  r4  s   ` r   dont_suppress_errorsr8    s%    __X  Nr
   c                   4    \ rS rSrSrS r\S 5       rS rSr	g)	PyGlobalsi  z9List all the globals in the currently select Python framec                     [         R                  R                  U S[         R                  [         R                  5        g )Nz
py-globalsr  rX   s    r   rP   PyGlobals.__init__  s+    d*!..!//	1r
   c                 `   [        U5      n[        R                  5       nU(       d  [        S5        g UR	                  5       nU(       d  [        [
        5        g UR                  5        HA  u  pg[        UR                  [        5       5      < SUR                  [        5      < 35        MC     g r+  r,  r-  s           r   r  PyGlobals.invoke  s    4y//112^^%
/0%/%;%;%=!I((/ 33NCE F &>r
   c                 "    UR                  5       $ r*   )r  )rO   r"  s     r   get_namespacePyGlobals.get_namespace  s    &&((r
   r	   N)
r   r   r   r    r\   rP   r8  r  r@  r!   r	   r
   r   r:  r:    s%    ?1 F F$)r
   r:  c                     U R                   R                  5       S:X  a.   U R                   R                  5       [        R                  :X  a  gg)z#Is this a PyEval_EvalFrameEx frame?PyEval_EvalFrameExTF)r  ro   r   r   r  )r   s    r   is_evalframeexrD    s@    !55	 ??!S%5%55r
   c                   *    \ rS rSrS r\S 5       rSrg)PyNameEqualsi  c                     [        [        R                  " 5       5      n[        U5      (       aL  UR	                  5       nUc  [
        R                  " S5        g [        X15      R                  [        5       5      $ g )NzBUse a Python debug build, Python breakpoints won't work otherwise.)
r  r   r  rD  r  warningswarngetattrr   r   )rO   attrr   pyframes       r   _get_pycurframe_attr!PyNameEquals._get_pycurframe_attr  s`    c((*+%  nn&G 6 77)223599r
   c                 ^    U R                  S5      nUS L=(       a    X!R                  5       :H  $ )Nr  )rM  r   )rO   funcnamerK  s      r   r  PyNameEquals.invoke  s,    ((34=DOO,=$==r
   r	   N)r   r   r   r    rM  r8  r  r!   r	   r
   r   rF  rF    s     > >r
   rF  pyname_equalsc                   $    \ rS rSr\S 5       rSrg)PyModEqualsi  c                     U R                  S5      nUbP  [        R                  R                  [        R                  R	                  U5      5      u  p4X1R                  5       :H  $ g)Nr  F)rM  ospathsplitextbasenamer   )rO   modnamerK  r@   exts        r   r  PyModEquals.invoke  sP    ((7GG,,RWW-=-=d-CDMH~~///r
   r	   N)r   r   r   r    r8  r  r!   r	   r
   r   rT  rT    s     r
   rT  pymod_equalsc                   (    \ rS rSrSr\S 5       rSrg)PyBreaki  z
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                     SU;   a   UR                  S5      u  p4nSU< SU< S3nOSU-  n[        R                  " SU-   5        g )N.z$pyname_equals("z") && $pymod_equals("z")z$pyname_equals("%s")zbreak PyEval_EvalFrameEx if )
rpartitionr   execute)rO   rP  r  rZ  dotconds         r   r  PyBreak.invoke  sJ    (?%-%8%8%="G(DLDKMD *H4D2T9:r
   r	   N)r   r   r   r    r\   r8  r  r!   r	   r
   r   r_  r_    s    
 ; ;r
   r_  zpy-breakc                   0    \ rS rSrSrS rS rS rS rSr	g)	_LoggingStatei  zA
State that helps to provide a reentrant gdb.execute() function.
c                     [         R                  " S5      nXl        UR                  U l        UR                  5       U l        [        SU R                  -  5        / U l        g )Nzr+zset logging file %s)	tempfileNamedTemporaryFiler0   ro   r@   filenofd_executefile_position_stack)rO   rH  s     r   rP   _LoggingState.__init__  sI    ''-	((*&67#% r
   c                     U R                   (       d!  [        S5        [        S5        [        S5        U R                   R                  [        R                  " U R
                  5      R                  5        U $ )Nzset logging redirect onzset logging onzset pagination off)ro  rn  r>   rV  fstatrm  st_sizerX   s    r   	__enter___LoggingState.__enter__'  sR    ''./%&)*  ''(9(A(ABr
   c                     [         R                  " 5         U R                  R                  U R                  S   5        U R                  R                  5       nU$ )N)r   flushr0   seekro  read)rO   rd  s     r   	getoutput_LoggingState.getoutput0  s;    				t//34!r
   c                    U R                   R                  5       nU R                  R                  U5        U R                  R	                  5         U R                   (       d"  [        S5        [        S5        [        S5        g g )Nzset logging offzset logging redirect offzset pagination on)ro  popr0   ry  truncatern  )rO   exc_typeexc_valtbstartposs        r   __exit___LoggingState.__exit__6  s_    ++//1		x 		''&'/0() (r
   )rm  r0   ro  r@   N)
r   r   r   r    r\   rP   rt  r{  r  r!   r	   r
   r   rh  rh    s    &*r
   rh  c                     U(       a+  [          n[        X5        UR                  5       sSSS5        $ [        X5        g! , (       d  f       g= f)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_statern  r{  )commandr  r  r8  s       r   rc  rc  @  s7     uW'??$ ^ 	#	 ^s	   ?
Ac                  2    [         R                  " 5       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                     [         R                  " 5       u  p#[        R                  " US5      nUR	                  U 5        UR                  5         [        R                  " SU-  US9  [        R                  " U5        g)z
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r  N)	rj  mkstemprV  fdopenr/   closer   rc  remove)script_contentsr  rm  r@   rH  s        r   source_gdb_scriptr  c  sX     ##%LB
		"cAGGOGGIKKh&)<IIhr
   c                      [        [        R                  " S5      [        R                  [
        R                  4-  5        g )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PyStepr\   PyNextr	   r
   r   register_definesr  r  s1    hoo ' 	  NNFNN+!, -r
   c                 T    SnU (       a  U R                  5       n US-  nU (       a  M  U$ )z$Tells the stackdepth of a gdb frame.r   r   )r  )r   depths     r   
stackdepthr    s,    E

 % Lr
   c                   r   ^  \ rS rSrSrU 4S jrS rS rS rS r	S r
S	 rS
 rS rSS jrS rS rSrU =r$ )ExecutionControlCommandBasei  z
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                 l   > [         TU ]  U[        R                  [        R                  5        X l        g r*   )superrP   r   COMMAND_RUNNINGr  	lang_info)rO   ro   r  	__class__s      r   rP   $ExecutionControlCommandBase.__init__  s)     $c&9&93;L;L	N"r
   c              #   ,  #    [         R                  " U R                  R                  5       U R                  R	                  5       5      nU HC  n[
        R                  " SU-  SS9n[        R                  " SU5      R                  S5      v   ME     g 7f)Nzbreak %sTr  Breakpoint (\d+)r   )
	itertoolschainr  static_break_functionsruntime_break_functionsr   rc  r  searchr  )rO   all_locationslocationrd  s       r   install_breakpoints/ExecutionControlCommandBase.install_breakpoints  sq     !NN113NN2246 &H[[h!6$GF))/8>>qAA &s   BBc                 H    U H  n[         R                  " SU-  5        M     g )N	delete %s)r   rc  )rO   breakpoint_listbps      r   delete_breakpoints.ExecutionControlCommandBase.delete_breakpoints  s    !BKKb() "r
   c                    ^ [         R                  nSU[         R                  -  4SSU4/nSU4SU4/nU4S jn[         R                  " ST[         R                  5      nU(       a  SUR	                  S	5      -  nOS
nU" U5      Xu" U5      -   4$ )Nz^Program received signal .*)z.*[Ww]arning.*r   z^Program exited .*z^(Old|New) value = .*z^\d+: \w+ = .*c                    > / nU  HD  u  p#[         R                  " UTU5       H#  nUR                  UR                  S5      5        M%     MF     SR	                  U5      $ )Nr   r  )r  finditerr>   r  r?   )regexesoutputregexflagsr  rd  s        r   filter_output@ExecutionControlCommandBase.filter_output.<locals>.filter_output  sP    F '[[>EMM%++a.1 ? !( 99V$$r
   z^Value returned is \$\d+ = (.*)zValue returned: %s
r   r6   )r  	MULTILINEDOTALLr  r  )rO   rd  reflagsoutput_on_haltoutput_alwaysr  match_finishfinish_outputs    `      r   r  )ExecutionControlCommandBase.filter_output  s    ,, ,WRYY->?""G,
 &w/(	
	% yy!CV!#/2\5G5G5JJMMn-m <<> 	>r
   c                 0    [        5       R                  S:H  $ rS   )r  pidrX   s    r   stopped#ExecutionControlCommandBase.stopped  s    $&**a//r
   c                    U R                  U5      u  p#U R                  5       (       a  [        U5        [        U5        g[        R                  " 5       nU R
                  R                  U5      nU R
                  R                  U5      (       a-  U R
                  R                  U5      nU(       a  [        U5        U(       a:  UR                  5       (       a  [        UR                  5       5        [        U5        g[        U5        g)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)
r  r  r  r   r  r  get_source_lineis_relevant_functionexc_inforstrip)rO   rd  r  r  r   source_lineraised_exceptions          r   finish_executing,ExecutionControlCommandBase.finish_executing  s     )-(:(:6(B%<<>>- .!&&(E..88?K~~22599#'>>#:#:5#A #*+ ''))-..01k"fr
   c                     [         R                  " 5       R                  5       b  [         R                  " SSS9$ [         R                  " SSS9$ )zL
Execute until the function returns (or until something else makes it
stop)
finishTr  cont)r   r  r  rc  rX   s    r   _finish#ExecutionControlCommandBase._finish  s@    
 %%'3;;x488 ;;v66r
   c                 &    U R                  5       n [        R                  " 5       n[        R
                  " SU5      nU R                  R                  U5      nU(       d  U(       d  U R                  5       (       a   U$ M  ! [         a     U$ f = f)z:
Execute until the function returns to a relevant caller.
r  )	r  r   r  r   r  r  r  r  r  )rO   rd  r   hitbpis_relevants        r   _finish_frame)ExecutionControlCommandBase._finish_frame  s     \\^F**, II16:E..==eDKt||~~ 
    s   B 
BBc                 F    U R                  5       nU R                  U5        g)zImplements the finish command.N)r  r  )rO   r  rd  s      r   r  "ExecutionControlCommandBase.finish	  s    ##%f%r
   c                    U(       a  [        U R                  5       5      n[        R                  " 5       nU R                  R                  U5      (       a-  U R                  R                  U5      nU(       d  [        U5      nUn U R                  R                  U5      (       a  [        R                  " USS9nOU R                  5       nU R                  5       (       a  O[        R                  " 5       nU R                  R                  U5      n	 UR                  5       n
[        R                  " SU5      nU(       a  U	(       a  UR                  S5      W;   a  OZXt:w  a)  U(       d  [        U5      nUW:  =(       a    U	n	U	(       a  O-O)U R                  R                  U5      nU(       a  UW:w  a  OGM2  U(       a  U R!                  W5        U R#                  U5        g! [         a    Sn
 Nf = f)aS  
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  Nr  r   )listr  r   r  r  r  rK  r  rc  r  r  ro   r   r  r  r  r  r  )rO   stepintostepover_commandr  
beginframe	beginliner  newframerd  r  	framenamer  newdepthrK  s                 r   step ExecutionControlCommandBase.step	  s   & "4#;#;#=>O'')
>>..z::
 --j9I":.~~228<<%5F++-||~~))+H#'>>#F#Fx#P !$MMO	 		-v6A'AGGAJ/,I %  )(3H,4u,< -A,@ ) ( (
 ..x8f	1Q T ##O4f%A   ! 	!s   G" "G10G1c                 R    U R                  [        R                  " SU-   SS95        g )Nzrun Tr  r  r   rc  r  s      r   runExecutionControlCommandBase.run_	  s    ckk&4-4HIr
   c                 L    U R                  [        R                  " SSS95        g )Nr  Tr  r  )rO   r  s     r   r   ExecutionControlCommandBase.contb	  s    ckk&DABr
   )r  )next)r   r   r   r    r\   rP   r  r  r  r  r  r  r  r  r  r  r  r!   __classcell__r  s   @r   r  r    sP    #
B*!>F04	7&&
Q&fJC Cr
   r  c                   $    \ rS rSrSrS rS rSrg)LanguageInfoif	  ay  
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                     g)zSee this class' docstring.Nr	   rO   r   s     r   r  LanguageInfo.exc_info	  s    r
   c                     g)zK
Implement this if the list of step-into functions depends on the
context.
r	   r	   rX   s    r   r  $LanguageInfo.runtime_break_functions	  s    
 r
   r	   N)r   r   r   r    r\   r  r  r!   r	   r
   r   r  r  f	  s    @%r
   r  c                   8    \ rS rSrS rS rS rS rS rS r	Sr
g	)

PythonInfoi	  c                 r    [        U5      R                  5       nU(       a  U$ [        R                  " S5      e)NzuUnable to find the Python frame, run your code with a debug build (configure with --with-pydebug or compile with -g).)r  r  r   r   rO   r   rL  s      r   rL  PythonInfo.pyframe	  s7    ,'')N""LM Mr
   c                 @    U R                  U5      R                  5       $ r*   )rL  r  r  s     r   rK  PythonInfo.lineno	  s    ||E"3355r
   c                 4    [        U5      R                  5       $ r*   )r  rD  r  s     r   r  PythonInfo.is_relevant_function	  s    U|**,,r
   c                      U R                  U5      nSUR                  5       UR                  5       R                  5       4-  $ ! [         a     g f = f)Nz	%4d    %s)rL  r  r  r  r  r  s      r   r  PythonInfo.get_source_line	  s\    	ll5)G'":":"<")"6"6"8"?"?"A"C C C 		s   AA 
AAc                     UR                  S5      R                  5       n[        R                  " S5      (       a3  [        R
                  S:  a  US   =p4O
US   nUS   nU(       a  SU< 3$ g g ! [        [        4 a     g f = f)Ntstateztstate->frame == f)   rY  r   alpha   current_exceptioncurexc_typecurexc_valuezAn exception was raised: )r  rm   r   parse_and_evalr9   version_infor  r   )rO   r   r  inf_type	inf_values        r   r  PythonInfo.exc_info	  s    	^^H-99;F!!"677##'==+12E+FFHy%m4H &~ 6I<EGG  8 L) 		s   A+A0 0BBc              #      #    Sv   g 7f)NrC  r	   rX   s    r   r  !PythonInfo.static_break_functions	  s     ""s   r	   N)r   r   r   r    rL  rK  r  r  r  r  r!   r	   r
   r   r  r  	  s!    M6-"#r
   r  c                       \ rS rSrSrS rSrg)PythonStepperMixini	  zf
Make this a mixin so CyStep can also inherit from this and use a
CythonCodeStepper at the same time.
c                     [         R                  " SSS9n[        [        R                  " SU5      R                  S5      5      nU R                  USS9  [         R                  " SU-  5        g	)
zY
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  )r  r  r  N)r   rc  r,   r  r  r  r  )rO   r  r  
watchpoints       r   python_stepPythonStepperMixin.python_step	  sW    
 /4@#:FCII!LM
		8h	?K*,-r
   r	   N)r   r   r   r    r\   r  r!   r	   r
   r   r  r  	  s    
.r
   r  c                   ,    \ rS rSrSrSr\S 5       rSrg)r  i	  zStep through Python code.Tc                 6    U R                  U R                  S9  g )N)r  )r  r  r  s      r   r  PyStep.invoke	  s    $--0r
   r	   N)	r   r   r   r    r\   r  r8  r  r!   r	   r
   r   r  r  	  s    H1 1r
   r  c                       \ rS rSrSrSrSrg)r  i	  zStep-over Python code.Fr	   N)r   r   r   r    r\   r  r!   r	   r
   r   r  r  	  s
    Hr
   r  c                   <    \ rS rSrSr\" \R                  5      rSr	g)PyFinishi	  z+Execute until function returns to a caller.r	   N)
r   r   r   r    r\   r8  r  r  r  r!   r	   r
   r   r%  r%  	  s    1!"="D"DEFr
   r%  c                   <    \ rS rSrSr\" \R                  5      rSr	g)PyRuni	  zRun the program.r	   N)
r   r   r   r    r\   r8  r  r  r  r!   r	   r
   r   r'  r'  	  s    !"="A"ABFr
   r'  c                   8    \ rS rSr\" \R                  5      rSrg)PyConti	  r	   N)	r   r   r   r    r8  r  r  r  r!   r	   r
   r   r)  r)  	  s    !"="B"BCFr
   r)  c                 \    U R                   b  [        U R                   5      $ [        U 5      $ )zV
Return the value of the pointer as a Python int.

gdbval.type must be a pointer type
)r   r,   rs  s    r   _pointervaluer+  	  s)     ~~!6>>"" 6{r
   c                 z    [        U 5      n US:  a  [        R                  " S5      e U$ ! [         a     U$ f = f)Nr   z:Negative pointer value, presumably a bug in gdb, aborting.)r+  r   GdbErrorr   )re   r   s     r   pointervaluer.  
  sU    F#GQ;,,  3 4 4  N   	Ns   , 
::c                       [         R                  " S5        g! [         a+     [         R                  " S5         g! [         a      gf = ff = f)NPyUnicode_FromEncodedObjectr6   PyUnicodeUCS2_FromEncodedObjectUCS2UCS4)r   r  r   r	   r
   r   get_inferior_unicode_postfixr4  
  sY    
89   	@A   		s$    
A<
A
A	A

Ac                   N    \ rS rSrSrSrSrS rS rS r	S r
S	 rS
 rSS jrSrg)PythonCodeExecutori
     i  i  c                     [         R                  " SU-  5      n[        U5      nUS:X  a  [         R                  " S5      eU$ )Nz(void *) malloc((size_t) %d)r   z-No memory could be allocated in the inferior.)r   r  r.  r-  )rO   r  chunkr   s       r   mallocPythonCodeExecutor.malloc#
  s>    ##$BT$IJu%a<,,NOOr
   c                 l    U R                  [        U5      5      n[        5       R                  X!5        U$ r*   )r:  rT   r  write_memory)rO   r   r   s      r   alloc_stringPythonCodeExecutor.alloc_string,
  s*    ++c&k*,,W=r
   c                 ~   U R                  U5      nSn [        R                  " U5         [        R                  " SX2[        U5      4-  5      nU R                  U5        [        U5      nUS:X  a  [        R                  " S5      eU$ ! [         a    S[	        5       < S3n Nzf = f! U R                  U5        f = f)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   r4  rT   freer.  r-  )rO   r   stringprA  rd  r   s         r   alloc_pystring!PythonCodeExecutor.alloc_pystring2
  s    ##F+%A"	R9:	'';6V?N NOF IIgv&a<,,  / 0 0 #  	R 1M0N+Q&	R IIgs   B
 $B) 
B&%B&)B<c                 6    [         R                  " SU-  5        g )Nz(void) free((void *) %d)r   r  rO   r   s     r   rD  PythonCodeExecutor.freeK
  s    5?@r
   c                 6    [         R                  " SU-  5        g)zAIncrement the reference count of a Python object in the inferior.zPy_IncRef((PyObject *) %d)NrI  rJ  s     r   increfPythonCodeExecutor.increfN
  s    7'ABr
   c                 6    [         R                  " SU-  5        g)zADecrement the reference count of a Python object in the inferior.zPy_DecRef((PyObject *) %d)NrI  rJ  s     r   xdecrefPythonCodeExecutor.xdecrefR
  s    
 	7'ABr
   Nc                    SU;   a  [         R                  " S5      eUS-  nU R                  U5      n[        U5      n[        U5      nUS:X  d  US:X  a  [         R                  " S5      eS[	        XRXgS9-  n[        5           [         R                  " U5      nU R                  U5         SSS5        U$ ! U R                  U5        f = f! , (       d  f       W$ = f)a  
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)
        )rR  r  globalslocalsN)r   r-  r>  r.  r   FetchAndRestoreErrorr  rD  )	rO   rR  
input_typeglobal_dict
local_dictr   globalsplocalsppyobject_return_values	            r   evalcodePythonCodeExecutor.evalcodeY
  s     4<,,:;;##D),z*q=GqL,,NOO #55 "##(+(:(:4(@%		'"	 $ %$ 		'"	 $# %$s$   CB6C6C		C
Cr	   r  )r   r   r   r    Py_single_inputPy_file_inputPy_eval_inputr:  r>  rF  rD  rM  rP  r]  r!   r	   r
   r   r6  r6  
  s7    OMM2ACC%%r
   r6  c                   *    \ rS rSrSrS rS rS rSrg)rV  i
  z[
Context manager that fetches the error indicator in the inferior and
restores it on exit.
c                 D   [         R                  " S5      R                  5       R                  U l        U R                  U R                  S-  5      U l        U R                  nU R                  U R                  -   nU R                  U R                  S-  -   nXU4U l        g )Nr`   r  rH  )r   r   r   r   sizeof_PyObjectPtrr:  errstate)rO   r   r  r1  s       r   rP   FetchAndRestoreError.__init__
  s    "%//*"="E"E"G"N"N{{4#:#:Q#>?||t666LL4#:#:Q#>>	Y.r
   c                 J    [         R                  " SU R                  -  5        g )NzPyErr_Fetch(%d, %d, %d))r   r  re  rX   s    r   rt  FetchAndRestoreError.__enter__
  s    4t}}DEr
   c                 &   [         R                  " S5      (       a  [         R                  " S5        Sn [         R                  " X R                  -  5        U R                  U R                  5        g ! U R                  U R                  5        f = f)Nz(int) PyErr_Occurred()zPyErr_Print()zAPyErr_Restore((PyObject *) *%d,(PyObject *) *%d,(PyObject *) *%d))r   r  re  rD  r   )rO   r  pyerr_restores      r   r  FetchAndRestoreError.__exit__
  sd    677/0
	$}}}<=IIdll#DIIdll#s   "A3 3B)re  r   rd  N)	r   r   r   r    r\   rP   rt  r  r!   r	   r
   r   rV  rV  
  s    
/F$r
   rV  c                   >   ^  \ rS rSrU 4S jrS r\S 5       rSrU =r	$ )FixGdbCommandi
  c                 l   > [         TU ]  U[        R                  [        R                  5        X l        g r*   )r  rP   r   r  r  actual_command)rO   r  ro  r  s      r   rP   FixGdbCommand.__init__
  s)    #"2"2,/,=,=	?,r
   c                     [         R                  " SS[        [        R                  " [
        5      5         [        [        R                  " S5      5      S:H    g! [         a     gf = f)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)
rH  filterwarningsRuntimeWarningr  escaper   r,   r   r  r   rX   s    r   fix_gdbFixGdbCommand.fix_gdb
  sS    " 	% "		( 3	5	""<01Q6 		s   "A 
A'&A'c                     U R                  5          [        R                  " U R                  < SU< 35        U R                  5         g ! [         a$  n[        R
                  " [        U5      5      eS nAff = f)Nr  )rv  r   rc  ro  r   r-  r  )rO   r  r  es       r   r  FixGdbCommand.invoke
  sW    	'KK4#6#6=> 	  	',,s1v&&	's   'A
 

A8A33A8)ro  )
r   r   r   r    rP   rv  r8  r  r!   r  r  s   @r   rm  rm  
  s!    -
2  r
   rm  c                     [         R                  " S5      n[         R                  " S5      n[        U5      S:X  d  [        U5      S:X  a  [         R                  " S5      eU R	                  XX45      $ )z5
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]  )executorrR  rW  rX  rY  s        r   _evalcode_pythonr}  
  sj     $$%:;K##$89J[!Q&,z*Ba*Gll . / 	/ T{GGr
   c                   *    \ rS rSrS r\S 5       rSrg)PyExeci
  c                    U(       a  U[         R                  4$ / n  [        S5      nUR                  5       S:X  a  OUR	                  U5        M4  SR                  U5      [         R                  4$ ! [
         a     M.  f = f)Nrm  r  r  )r6  r_  inputr  r>   EOFErrorr?   r`  )rO   exprr  r  s       r   readcodePyExec.readcode
  s    +;;;;E' :D {{}-LL&  99U#%7%E%EEE   s   A2 2
B ?B c                     U R                  U5      u  p[        5       nUR                  [        XC[        [
        5      5        g r*   )r  r6  rP  r}  rX  rY  )rO   r  r  rW  r|  s        r   r  PyExec.invoke
  s3    ==.%')(ZXYr
   r	   N)r   r   r   r    r  r8  r  r!   r	   r
   r   r  r  
  s    F$ 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)r\   r   rV  localer9   r   r   r   r   r   rF  r   r   r   r   r   r   r   r   Py_TPFLAGS_TYPE_SUBCLASSr}  r  getpreferredencodingr[  r  r  r  r   r   r(   r-   r2   fsencoderD   AttributeErrorrF   rI   r^   rn   r   r   r   r   r  r   r  r  r   r   r,  r4  r   rC  r   r   r   r   r   r   r   r   r   r   r.  r   r   r  rw  ry  r  r  current_objfiler  r  r  r  r  r  r  r  r  r  r'  r  rH  rj  r6  r  r  r1  r8  r:  rD  FunctionrF  rT  r_  r  r  rh  rc  rn  r  r  r  r  r  r  r  r  r  r  r  r%  r'  r)  r+  r.  r4  r6  rV  rm  r}  r  py_steppy_next	py_finishpy_runpy_contpy_execr  _py_execrI  r	   r
   r   <module>r     st  (Z  	  
-6754
 
  '  '  '  '  '  '  '  '  	&&(>  L &		l 	)
 ++K&	l 	 &u! u!n[  ,*: :"3 #'  @T @TD2Y 24{ 44 {  7 7
 
<; <*k :P%k P%fk B* k * Z	o 	k ]6{ ]6~;[ ;|){ )V"{ "Hk 8y yx K  F' ' *&6 	#


  !k= k=ZJ@S[[ J@\ 5:
$3;; 
$
%S[[ 
% 399hF
H"ckk "(  "#++ "* )ckk )@ 	s{{ 8 	
 
      
) )> 
 >3<< >( _ ,  N ;ckk ;0 
C''):): ;#* #*L$ ;;  -(SC#++ SCl) )X+# +#\. ."1(*< 1V F* FC' CD( D
a% a%H$- $D'CKK 'THZS[[ Z6 ' (
3
Z.GZ.Gjl3I8Z\*FY
-GIz2Gj#"2"2C4E4EFHMMEFKT     s   &S* *	S76S7