machine.c revision 13e715b491e876865e752a3a69dd6f347049a488
16cc492308712613cd23bee9240b1757428841a2fBruce Beare/* system/debuggerd/debuggerd.c
26cc492308712613cd23bee9240b1757428841a2fBruce Beare**
36cc492308712613cd23bee9240b1757428841a2fBruce Beare** Copyright 2006, The Android Open Source Project
46cc492308712613cd23bee9240b1757428841a2fBruce Beare**
56cc492308712613cd23bee9240b1757428841a2fBruce Beare** Licensed under the Apache License, Version 2.0 (the "License");
66cc492308712613cd23bee9240b1757428841a2fBruce Beare** you may not use this file except in compliance with the License.
76cc492308712613cd23bee9240b1757428841a2fBruce Beare** You may obtain a copy of the License at
86cc492308712613cd23bee9240b1757428841a2fBruce Beare**
96cc492308712613cd23bee9240b1757428841a2fBruce Beare**     http://www.apache.org/licenses/LICENSE-2.0
106cc492308712613cd23bee9240b1757428841a2fBruce Beare**
116cc492308712613cd23bee9240b1757428841a2fBruce Beare** Unless required by applicable law or agreed to in writing, software
126cc492308712613cd23bee9240b1757428841a2fBruce Beare** distributed under the License is distributed on an "AS IS" BASIS,
136cc492308712613cd23bee9240b1757428841a2fBruce Beare** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
146cc492308712613cd23bee9240b1757428841a2fBruce Beare** See the License for the specific language governing permissions and
156cc492308712613cd23bee9240b1757428841a2fBruce Beare** limitations under the License.
166cc492308712613cd23bee9240b1757428841a2fBruce Beare*/
176cc492308712613cd23bee9240b1757428841a2fBruce Beare
186cc492308712613cd23bee9240b1757428841a2fBruce Beare#include <stdio.h>
196cc492308712613cd23bee9240b1757428841a2fBruce Beare#include <errno.h>
206cc492308712613cd23bee9240b1757428841a2fBruce Beare#include <signal.h>
216cc492308712613cd23bee9240b1757428841a2fBruce Beare#include <pthread.h>
226cc492308712613cd23bee9240b1757428841a2fBruce Beare#include <fcntl.h>
236cc492308712613cd23bee9240b1757428841a2fBruce Beare#include <sys/types.h>
246cc492308712613cd23bee9240b1757428841a2fBruce Beare#include <dirent.h>
256cc492308712613cd23bee9240b1757428841a2fBruce Beare
266cc492308712613cd23bee9240b1757428841a2fBruce Beare#include <sys/ptrace.h>
276cc492308712613cd23bee9240b1757428841a2fBruce Beare#include <sys/wait.h>
286cc492308712613cd23bee9240b1757428841a2fBruce Beare#include <sys/exec_elf.h>
296cc492308712613cd23bee9240b1757428841a2fBruce Beare#include <sys/stat.h>
306cc492308712613cd23bee9240b1757428841a2fBruce Beare
316cc492308712613cd23bee9240b1757428841a2fBruce Beare#include <cutils/sockets.h>
326cc492308712613cd23bee9240b1757428841a2fBruce Beare#include <cutils/properties.h>
336cc492308712613cd23bee9240b1757428841a2fBruce Beare
3413e715b491e876865e752a3a69dd6f347049a488Jeff Brown#include <corkscrew/backtrace.h>
3513e715b491e876865e752a3a69dd6f347049a488Jeff Brown#include <corkscrew/ptrace.h>
3613e715b491e876865e752a3a69dd6f347049a488Jeff Brown
376cc492308712613cd23bee9240b1757428841a2fBruce Beare#include <linux/input.h>
386cc492308712613cd23bee9240b1757428841a2fBruce Beare
3913e715b491e876865e752a3a69dd6f347049a488Jeff Brown#include "../machine.h"
406cc492308712613cd23bee9240b1757428841a2fBruce Beare#include "../utility.h"
416cc492308712613cd23bee9240b1757428841a2fBruce Beare
4213e715b491e876865e752a3a69dd6f347049a488Jeff Brownstatic void dump_registers(ptrace_context_t* context __attribute((unused)),
4313e715b491e876865e752a3a69dd6f347049a488Jeff Brown        int tfd, pid_t pid, bool at_fault) {
446cc492308712613cd23bee9240b1757428841a2fBruce Beare    struct pt_regs_x86 r;
456cc492308712613cd23bee9240b1757428841a2fBruce Beare    bool only_in_tombstone = !at_fault;
466cc492308712613cd23bee9240b1757428841a2fBruce Beare
476cc492308712613cd23bee9240b1757428841a2fBruce Beare    if(ptrace(PTRACE_GETREGS, pid, 0, &r)) {
4813e715b491e876865e752a3a69dd6f347049a488Jeff Brown        _LOG(tfd, only_in_tombstone, "cannot get registers: %s\n", strerror(errno));
496cc492308712613cd23bee9240b1757428841a2fBruce Beare        return;
506cc492308712613cd23bee9240b1757428841a2fBruce Beare    }
5113e715b491e876865e752a3a69dd6f347049a488Jeff Brown    //if there is no stack, no print just like arm
526cc492308712613cd23bee9240b1757428841a2fBruce Beare    if(!r.ebp)
536cc492308712613cd23bee9240b1757428841a2fBruce Beare        return;
5413e715b491e876865e752a3a69dd6f347049a488Jeff Brown    _LOG(tfd, only_in_tombstone, "    eax %08x  ebx %08x  ecx %08x  edx %08x\n",
556cc492308712613cd23bee9240b1757428841a2fBruce Beare         r.eax, r.ebx, r.ecx, r.edx);
5613e715b491e876865e752a3a69dd6f347049a488Jeff Brown    _LOG(tfd, only_in_tombstone, "    esi %08x  edi %08x\n",
576cc492308712613cd23bee9240b1757428841a2fBruce Beare         r.esi, r.edi);
5813e715b491e876865e752a3a69dd6f347049a488Jeff Brown    _LOG(tfd, only_in_tombstone, "    xcs %08x  xds %08x  xes %08x  xfs %08x  xss %08x\n",
596cc492308712613cd23bee9240b1757428841a2fBruce Beare         r.xcs, r.xds, r.xes, r.xfs, r.xss);
6013e715b491e876865e752a3a69dd6f347049a488Jeff Brown    _LOG(tfd, only_in_tombstone, "    eip %08x  ebp %08x  esp %08x  flags %08x\n",
616cc492308712613cd23bee9240b1757428841a2fBruce Beare         r.eip, r.ebp, r.esp, r.eflags);
626cc492308712613cd23bee9240b1757428841a2fBruce Beare}
6313e715b491e876865e752a3a69dd6f347049a488Jeff Brown
6413e715b491e876865e752a3a69dd6f347049a488Jeff Brownvoid dump_thread(ptrace_context_t* context, int tfd, pid_t tid, bool at_fault) {
6513e715b491e876865e752a3a69dd6f347049a488Jeff Brown    dump_registers(context, tfd, tid, at_fault);
6613e715b491e876865e752a3a69dd6f347049a488Jeff Brown
6713e715b491e876865e752a3a69dd6f347049a488Jeff Brown    dump_backtrace_and_stack(context, tfd, tid, at_fault);
6813e715b491e876865e752a3a69dd6f347049a488Jeff Brown
6913e715b491e876865e752a3a69dd6f347049a488Jeff Brown    if (at_fault) {
7013e715b491e876865e752a3a69dd6f347049a488Jeff Brown        dump_nearby_maps(context, tfd, tid);
7113e715b491e876865e752a3a69dd6f347049a488Jeff Brown    }
7213e715b491e876865e752a3a69dd6f347049a488Jeff Brown}
7313e715b491e876865e752a3a69dd6f347049a488Jeff Brown
74