1 // Copyright (c) 2012 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 #include <stdio.h>
31 
32 #include "linux/handler/guid_generator.h"
33 #include "linux/handler/minidump_descriptor.h"
34 
35 namespace google_breakpad {
36 
37 //static
38 const MinidumpDescriptor::MicrodumpOnConsole
39     MinidumpDescriptor::kMicrodumpOnConsole = {};
40 
MinidumpDescriptor(const MinidumpDescriptor & descriptor)41 MinidumpDescriptor::MinidumpDescriptor(const MinidumpDescriptor& descriptor)
42     : mode_(descriptor.mode_),
43       fd_(descriptor.fd_),
44       directory_(descriptor.directory_),
45       c_path_(NULL),
46       size_limit_(descriptor.size_limit_),
47       address_within_principal_mapping_(
48           descriptor.address_within_principal_mapping_),
49       skip_dump_if_principal_mapping_not_referenced_(
50           descriptor.skip_dump_if_principal_mapping_not_referenced_),
51       sanitize_stacks_(descriptor.sanitize_stacks_),
52       microdump_extra_info_(descriptor.microdump_extra_info_) {
53   // The copy constructor is not allowed to be called on a MinidumpDescriptor
54   // with a valid path_, as getting its c_path_ would require the heap which
55   // can cause problems in compromised environments.
56   assert(descriptor.path_.empty());
57 }
58 
operator =(const MinidumpDescriptor & descriptor)59 MinidumpDescriptor& MinidumpDescriptor::operator=(
60     const MinidumpDescriptor& descriptor) {
61   assert(descriptor.path_.empty());
62 
63   mode_ = descriptor.mode_;
64   fd_ = descriptor.fd_;
65   directory_ = descriptor.directory_;
66   path_.clear();
67   if (c_path_) {
68     // This descriptor already had a path set, so generate a new one.
69     c_path_ = NULL;
70     UpdatePath();
71   }
72   size_limit_ = descriptor.size_limit_;
73   address_within_principal_mapping_ =
74       descriptor.address_within_principal_mapping_;
75   skip_dump_if_principal_mapping_not_referenced_ =
76       descriptor.skip_dump_if_principal_mapping_not_referenced_;
77   sanitize_stacks_ = descriptor.sanitize_stacks_;
78   microdump_extra_info_ = descriptor.microdump_extra_info_;
79   return *this;
80 }
81 
UpdatePath()82 void MinidumpDescriptor::UpdatePath() {
83   assert(mode_ == kWriteMinidumpToFile && !directory_.empty());
84 
85   GUID guid;
86   char guid_str[kGUIDStringLength + 1];
87   if (!CreateGUID(&guid) || !GUIDToString(&guid, guid_str, sizeof(guid_str))) {
88     assert(false);
89   }
90 
91   path_.clear();
92   path_ = directory_ + "/" + guid_str + ".dmp";
93   c_path_ = path_.c_str();
94 }
95 
96 }  // namespace google_breakpad
97