File: debug_daemon_log_source.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (326 lines) | stat: -rw-r--r-- 12,411 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright 2012 The Chromium Authors
// 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 "ash/constants/ash_features.h"
#include "base/containers/contains.h"
#include "base/feature_list.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/logging.h"
#include "base/memory/weak_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/task/thread_pool.h"
#include "chrome/browser/ash/os_feedback/chrome_os_feedback_delegate.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/common/chrome_switches.h"
#include "chromeos/ash/components/cryptohome/cryptohome_parameters.h"
#include "chromeos/ash/components/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 kEmpty[] = "<empty>";
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;

  // If true, |log_file_relative_path| is a pattern for which files to match.
  // This works like shell globbing. If there are multiple matches files, it
  // chooses the newest file, with the latest modified time.
  bool pattern = false;
} kUserLogs[] = {
    {"chrome_user_log", "log/chrome"},
    {"chrome_user_log.PREVIOUS", "log/chrome_????????-??????", true},
    {"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.
    "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;

std::vector<debugd::FeedbackLogType> GetLogTypesForUser(
    const user_manager::User* user) {
  // The default list of log types that we request from debugd.
  std::vector<debugd::FeedbackLogType> included_log_types = {
      debugd::FeedbackLogType::ARC_BUG_REPORT,
      debugd::FeedbackLogType::CONNECTIVITY_REPORT,
      debugd::FeedbackLogType::VERBOSE_COMMAND_LOGS,
      debugd::FeedbackLogType::COMMAND_LOGS,
      debugd::FeedbackLogType::FEEDBACK_LOGS,
      debugd::FeedbackLogType::BLUETOOTH_BQR,
      debugd::FeedbackLogType::LSB_RELEASE_INFO,
      debugd::FeedbackLogType::PERF_DATA,
      debugd::FeedbackLogType::OS_RELEASE_INFO,
      debugd::FeedbackLogType::VAR_LOG_FILES};
  if (user && ash::ChromeOsFeedbackDelegate::IsWifiDebugLogsAllowed(
                  user->GetProfilePrefs())) {
    // Include WIFI_FIRMWARE_DUMPS since it is allowed for the user.
    included_log_types.push_back(debugd::FeedbackLogType::WIFI_FIRMWARE_DUMPS);
  }

  return included_log_types;
}

}  // namespace

std::string ReadUserLogFile(const base::FilePath& log_file_path) {
  std::optional<std::string> maybe_value =
      feedback_util::ReadEndOfFile(log_file_path, kMaxLogSize);

  if (maybe_value.has_value() && maybe_value.value().size() == kMaxLogSize) {
    maybe_value.value().replace(0, strlen(kLogTruncated), kLogTruncated);

    LOG(WARNING) << "Large log file was likely truncated: " << log_file_path;
  }
  return (maybe_value.has_value() && !maybe_value.value().empty())
             ? maybe_value.value()
             : std::string(kNotAvailable);
}

std::string ReadUserLogFilePattern(
    const base::FilePath& log_file_path_pattern) {
  base::FilePath log_file_dir = log_file_path_pattern.DirName();
  base::FileEnumerator file_enumerator(
      log_file_dir, /*recursive=*/false, base::FileEnumerator::FILES,
      log_file_path_pattern.BaseName().value());

  base::Time newest_file_mtime;
  base::FilePath newest_file_path;
  for (base::FilePath path = file_enumerator.Next(); !path.empty();
       path = file_enumerator.Next()) {
    const base::FileEnumerator::FileInfo info = file_enumerator.GetInfo();
    if (newest_file_mtime.is_null() ||
        info.GetLastModifiedTime() >= newest_file_mtime) {
      newest_file_mtime = info.GetLastModifiedTime();
      newest_file_path = path;
    }
  }

  return newest_file_mtime.is_null() ? std::string(kNotAvailable)
                                     : ReadUserLogFile(newest_file_path);
}

// 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) + "] ";
    const base::FilePath profile_dir = profile_dirs[i];
    for (const auto& log : kUserLogs) {
      const base::FilePath log_file_path_or_pattern =
          profile_dir.AppendASCII(log.log_file_relative_path);
      const std::string content =
          log.pattern ? ReadUserLogFilePattern(log_file_path_or_pattern)
                      : ReadUserLogFile(log_file_path_or_pattern);
      response->emplace(profile_prefix + log.log_key, content);
    }
  }
}

DebugDaemonLogSource::DebugDaemonLogSource(bool scrub)
    : SystemLogsSource("DebugDemon"),
      response_(new SystemLogsResponse()),
      num_pending_requests_(0),
      scrub_(scrub) {}

DebugDaemonLogSource::~DebugDaemonLogSource() = default;

void DebugDaemonLogSource::Fetch(SysLogsSourceCallback callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(!callback.is_null());
  DCHECK(callback_.is_null());

  callback_ = std::move(callback);
  ash::DebugDaemonClient* client = ash::DebugDaemonClient::Get();

  client->GetRoutes(true,   // Numeric
                    false,  // No IPv6
                    false,  // All tables option disabled
                    base::BindOnce(&DebugDaemonLogSource::OnGetRoutes,
                                   weak_ptr_factory_.GetWeakPtr(), false));
  ++num_pending_requests_;
  client->GetRoutes(true,   // Numeric
                    true,   // with IPv6
                    false,  // All tables option disabled
                    base::BindOnce(&DebugDaemonLogSource::OnGetRoutes,
                                   weak_ptr_factory_.GetWeakPtr(), true));
  ++num_pending_requests_;

  const auto start_time = base::TimeTicks::Now();
  if (scrub_) {
    const user_manager::User* user =
        user_manager::UserManager::Get()->GetActiveUser();
    const auto account_identifier =
        cryptohome::CreateAccountIdentifierFromAccountId(
            user ? user->GetAccountId() : EmptyAccountId());

    client->GetFeedbackLogs(
        account_identifier, GetLogTypesForUser(user),
        base::BindOnce(&DebugDaemonLogSource::OnGetLogs,
                       weak_ptr_factory_.GetWeakPtr(), start_time));

  } else {
    client->GetAllLogs(base::BindOnce(&DebugDaemonLogSource::OnGetLogs,
                                      weak_ptr_factory_.GetWeakPtr(),
                                      start_time));
  }
  ++num_pending_requests_;
}

void DebugDaemonLogSource::OnGetRoutes(
    bool is_ipv6,
    std::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,
                                       std::optional<std::string> status) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  (*response_)[std::move(key)] = std::move(status).value_or(kNotAvailable);
  RequestCompleted();
}

void DebugDaemonLogSource::OnGetLogs(const base::TimeTicks get_start_time,
                                     bool succeeded,
                                     const KeyValueMap& logs) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  // We are interested in the performance of gathering the logs for feedback
  // reports only where the logs will always be scrubbed. GetBigFeedbackLogs is
  // the dbus method used.
  if (scrub_) {
    base::UmaHistogramBoolean("Feedback.ChromeOSApp.GetBigFeedbackLogs.Success",
                              succeeded);
    base::UmaHistogramMediumTimes(
        "Feedback.ChromeOSApp.Duration.GetBigFeedbackLogs",
        base::TimeTicks::Now() - get_start_time);
  }
  int empty_log_count = 0;
  int not_available_log_count = 0;
  int other_log_count = 0;

  // 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);
    if (log.second == kEmpty) {
      ++empty_log_count;
    } else if (log.second == kNotAvailable) {
      ++not_available_log_count;
    } else {
      ++other_log_count;
    }
  }
  if (scrub_) {
    // Record stats for the logs received from debugd.
    // As of today, the total logs are about 211.
    base::UmaHistogramCounts1000(
        "Feedback.ChromeOSApp.GetBigFeedbackLogs.EmptyCount", empty_log_count);
    base::UmaHistogramCounts1000(
        "Feedback.ChromeOSApp.GetBigFeedbackLogs.NotAvailableCount",
        not_available_log_count);
    base::UmaHistogramCounts1000(
        "Feedback.ChromeOSApp.GetBigFeedbackLogs.OtherCount", other_log_count);
  }
  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 user_manager::User* 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