Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "content/browser/tracing/arc_tracing_agent.h" | 5 #include "content/browser/tracing/arc_tracing_agent.h" |
| 6 | 6 |
| 7 #include <string.h> | |
| 8 #include <sys/socket.h> | |
| 9 | |
| 10 #include <memory> | |
| 7 #include <string> | 11 #include <string> |
| 12 #include <utility> | |
| 13 #include <vector> | |
| 8 | 14 |
| 9 #include "base/bind.h" | 15 #include "base/bind.h" |
| 16 #include "base/bind_helpers.h" | |
| 17 #include "base/files/file.h" | |
| 18 #include "base/files/file_descriptor_watcher_posix.h" | |
| 19 #include "base/json/json_reader.h" | |
| 10 #include "base/logging.h" | 20 #include "base/logging.h" |
| 21 #include "base/memory/ptr_util.h" | |
| 11 #include "base/memory/singleton.h" | 22 #include "base/memory/singleton.h" |
| 12 #include "base/threading/thread_checker.h" | 23 #include "base/memory/weak_ptr.h" |
| 24 #include "base/posix/unix_domain_socket_linux.h" | |
| 25 #include "base/threading/sequenced_task_runner_handle.h" | |
| 13 #include "base/threading/thread_task_runner_handle.h" | 26 #include "base/threading/thread_task_runner_handle.h" |
| 27 #include "base/time/time.h" | |
| 28 #include "base/trace_event/trace_buffer.h" | |
| 29 #include "content/public/browser/browser_thread.h" | |
| 30 | |
| 31 using base::trace_event::TraceEvent; | |
| 32 using base::trace_event::TraceBuffer; | |
| 33 using base::trace_event::TraceBufferChunk; | |
| 14 | 34 |
| 15 namespace content { | 35 namespace content { |
| 16 | 36 |
| 17 namespace { | 37 namespace { |
| 18 | 38 |
| 39 constexpr size_t kArcTraceMessageLength = 1024 + 512; | |
| 19 constexpr char kArcTracingAgentName[] = "arc"; | 40 constexpr char kArcTracingAgentName[] = "arc"; |
| 20 constexpr char kArcTraceLabel[] = "ArcTraceEvents"; | 41 constexpr char kArcTraceLabel[] = "ArcTraceEvents"; |
| 21 | 42 |
| 22 void OnStopTracing(bool success) { | 43 // Number of chunks for the ring buffer. |
| 23 DLOG_IF(WARNING, !success) << "Failed to stop ARC tracing."; | 44 constexpr size_t kTraceEventChunks = |
| 24 } | 45 64000 / TraceBufferChunk::kTraceBufferChunkSize; |
| 46 | |
| 47 class ArcTracingReader { | |
| 48 public: | |
| 49 using StopTracingCallback = | |
| 50 base::Callback<void(const scoped_refptr<base::RefCountedString>&)>; | |
| 51 | |
| 52 ArcTracingReader() : weak_ptr_factory_(this) {} | |
| 53 | |
| 54 void StartTracing(base::ScopedFD read_fd) { | |
| 55 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 56 read_fd_ = std::move(read_fd); | |
| 57 trace_buffer_.reset( | |
| 58 TraceBuffer::CreateTraceBufferRingBuffer(kTraceEventChunks)); | |
| 59 chunk_.reset(); | |
| 60 chunk_index_ = 0; | |
| 61 fd_watcher_ = base::FileDescriptorWatcher::WatchReadable( | |
| 62 read_fd_.get(), base::Bind(&ArcTracingReader::OnTraceDataAvailable, | |
| 63 base::Unretained(this))); | |
| 64 } | |
| 65 | |
| 66 void OnTraceDataAvailable() { | |
| 67 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 68 | |
| 69 char buf[kArcTraceMessageLength]; | |
| 70 std::vector<base::ScopedFD> unused_fds; | |
| 71 ssize_t n = base::UnixDomainSocket::RecvMsg( | |
| 72 read_fd_.get(), buf, kArcTraceMessageLength, &unused_fds); | |
| 73 // When EOF, return and do nothing. The clean up is done in StopTracing. | |
| 74 if (n == 0) | |
| 75 return; | |
| 76 | |
| 77 if (n < 0) { | |
| 78 PLOG(WARNING) << "Unexpected error while reading trace from client."; | |
| 79 // Do nothing here as StopTracing will do the clean up and the existing | |
| 80 // trace logs will be returned. | |
| 81 return; | |
| 82 } | |
| 83 | |
| 84 if (n > static_cast<ssize_t>(kArcTraceMessageLength)) { | |
| 85 LOG(WARNING) << "Unexpected data size when reading trace from client."; | |
| 86 return; | |
| 87 } | |
| 88 | |
| 89 std::unique_ptr<char[]> data = base::MakeUnique<char[]>(n + 1); | |
| 90 memcpy(data.get(), buf, n); | |
| 91 data[n] = 0; | |
| 92 | |
| 93 // Here we only validate the data, which should be a JSON string. But we | |
| 94 // don't really parse them into the fields of |TraceEvent|. It's because | |
| 95 // |TraceEvent| is designed for Chrome trace. There are many restrictions in | |
| 96 // its implementation, e.g., no pid and tid together, and no creation of | |
| 97 // TimeTicks with existing long value. The flags used for |TraceEvent| is | |
| 98 // also not suitable for our use case here. | |
| 99 // | |
| 100 // To avoid the issue, we put entire JSON string in the |name| field | |
| 101 // instead, so that we can still use |TraceBuffer| and |TraceBufferChunk| | |
| 102 // for data storage. | |
| 103 std::unique_ptr<base::Value> value = base::JSONReader::Read(data.get()); | |
|
dcheng
2017/04/06 06:14:14
We shouldn't be parsing JSON from less trusted pro
shunhsingou
2017/04/06 08:42:28
The JSON data is generated by our own service in A
| |
| 104 if (value.get() == nullptr) { | |
| 105 LOG(WARNING) << "Get incorrect trace data from container."; | |
| 106 return; | |
| 107 } | |
| 108 | |
| 109 // Save data into trace buffer. | |
| 110 if (!chunk_) | |
| 111 chunk_ = trace_buffer_->GetChunk(&chunk_index_); | |
| 112 size_t event_index; | |
| 113 TraceEvent* event = chunk_->AddTraceEvent(&event_index); | |
| 114 | |
| 115 // If this is a reused event, free the data we saved previously before | |
| 116 // calling Initialize() again. | |
| 117 if (event->name() != nullptr) | |
| 118 delete[] event->name(); | |
|
dcheng
2017/04/06 06:14:14
The manual management of event->name() is really h
shunhsingou
2017/04/06 08:42:27
It's a ring buffer so it doesn't make sense to hav
| |
| 119 | |
| 120 event->Initialize(0, base::TimeTicks(), base::ThreadTicks(), 0, nullptr, | |
| 121 data.release(), nullptr, 0, 0, 0, nullptr, nullptr, | |
| 122 nullptr, nullptr, 0); | |
| 123 | |
| 124 if (chunk_->IsFull()) { | |
| 125 trace_buffer_->ReturnChunk(chunk_index_, std::move(chunk_)); | |
| 126 chunk_ = trace_buffer_->GetChunk(&chunk_index_); | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 void StopTracing(const StopTracingCallback& callback) { | |
| 131 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 132 // Stop fd_watcher_. | |
| 133 fd_watcher_.reset(); | |
| 134 read_fd_.reset(); | |
| 135 | |
| 136 if (chunk_) | |
| 137 trace_buffer_->ReturnChunk(chunk_index_, std::move(chunk_)); | |
| 138 | |
| 139 bool append_comma = false; | |
| 140 std::string data; | |
| 141 while (const TraceBufferChunk* chunk = trace_buffer_->NextChunk()) { | |
| 142 for (size_t i = 0; i < chunk->size(); ++i) { | |
| 143 const TraceEvent* event = chunk->GetEventAt(i); | |
| 144 if (append_comma) | |
| 145 data.append(","); | |
| 146 // See comment in |OnTraceDataAvailable|. We put the whole valid JSON | |
| 147 // object in |name|. | |
| 148 data.append(event->name()); | |
| 149 append_comma = true; | |
| 150 delete[] event->name(); | |
| 151 } | |
| 152 } | |
| 153 BrowserThread::PostTask( | |
| 154 BrowserThread::IO, FROM_HERE, | |
| 155 base::Bind(callback, base::RefCountedString::TakeString(&data))); | |
| 156 } | |
| 157 | |
| 158 base::WeakPtr<ArcTracingReader> GetWeakPtr() { | |
| 159 return weak_ptr_factory_.GetWeakPtr(); | |
| 160 } | |
| 161 | |
| 162 private: | |
| 163 base::ScopedFD read_fd_; | |
| 164 std::unique_ptr<base::FileDescriptorWatcher::Controller> fd_watcher_; | |
| 165 std::unique_ptr<TraceBuffer> trace_buffer_; | |
| 166 std::unique_ptr<TraceBufferChunk> chunk_; | |
| 167 size_t chunk_index_ = 0; | |
| 168 // NOTE: Weak pointers must be invalidated before all other member variables | |
| 169 // so it must be the last member. | |
| 170 base::WeakPtrFactory<ArcTracingReader> weak_ptr_factory_; | |
| 171 | |
| 172 DISALLOW_COPY_AND_ASSIGN(ArcTracingReader); | |
| 173 }; | |
| 25 | 174 |
| 26 class ArcTracingAgentImpl : public ArcTracingAgent { | 175 class ArcTracingAgentImpl : public ArcTracingAgent { |
| 27 public: | 176 public: |
| 28 // base::trace_event::TracingAgent overrides: | 177 // base::trace_event::TracingAgent overrides: |
| 29 std::string GetTracingAgentName() override { return kArcTracingAgentName; } | 178 std::string GetTracingAgentName() override { return kArcTracingAgentName; } |
| 30 | 179 |
| 31 std::string GetTraceEventLabel() override { return kArcTraceLabel; } | 180 std::string GetTraceEventLabel() override { return kArcTraceLabel; } |
| 32 | 181 |
| 33 void StartAgentTracing(const base::trace_event::TraceConfig& trace_config, | 182 void StartAgentTracing(const base::trace_event::TraceConfig& trace_config, |
| 34 const StartAgentTracingCallback& callback) override { | 183 const StartAgentTracingCallback& callback) override { |
| 35 DCHECK(thread_checker_.CalledOnValidThread()); | 184 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 185 | |
| 36 // delegate_ may be nullptr if ARC is not enabled on the system. In such | 186 // delegate_ may be nullptr if ARC is not enabled on the system. In such |
| 37 // case, simply do nothing. | 187 // case, simply do nothing. |
| 38 if (!delegate_) { | 188 bool success = delegate_ != nullptr; |
| 189 | |
| 190 base::ScopedFD write_fd, read_fd; | |
| 191 success = success && CreateSocketPair(&read_fd, &write_fd); | |
| 192 | |
| 193 if (!success) { | |
| 39 // Use PostTask as the convention of TracingAgent. The caller expects | 194 // Use PostTask as the convention of TracingAgent. The caller expects |
| 40 // callback to be called after this function returns. | 195 // callback to be called after this function returns. |
| 41 base::ThreadTaskRunnerHandle::Get()->PostTask( | 196 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 42 FROM_HERE, base::Bind(callback, GetTracingAgentName(), false)); | 197 FROM_HERE, base::Bind(callback, GetTracingAgentName(), false)); |
| 43 return; | 198 return; |
| 44 } | 199 } |
| 45 | 200 |
| 46 delegate_->StartTracing(trace_config, | 201 BrowserThread::PostTask( |
| 202 BrowserThread::IO, FROM_HERE, | |
| 203 base::Bind(&ArcTracingReader::StartTracing, reader_->GetWeakPtr(), | |
| 204 base::Passed(std::move(read_fd)))); | |
|
dcheng
2017/04/06 06:14:14
Nit: base::Passed(&read_fd)
shunhsingou
2017/04/06 08:42:28
Done.
| |
| 205 | |
| 206 delegate_->StartTracing(trace_config, std::move(write_fd), | |
| 47 base::Bind(callback, GetTracingAgentName())); | 207 base::Bind(callback, GetTracingAgentName())); |
| 48 } | 208 } |
| 49 | 209 |
| 50 void StopAgentTracing(const StopAgentTracingCallback& callback) override { | 210 void StopAgentTracing(const StopAgentTracingCallback& callback) override { |
| 51 DCHECK(thread_checker_.CalledOnValidThread()); | 211 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 52 if (delegate_) | |
| 53 delegate_->StopTracing(base::Bind(OnStopTracing)); | |
| 54 | 212 |
| 55 // Trace data is collect via systrace (debugd) in dev-mode. Simply | 213 if (is_stopping_) { |
| 56 // return empty data here. | 214 DLOG(WARNING) << "Already working on stopping ArcTracingAgent."; |
| 57 std::string no_data; | 215 return; |
| 58 base::ThreadTaskRunnerHandle::Get()->PostTask( | 216 } |
| 59 FROM_HERE, | 217 is_stopping_ = true; |
| 60 base::Bind(callback, GetTracingAgentName(), GetTraceEventLabel(), | 218 if (delegate_) { |
| 61 base::RefCountedString::TakeString(&no_data))); | 219 delegate_->StopTracing( |
| 220 base::Bind(&ArcTracingAgentImpl::OnArcTracingStopped, | |
| 221 weak_ptr_factory_.GetWeakPtr(), callback)); | |
| 222 } | |
| 62 } | 223 } |
| 63 | 224 |
| 64 // ArcTracingAgent overrides: | 225 // ArcTracingAgent overrides: |
| 65 void SetDelegate(Delegate* delegate) override { | 226 void SetDelegate(Delegate* delegate) override { |
| 66 DCHECK(thread_checker_.CalledOnValidThread()); | 227 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 67 delegate_ = delegate; | 228 delegate_ = delegate; |
| 68 } | 229 } |
| 69 | 230 |
| 70 static ArcTracingAgentImpl* GetInstance() { | 231 static ArcTracingAgentImpl* GetInstance() { |
| 71 return base::Singleton<ArcTracingAgentImpl>::get(); | 232 return base::Singleton<ArcTracingAgentImpl>::get(); |
| 72 } | 233 } |
| 73 | 234 |
| 74 private: | 235 private: |
| 75 // This allows constructor and destructor to be private and usable only | 236 // This allows constructor and destructor to be private and usable only |
| 76 // by the Singleton class. | 237 // by the Singleton class. |
| 77 friend struct base::DefaultSingletonTraits<ArcTracingAgentImpl>; | 238 friend struct base::DefaultSingletonTraits<ArcTracingAgentImpl>; |
| 78 | 239 |
| 79 ArcTracingAgentImpl() = default; | 240 ArcTracingAgentImpl() |
| 241 : reader_(base::MakeUnique<ArcTracingReader>()), | |
| 242 weak_ptr_factory_(this) {} | |
| 243 | |
| 80 ~ArcTracingAgentImpl() override = default; | 244 ~ArcTracingAgentImpl() override = default; |
| 81 | 245 |
| 246 void OnArcTracingStopped(const StopAgentTracingCallback& callback, | |
| 247 bool success) { | |
| 248 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 249 if (!success) { | |
| 250 DLOG(WARNING) << "Failed to stop ARC tracing."; | |
| 251 std::string no_data; | |
| 252 callback.Run(GetTracingAgentName(), GetTraceEventLabel(), | |
| 253 base::RefCountedString::TakeString(&no_data)); | |
|
dcheng
2017/04/06 06:14:14
A default constructed base::RefCountedString is th
shunhsingou
2017/04/06 08:42:28
Done.
| |
| 254 is_stopping_ = false; | |
| 255 return; | |
| 256 } | |
| 257 BrowserThread::PostTask( | |
| 258 BrowserThread::IO, FROM_HERE, | |
| 259 base::Bind(&ArcTracingReader::StopTracing, reader_->GetWeakPtr(), | |
| 260 base::Bind(&ArcTracingAgentImpl::OnTracingReaderStopped, | |
| 261 weak_ptr_factory_.GetWeakPtr(), callback))); | |
| 262 } | |
| 263 | |
| 264 void OnTracingReaderStopped( | |
| 265 const StopAgentTracingCallback& callback, | |
| 266 const scoped_refptr<base::RefCountedString>& data) { | |
| 267 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 268 callback.Run(GetTracingAgentName(), GetTraceEventLabel(), data); | |
| 269 is_stopping_ = false; | |
| 270 } | |
| 271 | |
| 82 Delegate* delegate_ = nullptr; // Owned by ArcServiceLauncher. | 272 Delegate* delegate_ = nullptr; // Owned by ArcServiceLauncher. |
| 83 base::ThreadChecker thread_checker_; | 273 std::unique_ptr<ArcTracingReader> reader_; |
| 274 bool is_stopping_ = false; | |
| 275 // NOTE: Weak pointers must be invalidated before all other member variables | |
| 276 // so it must be the last member. | |
| 277 base::WeakPtrFactory<ArcTracingAgentImpl> weak_ptr_factory_; | |
| 84 | 278 |
| 85 DISALLOW_COPY_AND_ASSIGN(ArcTracingAgentImpl); | 279 DISALLOW_COPY_AND_ASSIGN(ArcTracingAgentImpl); |
| 86 }; | 280 }; |
| 87 | 281 |
| 88 } // namespace | 282 } // namespace |
| 89 | 283 |
| 90 // static | 284 // static |
| 91 ArcTracingAgent* ArcTracingAgent::GetInstance() { | 285 ArcTracingAgent* ArcTracingAgent::GetInstance() { |
| 92 return ArcTracingAgentImpl::GetInstance(); | 286 return ArcTracingAgentImpl::GetInstance(); |
| 93 } | 287 } |
| 94 | 288 |
| 95 ArcTracingAgent::ArcTracingAgent() = default; | 289 ArcTracingAgent::ArcTracingAgent() = default; |
| 96 ArcTracingAgent::~ArcTracingAgent() = default; | 290 ArcTracingAgent::~ArcTracingAgent() = default; |
| 97 | 291 |
| 98 ArcTracingAgent::Delegate::~Delegate() = default; | 292 ArcTracingAgent::Delegate::~Delegate() = default; |
| 99 | 293 |
| 100 } // namespace content | 294 } // namespace content |
| OLD | NEW |