| OLD | NEW |
| (Empty) | |
| 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 |
| 15 #ifndef CRASHPAD_SNAPSHOT_WIN_PE_IMAGE_READER_H_ |
| 16 #define CRASHPAD_SNAPSHOT_WIN_PE_IMAGE_READER_H_ |
| 17 |
| 18 #include <windows.h> |
| 19 |
| 20 #include <string> |
| 21 |
| 22 #include "base/basictypes.h" |
| 23 #include "util/misc/initialization_state_dcheck.h" |
| 24 #include "util/misc/uuid.h" |
| 25 #include "util/win/address_types.h" |
| 26 #include "util/win/checked_win_address_range.h" |
| 27 #include "util/win/process_structs.h" |
| 28 |
| 29 namespace crashpad { |
| 30 |
| 31 class ProcessReaderWin; |
| 32 |
| 33 namespace process_types { |
| 34 |
| 35 template <class Traits> |
| 36 struct CrashpadInfo { |
| 37 uint32_t signature; |
| 38 uint32_t size; |
| 39 uint32_t version; |
| 40 uint8_t crashpad_handler_behavior; // TriState. |
| 41 uint8_t system_crash_reporter_forwarding; // TriState. |
| 42 uint16_t padding_0; |
| 43 typename Traits::Pointer simple_annotations; |
| 44 }; |
| 45 |
| 46 } // namespace process_types |
| 47 |
| 48 //! \brief A reader for PE images mapped into another process. |
| 49 //! |
| 50 //! This class is capable of reading both 32-bit and 64-bit images based on the |
| 51 //! bitness of the remote process. |
| 52 //! |
| 53 //! \sa PEImageAnnotationsReader |
| 54 class PEImageReader { |
| 55 public: |
| 56 PEImageReader(); |
| 57 ~PEImageReader(); |
| 58 |
| 59 //! \brief Initializes the reader. |
| 60 //! |
| 61 //! This method must be called only once on an object. This method must be |
| 62 //! called successfully before any other method in this class may be called. |
| 63 //! |
| 64 //! \param[in] process_reader The reader for the remote process. |
| 65 //! \param[in] address The address, in the remote process' address space, |
| 66 //! where the `IMAGE_DOS_HEADER` is located. |
| 67 //! \param[in] size The size of the image. |
| 68 //! \param[in] name The module's name, a string to be used in logged messages. |
| 69 //! This string is for diagnostic purposes. |
| 70 //! |
| 71 //! \return `true` if the image was read successfully, `false` otherwise, with |
| 72 //! an appropriate message logged. |
| 73 bool Initialize(ProcessReaderWin* process_reader, |
| 74 WinVMAddress address, |
| 75 WinVMSize size, |
| 76 const std::string& module_name); |
| 77 |
| 78 //! \brief Returns the image's load address. |
| 79 //! |
| 80 //! This is the value passed as \a address to Initialize(). |
| 81 WinVMAddress Address() const { return module_range_.Base(); } |
| 82 |
| 83 //! \brief Returns the image's size. |
| 84 //! |
| 85 //! This is the value passed as \a size to Initialize(). |
| 86 WinVMSize Size() const { return module_range_.Size(); } |
| 87 |
| 88 //! \brief Obtains the module's CrashpadInfo structure. |
| 89 //! |
| 90 //! \return `true` on success, `false` on failure. If the module does not have |
| 91 //! a `CPADinfo` section, this will return `false` without logging any |
| 92 //! messages. Other failures will result in messages being logged. |
| 93 template <class Traits> |
| 94 bool GetCrashpadInfo( |
| 95 process_types::CrashpadInfo<Traits>* crashpad_info) const; |
| 96 |
| 97 //! \brief Obtains information from the module's debug directory, if any. |
| 98 //! |
| 99 //! \param[out] uuid The unique identifier of the executable/PDB. |
| 100 //! \param[out] age The age field for the pdb (the number of times it's been |
| 101 //! relinked). |
| 102 //! \param[out] pdbname Name of the pdb file. |
| 103 //! \return `true` on success, or `false` if the module has no debug directory |
| 104 //! entry. |
| 105 bool DebugDirectoryInformation(UUID* uuid, |
| 106 DWORD* age, |
| 107 std::string* pdbname) const; |
| 108 |
| 109 private: |
| 110 //! \brief Implementation helper for DebugDirectoryInformation() templated by |
| 111 //! `IMAGE_NT_HEADERS` type for different bitnesses. |
| 112 //! |
| 113 //! \return `true` on success, with the parameters set appropriately. `false` |
| 114 //! on failure. This method may return `false` without logging anything in |
| 115 //! the case of a module that does not contain relevant debugging |
| 116 //! information but is otherwise properly structured. |
| 117 template <class NtHeadersType> |
| 118 bool ReadDebugDirectoryInformation(UUID* uuid, |
| 119 DWORD* age, |
| 120 std::string* pdbname) const; |
| 121 |
| 122 //! \brief Reads the `IMAGE_NT_HEADERS` from the beginning of the image. |
| 123 //! |
| 124 //! \param[out] nt_headers The contents of the templated NtHeadersType |
| 125 //! structure read from the remote process. |
| 126 //! \param[out] nt_headers_address The address of the templated NtHeadersType |
| 127 //! structure in the remote process’ address space. If this information is |
| 128 //! not needed, this parameter may be `nullptr`. |
| 129 //! |
| 130 //! \return `true` on success, with \a nt_headers and optionally \a |
| 131 //! nt_headers_address set appropriately. `false` on failure, with a |
| 132 //! message logged. |
| 133 template <class NtHeadersType> |
| 134 bool ReadNtHeaders(NtHeadersType* nt_headers, |
| 135 WinVMAddress* nt_headers_address) const; |
| 136 |
| 137 //! \brief Finds a given section by name in the image. |
| 138 template <class NtHeadersType> |
| 139 bool GetSectionByName(const std::string& name, |
| 140 IMAGE_SECTION_HEADER* section) const; |
| 141 |
| 142 //! \brief Reads memory from target process, first checking whether the range |
| 143 //! requested falls inside module_range_. |
| 144 //! |
| 145 //! \return `true` on success, with \a into filled out, otherwise `false` and |
| 146 //! a message will be logged. |
| 147 bool CheckedReadMemory(WinVMAddress address, |
| 148 WinVMSize size, |
| 149 void* into) const; |
| 150 |
| 151 ProcessReaderWin* process_reader_; // weak |
| 152 CheckedWinAddressRange module_range_; |
| 153 std::string module_name_; |
| 154 InitializationStateDcheck initialized_; |
| 155 |
| 156 DISALLOW_COPY_AND_ASSIGN(PEImageReader); |
| 157 }; |
| 158 |
| 159 } // namespace crashpad |
| 160 |
| 161 #endif // CRASHPAD_SNAPSHOT_WIN_PE_IMAGE_READER_H_ |
| OLD | NEW |