95 return null; 96 } 97 return charArrayToString(charArray, 0, (int) charArray.getLength()); 98 } 99 100 public static String charArrayToString(TypeArray charArray, int offset, int length) { 101 if (charArray == null) { 102 return null; 103 } 104 final int limit = offset + length; 105 if (Assert.ASSERTS_ENABLED) { 106 Assert.that(offset >= 0 && limit <= charArray.getLength(), "out of bounds"); 107 } 108 StringBuffer buf = new StringBuffer(length); 109 for (int i = offset; i < limit; i++) { 110 buf.append(charArray.getCharAt(i)); 111 } 112 return buf.toString(); 113 } 114 115 public static String stringOopToString(Oop stringOop) { 116 if (offsetField == null) { 117 InstanceKlass k = (InstanceKlass) stringOop.getKlass(); 118 offsetField = (IntField) k.findField("offset", "I"); 119 countField = (IntField) k.findField("count", "I"); 120 valueField = (OopField) k.findField("value", "[C"); 121 if (Assert.ASSERTS_ENABLED) { 122 Assert.that(offsetField != null && 123 countField != null && 124 valueField != null, "must find all java.lang.String fields"); 125 } 126 } 127 return charArrayToString((TypeArray) valueField.getValue(stringOop), 128 offsetField.getValue(stringOop), 129 countField.getValue(stringOop)); 130 } 131 132 private static void initThreadGroupFields() { 133 if (threadGroupParentField == null) { 134 SystemDictionary sysDict = VM.getVM().getSystemDictionary(); 135 InstanceKlass k = sysDict.getThreadGroupKlass(); 136 threadGroupParentField = (OopField) k.findField("parent", "Ljava/lang/ThreadGroup;"); 137 threadGroupNameField = (OopField) k.findField("name", "Ljava/lang/String;"); 138 threadGroupNThreadsField = (IntField) k.findField("nthreads", "I"); 139 threadGroupThreadsField = (OopField) k.findField("threads", "[Ljava/lang/Thread;"); 140 threadGroupNGroupsField = (IntField) k.findField("ngroups", "I"); 141 threadGroupGroupsField = (OopField) k.findField("groups", "[Ljava/lang/ThreadGroup;"); 142 if (Assert.ASSERTS_ENABLED) { 143 Assert.that(threadGroupParentField != null && 144 threadGroupNameField != null && 145 threadGroupNThreadsField != null && 146 threadGroupThreadsField != null && 147 threadGroupNGroupsField != null && 148 threadGroupGroupsField != null, "must find all java.lang.ThreadGroup fields"); 149 } 150 } 151 } | 95 return null; 96 } 97 return charArrayToString(charArray, 0, (int) charArray.getLength()); 98 } 99 100 public static String charArrayToString(TypeArray charArray, int offset, int length) { 101 if (charArray == null) { 102 return null; 103 } 104 final int limit = offset + length; 105 if (Assert.ASSERTS_ENABLED) { 106 Assert.that(offset >= 0 && limit <= charArray.getLength(), "out of bounds"); 107 } 108 StringBuffer buf = new StringBuffer(length); 109 for (int i = offset; i < limit; i++) { 110 buf.append(charArray.getCharAt(i)); 111 } 112 return buf.toString(); 113 } 114 115 public static String escapeString(String s) { 116 StringBuilder sb = null; 117 for (int index = 0; index < s.length(); index++) { 118 char value = s.charAt(index); 119 if (value >= 32 && value < 127 || value == '\'' || value == '\\') { 120 if (sb != null) { 121 sb.append(value); 122 } 123 } else { 124 if (sb == null) { 125 sb = new StringBuilder(s.length() * 2); 126 sb.append(s, 0, index); 127 } 128 sb.append("\\u"); 129 if (value < 0x10) sb.append("000"); 130 else if (value < 0x100) sb.append("00"); 131 else if (value < 0x1000) sb.append("0"); 132 sb.append(Integer.toHexString(value)); 133 } 134 } 135 if (sb != null) { 136 return sb.toString(); 137 } 138 return s; 139 } 140 141 public static String stringOopToString(Oop stringOop) { 142 if (offsetField == null) { 143 InstanceKlass k = (InstanceKlass) stringOop.getKlass(); 144 offsetField = (IntField) k.findField("offset", "I"); 145 countField = (IntField) k.findField("count", "I"); 146 valueField = (OopField) k.findField("value", "[C"); 147 if (Assert.ASSERTS_ENABLED) { 148 Assert.that(offsetField != null && 149 countField != null && 150 valueField != null, "must find all java.lang.String fields"); 151 } 152 } 153 return charArrayToString((TypeArray) valueField.getValue(stringOop), 154 offsetField.getValue(stringOop), 155 countField.getValue(stringOop)); 156 } 157 158 public static String stringOopToEscapedString(Oop stringOop) { 159 return escapeString(stringOopToString(stringOop)); 160 } 161 162 private static void initThreadGroupFields() { 163 if (threadGroupParentField == null) { 164 SystemDictionary sysDict = VM.getVM().getSystemDictionary(); 165 InstanceKlass k = sysDict.getThreadGroupKlass(); 166 threadGroupParentField = (OopField) k.findField("parent", "Ljava/lang/ThreadGroup;"); 167 threadGroupNameField = (OopField) k.findField("name", "Ljava/lang/String;"); 168 threadGroupNThreadsField = (IntField) k.findField("nthreads", "I"); 169 threadGroupThreadsField = (OopField) k.findField("threads", "[Ljava/lang/Thread;"); 170 threadGroupNGroupsField = (IntField) k.findField("ngroups", "I"); 171 threadGroupGroupsField = (OopField) k.findField("groups", "[Ljava/lang/ThreadGroup;"); 172 if (Assert.ASSERTS_ENABLED) { 173 Assert.that(threadGroupParentField != null && 174 threadGroupNameField != null && 175 threadGroupNThreadsField != null && 176 threadGroupThreadsField != null && 177 threadGroupNGroupsField != null && 178 threadGroupGroupsField != null, "must find all java.lang.ThreadGroup fields"); 179 } 180 } 181 } |