1 /*
   2  * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright (c) 2015, Red Hat Inc.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 package sun.jvm.hotspot.debugger.linux;
  27 
  28 import java.io.*;
  29 import java.util.*;
  30 
  31 import sun.jvm.hotspot.debugger.*;
  32 import sun.jvm.hotspot.debugger.cdbg.*;
  33 import sun.jvm.hotspot.debugger.x86.*;
  34 import sun.jvm.hotspot.debugger.amd64.*;
  35 import sun.jvm.hotspot.debugger.aarch64.*;
  36 import sun.jvm.hotspot.debugger.sparc.*;
  37 import sun.jvm.hotspot.debugger.ppc64.*;
  38 import sun.jvm.hotspot.debugger.linux.x86.*;
  39 import sun.jvm.hotspot.debugger.linux.amd64.*;
  40 import sun.jvm.hotspot.debugger.linux.sparc.*;
  41 import sun.jvm.hotspot.debugger.linux.ppc64.*;
  42 import sun.jvm.hotspot.debugger.linux.aarch64.*;
  43 import sun.jvm.hotspot.utilities.*;
  44 
  45 class LinuxCDebugger implements CDebugger {
  46   private LinuxDebugger dbg;
  47 
  48   LinuxCDebugger(LinuxDebugger dbg) {
  49     this.dbg = dbg;
  50   }
  51 
  52   public List getThreadList() throws DebuggerException {
  53     return dbg.getThreadList();
  54   }
  55 
  56   public List/*<LoadObject>*/ getLoadObjectList() throws DebuggerException {
  57     return dbg.getLoadObjectList();
  58   }
  59 
  60   public LoadObject loadObjectContainingPC(Address pc) throws DebuggerException {
  61     if (pc == null) {
  62       return null;
  63     }
  64 
  65     /* Typically we have about ten loaded objects here. So no reason to do
  66       sort/binary search here. Linear search gives us acceptable performance.*/
  67 
  68     List objs = getLoadObjectList();
  69 
  70     for (int i = 0; i < objs.size(); i++) {
  71       LoadObject ob = (LoadObject) objs.get(i);
  72       Address base = ob.getBase();
  73       long size = ob.getSize();
  74       if ( pc.greaterThanOrEqual(base) && pc.lessThan(base.addOffsetTo(size))) {
  75         return ob;
  76       }
  77     }
  78 
  79     return null;
  80   }
  81 
  82   public CFrame topFrameForThread(ThreadProxy thread) throws DebuggerException {
  83     String cpu = dbg.getCPU();
  84     if (cpu.equals("x86")) {
  85        X86ThreadContext context = (X86ThreadContext) thread.getContext();
  86        Address ebp = context.getRegisterAsAddress(X86ThreadContext.EBP);
  87        if (ebp == null) return null;
  88        Address pc  = context.getRegisterAsAddress(X86ThreadContext.EIP);
  89        if (pc == null) return null;
  90        return new LinuxX86CFrame(dbg, ebp, pc);
  91     } else if (cpu.equals("amd64")) {
  92        AMD64ThreadContext context = (AMD64ThreadContext) thread.getContext();
  93        Address pc  = context.getRegisterAsAddress(AMD64ThreadContext.RIP);
  94        if (pc == null) return null;
  95 
  96        long libptr = dbg.findLibPtrByAddress(pc);
  97        if (libptr == 0L) { // Java frame
  98          Address rbp  = context.getRegisterAsAddress(AMD64ThreadContext.RBP);
  99          if (rbp == null) {
 100            return null;
 101          }
 102          return new LinuxAMD64CFrame(dbg, rbp, pc, null);
 103        } else { // Native frame
 104          DwarfParser dwarf;
 105          try {
 106            dwarf = new DwarfParser(libptr);
 107          } catch (DebuggerException e) {
 108            Address rbp  = context.getRegisterAsAddress(AMD64ThreadContext.RBP);
 109            if (rbp == null) {
 110              return null;
 111            }
 112            return new LinuxAMD64CFrame(dbg, rbp, pc, null);
 113          }
 114          dwarf.processDwarf(pc);
 115          Address cfa = ((dwarf.getCFARegister() == AMD64ThreadContext.RBP) &&
 116                         !dwarf.isBPOffsetAvailable())
 117                            ? context.getRegisterAsAddress(AMD64ThreadContext.RBP)
 118                            : context.getRegisterAsAddress(dwarf.getCFARegister())
 119                                     .addOffsetTo(dwarf.getCFAOffset());
 120          if (cfa == null) {
 121            return null;
 122          }
 123          return new LinuxAMD64CFrame(dbg, cfa, pc, dwarf);
 124        }
 125     } else if (cpu.equals("sparc")) {
 126        SPARCThreadContext context = (SPARCThreadContext) thread.getContext();
 127        Address sp = context.getRegisterAsAddress(SPARCThreadContext.R_SP);
 128        if (sp == null) return null;
 129        Address pc  = context.getRegisterAsAddress(SPARCThreadContext.R_O7);
 130        if (pc == null) return null;
 131        return new LinuxSPARCCFrame(dbg, sp, pc, LinuxDebuggerLocal.getAddressSize());
 132     }  else if (cpu.equals("ppc64")) {
 133         PPC64ThreadContext context = (PPC64ThreadContext) thread.getContext();
 134         Address sp = context.getRegisterAsAddress(PPC64ThreadContext.SP);
 135         if (sp == null) return null;
 136         Address pc  = context.getRegisterAsAddress(PPC64ThreadContext.PC);
 137         if (pc == null) return null;
 138         return new LinuxPPC64CFrame(dbg, sp, pc, LinuxDebuggerLocal.getAddressSize());
 139     } else if (cpu.equals("aarch64")) {
 140        AARCH64ThreadContext context = (AARCH64ThreadContext) thread.getContext();
 141        Address fp = context.getRegisterAsAddress(AARCH64ThreadContext.FP);
 142        if (fp == null) return null;
 143        Address pc  = context.getRegisterAsAddress(AARCH64ThreadContext.PC);
 144        if (pc == null) return null;
 145        return new LinuxAARCH64CFrame(dbg, fp, pc);
 146      } else {
 147        // Runtime exception thrown by LinuxThreadContextFactory if unknown cpu
 148        ThreadContext context = (ThreadContext) thread.getContext();
 149        return context.getTopFrame(dbg);
 150     }
 151   }
 152 
 153   public String getNameOfFile(String fileName) {
 154     return new File(fileName).getName();
 155   }
 156 
 157   public ProcessControl getProcessControl() throws DebuggerException {
 158     // FIXME: after stabs parser
 159     return null;
 160   }
 161 
 162   public boolean canDemangle() {
 163     return true;
 164   }
 165 
 166   public String demangle(String sym) {
 167     return dbg.demangle(sym);
 168   }
 169 }