Regs.cpp 3.7 KB

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