process_info.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright 2015 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. #ifndef CRASHPAD_UTIL_WIN_PROCESS_INFO_H_
  15. #define CRASHPAD_UTIL_WIN_PROCESS_INFO_H_
  16. #include <windows.h>
  17. #include <sys/types.h>
  18. #include <string>
  19. #include <vector>
  20. #include "base/macros.h"
  21. #include "util/misc/initialization_state_dcheck.h"
  22. #include "util/numeric/checked_range.h"
  23. #include "util/stdlib/aligned_allocator.h"
  24. #include "util/win/address_types.h"
  25. namespace crashpad {
  26. //! \brief Gathers information about a process given its `HANDLE`. This consists
  27. //! primarily of information stored in the Process Environment Block.
  28. class ProcessInfo {
  29. public:
  30. //! \brief The return type of MemoryInfo(), for convenience.
  31. using MemoryBasicInformation64Vector =
  32. AlignedVector<MEMORY_BASIC_INFORMATION64>;
  33. //! \brief Contains information about a module loaded into a process.
  34. struct Module {
  35. Module();
  36. ~Module();
  37. //! \brief The pathname used to load the module from disk.
  38. std::wstring name;
  39. //! \brief The base address of the loaded DLL.
  40. WinVMAddress dll_base;
  41. //! \brief The size of the module.
  42. WinVMSize size;
  43. //! \brief The module's timestamp.
  44. time_t timestamp;
  45. };
  46. struct Handle {
  47. Handle();
  48. ~Handle();
  49. //! \brief A string representation of the handle's type.
  50. std::wstring type_name;
  51. //! \brief The handle's value.
  52. int handle;
  53. //! \brief The attributes for the handle, e.g. `OBJ_INHERIT`,
  54. //! `OBJ_CASE_INSENSITIVE`, etc.
  55. uint32_t attributes;
  56. //! \brief The `ACCESS_MASK` for the handle in this process.
  57. //!
  58. //! See
  59. //! http://blogs.msdn.com/b/openspecification/archive/2010/04/01/about-the-access-mask-structure.aspx
  60. //! for more information.
  61. uint32_t granted_access;
  62. //! \brief The number of kernel references to the object that this handle
  63. //! refers to.
  64. uint32_t pointer_count;
  65. //! \brief The number of open handles to the object that this handle refers
  66. //! to.
  67. uint32_t handle_count;
  68. };
  69. ProcessInfo();
  70. ~ProcessInfo();
  71. //! \brief Initializes this object with information about the given
  72. //! \a process.
  73. //!
  74. //! This method must be called successfully prior to calling any other
  75. //! method in this class. This method may only be called once.
  76. //!
  77. //! \return `true` on success, `false` on failure with a message logged.
  78. bool Initialize(HANDLE process);
  79. //! \return `true` if the target process is a 64-bit process.
  80. bool Is64Bit() const;
  81. //! \return `true` if the target process is running on the Win32-on-Win64
  82. //! subsystem.
  83. bool IsWow64() const;
  84. //! \return The target process's process ID.
  85. pid_t ProcessID() const;
  86. //! \return The target process's parent process ID.
  87. pid_t ParentProcessID() const;
  88. //! \return The command line from the target process's Process Environment
  89. //! Block.
  90. bool CommandLine(std::wstring* command_line) const;
  91. //! \brief Gets the address and size of the process's Process Environment
  92. //! Block.
  93. //!
  94. //! \param[out] peb_address The address of the Process Environment Block.
  95. //! \param[out] peb_size The size of the Process Environment Block.
  96. void Peb(WinVMAddress* peb_address, WinVMSize* peb_size) const;
  97. //! \brief Retrieves the modules loaded into the target process.
  98. //!
  99. //! The modules are enumerated in initialization order as detailed in the
  100. //! Process Environment Block. The main executable will always be the
  101. //! first element.
  102. bool Modules(std::vector<Module>* modules) const;
  103. //! \brief Retrieves information about all pages mapped into the process.
  104. const MemoryBasicInformation64Vector& MemoryInfo() const;
  105. //! \brief Given a range to be read from the target process, returns a vector
  106. //! of ranges, representing the readable portions of the original range.
  107. //!
  108. //! \param[in] range The range being identified.
  109. //!
  110. //! \return A vector of ranges corresponding to the portion of \a range that
  111. //! is readable based on the memory map.
  112. std::vector<CheckedRange<WinVMAddress, WinVMSize>> GetReadableRanges(
  113. const CheckedRange<WinVMAddress, WinVMSize>& range) const;
  114. //! \brief Given a range in the target process, determines if the entire range
  115. //! is readable.
  116. //!
  117. //! \param[in] range The range being inspected.
  118. //!
  119. //! \return `true` if the range is fully readable, otherwise `false` with a
  120. //! message logged.
  121. bool LoggingRangeIsFullyReadable(
  122. const CheckedRange<WinVMAddress, WinVMSize>& range) const;
  123. //! \brief Retrieves information about open handles in the target process.
  124. const std::vector<Handle>& Handles() const;
  125. private:
  126. template <class Traits>
  127. friend bool GetProcessBasicInformation(HANDLE process,
  128. bool is_wow64,
  129. ProcessInfo* process_info,
  130. WinVMAddress* peb_address,
  131. WinVMSize* peb_size);
  132. template <class Traits>
  133. friend bool ReadProcessData(HANDLE process,
  134. WinVMAddress peb_address_vmaddr,
  135. ProcessInfo* process_info);
  136. friend bool ReadMemoryInfo(HANDLE process,
  137. bool is_64_bit,
  138. ProcessInfo* process_info);
  139. // This function is best-effort under low memory conditions.
  140. std::vector<Handle> BuildHandleVector(HANDLE process) const;
  141. pid_t process_id_;
  142. pid_t inherited_from_process_id_;
  143. HANDLE process_;
  144. std::wstring command_line_;
  145. WinVMAddress peb_address_;
  146. WinVMSize peb_size_;
  147. std::vector<Module> modules_;
  148. // memory_info_ is a MemoryBasicInformation64Vector instead of a
  149. // std::vector<MEMORY_BASIC_INFORMATION64> because MEMORY_BASIC_INFORMATION64
  150. // is declared with __declspec(align(16)), but std::vector<> does not maintain
  151. // this alignment on 32-bit x86. clang-cl (but not MSVC cl) takes advantage of
  152. // the presumed alignment and emits SSE instructions that require aligned
  153. // storage. clang-cl should relax (unfortunately), but in the mean time, this
  154. // provides aligned storage. See https://crbug.com/564691 and
  155. // http://llvm.org/PR25779.
  156. //
  157. // TODO(mark): Remove this workaround when http://llvm.org/PR25779 is fixed
  158. // and the fix is present in the clang-cl that compiles this code.
  159. MemoryBasicInformation64Vector memory_info_;
  160. // Handles() is logically const, but updates this member on first retrieval.
  161. // See https://crashpad.chromium.org/bug/9.
  162. mutable std::vector<Handle> handles_;
  163. bool is_64_bit_;
  164. bool is_wow64_;
  165. InitializationStateDcheck initialized_;
  166. DISALLOW_COPY_AND_ASSIGN(ProcessInfo);
  167. };
  168. //! \brief Given a memory map of a process, and a range to be read from the
  169. //! target process, returns a vector of ranges, representing the readable
  170. //! portions of the original range.
  171. //!
  172. //! This is a free function for testing, but prefer
  173. //! ProcessInfo::GetReadableRanges().
  174. std::vector<CheckedRange<WinVMAddress, WinVMSize>> GetReadableRangesOfMemoryMap(
  175. const CheckedRange<WinVMAddress, WinVMSize>& range,
  176. const ProcessInfo::MemoryBasicInformation64Vector& memory_info);
  177. } // namespace crashpad
  178. #endif // CRASHPAD_UTIL_WIN_PROCESS_INFO_H_