OLD | NEW |
(Empty) | |
| 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_UTIL_MACH_TASK_MEMORY_H_ |
| 16 #define CRASHPAD_UTIL_MACH_TASK_MEMORY_H_ |
| 17 |
| 18 #include <mach/mach.h> |
| 19 |
| 20 #include <string> |
| 21 |
| 22 #include "base/basictypes.h" |
| 23 |
| 24 namespace crashpad { |
| 25 |
| 26 //! \brief Accesses the memory of another Mach task. |
| 27 class TaskMemory { |
| 28 public: |
| 29 //! \param[in] task A send right to the target task’s task port. This object |
| 30 //! does not take ownership of the send right. |
| 31 explicit TaskMemory(mach_port_t task); |
| 32 |
| 33 ~TaskMemory() {} |
| 34 |
| 35 //! \brief Copies memory from the target task into a user-provided buffer in |
| 36 //! the current task. |
| 37 //! |
| 38 //! \param[in] address The address, in the target task’s address space, of the |
| 39 //! memory region to copy. |
| 40 //! \param[in] size The size, in bytes, of the memory region to copy. \a |
| 41 //! buffer must be at least this size. |
| 42 //! \param[out] buffer The buffer into which the contents of the other task’s |
| 43 //! memory will be copied. |
| 44 //! |
| 45 //! \return `true` on success, with \a buffer filled appropriately. `false` on |
| 46 //! failure, with a warning logged. Failures can occur, for example, when |
| 47 //! encountering unmapped or unreadable pages. |
| 48 bool Read(mach_vm_address_t address, size_t size, void* buffer); |
| 49 |
| 50 //! \brief Reads a `NUL`-terminated C string from the target task into a |
| 51 //! string in the current task. |
| 52 //! |
| 53 //! The length of the string need not be known ahead of time. This method will |
| 54 //! read contiguous memory until a `NUL` terminator is found. |
| 55 //! |
| 56 //! \param[in] address The address, in the target task’s address space, of the |
| 57 //! string to copy. |
| 58 //! \param[out] string The string read from the other task. |
| 59 //! |
| 60 //! \return `true` on success, with \a string set appropriately. `false` on |
| 61 //! failure, with a warning logged. Failures can occur, for example, when |
| 62 //! encountering unmapped or unreadable pages. |
| 63 bool ReadCString(mach_vm_address_t address, std::string* string); |
| 64 |
| 65 //! \brief Reads a `NUL`-terminated C string from the target task into a |
| 66 //! string in the current task. |
| 67 //! |
| 68 //! \param[in] address The address, in the target task’s address space, of the |
| 69 //! string to copy. |
| 70 //! \param[in] size The maximum number of bytes to read. The string is |
| 71 //! required to be `NUL`-terminated within this many bytes. |
| 72 //! \param[out] string The string read from the other task. |
| 73 //! |
| 74 //! \return `true` on success, with \a string set appropriately. `false` on |
| 75 //! failure, with a warning logged. Failures can occur, for example, when |
| 76 //! a `NUL` terminator is not found within \a size bytes, or when |
| 77 //! encountering unmapped or unreadable pages. |
| 78 bool ReadCStringSizeLimited(mach_vm_address_t address, |
| 79 mach_vm_size_t size, |
| 80 std::string* string); |
| 81 |
| 82 private: |
| 83 // The common internal implementation shared by the ReadCString*() methods. |
| 84 bool ReadCStringInternal(mach_vm_address_t address, |
| 85 bool has_size, |
| 86 mach_vm_size_t size, |
| 87 std::string* string); |
| 88 |
| 89 mach_port_t task_; // weak |
| 90 |
| 91 DISALLOW_COPY_AND_ASSIGN(TaskMemory); |
| 92 }; |
| 93 |
| 94 } // namespace crashpad |
| 95 |
| 96 #endif // CRASHPAD_UTIL_MACH_TASK_MEMORY_H_ |
OLD | NEW |