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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
// Copyright (c) 2007, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Author: Alfred Peng
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include "client/solaris/handler/solaris_lwp.h"
#include "common/solaris/message_output.h"
using namespace google_breakpad;
// This unamed namespace contains helper function.
namespace {
uintptr_t stack_base_address = 0;
static const int HEADER_MAX = 2000;
static const int MAP_MAX = 1000;
// Convert from string to int.
static bool LocalAtoi(char *s, int *r) {
assert(s != NULL);
assert(r != NULL);
char *endptr = NULL;
int ret = strtol(s, &endptr, 10);
if (endptr == s)
return false;
*r = ret;
return true;
}
// Callback invoked for each mapped module.
// It use the module's adderss range to validate the address.
static bool AddressNotInModuleCallback(const ModuleInfo &module_info,
void *context) {
uintptr_t addr = reinterpret_cast<uintptr_t>(context);
if ((module_info.start_addr > 0) &&
(addr >= module_info.start_addr) &&
(addr <= module_info.start_addr + module_info.size)) {
stack_base_address = module_info.start_addr + module_info.size;
return false;
}
return true;
}
static int IterateLwpAll(int pid,
CallbackParam<LwpidCallback> *callback_param) {
char lwp_path[40];
DIR *dir;
int count = 0;
snprintf(lwp_path, sizeof (lwp_path), "/proc/%d/lwp", (int)pid);
if ((dir = opendir(lwp_path)) == NULL)
return -1;
struct dirent *entry = NULL;
while ((entry = readdir(dir)) != NULL) {
if ((strcmp(entry->d_name, ".") != 0) &&
(strcmp(entry->d_name, "..") != 0)) {
int lwpid = 0;
int last_pid = 0;
if (LocalAtoi(entry->d_name, &lwpid) && last_pid != lwpid) {
last_pid = lwpid;
++count;
if (callback_param &&
!(callback_param->call_back)(lwpid, callback_param->context)) {
break;
}
}
}
}
closedir(dir);
return count;
}
class AutoCloser {
public:
AutoCloser(int fd) : fd_(fd) {}
~AutoCloser() { if (fd_) close(fd_); }
private:
int fd_;
};
// Control the execution of the lwp.
// Suspend/Resume lwp based on the value of context.
static bool ControlLwp(int lwpid, void *context) {
// The current thread is the one to handle the crash. Ignore it.
if (lwpid != pthread_self()) {
int ctlfd;
char procname[PATH_MAX];
bool suspend = *(bool *)context;
// Open the /proc/$pid/lwp/$lwpid/lwpctl files
snprintf(procname, sizeof (procname), "/proc/self/lwp/%d/lwpctl", lwpid);
if ((ctlfd = open(procname, O_WRONLY|O_EXCL)) < 0) {
print_message2(2, "failed to open %s in ControlLwp\n", procname);
return false;
}
AutoCloser autocloser(ctlfd);
long ctl[2];
ctl[0] = suspend ? PCSTOP : PCRUN;
ctl[1] = 0;
if (write(ctlfd, ctl, sizeof (ctl)) != sizeof (ctl)) {
print_message2(2, "failed in lwp %d\n", lwpid);
return false;
}
}
return true;
}
/*
* Utility function to read the contents of a file that contains a
* prheader_t at the start (/proc/$pid/lstatus or /proc/$pid/lpsinfo).
* Return true on success.
*/
static bool read_lfile(int pid, const char *lname, prheader_t *lhp) {
char lpath[PATH_MAX];
struct stat statb;
int fd;
size_t size;
snprintf(lpath, sizeof (lpath), "/proc/%d/%s", pid, lname);
if ((fd = open(lpath, O_RDONLY)) < 0) {
print_message2(2, "failed to open %s in read_lfile\n", lpath);
return false;
}
AutoCloser autocloser(fd);
if (fstat(fd, &statb) != 0)
return false;
size = statb.st_size;
if ((size / sizeof (prheader_t)) + 32 > HEADER_MAX) {
print_message1(2, "map size overflow\n");
return false;
}
if (pread(fd, lhp, size, 0) <= sizeof (prheader_t))
return false;
return true;
}
} // namespace
namespace google_breakpad {
SolarisLwp::SolarisLwp(int pid) : pid_(pid) {
}
SolarisLwp::~SolarisLwp() {
}
int SolarisLwp::ControlAllLwps(bool suspend) {
CallbackParam<LwpidCallback> callback_param(ControlLwp, &suspend);
return IterateLwpAll(pid_, &callback_param);
}
int SolarisLwp::GetLwpCount() const {
return IterateLwpAll(pid_, NULL);
}
int SolarisLwp::Lwp_iter_all(int pid,
CallbackParam<LwpCallback> *callback_param) const {
lwpstatus_t *Lsp;
lwpstatus_t *sp;
prheader_t lphp[HEADER_MAX];
prheader_t lhp[HEADER_MAX];
prheader_t *Lphp = lphp;
prheader_t *Lhp = lhp;
lwpsinfo_t *Lpsp;
long nstat;
long ninfo;
int rv = 0;
/*
* The /proc/pid/lstatus file has the array of lwpstatus_t's and the
* /proc/pid/lpsinfo file has the array of lwpsinfo_t's.
*/
if (read_lfile(pid, "lstatus", Lhp) == NULL)
return -1;
if (read_lfile(pid, "lpsinfo", Lphp) == NULL) {
return -1;
}
Lsp = (lwpstatus_t *)(uintptr_t)(Lhp + 1);
Lpsp = (lwpsinfo_t *)(uintptr_t)(Lphp + 1);
for (ninfo = Lphp->pr_nent; ninfo != 0; --ninfo) {
if (Lpsp->pr_sname != 'Z') {
sp = Lsp;
Lsp = (lwpstatus_t *)((uintptr_t)Lsp + Lhp->pr_entsize);
} else {
sp = NULL;
}
if (callback_param &&
!(callback_param->call_back)(sp, callback_param->context))
break;
++rv;
Lpsp = (lwpsinfo_t *)((uintptr_t)Lpsp + Lphp->pr_entsize);
}
return rv;
}
uintptr_t SolarisLwp::GetLwpStackBottom(uintptr_t current_esp) const {
CallbackParam<ModuleCallback> callback_param(AddressNotInModuleCallback,
(void *)current_esp);
ListModules(&callback_param);
return stack_base_address;
}
int SolarisLwp::GetModuleCount() const {
return ListModules(NULL);
}
int SolarisLwp::ListModules(
CallbackParam<ModuleCallback> *callback_param) const {
const char *maps_path = "/proc/self/map";
struct stat status;
int fd = 0, num;
prmap_t map_array[MAP_MAX];
prmap_t *maps = map_array;
size_t size;
if ((fd = open(maps_path, O_RDONLY)) == -1) {
print_message2(2, "failed to open %s in ListModules\n", maps_path);
return -1;
}
AutoCloser autocloser(fd);
if (fstat(fd, &status))
return -1;
/*
* Determine number of mappings, this value must be
* larger than the actual module count
*/
size = status.st_size;
if ((num = (int)(size / sizeof (prmap_t))) > MAP_MAX) {
print_message1(2, "map size overflow\n");
return -1;
}
if (read(fd, (void *)maps, size) < 0) {
print_message2(2, "failed to read %d\n", fd);
return -1;
}
prmap_t *_maps;
int _num;
int module_count = 0;
/*
* Scan each mapping - note it is assummed that the mappings are
* presented in order. We fill holes between mappings. On intel
* the last mapping is usually the data segment of ld.so.1, after
* this comes a red zone into which non-fixed mapping won't get
* place. Thus we can simply bail from the loop after seeing the
* last mapping.
*/
for (_num = 0, _maps = maps; _num < num; ++_num, ++_maps) {
ModuleInfo module;
char *name = _maps->pr_mapname;
memset(&module, 0, sizeof (module));
module.start_addr = _maps->pr_vaddr;
module.size = _maps->pr_size;
if ((strlen(name) > 0) && (strcmp(name, "a.out") != 0)) {
strncpy(module.name, name, sizeof (module.name) - 1);
++module_count;
}
if (callback_param &&
(!callback_param->call_back(module, callback_param->context))) {
break;
}
}
return module_count;
}
} // namespace google_breakpad
|