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 #ifndef CLIENT_LINUX_HANDLER_MINIDUMP_DESCRIPTOR_H_
31 #define CLIENT_LINUX_HANDLER_MINIDUMP_DESCRIPTOR_H_
32 
33 #include <assert.h>
34 #include <sys/types.h>
35 
36 #include <string>
37 
38 #include "common/using_std_string.h"
39 
40 // This class describes how a crash dump should be generated, either:
41 // - Writing a full minidump to a file in a given directory (the actual path,
42 //   inside the directory, is determined by this class).
43 // - Writing a full minidump to a given fd.
44 // - Writing a reduced microdump to the console (logcat on Android).
45 namespace google_breakpad {
46 
47 class MinidumpDescriptor {
48  public:
49   struct MicrodumpOnConsole {};
50   static const MicrodumpOnConsole kMicrodumpOnConsole;
51 
MinidumpDescriptor()52   MinidumpDescriptor() : mode_(kUninitialized),
53                          fd_(-1),
54                          size_limit_(-1) {}
55 
MinidumpDescriptor(const string & directory)56   explicit MinidumpDescriptor(const string& directory)
57       : mode_(kWriteMinidumpToFile),
58         fd_(-1),
59         directory_(directory),
60         c_path_(NULL),
61         size_limit_(-1) {
62     assert(!directory.empty());
63   }
64 
MinidumpDescriptor(int fd)65   explicit MinidumpDescriptor(int fd)
66       : mode_(kWriteMinidumpToFd),
67         fd_(fd),
68         c_path_(NULL),
69         size_limit_(-1) {
70     assert(fd != -1);
71   }
72 
MinidumpDescriptor(const MicrodumpOnConsole &)73   explicit MinidumpDescriptor(const MicrodumpOnConsole&)
74       : mode_(kWriteMicrodumpToConsole),
75         fd_(-1),
76         size_limit_(-1) {}
77 
78   explicit MinidumpDescriptor(const MinidumpDescriptor& descriptor);
79   MinidumpDescriptor& operator=(const MinidumpDescriptor& descriptor);
80 
81   static MinidumpDescriptor getMicrodumpDescriptor();
82 
IsFD()83   bool IsFD() const { return mode_ == kWriteMinidumpToFd; }
84 
fd()85   int fd() const { return fd_; }
86 
directory()87   string directory() const { return directory_; }
88 
path()89   const char* path() const { return c_path_; }
90 
IsMicrodumpOnConsole()91   bool IsMicrodumpOnConsole() const {
92     return mode_ == kWriteMicrodumpToConsole;
93   }
94 
95   // Updates the path so it is unique.
96   // Should be called from a normal context: this methods uses the heap.
97   void UpdatePath();
98 
size_limit()99   off_t size_limit() const { return size_limit_; }
set_size_limit(off_t limit)100   void set_size_limit(off_t limit) { size_limit_ = limit; }
101 
102  private:
103   enum DumpMode {
104     kUninitialized = 0,
105     kWriteMinidumpToFile,
106     kWriteMinidumpToFd,
107     kWriteMicrodumpToConsole
108   };
109 
110   // Specifies the dump mode (see DumpMode).
111   DumpMode mode_;
112 
113   // The file descriptor where the minidump is generated.
114   int fd_;
115 
116   // The directory where the minidump should be generated.
117   string directory_;
118   // The full path to the generated minidump.
119   string path_;
120   // The C string of |path_|. Precomputed so it can be access from a compromised
121   // context.
122   const char* c_path_;
123 
124   off_t size_limit_;
125 };
126 
127 }  // namespace google_breakpad
128 
129 #endif  // CLIENT_LINUX_HANDLER_MINIDUMP_DESCRIPTOR_H_
130