o
    i e!                     @   s   d Z ddlZddlZddlmZ ddlmZ ddlmZ ddlm	Z	 ddlm
Z
 ddlmZ dd	lmZ d
d Zdd Zdd Zdd Z				dddZ	dddZ	dddZdS )z;Communicating tracebacks and source code with debug server.    N)debug_service_pb2)	debug_pb2)common)debug_service_pb2_grpc)source_utils)gfile)tfprof_loggerc                 C   s   t | }t |_| |_|j|_|j|_	z%t 
| d}|j|   W d    W d S 1 s3w   Y  W d S  tyD   Y d S w )Nr)r   Statsocketgethostnamehost	file_path
mtime_nseclast_modifiedlengthbytesOpenlinesextendread
splitlinesIOError)r   source_file_proto	file_statf r   X/var/www/myenv/lib/python3.10/site-packages/tensorflow/python/debug/lib/source_remote.py_load_debugged_source_file   s   

&r   c                 C   s   | |vr
t ||| < ||  S N)len)stringstring_to_idr   r   r   _string_to_id+   s   r#   c                 C   sz   i }d|d< | D ]}|\}}}}|j jjt|||t||t||d q|j}| D ]\}	}
|	dur6|	nd||
< q,dS )zFormat a traceback stack for a `CallTraceback` proto.

  Args:
    origin_stack: The stack list as returned by `traceback.extract_stack()`.
    call_traceback_proto: A `CallTraceback` proto whose fields are to be
      populated.
  r   N)file_idlinenofunction_idline_id )origin_stacktracesaddr#   origin_id_to_stringitems)r)   call_traceback_protor"   framer   r%   	func_name	line_textid_to_stringkeyvaluer   r   r   _format_origin_stack1   s   r5   c                    sL   t  }| D ]}|jD ]}||j q
q fdd|D }dd |D }|S )a4  Extract source file paths outside TensorFlow Python library.

  Args:
    code_defs: An iterable of `CodeDef` protos, i.e., an iterable of stack
      traces.
    id_to_string: A proto map from integer ids to strings.

  Returns:
    An iterable of source file paths outside the TensorFlow Python library.
  c                 3   s    | ]} | V  qd S r   r   ).0r$   r2   r   r   	<genexpr>W   s    zC_source_file_paths_outside_tensorflow_py_library.<locals>.<genexpr>c                 s   s(    | ]}t |st|r|V  qd S r   )r   guess_is_tensorflow_py_libraryr   Exists)r6   r   r   r   r   r8   X   s    
)setr*   r+   r$   )	code_defsr2   file_idscode_deftracenon_tf_filesr   r7   r   0_source_file_paths_outside_tensorflow_py_libraryH   s   
rA   FTc                 C   s0  t | ts| g} dd | D } |rtjjntjj}|r"tj|ddnd}tj||||r.|jndd}t	|| |rpt
 }	|	tdd |jjD |jj |	t|jg|j g }
|	D ]}t }t||j  |
| q\| D ]#}d	d
g}tj||d}t|}|| |r|
D ]}|| qqrdS )ar  Send the tracebacks of a TensorFlow execution call.

  To gRPC debug server(s). This applies to graph execution (`tf.Session.run()`)
  calls and eager execution calls.

  If `send_source`, also sends the underlying source files outside the
  TensorFlow library.

  Args:
    destinations: gRPC destination addresses, a `str` or a `list` of `str`s,
      e.g., "localhost:4242". If a `list`, gRPC requests containing the same
      `CallTraceback` proto payload will be sent to all the destinations.
    origin_stack: The traceback stack for the origin of the execution call. For
      graph execution, this is the traceback of the `tf.Session.run()`
      invocation. For eager execution, this is the traceback of the Python
      line that executes the eager operation.
    is_eager_execution: (`bool`) whether an eager execution call (i.e., not a
      `tf.Session.run` or derived methods) is being sent.
    call_key: The key of the execution call, as a string. For graph execution,
      this is a string describing the feeds, fetches (and targets) names of the
      `tf.Session.run` call. For eager execution, this is ignored.
    graph: A Python `tf.Graph` object (i.e., *not* a `tf.compat.v1.GraphDef`),
      which contains op tracebacks, if applicable.
    send_source: Whether the source files involved in the op tracebacks but
      outside the TensorFlow library are to be sent.
  c                 S   s.   g | ]}| tjr|ttjd  n|qS r   )
startswithr   GRPC_URL_PREFIXr    )r6   destr   r   r   
<listcomp>   s    
z)_send_call_tracebacks.<locals>.<listcomp>F)add_trainable_varN)	call_typecall_keygraph_tracebackgraph_versionc                 s   s    | ]}|j V  qd S r   )r>   )r6   	log_entryr   r   r   r8      s    z(_send_call_tracebacks.<locals>.<genexpr>)zgrpc.max_receive_message_length)zgrpc.max_send_message_lengthrL   )options)
isinstancelistr   CallTracebackEAGER_EXECUTIONGRAPH_EXECUTIONr   merge_default_with_oplogversionr5   r;   updaterA   rI   log_entriesr2   r)   r,   r   DebuggedSourceFilesr   source_filesr+   appendgrpcinsecure_channelr   EventListenerStubSendTracebacksSendSourceFiles)destinationsr)   is_eager_executionrH   graphsend_sourcerG   rI   call_tracebacksource_file_pathsdebugged_source_filesr   rX   destinationno_max_message_sizeschannelstubr   r   r   _send_call_tracebacks^   sd   
 





rj   c                 C   s   t | |d|||d dS )a  Send the tracebacks of a graph execution call to debug server(s).

  Args:
    destinations: gRPC destination addresses, a `str` or a `list` of `str`s,
      e.g., "localhost:4242". If a `list`, gRPC requests containing the same
      `CallTraceback` proto payload will be sent to all the destinations.
    run_key: A string describing the feeds, fetches (and targets) names of the
      `tf.Session.run` call.
    origin_stack: The traceback of the `tf.Session.run()` invocation.
    graph: A Python `tf.Graph` object (i.e., *not* a `tf.compat.v1.GraphDef`),
      which contains op tracebacks.
    send_source: Whether the source files involved in the op tracebacks but
      outside the TensorFlow library are to be sent.
  F)r`   rH   ra   rb   Nrj   )r_   run_keyr)   ra   rb   r   r   r   send_graph_tracebacks   s   
rm   c                 C   s   t | |d|d dS )a  Send the tracebacks of an eager execution call to debug server(s).

  Args:
    destinations: gRPC destination addresses, a `str` or a `list` of `str`s,
      e.g., "localhost:4242". If a `list`, gRPC requests containing the same
    origin_stack: The traceback of the eager operation invocation.
    send_source: Whether the source files involved in the op tracebacks but
      outside the TensorFlow library are to be sent.
  T)r`   rb   Nrk   )r_   r)   rb   r   r   r   send_eager_tracebacks   s   
rn   )FNNT)T)__doc__r   rZ   tensorflow.core.debugr   tensorflow.core.protobufr   tensorflow.python.debug.libr   r   r   tensorflow.python.platformr   tensorflow.python.profilerr   r   r#   r5   rA   rj   rm   rn   r   r   r   r   <module>   s.   
R
