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
30 // This code writes out minidump files:
31 // http://msdn.microsoft.com/en-us/library/ms680378(VS.85,loband).aspx
32 //
33 // Minidumps are a Microsoft format which Breakpad uses for recording crash
34 // dumps. This code has to run in a compromised environment (the address space
35 // may have received SIGSEGV), thus the following rules apply:
36 // * You may not enter the dynamic linker. This means that we cannot call
37 // any symbols in a shared library (inc libc). Because of this we replace
38 // libc functions in linux_libc_support.h.
39 // * You may not call syscalls via the libc wrappers. This rule is a subset
40 // of the first rule but it bears repeating. We have direct wrappers
41 // around the system calls in linux_syscall_support.h.
42 // * You may not malloc. There's an alternative allocator in memory.h and
43 // a canonical instance in the LinuxDumper object. We use the placement
44 // new form to allocate objects and we don't delete them.
45
46 #include "client/linux/handler/minidump_descriptor.h"
47 #include "client/linux/minidump_writer/minidump_writer.h"
48 #include "client/minidump_file_writer-inl.h"
49
50 #include <ctype.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <link.h>
54 #include <stdio.h>
55 #if defined(__ANDROID__)
56 #include <sys/system_properties.h>
57 #endif
58 #include <sys/types.h>
59 #include <sys/ucontext.h>
60 #include <sys/user.h>
61 #include <sys/utsname.h>
62 #include <time.h>
63 #include <unistd.h>
64
65 #include <algorithm>
66
67 #include "client/linux/dump_writer_common/seccomp_unwinder.h"
68 #include "client/linux/dump_writer_common/thread_info.h"
69 #include "client/linux/dump_writer_common/ucontext_reader.h"
70 #include "client/linux/handler/exception_handler.h"
71 #include "client/linux/minidump_writer/cpu_set.h"
72 #include "client/linux/minidump_writer/line_reader.h"
73 #include "client/linux/minidump_writer/linux_dumper.h"
74 #include "client/linux/minidump_writer/linux_ptrace_dumper.h"
75 #include "client/linux/minidump_writer/proc_cpuinfo_reader.h"
76 #include "client/minidump_file_writer.h"
77 #include "common/linux/linux_libc_support.h"
78 #include "common/minidump_type_helper.h"
79 #include "google_breakpad/common/minidump_format.h"
80 #include "third_party/lss/linux_syscall_support.h"
81
82 namespace {
83
84 using google_breakpad::AppMemoryList;
85 using google_breakpad::ExceptionHandler;
86 using google_breakpad::CpuSet;
87 using google_breakpad::LineReader;
88 using google_breakpad::LinuxDumper;
89 using google_breakpad::LinuxPtraceDumper;
90 using google_breakpad::MDTypeHelper;
91 using google_breakpad::MappingEntry;
92 using google_breakpad::MappingInfo;
93 using google_breakpad::MappingList;
94 using google_breakpad::MinidumpFileWriter;
95 using google_breakpad::PageAllocator;
96 using google_breakpad::ProcCpuInfoReader;
97 using google_breakpad::RawContextCPU;
98 using google_breakpad::SeccompUnwinder;
99 using google_breakpad::ThreadInfo;
100 using google_breakpad::TypedMDRVA;
101 using google_breakpad::UContextReader;
102 using google_breakpad::UntypedMDRVA;
103 using google_breakpad::wasteful_vector;
104
105 typedef MDTypeHelper<sizeof(void*)>::MDRawDebug MDRawDebug;
106 typedef MDTypeHelper<sizeof(void*)>::MDRawLinkMap MDRawLinkMap;
107
108 class MinidumpWriter {
109 public:
110 // The following kLimit* constants are for when minidump_size_limit_ is set
111 // and the minidump size might exceed it.
112 //
113 // Estimate for how big each thread's stack will be (in bytes).
114 static const unsigned kLimitAverageThreadStackLength = 8 * 1024;
115 // Number of threads whose stack size we don't want to limit. These base
116 // threads will simply be the first N threads returned by the dumper (although
117 // the crashing thread will never be limited). Threads beyond this count are
118 // the extra threads.
119 static const unsigned kLimitBaseThreadCount = 20;
120 // Maximum stack size to dump for any extra thread (in bytes).
121 static const unsigned kLimitMaxExtraThreadStackLen = 2 * 1024;
122 // Make sure this number of additional bytes can fit in the minidump
123 // (exclude the stack data).
124 static const unsigned kLimitMinidumpFudgeFactor = 64 * 1024;
125
MinidumpWriter(const char * minidump_path,int minidump_fd,const ExceptionHandler::CrashContext * context,const MappingList & mappings,const AppMemoryList & appmem,LinuxDumper * dumper)126 MinidumpWriter(const char* minidump_path,
127 int minidump_fd,
128 const ExceptionHandler::CrashContext* context,
129 const MappingList& mappings,
130 const AppMemoryList& appmem,
131 LinuxDumper* dumper)
132 : fd_(minidump_fd),
133 path_(minidump_path),
134 ucontext_(context ? &context->context : NULL),
135 #if !defined(__ARM_EABI__) && !defined(__mips__)
136 float_state_(context ? &context->float_state : NULL),
137 #endif
138 dumper_(dumper),
139 minidump_size_limit_(-1),
140 memory_blocks_(dumper_->allocator()),
141 mapping_list_(mappings),
142 app_memory_list_(appmem) {
143 // Assert there should be either a valid fd or a valid path, not both.
144 assert(fd_ != -1 || minidump_path);
145 assert(fd_ == -1 || !minidump_path);
146 }
147
Init()148 bool Init() {
149 if (!dumper_->Init())
150 return false;
151
152 if (fd_ != -1)
153 minidump_writer_.SetFile(fd_);
154 else if (!minidump_writer_.Open(path_))
155 return false;
156
157 return dumper_->ThreadsSuspend();
158 }
159
~MinidumpWriter()160 ~MinidumpWriter() {
161 // Don't close the file descriptor when it's been provided explicitly.
162 // Callers might still need to use it.
163 if (fd_ == -1)
164 minidump_writer_.Close();
165 dumper_->ThreadsResume();
166 }
167
Dump()168 bool Dump() {
169 // A minidump file contains a number of tagged streams. This is the number
170 // of stream which we write.
171 unsigned kNumWriters = 13;
172
173 TypedMDRVA<MDRawHeader> header(&minidump_writer_);
174 TypedMDRVA<MDRawDirectory> dir(&minidump_writer_);
175 if (!header.Allocate())
176 return false;
177 if (!dir.AllocateArray(kNumWriters))
178 return false;
179 my_memset(header.get(), 0, sizeof(MDRawHeader));
180
181 header.get()->signature = MD_HEADER_SIGNATURE;
182 header.get()->version = MD_HEADER_VERSION;
183 header.get()->time_date_stamp = time(NULL);
184 header.get()->stream_count = kNumWriters;
185 header.get()->stream_directory_rva = dir.position();
186
187 unsigned dir_index = 0;
188 MDRawDirectory dirent;
189
190 if (!WriteThreadListStream(&dirent))
191 return false;
192 dir.CopyIndex(dir_index++, &dirent);
193
194 if (!WriteMappings(&dirent))
195 return false;
196 dir.CopyIndex(dir_index++, &dirent);
197
198 if (!WriteAppMemory())
199 return false;
200
201 if (!WriteMemoryListStream(&dirent))
202 return false;
203 dir.CopyIndex(dir_index++, &dirent);
204
205 if (!WriteExceptionStream(&dirent))
206 return false;
207 dir.CopyIndex(dir_index++, &dirent);
208
209 if (!WriteSystemInfoStream(&dirent))
210 return false;
211 dir.CopyIndex(dir_index++, &dirent);
212
213 dirent.stream_type = MD_LINUX_CPU_INFO;
214 if (!WriteFile(&dirent.location, "/proc/cpuinfo"))
215 NullifyDirectoryEntry(&dirent);
216 dir.CopyIndex(dir_index++, &dirent);
217
218 dirent.stream_type = MD_LINUX_PROC_STATUS;
219 if (!WriteProcFile(&dirent.location, GetCrashThread(), "status"))
220 NullifyDirectoryEntry(&dirent);
221 dir.CopyIndex(dir_index++, &dirent);
222
223 dirent.stream_type = MD_LINUX_LSB_RELEASE;
224 if (!WriteFile(&dirent.location, "/etc/lsb-release"))
225 NullifyDirectoryEntry(&dirent);
226 dir.CopyIndex(dir_index++, &dirent);
227
228 dirent.stream_type = MD_LINUX_CMD_LINE;
229 if (!WriteProcFile(&dirent.location, GetCrashThread(), "cmdline"))
230 NullifyDirectoryEntry(&dirent);
231 dir.CopyIndex(dir_index++, &dirent);
232
233 dirent.stream_type = MD_LINUX_ENVIRON;
234 if (!WriteProcFile(&dirent.location, GetCrashThread(), "environ"))
235 NullifyDirectoryEntry(&dirent);
236 dir.CopyIndex(dir_index++, &dirent);
237
238 dirent.stream_type = MD_LINUX_AUXV;
239 if (!WriteProcFile(&dirent.location, GetCrashThread(), "auxv"))
240 NullifyDirectoryEntry(&dirent);
241 dir.CopyIndex(dir_index++, &dirent);
242
243 dirent.stream_type = MD_LINUX_MAPS;
244 if (!WriteProcFile(&dirent.location, GetCrashThread(), "maps"))
245 NullifyDirectoryEntry(&dirent);
246 dir.CopyIndex(dir_index++, &dirent);
247
248 dirent.stream_type = MD_LINUX_DSO_DEBUG;
249 if (!WriteDSODebugStream(&dirent))
250 NullifyDirectoryEntry(&dirent);
251 dir.CopyIndex(dir_index++, &dirent);
252
253 // If you add more directory entries, don't forget to update kNumWriters,
254 // above.
255
256 dumper_->ThreadsResume();
257 return true;
258 }
259
FillThreadStack(MDRawThread * thread,uintptr_t stack_pointer,int max_stack_len,uint8_t ** stack_copy)260 bool FillThreadStack(MDRawThread* thread, uintptr_t stack_pointer,
261 int max_stack_len, uint8_t** stack_copy) {
262 *stack_copy = NULL;
263 const void* stack;
264 size_t stack_len;
265 if (dumper_->GetStackInfo(&stack, &stack_len, stack_pointer)) {
266 UntypedMDRVA memory(&minidump_writer_);
267 if (max_stack_len >= 0 &&
268 stack_len > static_cast<unsigned int>(max_stack_len)) {
269 stack_len = max_stack_len;
270 }
271 if (!memory.Allocate(stack_len))
272 return false;
273 *stack_copy = reinterpret_cast<uint8_t*>(Alloc(stack_len));
274 dumper_->CopyFromProcess(*stack_copy, thread->thread_id, stack,
275 stack_len);
276 memory.Copy(*stack_copy, stack_len);
277 thread->stack.start_of_memory_range =
278 reinterpret_cast<uintptr_t>(stack);
279 thread->stack.memory = memory.location();
280 memory_blocks_.push_back(thread->stack);
281 } else {
282 thread->stack.start_of_memory_range = stack_pointer;
283 thread->stack.memory.data_size = 0;
284 thread->stack.memory.rva = minidump_writer_.position();
285 }
286 return true;
287 }
288
289 // Write information about the threads.
WriteThreadListStream(MDRawDirectory * dirent)290 bool WriteThreadListStream(MDRawDirectory* dirent) {
291 const unsigned num_threads = dumper_->threads().size();
292
293 TypedMDRVA<uint32_t> list(&minidump_writer_);
294 if (!list.AllocateObjectAndArray(num_threads, sizeof(MDRawThread)))
295 return false;
296
297 dirent->stream_type = MD_THREAD_LIST_STREAM;
298 dirent->location = list.location();
299
300 *list.get() = num_threads;
301
302 // If there's a minidump size limit, check if it might be exceeded. Since
303 // most of the space is filled with stack data, just check against that.
304 // If this expects to exceed the limit, set extra_thread_stack_len such
305 // that any thread beyond the first kLimitBaseThreadCount threads will
306 // have only kLimitMaxExtraThreadStackLen bytes dumped.
307 int extra_thread_stack_len = -1; // default to no maximum
308 if (minidump_size_limit_ >= 0) {
309 const unsigned estimated_total_stack_size = num_threads *
310 kLimitAverageThreadStackLength;
311 const off_t estimated_minidump_size = minidump_writer_.position() +
312 estimated_total_stack_size + kLimitMinidumpFudgeFactor;
313 if (estimated_minidump_size > minidump_size_limit_)
314 extra_thread_stack_len = kLimitMaxExtraThreadStackLen;
315 }
316
317 for (unsigned i = 0; i < num_threads; ++i) {
318 MDRawThread thread;
319 my_memset(&thread, 0, sizeof(thread));
320 thread.thread_id = dumper_->threads()[i];
321
322 // We have a different source of information for the crashing thread. If
323 // we used the actual state of the thread we would find it running in the
324 // signal handler with the alternative stack, which would be deeply
325 // unhelpful.
326 if (static_cast<pid_t>(thread.thread_id) == GetCrashThread() &&
327 ucontext_ &&
328 !dumper_->IsPostMortem()) {
329 uint8_t* stack_copy;
330 const uintptr_t stack_ptr = UContextReader::GetStackPointer(ucontext_);
331 if (!FillThreadStack(&thread, stack_ptr, -1, &stack_copy))
332 return false;
333
334 // Copy 256 bytes around crashing instruction pointer to minidump.
335 const size_t kIPMemorySize = 256;
336 uint64_t ip = UContextReader::GetInstructionPointer(ucontext_);
337 // Bound it to the upper and lower bounds of the memory map
338 // it's contained within. If it's not in mapped memory,
339 // don't bother trying to write it.
340 bool ip_is_mapped = false;
341 MDMemoryDescriptor ip_memory_d;
342 for (unsigned j = 0; j < dumper_->mappings().size(); ++j) {
343 const MappingInfo& mapping = *dumper_->mappings()[j];
344 if (ip >= mapping.start_addr &&
345 ip < mapping.start_addr + mapping.size) {
346 ip_is_mapped = true;
347 // Try to get 128 bytes before and after the IP, but
348 // settle for whatever's available.
349 ip_memory_d.start_of_memory_range =
350 std::max(mapping.start_addr,
351 uintptr_t(ip - (kIPMemorySize / 2)));
352 uintptr_t end_of_range =
353 std::min(uintptr_t(ip + (kIPMemorySize / 2)),
354 uintptr_t(mapping.start_addr + mapping.size));
355 ip_memory_d.memory.data_size =
356 end_of_range - ip_memory_d.start_of_memory_range;
357 break;
358 }
359 }
360
361 if (ip_is_mapped) {
362 UntypedMDRVA ip_memory(&minidump_writer_);
363 if (!ip_memory.Allocate(ip_memory_d.memory.data_size))
364 return false;
365 uint8_t* memory_copy =
366 reinterpret_cast<uint8_t*>(Alloc(ip_memory_d.memory.data_size));
367 dumper_->CopyFromProcess(
368 memory_copy,
369 thread.thread_id,
370 reinterpret_cast<void*>(ip_memory_d.start_of_memory_range),
371 ip_memory_d.memory.data_size);
372 ip_memory.Copy(memory_copy, ip_memory_d.memory.data_size);
373 ip_memory_d.memory = ip_memory.location();
374 memory_blocks_.push_back(ip_memory_d);
375 }
376
377 TypedMDRVA<RawContextCPU> cpu(&minidump_writer_);
378 if (!cpu.Allocate())
379 return false;
380 my_memset(cpu.get(), 0, sizeof(RawContextCPU));
381 #if !defined(__ARM_EABI__) && !defined(__mips__)
382 UContextReader::FillCPUContext(cpu.get(), ucontext_, float_state_);
383 #else
384 UContextReader::FillCPUContext(cpu.get(), ucontext_);
385 #endif
386 if (stack_copy)
387 SeccompUnwinder::PopSeccompStackFrame(cpu.get(), thread, stack_copy);
388 thread.thread_context = cpu.location();
389 crashing_thread_context_ = cpu.location();
390 } else {
391 ThreadInfo info;
392 if (!dumper_->GetThreadInfoByIndex(i, &info))
393 return false;
394
395 uint8_t* stack_copy;
396 int max_stack_len = -1; // default to no maximum for this thread
397 if (minidump_size_limit_ >= 0 && i >= kLimitBaseThreadCount)
398 max_stack_len = extra_thread_stack_len;
399 if (!FillThreadStack(&thread, info.stack_pointer, max_stack_len,
400 &stack_copy))
401 return false;
402
403 TypedMDRVA<RawContextCPU> cpu(&minidump_writer_);
404 if (!cpu.Allocate())
405 return false;
406 my_memset(cpu.get(), 0, sizeof(RawContextCPU));
407 info.FillCPUContext(cpu.get());
408 if (stack_copy)
409 SeccompUnwinder::PopSeccompStackFrame(cpu.get(), thread, stack_copy);
410 thread.thread_context = cpu.location();
411 if (dumper_->threads()[i] == GetCrashThread()) {
412 crashing_thread_context_ = cpu.location();
413 if (!dumper_->IsPostMortem()) {
414 // This is the crashing thread of a live process, but
415 // no context was provided, so set the crash address
416 // while the instruction pointer is already here.
417 dumper_->set_crash_address(info.GetInstructionPointer());
418 }
419 }
420 }
421
422 list.CopyIndexAfterObject(i, &thread, sizeof(thread));
423 }
424
425 return true;
426 }
427
428 // Write application-provided memory regions.
WriteAppMemory()429 bool WriteAppMemory() {
430 for (AppMemoryList::const_iterator iter = app_memory_list_.begin();
431 iter != app_memory_list_.end();
432 ++iter) {
433 uint8_t* data_copy =
434 reinterpret_cast<uint8_t*>(dumper_->allocator()->Alloc(iter->length));
435 dumper_->CopyFromProcess(data_copy, GetCrashThread(), iter->ptr,
436 iter->length);
437
438 UntypedMDRVA memory(&minidump_writer_);
439 if (!memory.Allocate(iter->length)) {
440 return false;
441 }
442 memory.Copy(data_copy, iter->length);
443 MDMemoryDescriptor desc;
444 desc.start_of_memory_range = reinterpret_cast<uintptr_t>(iter->ptr);
445 desc.memory = memory.location();
446 memory_blocks_.push_back(desc);
447 }
448
449 return true;
450 }
451
ShouldIncludeMapping(const MappingInfo & mapping)452 static bool ShouldIncludeMapping(const MappingInfo& mapping) {
453 if (mapping.name[0] == 0 || // only want modules with filenames.
454 // Only want to include one mapping per shared lib.
455 // Avoid filtering executable mappings.
456 (mapping.offset != 0 && !mapping.exec) ||
457 mapping.size < 4096) { // too small to get a signature for.
458 return false;
459 }
460
461 return true;
462 }
463
464 // If there is caller-provided information about this mapping
465 // in the mapping_list_ list, return true. Otherwise, return false.
HaveMappingInfo(const MappingInfo & mapping)466 bool HaveMappingInfo(const MappingInfo& mapping) {
467 for (MappingList::const_iterator iter = mapping_list_.begin();
468 iter != mapping_list_.end();
469 ++iter) {
470 // Ignore any mappings that are wholly contained within
471 // mappings in the mapping_info_ list.
472 if (mapping.start_addr >= iter->first.start_addr &&
473 (mapping.start_addr + mapping.size) <=
474 (iter->first.start_addr + iter->first.size)) {
475 return true;
476 }
477 }
478 return false;
479 }
480
481 // Write information about the mappings in effect. Because we are using the
482 // minidump format, the information about the mappings is pretty limited.
483 // Because of this, we also include the full, unparsed, /proc/$x/maps file in
484 // another stream in the file.
WriteMappings(MDRawDirectory * dirent)485 bool WriteMappings(MDRawDirectory* dirent) {
486 const unsigned num_mappings = dumper_->mappings().size();
487 unsigned num_output_mappings = mapping_list_.size();
488
489 for (unsigned i = 0; i < dumper_->mappings().size(); ++i) {
490 const MappingInfo& mapping = *dumper_->mappings()[i];
491 if (ShouldIncludeMapping(mapping) && !HaveMappingInfo(mapping))
492 num_output_mappings++;
493 }
494
495 TypedMDRVA<uint32_t> list(&minidump_writer_);
496 if (num_output_mappings) {
497 if (!list.AllocateObjectAndArray(num_output_mappings, MD_MODULE_SIZE))
498 return false;
499 } else {
500 // Still create the module list stream, although it will have zero
501 // modules.
502 if (!list.Allocate())
503 return false;
504 }
505
506 dirent->stream_type = MD_MODULE_LIST_STREAM;
507 dirent->location = list.location();
508 *list.get() = num_output_mappings;
509
510 // First write all the mappings from the dumper
511 unsigned int j = 0;
512 for (unsigned i = 0; i < num_mappings; ++i) {
513 const MappingInfo& mapping = *dumper_->mappings()[i];
514 if (!ShouldIncludeMapping(mapping) || HaveMappingInfo(mapping))
515 continue;
516
517 MDRawModule mod;
518 if (!FillRawModule(mapping, true, i, mod, NULL))
519 return false;
520 list.CopyIndexAfterObject(j++, &mod, MD_MODULE_SIZE);
521 }
522 // Next write all the mappings provided by the caller
523 for (MappingList::const_iterator iter = mapping_list_.begin();
524 iter != mapping_list_.end();
525 ++iter) {
526 MDRawModule mod;
527 if (!FillRawModule(iter->first, false, 0, mod, iter->second))
528 return false;
529 list.CopyIndexAfterObject(j++, &mod, MD_MODULE_SIZE);
530 }
531
532 return true;
533 }
534
535 // Fill the MDRawModule |mod| with information about the provided
536 // |mapping|. If |identifier| is non-NULL, use it instead of calculating
537 // a file ID from the mapping.
FillRawModule(const MappingInfo & mapping,bool member,unsigned int mapping_id,MDRawModule & mod,const uint8_t * identifier)538 bool FillRawModule(const MappingInfo& mapping,
539 bool member,
540 unsigned int mapping_id,
541 MDRawModule& mod,
542 const uint8_t* identifier) {
543 my_memset(&mod, 0, MD_MODULE_SIZE);
544
545 mod.base_of_image = mapping.start_addr;
546 mod.size_of_image = mapping.size;
547
548 uint8_t cv_buf[MDCVInfoPDB70_minsize + NAME_MAX];
549 uint8_t* cv_ptr = cv_buf;
550
551 const uint32_t cv_signature = MD_CVINFOPDB70_SIGNATURE;
552 my_memcpy(cv_ptr, &cv_signature, sizeof(cv_signature));
553 cv_ptr += sizeof(cv_signature);
554 uint8_t* signature = cv_ptr;
555 cv_ptr += sizeof(MDGUID);
556 if (identifier) {
557 // GUID was provided by caller.
558 my_memcpy(signature, identifier, sizeof(MDGUID));
559 } else {
560 // Note: ElfFileIdentifierForMapping() can manipulate the |mapping.name|.
561 dumper_->ElfFileIdentifierForMapping(mapping, member,
562 mapping_id, signature);
563 }
564 my_memset(cv_ptr, 0, sizeof(uint32_t)); // Set age to 0 on Linux.
565 cv_ptr += sizeof(uint32_t);
566
567 char file_name[NAME_MAX];
568 char file_path[NAME_MAX];
569 LinuxDumper::GetMappingEffectiveNameAndPath(
570 mapping, file_path, sizeof(file_path), file_name, sizeof(file_name));
571
572 const size_t file_name_len = my_strlen(file_name);
573 UntypedMDRVA cv(&minidump_writer_);
574 if (!cv.Allocate(MDCVInfoPDB70_minsize + file_name_len + 1))
575 return false;
576
577 // Write pdb_file_name
578 my_memcpy(cv_ptr, file_name, file_name_len + 1);
579 cv.Copy(cv_buf, MDCVInfoPDB70_minsize + file_name_len + 1);
580
581 mod.cv_record = cv.location();
582
583 MDLocationDescriptor ld;
584 if (!minidump_writer_.WriteString(file_path, my_strlen(file_path), &ld))
585 return false;
586 mod.module_name_rva = ld.rva;
587 return true;
588 }
589
WriteMemoryListStream(MDRawDirectory * dirent)590 bool WriteMemoryListStream(MDRawDirectory* dirent) {
591 TypedMDRVA<uint32_t> list(&minidump_writer_);
592 if (memory_blocks_.size()) {
593 if (!list.AllocateObjectAndArray(memory_blocks_.size(),
594 sizeof(MDMemoryDescriptor)))
595 return false;
596 } else {
597 // Still create the memory list stream, although it will have zero
598 // memory blocks.
599 if (!list.Allocate())
600 return false;
601 }
602
603 dirent->stream_type = MD_MEMORY_LIST_STREAM;
604 dirent->location = list.location();
605
606 *list.get() = memory_blocks_.size();
607
608 for (size_t i = 0; i < memory_blocks_.size(); ++i) {
609 list.CopyIndexAfterObject(i, &memory_blocks_[i],
610 sizeof(MDMemoryDescriptor));
611 }
612 return true;
613 }
614
WriteExceptionStream(MDRawDirectory * dirent)615 bool WriteExceptionStream(MDRawDirectory* dirent) {
616 TypedMDRVA<MDRawExceptionStream> exc(&minidump_writer_);
617 if (!exc.Allocate())
618 return false;
619 my_memset(exc.get(), 0, sizeof(MDRawExceptionStream));
620
621 dirent->stream_type = MD_EXCEPTION_STREAM;
622 dirent->location = exc.location();
623
624 exc.get()->thread_id = GetCrashThread();
625 exc.get()->exception_record.exception_code = dumper_->crash_signal();
626 exc.get()->exception_record.exception_address = dumper_->crash_address();
627 exc.get()->thread_context = crashing_thread_context_;
628
629 return true;
630 }
631
WriteSystemInfoStream(MDRawDirectory * dirent)632 bool WriteSystemInfoStream(MDRawDirectory* dirent) {
633 TypedMDRVA<MDRawSystemInfo> si(&minidump_writer_);
634 if (!si.Allocate())
635 return false;
636 my_memset(si.get(), 0, sizeof(MDRawSystemInfo));
637
638 dirent->stream_type = MD_SYSTEM_INFO_STREAM;
639 dirent->location = si.location();
640
641 WriteCPUInformation(si.get());
642 WriteOSInformation(si.get());
643
644 return true;
645 }
646
WriteDSODebugStream(MDRawDirectory * dirent)647 bool WriteDSODebugStream(MDRawDirectory* dirent) {
648 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr) *>(dumper_->auxv()[AT_PHDR]);
649 char* base;
650 int phnum = dumper_->auxv()[AT_PHNUM];
651 if (!phnum || !phdr)
652 return false;
653
654 // Assume the program base is at the beginning of the same page as the PHDR
655 base = reinterpret_cast<char *>(reinterpret_cast<uintptr_t>(phdr) & ~0xfff);
656
657 // Search for the program PT_DYNAMIC segment
658 ElfW(Addr) dyn_addr = 0;
659 for (; phnum >= 0; phnum--, phdr++) {
660 ElfW(Phdr) ph;
661 if (!dumper_->CopyFromProcess(&ph, GetCrashThread(), phdr, sizeof(ph)))
662 return false;
663
664 // Adjust base address with the virtual address of the PT_LOAD segment
665 // corresponding to offset 0
666 if (ph.p_type == PT_LOAD && ph.p_offset == 0) {
667 base -= ph.p_vaddr;
668 }
669 if (ph.p_type == PT_DYNAMIC) {
670 dyn_addr = ph.p_vaddr;
671 }
672 }
673 if (!dyn_addr)
674 return false;
675
676 ElfW(Dyn) *dynamic = reinterpret_cast<ElfW(Dyn) *>(dyn_addr + base);
677
678 // The dynamic linker makes information available that helps gdb find all
679 // DSOs loaded into the program. If this information is indeed available,
680 // dump it to a MD_LINUX_DSO_DEBUG stream.
681 struct r_debug* r_debug = NULL;
682 uint32_t dynamic_length = 0;
683
684 for (int i = 0; ; ++i) {
685 ElfW(Dyn) dyn;
686 dynamic_length += sizeof(dyn);
687 if (!dumper_->CopyFromProcess(&dyn, GetCrashThread(), dynamic + i,
688 sizeof(dyn))) {
689 return false;
690 }
691
692 if (dyn.d_tag == DT_DEBUG) {
693 r_debug = reinterpret_cast<struct r_debug*>(dyn.d_un.d_ptr);
694 continue;
695 } else if (dyn.d_tag == DT_NULL) {
696 break;
697 }
698 }
699
700 // The "r_map" field of that r_debug struct contains a linked list of all
701 // loaded DSOs.
702 // Our list of DSOs potentially is different from the ones in the crashing
703 // process. So, we have to be careful to never dereference pointers
704 // directly. Instead, we use CopyFromProcess() everywhere.
705 // See <link.h> for a more detailed discussion of the how the dynamic
706 // loader communicates with debuggers.
707
708 // Count the number of loaded DSOs
709 int dso_count = 0;
710 struct r_debug debug_entry;
711 if (!dumper_->CopyFromProcess(&debug_entry, GetCrashThread(), r_debug,
712 sizeof(debug_entry))) {
713 return false;
714 }
715 for (struct link_map* ptr = debug_entry.r_map; ptr; ) {
716 struct link_map map;
717 if (!dumper_->CopyFromProcess(&map, GetCrashThread(), ptr, sizeof(map)))
718 return false;
719
720 ptr = map.l_next;
721 dso_count++;
722 }
723
724 MDRVA linkmap_rva = minidump_writer_.kInvalidMDRVA;
725 if (dso_count > 0) {
726 // If we have at least one DSO, create an array of MDRawLinkMap
727 // entries in the minidump file.
728 TypedMDRVA<MDRawLinkMap> linkmap(&minidump_writer_);
729 if (!linkmap.AllocateArray(dso_count))
730 return false;
731 linkmap_rva = linkmap.location().rva;
732 int idx = 0;
733
734 // Iterate over DSOs and write their information to mini dump
735 for (struct link_map* ptr = debug_entry.r_map; ptr; ) {
736 struct link_map map;
737 if (!dumper_->CopyFromProcess(&map, GetCrashThread(), ptr, sizeof(map)))
738 return false;
739
740 ptr = map.l_next;
741 char filename[257] = { 0 };
742 if (map.l_name) {
743 dumper_->CopyFromProcess(filename, GetCrashThread(), map.l_name,
744 sizeof(filename) - 1);
745 }
746 MDLocationDescriptor location;
747 if (!minidump_writer_.WriteString(filename, 0, &location))
748 return false;
749 MDRawLinkMap entry;
750 entry.name = location.rva;
751 entry.addr = map.l_addr;
752 entry.ld = reinterpret_cast<uintptr_t>(map.l_ld);
753 linkmap.CopyIndex(idx++, &entry);
754 }
755 }
756
757 // Write MD_LINUX_DSO_DEBUG record
758 TypedMDRVA<MDRawDebug> debug(&minidump_writer_);
759 if (!debug.AllocateObjectAndArray(1, dynamic_length))
760 return false;
761 my_memset(debug.get(), 0, sizeof(MDRawDebug));
762 dirent->stream_type = MD_LINUX_DSO_DEBUG;
763 dirent->location = debug.location();
764
765 debug.get()->version = debug_entry.r_version;
766 debug.get()->map = linkmap_rva;
767 debug.get()->dso_count = dso_count;
768 debug.get()->brk = debug_entry.r_brk;
769 debug.get()->ldbase = debug_entry.r_ldbase;
770 debug.get()->dynamic = reinterpret_cast<uintptr_t>(dynamic);
771
772 wasteful_vector<char> dso_debug_data(dumper_->allocator(), dynamic_length);
773 // The passed-in size to the constructor (above) is only a hint.
774 // Must call .resize() to do actual initialization of the elements.
775 dso_debug_data.resize(dynamic_length);
776 dumper_->CopyFromProcess(&dso_debug_data[0], GetCrashThread(), dynamic,
777 dynamic_length);
778 debug.CopyIndexAfterObject(0, &dso_debug_data[0], dynamic_length);
779
780 return true;
781 }
782
set_minidump_size_limit(off_t limit)783 void set_minidump_size_limit(off_t limit) { minidump_size_limit_ = limit; }
784
785 private:
Alloc(unsigned bytes)786 void* Alloc(unsigned bytes) {
787 return dumper_->allocator()->Alloc(bytes);
788 }
789
GetCrashThread() const790 pid_t GetCrashThread() const {
791 return dumper_->crash_thread();
792 }
793
NullifyDirectoryEntry(MDRawDirectory * dirent)794 void NullifyDirectoryEntry(MDRawDirectory* dirent) {
795 dirent->stream_type = 0;
796 dirent->location.data_size = 0;
797 dirent->location.rva = 0;
798 }
799
800 #if defined(__i386__) || defined(__x86_64__) || defined(__mips__)
WriteCPUInformation(MDRawSystemInfo * sys_info)801 bool WriteCPUInformation(MDRawSystemInfo* sys_info) {
802 char vendor_id[sizeof(sys_info->cpu.x86_cpu_info.vendor_id) + 1] = {0};
803 static const char vendor_id_name[] = "vendor_id";
804
805 struct CpuInfoEntry {
806 const char* info_name;
807 int value;
808 bool found;
809 } cpu_info_table[] = {
810 { "processor", -1, false },
811 #if defined(__i386__) || defined(__x86_64__)
812 { "model", 0, false },
813 { "stepping", 0, false },
814 { "cpu family", 0, false },
815 #endif
816 };
817
818 // processor_architecture should always be set, do this first
819 sys_info->processor_architecture =
820 #if defined(__mips__)
821 MD_CPU_ARCHITECTURE_MIPS;
822 #elif defined(__i386__)
823 MD_CPU_ARCHITECTURE_X86;
824 #else
825 MD_CPU_ARCHITECTURE_AMD64;
826 #endif
827
828 const int fd = sys_open("/proc/cpuinfo", O_RDONLY, 0);
829 if (fd < 0)
830 return false;
831
832 {
833 PageAllocator allocator;
834 ProcCpuInfoReader* const reader = new(allocator) ProcCpuInfoReader(fd);
835 const char* field;
836 while (reader->GetNextField(&field)) {
837 for (size_t i = 0;
838 i < sizeof(cpu_info_table) / sizeof(cpu_info_table[0]);
839 i++) {
840 CpuInfoEntry* entry = &cpu_info_table[i];
841 if (i > 0 && entry->found) {
842 // except for the 'processor' field, ignore repeated values.
843 continue;
844 }
845 if (!my_strcmp(field, entry->info_name)) {
846 size_t value_len;
847 const char* value = reader->GetValueAndLen(&value_len);
848 if (value_len == 0)
849 continue;
850
851 uintptr_t val;
852 if (my_read_decimal_ptr(&val, value) == value)
853 continue;
854
855 entry->value = static_cast<int>(val);
856 entry->found = true;
857 }
858 }
859
860 // special case for vendor_id
861 if (!my_strcmp(field, vendor_id_name)) {
862 size_t value_len;
863 const char* value = reader->GetValueAndLen(&value_len);
864 if (value_len > 0)
865 my_strlcpy(vendor_id, value, sizeof(vendor_id));
866 }
867 }
868 sys_close(fd);
869 }
870
871 // make sure we got everything we wanted
872 for (size_t i = 0;
873 i < sizeof(cpu_info_table) / sizeof(cpu_info_table[0]);
874 i++) {
875 if (!cpu_info_table[i].found) {
876 return false;
877 }
878 }
879 // cpu_info_table[0] holds the last cpu id listed in /proc/cpuinfo,
880 // assuming this is the highest id, change it to the number of CPUs
881 // by adding one.
882 cpu_info_table[0].value++;
883
884 sys_info->number_of_processors = cpu_info_table[0].value;
885 #if defined(__i386__) || defined(__x86_64__)
886 sys_info->processor_level = cpu_info_table[3].value;
887 sys_info->processor_revision = cpu_info_table[1].value << 8 |
888 cpu_info_table[2].value;
889 #endif
890
891 if (vendor_id[0] != '\0') {
892 my_memcpy(sys_info->cpu.x86_cpu_info.vendor_id, vendor_id,
893 sizeof(sys_info->cpu.x86_cpu_info.vendor_id));
894 }
895 return true;
896 }
897 #elif defined(__arm__) || defined(__aarch64__)
WriteCPUInformation(MDRawSystemInfo * sys_info)898 bool WriteCPUInformation(MDRawSystemInfo* sys_info) {
899 // The CPUID value is broken up in several entries in /proc/cpuinfo.
900 // This table is used to rebuild it from the entries.
901 const struct CpuIdEntry {
902 const char* field;
903 char format;
904 char bit_lshift;
905 char bit_length;
906 } cpu_id_entries[] = {
907 { "CPU implementer", 'x', 24, 8 },
908 { "CPU variant", 'x', 20, 4 },
909 { "CPU part", 'x', 4, 12 },
910 { "CPU revision", 'd', 0, 4 },
911 };
912
913 // The ELF hwcaps are listed in the "Features" entry as textual tags.
914 // This table is used to rebuild them.
915 const struct CpuFeaturesEntry {
916 const char* tag;
917 uint32_t hwcaps;
918 } cpu_features_entries[] = {
919 #if defined(__arm__)
920 { "swp", MD_CPU_ARM_ELF_HWCAP_SWP },
921 { "half", MD_CPU_ARM_ELF_HWCAP_HALF },
922 { "thumb", MD_CPU_ARM_ELF_HWCAP_THUMB },
923 { "26bit", MD_CPU_ARM_ELF_HWCAP_26BIT },
924 { "fastmult", MD_CPU_ARM_ELF_HWCAP_FAST_MULT },
925 { "fpa", MD_CPU_ARM_ELF_HWCAP_FPA },
926 { "vfp", MD_CPU_ARM_ELF_HWCAP_VFP },
927 { "edsp", MD_CPU_ARM_ELF_HWCAP_EDSP },
928 { "java", MD_CPU_ARM_ELF_HWCAP_JAVA },
929 { "iwmmxt", MD_CPU_ARM_ELF_HWCAP_IWMMXT },
930 { "crunch", MD_CPU_ARM_ELF_HWCAP_CRUNCH },
931 { "thumbee", MD_CPU_ARM_ELF_HWCAP_THUMBEE },
932 { "neon", MD_CPU_ARM_ELF_HWCAP_NEON },
933 { "vfpv3", MD_CPU_ARM_ELF_HWCAP_VFPv3 },
934 { "vfpv3d16", MD_CPU_ARM_ELF_HWCAP_VFPv3D16 },
935 { "tls", MD_CPU_ARM_ELF_HWCAP_TLS },
936 { "vfpv4", MD_CPU_ARM_ELF_HWCAP_VFPv4 },
937 { "idiva", MD_CPU_ARM_ELF_HWCAP_IDIVA },
938 { "idivt", MD_CPU_ARM_ELF_HWCAP_IDIVT },
939 { "idiv", MD_CPU_ARM_ELF_HWCAP_IDIVA | MD_CPU_ARM_ELF_HWCAP_IDIVT },
940 #elif defined(__aarch64__)
941 // No hwcaps on aarch64.
942 #endif
943 };
944
945 // processor_architecture should always be set, do this first
946 sys_info->processor_architecture =
947 #if defined(__aarch64__)
948 MD_CPU_ARCHITECTURE_ARM64;
949 #else
950 MD_CPU_ARCHITECTURE_ARM;
951 #endif
952
953 // /proc/cpuinfo is not readable under various sandboxed environments
954 // (e.g. Android services with the android:isolatedProcess attribute)
955 // prepare for this by setting default values now, which will be
956 // returned when this happens.
957 //
958 // Note: Bogus values are used to distinguish between failures (to
959 // read /sys and /proc files) and really badly configured kernels.
960 sys_info->number_of_processors = 0;
961 sys_info->processor_level = 1U; // There is no ARMv1
962 sys_info->processor_revision = 42;
963 sys_info->cpu.arm_cpu_info.cpuid = 0;
964 sys_info->cpu.arm_cpu_info.elf_hwcaps = 0;
965
966 // Counting the number of CPUs involves parsing two sysfs files,
967 // because the content of /proc/cpuinfo will only mirror the number
968 // of 'online' cores, and thus will vary with time.
969 // See http://www.kernel.org/doc/Documentation/cputopology.txt
970 {
971 CpuSet cpus_present;
972 CpuSet cpus_possible;
973
974 int fd = sys_open("/sys/devices/system/cpu/present", O_RDONLY, 0);
975 if (fd >= 0) {
976 cpus_present.ParseSysFile(fd);
977 sys_close(fd);
978
979 fd = sys_open("/sys/devices/system/cpu/possible", O_RDONLY, 0);
980 if (fd >= 0) {
981 cpus_possible.ParseSysFile(fd);
982 sys_close(fd);
983
984 cpus_present.IntersectWith(cpus_possible);
985 int cpu_count = cpus_present.GetCount();
986 if (cpu_count > 255)
987 cpu_count = 255;
988 sys_info->number_of_processors = static_cast<uint8_t>(cpu_count);
989 }
990 }
991 }
992
993 // Parse /proc/cpuinfo to reconstruct the CPUID value, as well
994 // as the ELF hwcaps field. For the latter, it would be easier to
995 // read /proc/self/auxv but unfortunately, this file is not always
996 // readable from regular Android applications on later versions
997 // (>= 4.1) of the Android platform.
998 const int fd = sys_open("/proc/cpuinfo", O_RDONLY, 0);
999 if (fd < 0) {
1000 // Do not return false here to allow the minidump generation
1001 // to happen properly.
1002 return true;
1003 }
1004
1005 {
1006 PageAllocator allocator;
1007 ProcCpuInfoReader* const reader =
1008 new(allocator) ProcCpuInfoReader(fd);
1009 const char* field;
1010 while (reader->GetNextField(&field)) {
1011 for (size_t i = 0;
1012 i < sizeof(cpu_id_entries)/sizeof(cpu_id_entries[0]);
1013 ++i) {
1014 const CpuIdEntry* entry = &cpu_id_entries[i];
1015 if (my_strcmp(entry->field, field) != 0)
1016 continue;
1017 uintptr_t result = 0;
1018 const char* value = reader->GetValue();
1019 const char* p = value;
1020 if (value[0] == '0' && value[1] == 'x') {
1021 p = my_read_hex_ptr(&result, value+2);
1022 } else if (entry->format == 'x') {
1023 p = my_read_hex_ptr(&result, value);
1024 } else {
1025 p = my_read_decimal_ptr(&result, value);
1026 }
1027 if (p == value)
1028 continue;
1029
1030 result &= (1U << entry->bit_length)-1;
1031 result <<= entry->bit_lshift;
1032 sys_info->cpu.arm_cpu_info.cpuid |=
1033 static_cast<uint32_t>(result);
1034 }
1035 #if defined(__arm__)
1036 // Get the architecture version from the "Processor" field.
1037 // Note that it is also available in the "CPU architecture" field,
1038 // however, some existing kernels are misconfigured and will report
1039 // invalid values here (e.g. 6, while the CPU is ARMv7-A based).
1040 // The "Processor" field doesn't have this issue.
1041 if (!my_strcmp(field, "Processor")) {
1042 size_t value_len;
1043 const char* value = reader->GetValueAndLen(&value_len);
1044 // Expected format: <text> (v<level><endian>)
1045 // Where <text> is some text like "ARMv7 Processor rev 2"
1046 // and <level> is a decimal corresponding to the ARM
1047 // architecture number. <endian> is either 'l' or 'b'
1048 // and corresponds to the endianess, it is ignored here.
1049 while (value_len > 0 && my_isspace(value[value_len-1]))
1050 value_len--;
1051
1052 size_t nn = value_len;
1053 while (nn > 0 && value[nn-1] != '(')
1054 nn--;
1055 if (nn > 0 && value[nn] == 'v') {
1056 uintptr_t arch_level = 5;
1057 my_read_decimal_ptr(&arch_level, value + nn + 1);
1058 sys_info->processor_level = static_cast<uint16_t>(arch_level);
1059 }
1060 }
1061 #elif defined(__aarch64__)
1062 // The aarch64 architecture does not provide the architecture level
1063 // in the Processor field, so we instead check the "CPU architecture"
1064 // field.
1065 if (!my_strcmp(field, "CPU architecture")) {
1066 uintptr_t arch_level = 0;
1067 const char* value = reader->GetValue();
1068 const char* p = value;
1069 p = my_read_decimal_ptr(&arch_level, value);
1070 if (p == value)
1071 continue;
1072 sys_info->processor_level = static_cast<uint16_t>(arch_level);
1073 }
1074 #endif
1075 // Rebuild the ELF hwcaps from the 'Features' field.
1076 if (!my_strcmp(field, "Features")) {
1077 size_t value_len;
1078 const char* value = reader->GetValueAndLen(&value_len);
1079
1080 // Parse each space-separated tag.
1081 while (value_len > 0) {
1082 const char* tag = value;
1083 size_t tag_len = value_len;
1084 const char* p = my_strchr(tag, ' ');
1085 if (p != NULL) {
1086 tag_len = static_cast<size_t>(p - tag);
1087 value += tag_len + 1;
1088 value_len -= tag_len + 1;
1089 } else {
1090 tag_len = strlen(tag);
1091 value_len = 0;
1092 }
1093 for (size_t i = 0;
1094 i < sizeof(cpu_features_entries)/
1095 sizeof(cpu_features_entries[0]);
1096 ++i) {
1097 const CpuFeaturesEntry* entry = &cpu_features_entries[i];
1098 if (tag_len == strlen(entry->tag) &&
1099 !memcmp(tag, entry->tag, tag_len)) {
1100 sys_info->cpu.arm_cpu_info.elf_hwcaps |= entry->hwcaps;
1101 break;
1102 }
1103 }
1104 }
1105 }
1106 }
1107 sys_close(fd);
1108 }
1109
1110 return true;
1111 }
1112 #else
1113 # error "Unsupported CPU"
1114 #endif
1115
WriteFile(MDLocationDescriptor * result,const char * filename)1116 bool WriteFile(MDLocationDescriptor* result, const char* filename) {
1117 const int fd = sys_open(filename, O_RDONLY, 0);
1118 if (fd < 0)
1119 return false;
1120
1121 // We can't stat the files because several of the files that we want to
1122 // read are kernel seqfiles, which always have a length of zero. So we have
1123 // to read as much as we can into a buffer.
1124 static const unsigned kBufSize = 1024 - 2*sizeof(void*);
1125 struct Buffers {
1126 Buffers* next;
1127 size_t len;
1128 uint8_t data[kBufSize];
1129 } *buffers = reinterpret_cast<Buffers*>(Alloc(sizeof(Buffers)));
1130 buffers->next = NULL;
1131 buffers->len = 0;
1132
1133 size_t total = 0;
1134 for (Buffers* bufptr = buffers;;) {
1135 ssize_t r;
1136 do {
1137 r = sys_read(fd, &bufptr->data[bufptr->len], kBufSize - bufptr->len);
1138 } while (r == -1 && errno == EINTR);
1139
1140 if (r < 1)
1141 break;
1142
1143 total += r;
1144 bufptr->len += r;
1145 if (bufptr->len == kBufSize) {
1146 bufptr->next = reinterpret_cast<Buffers*>(Alloc(sizeof(Buffers)));
1147 bufptr = bufptr->next;
1148 bufptr->next = NULL;
1149 bufptr->len = 0;
1150 }
1151 }
1152 sys_close(fd);
1153
1154 if (!total)
1155 return false;
1156
1157 UntypedMDRVA memory(&minidump_writer_);
1158 if (!memory.Allocate(total))
1159 return false;
1160 for (MDRVA pos = memory.position(); buffers; buffers = buffers->next) {
1161 // Check for special case of a zero-length buffer. This should only
1162 // occur if a file's size happens to be a multiple of the buffer's
1163 // size, in which case the final sys_read() will have resulted in
1164 // zero bytes being read after the final buffer was just allocated.
1165 if (buffers->len == 0) {
1166 // This can only occur with final buffer.
1167 assert(buffers->next == NULL);
1168 continue;
1169 }
1170 memory.Copy(pos, &buffers->data, buffers->len);
1171 pos += buffers->len;
1172 }
1173 *result = memory.location();
1174 return true;
1175 }
1176
WriteOSInformation(MDRawSystemInfo * sys_info)1177 bool WriteOSInformation(MDRawSystemInfo* sys_info) {
1178 #if defined(__ANDROID__)
1179 sys_info->platform_id = MD_OS_ANDROID;
1180 #else
1181 sys_info->platform_id = MD_OS_LINUX;
1182 #endif
1183
1184 struct utsname uts;
1185 if (uname(&uts))
1186 return false;
1187
1188 static const size_t buf_len = 512;
1189 char buf[buf_len] = {0};
1190 size_t space_left = buf_len - 1;
1191 const char* info_table[] = {
1192 uts.sysname,
1193 uts.release,
1194 uts.version,
1195 uts.machine,
1196 NULL
1197 };
1198 bool first_item = true;
1199 for (const char** cur_info = info_table; *cur_info; cur_info++) {
1200 static const char separator[] = " ";
1201 size_t separator_len = sizeof(separator) - 1;
1202 size_t info_len = my_strlen(*cur_info);
1203 if (info_len == 0)
1204 continue;
1205
1206 if (space_left < info_len + (first_item ? 0 : separator_len))
1207 break;
1208
1209 if (!first_item) {
1210 my_strlcat(buf, separator, sizeof(buf));
1211 space_left -= separator_len;
1212 }
1213
1214 first_item = false;
1215 my_strlcat(buf, *cur_info, sizeof(buf));
1216 space_left -= info_len;
1217 }
1218
1219 MDLocationDescriptor location;
1220 if (!minidump_writer_.WriteString(buf, 0, &location))
1221 return false;
1222 sys_info->csd_version_rva = location.rva;
1223
1224 return true;
1225 }
1226
WriteProcFile(MDLocationDescriptor * result,pid_t pid,const char * filename)1227 bool WriteProcFile(MDLocationDescriptor* result, pid_t pid,
1228 const char* filename) {
1229 char buf[NAME_MAX];
1230 if (!dumper_->BuildProcPath(buf, pid, filename))
1231 return false;
1232 return WriteFile(result, buf);
1233 }
1234
1235 // Only one of the 2 member variables below should be set to a valid value.
1236 const int fd_; // File descriptor where the minidum should be written.
1237 const char* path_; // Path to the file where the minidum should be written.
1238
1239 const struct ucontext* const ucontext_; // also from the signal handler
1240 #if !defined(__ARM_EABI__) && !defined(__mips__)
1241 const google_breakpad::fpstate_t* const float_state_; // ditto
1242 #endif
1243 LinuxDumper* dumper_;
1244 MinidumpFileWriter minidump_writer_;
1245 off_t minidump_size_limit_;
1246 MDLocationDescriptor crashing_thread_context_;
1247 // Blocks of memory written to the dump. These are all currently
1248 // written while writing the thread list stream, but saved here
1249 // so a memory list stream can be written afterwards.
1250 wasteful_vector<MDMemoryDescriptor> memory_blocks_;
1251 // Additional information about some mappings provided by the caller.
1252 const MappingList& mapping_list_;
1253 // Additional memory regions to be included in the dump,
1254 // provided by the caller.
1255 const AppMemoryList& app_memory_list_;
1256 };
1257
1258
WriteMinidumpImpl(const char * minidump_path,int minidump_fd,off_t minidump_size_limit,pid_t crashing_process,const void * blob,size_t blob_size,const MappingList & mappings,const AppMemoryList & appmem)1259 bool WriteMinidumpImpl(const char* minidump_path,
1260 int minidump_fd,
1261 off_t minidump_size_limit,
1262 pid_t crashing_process,
1263 const void* blob, size_t blob_size,
1264 const MappingList& mappings,
1265 const AppMemoryList& appmem) {
1266 LinuxPtraceDumper dumper(crashing_process);
1267 const ExceptionHandler::CrashContext* context = NULL;
1268 if (blob) {
1269 if (blob_size != sizeof(ExceptionHandler::CrashContext))
1270 return false;
1271 context = reinterpret_cast<const ExceptionHandler::CrashContext*>(blob);
1272 dumper.set_crash_address(
1273 reinterpret_cast<uintptr_t>(context->siginfo.si_addr));
1274 dumper.set_crash_signal(context->siginfo.si_signo);
1275 dumper.set_crash_thread(context->tid);
1276 }
1277 MinidumpWriter writer(minidump_path, minidump_fd, context, mappings,
1278 appmem, &dumper);
1279 // Set desired limit for file size of minidump (-1 means no limit).
1280 writer.set_minidump_size_limit(minidump_size_limit);
1281 if (!writer.Init())
1282 return false;
1283 return writer.Dump();
1284 }
1285
1286 } // namespace
1287
1288 namespace google_breakpad {
1289
WriteMinidump(const char * minidump_path,pid_t crashing_process,const void * blob,size_t blob_size)1290 bool WriteMinidump(const char* minidump_path, pid_t crashing_process,
1291 const void* blob, size_t blob_size) {
1292 return WriteMinidumpImpl(minidump_path, -1, -1,
1293 crashing_process, blob, blob_size,
1294 MappingList(), AppMemoryList());
1295 }
1296
WriteMinidump(int minidump_fd,pid_t crashing_process,const void * blob,size_t blob_size)1297 bool WriteMinidump(int minidump_fd, pid_t crashing_process,
1298 const void* blob, size_t blob_size) {
1299 return WriteMinidumpImpl(NULL, minidump_fd, -1,
1300 crashing_process, blob, blob_size,
1301 MappingList(), AppMemoryList());
1302 }
1303
WriteMinidump(const char * minidump_path,pid_t process,pid_t process_blamed_thread)1304 bool WriteMinidump(const char* minidump_path, pid_t process,
1305 pid_t process_blamed_thread) {
1306 LinuxPtraceDumper dumper(process);
1307 // MinidumpWriter will set crash address
1308 dumper.set_crash_signal(MD_EXCEPTION_CODE_LIN_DUMP_REQUESTED);
1309 dumper.set_crash_thread(process_blamed_thread);
1310 MinidumpWriter writer(minidump_path, -1, NULL, MappingList(),
1311 AppMemoryList(), &dumper);
1312 if (!writer.Init())
1313 return false;
1314 return writer.Dump();
1315 }
1316
WriteMinidump(const char * minidump_path,pid_t crashing_process,const void * blob,size_t blob_size,const MappingList & mappings,const AppMemoryList & appmem)1317 bool WriteMinidump(const char* minidump_path, pid_t crashing_process,
1318 const void* blob, size_t blob_size,
1319 const MappingList& mappings,
1320 const AppMemoryList& appmem) {
1321 return WriteMinidumpImpl(minidump_path, -1, -1, crashing_process,
1322 blob, blob_size,
1323 mappings, appmem);
1324 }
1325
WriteMinidump(int minidump_fd,pid_t crashing_process,const void * blob,size_t blob_size,const MappingList & mappings,const AppMemoryList & appmem)1326 bool WriteMinidump(int minidump_fd, pid_t crashing_process,
1327 const void* blob, size_t blob_size,
1328 const MappingList& mappings,
1329 const AppMemoryList& appmem) {
1330 return WriteMinidumpImpl(NULL, minidump_fd, -1, crashing_process,
1331 blob, blob_size,
1332 mappings, appmem);
1333 }
1334
WriteMinidump(const char * minidump_path,off_t minidump_size_limit,pid_t crashing_process,const void * blob,size_t blob_size,const MappingList & mappings,const AppMemoryList & appmem)1335 bool WriteMinidump(const char* minidump_path, off_t minidump_size_limit,
1336 pid_t crashing_process,
1337 const void* blob, size_t blob_size,
1338 const MappingList& mappings,
1339 const AppMemoryList& appmem) {
1340 return WriteMinidumpImpl(minidump_path, -1, minidump_size_limit,
1341 crashing_process, blob, blob_size,
1342 mappings, appmem);
1343 }
1344
WriteMinidump(int minidump_fd,off_t minidump_size_limit,pid_t crashing_process,const void * blob,size_t blob_size,const MappingList & mappings,const AppMemoryList & appmem)1345 bool WriteMinidump(int minidump_fd, off_t minidump_size_limit,
1346 pid_t crashing_process,
1347 const void* blob, size_t blob_size,
1348 const MappingList& mappings,
1349 const AppMemoryList& appmem) {
1350 return WriteMinidumpImpl(NULL, minidump_fd, minidump_size_limit,
1351 crashing_process, blob, blob_size,
1352 mappings, appmem);
1353 }
1354
WriteMinidump(const char * filename,const MappingList & mappings,const AppMemoryList & appmem,LinuxDumper * dumper)1355 bool WriteMinidump(const char* filename,
1356 const MappingList& mappings,
1357 const AppMemoryList& appmem,
1358 LinuxDumper* dumper) {
1359 MinidumpWriter writer(filename, -1, NULL, mappings, appmem, dumper);
1360 if (!writer.Init())
1361 return false;
1362 return writer.Dump();
1363 }
1364
1365 } // namespace google_breakpad
1366