blob: 66003cfa41d8c8993ea7a39979b5f7ea247ca3fe [file] [log] [blame]
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/system_logs/debug_daemon_log_source.h"
#include <stddef.h>
#include <utility>
#include "base/bind.h"
#include "base/callback_helpers.h"
#include "base/containers/contains.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/task/post_task.h"
#include "base/task/thread_pool.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/common/chrome_switches.h"
#include "chromeos/cryptohome/cryptohome_parameters.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/debug_daemon/debug_daemon_client.h"
#include "components/feedback/feedback_util.h"
#include "components/user_manager/user.h"
#include "components/user_manager/user_manager.h"
#include "content/public/browser/browser_thread.h"
namespace system_logs {
namespace {
constexpr char kNotAvailable[] = "<not available>";
constexpr char kRoutesKeyName[] = "routes";
constexpr char kRoutesv6KeyName[] = "routes6";
constexpr char kLogTruncated[] = "<earlier logs truncated>\n";
// List of user log files that Chrome reads directly as these logs are generated
// by Chrome itself.
constexpr struct UserLogs {
// A string key used as a title for this log in feedback reports.
const char* log_key;
// The log file's path relative to the user's profile directory.
const char* log_file_relative_path;
} kUserLogs[] = {
{"chrome_user_log", "log/chrome"},
{"chrome_user_log.PREVIOUS", "log/chrome.PREVIOUS"},
{"libassistant_user_log", "google-assistant-library/log/libassistant.log"},
{"login-times", "login-times"},
{"logout-times", "logout-times"},
};
// List of debugd entries to exclude from the results.
constexpr std::array<const char*, 2> kExcludeList = {
// Shill device and service properties are retrieved by ShillLogSource.
// TODO(https://crbug.com/967800): Modify debugd to omit these for
// feedback report gathering and remove these entries.
"network-devices",
"network-services",
};
// Buffer size for user logs in bytes. Given that maximum feedback report size
// is ~7M and that majority of log files are under 1M, we set a per-file limit
// of 1MiB.
const int64_t kMaxLogSize = 1024 * 1024;
} // namespace
// Reads the contents of the user log files listed in |kUserLogs| and adds them
// to the |response| parameter.
void ReadUserLogFiles(const std::vector<base::FilePath>& profile_dirs,
SystemLogsResponse* response) {
for (size_t i = 0; i < profile_dirs.size(); ++i) {
std::string profile_prefix = "Profile[" + base::NumberToString(i) + "] ";
for (const auto& log : kUserLogs) {
std::string value;
const bool read_success = feedback_util::ReadEndOfFile(
profile_dirs[i].Append(log.log_file_relative_path), kMaxLogSize,
&value);
if (read_success && value.length() == kMaxLogSize) {
value.replace(0, strlen(kLogTruncated), kLogTruncated);
LOG(WARNING) << "Large log file was likely truncated: "
<< log.log_file_relative_path;
}
response->emplace(
profile_prefix + log.log_key,
(read_success && !value.empty()) ? std::move(value) : kNotAvailable);
}
}
}
DebugDaemonLogSource::DebugDaemonLogSource(bool scrub)
: SystemLogsSource("DebugDemon"),
response_(new SystemLogsResponse()),
num_pending_requests_(0),
scrub_(scrub) {}
DebugDaemonLogSource::~DebugDaemonLogSource() {}
void DebugDaemonLogSource::Fetch(SysLogsSourceCallback callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK(!callback.is_null());
DCHECK(callback_.is_null());
callback_ = std::move(callback);
chromeos::DebugDaemonClient* client =
chromeos::DBusThreadManager::Get()->GetDebugDaemonClient();
client->GetRoutes(true, // Numeric
false, // No IPv6
base::BindOnce(&DebugDaemonLogSource::OnGetRoutes,
weak_ptr_factory_.GetWeakPtr(), false));
++num_pending_requests_;
client->GetRoutes(true, // Numeric
true, // with IPv6
base::BindOnce(&DebugDaemonLogSource::OnGetRoutes,
weak_ptr_factory_.GetWeakPtr(), true));
++num_pending_requests_;
if (scrub_) {
const user_manager::User* user =
user_manager::UserManager::Get()->GetActiveUser();
client->GetScrubbedBigLogs(
cryptohome::CreateAccountIdentifierFromAccountId(
user ? user->GetAccountId() : EmptyAccountId()),
base::BindOnce(&DebugDaemonLogSource::OnGetLogs,
weak_ptr_factory_.GetWeakPtr()));
} else {
client->GetAllLogs(base::BindOnce(&DebugDaemonLogSource::OnGetLogs,
weak_ptr_factory_.GetWeakPtr()));
}
++num_pending_requests_;
}
void DebugDaemonLogSource::OnGetRoutes(
bool is_ipv6,
absl::optional<std::vector<std::string>> routes) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
std::string key = is_ipv6 ? kRoutesv6KeyName : kRoutesKeyName;
(*response_)[key] = routes.has_value()
? base::JoinString(routes.value(), "\n")
: kNotAvailable;
RequestCompleted();
}
void DebugDaemonLogSource::OnGetOneLog(std::string key,
absl::optional<std::string> status) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
(*response_)[std::move(key)] = std::move(status).value_or(kNotAvailable);
RequestCompleted();
}
void DebugDaemonLogSource::OnGetLogs(bool /* succeeded */,
const KeyValueMap& logs) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
// We ignore 'succeeded' for this callback - we want to display as much of the
// debug info as we can even if we failed partway through parsing, and if we
// couldn't fetch any of it, none of the fields will even appear.
for (const auto& log : logs) {
if (base::Contains(kExcludeList, log.first))
continue;
response_->insert(log);
}
RequestCompleted();
}
void DebugDaemonLogSource::GetLoggedInUsersLogFiles() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
// List all logged-in users' profile directories.
std::vector<base::FilePath> profile_dirs;
const user_manager::UserList& users =
user_manager::UserManager::Get()->GetLoggedInUsers();
for (const auto* user : users) {
if (user->username_hash().empty())
continue;
profile_dirs.emplace_back(
ash::ProfileHelper::GetProfilePathByUserIdHash(user->username_hash()));
}
auto response = std::make_unique<SystemLogsResponse>();
SystemLogsResponse* response_ptr = response.get();
base::ThreadPool::PostTaskAndReply(
FROM_HERE, {base::MayBlock(), base::TaskPriority::BEST_EFFORT},
base::BindOnce(&ReadUserLogFiles, profile_dirs, response_ptr),
base::BindOnce(&DebugDaemonLogSource::MergeUserLogFilesResponse,
weak_ptr_factory_.GetWeakPtr(), std::move(response)));
}
void DebugDaemonLogSource::MergeUserLogFilesResponse(
std::unique_ptr<SystemLogsResponse> response) {
for (auto& pair : *response)
response_->emplace(pair.first, std::move(pair.second));
auto response_to_return = std::make_unique<SystemLogsResponse>();
std::swap(response_to_return, response_);
DCHECK(!callback_.is_null());
std::move(callback_).Run(std::move(response_to_return));
}
void DebugDaemonLogSource::RequestCompleted() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK(!callback_.is_null());
--num_pending_requests_;
if (num_pending_requests_ > 0)
return;
// When all other logs are collected, fetch the user logs, because any errors
// fetching the other logs is reported in the user logs.
GetLoggedInUsersLogFiles();
}
} // namespace system_logs