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

http://github.com/zpao/v8monkey · C++ · 436 lines · 302 code · 65 blank · 69 comment · 70 complexity · eadf85af8e15bc7658323b50f54e019d MD5 · raw file

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