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  */
16 
17 #ifndef CRASH_REPORTER_KERNEL_COLLECTOR_H_
18 #define CRASH_REPORTER_KERNEL_COLLECTOR_H_
19 
20 #include <pcrecpp.h>
21 
22 #include <string>
23 
24 #include <base/files/file_path.h>
25 #include <base/macros.h>
26 #include <gtest/gtest_prod.h>  // for FRIEND_TEST
27 
28 #include "crash_collector.h"
29 
30 // Kernel crash collector.
31 class KernelCollector : public CrashCollector {
32  public:
33   // Enumeration to specify architecture type.
34   enum ArchKind {
35     kArchUnknown,
36     kArchArm,
37     kArchMips,
38     kArchX86,
39     kArchX86_64,
40 
41     kArchCount  // Number of architectures.
42   };
43 
44   KernelCollector();
45 
46   ~KernelCollector() override;
47 
48   void OverridePreservedDumpPath(const base::FilePath &file_path);
49 
50   // Enable collection.
51   bool Enable();
52 
53   // Returns true if the kernel collection currently enabled.
is_enabled()54   bool is_enabled() const { return is_enabled_; }
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 
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 
65   // Set the architecture of the crash dumps we are looking at.
set_arch(ArchKind arch)66   void set_arch(ArchKind arch) { arch_ = arch; }
arch()67   ArchKind arch() const { return arch_; }
68 
69  private:
70   friend class KernelCollectorTest;
71   FRIEND_TEST(KernelCollectorTest, LoadPreservedDump);
72   FRIEND_TEST(KernelCollectorTest, StripSensitiveDataBasic);
73   FRIEND_TEST(KernelCollectorTest, StripSensitiveDataBulk);
74   FRIEND_TEST(KernelCollectorTest, StripSensitiveDataSample);
75   FRIEND_TEST(KernelCollectorTest, CollectOK);
76 
77   virtual bool DumpDirMounted();
78 
79   bool LoadPreservedDump(std::string *contents);
80   void StripSensitiveData(std::string *kernel_dump);
81 
82   void GetRamoopsRecordPath(base::FilePath *path, size_t record);
83   bool LoadParameters();
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,
94                           size_t current_record,
95                           bool *record_found);
96 
97   void ProcessStackTrace(pcrecpp::StringPiece kernel_dump,
98                          bool print_diagnostics,
99                          unsigned *hash,
100                          float *last_stack_timestamp,
101                          bool *is_watchdog_crash);
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 
110   // Returns the architecture kind for which we are built.
111   static ArchKind GetCompilerArch();
112 
113   bool is_enabled_;
114   base::FilePath ramoops_dump_path_;
115   size_t records_;
116 
117   // The architecture of kernel dump strings we are working with.
118   ArchKind arch_;
119 
120   DISALLOW_COPY_AND_ASSIGN(KernelCollector);
121 };
122 
123 #endif  // CRASH_REPORTER_KERNEL_COLLECTOR_H_
124