Update UnusedResources lint suppressions.
[chromium-blink-merge.git] / chromecast / crash / linux / crash_util.cc
blobe80b179c16e35d8f2292fd0892a9303563ac97a9
1 // Copyright 2015 The Chromium 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.
5 #include "chromecast/crash/linux/crash_util.h"
7 #include <stdlib.h>
9 #include "base/bind.h"
10 #include "base/files/file_path.h"
11 #include "base/time/time.h"
12 #include "chromecast/base/path_utils.h"
13 #include "chromecast/base/version.h"
14 #include "chromecast/crash/app_state_tracker.h"
15 #include "chromecast/crash/linux/dummy_minidump_generator.h"
16 #include "chromecast/crash/linux/minidump_writer.h"
18 namespace chromecast {
20 namespace {
22 // This can be set to a callback for testing. This allows us to inject a fake
23 // dumpstate routine to avoid calling an executable during an automated test.
24 // This value should not be mutated through any other function except
25 // CrashUtil::SetDumpStateCbForTest().
26 static base::Callback<int(const std::string&)>* g_dumpstate_cb = nullptr;
28 } // namespace
30 // static
31 uint64_t CrashUtil::GetCurrentTimeMs() {
32 return (base::TimeTicks::Now() - base::TimeTicks()).InMilliseconds();
35 // static
36 bool CrashUtil::RequestUploadCrashDump(
37 const std::string& existing_minidump_path,
38 const std::string& crashed_process_name,
39 uint64_t crashed_process_start_time_ms) {
40 LOG(INFO) << "Request to upload crash dump " << existing_minidump_path
41 << " for process " << crashed_process_name;
43 // Note: Do not use Chromium IO methods in this function. When cast_shell
44 // crashes, this function can be called by any thread, which may not allow IO.
45 // Use stdlib system calls for IO instead.
46 uint64_t uptime_ms = GetCurrentTimeMs() - crashed_process_start_time_ms;
47 MinidumpParams params(crashed_process_name,
48 uptime_ms,
49 "", // suffix
50 AppStateTracker::GetPreviousApp(),
51 AppStateTracker::GetCurrentApp(),
52 AppStateTracker::GetLastLaunchedApp(),
53 CAST_BUILD_RELEASE,
54 CAST_BUILD_INCREMENTAL);
55 DummyMinidumpGenerator minidump_generator(existing_minidump_path);
57 base::FilePath filename = base::FilePath(existing_minidump_path).BaseName();
59 scoped_ptr<MinidumpWriter> writer;
60 if (g_dumpstate_cb) {
61 writer.reset(new MinidumpWriter(
62 &minidump_generator, filename.value(), params, *g_dumpstate_cb));
63 } else {
64 writer.reset(
65 new MinidumpWriter(&minidump_generator, filename.value(), params));
67 bool success = false;
68 writer->set_non_blocking(false);
69 success = (0 == writer->Write()); // error already logged.
71 // In case the file is still in $TEMP, remove it.
72 if (remove(existing_minidump_path.c_str()) < 0 && errno != ENOENT) {
73 LOG(ERROR) << "Unable to delete temp minidump file "
74 << existing_minidump_path << ": " << strerror(errno);
75 success = false;
78 // Use std::endl to flush the log stream in case this process exits.
79 LOG(INFO) << "Request to upload crash dump finished. "
80 << "Exit now if it is main process that crashed." << std::endl;
82 return success;
85 void CrashUtil::SetDumpStateCbForTest(
86 const base::Callback<int(const std::string&)>& cb) {
87 DCHECK(!g_dumpstate_cb);
88 g_dumpstate_cb = new base::Callback<int(const std::string&)>(cb);
91 } // namespace chromecast