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.
GetPythonString(PyObject * o)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 
ToStackFrames(const StackTraceMap & mapper,const StackTraceFilter & filtered,bool reverse_traversal,int limit) const44 std::vector<StackFrame> StackTrace::ToStackFrames(
45     const StackTraceMap& mapper, 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 = PyCode_Addr2Line(code_obj.first, code_obj.second);
59 
60     if (filtered && filtered(file_name)) {
61       continue;
62     }
63 
64     absl::optional<StackFrame> mapped =
65         mapper ? mapper(std::make_pair(file_name, line_number)) : absl::nullopt;
66 
67     if (mapped) {
68       result.push_back(*mapped);
69     } else {
70       result.emplace_back(StackFrame{file_name, line_number,
71                                      GetPythonString(code_obj.first->co_name)});
72     }
73 
74     if (result.size() == limit) {
75       break;
76     }
77   }
78 
79   return result;
80 }
81 
Get(int id)82 StackTrace* StackTraceManager::Get(int id) {
83   DCheckPyGilStateForStackTrace();
84   if (next_id_ - id > kStackTraceCircularBufferSize) return nullptr;
85 
86   return &stack_traces_[id & (kStackTraceCircularBufferSize - 1)];
87 }
88 
89 StackTraceManager* const stack_trace_manager = new StackTraceManager();
90 
91 }  // namespace tensorflow
92