1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// found in the LICENSE file.
4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifndef CONTENT_BROWSER_TRACING_TRACING_CONSUMER_WIN_H_
6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define CONTENT_BROWSER_TRACING_TRACING_CONSUMER_WIN_H_
7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/bind.h"
9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/memory/ref_counted_memory.h"
10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/threading/thread.h"
11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/values.h"
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/win/event_trace_consumer.h"
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/win/event_trace_controller.h"
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)template <typename Type>
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)struct DefaultSingletonTraits;
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace content {
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class EtwSystemEventConsumer :
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    public base::win::EtwTraceConsumerBase<EtwSystemEventConsumer> {
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) public:
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  typedef base::Callback<void(const scoped_refptr<base::RefCountedString>&)>
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      OutputCallback;
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool StartSystemTracing();
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void StopSystemTracing(const OutputCallback& callback);
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Retrieve the ETW consumer instance.
30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static EtwSystemEventConsumer* GetInstance();
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) private:
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // This allows constructor and destructor to be private and usable only
34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // by the Singleton class.
35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  friend struct DefaultSingletonTraits<EtwSystemEventConsumer>;
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Constructor.
38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  EtwSystemEventConsumer();
39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual ~EtwSystemEventConsumer();
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void AddSyncEventToBuffer();
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void AppendEventToBuffer(EVENT_TRACE* event);
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Static override of EtwTraceConsumerBase::ProcessEvent.
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // @param event the raw ETW event to process.
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  friend class base::win::EtwTraceConsumerBase<EtwSystemEventConsumer>;
47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  static void ProcessEvent(EVENT_TRACE* event);
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Request the ETW trace controller to activate the kernel tracing.
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // returns true on success, false if the kernel tracing isn't activated.
51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool StartKernelSessionTracing();
52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Request the ETW trace controller to deactivate the kernel tracing.
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // @param callback the callback to call with the consumed events.
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // @returns true on success, false if an error occurred.
56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool StopKernelSessionTracing();
57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void OnStopSystemTracingDone(
59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const OutputCallback& callback,
60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const scoped_refptr<base::RefCountedString>& result);
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void TraceAndConsumeOnThread();
63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void FlushOnThread(const OutputCallback& callback);
64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  scoped_ptr<base::ListValue> events_;
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::Thread thread_;
67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  TRACEHANDLE session_handle_;
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::win::EtwTraceProperties properties_;
69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(EtwSystemEventConsumer);
71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
73a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}  // namespace content
74a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif  // CONTENT_BROWSER_TRACING_TRACING_CONSUMER_WIN_H_
76