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 "content/common/child_process.h" | |
6 | |
7 #if defined(OS_POSIX) && !defined(OS_ANDROID) | |
8 #include <signal.h> // For SigUSR1Handler below. | |
9 #endif | |
10 | |
11 #include "base/message_loop.h" | |
12 #include "base/metrics/statistics_recorder.h" | |
13 #include "base/process_util.h" | |
14 #include "base/string_number_conversions.h" | |
15 #include "base/threading/thread.h" | |
16 #include "base/utf_string_conversions.h" | |
17 #include "content/common/child_thread.h" | |
18 | |
19 #if defined(OS_ANDROID) | |
20 #include "base/debug/debugger.h" | |
21 #endif | |
22 | |
23 #if defined(OS_POSIX) && !defined(OS_ANDROID) | |
24 static void SigUSR1Handler(int signal) { } | |
25 #endif | |
26 | |
27 namespace content { | |
28 // The singleton instance for this process. | |
29 ChildProcess* child_process = NULL; | |
30 | |
31 ChildProcess::ChildProcess() | |
32 : ref_count_(0), | |
33 shutdown_event_(true, false), | |
34 io_thread_("Chrome_ChildIOThread") { | |
35 DCHECK(!child_process); | |
36 child_process = this; | |
37 | |
38 base::StatisticsRecorder::Initialize(); | |
39 | |
40 // We can't recover from failing to start the IO thread. | |
41 CHECK(io_thread_.StartWithOptions( | |
42 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | |
43 | |
44 #if defined(OS_ANDROID) | |
45 io_thread_.SetPriority(base::kThreadPriority_Display); | |
46 #endif | |
47 } | |
48 | |
49 ChildProcess::~ChildProcess() { | |
50 DCHECK(child_process == this); | |
51 | |
52 // Signal this event before destroying the child process. That way all | |
53 // background threads can cleanup. | |
54 // For example, in the renderer the RenderThread instances will be able to | |
55 // notice shutdown before the render process begins waiting for them to exit. | |
56 shutdown_event_.Signal(); | |
57 | |
58 // Kill the main thread object before nulling child_process, since | |
59 // destruction code might depend on it. | |
60 if (main_thread_) { // null in unittests. | |
61 main_thread_->Shutdown(); | |
62 main_thread_.reset(); | |
63 } | |
64 | |
65 child_process = NULL; | |
66 } | |
67 | |
68 ChildThread* ChildProcess::main_thread() { | |
69 return main_thread_.get(); | |
70 } | |
71 | |
72 void ChildProcess::set_main_thread(ChildThread* thread) { | |
73 main_thread_.reset(thread); | |
74 } | |
75 | |
76 void ChildProcess::AddRefProcess() { | |
77 DCHECK(!main_thread_.get() || // null in unittests. | |
78 base::MessageLoop::current() == main_thread_->message_loop()); | |
79 ref_count_++; | |
80 } | |
81 | |
82 void ChildProcess::ReleaseProcess() { | |
83 DCHECK(!main_thread_.get() || // null in unittests. | |
84 base::MessageLoop::current() == main_thread_->message_loop()); | |
85 DCHECK(ref_count_); | |
86 DCHECK(child_process); | |
87 if (--ref_count_) | |
88 return; | |
89 | |
90 if (main_thread_) // null in unittests. | |
91 main_thread_->OnProcessFinalRelease(); | |
92 } | |
93 | |
94 ChildProcess* ChildProcess::current() { | |
95 return child_process; | |
96 } | |
97 | |
98 base::WaitableEvent* ChildProcess::GetShutDownEvent() { | |
99 DCHECK(child_process); | |
100 return &child_process->shutdown_event_; | |
101 } | |
102 | |
103 void ChildProcess::WaitForDebugger(const std::string& label) { | |
104 #if defined(OS_WIN) | |
105 #if defined(GOOGLE_CHROME_BUILD) | |
106 std::string title = "Google Chrome"; | |
107 #else // CHROMIUM_BUILD | |
108 std::string title = "Chromium"; | |
109 #endif // CHROMIUM_BUILD | |
110 title += " "; | |
111 title += label; // makes attaching to process easier | |
112 std::string message = label; | |
113 message += " starting with pid: "; | |
114 message += base::IntToString(base::GetCurrentProcId()); | |
115 ::MessageBox(NULL, UTF8ToWide(message).c_str(), UTF8ToWide(title).c_str(), | |
116 MB_OK | MB_SETFOREGROUND); | |
117 #elif defined(OS_POSIX) | |
118 #if defined(OS_ANDROID) | |
119 LOG(ERROR) << label << " waiting for GDB."; | |
120 // Wait 24 hours for a debugger to be attached to the current process. | |
121 base::debug::WaitForDebugger(24 * 60 * 60, false); | |
122 #else | |
123 // TODO(playmobil): In the long term, overriding this flag doesn't seem | |
124 // right, either use our own flag or open a dialog we can use. | |
125 // This is just to ease debugging in the interim. | |
126 LOG(ERROR) << label | |
127 << " (" | |
128 << getpid() | |
129 << ") paused waiting for debugger to attach. " | |
130 << "Send SIGUSR1 to unpause."; | |
131 // Install a signal handler so that pause can be woken. | |
132 struct sigaction sa; | |
133 memset(&sa, 0, sizeof(sa)); | |
134 sa.sa_handler = SigUSR1Handler; | |
135 sigaction(SIGUSR1, &sa, NULL); | |
136 | |
137 pause(); | |
138 #endif // defined(OS_ANDROID) | |
139 #endif // defined(OS_POSIX) | |
140 } | |
141 | |
142 } // namespace content | |
OLD | NEW |