1 // Copyright 2017 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_FUCHSIA_PROCESS_SNAPSHOT_FUCHSIA_H_
16 #define CRASHPAD_SNAPSHOT_FUCHSIA_PROCESS_SNAPSHOT_FUCHSIA_H_
17 
18 #include <lib/zx/process.h>
19 #include <sys/time.h>
20 #include <zircon/syscalls/exception.h>
21 #include <zircon/types.h>
22 
23 #include <memory>
24 #include <vector>
25 
26 #include "base/macros.h"
27 #include "snapshot/crashpad_info_client_options.h"
28 #include "snapshot/elf/elf_image_reader.h"
29 #include "snapshot/elf/module_snapshot_elf.h"
30 #include "snapshot/fuchsia/exception_snapshot_fuchsia.h"
31 #include "snapshot/fuchsia/memory_map_region_snapshot_fuchsia.h"
32 #include "snapshot/fuchsia/process_reader_fuchsia.h"
33 #include "snapshot/fuchsia/system_snapshot_fuchsia.h"
34 #include "snapshot/fuchsia/thread_snapshot_fuchsia.h"
35 #include "snapshot/process_snapshot.h"
36 #include "snapshot/unloaded_module_snapshot.h"
37 #include "util/misc/initialization_state_dcheck.h"
38 #include "util/process/process_id.h"
39 #include "util/process/process_memory_range.h"
40 
41 namespace crashpad {
42 
43 //! \brief A ProcessSnapshot of a running (or crashed) process running on a
44 //!     Fuchsia system. This class is not yet implemented.
45 class ProcessSnapshotFuchsia : public ProcessSnapshot {
46  public:
47   ProcessSnapshotFuchsia();
48   ~ProcessSnapshotFuchsia() override;
49 
50   //! \brief Initializes the object.
51   //!
52   //! \param[in] process The process handle to create a snapshot from.
53   //!
54   //! \return `true` if the snapshot could be created, `false` otherwise with
55   //!     an appropriate message logged.
56   bool Initialize(const zx::process& process);
57 
58   //! \brief Initializes the object's exception.
59   //!
60   //! This populates the data to be returned by Exception(). The thread
61   //! identified by \a thread_id must be in an exception.
62   //!
63   //! This method must not be called until after a successful call to
64   //! Initialize().
65   //!
66   //! \param[in] thread_id Koid of the thread which sustained the exception.
67   //! \param[in] report The `zx_exception_report_t` for the thread which
68   //!     sustained the exception.
69   //! \return `true` if the exception information could be initialized, `false`
70   //!     otherwise with an appropriate message logged. When this method returns
71   //!     `false`, the ProcessSnapshotFuchsia object’s validity remains
72   //!     unchanged.
73   bool InitializeException(zx_koid_t thread_id,
74                            const zx_exception_report_t& report);
75 
76   //! \brief Returns options from CrashpadInfo structures found in modules in
77   //!     the process.
78   //!
79   //! \param[out] options Options set in CrashpadInfo structures in modules in
80   //!     the process.
81   void GetCrashpadOptions(CrashpadInfoClientOptions* options);
82 
83   //! \brief Sets the value to be returned by ReportID().
84   //!
85   //! On Fuchsia, the crash report ID is under the control of the snapshot
86   //! producer, which may call this method to set the report ID. If this is not
87   //! done, ReportID() will return an identifier consisting entirely of zeroes.
SetReportID(const UUID & report_id)88   void SetReportID(const UUID& report_id) { report_id_ = report_id; }
89 
90   //! \brief Sets the value to be returned by ClientID().
91   //!
92   //! On Fuchsia, the client ID is under the control of the snapshot producer,
93   //! which may call this method to set the client ID. If this is not done,
94   //! ClientID() will return an identifier consisting entirely of zeroes.
SetClientID(const UUID & client_id)95   void SetClientID(const UUID& client_id) { client_id_ = client_id; }
96 
97   //! \brief Sets the value to be returned by AnnotationsSimpleMap().
98   //!
99   //! On Fuchsia, all process annotations are under the control of the snapshot
100   //! producer, which may call this method to establish these annotations.
101   //! Contrast this with module annotations, which are under the control of the
102   //! process being snapshotted.
SetAnnotationsSimpleMap(const std::map<std::string,std::string> & annotations_simple_map)103   void SetAnnotationsSimpleMap(
104       const std::map<std::string, std::string>& annotations_simple_map) {
105     annotations_simple_map_ = annotations_simple_map;
106   }
107 
108   // ProcessSnapshot:
109   crashpad::ProcessID ProcessID() const override;
110   crashpad::ProcessID ParentProcessID() const override;
111   void SnapshotTime(timeval* snapshot_time) const override;
112   void ProcessStartTime(timeval* start_time) const override;
113   void ProcessCPUTimes(timeval* user_time, timeval* system_time) const override;
114   void ReportID(UUID* report_id) const override;
115   void ClientID(UUID* client_id) const override;
116   const std::map<std::string, std::string>& AnnotationsSimpleMap()
117       const override;
118   const SystemSnapshot* System() const override;
119   std::vector<const ThreadSnapshot*> Threads() const override;
120   std::vector<const ModuleSnapshot*> Modules() const override;
121   std::vector<UnloadedModuleSnapshot> UnloadedModules() const override;
122   const ExceptionSnapshot* Exception() const override;
123   std::vector<const MemoryMapRegionSnapshot*> MemoryMap() const override;
124   std::vector<HandleSnapshot> Handles() const override;
125   std::vector<const MemorySnapshot*> ExtraMemory() const override;
126   const ProcessMemory* Memory() const override;
127 
128  private:
129   // Initializes threads_ on behalf of Initialize().
130   void InitializeThreads();
131 
132   // Initializes modules_ on behalf of Initialize().
133   void InitializeModules();
134 
135   internal::SystemSnapshotFuchsia system_;
136   std::vector<std::unique_ptr<internal::ThreadSnapshotFuchsia>> threads_;
137   std::vector<std::unique_ptr<internal::ModuleSnapshotElf>> modules_;
138   std::unique_ptr<internal::ExceptionSnapshotFuchsia> exception_;
139   ProcessReaderFuchsia process_reader_;
140   ProcessMemoryRange memory_range_;
141   std::map<std::string, std::string> annotations_simple_map_;
142   std::vector<std::unique_ptr<internal::MemoryMapRegionSnapshotFuchsia>>
143       memory_map_;
144   UUID report_id_;
145   UUID client_id_;
146   timeval snapshot_time_;
147   InitializationStateDcheck initialized_;
148 
149   DISALLOW_COPY_AND_ASSIGN(ProcessSnapshotFuchsia);
150 };
151 
152 }  // namespace crashpad
153 
154 #endif  // CRASHPAD_SNAPSHOT_FUCHSIA_PROCESS_SNAPSHOT_FUCHSIA_H_
155