PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/netbeans-7.3/cnd.dwarfdump/src/org/netbeans/modules/cnd/dwarfdump/dwarf/DwarfEntry.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 511 lines | 353 code | 94 blank | 64 comment | 81 complexity | cb98b05d3754dfacf869f56a8dc687cc MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 1997-2010 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * Contributor(s):
  28. *
  29. * The Original Software is NetBeans. The Initial Developer of the Original
  30. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
  31. * Microsystems, Inc. All Rights Reserved.
  32. *
  33. * If you wish your version of this file to be governed by only the CDDL
  34. * or only the GPL Version 2, indicate your decision by adding
  35. * "[Contributor] elects to include this software in this distribution
  36. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  37. * single choice of license, a recipient has the option to distribute
  38. * your version of this file under either the CDDL, the GPL Version 2 or
  39. * to extend the choice of license to its licensees as provided above.
  40. * However, if you add GPL Version 2 code and therefore, elected the GPL
  41. * Version 2 license, then the option applies only if the new code is
  42. * made subject to such option by the copyright holder.
  43. */
  44. package org.netbeans.modules.cnd.dwarfdump.dwarf;
  45. import java.io.ByteArrayOutputStream;
  46. import java.io.IOException;
  47. import org.netbeans.modules.cnd.dwarfdump.CompilationUnit;
  48. import org.netbeans.modules.cnd.dwarfdump.dwarfconsts.ACCESS;
  49. import org.netbeans.modules.cnd.dwarfdump.dwarfconsts.ATTR;
  50. import org.netbeans.modules.cnd.dwarfdump.dwarfconsts.TAG;
  51. import java.io.PrintStream;
  52. import java.util.ArrayList;
  53. import java.util.Collections;
  54. import java.util.Iterator;
  55. import java.util.List;
  56. import org.netbeans.modules.cnd.dwarfdump.dwarfconsts.VIS;
  57. import org.netbeans.modules.cnd.dwarfdump.reader.ByteStreamReader;
  58. /**
  59. *
  60. * @author ak119685
  61. */
  62. public class DwarfEntry {
  63. private final CompilationUnit compilationUnit;
  64. private final DwarfAbbriviationTableEntry abbriviationTableEntry;
  65. private final List<Object> values;
  66. private final List<DwarfEntry> children;
  67. private final long refference;
  68. private final int hierarchyLevel;
  69. private String qualifiedName = null;
  70. private String name = null;
  71. private DwarfEntry parent;
  72. /** Creates a new instance of DwarfEntry */
  73. public DwarfEntry(CompilationUnit compilationUnit, DwarfAbbriviationTableEntry abbrEntry, long refference, int hierarchyLevel) {
  74. this.abbriviationTableEntry = abbrEntry;
  75. this.compilationUnit = compilationUnit;
  76. this.refference = refference;
  77. this.hierarchyLevel = hierarchyLevel;
  78. if (abbriviationTableEntry.hasChildren()) {
  79. children = new ArrayList<DwarfEntry>();
  80. } else {
  81. children = Collections.<DwarfEntry>emptyList();
  82. }
  83. values = new ArrayList<Object>(abbriviationTableEntry.getAttributesCount());
  84. }
  85. public TAG getKind() {
  86. return abbriviationTableEntry.getKind();
  87. }
  88. public String getName() throws IOException {
  89. if (name == null) {
  90. Object nameAttr = getAttributeValue(ATTR.DW_AT_name);
  91. name = (nameAttr == null) ? "" : stripComments((String)nameAttr); // NOI18N
  92. }
  93. return name;
  94. }
  95. public String getQualifiedName() throws IOException {
  96. if (qualifiedName == null) {
  97. DwarfEntry specification = getSpecification();
  98. if (specification != null) {
  99. return specification.getQualifiedName();
  100. }
  101. qualifiedName = constructQualifiedName();
  102. }
  103. return qualifiedName;
  104. }
  105. private String constructQualifiedName() throws IOException {
  106. if (parent == null) {
  107. return getName();
  108. }
  109. TAG kind = parent.getKind();
  110. switch (kind) {
  111. case DW_TAG_compile_unit:
  112. return getName();
  113. case DW_TAG_lexical_block:
  114. return getName();
  115. }
  116. String aName = getName();
  117. String pname = parent.getQualifiedName();
  118. String qname = (pname != null && aName != null && !pname.equals("") && !aName.equals("")) ? pname + "::" + aName : aName; // NOI18N
  119. return qname;
  120. }
  121. private String stripComments(String str) {
  122. if (str == null) {
  123. return null;
  124. }
  125. int idx = str.indexOf('#');
  126. if (idx != -1) {
  127. str = str.substring(0, idx);
  128. }
  129. return str.trim();
  130. }
  131. public void setQualifiedName(String qualifiedName) throws IOException {
  132. this.qualifiedName = qualifiedName;
  133. DwarfEntry origin = getAbstractOrigin();
  134. if (origin != null) {
  135. origin.setQualifiedName(qualifiedName);
  136. }
  137. DwarfEntry specification = getSpecification();
  138. if (specification != null) {
  139. specification.setQualifiedName(qualifiedName);
  140. }
  141. }
  142. public String getType() throws IOException {
  143. return compilationUnit.getType(this);
  144. }
  145. public int getUintAttributeValue(ATTR attr) throws IOException {
  146. Object value = getAttributeValue(attr);
  147. if (value == null) {
  148. return -1;
  149. }
  150. int result = ((Number)value).intValue();
  151. if (result < 0) {
  152. result &= 0xFF;
  153. }
  154. return result;
  155. }
  156. public Object getAttributeValue(ATTR attr) throws IOException {
  157. return getAttributeValue(attr, true);
  158. }
  159. public Object getAttributeValue(ATTR attr, boolean recursive) throws IOException {
  160. Object attrValue = null;
  161. // Get the index of this attribute from the abbriviation table entry
  162. // associated with this one.
  163. int attrIdx = abbriviationTableEntry.getAttribute(attr);
  164. // If there is no such attribute in this entry - try to get this
  165. // attribute from "abstract origin" or "specification" entry (if any)
  166. if (attrIdx == -1) {
  167. if (recursive) {
  168. Integer offset = -1;
  169. if (abbriviationTableEntry.getAttribute(ATTR.DW_AT_abstract_origin) >= 0) {
  170. offset = (Integer)getAttributeValue(ATTR.DW_AT_abstract_origin);
  171. } else if (abbriviationTableEntry.getAttribute(ATTR.DW_AT_specification) >= 0) {
  172. offset = (Integer)getAttributeValue(ATTR.DW_AT_specification);
  173. }
  174. if (offset >= 0) {
  175. DwarfEntry attrEntry = compilationUnit.getEntry(offset);
  176. if (attrEntry != null) {
  177. attrValue = attrEntry.getAttributeValue(attr);
  178. }
  179. }
  180. }
  181. } else {
  182. // Attribute has been found
  183. attrValue = values.get(attrIdx);
  184. }
  185. return attrValue;
  186. }
  187. public void addValue(Object value) {
  188. values.add(value);
  189. }
  190. public List<DwarfEntry> getChildren() {
  191. return children;
  192. }
  193. /**
  194. * Gets an entry, for which this entry is referred as specification
  195. * (via DW_AT_specification).
  196. * Note that this works only after all entries have been read.
  197. */
  198. public DwarfEntry getDefinition() throws IOException {
  199. return compilationUnit.getDefinition(this);
  200. }
  201. /**
  202. * Gets an entry that is referred by this is entry as specification
  203. * (via DW_AT_specification).
  204. * Note that this works only after all entries have been read.
  205. */
  206. public DwarfEntry getSpecification() throws IOException {
  207. Object o = getAttributeValue(ATTR.DW_AT_specification);
  208. if( o instanceof Integer ) {
  209. return compilationUnit.getEntry(((Integer) o).intValue());
  210. }
  211. return null;
  212. }
  213. public DwarfEntry getAbstractOrigin() throws IOException {
  214. Object o = getAttributeValue(ATTR.DW_AT_abstract_origin);
  215. if (o instanceof Integer) {
  216. return compilationUnit.getEntry(((Integer)o).intValue());
  217. }
  218. return null;
  219. }
  220. public boolean hasChildren() {
  221. return abbriviationTableEntry.hasChildren();
  222. }
  223. public void addChild(DwarfEntry child) {
  224. children.add(child);
  225. child.setParent(this);
  226. }
  227. public DwarfEntry getParent() {
  228. return parent;
  229. }
  230. private void setParent(DwarfEntry parent) {
  231. this.parent = parent;
  232. }
  233. public long getRefference() {
  234. return refference;
  235. }
  236. public String getParametersString() throws IOException {
  237. return getParametersString(true);
  238. }
  239. public String getParametersString(boolean withNames) throws IOException {
  240. List<DwarfEntry> params = getParameters();
  241. StringBuilder paramStr = new StringBuilder(); // NOI18N
  242. DwarfEntry param = null;
  243. paramStr.append('(');
  244. for (Iterator<DwarfEntry> it = params.iterator(); it.hasNext();) {
  245. param = it.next();
  246. if (param.getKind().equals(TAG.DW_TAG_unspecified_parameters)) {
  247. paramStr.append("..."); // NOI18N
  248. } else {
  249. paramStr.append(param.getType());
  250. if (withNames) {
  251. paramStr.append(" "); // NOI18N
  252. paramStr.append(param.getName());
  253. }
  254. }
  255. if (it.hasNext()) {
  256. paramStr.append(", "); // NOI18N
  257. }
  258. }
  259. paramStr.append(')'); // NOI18N
  260. return paramStr.toString();
  261. }
  262. public DwarfDeclaration getDeclaration() throws IOException {
  263. TAG kind = getKind();
  264. String aName = getQualifiedName();
  265. String type = getType();
  266. String paramStr = ""; // NOI18N
  267. if (kind.equals(TAG.DW_TAG_subprogram)) {
  268. paramStr += getParametersString();
  269. }
  270. String declarationString = type + " " + (aName == null ? getName() : aName) + paramStr; // NOI18N
  271. int declarationLine = getLine();
  272. int declarationColumn = getColumn();
  273. String declarationPosition = ((declarationLine == -1) ? "" : declarationLine) + // NOI18N
  274. ((declarationColumn == -1) ? "" : ":" + declarationColumn); // NOI18N
  275. declarationPosition += " <" + refference + " (0x" + Long.toHexString(refference) + ")>"; // NOI18N
  276. return new DwarfDeclaration(kind.toString(), declarationString, getDeclarationFilePath(), declarationPosition);
  277. }
  278. public int getLine() throws IOException {
  279. return getUintAttributeValue(ATTR.DW_AT_decl_line);
  280. }
  281. public int getColumn() throws IOException {
  282. return getUintAttributeValue(ATTR.DW_AT_decl_column);
  283. }
  284. public List<DwarfEntry> getParameters() throws IOException {
  285. List<DwarfEntry> result = new ArrayList<DwarfEntry>();
  286. List<DwarfEntry> aChildren = getChildren();
  287. for (DwarfEntry child: aChildren) {
  288. if (child.isParameter() && !child.isArtifitial()) {
  289. result.add(child);
  290. }
  291. }
  292. return result;
  293. }
  294. public List<DwarfEntry> getMembers() throws IOException {
  295. List<DwarfEntry> result = new ArrayList<DwarfEntry>();
  296. List<DwarfEntry> aChildren = getChildren();
  297. for (DwarfEntry child: aChildren) {
  298. if (child.isMember() && !child.isArtifitial()) {
  299. result.add(child);
  300. }
  301. }
  302. return result;
  303. }
  304. public TAG getTag() {
  305. return abbriviationTableEntry.getKind();
  306. }
  307. public void dump(PrintStream out) {
  308. out.print("<" + hierarchyLevel + "><" + Long.toHexString(refference) + ">: "); // NOI18N
  309. abbriviationTableEntry.dump(out, this);
  310. for (int i = 0; i < children.size(); i++) {
  311. children.get(i).dump(out);
  312. }
  313. }
  314. @Override
  315. public String toString() {
  316. ByteArrayOutputStream st = new ByteArrayOutputStream();
  317. PrintStream out = new PrintStream(st);
  318. dump(out);
  319. return st.toString();
  320. }
  321. public boolean isArtifitial() throws IOException {
  322. Object isArt = getAttributeValue(ATTR.DW_AT_artificial);
  323. return ((isArt != null) && ((Boolean)isArt).booleanValue());
  324. }
  325. public boolean hasAbastractOrigin() throws IOException {
  326. Object abastractOrigin = getAttributeValue(ATTR.DW_AT_abstract_origin);
  327. return (abastractOrigin != null);
  328. }
  329. public boolean isExternal() throws IOException {
  330. Object result = getAttributeValue(ATTR.DW_AT_external);
  331. return ((result != null) && ((Boolean)result).booleanValue());
  332. }
  333. public VIS getVisibility() throws IOException {
  334. Object result = getAttributeValue(ATTR.DW_AT_visibility);
  335. if (result instanceof Byte) {
  336. return VIS.get((Byte)result);
  337. }
  338. return null;
  339. }
  340. public boolean isNamespace() {
  341. return getKind().equals(TAG.DW_TAG_namespace);
  342. }
  343. public ACCESS getAccessibility() throws IOException {
  344. Object result = getAttributeValue(ATTR.DW_AT_accessibility);
  345. return (result == null) ? null : ACCESS.get(((Number)result).intValue());
  346. }
  347. public boolean isParameter() {
  348. TAG kind = getKind();
  349. return kind.equals(TAG.DW_TAG_formal_parameter) || kind.equals(TAG.DW_TAG_unspecified_parameters);
  350. }
  351. public boolean isMember() {
  352. TAG kind = getKind();
  353. //return kind.equals(TAG.DW_TAG_member);
  354. return !kind.equals(TAG.DW_TAG_inheritance);
  355. }
  356. public boolean isEntryDefinedInFile(int fileEntryIdx) throws IOException {
  357. int fileIdx = getUintAttributeValue(ATTR.DW_AT_decl_file);
  358. return (fileIdx == fileEntryIdx);
  359. }
  360. public String getDeclarationFilePath() throws IOException {
  361. int fileIdx = (Integer)getUintAttributeValue(ATTR.DW_AT_decl_file);
  362. return (fileIdx <= 0) ? null : compilationUnit.getStatementList().getFilePath(fileIdx);
  363. }
  364. public String getTypeDef() throws IOException {
  365. if (getKind().equals(TAG.DW_TAG_typedef)) {
  366. return getType();
  367. }
  368. Object typeRefIdx = getAttributeValue(ATTR.DW_AT_type);
  369. DwarfEntry typeRef = null;
  370. if (typeRefIdx instanceof Integer) {
  371. typeRef = compilationUnit.getTypedefFor((Integer)typeRefIdx);
  372. } else if (typeRefIdx instanceof Long) {
  373. typeRef = compilationUnit.getTypedefFor((Long)typeRefIdx);
  374. }
  375. return (typeRef == null) ? getType() : typeRef.getType();
  376. }
  377. public long getSibling() throws IOException{
  378. Object refIdx = getAttributeValue(ATTR.DW_AT_sibling, false);
  379. if (refIdx instanceof Integer) {
  380. return ((Integer)refIdx).intValue();
  381. } else if (refIdx instanceof Long) {
  382. return ((Long)refIdx).longValue();
  383. }
  384. return -1;
  385. }
  386. List<Object> getValues() {
  387. return values;
  388. }
  389. public long getLowAddress() throws IOException{
  390. byte[] bytes = (byte[])getAttributeValue(ATTR.DW_AT_low_pc);
  391. if (bytes != null) {
  392. return getAddress(bytes);
  393. }
  394. return 0;
  395. }
  396. public long getHighAddress() throws IOException{
  397. byte[] bytes = (byte[])getAttributeValue(ATTR.DW_AT_high_pc);
  398. if (bytes != null) {
  399. return getAddress(bytes);
  400. }
  401. return 0;
  402. }
  403. public long getAddress(byte[] bytes){
  404. long n = 0;
  405. int size = bytes.length;
  406. for (int i = 0; i < size; i++) {
  407. long u = 0;
  408. if (compilationUnit.getDataEncoding() == ByteStreamReader.LSB) {
  409. u = (0xff & bytes[i]);
  410. } else {
  411. u = (0xff & bytes[size - i - 1]);
  412. }
  413. n |= (u << (i * 8));
  414. }
  415. return n;
  416. }
  417. }