toolkit/crashreporter/google-breakpad/src/client/solaris/handler/solaris_lwp.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright (c) 2007, Google Inc.
michael@0 2 // All rights reserved.
michael@0 3 //
michael@0 4 // Redistribution and use in source and binary forms, with or without
michael@0 5 // modification, are permitted provided that the following conditions are
michael@0 6 // met:
michael@0 7 //
michael@0 8 // * Redistributions of source code must retain the above copyright
michael@0 9 // notice, this list of conditions and the following disclaimer.
michael@0 10 // * Redistributions in binary form must reproduce the above
michael@0 11 // copyright notice, this list of conditions and the following disclaimer
michael@0 12 // in the documentation and/or other materials provided with the
michael@0 13 // distribution.
michael@0 14 // * Neither the name of Google Inc. nor the names of its
michael@0 15 // contributors may be used to endorse or promote products derived from
michael@0 16 // this software without specific prior written permission.
michael@0 17 //
michael@0 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 29
michael@0 30 // Author: Alfred Peng
michael@0 31
michael@0 32 #include <dirent.h>
michael@0 33 #include <elf.h>
michael@0 34 #include <errno.h>
michael@0 35 #include <fcntl.h>
michael@0 36 #include <limits.h>
michael@0 37 #include <sys/frame.h>
michael@0 38 #include <sys/stat.h>
michael@0 39 #include <sys/types.h>
michael@0 40 #include <sys/wait.h>
michael@0 41 #include <unistd.h>
michael@0 42
michael@0 43 #include <algorithm>
michael@0 44 #include <cassert>
michael@0 45 #include <cstdio>
michael@0 46 #include <cstdlib>
michael@0 47 #include <functional>
michael@0 48
michael@0 49 #include "client/solaris/handler/solaris_lwp.h"
michael@0 50 #include "common/solaris/message_output.h"
michael@0 51
michael@0 52 using namespace google_breakpad;
michael@0 53
michael@0 54 // This unamed namespace contains helper function.
michael@0 55 namespace {
michael@0 56
michael@0 57 uintptr_t stack_base_address = 0;
michael@0 58 static const int HEADER_MAX = 2000;
michael@0 59 static const int MAP_MAX = 1000;
michael@0 60
michael@0 61 // Context information for the callbacks when validating address by listing
michael@0 62 // modules.
michael@0 63 struct AddressValidatingContext {
michael@0 64 uintptr_t address;
michael@0 65 bool is_mapped;
michael@0 66
michael@0 67 AddressValidatingContext() : address(0UL), is_mapped(false) {
michael@0 68 }
michael@0 69 };
michael@0 70
michael@0 71 // Convert from string to int.
michael@0 72 static bool LocalAtoi(char *s, int *r) {
michael@0 73 assert(s != NULL);
michael@0 74 assert(r != NULL);
michael@0 75 char *endptr = NULL;
michael@0 76 int ret = strtol(s, &endptr, 10);
michael@0 77 if (endptr == s)
michael@0 78 return false;
michael@0 79 *r = ret;
michael@0 80 return true;
michael@0 81 }
michael@0 82
michael@0 83 // Callback invoked for each mapped module.
michael@0 84 // It uses the module's adderss range to validate the address.
michael@0 85 static bool AddressNotInModuleCallback(const ModuleInfo &module_info,
michael@0 86 void *context) {
michael@0 87 AddressValidatingContext *addr =
michael@0 88 reinterpret_cast<AddressValidatingContext *>(context);
michael@0 89 if (addr->is_mapped = ((module_info.start_addr > 0) &&
michael@0 90 (addr->address >= module_info.start_addr) &&
michael@0 91 (addr->address <= module_info.start_addr +
michael@0 92 module_info.size))) {
michael@0 93 stack_base_address = module_info.start_addr + module_info.size;
michael@0 94 }
michael@0 95
michael@0 96 return !addr->is_mapped;
michael@0 97 }
michael@0 98
michael@0 99 static int IterateLwpAll(int pid,
michael@0 100 CallbackParam<LwpidCallback> *callback_param) {
michael@0 101 char lwp_path[40];
michael@0 102 DIR *dir;
michael@0 103 int count = 0;
michael@0 104
michael@0 105 snprintf(lwp_path, sizeof (lwp_path), "/proc/%d/lwp", (int)pid);
michael@0 106 if ((dir = opendir(lwp_path)) == NULL)
michael@0 107 return -1;
michael@0 108
michael@0 109 struct dirent *entry = NULL;
michael@0 110 while ((entry = readdir(dir)) != NULL) {
michael@0 111 if ((strcmp(entry->d_name, ".") != 0) &&
michael@0 112 (strcmp(entry->d_name, "..") != 0)) {
michael@0 113 int lwpid = 0;
michael@0 114 int last_pid = 0;
michael@0 115 if (LocalAtoi(entry->d_name, &lwpid) && last_pid != lwpid) {
michael@0 116 last_pid = lwpid;
michael@0 117 ++count;
michael@0 118 if (callback_param &&
michael@0 119 !(callback_param->call_back)(lwpid, callback_param->context)) {
michael@0 120 break;
michael@0 121 }
michael@0 122 }
michael@0 123 }
michael@0 124 }
michael@0 125
michael@0 126 closedir(dir);
michael@0 127 return count;
michael@0 128 }
michael@0 129
michael@0 130 #if defined(__i386) && !defined(NO_FRAME_POINTER)
michael@0 131 void *GetNextFrame(void **last_ebp) {
michael@0 132 void *sp = *last_ebp;
michael@0 133 if ((unsigned long)sp == (unsigned long)last_ebp)
michael@0 134 return NULL;
michael@0 135 if ((unsigned long)sp & (sizeof(void *) - 1))
michael@0 136 return NULL;
michael@0 137 if ((unsigned long)sp - (unsigned long)last_ebp > 100000)
michael@0 138 return NULL;
michael@0 139 return sp;
michael@0 140 }
michael@0 141 #elif defined(__sparc)
michael@0 142 void *GetNextFrame(void *last_ebp) {
michael@0 143 return reinterpret_cast<struct frame *>(last_ebp)->fr_savfp;
michael@0 144 }
michael@0 145 #else
michael@0 146 void *GetNextFrame(void **last_ebp) {
michael@0 147 return reinterpret_cast<void*>(last_ebp);
michael@0 148 }
michael@0 149 #endif
michael@0 150
michael@0 151
michael@0 152 class AutoCloser {
michael@0 153 public:
michael@0 154 AutoCloser(int fd) : fd_(fd) {}
michael@0 155 ~AutoCloser() { if (fd_) close(fd_); }
michael@0 156 private:
michael@0 157 int fd_;
michael@0 158 };
michael@0 159
michael@0 160 // Control the execution of the lwp.
michael@0 161 // Suspend/Resume lwp based on the value of context.
michael@0 162 static bool ControlLwp(int lwpid, void *context) {
michael@0 163 // The current thread is the one to handle the crash. Ignore it.
michael@0 164 if (lwpid != pthread_self()) {
michael@0 165 int ctlfd;
michael@0 166 char procname[PATH_MAX];
michael@0 167 bool suspend = *(bool *)context;
michael@0 168
michael@0 169 // Open the /proc/$pid/lwp/$lwpid/lwpctl files
michael@0 170 snprintf(procname, sizeof (procname), "/proc/self/lwp/%d/lwpctl", lwpid);
michael@0 171
michael@0 172 if ((ctlfd = open(procname, O_WRONLY|O_EXCL)) < 0) {
michael@0 173 print_message2(2, "failed to open %s in ControlLwp\n", procname);
michael@0 174 return false;
michael@0 175 }
michael@0 176
michael@0 177 AutoCloser autocloser(ctlfd);
michael@0 178
michael@0 179 long ctl[2];
michael@0 180 ctl[0] = suspend ? PCSTOP : PCRUN;
michael@0 181 ctl[1] = 0;
michael@0 182 if (write(ctlfd, ctl, sizeof (ctl)) != sizeof (ctl)) {
michael@0 183 print_message2(2, "failed in lwp %d\n", lwpid);
michael@0 184 return false;
michael@0 185 }
michael@0 186 }
michael@0 187
michael@0 188 return true;
michael@0 189 }
michael@0 190
michael@0 191 /*
michael@0 192 * Utility function to read the contents of a file that contains a
michael@0 193 * prheader_t at the start (/proc/$pid/lstatus or /proc/$pid/lpsinfo).
michael@0 194 * Return true on success.
michael@0 195 */
michael@0 196 static bool read_lfile(int pid, const char *lname, prheader_t *lhp) {
michael@0 197 char lpath[PATH_MAX];
michael@0 198 struct stat statb;
michael@0 199 int fd;
michael@0 200 size_t size;
michael@0 201
michael@0 202 snprintf(lpath, sizeof (lpath), "/proc/%d/%s", pid, lname);
michael@0 203 if ((fd = open(lpath, O_RDONLY)) < 0) {
michael@0 204 print_message2(2, "failed to open %s in read_lfile\n", lpath);
michael@0 205 return false;
michael@0 206 }
michael@0 207
michael@0 208 AutoCloser autocloser(fd);
michael@0 209
michael@0 210 if (fstat(fd, &statb) != 0)
michael@0 211 return false;
michael@0 212
michael@0 213 size = statb.st_size;
michael@0 214 if ((size / sizeof (prheader_t)) + 32 > HEADER_MAX) {
michael@0 215 print_message1(2, "map size overflow\n");
michael@0 216 return false;
michael@0 217 }
michael@0 218
michael@0 219 if (pread(fd, lhp, size, 0) <= sizeof (prheader_t))
michael@0 220 return false;
michael@0 221
michael@0 222 return true;
michael@0 223 }
michael@0 224
michael@0 225 } // namespace
michael@0 226
michael@0 227 namespace google_breakpad {
michael@0 228
michael@0 229 SolarisLwp::SolarisLwp(int pid) : pid_(pid) {
michael@0 230 }
michael@0 231
michael@0 232 SolarisLwp::~SolarisLwp() {
michael@0 233 }
michael@0 234
michael@0 235 int SolarisLwp::ControlAllLwps(bool suspend) {
michael@0 236 CallbackParam<LwpidCallback> callback_param(ControlLwp, &suspend);
michael@0 237 return IterateLwpAll(pid_, &callback_param);
michael@0 238 }
michael@0 239
michael@0 240 int SolarisLwp::GetLwpCount() const {
michael@0 241 return IterateLwpAll(pid_, NULL);
michael@0 242 }
michael@0 243
michael@0 244 int SolarisLwp::Lwp_iter_all(int pid,
michael@0 245 CallbackParam<LwpCallback> *callback_param) const {
michael@0 246 lwpstatus_t *Lsp;
michael@0 247 lwpstatus_t *sp;
michael@0 248 prheader_t lphp[HEADER_MAX];
michael@0 249 prheader_t lhp[HEADER_MAX];
michael@0 250 prheader_t *Lphp = lphp;
michael@0 251 prheader_t *Lhp = lhp;
michael@0 252 lwpsinfo_t *Lpsp;
michael@0 253 long nstat;
michael@0 254 long ninfo;
michael@0 255 int rv = 0;
michael@0 256
michael@0 257 /*
michael@0 258 * The /proc/pid/lstatus file has the array of lwpstatus_t's and the
michael@0 259 * /proc/pid/lpsinfo file has the array of lwpsinfo_t's.
michael@0 260 */
michael@0 261 if (read_lfile(pid, "lstatus", Lhp) == NULL)
michael@0 262 return -1;
michael@0 263 if (read_lfile(pid, "lpsinfo", Lphp) == NULL) {
michael@0 264 return -1;
michael@0 265 }
michael@0 266
michael@0 267 Lsp = (lwpstatus_t *)(uintptr_t)(Lhp + 1);
michael@0 268 Lpsp = (lwpsinfo_t *)(uintptr_t)(Lphp + 1);
michael@0 269
michael@0 270 for (ninfo = Lphp->pr_nent; ninfo != 0; --ninfo) {
michael@0 271 if (Lpsp->pr_sname != 'Z') {
michael@0 272 sp = Lsp;
michael@0 273 Lsp = (lwpstatus_t *)((uintptr_t)Lsp + Lhp->pr_entsize);
michael@0 274 } else {
michael@0 275 sp = NULL;
michael@0 276 }
michael@0 277 if (callback_param &&
michael@0 278 !(callback_param->call_back)(sp, callback_param->context))
michael@0 279 break;
michael@0 280 ++rv;
michael@0 281 Lpsp = (lwpsinfo_t *)((uintptr_t)Lpsp + Lphp->pr_entsize);
michael@0 282 }
michael@0 283
michael@0 284 return rv;
michael@0 285 }
michael@0 286
michael@0 287 uintptr_t SolarisLwp::GetLwpStackBottom(uintptr_t current_esp) const {
michael@0 288 AddressValidatingContext addr;
michael@0 289 addr.address = current_esp;
michael@0 290 CallbackParam<ModuleCallback> callback_param(AddressNotInModuleCallback,
michael@0 291 &addr);
michael@0 292 ListModules(&callback_param);
michael@0 293 return stack_base_address;
michael@0 294 }
michael@0 295
michael@0 296 int SolarisLwp::GetModuleCount() const {
michael@0 297 return ListModules(NULL);
michael@0 298 }
michael@0 299
michael@0 300 int SolarisLwp::ListModules(
michael@0 301 CallbackParam<ModuleCallback> *callback_param) const {
michael@0 302 const char *maps_path = "/proc/self/map";
michael@0 303 struct stat status;
michael@0 304 int fd = 0, num;
michael@0 305 prmap_t map_array[MAP_MAX];
michael@0 306 prmap_t *maps = map_array;
michael@0 307 size_t size;
michael@0 308
michael@0 309 if ((fd = open(maps_path, O_RDONLY)) == -1) {
michael@0 310 print_message2(2, "failed to open %s in ListModules\n", maps_path);
michael@0 311 return -1;
michael@0 312 }
michael@0 313
michael@0 314 AutoCloser autocloser(fd);
michael@0 315
michael@0 316 if (fstat(fd, &status))
michael@0 317 return -1;
michael@0 318
michael@0 319 /*
michael@0 320 * Determine number of mappings, this value must be
michael@0 321 * larger than the actual module count
michael@0 322 */
michael@0 323 size = status.st_size;
michael@0 324 if ((num = (int)(size / sizeof (prmap_t))) > MAP_MAX) {
michael@0 325 print_message1(2, "map size overflow\n");
michael@0 326 return -1;
michael@0 327 }
michael@0 328
michael@0 329 if (read(fd, (void *)maps, size) < 0) {
michael@0 330 print_message2(2, "failed to read %d\n", fd);
michael@0 331 return -1;
michael@0 332 }
michael@0 333
michael@0 334 prmap_t *_maps;
michael@0 335 int _num;
michael@0 336 int module_count = 0;
michael@0 337
michael@0 338 /*
michael@0 339 * Scan each mapping - note it is assummed that the mappings are
michael@0 340 * presented in order. We fill holes between mappings. On intel
michael@0 341 * the last mapping is usually the data segment of ld.so.1, after
michael@0 342 * this comes a red zone into which non-fixed mapping won't get
michael@0 343 * place. Thus we can simply bail from the loop after seeing the
michael@0 344 * last mapping.
michael@0 345 */
michael@0 346 for (_num = 0, _maps = maps; _num < num; ++_num, ++_maps) {
michael@0 347 ModuleInfo module;
michael@0 348 char *name = _maps->pr_mapname;
michael@0 349
michael@0 350 memset(&module, 0, sizeof (module));
michael@0 351 module.start_addr = _maps->pr_vaddr;
michael@0 352 module.size = _maps->pr_size;
michael@0 353 if (strlen(name) > 0) {
michael@0 354 int objectfd = 0;
michael@0 355 char path[PATH_MAX];
michael@0 356 char buf[SELFMAG];
michael@0 357
michael@0 358 snprintf(path, sizeof (path), "/proc/self/object/%s", name);
michael@0 359 if ((objectfd = open(path, O_RDONLY)) < 0) {
michael@0 360 print_message1(2, "can't open module file\n");
michael@0 361 continue;
michael@0 362 }
michael@0 363
michael@0 364 AutoCloser autocloser(objectfd);
michael@0 365
michael@0 366 if (read(objectfd, buf, SELFMAG) != SELFMAG) {
michael@0 367 print_message1(2, "can't read module file\n");
michael@0 368 continue;
michael@0 369 }
michael@0 370 if (buf[0] != ELFMAG0 || buf[1] != ELFMAG1 ||
michael@0 371 buf[2] != ELFMAG2 || buf[3] != ELFMAG3) {
michael@0 372 continue;
michael@0 373 }
michael@0 374
michael@0 375 strncpy(module.name, name, sizeof (module.name) - 1);
michael@0 376 ++module_count;
michael@0 377 }
michael@0 378 if (callback_param &&
michael@0 379 (!callback_param->call_back(module, callback_param->context))) {
michael@0 380 break;
michael@0 381 }
michael@0 382 }
michael@0 383
michael@0 384 return module_count;
michael@0 385 }
michael@0 386
michael@0 387 // Check if the address is a valid virtual address.
michael@0 388 // If the address is in any of the mapped modules, we take it as valid.
michael@0 389 // Otherwise it is invalid.
michael@0 390 bool SolarisLwp::IsAddressMapped(uintptr_t address) const {
michael@0 391 AddressValidatingContext addr;
michael@0 392 addr.address = address;
michael@0 393 CallbackParam<ModuleCallback> callback_param(AddressNotInModuleCallback,
michael@0 394 &addr);
michael@0 395 ListModules(&callback_param);
michael@0 396 return addr.is_mapped;
michael@0 397 }
michael@0 398
michael@0 399 // We're looking for a ucontext_t as the second parameter
michael@0 400 // to a signal handler function call. Luckily, the ucontext_t
michael@0 401 // has an ebp(fp on SPARC) member which should match the ebp(fp)
michael@0 402 // pointed to by the ebp(fp) of the signal handler frame.
michael@0 403 // The Solaris stack looks like this:
michael@0 404 // http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/libproc/common/Pstack.c#81
michael@0 405 bool SolarisLwp::FindSigContext(uintptr_t sighandler_ebp,
michael@0 406 ucontext_t **sig_ctx) {
michael@0 407 uintptr_t previous_ebp;
michael@0 408 uintptr_t sig_ebp;
michael@0 409 const int MAX_STACK_DEPTH = 50;
michael@0 410 int depth_counter = 0;
michael@0 411
michael@0 412 do {
michael@0 413 #if TARGET_CPU_SPARC
michael@0 414 previous_ebp = reinterpret_cast<uintptr_t>(GetNextFrame(
michael@0 415 reinterpret_cast<void*>(sighandler_ebp)));
michael@0 416 *sig_ctx = reinterpret_cast<ucontext_t*>(sighandler_ebp + sizeof (struct frame));
michael@0 417 uintptr_t sig_esp = (*sig_ctx)->uc_mcontext.gregs[REG_O6];
michael@0 418 if (sig_esp < previous_ebp && sig_esp > sighandler_ebp)
michael@0 419 sig_ebp = (uintptr_t)(((struct frame *)sig_esp)->fr_savfp);
michael@0 420
michael@0 421 #elif TARGET_CPU_X86
michael@0 422 previous_ebp = reinterpret_cast<uintptr_t>(GetNextFrame(
michael@0 423 reinterpret_cast<void**>(sighandler_ebp)));
michael@0 424 *sig_ctx = reinterpret_cast<ucontext_t*>(sighandler_ebp + sizeof (struct frame) +
michael@0 425 3 * sizeof(uintptr_t));
michael@0 426 sig_ebp = (*sig_ctx)->uc_mcontext.gregs[EBP];
michael@0 427 #endif
michael@0 428 sighandler_ebp = previous_ebp;
michael@0 429 depth_counter++;
michael@0 430 } while(previous_ebp != sig_ebp && sighandler_ebp != 0 &&
michael@0 431 IsAddressMapped(sighandler_ebp) && depth_counter < MAX_STACK_DEPTH);
michael@0 432
michael@0 433 return previous_ebp == sig_ebp && previous_ebp != 0;
michael@0 434 }
michael@0 435
michael@0 436 } // namespace google_breakpad

mercurial