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 | #include "tensorflow/python/util/stack_trace.h" |
17 | |
18 | #include <limits> |
19 | |
20 | #include "tensorflow/core/platform/str_util.h" |
21 | #include "tensorflow/core/platform/stringpiece.h" |
22 | |
23 | namespace { |
24 | |
25 | // Returns C string from a Python string object. Handles Python2/3 strings. |
26 | // TODO(kkb): This is a generic Python utility function. factor out as a |
27 | // utility. |
28 | const char* GetPythonString(PyObject* o) { |
29 | #if PY_MAJOR_VERSION >= 3 |
30 | if (PyBytes_Check(o)) { |
31 | return PyBytes_AsString(o); |
32 | } else { |
33 | return PyUnicode_AsUTF8(o); |
34 | } |
35 | #else |
36 | return PyBytes_AsString(o); |
37 | #endif |
38 | } |
39 | |
40 | } // namespace |
41 | |
42 | namespace tensorflow { |
43 | |
44 | std::vector<StackFrame> StackTrace::ToStackFrames( |
45 | const SourceMap& source_map, const StackTraceFilter& filtered, |
46 | bool reverse_traversal, int limit) const { |
47 | DCheckPyGilStateForStackTrace(); |
48 | std::vector<StackFrame> result; |
49 | result.reserve(code_objs_.size()); |
50 | |
51 | if (limit == -1) limit = std::numeric_limits<int>::max(); |
52 | |
53 | for (int i = 0; i < code_objs_.size(); i++) { |
54 | int idx = reverse_traversal ? i : code_objs_.size() - 1 - i; |
55 | |
56 | const std::pair<PyCodeObject*, int>& code_obj = code_objs_[idx]; |
57 | const char* file_name = GetPythonString(code_obj.first->co_filename); |
58 | const int line_number = code_obj.second; |
59 | |
60 | if (filtered && filtered(file_name)) { |
61 | continue; |
62 | } |
63 | |
64 | const auto it = source_map.find(SourceLoc{file_name, line_number}); |
65 | if (it != source_map.end()) { |
66 | result.push_back(it->second); |
67 | } else { |
68 | result.emplace_back(StackFrame{file_name, line_number, |
69 | GetPythonString(code_obj.first->co_name)}); |
70 | } |
71 | |
72 | if (result.size() == limit) { |
73 | break; |
74 | } |
75 | } |
76 | |
77 | return result; |
78 | } |
79 | |
80 | StackTrace* StackTraceManager::Get(int id) { |
81 | DCheckPyGilStateForStackTrace(); |
82 | if (next_id_ - id > kStackTraceCircularBufferSize) return nullptr; |
83 | |
84 | return &stack_traces_[id & (kStackTraceCircularBufferSize - 1)]; |
85 | } |
86 | |
87 | StackTraceManager* const stack_trace_manager = new StackTraceManager(); |
88 | |
89 | } // namespace tensorflow |
90 | |