annotate agent/src/share/classes/sun/jvm/hotspot/debugger/cdbg/basic/BasicLineNumberMapping.java @ 0:a61af66fc99e jdk7-b24

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children c18cbe5936b8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
2 * Copyright 2001 Sun Microsystems, Inc. All Rights Reserved.
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
a61af66fc99e Initial load
duke
parents:
diff changeset
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
a61af66fc99e Initial load
duke
parents:
diff changeset
20 * CA 95054 USA or visit www.sun.com if you need additional information or
a61af66fc99e Initial load
duke
parents:
diff changeset
21 * have any questions.
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 package sun.jvm.hotspot.debugger.cdbg.basic;
a61af66fc99e Initial load
duke
parents:
diff changeset
26
a61af66fc99e Initial load
duke
parents:
diff changeset
27 import java.util.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
28 import sun.jvm.hotspot.debugger.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
29 import sun.jvm.hotspot.debugger.cdbg.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
30 import sun.jvm.hotspot.utilities.AddressOps;
a61af66fc99e Initial load
duke
parents:
diff changeset
31
a61af66fc99e Initial load
duke
parents:
diff changeset
32 public class BasicLineNumberMapping {
a61af66fc99e Initial load
duke
parents:
diff changeset
33 private List infoList;
a61af66fc99e Initial load
duke
parents:
diff changeset
34
a61af66fc99e Initial load
duke
parents:
diff changeset
35 public BasicLineNumberMapping() {
a61af66fc99e Initial load
duke
parents:
diff changeset
36 }
a61af66fc99e Initial load
duke
parents:
diff changeset
37
a61af66fc99e Initial load
duke
parents:
diff changeset
38 /** Add line number information for the given PC. The end PC may be
a61af66fc99e Initial load
duke
parents:
diff changeset
39 a very loose approximation (i.e., the end of the given DLL) if
a61af66fc99e Initial load
duke
parents:
diff changeset
40 that information is not available in the debug information.
a61af66fc99e Initial load
duke
parents:
diff changeset
41 recomputeEndPCs() will recompute them if needed. */
a61af66fc99e Initial load
duke
parents:
diff changeset
42 public void addLineNumberInfo(BasicLineNumberInfo info) {
a61af66fc99e Initial load
duke
parents:
diff changeset
43 if (infoList == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
44 infoList = new ArrayList();
a61af66fc99e Initial load
duke
parents:
diff changeset
45 }
a61af66fc99e Initial load
duke
parents:
diff changeset
46 infoList.add(info);
a61af66fc99e Initial load
duke
parents:
diff changeset
47 }
a61af66fc99e Initial load
duke
parents:
diff changeset
48
a61af66fc99e Initial load
duke
parents:
diff changeset
49 /** Sort the line number information by increasing starting program
a61af66fc99e Initial load
duke
parents:
diff changeset
50 counter. This must be done before any queries are made. */
a61af66fc99e Initial load
duke
parents:
diff changeset
51 public void sort() {
a61af66fc99e Initial load
duke
parents:
diff changeset
52 if (infoList == null) return;
a61af66fc99e Initial load
duke
parents:
diff changeset
53 Collections.sort(infoList, new Comparator() {
a61af66fc99e Initial load
duke
parents:
diff changeset
54 public int compare(Object o1, Object o2) {
a61af66fc99e Initial load
duke
parents:
diff changeset
55 BasicLineNumberInfo l1 = (BasicLineNumberInfo) o1;
a61af66fc99e Initial load
duke
parents:
diff changeset
56 BasicLineNumberInfo l2 = (BasicLineNumberInfo) o2;
a61af66fc99e Initial load
duke
parents:
diff changeset
57 Address a1 = l1.getStartPC();
a61af66fc99e Initial load
duke
parents:
diff changeset
58 Address a2 = l2.getStartPC();
a61af66fc99e Initial load
duke
parents:
diff changeset
59 if (AddressOps.lt(a1, a2)) { return -1; }
a61af66fc99e Initial load
duke
parents:
diff changeset
60 if (AddressOps.gt(a1, a2)) { return 1; }
a61af66fc99e Initial load
duke
parents:
diff changeset
61 return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
62 }
a61af66fc99e Initial load
duke
parents:
diff changeset
63 });
a61af66fc99e Initial load
duke
parents:
diff changeset
64 }
a61af66fc99e Initial load
duke
parents:
diff changeset
65
a61af66fc99e Initial load
duke
parents:
diff changeset
66 /** Recomputes the ending PCs of each interval based on the starting
a61af66fc99e Initial load
duke
parents:
diff changeset
67 PC of the next one. If this needs to be called, must be called
a61af66fc99e Initial load
duke
parents:
diff changeset
68 after sort(). */
a61af66fc99e Initial load
duke
parents:
diff changeset
69 public void recomputeEndPCs() {
a61af66fc99e Initial load
duke
parents:
diff changeset
70 if (infoList == null) return;
a61af66fc99e Initial load
duke
parents:
diff changeset
71 for (int i = 0; i < infoList.size() - 1; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
72 BasicLineNumberInfo i1 = get(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
73 BasicLineNumberInfo i2 = get(i + 1);
a61af66fc99e Initial load
duke
parents:
diff changeset
74 i1.setEndPC(i2.getStartPC());
a61af66fc99e Initial load
duke
parents:
diff changeset
75 }
a61af66fc99e Initial load
duke
parents:
diff changeset
76 }
a61af66fc99e Initial load
duke
parents:
diff changeset
77
a61af66fc99e Initial load
duke
parents:
diff changeset
78 public BasicLineNumberInfo lineNumberForPC(Address pc) throws DebuggerException {
a61af66fc99e Initial load
duke
parents:
diff changeset
79 if (infoList == null) return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
80 return searchLineNumbers(pc, 0, infoList.size() - 1);
a61af66fc99e Initial load
duke
parents:
diff changeset
81 }
a61af66fc99e Initial load
duke
parents:
diff changeset
82
a61af66fc99e Initial load
duke
parents:
diff changeset
83 public void iterate(LineNumberVisitor v) {
a61af66fc99e Initial load
duke
parents:
diff changeset
84 if (infoList == null) return;
a61af66fc99e Initial load
duke
parents:
diff changeset
85 for (int i = 0; i < infoList.size(); i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
86 v.doLineNumber(get(i));
a61af66fc99e Initial load
duke
parents:
diff changeset
87 }
a61af66fc99e Initial load
duke
parents:
diff changeset
88 }
a61af66fc99e Initial load
duke
parents:
diff changeset
89
a61af66fc99e Initial load
duke
parents:
diff changeset
90 //----------------------------------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
91 // Internals only below this point
a61af66fc99e Initial load
duke
parents:
diff changeset
92 //
a61af66fc99e Initial load
duke
parents:
diff changeset
93
a61af66fc99e Initial load
duke
parents:
diff changeset
94 private BasicLineNumberInfo get(int i) {
a61af66fc99e Initial load
duke
parents:
diff changeset
95 return (BasicLineNumberInfo) infoList.get(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
96 }
a61af66fc99e Initial load
duke
parents:
diff changeset
97
a61af66fc99e Initial load
duke
parents:
diff changeset
98 private BasicLineNumberInfo searchLineNumbers(Address addr, int lowIdx, int highIdx) {
a61af66fc99e Initial load
duke
parents:
diff changeset
99 if (highIdx < lowIdx) return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
100 if (lowIdx == highIdx) {
a61af66fc99e Initial load
duke
parents:
diff changeset
101 // Base case: see whether start PC is less than or equal to addr
a61af66fc99e Initial load
duke
parents:
diff changeset
102 if (check(addr, lowIdx)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
103 return get(lowIdx);
a61af66fc99e Initial load
duke
parents:
diff changeset
104 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
105 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
106 }
a61af66fc99e Initial load
duke
parents:
diff changeset
107 } else if (lowIdx == highIdx - 1) {
a61af66fc99e Initial load
duke
parents:
diff changeset
108 if (check(addr, lowIdx)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
109 return get(lowIdx);
a61af66fc99e Initial load
duke
parents:
diff changeset
110 } else if (check(addr, highIdx)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
111 return get(highIdx);
a61af66fc99e Initial load
duke
parents:
diff changeset
112 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
113 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
114 }
a61af66fc99e Initial load
duke
parents:
diff changeset
115 }
a61af66fc99e Initial load
duke
parents:
diff changeset
116 int midIdx = (lowIdx + highIdx) >> 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
117 BasicLineNumberInfo info = get(midIdx);
a61af66fc99e Initial load
duke
parents:
diff changeset
118 if (AddressOps.lt(addr, info.getStartPC())) {
a61af66fc99e Initial load
duke
parents:
diff changeset
119 // Always move search down
a61af66fc99e Initial load
duke
parents:
diff changeset
120 return searchLineNumbers(addr, lowIdx, midIdx);
a61af66fc99e Initial load
duke
parents:
diff changeset
121 } else if (AddressOps.equal(addr, info.getStartPC())) {
a61af66fc99e Initial load
duke
parents:
diff changeset
122 return info;
a61af66fc99e Initial load
duke
parents:
diff changeset
123 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
124 // Move search up
a61af66fc99e Initial load
duke
parents:
diff changeset
125 return searchLineNumbers(addr, midIdx, highIdx);
a61af66fc99e Initial load
duke
parents:
diff changeset
126 }
a61af66fc99e Initial load
duke
parents:
diff changeset
127 }
a61af66fc99e Initial load
duke
parents:
diff changeset
128
a61af66fc99e Initial load
duke
parents:
diff changeset
129 private boolean check(Address addr, int idx) {
a61af66fc99e Initial load
duke
parents:
diff changeset
130 BasicLineNumberInfo info = get(idx);
a61af66fc99e Initial load
duke
parents:
diff changeset
131 if (AddressOps.lte(info.getStartPC(), addr)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
132 return true;
a61af66fc99e Initial load
duke
parents:
diff changeset
133 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
134 return false;
a61af66fc99e Initial load
duke
parents:
diff changeset
135 }
a61af66fc99e Initial load
duke
parents:
diff changeset
136 }
a61af66fc99e Initial load
duke
parents:
diff changeset
137 }