Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 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. | |
| 4 | |
| 5 #include "chrome/browser/android/crash_dump_manager.h" | |
| 6 | |
| 7 #include <inttypes.h> | |
|
Lei Zhang
2012/10/23 02:57:34
why do we need this?
Jay Civelli
2012/10/23 20:39:55
We need it for the PRIx64.
Lei Zhang
2012/10/23 21:34:57
Can we use base/format_macros.h instead?
Jay Civelli
2012/10/24 00:12:53
Done.
| |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/file_util.h" | |
| 11 #include "base/global_descriptors_posix.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/path_service.h" | |
| 14 #include "base/process.h" | |
| 15 #include "base/rand_util.h" | |
| 16 #include "base/stringprintf.h" | |
| 17 #include "chrome/common/chrome_paths.h" | |
| 18 #include "chrome/common/descriptors_android.h" | |
| 19 #include "content/public/browser/browser_thread.h" | |
| 20 #include "content/public/browser/child_process_data.h" | |
| 21 #include "content/public/browser/file_descriptor_info.h" | |
| 22 #include "content/public/browser/notification_service.h" | |
| 23 #include "content/public/browser/notification_types.h" | |
| 24 #include "content/public/browser/render_process_host.h" | |
| 25 | |
| 26 using content::BrowserThread; | |
| 27 | |
| 28 CrashDumpManager::CrashDumpManager() { | |
| 29 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 30 notification_registrar_.Add(this, | |
| 31 content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | |
| 32 content::NotificationService::AllSources()); | |
| 33 notification_registrar_.Add(this, | |
| 34 content::NOTIFICATION_CHILD_PROCESS_HOST_DISCONNECTED, | |
| 35 content::NotificationService::AllSources()); | |
| 36 | |
|
jam
2012/10/23 01:20:47
nit: extra line
Jay Civelli
2012/10/23 20:39:55
Done.
| |
| 37 } | |
| 38 | |
| 39 CrashDumpManager::~CrashDumpManager() { | |
| 40 } | |
| 41 | |
| 42 int CrashDumpManager::CreateMinidumpFile(int process_host_id) { | |
| 43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::PROCESS_LAUNCHER)); | |
| 44 FilePath minidump_path; | |
| 45 if (!file_util::CreateTemporaryFile(&minidump_path)) | |
| 46 return base::kInvalidPlatformFileValue; | |
| 47 | |
| 48 base::PlatformFileError error; | |
| 49 // We need read permission as the minidump is generated in several phases | |
| 50 // and needs to be read at some point. | |
| 51 int flags = base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ | | |
| 52 base::PLATFORM_FILE_WRITE; | |
| 53 base::PlatformFile f = | |
|
Lei Zhang
2012/10/23 02:57:34
nit: can we name the variable to something that's
Jay Civelli
2012/10/23 20:39:55
Done.
Jay Civelli
2012/10/23 20:39:55
Done.
| |
| 54 base::CreatePlatformFile(minidump_path, flags, NULL, &error); | |
| 55 if (f == base::kInvalidPlatformFileValue) { | |
| 56 LOG(ERROR) << "Failed to create temporary file, crash won't be reported."; | |
| 57 return base::kInvalidPlatformFileValue; | |
| 58 } | |
| 59 | |
| 60 MinidumpInfo minidump_info; | |
| 61 minidump_info.file = f; | |
| 62 minidump_info.path = minidump_path; | |
| 63 { | |
| 64 base::AutoLock auto_lock(process_host_id_to_minidump_info_lock_); | |
| 65 DCHECK(process_host_id_to_minidump_info_.find(process_host_id) == | |
|
Lei Zhang
2012/10/23 02:57:34
nit: more readable with ContainsKey().
Jay Civelli
2012/10/23 20:39:55
Done.
| |
| 66 process_host_id_to_minidump_info_.end()); | |
| 67 process_host_id_to_minidump_info_[process_host_id] = minidump_info; | |
| 68 } | |
| 69 return f; | |
| 70 } | |
| 71 | |
| 72 void CrashDumpManager::ProcessMinidump(const MinidumpInfo& minidump) { | |
| 73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 74 // Close the file descriptor, it is still open. | |
| 75 bool r = base::ClosePlatformFile(minidump.file); | |
| 76 DCHECK(r) << "Failed to close minidump file descriptor."; | |
| 77 | |
| 78 int64 file_size = 0; | |
| 79 r = file_util::GetFileSize(minidump.path, &file_size); | |
| 80 DCHECK(r) << "Failed to retrieve size for minidump " << | |
|
Lei Zhang
2012/10/23 02:57:34
nit: put the trailing << on the next line and line
Jay Civelli
2012/10/23 20:39:55
Done.
| |
| 81 minidump.path.value(); | |
| 82 | |
| 83 if (file_size == 0) { | |
| 84 // Empty minidump, this process did not crash. Just remove the file. | |
| 85 r = file_util::Delete(minidump.path, false); | |
| 86 DCHECK(r) << "Failed to delete temporary minidump file " << | |
| 87 minidump.path.value(); | |
| 88 return; | |
| 89 } | |
| 90 | |
| 91 // We are dealing with a valid minidump. Copy it to the crash report | |
| 92 // directory from where Java code will upload it later on. | |
| 93 FilePath crash_dump_dir; | |
| 94 r = PathService::Get(chrome::DIR_CRASH_DUMPS, &crash_dump_dir); | |
| 95 if (!r) { | |
| 96 NOTREACHED() << "Failed to retrieve the crash dump directory."; | |
| 97 return; | |
| 98 } | |
| 99 | |
| 100 const uint64 rand = base::RandUint64(); | |
| 101 const std::string filename = | |
| 102 base::StringPrintf("chromium-renderer-minidump-%016" PRIx64 ".dmp%d", | |
| 103 rand, minidump.pid); | |
| 104 FilePath dest_path = crash_dump_dir.Append(filename); | |
| 105 r = file_util::Move(minidump.path, dest_path); | |
| 106 if (!r) { | |
| 107 LOG(ERROR) << "Failed to move crash dump from " << minidump.path.value() << | |
| 108 " to " << dest_path.value(); | |
| 109 file_util::Delete(minidump.path, false); | |
| 110 return; | |
| 111 } | |
| 112 LOG(INFO) << "Crash minidump successfully generated: " << | |
| 113 crash_dump_dir.Append(filename).value(); | |
| 114 } | |
| 115 | |
| 116 void CrashDumpManager::Observe(int type, | |
| 117 const content::NotificationSource& source, | |
| 118 const content::NotificationDetails& details) { | |
| 119 int process_host_id; | |
| 120 switch (type) { | |
| 121 case content::NOTIFICATION_RENDERER_PROCESS_CLOSED: { | |
| 122 content::RenderProcessHost* rph = | |
| 123 content::Source<content::RenderProcessHost>(source).ptr(); | |
| 124 process_host_id = rph->GetID(); | |
| 125 break; | |
| 126 } | |
| 127 case content::NOTIFICATION_CHILD_PROCESS_HOST_DISCONNECTED: { | |
| 128 content::ChildProcessData* chil_process_data = | |
|
Lei Zhang
2012/10/23 02:57:34
nit: chil_ -> child_ ?
Jay Civelli
2012/10/23 20:39:55
Done.
| |
| 129 content::Details<content::ChildProcessData>(details).ptr(); | |
| 130 process_host_id = chil_process_data->id; | |
| 131 break; | |
| 132 } | |
| 133 default: | |
| 134 NOTREACHED(); | |
| 135 return; | |
| 136 } | |
| 137 MinidumpInfo minidump_info; | |
| 138 { | |
| 139 base::AutoLock auto_lock(process_host_id_to_minidump_info_lock_); | |
| 140 ProcessHostIDToMinidumpInfo::iterator iter = | |
| 141 process_host_id_to_minidump_info_.find(process_host_id); | |
| 142 DCHECK(iter != process_host_id_to_minidump_info_.end()); | |
| 143 minidump_info = iter->second; | |
| 144 process_host_id_to_minidump_info_.erase(iter); | |
| 145 } | |
| 146 ProcessMinidump(minidump_info); | |
| 147 } | |
| OLD | NEW |