Home | History | Annotate | Download | only in x86_64
      1 /*
      2 ** Copyright 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 <sys/ptrace.h>
     20 #include <string.h>
     21 #include <sys/user.h>
     22 
     23 #include <backtrace/Backtrace.h>
     24 
     25 #include "machine.h"
     26 #include "utility.h"
     27 
     28 void dump_memory_and_code(log_t* log, Backtrace* backtrace) {
     29   struct user_regs_struct r;
     30   if (ptrace(PTRACE_GETREGS, backtrace->Tid(), 0, &r) == -1) {
     31     _LOG(log, logtype::ERROR, "cannot get registers: %s\n", strerror(errno));
     32     return;
     33   }
     34 
     35   dump_memory(log, backtrace, static_cast<uintptr_t>(r.rax), "memory near rax:");
     36   dump_memory(log, backtrace, static_cast<uintptr_t>(r.rbx), "memory near rbx:");
     37   dump_memory(log, backtrace, static_cast<uintptr_t>(r.rcx), "memory near rcx:");
     38   dump_memory(log, backtrace, static_cast<uintptr_t>(r.rdx), "memory near rdx:");
     39   dump_memory(log, backtrace, static_cast<uintptr_t>(r.rsi), "memory near rsi:");
     40   dump_memory(log, backtrace, static_cast<uintptr_t>(r.rdi), "memory near rdi:");
     41 
     42   dump_memory(log, backtrace, static_cast<uintptr_t>(r.rip), "code around rip:");
     43 }
     44 
     45 void dump_registers(log_t* log, pid_t tid) {
     46   struct user_regs_struct r;
     47   if (ptrace(PTRACE_GETREGS, tid, 0, &r) == -1) {
     48     _LOG(log, logtype::ERROR, "cannot get registers: %s\n", strerror(errno));
     49     return;
     50   }
     51 
     52   _LOG(log, logtype::REGISTERS, "    rax %016lx  rbx %016lx  rcx %016lx  rdx %016lx\n",
     53        r.rax, r.rbx, r.rcx, r.rdx);
     54   _LOG(log, logtype::REGISTERS, "    rsi %016lx  rdi %016lx\n",
     55        r.rsi, r.rdi);
     56   _LOG(log, logtype::REGISTERS, "    r8  %016lx  r9  %016lx  r10 %016lx  r11 %016lx\n",
     57        r.r8, r.r9, r.r10, r.r11);
     58   _LOG(log, logtype::REGISTERS, "    r12 %016lx  r13 %016lx  r14 %016lx  r15 %016lx\n",
     59        r.r12, r.r13, r.r14, r.r15);
     60   _LOG(log, logtype::REGISTERS, "    cs  %016lx  ss  %016lx\n",
     61        r.cs, r.ss);
     62   _LOG(log, logtype::REGISTERS, "    rip %016lx  rbp %016lx  rsp %016lx  eflags %016lx\n",
     63        r.rip, r.rbp, r.rsp, r.eflags);
     64 }
     65