OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromecast/crash/linux/crash_util.h" | 5 #include "chromecast/crash/linux/crash_util.h" |
6 | 6 |
| 7 #include <stdlib.h> |
| 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
9 #include "base/files/file_util.h" | |
10 #include "base/threading/thread_restrictions.h" | |
11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
12 #include "chromecast/base/path_utils.h" | 12 #include "chromecast/base/path_utils.h" |
13 #include "chromecast/base/version.h" | 13 #include "chromecast/base/version.h" |
14 #include "chromecast/crash/app_state_tracker.h" | 14 #include "chromecast/crash/app_state_tracker.h" |
15 #include "chromecast/crash/linux/dummy_minidump_generator.h" | 15 #include "chromecast/crash/linux/dummy_minidump_generator.h" |
16 #include "chromecast/crash/linux/minidump_writer.h" | 16 #include "chromecast/crash/linux/minidump_writer.h" |
17 | 17 |
18 namespace chromecast { | 18 namespace chromecast { |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 // This can be set to a callback for testing. This allows us to inject a fake | 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. | 23 // dumpstate routine to avoid calling an executable during an automated test. |
24 // This value should not be mutated through any other function except | 24 // This value should not be mutated through any other function except |
25 // CrashUtil::SetDumpStateCbForTest(). | 25 // CrashUtil::SetDumpStateCbForTest(). |
26 static base::Callback<int(const std::string&)>* g_dumpstate_cb = nullptr; | 26 static base::Callback<int(const std::string&)>* g_dumpstate_cb = nullptr; |
27 | 27 |
28 } // namespace | 28 } // namespace |
29 | 29 |
30 // static | 30 // static |
31 uint64_t CrashUtil::GetCurrentTimeMs() { | 31 uint64_t CrashUtil::GetCurrentTimeMs() { |
32 return (base::TimeTicks::Now() - base::TimeTicks()).InMilliseconds(); | 32 return (base::TimeTicks::Now() - base::TimeTicks()).InMilliseconds(); |
33 } | 33 } |
34 | 34 |
35 // static | 35 // static |
36 bool CrashUtil::RequestUploadCrashDump( | 36 bool CrashUtil::RequestUploadCrashDump( |
37 const std::string& existing_minidump_path, | 37 const std::string& existing_minidump_path, |
38 const std::string& crashed_process_name, | 38 const std::string& crashed_process_name, |
39 uint64_t crashed_process_start_time_ms) { | 39 uint64_t crashed_process_start_time_ms) { |
40 // Remove IO restrictions from this thread. Chromium IO functions must be used | |
41 // to access the file system and upload information to the crash server. | |
42 const bool io_allowed = base::ThreadRestrictions::SetIOAllowed(true); | |
43 | |
44 LOG(INFO) << "Request to upload crash dump " << existing_minidump_path | 40 LOG(INFO) << "Request to upload crash dump " << existing_minidump_path |
45 << " for process " << crashed_process_name; | 41 << " for process " << crashed_process_name; |
46 | 42 |
| 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. |
47 uint64_t uptime_ms = GetCurrentTimeMs() - crashed_process_start_time_ms; | 46 uint64_t uptime_ms = GetCurrentTimeMs() - crashed_process_start_time_ms; |
48 MinidumpParams params(crashed_process_name, | 47 MinidumpParams params(crashed_process_name, |
49 uptime_ms, | 48 uptime_ms, |
50 "", // suffix | 49 "", // suffix |
51 AppStateTracker::GetPreviousApp(), | 50 AppStateTracker::GetPreviousApp(), |
52 AppStateTracker::GetCurrentApp(), | 51 AppStateTracker::GetCurrentApp(), |
53 AppStateTracker::GetLastLaunchedApp(), | 52 AppStateTracker::GetLastLaunchedApp(), |
54 CAST_BUILD_RELEASE, | 53 CAST_BUILD_RELEASE, |
55 CAST_BUILD_INCREMENTAL); | 54 CAST_BUILD_INCREMENTAL); |
56 DummyMinidumpGenerator minidump_generator(existing_minidump_path); | 55 DummyMinidumpGenerator minidump_generator(existing_minidump_path); |
57 | 56 |
58 base::FilePath filename = base::FilePath(existing_minidump_path).BaseName(); | 57 base::FilePath filename = base::FilePath(existing_minidump_path).BaseName(); |
59 | 58 |
60 scoped_ptr<MinidumpWriter> writer; | 59 scoped_ptr<MinidumpWriter> writer; |
61 if (g_dumpstate_cb) { | 60 if (g_dumpstate_cb) { |
62 writer.reset(new MinidumpWriter( | 61 writer.reset(new MinidumpWriter( |
63 &minidump_generator, filename.value(), params, *g_dumpstate_cb)); | 62 &minidump_generator, filename.value(), params, *g_dumpstate_cb)); |
64 } else { | 63 } else { |
65 writer.reset( | 64 writer.reset( |
66 new MinidumpWriter(&minidump_generator, filename.value(), params)); | 65 new MinidumpWriter(&minidump_generator, filename.value(), params)); |
67 } | 66 } |
68 bool success = false; | 67 bool success = false; |
69 writer->set_non_blocking(false); | 68 writer->set_non_blocking(false); |
70 success = (0 == writer->Write()); // error already logged. | 69 success = (0 == writer->Write()); // error already logged. |
71 | 70 |
72 // In case the file is still in $TEMP, remove it. Note that DeleteFile() will | 71 // In case the file is still in $TEMP, remove it. |
73 // return true if |existing_minidump_path| has already been moved. | 72 if (remove(existing_minidump_path.c_str()) < 0 && errno != ENOENT) { |
74 if (!base::DeleteFile(base::FilePath(existing_minidump_path), false)) { | |
75 LOG(ERROR) << "Unable to delete temp minidump file " | 73 LOG(ERROR) << "Unable to delete temp minidump file " |
76 << existing_minidump_path; | 74 << existing_minidump_path << ": " << strerror(errno); |
77 success = false; | 75 success = false; |
78 } | 76 } |
79 | 77 |
80 // Use std::endl to flush the log stream in case this process exits. | 78 // Use std::endl to flush the log stream in case this process exits. |
81 LOG(INFO) << "Request to upload crash dump finished. " | 79 LOG(INFO) << "Request to upload crash dump finished. " |
82 << "Exit now if it is main process that crashed." << std::endl; | 80 << "Exit now if it is main process that crashed." << std::endl; |
83 | 81 |
84 // Restore the original IO restrictions on the thread, if there were any. | |
85 base::ThreadRestrictions::SetIOAllowed(io_allowed); | |
86 | |
87 return success; | 82 return success; |
88 } | 83 } |
89 | 84 |
90 void CrashUtil::SetDumpStateCbForTest( | 85 void CrashUtil::SetDumpStateCbForTest( |
91 const base::Callback<int(const std::string&)>& cb) { | 86 const base::Callback<int(const std::string&)>& cb) { |
92 DCHECK(!g_dumpstate_cb); | 87 DCHECK(!g_dumpstate_cb); |
93 g_dumpstate_cb = new base::Callback<int(const std::string&)>(cb); | 88 g_dumpstate_cb = new base::Callback<int(const std::string&)>(cb); |
94 } | 89 } |
95 | 90 |
96 } // namespace chromecast | 91 } // namespace chromecast |
OLD | NEW |