[email protected] | 154cbabd | 2011-02-17 23:01:57 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 2456c57 | 2009-11-09 04:21:51 | [diff] [blame] | 5 | #include "chrome/browser/crash_handler_host_linux.h" |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 6 | |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 7 | #include <stdint.h> |
[email protected] | 85ebe8f | 2009-10-29 04:02:55 | [diff] [blame] | 8 | #include <stdlib.h> |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 9 | #include <sys/socket.h> |
[email protected] | 66218314 | 2010-07-16 19:28:17 | [diff] [blame] | 10 | #include <sys/syscall.h> |
[email protected] | 4378a82 | 2009-07-08 01:15:14 | [diff] [blame] | 11 | #include <unistd.h> |
| 12 | |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 13 | #include "base/eintr_wrapper.h" |
[email protected] | cbd5fd5 | 2009-08-26 00:14:27 | [diff] [blame] | 14 | #include "base/file_path.h" |
[email protected] | c725d792 | 2009-06-30 00:05:08 | [diff] [blame] | 15 | #include "base/format_macros.h" |
[email protected] | 85ebe8f | 2009-10-29 04:02:55 | [diff] [blame] | 16 | #include "base/linux_util.h" |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 17 | #include "base/logging.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 18 | #include "base/memory/singleton.h" |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 19 | #include "base/message_loop.h" |
[email protected] | cbd5fd5 | 2009-08-26 00:14:27 | [diff] [blame] | 20 | #include "base/path_service.h" |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 21 | #include "base/rand_util.h" |
| 22 | #include "base/string_util.h" |
[email protected] | b064f0eb | 2010-09-02 23:53:26 | [diff] [blame] | 23 | #include "base/task.h" |
[email protected] | 34b9963 | 2011-01-01 01:01:06 | [diff] [blame] | 24 | #include "base/threading/thread.h" |
[email protected] | b07fc511 | 2009-12-02 01:55:06 | [diff] [blame] | 25 | #include "breakpad/src/client/linux/handler/exception_handler.h" |
| 26 | #include "breakpad/src/client/linux/minidump_writer/linux_dumper.h" |
| 27 | #include "breakpad/src/client/linux/minidump_writer/minidump_writer.h" |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 28 | #include "chrome/app/breakpad_linux.h" |
[email protected] | cbd5fd5 | 2009-08-26 00:14:27 | [diff] [blame] | 29 | #include "chrome/common/chrome_paths.h" |
[email protected] | 99ca9a1 | 2010-03-12 18:32:10 | [diff] [blame] | 30 | #include "chrome/common/env_vars.h" |
[email protected] | 3781908b | 2011-03-01 22:49:15 | [diff] [blame] | 31 | #include "content/browser/browser_thread.h" |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 32 | |
[email protected] | 66218314 | 2010-07-16 19:28:17 | [diff] [blame] | 33 | using google_breakpad::ExceptionHandler; |
| 34 | |
[email protected] | b064f0eb | 2010-09-02 23:53:26 | [diff] [blame] | 35 | namespace { |
| 36 | |
[email protected] | 154cbabd | 2011-02-17 23:01:57 | [diff] [blame] | 37 | // The length of the control message: |
| 38 | const unsigned kControlMsgSize = |
| 39 | CMSG_SPACE(2*sizeof(int)) + CMSG_SPACE(sizeof(struct ucred)); |
| 40 | // The length of the regular payload: |
| 41 | const unsigned kCrashContextSize = sizeof(ExceptionHandler::CrashContext); |
| 42 | |
[email protected] | b064f0eb | 2010-09-02 23:53:26 | [diff] [blame] | 43 | // Handles the crash dump and frees the allocated BreakpadInfo struct. |
[email protected] | ca77966 | 2010-11-11 23:28:43 | [diff] [blame] | 44 | void CrashDumpTask(CrashHandlerHostLinux* handler, BreakpadInfo* info) { |
| 45 | if (handler->IsShuttingDown()) |
| 46 | return; |
| 47 | |
[email protected] | b064f0eb | 2010-09-02 23:53:26 | [diff] [blame] | 48 | HandleCrashDump(*info); |
| 49 | delete[] info->filename; |
| 50 | delete[] info->process_type; |
| 51 | delete[] info->crash_url; |
| 52 | delete[] info->guid; |
| 53 | delete[] info->distro; |
| 54 | delete info; |
| 55 | } |
| 56 | |
| 57 | } // namespace |
| 58 | |
[email protected] | 2456c57 | 2009-11-09 04:21:51 | [diff] [blame] | 59 | // Since classes derived from CrashHandlerHostLinux are singletons, it's only |
| 60 | // destroyed at the end of the processes lifetime, which is greater in span than |
| 61 | // the lifetime of the IO message loop. |
[email protected] | c56428f2 | 2010-06-16 02:17:23 | [diff] [blame] | 62 | DISABLE_RUNNABLE_METHOD_REFCOUNT(CrashHandlerHostLinux); |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 63 | |
[email protected] | ca77966 | 2010-11-11 23:28:43 | [diff] [blame] | 64 | CrashHandlerHostLinux::CrashHandlerHostLinux() |
| 65 | : shutting_down_(false) { |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 66 | int fds[2]; |
[email protected] | 2456c57 | 2009-11-09 04:21:51 | [diff] [blame] | 67 | // We use SOCK_SEQPACKET rather than SOCK_DGRAM to prevent the process from |
[email protected] | 54730a1 | 2009-10-07 22:55:48 | [diff] [blame] | 68 | // sending datagrams to other sockets on the system. The sandbox may prevent |
[email protected] | 2456c57 | 2009-11-09 04:21:51 | [diff] [blame] | 69 | // the process from calling socket() to create new sockets, but it'll still |
[email protected] | 54730a1 | 2009-10-07 22:55:48 | [diff] [blame] | 70 | // inherit some sockets. With PF_UNIX+SOCK_DGRAM, it can call sendmsg to send |
| 71 | // a datagram to any (abstract) socket on the same system. With |
| 72 | // SOCK_SEQPACKET, this is prevented. |
[email protected] | c83dd91 | 2010-04-06 18:50:51 | [diff] [blame] | 73 | CHECK_EQ(socketpair(AF_UNIX, SOCK_SEQPACKET, 0, fds), 0); |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 74 | static const int on = 1; |
| 75 | |
| 76 | // Enable passcred on the server end of the socket |
[email protected] | c83dd91 | 2010-04-06 18:50:51 | [diff] [blame] | 77 | CHECK_EQ(setsockopt(fds[1], SOL_SOCKET, SO_PASSCRED, &on, sizeof(on)), 0); |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 78 | |
[email protected] | 2456c57 | 2009-11-09 04:21:51 | [diff] [blame] | 79 | process_socket_ = fds[0]; |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 80 | browser_socket_ = fds[1]; |
| 81 | |
[email protected] | d04e766 | 2010-10-10 22:24:48 | [diff] [blame] | 82 | BrowserThread::PostTask( |
| 83 | BrowserThread::IO, FROM_HERE, |
[email protected] | 2456c57 | 2009-11-09 04:21:51 | [diff] [blame] | 84 | NewRunnableMethod(this, &CrashHandlerHostLinux::Init)); |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 85 | } |
| 86 | |
[email protected] | 2456c57 | 2009-11-09 04:21:51 | [diff] [blame] | 87 | CrashHandlerHostLinux::~CrashHandlerHostLinux() { |
| 88 | HANDLE_EINTR(close(process_socket_)); |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 89 | HANDLE_EINTR(close(browser_socket_)); |
| 90 | } |
| 91 | |
[email protected] | 2456c57 | 2009-11-09 04:21:51 | [diff] [blame] | 92 | void CrashHandlerHostLinux::Init() { |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 93 | MessageLoopForIO* ml = MessageLoopForIO::current(); |
| 94 | CHECK(ml->WatchFileDescriptor( |
| 95 | browser_socket_, true /* persistent */, |
| 96 | MessageLoopForIO::WATCH_READ, |
| 97 | &file_descriptor_watcher_, this)); |
| 98 | ml->AddDestructionObserver(this); |
| 99 | } |
| 100 | |
[email protected] | 19eef06 | 2010-09-16 19:44:09 | [diff] [blame] | 101 | void CrashHandlerHostLinux::InitCrashUploaderThread() { |
| 102 | SetProcessType(); |
| 103 | uploader_thread_.reset( |
| 104 | new base::Thread(std::string(process_type_ + "_crash_uploader").c_str())); |
| 105 | uploader_thread_->Start(); |
| 106 | } |
| 107 | |
[email protected] | 2456c57 | 2009-11-09 04:21:51 | [diff] [blame] | 108 | void CrashHandlerHostLinux::OnFileCanWriteWithoutBlocking(int fd) { |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 109 | DCHECK(false); |
| 110 | } |
| 111 | |
[email protected] | 2456c57 | 2009-11-09 04:21:51 | [diff] [blame] | 112 | void CrashHandlerHostLinux::OnFileCanReadWithoutBlocking(int fd) { |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 113 | DCHECK_EQ(fd, browser_socket_); |
| 114 | |
[email protected] | 2456c57 | 2009-11-09 04:21:51 | [diff] [blame] | 115 | // A process has crashed and has signaled us by writing a datagram |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 116 | // to the death signal socket. The datagram contains the crash context needed |
| 117 | // for writing the minidump as well as a file descriptor and a credentials |
| 118 | // block so that they can't lie about their pid. |
| 119 | |
[email protected] | c7b1d2f | 2010-12-03 03:33:13 | [diff] [blame] | 120 | const size_t kIovSize = 7; |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 121 | struct msghdr msg = {0}; |
[email protected] | c7b1d2f | 2010-12-03 03:33:13 | [diff] [blame] | 122 | struct iovec iov[kIovSize]; |
[email protected] | c468f949 | 2011-04-15 20:56:37 | [diff] [blame] | 123 | |
| 124 | // Freed in WriteDumpFile(); |
| 125 | char* crash_context = new char[kCrashContextSize]; |
| 126 | // Freed in CrashDumpTask(); |
[email protected] | b064f0eb | 2010-09-02 23:53:26 | [diff] [blame] | 127 | char* guid = new char[kGuidSize + 1]; |
| 128 | char* crash_url = new char[kMaxActiveURLSize + 1]; |
| 129 | char* distro = new char[kDistroSize + 1]; |
[email protected] | c468f949 | 2011-04-15 20:56:37 | [diff] [blame] | 130 | |
[email protected] | 66218314 | 2010-07-16 19:28:17 | [diff] [blame] | 131 | char* tid_buf_addr = NULL; |
| 132 | int tid_fd = -1; |
[email protected] | c7b1d2f | 2010-12-03 03:33:13 | [diff] [blame] | 133 | uint64_t uptime; |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 134 | char control[kControlMsgSize]; |
[email protected] | c468f949 | 2011-04-15 20:56:37 | [diff] [blame] | 135 | const ssize_t expected_msg_size = |
| 136 | kCrashContextSize + |
[email protected] | b064f0eb | 2010-09-02 23:53:26 | [diff] [blame] | 137 | kGuidSize + 1 + |
| 138 | kMaxActiveURLSize + 1 + |
| 139 | kDistroSize + 1 + |
[email protected] | c7b1d2f | 2010-12-03 03:33:13 | [diff] [blame] | 140 | sizeof(tid_buf_addr) + sizeof(tid_fd) + |
| 141 | sizeof(uptime); |
[email protected] | 2eb41e7 | 2009-07-15 23:07:34 | [diff] [blame] | 142 | |
| 143 | iov[0].iov_base = crash_context; |
[email protected] | c468f949 | 2011-04-15 20:56:37 | [diff] [blame] | 144 | iov[0].iov_len = kCrashContextSize; |
[email protected] | 2eb41e7 | 2009-07-15 23:07:34 | [diff] [blame] | 145 | iov[1].iov_base = guid; |
[email protected] | b064f0eb | 2010-09-02 23:53:26 | [diff] [blame] | 146 | iov[1].iov_len = kGuidSize + 1; |
[email protected] | 2eb41e7 | 2009-07-15 23:07:34 | [diff] [blame] | 147 | iov[2].iov_base = crash_url; |
[email protected] | b064f0eb | 2010-09-02 23:53:26 | [diff] [blame] | 148 | iov[2].iov_len = kMaxActiveURLSize + 1; |
[email protected] | 912c645 | 2009-07-17 05:55:51 | [diff] [blame] | 149 | iov[3].iov_base = distro; |
[email protected] | b064f0eb | 2010-09-02 23:53:26 | [diff] [blame] | 150 | iov[3].iov_len = kDistroSize + 1; |
[email protected] | 66218314 | 2010-07-16 19:28:17 | [diff] [blame] | 151 | iov[4].iov_base = &tid_buf_addr; |
| 152 | iov[4].iov_len = sizeof(tid_buf_addr); |
| 153 | iov[5].iov_base = &tid_fd; |
| 154 | iov[5].iov_len = sizeof(tid_fd); |
[email protected] | c7b1d2f | 2010-12-03 03:33:13 | [diff] [blame] | 155 | iov[6].iov_base = &uptime; |
| 156 | iov[6].iov_len = sizeof(uptime); |
[email protected] | 2eb41e7 | 2009-07-15 23:07:34 | [diff] [blame] | 157 | msg.msg_iov = iov; |
[email protected] | c7b1d2f | 2010-12-03 03:33:13 | [diff] [blame] | 158 | msg.msg_iovlen = kIovSize; |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 159 | msg.msg_control = control; |
| 160 | msg.msg_controllen = kControlMsgSize; |
| 161 | |
[email protected] | 2eb41e7 | 2009-07-15 23:07:34 | [diff] [blame] | 162 | const ssize_t msg_size = HANDLE_EINTR(recvmsg(browser_socket_, &msg, 0)); |
| 163 | if (msg_size != expected_msg_size) { |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 164 | LOG(ERROR) << "Error reading from death signal socket. Crash dumping" |
| 165 | << " is disabled." |
[email protected] | 2eb41e7 | 2009-07-15 23:07:34 | [diff] [blame] | 166 | << " msg_size:" << msg_size |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 167 | << " errno:" << errno; |
| 168 | file_descriptor_watcher_.StopWatchingFileDescriptor(); |
| 169 | return; |
| 170 | } |
| 171 | |
[email protected] | 2eb41e7 | 2009-07-15 23:07:34 | [diff] [blame] | 172 | if (msg.msg_controllen != kControlMsgSize || |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 173 | msg.msg_flags & ~MSG_TRUNC) { |
| 174 | LOG(ERROR) << "Received death signal message with the wrong size;" |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 175 | << " msg.msg_controllen:" << msg.msg_controllen |
| 176 | << " msg.msg_flags:" << msg.msg_flags |
| 177 | << " kCrashContextSize:" << kCrashContextSize |
| 178 | << " kControlMsgSize:" << kControlMsgSize; |
| 179 | return; |
| 180 | } |
| 181 | |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 182 | // Walk the control payload an extract the file descriptor and validated pid. |
| 183 | pid_t crashing_pid = -1; |
[email protected] | 15e8577 | 2010-08-09 20:44:03 | [diff] [blame] | 184 | int partner_fd = -1; |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 185 | int signal_fd = -1; |
| 186 | for (struct cmsghdr *hdr = CMSG_FIRSTHDR(&msg); hdr; |
| 187 | hdr = CMSG_NXTHDR(&msg, hdr)) { |
| 188 | if (hdr->cmsg_level != SOL_SOCKET) |
| 189 | continue; |
| 190 | if (hdr->cmsg_type == SCM_RIGHTS) { |
| 191 | const unsigned len = hdr->cmsg_len - |
| 192 | (((uint8_t*)CMSG_DATA(hdr)) - (uint8_t*)hdr); |
[email protected] | 912c645 | 2009-07-17 05:55:51 | [diff] [blame] | 193 | DCHECK_EQ(len % sizeof(int), 0u); |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 194 | const unsigned num_fds = len / sizeof(int); |
[email protected] | 15e8577 | 2010-08-09 20:44:03 | [diff] [blame] | 195 | if (num_fds != 2) { |
[email protected] | 2456c57 | 2009-11-09 04:21:51 | [diff] [blame] | 196 | // A nasty process could try and send us too many descriptors and |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 197 | // force a leak. |
[email protected] | 15e8577 | 2010-08-09 20:44:03 | [diff] [blame] | 198 | LOG(ERROR) << "Death signal contained wrong number of descriptors;" |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 199 | << " num_fds:" << num_fds; |
| 200 | for (unsigned i = 0; i < num_fds; ++i) |
| 201 | HANDLE_EINTR(close(reinterpret_cast<int*>(CMSG_DATA(hdr))[i])); |
| 202 | return; |
| 203 | } else { |
[email protected] | 15e8577 | 2010-08-09 20:44:03 | [diff] [blame] | 204 | partner_fd = reinterpret_cast<int*>(CMSG_DATA(hdr))[0]; |
| 205 | signal_fd = reinterpret_cast<int*>(CMSG_DATA(hdr))[1]; |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 206 | } |
| 207 | } else if (hdr->cmsg_type == SCM_CREDENTIALS) { |
| 208 | const struct ucred *cred = |
| 209 | reinterpret_cast<struct ucred*>(CMSG_DATA(hdr)); |
| 210 | crashing_pid = cred->pid; |
| 211 | } |
| 212 | } |
| 213 | |
[email protected] | 15e8577 | 2010-08-09 20:44:03 | [diff] [blame] | 214 | if (crashing_pid == -1 || partner_fd == -1 || signal_fd == -1) { |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 215 | LOG(ERROR) << "Death signal message didn't contain all expected control" |
| 216 | << " messages"; |
[email protected] | 15e8577 | 2010-08-09 20:44:03 | [diff] [blame] | 217 | if (partner_fd >= 0) |
| 218 | HANDLE_EINTR(close(partner_fd)); |
| 219 | if (signal_fd >= 0) |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 220 | HANDLE_EINTR(close(signal_fd)); |
| 221 | return; |
| 222 | } |
| 223 | |
[email protected] | cb7d53e | 2011-06-21 04:21:06 | [diff] [blame] | 224 | // Kernel bug workaround (broken in 2.6.30 and 2.6.32, working in 2.6.38). |
[email protected] | 4378a82 | 2009-07-08 01:15:14 | [diff] [blame] | 225 | // The kernel doesn't translate PIDs in SCM_CREDENTIALS across PID |
| 226 | // namespaces. Thus |crashing_pid| might be garbage from our point of view. |
| 227 | // In the future we can remove this workaround, but we have to wait a couple |
| 228 | // of years to be sure that it's worked its way out into the world. |
[email protected] | cb7d53e | 2011-06-21 04:21:06 | [diff] [blame] | 229 | // TODO(thestig) Remove the workaround when Ubuntu Lucid is deprecated. |
[email protected] | 4378a82 | 2009-07-08 01:15:14 | [diff] [blame] | 230 | |
[email protected] | 15e8577 | 2010-08-09 20:44:03 | [diff] [blame] | 231 | // The crashing process closes its copy of the signal_fd immediately after |
| 232 | // calling sendmsg(). We can thus not reliably look for with with |
| 233 | // FindProcessHoldingSocket(). But by necessity, it has to keep the |
| 234 | // partner_fd open until the crashdump is complete. |
[email protected] | 4378a82 | 2009-07-08 01:15:14 | [diff] [blame] | 235 | uint64_t inode_number; |
[email protected] | 15e8577 | 2010-08-09 20:44:03 | [diff] [blame] | 236 | if (!base::FileDescriptorGetInode(&inode_number, partner_fd)) { |
[email protected] | 4378a82 | 2009-07-08 01:15:14 | [diff] [blame] | 237 | LOG(WARNING) << "Failed to get inode number for passed socket"; |
[email protected] | 15e8577 | 2010-08-09 20:44:03 | [diff] [blame] | 238 | HANDLE_EINTR(close(partner_fd)); |
[email protected] | 4378a82 | 2009-07-08 01:15:14 | [diff] [blame] | 239 | HANDLE_EINTR(close(signal_fd)); |
| 240 | return; |
| 241 | } |
[email protected] | 15e8577 | 2010-08-09 20:44:03 | [diff] [blame] | 242 | HANDLE_EINTR(close(partner_fd)); |
[email protected] | 4378a82 | 2009-07-08 01:15:14 | [diff] [blame] | 243 | |
[email protected] | 66218314 | 2010-07-16 19:28:17 | [diff] [blame] | 244 | pid_t actual_crashing_pid = -1; |
[email protected] | 15e8577 | 2010-08-09 20:44:03 | [diff] [blame] | 245 | if (!base::FindProcessHoldingSocket(&actual_crashing_pid, inode_number)) { |
[email protected] | 4378a82 | 2009-07-08 01:15:14 | [diff] [blame] | 246 | LOG(WARNING) << "Failed to find process holding other end of crash reply " |
| 247 | "socket"; |
| 248 | HANDLE_EINTR(close(signal_fd)); |
| 249 | return; |
| 250 | } |
[email protected] | 15e8577 | 2010-08-09 20:44:03 | [diff] [blame] | 251 | |
[email protected] | cb7d53e | 2011-06-21 04:21:06 | [diff] [blame] | 252 | crashing_pid = actual_crashing_pid; |
[email protected] | 66218314 | 2010-07-16 19:28:17 | [diff] [blame] | 253 | |
[email protected] | cb7d53e | 2011-06-21 04:21:06 | [diff] [blame] | 254 | // The crashing TID set inside the compromised context via |
| 255 | // sys_gettid() in ExceptionHandler::HandleSignal might be wrong (if |
| 256 | // the kernel supports PID namespacing) and may need to be |
| 257 | // translated. |
| 258 | // |
| 259 | // We expect the crashing thread to be in sys_read(), waiting for us to |
| 260 | // write to |signal_fd|. Most newer kernels where we have the different pid |
| 261 | // namespaces also have /proc/[pid]/syscall, so we can look through |
| 262 | // |actual_crashing_pid|'s thread group and find the thread that's in the |
| 263 | // read syscall with the right arguments. |
[email protected] | 66218314 | 2010-07-16 19:28:17 | [diff] [blame] | 264 | |
[email protected] | cb7d53e | 2011-06-21 04:21:06 | [diff] [blame] | 265 | std::string expected_syscall_data; |
| 266 | // /proc/[pid]/syscall is formatted as follows: |
| 267 | // syscall_number arg1 ... arg6 sp pc |
| 268 | // but we just check syscall_number through arg3. |
| 269 | base::StringAppendF(&expected_syscall_data, "%d 0x%x %p 0x1 ", |
| 270 | SYS_read, tid_fd, tid_buf_addr); |
| 271 | bool syscall_supported = false; |
| 272 | pid_t crashing_tid = |
| 273 | base::FindThreadIDWithSyscall(crashing_pid, |
| 274 | expected_syscall_data, |
| 275 | &syscall_supported); |
| 276 | if (crashing_tid == -1 && syscall_supported) { |
| 277 | // We didn't find the thread we want. Maybe it didn't reach |
| 278 | // sys_read() yet or the thread went away. We'll just take a |
| 279 | // guess here and assume the crashing thread is the thread group |
| 280 | // leader. If procfs syscall is not supported by the kernel, then |
| 281 | // we assume the kernel also does not support TID namespacing and |
| 282 | // trust the TID passed by the crashing process. |
| 283 | crashing_tid = crashing_pid; |
[email protected] | 66218314 | 2010-07-16 19:28:17 | [diff] [blame] | 284 | } |
[email protected] | 4378a82 | 2009-07-08 01:15:14 | [diff] [blame] | 285 | |
[email protected] | cb7d53e | 2011-06-21 04:21:06 | [diff] [blame] | 286 | ExceptionHandler::CrashContext* bad_context = |
| 287 | reinterpret_cast<ExceptionHandler::CrashContext*>(crash_context); |
| 288 | bad_context->tid = crashing_tid; |
| 289 | |
[email protected] | 9ddbcd9 | 2009-09-23 21:27:43 | [diff] [blame] | 290 | // Sanitize the string data a bit more |
| 291 | guid[kGuidSize] = crash_url[kMaxActiveURLSize] = distro[kDistroSize] = 0; |
| 292 | |
[email protected] | c468f949 | 2011-04-15 20:56:37 | [diff] [blame] | 293 | // Freed in CrashDumpTask(); |
[email protected] | b064f0eb | 2010-09-02 23:53:26 | [diff] [blame] | 294 | BreakpadInfo* info = new BreakpadInfo; |
| 295 | |
[email protected] | b064f0eb | 2010-09-02 23:53:26 | [diff] [blame] | 296 | info->process_type_length = process_type_.length(); |
| 297 | char* process_type_str = new char[info->process_type_length + 1]; |
| 298 | process_type_.copy(process_type_str, info->process_type_length); |
| 299 | process_type_str[info->process_type_length] = '\0'; |
| 300 | info->process_type = process_type_str; |
| 301 | |
| 302 | info->crash_url_length = strlen(crash_url); |
| 303 | info->crash_url = crash_url; |
| 304 | |
| 305 | info->guid_length = strlen(guid); |
| 306 | info->guid = guid; |
| 307 | |
| 308 | info->distro_length = strlen(distro); |
| 309 | info->distro = distro; |
| 310 | |
[email protected] | 154cbabd | 2011-02-17 23:01:57 | [diff] [blame] | 311 | info->upload = (getenv(env_vars::kHeadless) == NULL); |
[email protected] | c7b1d2f | 2010-12-03 03:33:13 | [diff] [blame] | 312 | info->process_start_time = uptime; |
[email protected] | b064f0eb | 2010-09-02 23:53:26 | [diff] [blame] | 313 | |
[email protected] | 154cbabd | 2011-02-17 23:01:57 | [diff] [blame] | 314 | BrowserThread::PostTask( |
| 315 | BrowserThread::FILE, FROM_HERE, |
| 316 | NewRunnableMethod(this, |
| 317 | &CrashHandlerHostLinux::WriteDumpFile, |
| 318 | info, |
| 319 | crashing_pid, |
[email protected] | c468f949 | 2011-04-15 20:56:37 | [diff] [blame] | 320 | crash_context, |
[email protected] | 154cbabd | 2011-02-17 23:01:57 | [diff] [blame] | 321 | signal_fd)); |
| 322 | } |
| 323 | |
| 324 | void CrashHandlerHostLinux::WriteDumpFile(BreakpadInfo* info, |
| 325 | pid_t crashing_pid, |
| 326 | char* crash_context, |
| 327 | int signal_fd) { |
| 328 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 329 | |
| 330 | FilePath dumps_path("/tmp"); |
| 331 | PathService::Get(base::DIR_TEMP, &dumps_path); |
| 332 | if (!info->upload) |
| 333 | PathService::Get(chrome::DIR_CRASH_DUMPS, &dumps_path); |
| 334 | const uint64 rand = base::RandUint64(); |
| 335 | const std::string minidump_filename = |
| 336 | StringPrintf("%s/chromium-%s-minidump-%016" PRIx64 ".dmp", |
| 337 | dumps_path.value().c_str(), process_type_.c_str(), rand); |
| 338 | if (!google_breakpad::WriteMinidump(minidump_filename.c_str(), |
| 339 | crashing_pid, crash_context, |
| 340 | kCrashContextSize)) { |
| 341 | LOG(ERROR) << "Failed to write crash dump for pid " << crashing_pid; |
| 342 | } |
[email protected] | c468f949 | 2011-04-15 20:56:37 | [diff] [blame] | 343 | delete[] crash_context; |
[email protected] | 154cbabd | 2011-02-17 23:01:57 | [diff] [blame] | 344 | |
[email protected] | c468f949 | 2011-04-15 20:56:37 | [diff] [blame] | 345 | // Freed in CrashDumpTask(); |
[email protected] | 154cbabd | 2011-02-17 23:01:57 | [diff] [blame] | 346 | char* minidump_filename_str = new char[minidump_filename.length() + 1]; |
| 347 | minidump_filename.copy(minidump_filename_str, minidump_filename.length()); |
| 348 | minidump_filename_str[minidump_filename.length()] = '\0'; |
| 349 | info->filename = minidump_filename_str; |
| 350 | |
| 351 | BrowserThread::PostTask( |
| 352 | BrowserThread::IO, FROM_HERE, |
| 353 | NewRunnableMethod(this, |
| 354 | &CrashHandlerHostLinux::QueueCrashDumpTask, |
| 355 | info, |
| 356 | signal_fd)); |
| 357 | } |
| 358 | |
| 359 | void CrashHandlerHostLinux::QueueCrashDumpTask(BreakpadInfo* info, |
| 360 | int signal_fd) { |
| 361 | DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 362 | |
| 363 | // Send the done signal to the process: it can exit now. |
| 364 | struct msghdr msg = {0}; |
| 365 | struct iovec done_iov; |
| 366 | done_iov.iov_base = const_cast<char*>("\x42"); |
| 367 | done_iov.iov_len = 1; |
| 368 | msg.msg_iov = &done_iov; |
| 369 | msg.msg_iovlen = 1; |
| 370 | |
| 371 | HANDLE_EINTR(sendmsg(signal_fd, &msg, MSG_DONTWAIT | MSG_NOSIGNAL)); |
| 372 | HANDLE_EINTR(close(signal_fd)); |
| 373 | |
[email protected] | b064f0eb | 2010-09-02 23:53:26 | [diff] [blame] | 374 | uploader_thread_->message_loop()->PostTask( |
| 375 | FROM_HERE, |
[email protected] | ca77966 | 2010-11-11 23:28:43 | [diff] [blame] | 376 | NewRunnableFunction(&CrashDumpTask, this, info)); |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 377 | } |
| 378 | |
[email protected] | 2456c57 | 2009-11-09 04:21:51 | [diff] [blame] | 379 | void CrashHandlerHostLinux::WillDestroyCurrentMessageLoop() { |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 380 | file_descriptor_watcher_.StopWatchingFileDescriptor(); |
[email protected] | ca77966 | 2010-11-11 23:28:43 | [diff] [blame] | 381 | |
| 382 | // If we are quitting and there are crash dumps in the queue, turn them into |
| 383 | // no-ops. |
| 384 | shutting_down_ = true; |
| 385 | uploader_thread_->Stop(); |
| 386 | } |
| 387 | |
| 388 | bool CrashHandlerHostLinux::IsShuttingDown() const { |
| 389 | return shutting_down_; |
[email protected] | 9a5d2a5 | 2009-05-22 03:37:45 | [diff] [blame] | 390 | } |
[email protected] | b064f0eb | 2010-09-02 23:53:26 | [diff] [blame] | 391 | |
[email protected] | 9dbfff1 | 2011-07-01 19:37:07 | [diff] [blame^] | 392 | ExtensionCrashHandlerHostLinux::ExtensionCrashHandlerHostLinux() { |
| 393 | InitCrashUploaderThread(); |
| 394 | } |
| 395 | |
| 396 | ExtensionCrashHandlerHostLinux::~ExtensionCrashHandlerHostLinux() { |
| 397 | } |
| 398 | |
| 399 | void ExtensionCrashHandlerHostLinux::SetProcessType() { |
| 400 | process_type_ = "extension"; |
| 401 | } |
| 402 | |
| 403 | // static |
| 404 | ExtensionCrashHandlerHostLinux* ExtensionCrashHandlerHostLinux::GetInstance() { |
| 405 | return Singleton<ExtensionCrashHandlerHostLinux>::get(); |
| 406 | } |
| 407 | |
[email protected] | 9289af82 | 2011-02-03 18:21:20 | [diff] [blame] | 408 | GpuCrashHandlerHostLinux::GpuCrashHandlerHostLinux() { |
| 409 | InitCrashUploaderThread(); |
| 410 | } |
| 411 | |
| 412 | GpuCrashHandlerHostLinux::~GpuCrashHandlerHostLinux() { |
| 413 | } |
| 414 | |
| 415 | void GpuCrashHandlerHostLinux::SetProcessType() { |
| 416 | process_type_ = "gpu-process"; |
| 417 | } |
| 418 | |
| 419 | // static |
| 420 | GpuCrashHandlerHostLinux* GpuCrashHandlerHostLinux::GetInstance() { |
| 421 | return Singleton<GpuCrashHandlerHostLinux>::get(); |
| 422 | } |
| 423 | |
[email protected] | b064f0eb | 2010-09-02 23:53:26 | [diff] [blame] | 424 | PluginCrashHandlerHostLinux::PluginCrashHandlerHostLinux() { |
[email protected] | 19eef06 | 2010-09-16 19:44:09 | [diff] [blame] | 425 | InitCrashUploaderThread(); |
[email protected] | b064f0eb | 2010-09-02 23:53:26 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | PluginCrashHandlerHostLinux::~PluginCrashHandlerHostLinux() { |
| 429 | } |
| 430 | |
| 431 | void PluginCrashHandlerHostLinux::SetProcessType() { |
| 432 | process_type_ = "plugin"; |
| 433 | } |
| 434 | |
[email protected] | d3c6c0d7 | 2010-12-09 08:15:04 | [diff] [blame] | 435 | // static |
| 436 | PluginCrashHandlerHostLinux* PluginCrashHandlerHostLinux::GetInstance() { |
| 437 | return Singleton<PluginCrashHandlerHostLinux>::get(); |
| 438 | } |
| 439 | |
[email protected] | 54457f3 | 2011-04-15 22:05:29 | [diff] [blame] | 440 | PpapiCrashHandlerHostLinux::PpapiCrashHandlerHostLinux() { |
| 441 | InitCrashUploaderThread(); |
| 442 | } |
| 443 | |
| 444 | PpapiCrashHandlerHostLinux::~PpapiCrashHandlerHostLinux() { |
| 445 | } |
| 446 | |
| 447 | void PpapiCrashHandlerHostLinux::SetProcessType() { |
| 448 | process_type_ = "ppapi"; |
| 449 | } |
| 450 | |
| 451 | // static |
| 452 | PpapiCrashHandlerHostLinux* PpapiCrashHandlerHostLinux::GetInstance() { |
| 453 | return Singleton<PpapiCrashHandlerHostLinux>::get(); |
| 454 | } |
[email protected] | 9dbfff1 | 2011-07-01 19:37:07 | [diff] [blame^] | 455 | |
| 456 | RendererCrashHandlerHostLinux::RendererCrashHandlerHostLinux() { |
| 457 | InitCrashUploaderThread(); |
| 458 | } |
| 459 | |
| 460 | RendererCrashHandlerHostLinux::~RendererCrashHandlerHostLinux() { |
| 461 | } |
| 462 | |
| 463 | void RendererCrashHandlerHostLinux::SetProcessType() { |
| 464 | process_type_ = "renderer"; |
| 465 | } |
| 466 | |
| 467 | // static |
| 468 | RendererCrashHandlerHostLinux* RendererCrashHandlerHostLinux::GetInstance() { |
| 469 | return Singleton<RendererCrashHandlerHostLinux>::get(); |
| 470 | } |