Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 17 | #include <stdint.h> |
| 18 | #include <sys/ptrace.h> |
| 19 | #include <sys/uio.h> |
| 20 | |
| 21 | #include <vector> |
| 22 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 23 | #include <unwindstack/Elf.h> |
| 24 | #include <unwindstack/MapInfo.h> |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 25 | #include <unwindstack/Regs.h> |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 26 | #include <unwindstack/RegsArm.h> |
| 27 | #include <unwindstack/RegsArm64.h> |
| 28 | #include <unwindstack/RegsX86.h> |
| 29 | #include <unwindstack/RegsX86_64.h> |
Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame^] | 30 | #include <unwindstack/RegsMips.h> |
| 31 | #include <unwindstack/RegsMips64.h> |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 32 | |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 33 | #include "UserArm.h" |
| 34 | #include "UserArm64.h" |
| 35 | #include "UserX86.h" |
| 36 | #include "UserX86_64.h" |
Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame^] | 37 | #include "UserMips.h" |
| 38 | #include "UserMips64.h" |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 39 | |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 40 | namespace unwindstack { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 41 | |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 42 | // The largest user structure. |
Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame^] | 43 | constexpr size_t MAX_USER_REGS_SIZE = sizeof(mips64_user_regs) + 10; |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 44 | |
| 45 | // This function assumes that reg_data is already aligned to a 64 bit value. |
| 46 | // If not this could crash with an unaligned access. |
Josh Gao | 0953ecd | 2017-08-25 13:55:06 -0700 | [diff] [blame] | 47 | Regs* Regs::RemoteGet(pid_t pid) { |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 48 | // Make the buffer large enough to contain the largest registers type. |
| 49 | std::vector<uint64_t> buffer(MAX_USER_REGS_SIZE / sizeof(uint64_t)); |
| 50 | struct iovec io; |
| 51 | io.iov_base = buffer.data(); |
| 52 | io.iov_len = buffer.size() * sizeof(uint64_t); |
| 53 | |
| 54 | if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, reinterpret_cast<void*>(&io)) == -1) { |
| 55 | return nullptr; |
| 56 | } |
| 57 | |
| 58 | switch (io.iov_len) { |
| 59 | case sizeof(x86_user_regs): |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 60 | return RegsX86::Read(buffer.data()); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 61 | case sizeof(x86_64_user_regs): |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 62 | return RegsX86_64::Read(buffer.data()); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 63 | case sizeof(arm_user_regs): |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 64 | return RegsArm::Read(buffer.data()); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 65 | case sizeof(arm64_user_regs): |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 66 | return RegsArm64::Read(buffer.data()); |
Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame^] | 67 | case sizeof(mips_user_regs): |
| 68 | return RegsMips::Read(buffer.data()); |
| 69 | case sizeof(mips64_user_regs): |
| 70 | return RegsMips64::Read(buffer.data()); |
Christopher Ferris | 3958f80 | 2017-02-01 15:44:40 -0800 | [diff] [blame] | 71 | } |
| 72 | return nullptr; |
| 73 | } |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 74 | |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 75 | Regs* Regs::CreateFromUcontext(ArchEnum arch, void* ucontext) { |
| 76 | switch (arch) { |
| 77 | case ARCH_X86: |
| 78 | return RegsX86::CreateFromUcontext(ucontext); |
| 79 | case ARCH_X86_64: |
| 80 | return RegsX86_64::CreateFromUcontext(ucontext); |
| 81 | case ARCH_ARM: |
| 82 | return RegsArm::CreateFromUcontext(ucontext); |
| 83 | case ARCH_ARM64: |
| 84 | return RegsArm64::CreateFromUcontext(ucontext); |
Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame^] | 85 | case ARCH_MIPS: |
| 86 | return RegsMips::CreateFromUcontext(ucontext); |
| 87 | case ARCH_MIPS64: |
| 88 | return RegsMips64::CreateFromUcontext(ucontext); |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 89 | case ARCH_UNKNOWN: |
| 90 | default: |
| 91 | return nullptr; |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 92 | } |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 95 | ArchEnum Regs::CurrentArch() { |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 96 | #if defined(__arm__) |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 97 | return ARCH_ARM; |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 98 | #elif defined(__aarch64__) |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 99 | return ARCH_ARM64; |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 100 | #elif defined(__i386__) |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 101 | return ARCH_X86; |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 102 | #elif defined(__x86_64__) |
Christopher Ferris | d06001d | 2017-11-30 18:56:01 -0800 | [diff] [blame] | 103 | return ARCH_X86_64; |
Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame^] | 104 | #elif defined(__mips__) && !defined(__LP64__) |
| 105 | return ARCH_MIPS; |
| 106 | #elif defined(__mips__) && defined(__LP64__) |
| 107 | return ARCH_MIPS64; |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 108 | #else |
| 109 | abort(); |
| 110 | #endif |
| 111 | } |
| 112 | |
| 113 | Regs* Regs::CreateFromLocal() { |
| 114 | Regs* regs; |
| 115 | #if defined(__arm__) |
| 116 | regs = new RegsArm(); |
| 117 | #elif defined(__aarch64__) |
| 118 | regs = new RegsArm64(); |
| 119 | #elif defined(__i386__) |
| 120 | regs = new RegsX86(); |
| 121 | #elif defined(__x86_64__) |
| 122 | regs = new RegsX86_64(); |
Douglas Leung | 61b1a1a | 2017-11-08 10:53:53 +0100 | [diff] [blame^] | 123 | #elif defined(__mips__) && !defined(__LP64__) |
| 124 | regs = new RegsMips(); |
| 125 | #elif defined(__mips__) && defined(__LP64__) |
| 126 | regs = new RegsMips64(); |
Christopher Ferris | 2a25c4a | 2017-07-07 16:35:48 -0700 | [diff] [blame] | 127 | #else |
| 128 | abort(); |
| 129 | #endif |
| 130 | return regs; |
| 131 | } |
Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 132 | |
| 133 | } // namespace unwindstack |