Steve Fung | 6c34c25 | 2015-08-20 00:27:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 16 | |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 17 | #ifndef CRASH_REPORTER_KERNEL_COLLECTOR_H_ |
| 18 | #define CRASH_REPORTER_KERNEL_COLLECTOR_H_ |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 19 | |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 20 | #include <pcrecpp.h> |
| 21 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 22 | #include <string> |
| 23 | |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 24 | #include <base/files/file_path.h> |
Ben Chan | 895fa5d | 2014-09-02 20:58:20 -0700 | [diff] [blame] | 25 | #include <base/macros.h> |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 26 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 27 | |
Steve Fung | 129bea5 | 2015-07-23 13:11:15 -0700 | [diff] [blame] | 28 | #include "crash_collector.h" |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 29 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 30 | // Kernel crash collector. |
| 31 | class KernelCollector : public CrashCollector { |
| 32 | public: |
Simon Glass | d74cc09 | 2011-04-06 10:47:01 -0700 | [diff] [blame] | 33 | // Enumeration to specify architecture type. |
| 34 | enum ArchKind { |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 35 | kArchUnknown, |
| 36 | kArchArm, |
| 37 | kArchMips, |
| 38 | kArchX86, |
| 39 | kArchX86_64, |
Simon Glass | d74cc09 | 2011-04-06 10:47:01 -0700 | [diff] [blame] | 40 | |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 41 | kArchCount // Number of architectures. |
Simon Glass | d74cc09 | 2011-04-06 10:47:01 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 44 | KernelCollector(); |
| 45 | |
Ben Chan | efec0b3 | 2014-08-12 08:52:11 -0700 | [diff] [blame] | 46 | ~KernelCollector() override; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 47 | |
Simon Que | 9f90aca | 2013-02-19 17:19:52 -0800 | [diff] [blame] | 48 | void OverridePreservedDumpPath(const base::FilePath &file_path); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 49 | |
| 50 | // Enable collection. |
| 51 | bool Enable(); |
| 52 | |
| 53 | // Returns true if the kernel collection currently enabled. |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 54 | bool is_enabled() const { return is_enabled_; } |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 55 | |
| 56 | // Collect any preserved kernel crash dump. Returns true if there was |
| 57 | // a dump (even if there were problems storing the dump), false otherwise. |
| 58 | bool Collect(); |
| 59 | |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 60 | // Compute a stack signature string from a kernel dump. |
| 61 | bool ComputeKernelStackSignature(const std::string &kernel_dump, |
| 62 | std::string *kernel_signature, |
| 63 | bool print_diagnostics); |
| 64 | |
Simon Glass | d74cc09 | 2011-04-06 10:47:01 -0700 | [diff] [blame] | 65 | // Set the architecture of the crash dumps we are looking at. |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 66 | void set_arch(ArchKind arch) { arch_ = arch; } |
| 67 | ArchKind arch() const { return arch_; } |
Simon Glass | d74cc09 | 2011-04-06 10:47:01 -0700 | [diff] [blame] | 68 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 69 | private: |
| 70 | friend class KernelCollectorTest; |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 71 | FRIEND_TEST(KernelCollectorTest, LoadPreservedDump); |
Doug Anderson | 1e6b8bd | 2011-04-07 09:40:05 -0700 | [diff] [blame] | 72 | FRIEND_TEST(KernelCollectorTest, StripSensitiveDataBasic); |
| 73 | FRIEND_TEST(KernelCollectorTest, StripSensitiveDataBulk); |
| 74 | FRIEND_TEST(KernelCollectorTest, StripSensitiveDataSample); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 75 | FRIEND_TEST(KernelCollectorTest, CollectOK); |
| 76 | |
Mike Frysinger | 6f891c5 | 2014-09-24 15:42:11 -0400 | [diff] [blame] | 77 | virtual bool DumpDirMounted(); |
| 78 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 79 | bool LoadPreservedDump(std::string *contents); |
Doug Anderson | 1e6b8bd | 2011-04-07 09:40:05 -0700 | [diff] [blame] | 80 | void StripSensitiveData(std::string *kernel_dump); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 81 | |
Simon Que | 9f90aca | 2013-02-19 17:19:52 -0800 | [diff] [blame] | 82 | void GetRamoopsRecordPath(base::FilePath *path, size_t record); |
Ben Chan | efec0b3 | 2014-08-12 08:52:11 -0700 | [diff] [blame] | 83 | bool LoadParameters(); |
Sergiu Iordache | 1ea8abe | 2011-08-03 16:11:36 -0700 | [diff] [blame] | 84 | bool HasMoreRecords(); |
| 85 | |
| 86 | // Read a record to string, modified from file_utils since that didn't |
| 87 | // provide a way to restrict the read length. |
| 88 | // Return value indicates (only) error state: |
| 89 | // * false when we get an error (can't read from dump location). |
| 90 | // * true if no error occured. |
| 91 | // Not finding a valid record is not an error state and is signaled by the |
| 92 | // record_found output parameter. |
| 93 | bool ReadRecordToString(std::string *contents, |
Kees Cook | ce9556e | 2011-11-04 20:49:09 +0000 | [diff] [blame] | 94 | size_t current_record, |
Sergiu Iordache | 1ea8abe | 2011-08-03 16:11:36 -0700 | [diff] [blame] | 95 | bool *record_found); |
| 96 | |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 97 | void ProcessStackTrace(pcrecpp::StringPiece kernel_dump, |
| 98 | bool print_diagnostics, |
| 99 | unsigned *hash, |
Luigi Semenzato | f640099 | 2011-12-29 13:18:35 -0800 | [diff] [blame] | 100 | float *last_stack_timestamp, |
| 101 | bool *is_watchdog_crash); |
Ken Mixter | afcf808 | 2010-10-26 14:45:01 -0700 | [diff] [blame] | 102 | bool FindCrashingFunction(pcrecpp::StringPiece kernel_dump, |
| 103 | bool print_diagnostics, |
| 104 | float stack_trace_timestamp, |
| 105 | std::string *crashing_function); |
| 106 | bool FindPanicMessage(pcrecpp::StringPiece kernel_dump, |
| 107 | bool print_diagnostics, |
| 108 | std::string *panic_message); |
| 109 | |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 110 | // Returns the architecture kind for which we are built. |
| 111 | static ArchKind GetCompilerArch(); |
Simon Glass | d74cc09 | 2011-04-06 10:47:01 -0700 | [diff] [blame] | 112 | |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 113 | bool is_enabled_; |
Simon Que | 9f90aca | 2013-02-19 17:19:52 -0800 | [diff] [blame] | 114 | base::FilePath ramoops_dump_path_; |
Kees Cook | ce9556e | 2011-11-04 20:49:09 +0000 | [diff] [blame] | 115 | size_t records_; |
Simon Glass | d74cc09 | 2011-04-06 10:47:01 -0700 | [diff] [blame] | 116 | |
| 117 | // The architecture of kernel dump strings we are working with. |
Ben Chan | 3c6b82c | 2014-07-23 14:52:14 -0700 | [diff] [blame] | 118 | ArchKind arch_; |
| 119 | |
| 120 | DISALLOW_COPY_AND_ASSIGN(KernelCollector); |
Ken Mixter | 0340316 | 2010-08-18 15:23:16 -0700 | [diff] [blame] | 121 | }; |
| 122 | |
Ben Chan | 7e77690 | 2014-06-18 13:19:51 -0700 | [diff] [blame] | 123 | #endif // CRASH_REPORTER_KERNEL_COLLECTOR_H_ |