ObjectMap.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* ObjectMap.java -- Contains a map of all objects keyed by their addresses.
  2. Copyright (C) 2007 Free Software Foundation
  3. This file is part of libgcj.
  4. This software is copyrighted work licensed under the terms of the
  5. Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
  6. details. */
  7. package gnu.gcj.tools.gc_analyze;
  8. import java.io.BufferedReader;
  9. import java.io.IOException;
  10. import java.util.Iterator;
  11. import java.util.Map;
  12. import java.util.TreeMap;
  13. class ObjectMap implements Iterable<Map.Entry<Long, ObjectMap.ObjectItem>>
  14. {
  15. class ObjectItem
  16. {
  17. int used;
  18. int size;
  19. int kind;
  20. long klass;
  21. long data;
  22. long ptr;
  23. String typeName;
  24. String string; // only for string objects
  25. boolean stringData; // character array pointed to by a string
  26. ObjectItem reference; // object at reference points to this
  27. ItemList points_to = new ItemList();
  28. ItemList pointed_by = new ItemList();
  29. }
  30. private TreeMap<Long, ObjectItem> map = new TreeMap<Long, ObjectItem>();
  31. public Iterator<Map.Entry<Long, ObjectItem>> iterator()
  32. {
  33. return map.entrySet().iterator();
  34. }
  35. public ObjectItem get(long ptr)
  36. {
  37. ObjectItem item = map.get(ptr);
  38. return item;
  39. }
  40. public ObjectMap(BufferedReader reader) throws IOException
  41. {
  42. outer_loop:
  43. for (;;)
  44. {
  45. String s = reader.readLine();
  46. if (s == null)
  47. break;
  48. if (s.indexOf("Begin object map") >= 0)
  49. {
  50. for (;;)
  51. {
  52. s = reader.readLine();
  53. if (s.indexOf("End object map") >= 0)
  54. break outer_loop;
  55. String[] items = s.split(",");
  56. ObjectItem item = new ObjectItem();
  57. long ptr = 0;
  58. for (int i=0; i<items.length; i++)
  59. {
  60. String[] x = items[i].split(" ");
  61. String last = x[x.length-1];
  62. switch (i)
  63. {
  64. case 0:
  65. item.used = Integer.parseInt(last);
  66. break;
  67. case 1:
  68. ptr = MemoryMap.parseHexLong(last.substring(2));
  69. break;
  70. case 2:
  71. item.size = Integer.parseInt(last);
  72. break;
  73. case 3:
  74. item.kind = Integer.parseInt(last);
  75. break;
  76. case 4:
  77. if (last.length() > 1)
  78. item.klass =
  79. MemoryMap.parseHexLong(last.substring(2));
  80. else
  81. item.klass = Integer.parseInt(last,16);
  82. break;
  83. case 5:
  84. try
  85. {
  86. item.data =
  87. Integer.parseInt(last.substring(2), 16);
  88. }
  89. catch (Exception e)
  90. {
  91. item.data = 0;
  92. }
  93. break;
  94. }
  95. }
  96. item.ptr = ptr;
  97. map.put(ptr, item);
  98. } // inner loop
  99. } // started inner loop
  100. } // outer loop - finding begin
  101. for (Map.Entry<Long, ObjectItem> me : this)
  102. {
  103. ObjectItem item = me.getValue();
  104. if (item.data != 0)
  105. {
  106. // see if data is a pointer to a block
  107. ObjectItem referenced = map.get(item.data);
  108. if (referenced != null)
  109. {
  110. referenced.reference = item;
  111. }
  112. }
  113. }
  114. } // memoryMap
  115. public void dump()
  116. {
  117. for (Map.Entry<Long, ObjectItem> me : this)
  118. {
  119. long ptr = me.getKey();
  120. ObjectItem item = me.getValue();
  121. System.out.println("ptr = " + Long.toHexString(ptr)
  122. + ", size = " + item.size
  123. + ", klass = " + Long.toHexString(item.klass)
  124. + ", kind = " + item.kind
  125. + ", data = " + item.data);
  126. }
  127. }
  128. }