machine.c revision 9524e4158fbb988b6a5e4f5be68ee10b7e4dd6d8
1/* system/debuggerd/debuggerd.c
2**
3** Copyright 2006, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9**     http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18#include <stdio.h>
19#include <errno.h>
20#include <signal.h>
21#include <pthread.h>
22#include <fcntl.h>
23#include <sys/types.h>
24#include <dirent.h>
25
26#include <sys/ptrace.h>
27#include <sys/wait.h>
28#include <sys/exec_elf.h>
29#include <sys/stat.h>
30
31#include <cutils/sockets.h>
32#include <cutils/properties.h>
33
34#include <linux/input.h>
35#include <linux/user.h>
36
37#include "../utility.h"
38#include "../machine.h"
39
40/* enable to dump memory pointed to by every register */
41#define DUMP_MEMORY_FOR_ALL_REGISTERS 1
42
43#ifdef WITH_VFP
44#ifdef WITH_VFP_D32
45#define NUM_VFP_REGS 32
46#else
47#define NUM_VFP_REGS 16
48#endif
49#endif
50
51/*
52 * If configured to do so, dump memory around *all* registers
53 * for the crashing thread.
54 */
55static void dump_memory_and_code(int tfd, pid_t tid, bool at_fault) {
56    struct pt_regs regs;
57    if(ptrace(PTRACE_GETREGS, tid, 0, &regs)) {
58        return;
59    }
60
61    if (at_fault && DUMP_MEMORY_FOR_ALL_REGISTERS) {
62        static const char REG_NAMES[] = "r0r1r2r3r4r5r6r7r8r9slfpipsp";
63
64        for (int reg = 0; reg < 14; reg++) {
65            /* this may not be a valid way to access, but it'll do for now */
66            uintptr_t addr = regs.uregs[reg];
67
68            /*
69             * Don't bother if it looks like a small int or ~= null, or if
70             * it's in the kernel area.
71             */
72            if (addr < 4096 || addr >= 0xc0000000) {
73                continue;
74            }
75
76            _LOG(tfd, false, "\nmemory near %.2s:\n", &REG_NAMES[reg * 2]);
77            dump_memory(tfd, tid, addr, at_fault);
78        }
79    }
80
81    _LOG(tfd, !at_fault, "\ncode around pc:\n");
82    dump_memory(tfd, tid, (uintptr_t)regs.ARM_pc, at_fault);
83
84    if (regs.ARM_pc != regs.ARM_lr) {
85        _LOG(tfd, !at_fault, "\ncode around lr:\n");
86        dump_memory(tfd, tid, (uintptr_t)regs.ARM_lr, at_fault);
87    }
88}
89
90void dump_registers(ptrace_context_t* context __attribute((unused)),
91        int tfd, pid_t tid, bool at_fault)
92{
93    struct pt_regs r;
94    bool only_in_tombstone = !at_fault;
95
96    if(ptrace(PTRACE_GETREGS, tid, 0, &r)) {
97        _LOG(tfd, only_in_tombstone, "cannot get registers: %s\n", strerror(errno));
98        return;
99    }
100
101    _LOG(tfd, only_in_tombstone, "    r0 %08x  r1 %08x  r2 %08x  r3 %08x\n",
102            (uint32_t)r.ARM_r0, (uint32_t)r.ARM_r1, (uint32_t)r.ARM_r2, (uint32_t)r.ARM_r3);
103    _LOG(tfd, only_in_tombstone, "    r4 %08x  r5 %08x  r6 %08x  r7 %08x\n",
104            (uint32_t)r.ARM_r4, (uint32_t)r.ARM_r5, (uint32_t)r.ARM_r6, (uint32_t)r.ARM_r7);
105    _LOG(tfd, only_in_tombstone, "    r8 %08x  r9 %08x  sl %08x  fp %08x\n",
106            (uint32_t)r.ARM_r8, (uint32_t)r.ARM_r9, (uint32_t)r.ARM_r10, (uint32_t)r.ARM_fp);
107    _LOG(tfd, only_in_tombstone, "    ip %08x  sp %08x  lr %08x  pc %08x  cpsr %08x\n",
108            (uint32_t)r.ARM_ip, (uint32_t)r.ARM_sp, (uint32_t)r.ARM_lr,
109            (uint32_t)r.ARM_pc, (uint32_t)r.ARM_cpsr);
110
111#ifdef WITH_VFP
112    struct user_vfp vfp_regs;
113    int i;
114
115    if(ptrace(PTRACE_GETVFPREGS, tid, 0, &vfp_regs)) {
116        _LOG(tfd, only_in_tombstone, "cannot get registers: %s\n", strerror(errno));
117        return;
118    }
119
120    for (i = 0; i < NUM_VFP_REGS; i += 2) {
121        _LOG(tfd, only_in_tombstone, "    d%-2d %016llx  d%-2d %016llx\n",
122                i, vfp_regs.fpregs[i], i+1, vfp_regs.fpregs[i+1]);
123    }
124    _LOG(tfd, only_in_tombstone, "    scr %08lx\n", vfp_regs.fpscr);
125#endif
126}
127
128void dump_thread(ptrace_context_t* context, int tfd, pid_t tid, bool at_fault) {
129    dump_registers(context, tfd, tid, at_fault);
130
131    dump_backtrace_and_stack(context, tfd, tid, at_fault);
132
133    if (at_fault) {
134        dump_memory_and_code(tfd, tid, at_fault);
135        dump_nearby_maps(context, tfd, tid);
136    }
137}
138