001: /* Soot - a J*va Optimization Framework
002: * Copyright (C) 1997-1999 Raja Vallee-Rai
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the
016: * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017: * Boston, MA 02111-1307, USA.
018: */
019:
020: package soot.jbco.util;
021:
022: import java.util.HashMap;
023: import java.util.Iterator;
024:
025: import soot.baf.JSRInst;
026: import soot.baf.TableSwitchInst;
027: import soot.baf.TargetArgInst;
028: import soot.Body;
029: import soot.PatchingChain;
030: import soot.Trap;
031: import soot.Unit;
032:
033: public class Debugger {
034:
035: public static void printBaf(Body b) {
036:
037: System.out.println(b.getMethod().getName() + "\n");
038: int i = 0;
039: HashMap index = new HashMap();
040: Iterator it = b.getUnits().iterator();
041: while (it.hasNext())
042: index.put(it.next(), new Integer(i++));
043: it = b.getUnits().iterator();
044: while (it.hasNext()) {
045: Object o = it.next();
046: System.out.println(index.get(o).toString()
047: + " "
048: + o
049: + " "
050: + (o instanceof TargetArgInst ? index.get(
051: ((TargetArgInst) o).getTarget()).toString()
052: : ""));
053: }
054: System.out.println("\n");
055: }
056:
057: public static void printUnits(Body b, String msg) {
058: int i = 0;
059: HashMap<Unit, Integer> numbers = new HashMap<Unit, Integer>();
060: PatchingChain u = b.getUnits();
061: Iterator<Unit> it = u.snapshotIterator();
062: while (it.hasNext())
063: numbers.put(it.next(), new Integer(i++));
064:
065: int jsr = 0;
066: System.out.println("\r\r" + b.getMethod().getName() + " "
067: + msg);
068: Iterator udit = u.snapshotIterator();
069: while (udit.hasNext()) {
070: Unit unit = (Unit) udit.next();
071: Integer numb = numbers.get(unit);
072:
073: if (numb.intValue() == 149)
074: System.out.println("hi");
075:
076: if (unit instanceof TargetArgInst) {
077: if (unit instanceof JSRInst)
078: jsr++;
079: TargetArgInst ti = (TargetArgInst) unit;
080: if (ti.getTarget() == null) {
081: System.out
082: .println(unit
083: + " null null null null null null null null null");
084: continue;
085: }
086: System.out.println(numbers.get(unit).toString() + " "
087: + unit + " #"
088: + numbers.get(ti.getTarget()).toString());
089: continue;
090: } else if (unit instanceof TableSwitchInst) {
091: TableSwitchInst tswi = (TableSwitchInst) unit;
092: System.out.println(numbers.get(unit).toString()
093: + " SWITCH:");
094: System.out.println("\tdefault: "
095: + tswi.getDefaultTarget()
096: + " "
097: + numbers.get(tswi.getDefaultTarget())
098: .toString());
099: int index = 0;
100: for (int x = tswi.getLowIndex(); x <= tswi
101: .getHighIndex(); x++)
102: System.out.println("\t "
103: + x
104: + ": "
105: + tswi.getTarget(index)
106: + " "
107: + numbers.get(tswi.getTarget(index++))
108: .toString());
109: continue;
110: }
111: System.out.println(numb.toString() + " " + unit);
112: }
113:
114: Iterator tit = b.getTraps().iterator();
115: while (tit.hasNext()) {
116: Trap t = (Trap) tit.next();
117: System.out.println(numbers.get(t.getBeginUnit()) + " "
118: + t.getBeginUnit() + " to "
119: + numbers.get(t.getEndUnit()) + " "
120: + t.getEndUnit() + " at "
121: + numbers.get(t.getHandlerUnit()) + " "
122: + t.getHandlerUnit());
123: }
124: if (jsr > 0)
125: System.out.println("\r\tJSR Instructions: " + jsr);
126: }
127:
128: public static void printUnits(PatchingChain u, String msg) {
129: int i = 0;
130: HashMap<Unit, Integer> numbers = new HashMap<Unit, Integer>();
131: Iterator<Unit> it = u.snapshotIterator();
132: while (it.hasNext())
133: numbers.put(it.next(), new Integer(i++));
134:
135: int jsr = 0;
136: System.out.println("\r\r*********** " + msg);
137: Iterator udit = u.snapshotIterator();
138: while (udit.hasNext()) {
139: Unit unit = (Unit) udit.next();
140: Integer numb = numbers.get(unit);
141:
142: if (numb.intValue() == 149)
143: System.out.println("hi");
144:
145: if (unit instanceof TargetArgInst) {
146: if (unit instanceof JSRInst)
147: jsr++;
148: TargetArgInst ti = (TargetArgInst) unit;
149: if (ti.getTarget() == null) {
150: System.out
151: .println(unit
152: + " null null null null null null null null null");
153: continue;
154: }
155: System.out.println(numbers.get(unit).toString() + " "
156: + unit + " #"
157: + numbers.get(ti.getTarget()).toString());
158: continue;
159: } else if (unit instanceof TableSwitchInst) {
160: TableSwitchInst tswi = (TableSwitchInst) unit;
161: System.out.println(numbers.get(unit).toString()
162: + " SWITCH:");
163: System.out.println("\tdefault: "
164: + tswi.getDefaultTarget()
165: + " "
166: + numbers.get(tswi.getDefaultTarget())
167: .toString());
168: int index = 0;
169: for (int x = tswi.getLowIndex(); x <= tswi
170: .getHighIndex(); x++)
171: System.out.println("\t "
172: + x
173: + ": "
174: + tswi.getTarget(index)
175: + " "
176: + numbers.get(tswi.getTarget(index++))
177: .toString());
178: continue;
179: }
180: System.out.println(numb.toString() + " " + unit);
181: }
182: }
183: }
|