001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package test.dwarfdump;
043:
044: import java.util.TreeSet;
045: import org.netbeans.modules.cnd.dwarfdump.CompilationUnit;
046: import org.netbeans.modules.cnd.dwarfdump.Dwarf;
047: import org.netbeans.modules.cnd.dwarfdump.dwarf.DwarfMacinfoTable;
048: import org.netbeans.modules.cnd.dwarfdump.dwarfconsts.SECTIONS;
049: import org.netbeans.modules.cnd.dwarfdump.reader.DwarfReader;
050: import org.netbeans.modules.cnd.dwarfdump.section.DwarfArangesSection;
051: import org.netbeans.modules.cnd.dwarfdump.section.DwarfDebugInfoSection;
052: import java.io.File;
053: import java.io.FileNotFoundException;
054: import java.io.IOException;
055: import java.io.PrintStream;
056: import java.util.ArrayList;
057: import java.util.Iterator;
058: import java.util.List;
059: import modelutils.Config;
060:
061: /**
062: *
063: * @author ak119685
064: */
065: public class DwarfDump {
066:
067: Dwarf dwarf = null;
068:
069: public DwarfDump(String objFileName) throws FileNotFoundException,
070: IOException {
071: this .dwarf = new Dwarf(objFileName);
072: }
073:
074: public static void main(String[] args) {
075: try {
076: Config config = new Config("faido:", args); // NOI18N
077: DwarfDump dwarfDump = new DwarfDump(config.getArgument());
078:
079: String dumpFile = config.getParameterFor("-o"); // NOI18N
080: PrintStream dumpStream = null;
081:
082: if (dumpFile != null) {
083: System.err.println("Dumping DWARF to " + dumpFile);
084: dumpStream = new PrintStream(dumpFile);
085: } else {
086: dumpStream = System.out;
087: }
088:
089: if (config.flagSet("-i")) { // NOI18N
090: dwarfDump.dumpIncludes(dumpStream);
091: }
092:
093: if (config.flagSet("-d")) { // NOI18N
094: dwarfDump.dumpDefines(dumpStream);
095: }
096:
097: if (config.flagSet("-a")) { // NOI18N
098: dwarfDump.dump(dumpStream);
099: }
100:
101: if (config.flagSet("-f")) { // NOI18N
102: dwarfDump.dumpFiles(dumpStream);
103: }
104:
105: } catch (Exception ex) {
106: System.err.println("Fatal error: " + ex.getMessage());
107: ex.printStackTrace(System.err);
108: }
109: }
110:
111: public CompilationUnit getCompilationUnit(String srcFileFullName) {
112: for (CompilationUnit unit : getCompilationUnits()) {
113: // TODO: remove hack
114:
115: String srcFileName = srcFileFullName
116: .substring(srcFileFullName
117: .lastIndexOf(File.separatorChar));
118: String unitFileName = unit.getSourceFileFullName();
119: unitFileName = unitFileName.substring(unitFileName
120: .lastIndexOf(File.separatorChar));
121:
122: if (unitFileName.equals(srcFileName)) {
123: //if (unit.getSourceFileFullName().equals(srcFileFullName)) {
124: return unit;
125: }
126: }
127:
128: return null;
129: }
130:
131: public List<CompilationUnit> getCompilationUnits() {
132: DwarfDebugInfoSection debugInfo = (DwarfDebugInfoSection) dwarf
133: .getSection(SECTIONS.DEBUG_INFO);
134: List<CompilationUnit> result = null;
135:
136: if (debugInfo != null) {
137: try {
138: result = debugInfo.getCompilationUnits();
139: } catch (IOException ex) {
140: ex.printStackTrace();
141: }
142: } else {
143: return new ArrayList<CompilationUnit>();
144: }
145:
146: return (result == null) ? new ArrayList<CompilationUnit>()
147: : result;
148: }
149:
150: public void dump(PrintStream out) {
151: out.println("Dwarf dump for " + dwarf.getFileName() + "\n"); // NOI18N
152:
153: // ElfSection stringsSection = reader.getSection(".shstrtab");
154: // stringsSection.dump(out);
155:
156: DwarfArangesSection aranges = (DwarfArangesSection) dwarf
157: .getSection(SECTIONS.DEBUG_ARANGES);
158:
159: if (aranges != null) {
160: aranges.dump(out);
161: }
162:
163: List<CompilationUnit> compUnits = getCompilationUnits();
164: if (compUnits != null) {
165: for (CompilationUnit cu : compUnits) {
166: cu.dump(out);
167: }
168: }
169: }
170:
171: private void dumpIncludes(PrintStream out) {
172: List<CompilationUnit> CUs = getCompilationUnits();
173:
174: if (CUs == null) {
175: out.println("No Compilation Units found for"
176: + this .dwarf.getFileName()); // NOI18N
177: return;
178: }
179:
180: for (CompilationUnit cu : CUs) {
181: out.println("Include directories for "
182: + cu.getSourceFileFullName()); // NOI18N
183: List<String> dirs = cu.getStatementList()
184: .getIncludeDirectories();
185: String basedir = cu.getCompilationDir();
186:
187: for (String dir : dirs) {
188: if (!(dir.startsWith("/") || dir.startsWith("\\"))) { // NOI18N
189: dir = basedir + File.separator + dir;
190: }
191:
192: try {
193: out.println(new File(dir).getCanonicalPath());
194: } catch (IOException ex) {
195: ex.printStackTrace();
196: }
197: }
198: out.println();
199: }
200: }
201:
202: private void dumpDefines(PrintStream out) {
203: for (CompilationUnit cu : getCompilationUnits()) {
204: out.println("Defines for " + cu.getSourceFileFullName()); // NOI18N
205: DwarfMacinfoTable macroTable = cu.getMacrosTable();
206:
207: if (macroTable == null) {
208: out
209: .println("No defines found for this CU. Be sure to compile with -g3 -gdwarf-2 flags"); // NOI18N
210: } else {
211: List<String> dirs = macroTable.getCommandLineDefines();
212: for (String dir : dirs) {
213: out.println(dir);
214: }
215: }
216: out.println();
217: }
218: }
219:
220: private void dumpFiles(PrintStream out) {
221: out.println("Files used for building " + dwarf.getFileName()); // NOI18N
222:
223: TreeSet<String> paths = new TreeSet();
224:
225: for (CompilationUnit cu : getCompilationUnits()) {
226: for (String path : cu.getStatementList().getFilePaths()) {
227: path = path.replaceAll("\\\\", "/"); // NOI18N
228:
229: if (!paths.contains(path)) {
230: paths.add(path);
231: }
232: }
233: }
234:
235: for (String path : paths) {
236: out.println(path);
237: }
238: }
239: }
|