summaryrefslogtreecommitdiff
path: root/chromium/chromecast/crash/linux/crash_util.cc
blob: 396fd84481e8d9e6bec6f6d06c552d312fc672cc (plain)
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
// Copyright 2015 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 "chromecast/crash/linux/crash_util.h"

#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/threading/thread_restrictions.h"
#include "base/time/time.h"
#include "chromecast/base/path_utils.h"
#include "chromecast/base/version.h"
#include "chromecast/crash/app_state_tracker.h"
#include "chromecast/crash/linux/dummy_minidump_generator.h"
#include "chromecast/crash/linux/minidump_writer.h"

namespace chromecast {

namespace {

// This can be set to a callback for testing. This allows us to inject a fake
// dumpstate routine to avoid calling an executable during an automated test.
// This value should not be mutated through any other function except
// CrashUtil::SetDumpStateCbForTest().
static base::Callback<int(const std::string&)>* g_dumpstate_cb = nullptr;

}  // namespace

// static
uint64_t CrashUtil::GetCurrentTimeMs() {
  return (base::TimeTicks::Now() - base::TimeTicks()).InMilliseconds();
}

// static
bool CrashUtil::RequestUploadCrashDump(
    const std::string& existing_minidump_path,
    const std::string& crashed_process_name,
    uint64_t crashed_process_start_time_ms) {
  // Remove IO restrictions from this thread. Chromium IO functions must be used
  // to access the file system and upload information to the crash server.
  const bool io_allowed = base::ThreadRestrictions::SetIOAllowed(true);

  LOG(INFO) << "Request to upload crash dump " << existing_minidump_path
            << " for process " << crashed_process_name;

  uint64_t uptime_ms = GetCurrentTimeMs() - crashed_process_start_time_ms;
  MinidumpParams params(crashed_process_name,
                        uptime_ms,
                        "",  // suffix
                        AppStateTracker::GetPreviousApp(),
                        AppStateTracker::GetCurrentApp(),
                        AppStateTracker::GetLastLaunchedApp(),
                        CAST_BUILD_RELEASE,
                        CAST_BUILD_INCREMENTAL,
                        ""  /* reason */);
  DummyMinidumpGenerator minidump_generator(existing_minidump_path);

  base::FilePath filename = base::FilePath(existing_minidump_path).BaseName();

  std::unique_ptr<MinidumpWriter> writer;
  if (g_dumpstate_cb) {
    writer.reset(new MinidumpWriter(
        &minidump_generator, filename.value(), params, *g_dumpstate_cb));
  } else {
    writer.reset(
        new MinidumpWriter(&minidump_generator, filename.value(), params));
  }
  bool success = false;
  writer->set_non_blocking(false);
  success = (0 == writer->Write());  // error already logged.

  // In case the file is still in $TEMP, remove it. Note that DeleteFile() will
  // return true if |existing_minidump_path| has already been moved.
  if (!base::DeleteFile(base::FilePath(existing_minidump_path), false)) {
    LOG(ERROR) << "Unable to delete temp minidump file "
               << existing_minidump_path;
    success = false;
  }

  // Use std::endl to flush the log stream in case this process exits.
  LOG(INFO) << "Request to upload crash dump finished. "
            << "Exit now if it is main process that crashed." << std::endl;

  // Restore the original IO restrictions on the thread, if there were any.
  base::ThreadRestrictions::SetIOAllowed(io_allowed);

  return success;
}

void CrashUtil::SetDumpStateCbForTest(
    const base::Callback<int(const std::string&)>& cb) {
  DCHECK(!g_dumpstate_cb);
  g_dumpstate_cb = new base::Callback<int(const std::string&)>(cb);
}

}  // namespace chromecast