1 // Copyright 2014 The Crashpad Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef CRASHPAD_SNAPSHOT_TEST_TEST_PROCESS_SNAPSHOT_H_
16 #define CRASHPAD_SNAPSHOT_TEST_TEST_PROCESS_SNAPSHOT_H_
17 
18 #include <stdint.h>
19 #include <sys/time.h>
20 #include <sys/types.h>
21 
22 #include <map>
23 #include <memory>
24 #include <string>
25 #include <utility>
26 #include <vector>
27 
28 #include "base/macros.h"
29 #include "snapshot/exception_snapshot.h"
30 #include "snapshot/memory_map_region_snapshot.h"
31 #include "snapshot/memory_snapshot.h"
32 #include "snapshot/module_snapshot.h"
33 #include "snapshot/process_snapshot.h"
34 #include "snapshot/system_snapshot.h"
35 #include "snapshot/thread_snapshot.h"
36 #include "snapshot/unloaded_module_snapshot.h"
37 #include "util/misc/uuid.h"
38 #include "util/process/process_id.h"
39 #include "util/process/process_memory.h"
40 
41 namespace crashpad {
42 namespace test {
43 
44 //! \brief A test ProcessSnapshot that can carry arbitrary data for testing
45 //!     purposes.
46 class TestProcessSnapshot final : public ProcessSnapshot {
47  public:
48   TestProcessSnapshot();
49   ~TestProcessSnapshot() override;
50 
SetProcessID(crashpad::ProcessID process_id)51   void SetProcessID(crashpad::ProcessID process_id) {
52     process_id_ = process_id;
53   }
SetParentProcessID(crashpad::ProcessID parent_process_id)54   void SetParentProcessID(crashpad::ProcessID parent_process_id) {
55     parent_process_id_ = parent_process_id;
56   }
SetSnapshotTime(const timeval & snapshot_time)57   void SetSnapshotTime(const timeval& snapshot_time) {
58     snapshot_time_ = snapshot_time;
59   }
SetProcessStartTime(const timeval & start_time)60   void SetProcessStartTime(const timeval& start_time) {
61     process_start_time_ = start_time;
62   }
SetProcessCPUTimes(const timeval & user_time,const timeval & system_time)63   void SetProcessCPUTimes(const timeval& user_time,
64                           const timeval& system_time) {
65     process_cpu_user_time_ = user_time;
66     process_cpu_system_time_ = system_time;
67   }
SetReportID(const UUID & report_id)68   void SetReportID(const UUID& report_id) { report_id_ = report_id; }
SetClientID(const UUID & client_id)69   void SetClientID(const UUID& client_id) { client_id_ = client_id; }
SetAnnotationsSimpleMap(const std::map<std::string,std::string> & annotations_simple_map)70   void SetAnnotationsSimpleMap(
71       const std::map<std::string, std::string>& annotations_simple_map) {
72     annotations_simple_map_ = annotations_simple_map;
73   }
74 
75   //! \brief Sets the system snapshot to be returned by System().
76   //!
77   //! \param[in] system The system snapshot that System() will return. The
78   //!     TestProcessSnapshot object takes ownership of \a system.
SetSystem(std::unique_ptr<SystemSnapshot> system)79   void SetSystem(std::unique_ptr<SystemSnapshot> system) {
80     system_ = std::move(system);
81   }
82 
83   //! \brief Adds a thread snapshot to be returned by Threads().
84   //!
85   //! \param[in] thread The thread snapshot that will be included in Threads().
86   //!     The TestProcessSnapshot object takes ownership of \a thread.
AddThread(std::unique_ptr<ThreadSnapshot> thread)87   void AddThread(std::unique_ptr<ThreadSnapshot> thread) {
88     threads_.push_back(std::move(thread));
89   }
90 
91   //! \brief Adds a module snapshot to be returned by Modules().
92   //!
93   //! \param[in] module The module snapshot that will be included in Modules().
94   //!     The TestProcessSnapshot object takes ownership of \a module.
AddModule(std::unique_ptr<ModuleSnapshot> module)95   void AddModule(std::unique_ptr<ModuleSnapshot> module) {
96     modules_.push_back(std::move(module));
97   }
98 
99   //! \brief Adds an unloaded module snapshot to be returned by
100   //!     UnloadedModules().
101   //!
102   //! \param[in] unloaded_module The unloaded module snapshot that will be
103   //!     included in UnloadedModules().
AddModule(const UnloadedModuleSnapshot & unloaded_module)104   void AddModule(const UnloadedModuleSnapshot& unloaded_module) {
105     unloaded_modules_.push_back(unloaded_module);
106   }
107 
108   //! \brief Sets the exception snapshot to be returned by Exception().
109   //!
110   //! \param[in] exception The exception snapshot that Exception() will return.
111   //!     The TestProcessSnapshot object takes ownership of \a exception.
SetException(std::unique_ptr<ExceptionSnapshot> exception)112   void SetException(std::unique_ptr<ExceptionSnapshot> exception) {
113     exception_ = std::move(exception);
114   }
115 
116   //! \brief Adds a memory map region snapshot to be returned by MemoryMap().
117   //!
118   //! \param[in] region The memory map region snapshot that will be included in
119   //!     MemoryMap(). The TestProcessSnapshot object takes ownership of \a
120   //!     region.
AddMemoryMapRegion(std::unique_ptr<MemoryMapRegionSnapshot> region)121   void AddMemoryMapRegion(std::unique_ptr<MemoryMapRegionSnapshot> region) {
122     memory_map_.push_back(std::move(region));
123   }
124 
125   //! \brief Adds a handle snapshot to be returned by Handles().
126   //!
127   //! \param[in] handle The handle snapshot that will be included in Handles().
AddHandle(const HandleSnapshot & handle)128   void AddHandle(const HandleSnapshot& handle) {
129     handles_.push_back(handle);
130   }
131 
132   //! \brief Add a memory snapshot to be returned by ExtraMemory().
133   //!
134   //! \param[in] extra_memory The memory snapshot that will be included in
135   //!     ExtraMemory(). The TestProcessSnapshot object takes ownership of \a
136   //!     extra_memory.
AddExtraMemory(std::unique_ptr<MemorySnapshot> extra_memory)137   void AddExtraMemory(std::unique_ptr<MemorySnapshot> extra_memory) {
138     extra_memory_.push_back(std::move(extra_memory));
139   }
140 
141   //! \brief Add a process memory object to be returned by Memory().
142   //!
143   //! \param[in] process_memory The memory object that will be returned by
144   //!     Memory(). The TestProcessSnapshot object takes ownership of \a
145   //!     extra_memory.
SetProcessMemory(std::unique_ptr<ProcessMemory> process_memory)146   void SetProcessMemory(std::unique_ptr<ProcessMemory> process_memory) {
147     process_memory_ = std::move(process_memory);
148   }
149 
150   // ProcessSnapshot:
151 
152   crashpad::ProcessID ProcessID() const override;
153   crashpad::ProcessID ParentProcessID() const override;
154   void SnapshotTime(timeval* snapshot_time) const override;
155   void ProcessStartTime(timeval* start_time) const override;
156   void ProcessCPUTimes(timeval* user_time, timeval* system_time) const override;
157   void ReportID(UUID* report_id) const override;
158   void ClientID(UUID* client_id) const override;
159   const std::map<std::string, std::string>& AnnotationsSimpleMap()
160       const override;
161   const SystemSnapshot* System() const override;
162   std::vector<const ThreadSnapshot*> Threads() const override;
163   std::vector<const ModuleSnapshot*> Modules() const override;
164   std::vector<UnloadedModuleSnapshot> UnloadedModules() const override;
165   const ExceptionSnapshot* Exception() const override;
166   std::vector<const MemoryMapRegionSnapshot*> MemoryMap() const override;
167   std::vector<HandleSnapshot> Handles() const override;
168   std::vector<const MemorySnapshot*> ExtraMemory() const override;
169   const ProcessMemory* Memory() const override;
170 
171  private:
172   crashpad::ProcessID process_id_;
173   crashpad::ProcessID parent_process_id_;
174   timeval snapshot_time_;
175   timeval process_start_time_;
176   timeval process_cpu_user_time_;
177   timeval process_cpu_system_time_;
178   UUID report_id_;
179   UUID client_id_;
180   std::map<std::string, std::string> annotations_simple_map_;
181   std::unique_ptr<SystemSnapshot> system_;
182   std::vector<std::unique_ptr<ThreadSnapshot>> threads_;
183   std::vector<std::unique_ptr<ModuleSnapshot>> modules_;
184   std::vector<UnloadedModuleSnapshot> unloaded_modules_;
185   std::unique_ptr<ExceptionSnapshot> exception_;
186   std::vector<std::unique_ptr<MemoryMapRegionSnapshot>> memory_map_;
187   std::vector<HandleSnapshot> handles_;
188   std::vector<std::unique_ptr<MemorySnapshot>> extra_memory_;
189   std::unique_ptr<ProcessMemory> process_memory_;
190 
191   DISALLOW_COPY_AND_ASSIGN(TestProcessSnapshot);
192 };
193 
194 }  // namespace test
195 }  // namespace crashpad
196 
197 #endif  // CRASHPAD_SNAPSHOT_TEST_TEST_PROCESS_SNAPSHOT_H_
198