blob: 6561fa9c6a432333cea6b6c1264f3f2bbb648afa [file] [log] [blame]
Satoru Takabayashie7f6d2a2018-08-08 17:06:29 +09001// Copyright 2018 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CRASH_REPORTER_UTIL_H_
6#define CRASH_REPORTER_UTIL_H_
7
Satoru Takabayashib2ca40d2018-08-09 14:02:04 +09008#include <string>
9#include <vector>
10
11#include <base/files/file_path.h>
Jeffrey Kardatzkee3fb8fb2019-05-13 13:59:12 -070012#include <base/time/time.h>
Jeffrey Kardatzke18d50982019-03-21 14:13:34 -070013#include <brillo/process.h>
Jeffrey Kardatzke437fa922019-05-09 11:34:32 -070014#include <brillo/streams/stream.h>
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +090015#include <session_manager/dbus-proxies.h>
Satoru Takabayashib2ca40d2018-08-09 14:02:04 +090016
Satoru Takabayashie7f6d2a2018-08-08 17:06:29 +090017namespace util {
18
19// Returns true if integration tests are currently running.
20bool IsCrashTestInProgress();
21
Satoru Takabayashi2d728042018-12-10 09:19:00 +090022// Returns true if uploading of device coredumps is allowed.
23bool IsDeviceCoredumpUploadAllowed();
24
Satoru Takabayashie7f6d2a2018-08-08 17:06:29 +090025// Returns true if running on a developer image.
26bool IsDeveloperImage();
27
Satoru Takabayashif6a36802018-08-14 16:23:05 +090028// Returns true if running on a test image.
29bool IsTestImage();
30
Satoru Takabayashi9a587522018-10-29 09:36:27 +090031// Returns true if running on an official image.
32bool IsOfficialImage();
33
Jeffrey Kardatzkee3fb8fb2019-05-13 13:59:12 -070034// Returns the timestamp for the OS version we are currently running. Returns
35// a null (zero-valued) base::Time if it is unable to calculate it for some
36// reason.
37base::Time GetOsTimestamp();
38
39// Returns true if this version is old enough that we do not want to upload the
40// crash reports anymore. This just checks if |timestamp| is more than 180
41// days old. If |timestamp| is null (zero-valued) then this will return false.
42bool IsOsTimestampTooOldForUploads(base::Time timestamp);
43
Jeffrey Kardatzkeea333932019-04-12 10:17:51 -070044// Gets a string describing the hardware class of the device. Returns
45// "undefined" if this cannot be determined.
46std::string GetHardwareClass();
47
48// Returns the boot mode which will either be "dev", "missing-crossystem" (if it
49// cannot be determined) or the empty string.
50std::string GetBootModeString();
51
Satoru Takabayashib2ca40d2018-08-09 14:02:04 +090052// Tries to find |key| in a key-value file named |base_name| in |directories| in
53// the specified order, and writes the value to |value|. This function returns
54// as soon as the key is found (i.e. if the key is found in the first directory,
55// the remaining directories won't be checked). Returns true on success.
56bool GetCachedKeyValue(const base::FilePath& base_name,
57 const std::string& key,
58 const std::vector<base::FilePath>& directories,
59 std::string* value);
60
61// Similar to GetCachedKeyValue(), but this version checks the predefined
62// default directories.
63bool GetCachedKeyValueDefault(const base::FilePath& base_name,
64 const std::string& key,
65 std::string* value);
66
Satoru Takabayashi8ce6db82018-08-17 15:18:41 +090067// Gets the user crash directories via D-Bus using |session_manager_proxy|.
68// Returns true on success. The original contents of |directories| will be lost.
69bool GetUserCrashDirectories(
70 org::chromium::SessionManagerInterfaceProxyInterface* session_manager_proxy,
71 std::vector<base::FilePath>* directories);
72
Jeffrey Kardatzke437fa922019-05-09 11:34:32 -070073// Gzip's the |data| passed in and returns the compressed data. Returns an empty
Ian Barkley-Yeunge87b4f42019-05-22 15:05:13 -070074// vector on failure.
75std::vector<unsigned char> GzipStream(brillo::StreamPtr data);
Jeffrey Kardatzke437fa922019-05-09 11:34:32 -070076
Jeffrey Kardatzke18d50982019-03-21 14:13:34 -070077// Runs |process| and redirects |fd| to |output|. Returns the exit code, or -1
78// if the process failed to start.
79int RunAndCaptureOutput(brillo::ProcessImpl* process,
80 int fd,
81 std::string* output);
82
83// Breaks up |error| using std::getline and then does a LOG(ERROR) of each
84// individual line.
85void LogMultilineError(const std::string& error);
86
Tim Zheng11a665e2019-06-26 17:44:01 -070087// Read the memfd file contents. Return false on failure.
88bool ReadMemfdToString(int mem_fd, std::string* contents);
89
Satoru Takabayashie7f6d2a2018-08-08 17:06:29 +090090} // namespace util
91
92#endif // CRASH_REPORTER_UTIL_H_