blob: 206ee26ddd27644148ed4d53648c0403750fe92f [file] [log] [blame]
Steve Fung6c34c252015-08-20 00:27:30 -07001/*
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 Mixter03403162010-08-18 15:23:16 -070016
Ben Chan7e776902014-06-18 13:19:51 -070017#ifndef CRASH_REPORTER_KERNEL_COLLECTOR_H_
18#define CRASH_REPORTER_KERNEL_COLLECTOR_H_
Ken Mixter03403162010-08-18 15:23:16 -070019
Ken Mixterafcf8082010-10-26 14:45:01 -070020#include <pcrecpp.h>
21
Ken Mixter03403162010-08-18 15:23:16 -070022#include <string>
23
Ben Chan7e776902014-06-18 13:19:51 -070024#include <base/files/file_path.h>
Ben Chan895fa5d2014-09-02 20:58:20 -070025#include <base/macros.h>
Ben Chan7e776902014-06-18 13:19:51 -070026#include <gtest/gtest_prod.h> // for FRIEND_TEST
27
Steve Fung129bea52015-07-23 13:11:15 -070028#include "crash_collector.h"
Ken Mixter03403162010-08-18 15:23:16 -070029
Ken Mixter03403162010-08-18 15:23:16 -070030// Kernel crash collector.
31class KernelCollector : public CrashCollector {
32 public:
Simon Glassd74cc092011-04-06 10:47:01 -070033 // Enumeration to specify architecture type.
34 enum ArchKind {
Ben Chan3c6b82c2014-07-23 14:52:14 -070035 kArchUnknown,
36 kArchArm,
37 kArchMips,
38 kArchX86,
39 kArchX86_64,
Simon Glassd74cc092011-04-06 10:47:01 -070040
Ben Chan3c6b82c2014-07-23 14:52:14 -070041 kArchCount // Number of architectures.
Simon Glassd74cc092011-04-06 10:47:01 -070042 };
43
Ken Mixter03403162010-08-18 15:23:16 -070044 KernelCollector();
45
Ben Chanefec0b32014-08-12 08:52:11 -070046 ~KernelCollector() override;
Ken Mixter03403162010-08-18 15:23:16 -070047
Simon Que9f90aca2013-02-19 17:19:52 -080048 void OverridePreservedDumpPath(const base::FilePath &file_path);
Ken Mixter03403162010-08-18 15:23:16 -070049
50 // Enable collection.
51 bool Enable();
52
53 // Returns true if the kernel collection currently enabled.
Ben Chan3c6b82c2014-07-23 14:52:14 -070054 bool is_enabled() const { return is_enabled_; }
Ken Mixter03403162010-08-18 15:23:16 -070055
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 Mixterafcf8082010-10-26 14:45:01 -070060 // 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 Glassd74cc092011-04-06 10:47:01 -070065 // Set the architecture of the crash dumps we are looking at.
Ben Chan3c6b82c2014-07-23 14:52:14 -070066 void set_arch(ArchKind arch) { arch_ = arch; }
67 ArchKind arch() const { return arch_; }
Simon Glassd74cc092011-04-06 10:47:01 -070068
Ken Mixter03403162010-08-18 15:23:16 -070069 private:
70 friend class KernelCollectorTest;
Ken Mixter03403162010-08-18 15:23:16 -070071 FRIEND_TEST(KernelCollectorTest, LoadPreservedDump);
Doug Anderson1e6b8bd2011-04-07 09:40:05 -070072 FRIEND_TEST(KernelCollectorTest, StripSensitiveDataBasic);
73 FRIEND_TEST(KernelCollectorTest, StripSensitiveDataBulk);
74 FRIEND_TEST(KernelCollectorTest, StripSensitiveDataSample);
Ken Mixter03403162010-08-18 15:23:16 -070075 FRIEND_TEST(KernelCollectorTest, CollectOK);
76
Mike Frysinger6f891c52014-09-24 15:42:11 -040077 virtual bool DumpDirMounted();
78
Ken Mixter03403162010-08-18 15:23:16 -070079 bool LoadPreservedDump(std::string *contents);
Doug Anderson1e6b8bd2011-04-07 09:40:05 -070080 void StripSensitiveData(std::string *kernel_dump);
Ken Mixter03403162010-08-18 15:23:16 -070081
Simon Que9f90aca2013-02-19 17:19:52 -080082 void GetRamoopsRecordPath(base::FilePath *path, size_t record);
Ben Chanefec0b32014-08-12 08:52:11 -070083 bool LoadParameters();
Sergiu Iordache1ea8abe2011-08-03 16:11:36 -070084 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 Cookce9556e2011-11-04 20:49:09 +000094 size_t current_record,
Sergiu Iordache1ea8abe2011-08-03 16:11:36 -070095 bool *record_found);
96
Ken Mixterafcf8082010-10-26 14:45:01 -070097 void ProcessStackTrace(pcrecpp::StringPiece kernel_dump,
98 bool print_diagnostics,
99 unsigned *hash,
Luigi Semenzatof6400992011-12-29 13:18:35 -0800100 float *last_stack_timestamp,
101 bool *is_watchdog_crash);
Ken Mixterafcf8082010-10-26 14:45:01 -0700102 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 Chan3c6b82c2014-07-23 14:52:14 -0700110 // Returns the architecture kind for which we are built.
111 static ArchKind GetCompilerArch();
Simon Glassd74cc092011-04-06 10:47:01 -0700112
Ken Mixter03403162010-08-18 15:23:16 -0700113 bool is_enabled_;
Simon Que9f90aca2013-02-19 17:19:52 -0800114 base::FilePath ramoops_dump_path_;
Kees Cookce9556e2011-11-04 20:49:09 +0000115 size_t records_;
Simon Glassd74cc092011-04-06 10:47:01 -0700116
117 // The architecture of kernel dump strings we are working with.
Ben Chan3c6b82c2014-07-23 14:52:14 -0700118 ArchKind arch_;
119
120 DISALLOW_COPY_AND_ASSIGN(KernelCollector);
Ken Mixter03403162010-08-18 15:23:16 -0700121};
122
Ben Chan7e776902014-06-18 13:19:51 -0700123#endif // CRASH_REPORTER_KERNEL_COLLECTOR_H_