1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved. 2 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 7 http://www.apache.org/licenses/LICENSE-2.0 8 9 Unless required by applicable law or agreed to in writing, software 10 distributed under the License is distributed on an "AS IS" BASIS, 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 See the License for the specific language governing permissions and 13 limitations under the License. 14 ==============================================================================*/ 15 16 #ifndef TENSORFLOW_COMPILER_XLA_PYTHON_TRACEBACK_H_ 17 #define TENSORFLOW_COMPILER_XLA_PYTHON_TRACEBACK_H_ 18 19 #include <memory> 20 #include <string> 21 #include <vector> 22 23 #include "absl/container/inlined_vector.h" 24 #include "pybind11/pybind11.h" 25 26 namespace xla { 27 28 // Represents a Python traceback. 29 class Traceback { 30 public: 31 // Require GIL. 32 static std::shared_ptr<Traceback> Get(); 33 34 // Require GIL. enabled()35 static bool enabled() { return enabled_; } 36 // Require GIL. 37 static void SetEnabled(bool enabled); 38 39 Traceback() = default; 40 ~Traceback(); 41 42 Traceback(const Traceback&) = delete; 43 Traceback(Traceback&&) = delete; 44 Traceback& operator=(const Traceback&) = delete; 45 Traceback& operator=(Traceback&&) = delete; 46 47 // Requires the GIL be held. 48 std::string ToString() const; 49 50 struct Frame { 51 pybind11::str file_name; 52 pybind11::str function_name; 53 int function_start_line; 54 int line_num; 55 56 std::string ToString() const; 57 }; 58 std::vector<Frame> Frames() const; 59 raw_frames()60 const absl::InlinedVector<std::pair<PyCodeObject*, int>, 32>& raw_frames() 61 const { 62 return frames_; 63 } 64 65 private: 66 absl::InlinedVector<std::pair<PyCodeObject*, int>, 32> frames_; 67 68 // Protected by GIL. 69 static bool enabled_; 70 }; 71 72 } // namespace xla 73 74 #endif // TENSORFLOW_COMPILER_XLA_PYTHON_TRACEBACK_H_ 75