def _process_stack_frames(self): """Process stack frames. Send the content of source-files, on a best-effort basis. Returns: A list of stack frame IDs. """ stack_frames = tf_stack.extract_stack() stack_frame_ids = [] writer = None for file_path, lineno, func, _ in stack_frames: if (file_path, lineno, func) in self._stack_frame_to_id: stack_frame_ids.append(self._stack_frame_to_id[(file_path, lineno, func)]) continue with self._stack_frame_to_id_lock: if (file_path, lineno, func) not in self._stack_frame_to_id: stack_frame_id = _get_id() self._stack_frame_to_id[(file_path, lineno, func)] = stack_frame_id file_index = self._write_source_file_content(file_path) file_line_col = graph_debug_info_pb2.GraphDebugInfo.FileLineCol( file_index=file_index, line=lineno, func=func) stack_frame_with_id = debug_event_pb2.StackFrameWithId( id=stack_frame_id, file_line_col=file_line_col) writer = self.get_writer() writer.WriteStackFrameWithId(stack_frame_with_id) stack_frame_ids.append(self._stack_frame_to_id[(file_path, lineno, func)]) code_location = debug_event_pb2.CodeLocation( host_name=self._hostname, stack_frame_ids=stack_frame_ids) return code_location
def testWriteSourceFilesAndStackFrames(self): writer = debug_events_writer.DebugEventsWriter(self.dump_root, self.tfdbg_run_id) num_protos = 10 for i in range(num_protos): source_file = debug_event_pb2.SourceFile() source_file.file_path = "/home/tf2user/main.py" source_file.host_name = "machine.cluster" source_file.lines.append("print(%d)" % i) writer.WriteSourceFile(source_file) stack_frame = debug_event_pb2.StackFrameWithId() stack_frame.id = "stack_%d" % i stack_frame.file_line_col.file_index = i * 10 writer.WriteStackFrameWithId(stack_frame) writer.FlushNonExecutionFiles() with debug_events_reader.DebugEventsReader(self.dump_root) as reader: actuals = list(item.debug_event.source_file for item in reader.source_files_iterator()) self.assertLen(actuals, num_protos) for i in range(num_protos): self.assertEqual(actuals[i].file_path, "/home/tf2user/main.py") self.assertEqual(actuals[i].host_name, "machine.cluster") self.assertEqual(actuals[i].lines, ["print(%d)" % i]) actuals = list(item.debug_event.stack_frame_with_id for item in reader.stack_frames_iterator()) self.assertLen(actuals, num_protos) for i in range(num_protos): self.assertEqual(actuals[i].id, "stack_%d" % i) self.assertEqual(actuals[i].file_line_col.file_index, i * 10)
def WriteStackFrame(): stack_frame = debug_event_pb2.StackFrameWithId() with stack_frame_state["lock"]: stack_frame.id = "stack_frame_%d" % stack_frame_state["counter"] stack_frame_state["counter"] += 1 writer.WriteStackFrameWithId(stack_frame) # More-frequent-than-necessary concurrent flushing is not recommended, # but tolerated. writer.FlushNonExecutionFiles()
def testWriteSourceFilesAndStackFrames(self): writer = debug_events_writer.DebugEventsWriter(self.dump_root) num_protos = 10 for i in range(num_protos): source_file = debug_event_pb2.SourceFile() source_file.file_path = "/home/tf2user/main.py" source_file.host_name = "machine.cluster" source_file.lines.append("print(%d)" % i) writer.WriteSourceFile(source_file) stack_frame = debug_event_pb2.StackFrameWithId() stack_frame.id = "stack_%d" % i stack_frame.file_line_col.file_index = i * 10 writer.WriteStackFrameWithId(stack_frame) writer.FlushNonExecutionFiles() source_files_paths = glob.glob( os.path.join(self.dump_root, "*.source_files")) self.assertEqual(len(source_files_paths), 1) actuals = ReadDebugEvents(source_files_paths[0]) self.assertEqual(len(actuals), num_protos) for i in range(num_protos): self.assertEqual(actuals[i].source_file.file_path, "/home/tf2user/main.py") self.assertEqual(actuals[i].source_file.host_name, "machine.cluster") self.assertEqual(actuals[i].source_file.lines, ["print(%d)" % i]) stack_frames_paths = glob.glob( os.path.join(self.dump_root, "*.stack_frames")) self.assertEqual(len(stack_frames_paths), 1) actuals = ReadDebugEvents(stack_frames_paths[0]) self.assertEqual(len(actuals), num_protos) for i in range(num_protos): self.assertEqual(actuals[i].stack_frame_with_id.id, "stack_%d" % i) self.assertEqual( actuals[i].stack_frame_with_id.file_line_col.file_index, i * 10)