forked from peadar/pstack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlive.cc
212 lines (188 loc) · 5.3 KB
/
live.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "libpstack/proc.h"
#include "libpstack/ps_callback.h"
#include <sys/ptrace.h>
#include <sys/types.h>
#include <dirent.h>
#include <err.h>
#include <fcntl.h>
#include <unistd.h>
#include <wait.h>
#include <climits>
#include <iostream>
#include <utility>
#include <fstream>
std::string
procname(pid_t pid, const std::string &base)
{
return linkResolve(stringify("/proc/", pid, "/", base));
}
LiveReader::LiveReader(pid_t pid, const std::string &base)
: FileReader(procname(pid, base)) {}
LiveProcess::LiveProcess(Elf::Object::sptr &ex, pid_t pid_,
const PstackOptions &options, Dwarf::ImageCache &imageCache)
: Process(
ex ? ex : imageCache.getImageForName(procname(pid_, "exe")),
std::make_shared<CacheReader>(std::make_shared<LiveReader>(pid_, "mem")),
options, imageCache)
, pid(pid_)
{
(void)ps_getpid(this);
}
void
LiveProcess::load(const PstackOptions &options)
{
StopLWP here(this, pid);
processAUXV(LiveReader(pid, "auxv"));
Process::load(options);
}
bool
LiveProcess::getRegs(lwpid_t pid, Elf::CoreRegisters *reg)
{
#ifdef __FreeBSD__
int rc;
rc = ptrace(PT_GETREGS, pid, (caddr_t)reg, 0);
if (rc == -1) {
warn("failed to trace LWP %d", (int)pid);
return false;
}
return true;
#endif
#ifdef __linux__
stop(pid);
iovec iov;
iov.iov_base = reg;
iov.iov_len = sizeof *reg;
int rc= ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iov) != -1;
resume(pid);
return rc == 0;
#endif
}
void
LiveProcess::resume(lwpid_t pid)
{
auto &tcb = lwps[pid];
assert(tcb.stopCount != 0); // We can't resume an LWP that is not suspended.
if (--tcb.stopCount != 0)
return;
kill(pid, SIGCONT);
if (ptrace(PT_DETACH, pid, caddr_t(1), 0) != 0)
std::clog << "failed to detach from process " << pid << ": " << strerror(errno) << "\n";
dynamic_cast<CacheReader&>(*io).flush();
if (verbose >= 1) {
timeval tv;
gettimeofday(&tv, nullptr);
intmax_t usecs = (tv.tv_sec - tcb.stoppedAt.tv_sec) * 1000000;
usecs += tv.tv_usec;
usecs -= tcb.stoppedAt.tv_usec;
*debug << "resumed LWP " << pid << ": was stopped for " << std::dec <<
usecs << " microseconds" << std::endl;
}
}
void
LiveProcess::findLWPs()
{
std::string dirName = procname(pid, "task");
DIR *d = opendir(dirName.c_str());
dirent *de;
if (d != nullptr) {
while ((de = readdir(d)) != nullptr) {
char *p;
lwpid_t pid = strtol(de->d_name, &p, 0);
if (*p == 0)
(void)lwps[pid];
}
closedir(d);
}
}
pid_t
LiveProcess::getPID() const
{
return pid;
}
void
LiveProcess::stopProcess()
{
stop(pid); // suspend the main process itself first.
findLWPs();
/*
* suspend any threads that the thread-db knows about.
* XXX: This doesn't actually work under linux: If we fail, just stop the LWP
*/
listThreads([this] (const td_thrhandle_t *thr) {
td_thrinfo_t info;
td_thr_get_info(thr, &info);
(void)lwps[info.ti_lid]; // make sure we have the LWP
if (td_thr_dbsuspend(thr) == TD_NOCAPAB) {
if (verbose >= 3)
*debug << "can't suspend thread " << thr
<< ": will suspend it's LWP " << info.ti_lid << "\n";
}
});
// Stop all LWPS.
int i = 0;
for (auto &lwp : lwps) {
stop(lwp.first);
i++;
}
if (verbose >= 2)
*debug << "stopped process " << pid << "\n";
}
void
LiveProcess::resumeProcess()
{
listThreads([] (const td_thrhandle_t *thr) {
if (td_thr_dbresume(thr) == TD_NOCAPAB) {
// this doesn't work in general, but it's ok, we'll suspend the LWP
if (verbose >= 3)
*debug << "can't resume thread " << thr << "\n";
}
});
for (auto &lwp : lwps)
resume(lwp.first);
resume(pid);
}
void
LiveProcess::stop(lwpid_t pid)
{
auto &tcb = lwps[pid];
if (tcb.stopCount++ != 0)
return;
gettimeofday(&tcb.stoppedAt, nullptr);
if (ptrace(PT_ATTACH, pid, 0, 0) != 0) {
*debug << "failed to stop LWP " << pid << ": ptrace failed: " << strerror(errno) << "\n";
return;
}
int status;
pid_t waitedpid = waitpid(pid, &status, pid == this->pid ? 0 : __WCLONE);
if (waitedpid == -1)
*debug << "failed to stop LWP " << pid << ": wait failed: " << strerror(errno) << "\n";
}
std::vector<AddressRange>
LiveProcess::addressSpace() const {
std::vector<AddressRange> rv;
std::ifstream input(procname(pid, "maps"));
for (;;) {
std::string startS, endS, restS;
getline(input, startS, '-');
getline(input, endS, ' ');
getline(input, restS);
if (input.eof() || !input.good())
break;
size_t pos;
Elf::Addr start = stoull(startS, &pos, 16);
if (startS[pos] != 0)
continue;
Elf::Addr end = stoull(endS, &pos, 16);
if (endS[pos] != 0)
continue;
rv.emplace_back(start, end - start, end - start);
}
return rv;
}
bool
LiveProcess::loadSharedObjectsFromFileNote()
{
// In theory we can implement this by grovelling in /proc/<pid>/maps, but
// it mostly exists for truncated core files, so don't bother now.
return false;
}