Repository navigation
Expand file tree
/
Copy pathstack_trace.h
More file actions
108 lines (84 loc) · 2.96 KB
/
Copy pathstack_trace.h
File metadata and controls
108 lines (84 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#ifndef TENSORFLOW_PYTHON_UTIL_STACK_TRACE_H_
#define TENSORFLOW_PYTHON_UTIL_STACK_TRACE_H_
#include <Python.h>
#include <frameobject.h>
#include <array>
#include <sstream>
#include <string>
#include "absl/base/attributes.h"
#include "absl/base/optimization.h"
#include "tensorflow/python/lib/core/py_util.h"
namespace tensorflow {
// A class for capturing Python stack trace.
class StackTrace final {
public:
static constexpr int kMaxDepth = 10;
StackTrace() : size_(0) {}
// Returns `StackTrace` object that captures the current Python stack trace.
// Python GIL must be acquired beforehand.
ABSL_MUST_USE_RESULT
ABSL_ATTRIBUTE_HOT
static StackTrace Capture() {
DCheckPyGilState();
StackTrace result;
const PyFrameObject* frame = PyThreadState_GET()->frame;
int i = 0;
for (; i < kMaxDepth && frame != nullptr; frame = frame->f_back, ++i) {
PyCodeObject* code_obj = frame->f_code;
DCHECK(frame->f_trace == nullptr);
DCHECK(code_obj != nullptr);
Py_INCREF(code_obj);
result.code_objs_[i] = code_obj;
result.last_instructions_[i] = frame->f_lasti;
}
result.size_ = i;
return result;
}
// Python GIL must be acquired beforehand.
ABSL_ATTRIBUTE_HOT
~StackTrace() { Clear(); }
StackTrace(StackTrace&& other) {
code_objs_ = other.code_objs_;
last_instructions_ = other.last_instructions_;
size_ = other.size_;
other.size_ = 0;
}
// Python GIL must be acquired beforehand.
ABSL_ATTRIBUTE_HOT
StackTrace& operator=(StackTrace&& other) {
Clear();
code_objs_ = other.code_objs_;
last_instructions_ = other.last_instructions_;
size_ = other.size_;
other.size_ = 0;
return *this;
}
// Returns string representation of the captured stack trace.
std::string ToString() const;
// TODO(kkb): Implement structured stack trace object getter.
private:
std::array<PyCodeObject*, kMaxDepth> code_objs_;
std::array<int, kMaxDepth> last_instructions_;
int size_;
// Python GIL must be acquired beforehand.
ABSL_ATTRIBUTE_HOT
void Clear() {
DCheckPyGilState();
for (int i = 0; i < size_; ++i) Py_DECREF(code_objs_[i]);
}
StackTrace(const StackTrace&) = delete;
StackTrace& operator=(const StackTrace&) = delete;
};
} // namespace tensorflow
#endif // TENSORFLOW_PYTHON_UTIL_STACK_TRACE_H_