1 // Copyright 2011 Google LLC 2 // 3 // Redistribution and use in source and binary forms, with or without 4 // modification, are permitted provided that the following conditions are 5 // met: 6 // 7 // * Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following disclaimer. 9 // * Redistributions in binary form must reproduce the above 10 // copyright notice, this list of conditions and the following disclaimer 11 // in the documentation and/or other materials provided with the 12 // distribution. 13 // * Neither the name of Google LLC nor the names of its 14 // contributors may be used to endorse or promote products derived from 15 // this software without specific prior written permission. 16 // 17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29 // crash_generator.h: Define the google_breakpad::CrashGenerator class, 30 // which is used to generate a crash (and a core dump file) for testing. 31 32 #ifndef COMMON_LINUX_TESTS_CRASH_GENERATOR_H_ 33 #define COMMON_LINUX_TESTS_CRASH_GENERATOR_H_ 34 35 #include <sys/resource.h> 36 37 #include <string> 38 39 #include "common/tests/auto_tempdir.h" 40 #include "common/using_std_string.h" 41 42 namespace google_breakpad { 43 44 // A utility class for generating a crash (and a core dump file) for 45 // testing. It creates a child process with the specified number of 46 // threads, which is then termainated by the specified signal. A core 47 // dump file is expected to be created upon the termination of the child 48 // process, which can then be used for testing code that processes core 49 // dump files. 50 class CrashGenerator { 51 public: 52 CrashGenerator(); 53 54 ~CrashGenerator(); 55 56 // Returns true if a core dump file named 'core' will be generated in 57 // the current directory for a test that produces a crash by checking 58 // if /proc/sys/kernel/core_pattern has the default value 'core'. 59 bool HasDefaultCorePattern() const; 60 61 // Returns the expected path of the core dump file. 62 string GetCoreFilePath() const; 63 64 // Returns the directory of a copy of proc files of the child process. 65 string GetDirectoryOfProcFilesCopy() const; 66 67 // Returns whether current resource limits would prevent `CreateChildCrash` 68 // from operating. 69 bool HasResourceLimitsAmenableToCrashCollection() const; 70 71 // Creates a crash (and a core dump file) by creating a child process with 72 // |num_threads| threads, and the terminating the child process by sending 73 // a signal with number |crash_signal| to the |crash_thread|-th thread. 74 // Returns true on success. 75 bool CreateChildCrash(unsigned num_threads, unsigned crash_thread, 76 int crash_signal, pid_t* child_pid); 77 78 // Returns the thread ID of the |index|-th thread in the child process. 79 // This method does not validate |index|. 80 pid_t GetThreadId(unsigned index) const; 81 82 private: 83 // Copies the following proc files of the process with |pid| to the directory 84 // at |path|: auxv, cmdline, environ, maps, status 85 // The directory must have been created. Returns true on success. 86 bool CopyProcFiles(pid_t pid, const char* path) const; 87 88 // Creates |num_threads| threads in the child process. 89 void CreateThreadsInChildProcess(unsigned num_threads); 90 91 // Sets the maximum size of core dump file (both the soft and hard limit) 92 // to |limit| bytes. Returns true on success. 93 bool SetCoreFileSizeLimit(rlim_t limit) const; 94 95 // Creates a shared memory of |memory_size| bytes for communicating thread 96 // IDs between the parent and child process. Returns true on success. 97 bool MapSharedMemory(size_t memory_size); 98 99 // Releases any shared memory created by MapSharedMemory(). Returns true on 100 // success. 101 bool UnmapSharedMemory(); 102 103 // Returns the pointer to the thread ID of the |index|-th thread in the child 104 // process. This method does not validate |index|. 105 pid_t* GetThreadIdPointer(unsigned index); 106 107 // Temporary directory in which a core file is generated. 108 AutoTempDir temp_dir_; 109 110 // Shared memory for communicating thread IDs between the parent and 111 // child process. 112 void* shared_memory_; 113 114 // Number of bytes mapped for |shared_memory_|. 115 size_t shared_memory_size_; 116 }; 117 118 } // namespace google_breakpad 119 120 #endif // COMMON_LINUX_TESTS_CRASH_GENERATOR_H_ 121