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 #include "pybind11/stl.h"
26
27 namespace xla {
28
29 // Represents a Python traceback.
30 class Traceback {
31 public:
32 // Require GIL. Creates a Traceback object that requires destructor to be
33 // invoked with GIL held as well.
34 static std::shared_ptr<Traceback> Get();
35
36 // Safely destroy the traceback object regardless of whether GIL is held or
37 // not.
38 static void SafeDestroy(Traceback traceback);
39
40 // Require GIL.
enabled()41 static bool enabled() { return enabled_; }
42 // Require GIL.
43 static void SetEnabled(bool enabled);
44
45 // Require GIL.
46 Traceback();
47 // Require GIL.
48 ~Traceback();
49
50 Traceback(const Traceback&) = delete;
51 Traceback(Traceback&& other);
52 Traceback& operator=(const Traceback&) = delete;
53 Traceback& operator=(Traceback&&) = delete;
54
55 // Requires the GIL be held.
56 std::string ToString() const;
57
58 struct Frame {
59 pybind11::str file_name;
60 pybind11::str function_name;
61 int function_start_line;
62 int line_num;
63
64 std::string ToString() const;
65 };
66 std::vector<Frame> Frames() const;
67
raw_frames()68 const absl::InlinedVector<std::pair<PyCodeObject*, int>, 32>& raw_frames()
69 const {
70 return frames_;
71 }
72
73 // Returns the traceback as a fake Python Traceback object, suitable for
74 // using as an exception traceback.
75 pybind11::object AsPythonTraceback() const;
76
77 bool operator==(const Traceback& other) const {
78 return frames_ == other.frames_;
79 }
80 bool operator!=(const Traceback& other) const {
81 return frames_ != other.frames_;
82 }
83
84 private:
85 absl::InlinedVector<std::pair<PyCodeObject*, int>, 32> frames_;
86
87 // Protected by GIL.
88 static bool enabled_;
89 };
90
91 template <typename H>
AbslHashValue(H h,const Traceback & traceback)92 H AbslHashValue(H h, const Traceback& traceback) {
93 h = H::combine(std::move(h), traceback.raw_frames());
94 return h;
95 }
96
97 void BuildTracebackSubmodule(pybind11::module& m);
98
99 } // namespace xla
100
101 #endif // TENSORFLOW_COMPILER_XLA_PYTHON_TRACEBACK_H_
102