forked from peadar/pstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython.cc
164 lines (139 loc) · 5.27 KB
/
python.cc
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
164
#include "libpstack/python.h"
#include <dlfcn.h>
#include <string.h>
#include <regex.h>
static
std::tuple<Elf::Object::sptr, Elf::Addr, Elf::Addr>
getInterpHead(const Process &);
template <int V>
std::tuple<Elf::Object::sptr, Elf::Addr, Elf::Addr>
getInterpHead(const Process &);
PyInterpInfo
getPyInterpInfo(const Process &proc) {
Elf::Object::sptr libpython;
Elf::Addr libpythonAddr;
Elf::Addr interpreterHead;
std::tie(libpython, libpythonAddr, interpreterHead) = getInterpHead(proc);
std::string filename = libpython->io->filename();
auto index = filename.find("python");
if (filename.length() < index + 9) //index + len("pythonX.Y")
throw Exception() << "Can't parse python version from lib/exec name: " << filename;
char majorChar = filename[index + 6];
char minorChar = filename[index + 8];
if (!isdigit(majorChar) || !isdigit(minorChar))
throw Exception() << "lib/exec name doesn't match \"*pythonX.Y.*\" format";
int major = majorChar - '0';
int minor = minorChar - '0';
if (verbose)
std::clog << "python version is: " << major << "." << minor << std::endl;
return PyInterpInfo {
libpython, libpythonAddr, interpreterHead,
"v" + std::to_string(major) + "." + std::to_string(minor),
V2HEX(major, minor)};
}
template <>
std::tuple<Elf::Object::sptr, Elf::Addr, Elf::Addr>
getInterpHead<2>(const Process &proc) {
for (auto &o : proc.objects) {
std::string module = stringify(*o.second->io);
if (module.find("python") == std::string::npos)
continue;
auto image = o.second;
auto &syms = image->commonSections->debugSymbols;
for (auto sym : syms) {
if (sym.name.substr(0, 11) != "interp_head")
continue;
auto libpython = o.second;
auto libpythonAddr = o.first;
auto interpHead = libpythonAddr + sym.symbol.st_value;
if (verbose)
*debug << "python2 library is " << *libpython->io << std::endl;
return std::make_tuple(libpython, libpythonAddr, interpHead);
}
}
throw Exception() << "No libpython2 found";
}
template <>
std::tuple<Elf::Object::sptr, Elf::Addr, Elf::Addr>
getInterpHead<3>(const Process &proc) {
Elf::Object::sptr libpython;
Elf::Addr libpythonAddr;
Elf::Addr _PyRuntime;
std::tie(libpython, libpythonAddr, _PyRuntime) = proc.findSymbolDetail("_PyRuntime", false);
Elf::Addr interpHead = _PyRuntime + sizeof(int) * 2 + sizeof(void *)*2;
if (libpython == nullptr) {
if (verbose)
std::clog << "Python 3 interpreter not found" << std::endl;
throw Exception() << "No libpython3 found";
}
if (verbose)
*debug << "python3 library is " << *libpython->io << std::endl;
return std::make_tuple(libpython, libpythonAddr, interpHead);
}
std::tuple<Elf::Object::sptr, Elf::Addr, Elf::Addr>
getInterpHead(const Process &proc) {
try {
Elf::Object::sptr libpython;
Elf::Addr libpythonAddr;
Elf::Addr interpHeadp = proc.findSymbol("Py_interp_headp", false,
[&](Elf::Addr loadAddr, const Elf::Object::sptr &o) mutable {
libpython = o;
libpythonAddr = loadAddr;
auto name = stringify(*o->io);
return name.find("python") != std::string::npos;
});
if (verbose)
*debug << "found interp_headp in ELF syms" << std::endl;
Elf::Addr interpHead;
proc.io->readObj(interpHeadp, &interpHead);
return std::make_tuple(libpython, libpythonAddr, interpHead);
}
catch (...) {
if (verbose)
std::clog << "Py_interp_headp symbol not found. Trying fallback" << std::endl;
}
try {
return getInterpHead<2>(proc);
} catch (...) {
if (verbose)
std::clog << "Python 2 interpreter not found" << std::endl;
}
try {
return getInterpHead<3>(proc);
} catch (...) {
if (verbose)
std::clog << "Python 3 interpreter not found" << std::endl;
}
throw Exception() << "Couldn't find a Python interpreter";
}
// libpthread includes offsets for fields in various structures. We can use
// _thred_db_pthread_tid to work out the offset within a pthread structure of
// the "tid" in a pthread. This gives us a way to find an LWP for a given
// pthread_t. (In Linux's 1:1 modern threadding model, each pthread_t is associated
// with a single LWP, or Linux task.)
bool
pthreadTidOffset(const Process &proc, size_t *offsetp)
{
static size_t offset;
static enum { notDone, notFound, found } status;
if (status == notDone) {
try {
auto addr = proc.findSymbol("_thread_db_pthread_tid", true);
uint32_t desc[3];
proc.io->readObj(addr, &desc[0], 3);
offset = desc[2];
status = found;
if (verbose)
*debug << "found thread offset " << offset << "\n";
} catch (const std::exception &ex) {
if (verbose)
*debug << "failed to find offset of tid in pthread: " << ex.what();
status = notFound;
}
}
if (status == found) {
*offsetp = offset;
return true;
}
return false;
}