-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathpystack.cpp
More file actions
163 lines (132 loc) · 4.99 KB
/
pystack.cpp
File metadata and controls
163 lines (132 loc) · 4.99 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "extension.h"
#include "PyFrameObject.h"
#include "PyInterpreterFrame.h"
#include "PyDictObject.h"
#include "PyCodeObject.h"
#include "PyInterpreterState.h"
#include "PyThreadState.h"
using namespace PyExt::Remote;
#include "ExtHelpers.h"
#include <engextcpp.hpp>
#include <string>
#include <utility>
#include <sstream>
#include <stdexcept>
#include <algorithm>
#include <iterator>
#include <optional>
using namespace std;
namespace {
auto getCurrentThreadId(IDebugSystemObjects* pSystem) -> uint64_t
{
ULONG currentThreadSystemId = 0;
HRESULT hr = pSystem->GetCurrentThreadId(¤tThreadSystemId);
if (FAILED(hr))
throw runtime_error("Failed to retreive current thread id.");
return currentThreadSystemId;
}
auto getCurrentThreadSystemId(IDebugSystemObjects* pSystem) -> uint64_t
{
ULONG currentThreadSystemId = 0;
HRESULT hr = pSystem->GetCurrentThreadSystemId(¤tThreadSystemId);
if (FAILED(hr))
throw runtime_error("Failed to retreive current thread system id.");
return currentThreadSystemId;
}
auto frameToString(const PyFrame& frame) -> string
{
ostringstream oss;
auto codeObject = frame.code();
if (codeObject == nullptr)
throw runtime_error("Warning: PyFrameObject is missing PyCodeObject.");
auto filename = codeObject->filename();
oss << "File \"" << utils::link(filename, ".open " + filename, "Open source code.")
<< "\", line " << frame.currentLineNumber()
<< ", in " << utils::link(codeObject->name(), "!pyobj 0n" + to_string(codeObject->offset()), "Inspect PyCodeObject.");
return oss.str();
}
auto frameToCommandString(const PyFrame& frame) -> string
{
ostringstream oss;
auto* interpreterFrame = dynamic_cast<const PyInterpreterFrame*>(&frame);
if (interpreterFrame) {
// 3.11+
oss << utils::link("[Frame]", "!pyinterpreterframe 0n"s + to_string(interpreterFrame->offset()), "Inspect interpreter frame (including localsplus).") << " ";
} else {
// <3.11
auto& frameObject = dynamic_cast<const PyFrameObject&>(frame);
oss << utils::link("[Frame]", "!pyobj 0n"s + to_string(frameObject.offset()), "Inspect frame object (including localsplus).") << " ";
}
auto locals = frame.locals();
if (locals != nullptr && locals->offset() != 0)
oss << utils::link("[Locals]", "!pyobj 0n"s + to_string(locals->offset()), "Inspect this frame's locals.") << " ";
auto globals = frame.globals();
if (globals != nullptr && globals->offset() != 0)
oss << utils::link("[Globals]", "!pyobj 0n"s + to_string(globals->offset()), "Inspect this frame's captured globals.") << " ";
return oss.str();
}
}
namespace PyExt {
EXT_COMMAND(pystack,
"Prints the Python stack for the current thread, or starting at a provided PyFrameObject."
" Use -all to enumerate all Python threads.",
"{all;b;;Print the Python stack for all Python threads.}"
"{;s,o;PyFrameObject address}")
{
ensureSymbolsLoaded();
// Prints the frame chain starting at `topFrame`, consuming the unique_ptr.
auto printStack = [this](unique_ptr<PyFrame> frame) {
for (; frame != nullptr; frame = frame->previous()) {
Dml("\t%s\n", frameToString(*frame).c_str());
Dml("\t\t%s\n", frameToCommandString(*frame).c_str());
}
};
try {
if (HasArg("all")) {
for (auto&& istate : PyInterpreterState::allInterpreterStates()) {
for (auto&& tstate : istate.allThreadStates()) {
Out("Python Thread 0n%s:\n", to_string(tstate.thread_id()).c_str());
try {
auto frame = tstate.currentFrame();
if (frame == nullptr)
Out("\t<no Python frames>\n");
else
printStack(move(frame));
} catch (exception& ex) {
Warn("\t%s\n", ex.what());
}
Out("\n");
}
}
}
if (m_NumUnnamedArgs >= 1) {
// Print info about the user-provided PyFrameObject as a header.
auto frameOffset = evalOffset(GetUnnamedArgStr(0));
Out("Stack trace starting at (PyFrameObject*)(%y):\n", frameOffset);
auto frame = make_unique<PyFrameObject>(frameOffset);
if (frame == nullptr)
throw runtime_error("Could not find PyFrameObject or PyInterpreterFrame.");
printStack(move(frame));
} else if (!HasArg("all")) {
// Print the thread header.
auto threadHeader = "Thread " + to_string(getCurrentThreadId(m_System)) + ":";
Out("%s\n", threadHeader.c_str());
auto threadSystemId = getCurrentThreadSystemId(m_System);
auto threadState = PyInterpreterState::findThreadStateBySystemThreadId(threadSystemId);
if (!threadState.has_value())
throw runtime_error("Thread does not contain any Python frames.");
auto frame = threadState->currentFrame();
if (frame == nullptr)
throw runtime_error("Could not find PyFrameObject or PyInterpreterFrame.");
printStack(move(frame));
}
} catch (exception& ex) {
Warn("\t%s\n", ex.what());
if (!HasFullMemBasic()) {
Err("\tThe dump file does not contain enough data for PyExt to work properly.\n");
Err("\tCapture a dump with full memory to ensure PyExt can reconstruct callstacks.\n");
}
}
Out("\n");
}
}