blob: 148c4181021c57b37f0a9d88a4549b8c231d98d3 [file] [log] [blame]
Christopher Ferris2c43cff2015-03-26 19:18:36 -07001/*
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>
Christopher Ferris279843e2016-10-12 12:39:18 -070020#include <sys/uio.h>
Christopher Ferris2c43cff2015-03-26 19:18:36 -070021#include <sys/param.h>
22#include <sys/ptrace.h>
23#include <sys/types.h>
24#include <ucontext.h>
25#include <unistd.h>
26
27#include <backtrace/Backtrace.h>
28#include <backtrace/BacktraceMap.h>
29
30#include "BacktraceLog.h"
31#include "BacktracePtrace.h"
32#include "thread_utils.h"
33
34#if !defined(__APPLE__)
35static bool PtraceRead(pid_t tid, uintptr_t addr, word_t* out_value) {
36 // ptrace() returns -1 and sets errno when the operation fails.
37 // To disambiguate -1 from a valid result, we clear errno beforehand.
38 errno = 0;
39 *out_value = ptrace(PTRACE_PEEKTEXT, tid, reinterpret_cast<void*>(addr), nullptr);
40 if (*out_value == static_cast<word_t>(-1) && errno) {
Christopher Ferris2c43cff2015-03-26 19:18:36 -070041 return false;
42 }
43 return true;
44}
45#endif
46
47bool BacktracePtrace::ReadWord(uintptr_t ptr, word_t* out_value) {
48#if defined(__APPLE__)
49 BACK_LOGW("MacOS does not support reading from another pid.");
50 return false;
51#else
52 if (!VerifyReadWordArgs(ptr, out_value)) {
53 return false;
54 }
55
56 backtrace_map_t map;
57 FillInMap(ptr, &map);
58 if (!BacktraceMap::IsValid(map) || !(map.flags & PROT_READ)) {
59 return false;
60 }
61
62 return PtraceRead(Tid(), ptr, out_value);
63#endif
64}
65
66size_t BacktracePtrace::Read(uintptr_t addr, uint8_t* buffer, size_t bytes) {
67#if defined(__APPLE__)
68 BACK_LOGW("MacOS does not support reading from another pid.");
69 return 0;
70#else
71 backtrace_map_t map;
72 FillInMap(addr, &map);
73 if (!BacktraceMap::IsValid(map) || !(map.flags & PROT_READ)) {
74 return 0;
75 }
Christopher Ferris2c43cff2015-03-26 19:18:36 -070076 bytes = MIN(map.end - addr, bytes);
Christopher Ferris2c43cff2015-03-26 19:18:36 -070077
Christopher Ferris279843e2016-10-12 12:39:18 -070078 struct iovec local_io;
79 local_io.iov_base = buffer;
80 local_io.iov_len = bytes;
Christopher Ferris2c43cff2015-03-26 19:18:36 -070081
Christopher Ferris279843e2016-10-12 12:39:18 -070082 struct iovec remote_io;
83 remote_io.iov_base = reinterpret_cast<void*>(addr);
84 remote_io.iov_len = bytes;
85
86 ssize_t bytes_read = process_vm_readv(Tid(), &local_io, 1, &remote_io, 1, 0);
87 if (bytes_read == -1) {
88 return 0;
Christopher Ferris2c43cff2015-03-26 19:18:36 -070089 }
Christopher Ferris279843e2016-10-12 12:39:18 -070090 return static_cast<size_t>(bytes_read);
Christopher Ferris2c43cff2015-03-26 19:18:36 -070091#endif
92}