/thirdparty/breakpad/client/linux/minidump_writer/linux_dumper.cc

http://github.com/tomahawk-player/tomahawk · C++ · 358 lines · 244 code · 43 blank · 71 comment · 68 complexity · 91c26acc4c337aea9a34f9bb2ce317d3 MD5 · raw file

  1. // Copyright (c) 2010, 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. // linux_dumper.cc: Implement google_breakpad::LinuxDumper.
  30. // See linux_dumper.h for details.
  31. // This code deals with the mechanics of getting information about a crashed
  32. // process. Since this code may run in a compromised address space, the same
  33. // rules apply as detailed at the top of minidump_writer.h: no libc calls and
  34. // use the alternative allocator.
  35. #include "client/linux/minidump_writer/linux_dumper.h"
  36. #include <assert.h>
  37. #include <fcntl.h>
  38. #include <limits.h>
  39. #include <stddef.h>
  40. #include <string.h>
  41. #include "client/linux/minidump_writer/line_reader.h"
  42. #include "common/linux/file_id.h"
  43. #include "common/linux/linux_libc_support.h"
  44. #include "common/linux/memory_mapped_file.h"
  45. #include "common/linux/safe_readlink.h"
  46. #include "third_party/lss/linux_syscall_support.h"
  47. static const char kMappedFileUnsafePrefix[] = "/dev/";
  48. static const char kDeletedSuffix[] = " (deleted)";
  49. inline static bool IsMappedFileOpenUnsafe(
  50. const google_breakpad::MappingInfo& mapping) {
  51. // It is unsafe to attempt to open a mapped file that lives under /dev,
  52. // because the semantics of the open may be driver-specific so we'd risk
  53. // hanging the crash dumper. And a file in /dev/ almost certainly has no
  54. // ELF file identifier anyways.
  55. return my_strncmp(mapping.name,
  56. kMappedFileUnsafePrefix,
  57. sizeof(kMappedFileUnsafePrefix) - 1) == 0;
  58. }
  59. namespace google_breakpad {
  60. LinuxDumper::LinuxDumper(pid_t pid)
  61. : pid_(pid),
  62. crash_address_(0),
  63. crash_signal_(0),
  64. crash_thread_(0),
  65. threads_(&allocator_, 8),
  66. mappings_(&allocator_) {
  67. }
  68. LinuxDumper::~LinuxDumper() {
  69. }
  70. bool LinuxDumper::Init() {
  71. return EnumerateThreads() && EnumerateMappings();
  72. }
  73. bool
  74. LinuxDumper::ElfFileIdentifierForMapping(const MappingInfo& mapping,
  75. bool member,
  76. unsigned int mapping_id,
  77. uint8_t identifier[sizeof(MDGUID)])
  78. {
  79. assert(!member || mapping_id < mappings_.size());
  80. my_memset(identifier, 0, sizeof(MDGUID));
  81. if (IsMappedFileOpenUnsafe(mapping))
  82. return false;
  83. // Special-case linux-gate because it's not a real file.
  84. if (my_strcmp(mapping.name, kLinuxGateLibraryName) == 0) {
  85. const uintptr_t kPageSize = getpagesize();
  86. void* linux_gate = NULL;
  87. if (pid_ == sys_getpid()) {
  88. linux_gate = reinterpret_cast<void*>(mapping.start_addr);
  89. } else {
  90. linux_gate = allocator_.Alloc(kPageSize);
  91. CopyFromProcess(linux_gate, pid_,
  92. reinterpret_cast<const void*>(mapping.start_addr),
  93. kPageSize);
  94. }
  95. return FileID::ElfFileIdentifierFromMappedFile(linux_gate, identifier);
  96. }
  97. char filename[NAME_MAX];
  98. size_t filename_len = my_strlen(mapping.name);
  99. assert(filename_len < NAME_MAX);
  100. if (filename_len >= NAME_MAX)
  101. return false;
  102. memcpy(filename, mapping.name, filename_len);
  103. filename[filename_len] = '\0';
  104. bool filename_modified = HandleDeletedFileInMapping(filename);
  105. MemoryMappedFile mapped_file(filename);
  106. if (!mapped_file.data()) // Should probably check if size >= ElfW(Ehdr)?
  107. return false;
  108. bool success =
  109. FileID::ElfFileIdentifierFromMappedFile(mapped_file.data(), identifier);
  110. if (success && member && filename_modified) {
  111. mappings_[mapping_id]->name[filename_len -
  112. sizeof(kDeletedSuffix) + 1] = '\0';
  113. }
  114. return success;
  115. }
  116. void*
  117. LinuxDumper::FindBeginningOfLinuxGateSharedLibrary(pid_t pid) const {
  118. char auxv_path[NAME_MAX];
  119. if (!BuildProcPath(auxv_path, pid, "auxv"))
  120. return NULL;
  121. // Find the AT_SYSINFO_EHDR entry for linux-gate.so
  122. // See http://www.trilithium.com/johan/2005/08/linux-gate/ for more
  123. // information.
  124. int fd = sys_open(auxv_path, O_RDONLY, 0);
  125. if (fd < 0) {
  126. return NULL;
  127. }
  128. elf_aux_entry one_aux_entry;
  129. while (sys_read(fd,
  130. &one_aux_entry,
  131. sizeof(elf_aux_entry)) == sizeof(elf_aux_entry) &&
  132. one_aux_entry.a_type != AT_NULL) {
  133. if (one_aux_entry.a_type == AT_SYSINFO_EHDR) {
  134. close(fd);
  135. return reinterpret_cast<void*>(one_aux_entry.a_un.a_val);
  136. }
  137. }
  138. close(fd);
  139. return NULL;
  140. }
  141. void*
  142. LinuxDumper::FindEntryPoint(pid_t pid) const {
  143. char auxv_path[NAME_MAX];
  144. if (!BuildProcPath(auxv_path, pid, "auxv"))
  145. return NULL;
  146. int fd = sys_open(auxv_path, O_RDONLY, 0);
  147. if (fd < 0) {
  148. return NULL;
  149. }
  150. // Find the AT_ENTRY entry
  151. elf_aux_entry one_aux_entry;
  152. while (sys_read(fd,
  153. &one_aux_entry,
  154. sizeof(elf_aux_entry)) == sizeof(elf_aux_entry) &&
  155. one_aux_entry.a_type != AT_NULL) {
  156. if (one_aux_entry.a_type == AT_ENTRY) {
  157. close(fd);
  158. return reinterpret_cast<void*>(one_aux_entry.a_un.a_val);
  159. }
  160. }
  161. close(fd);
  162. return NULL;
  163. }
  164. bool LinuxDumper::EnumerateMappings() {
  165. char maps_path[NAME_MAX];
  166. if (!BuildProcPath(maps_path, pid_, "maps"))
  167. return false;
  168. // linux_gate_loc is the beginning of the kernel's mapping of
  169. // linux-gate.so in the process. It doesn't actually show up in the
  170. // maps list as a filename, so we use the aux vector to find it's
  171. // load location and special case it's entry when creating the list
  172. // of mappings.
  173. const void* linux_gate_loc;
  174. linux_gate_loc = FindBeginningOfLinuxGateSharedLibrary(pid_);
  175. // Although the initial executable is usually the first mapping, it's not
  176. // guaranteed (see http://crosbug.com/25355); therefore, try to use the
  177. // actual entry point to find the mapping.
  178. const void* entry_point_loc = FindEntryPoint(pid_);
  179. const int fd = sys_open(maps_path, O_RDONLY, 0);
  180. if (fd < 0)
  181. return false;
  182. LineReader* const line_reader = new(allocator_) LineReader(fd);
  183. const char* line;
  184. unsigned line_len;
  185. while (line_reader->GetNextLine(&line, &line_len)) {
  186. uintptr_t start_addr, end_addr, offset;
  187. const char* i1 = my_read_hex_ptr(&start_addr, line);
  188. if (*i1 == '-') {
  189. const char* i2 = my_read_hex_ptr(&end_addr, i1 + 1);
  190. if (*i2 == ' ') {
  191. const char* i3 = my_read_hex_ptr(&offset, i2 + 6 /* skip ' rwxp ' */);
  192. if (*i3 == ' ') {
  193. const char* name = NULL;
  194. // Only copy name if the name is a valid path name, or if
  195. // it's the VDSO image.
  196. if (((name = my_strchr(line, '/')) == NULL) &&
  197. linux_gate_loc &&
  198. reinterpret_cast<void*>(start_addr) == linux_gate_loc) {
  199. name = kLinuxGateLibraryName;
  200. offset = 0;
  201. }
  202. // Merge adjacent mappings with the same name into one module,
  203. // assuming they're a single library mapped by the dynamic linker
  204. if (name && !mappings_.empty()) {
  205. MappingInfo* module = mappings_.back();
  206. if ((start_addr == module->start_addr + module->size) &&
  207. (my_strlen(name) == my_strlen(module->name)) &&
  208. (my_strncmp(name, module->name, my_strlen(name)) == 0)) {
  209. module->size = end_addr - module->start_addr;
  210. line_reader->PopLine(line_len);
  211. continue;
  212. }
  213. }
  214. MappingInfo* const module = new(allocator_) MappingInfo;
  215. memset(module, 0, sizeof(MappingInfo));
  216. module->start_addr = start_addr;
  217. module->size = end_addr - start_addr;
  218. module->offset = offset;
  219. if (name != NULL) {
  220. const unsigned l = my_strlen(name);
  221. if (l < sizeof(module->name))
  222. memcpy(module->name, name, l);
  223. }
  224. // If this is the entry-point mapping, and it's not already the
  225. // first one, then we need to make it be first. This is because
  226. // the minidump format assumes the first module is the one that
  227. // corresponds to the main executable (as codified in
  228. // processor/minidump.cc:MinidumpModuleList::GetMainModule()).
  229. if (entry_point_loc &&
  230. (entry_point_loc >=
  231. reinterpret_cast<void*>(module->start_addr)) &&
  232. (entry_point_loc <
  233. reinterpret_cast<void*>(module->start_addr+module->size)) &&
  234. !mappings_.empty()) {
  235. // push the module onto the front of the list.
  236. mappings_.resize(mappings_.size() + 1);
  237. for (size_t idx = mappings_.size() - 1; idx > 0; idx--)
  238. mappings_[idx] = mappings_[idx - 1];
  239. mappings_[0] = module;
  240. } else {
  241. mappings_.push_back(module);
  242. }
  243. }
  244. }
  245. }
  246. line_reader->PopLine(line_len);
  247. }
  248. sys_close(fd);
  249. return !mappings_.empty();
  250. }
  251. // Get information about the stack, given the stack pointer. We don't try to
  252. // walk the stack since we might not have all the information needed to do
  253. // unwind. So we just grab, up to, 32k of stack.
  254. bool LinuxDumper::GetStackInfo(const void** stack, size_t* stack_len,
  255. uintptr_t int_stack_pointer) {
  256. // Move the stack pointer to the bottom of the page that it's in.
  257. const uintptr_t page_size = getpagesize();
  258. uint8_t* const stack_pointer =
  259. reinterpret_cast<uint8_t*>(int_stack_pointer & ~(page_size - 1));
  260. // The number of bytes of stack which we try to capture.
  261. static const ptrdiff_t kStackToCapture = 32 * 1024;
  262. const MappingInfo* mapping = FindMapping(stack_pointer);
  263. if (!mapping)
  264. return false;
  265. const ptrdiff_t offset = stack_pointer - (uint8_t*) mapping->start_addr;
  266. const ptrdiff_t distance_to_end =
  267. static_cast<ptrdiff_t>(mapping->size) - offset;
  268. *stack_len = distance_to_end > kStackToCapture ?
  269. kStackToCapture : distance_to_end;
  270. *stack = stack_pointer;
  271. return true;
  272. }
  273. // Find the mapping which the given memory address falls in.
  274. const MappingInfo* LinuxDumper::FindMapping(const void* address) const {
  275. const uintptr_t addr = (uintptr_t) address;
  276. for (size_t i = 0; i < mappings_.size(); ++i) {
  277. const uintptr_t start = static_cast<uintptr_t>(mappings_[i]->start_addr);
  278. if (addr >= start && addr - start < mappings_[i]->size)
  279. return mappings_[i];
  280. }
  281. return NULL;
  282. }
  283. bool LinuxDumper::HandleDeletedFileInMapping(char* path) const {
  284. static const size_t kDeletedSuffixLen = sizeof(kDeletedSuffix) - 1;
  285. // Check for ' (deleted)' in |path|.
  286. // |path| has to be at least as long as "/x (deleted)".
  287. const size_t path_len = my_strlen(path);
  288. if (path_len < kDeletedSuffixLen + 2)
  289. return false;
  290. if (my_strncmp(path + path_len - kDeletedSuffixLen, kDeletedSuffix,
  291. kDeletedSuffixLen) != 0) {
  292. return false;
  293. }
  294. // Check |path| against the /proc/pid/exe 'symlink'.
  295. char exe_link[NAME_MAX];
  296. char new_path[NAME_MAX];
  297. if (!BuildProcPath(exe_link, pid_, "exe"))
  298. return false;
  299. if (!SafeReadLink(exe_link, new_path))
  300. return false;
  301. if (my_strcmp(path, new_path) != 0)
  302. return false;
  303. // Check to see if someone actually named their executable 'foo (deleted)'.
  304. struct kernel_stat exe_stat;
  305. struct kernel_stat new_path_stat;
  306. if (sys_stat(exe_link, &exe_stat) == 0 &&
  307. sys_stat(new_path, &new_path_stat) == 0 &&
  308. exe_stat.st_dev == new_path_stat.st_dev &&
  309. exe_stat.st_ino == new_path_stat.st_ino) {
  310. return false;
  311. }
  312. memcpy(path, exe_link, NAME_MAX);
  313. return true;
  314. }
  315. } // namespace google_breakpad