| Christopher Ferris | 2c43cff | 2015-03-26 19:18:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2013 The Android Open Source Project | 
 | 3 |  * | 
 | 4 |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
 | 5 |  * you may not use this file except in compliance with the License. | 
 | 6 |  * You may obtain a copy of the License at | 
 | 7 |  * | 
 | 8 |  *      http://www.apache.org/licenses/LICENSE-2.0 | 
 | 9 |  * | 
 | 10 |  * Unless required by applicable law or agreed to in writing, software | 
 | 11 |  * distributed under the License is distributed on an "AS IS" BASIS, | 
 | 12 |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
 | 13 |  * See the License for the specific language governing permissions and | 
 | 14 |  * limitations under the License. | 
 | 15 |  */ | 
 | 16 |  | 
 | 17 | #include <errno.h> | 
 | 18 | #include <stdint.h> | 
 | 19 | #include <string.h> | 
 | 20 | #include <sys/param.h> | 
 | 21 | #include <sys/ptrace.h> | 
 | 22 | #include <sys/types.h> | 
 | 23 | #include <ucontext.h> | 
 | 24 | #include <unistd.h> | 
 | 25 |  | 
 | 26 | #include <backtrace/Backtrace.h> | 
 | 27 | #include <backtrace/BacktraceMap.h> | 
 | 28 |  | 
 | 29 | #include "BacktraceLog.h" | 
 | 30 | #include "BacktracePtrace.h" | 
 | 31 | #include "thread_utils.h" | 
 | 32 |  | 
 | 33 | #if !defined(__APPLE__) | 
 | 34 | static bool PtraceRead(pid_t tid, uintptr_t addr, word_t* out_value) { | 
 | 35 |   // ptrace() returns -1 and sets errno when the operation fails. | 
 | 36 |   // To disambiguate -1 from a valid result, we clear errno beforehand. | 
 | 37 |   errno = 0; | 
 | 38 |   *out_value = ptrace(PTRACE_PEEKTEXT, tid, reinterpret_cast<void*>(addr), nullptr); | 
 | 39 |   if (*out_value == static_cast<word_t>(-1) && errno) { | 
 | 40 |     BACK_LOGW("invalid pointer %p reading from tid %d, ptrace() strerror(errno)=%s", | 
 | 41 |               reinterpret_cast<void*>(addr), tid, strerror(errno)); | 
 | 42 |     return false; | 
 | 43 |   } | 
 | 44 |   return true; | 
 | 45 | } | 
 | 46 | #endif | 
 | 47 |  | 
 | 48 | bool BacktracePtrace::ReadWord(uintptr_t ptr, word_t* out_value) { | 
 | 49 | #if defined(__APPLE__) | 
 | 50 |   BACK_LOGW("MacOS does not support reading from another pid."); | 
 | 51 |   return false; | 
 | 52 | #else | 
 | 53 |   if (!VerifyReadWordArgs(ptr, out_value)) { | 
 | 54 |     return false; | 
 | 55 |   } | 
 | 56 |  | 
 | 57 |   backtrace_map_t map; | 
 | 58 |   FillInMap(ptr, &map); | 
 | 59 |   if (!BacktraceMap::IsValid(map) || !(map.flags & PROT_READ)) { | 
 | 60 |     return false; | 
 | 61 |   } | 
 | 62 |  | 
 | 63 |   return PtraceRead(Tid(), ptr, out_value); | 
 | 64 | #endif | 
 | 65 | } | 
 | 66 |  | 
 | 67 | size_t BacktracePtrace::Read(uintptr_t addr, uint8_t* buffer, size_t bytes) { | 
 | 68 | #if defined(__APPLE__) | 
 | 69 |   BACK_LOGW("MacOS does not support reading from another pid."); | 
 | 70 |   return 0; | 
 | 71 | #else | 
 | 72 |   backtrace_map_t map; | 
 | 73 |   FillInMap(addr, &map); | 
 | 74 |   if (!BacktraceMap::IsValid(map) || !(map.flags & PROT_READ)) { | 
 | 75 |     return 0; | 
 | 76 |   } | 
 | 77 |  | 
 | 78 |   bytes = MIN(map.end - addr, bytes); | 
 | 79 |   size_t bytes_read = 0; | 
 | 80 |   word_t data_word; | 
 | 81 |   size_t align_bytes = addr & (sizeof(word_t) - 1); | 
 | 82 |   if (align_bytes != 0) { | 
 | 83 |     if (!PtraceRead(Tid(), addr & ~(sizeof(word_t) - 1), &data_word)) { | 
 | 84 |       return 0; | 
 | 85 |     } | 
 | 86 |     align_bytes = sizeof(word_t) - align_bytes; | 
 | 87 |     memcpy(buffer, reinterpret_cast<uint8_t*>(&data_word) + sizeof(word_t) - align_bytes, | 
 | 88 |            align_bytes); | 
 | 89 |     addr += align_bytes; | 
 | 90 |     buffer += align_bytes; | 
 | 91 |     bytes -= align_bytes; | 
 | 92 |     bytes_read += align_bytes; | 
 | 93 |   } | 
 | 94 |  | 
 | 95 |   size_t num_words = bytes / sizeof(word_t); | 
 | 96 |   for (size_t i = 0; i < num_words; i++) { | 
 | 97 |     if (!PtraceRead(Tid(), addr, &data_word)) { | 
 | 98 |       return bytes_read; | 
 | 99 |     } | 
 | 100 |     memcpy(buffer, &data_word, sizeof(word_t)); | 
 | 101 |     buffer += sizeof(word_t); | 
 | 102 |     addr += sizeof(word_t); | 
 | 103 |     bytes_read += sizeof(word_t); | 
 | 104 |   } | 
 | 105 |  | 
 | 106 |   size_t left_over = bytes & (sizeof(word_t) - 1); | 
 | 107 |   if (left_over) { | 
 | 108 |     if (!PtraceRead(Tid(), addr, &data_word)) { | 
 | 109 |       return bytes_read; | 
 | 110 |     } | 
 | 111 |     memcpy(buffer, &data_word, left_over); | 
 | 112 |     bytes_read += left_over; | 
 | 113 |   } | 
 | 114 |   return bytes_read; | 
 | 115 | #endif | 
 | 116 | } |