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
25package sun.jvm.hotspot.debugger.bsd;
26
27import java.io.*;
28import java.util.*;
29import sun.jvm.hotspot.debugger.*;
30import sun.jvm.hotspot.debugger.cdbg.*;
31import sun.jvm.hotspot.debugger.x86.*;
32import sun.jvm.hotspot.debugger.amd64.*;
33import sun.jvm.hotspot.debugger.bsd.x86.*;
34import sun.jvm.hotspot.debugger.bsd.amd64.*;
35import sun.jvm.hotspot.utilities.*;
36
37class BsdCDebugger implements CDebugger {
38  private BsdDebugger dbg;
39
40  BsdCDebugger(BsdDebugger dbg) {
41    this.dbg = dbg;
42  }
43
44  public List getThreadList() throws DebuggerException {
45    return dbg.getThreadList();
46  }
47
48  public List/*<LoadObject>*/ getLoadObjectList() throws DebuggerException {
49    return dbg.getLoadObjectList();
50  }
51
52  public LoadObject loadObjectContainingPC(Address pc) throws DebuggerException {
53    if (pc == null) {
54      return null;
55    }
56    List objs = getLoadObjectList();
57    Object[] arr = objs.toArray();
58    // load objects are sorted by base address, do binary search
59    int mid  = -1;
60    int low  = 0;
61    int high = arr.length - 1;
62
63    while (low <= high) {
64       mid = (low + high) >> 1;
65       LoadObject midVal = (LoadObject) arr[mid];
66       long cmp = pc.minus(midVal.getBase());
67       if (cmp < 0) {
68          high = mid - 1;
69       } else if (cmp > 0) {
70          long size = midVal.getSize();
71          if (cmp >= size) {
72             low = mid + 1;
73          } else {
74             return (LoadObject) arr[mid];
75          }
76       } else { // match found
77          return (LoadObject) arr[mid];
78       }
79    }
80    // no match found.
81    return null;
82  }
83
84  public CFrame topFrameForThread(ThreadProxy thread) throws DebuggerException {
85    String cpu = dbg.getCPU();
86    if (cpu.equals("x86")) {
87       X86ThreadContext context = (X86ThreadContext) thread.getContext();
88       Address ebp = context.getRegisterAsAddress(X86ThreadContext.EBP);
89       if (ebp == null) return null;
90       Address pc  = context.getRegisterAsAddress(X86ThreadContext.EIP);
91       if (pc == null) return null;
92       return new BsdX86CFrame(dbg, ebp, pc);
93    } else if (cpu.equals("amd64") || cpu.equals("x86_64")) {
94       AMD64ThreadContext context = (AMD64ThreadContext) thread.getContext();
95       Address rbp = context.getRegisterAsAddress(AMD64ThreadContext.RBP);
96       if (rbp == null) return null;
97       Address pc  = context.getRegisterAsAddress(AMD64ThreadContext.RIP);
98       if (pc == null) return null;
99       return new BsdAMD64CFrame(dbg, rbp, pc);
100    } else {
101       throw new DebuggerException(cpu + " is not yet supported");
102    }
103  }
104
105  public String getNameOfFile(String fileName) {
106    return new File(fileName).getName();
107  }
108
109  public ProcessControl getProcessControl() throws DebuggerException {
110    // FIXME: after stabs parser
111    return null;
112  }
113
114  public boolean canDemangle() {
115    return false;
116  }
117
118  public String demangle(String sym) {
119    throw new UnsupportedOperationException();
120  }
121}
122