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