1 /* 2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2019 SAP SE. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 * 24 */ 25 26 // AbstractDisassembler is the base class for 27 // platform-specific Disassembler classes. 28 29 #include "precompiled.hpp" 30 #include "asm/assembler.inline.hpp" 31 #include "compiler/abstractDisassembler.hpp" 32 #include "oops/oop.inline.hpp" 33 #include "utilities/debug.hpp" 34 #include "utilities/ostream.hpp" 35 36 // Default values for what is being printed as line prefix when disassembling a single instruction. 37 // Can be overridden by command line parameter PrintAssemblyOptions. 38 bool AbstractDisassembler::_show_data_hex = true; 39 bool AbstractDisassembler::_show_data_int = false; 40 bool AbstractDisassembler::_show_data_float = false; 41 bool AbstractDisassembler::_align_instr = false; 42 bool AbstractDisassembler::_show_pc = true; 43 bool AbstractDisassembler::_show_offset = false; 44 bool AbstractDisassembler::_show_structs = false; 45 bool AbstractDisassembler::_show_comment = false; 46 bool AbstractDisassembler::_show_block_comment = false; 47 #if defined(ARM) || defined(AARCH64) 48 bool AbstractDisassembler::_show_bytes = false; // set "true" to see what's in memory bit by bit 49 // might prove cumbersome because instr_len is hard to find on arm 50 #endif 51 #if defined(PPC) 52 bool AbstractDisassembler::_show_bytes = false; // set "true" to see what's in memory bit by bit 53 #endif 54 #if defined(S390) 55 bool AbstractDisassembler::_show_bytes = false; // set "true" to see what's in memory bit by bit 56 #endif 57 #if defined(SPARC) 58 bool AbstractDisassembler::_show_bytes = false; // set "true" to see what's in memory bit by bit 59 #endif 60 #if defined(X86) 61 bool AbstractDisassembler::_show_bytes = false; // set "true" to see what's in memory bit by bit 62 // might prove cumbersome because instr_len is hard to find on x86 63 #endif 64 65 // Return #bytes printed. Callers may use that for output alignment. 66 // Print instruction address, and offset from blob begin. 67 // Offset width (2, 4, 6, 8 bytes) is adapted to size of blob. 68 // Working assumption: we are at st->bol() upon entry. If not, it's the 69 // caller's responsibility to guarantee proper alignment. 70 int AbstractDisassembler::print_location(address here, address begin, address end, outputStream* st, bool align, bool print_header) { 71 const int pos_0 = st->position(); 72 73 if (show_pc() || show_offset()) { 74 st->print(" "); 75 } 76 77 if (show_pc()) { 78 if (print_header) { 79 st->print(" %*s", 18, "Address"); 80 } else { 81 st->print(" " PTR_FORMAT, p2i(here)); 82 } 83 } 84 85 if (show_offset()) { 86 #ifdef ASSERT 87 if ((uintptr_t)begin > (uintptr_t)here) st->print(">>begin(" PTR_FORMAT ") > here(" PTR_FORMAT ")<<", p2i(begin), p2i(here)); 88 if ((uintptr_t)end < (uintptr_t)here) st->print(">> end(" PTR_FORMAT ") < here(" PTR_FORMAT ")<<", p2i(end), p2i(here)); 89 assert((uintptr_t)begin <= (uintptr_t)end, "inverted address range"); 90 #endif 91 const int blob_len = end - begin; 92 const int offset = here - begin; 93 const int width = (blob_len < (1<< 8)) ? 2 : (blob_len < (1<<16)) ? 4 : (blob_len < (1<<24)) ? 6 : 8; 94 if (print_header) { 95 st->print(" %*s", width+5, "offset"); 96 } else { 97 st->print(" (+0x%*.*x)", width, width, offset); 98 } 99 } 100 101 if ((show_pc() || show_offset()) && !print_header) { 102 st->print(": "); 103 } 104 105 if (align) { 106 const uint tabspacing = 8; 107 const uint pos = st->position(); 108 const uint aligned_pos = ((pos+tabspacing-1)/tabspacing)*tabspacing /* - 1 */; 109 st->fill_to(aligned_pos); 110 } 111 112 return st->position() - pos_0; 113 } 114 115 116 // Return #bytes printed. Callers may use that for output alignment. 117 // Print instruction in hexadecimal representation, using 2-byte blocks. 118 // Used with real disassemblies. Not so useful with abstract disassemblies. 119 int AbstractDisassembler::print_instruction(address here, int len, int max_len, outputStream* st, bool align, bool print_header) { 120 if (show_bytes()) { 121 const int block_bytes = 2; 122 const int pos_0 = st->position(); 123 address pos = here; 124 125 //---< print instruction bytes in blocks >--- 126 // must print byte by byte: address might be unaligned. 127 for (; pos <= here + len - block_bytes; pos += block_bytes) { 128 for (address byte = pos; byte < pos + block_bytes; byte++) { 129 st->print("%2.2x", *byte); 130 } 131 st->print(" "); 132 } 133 134 //---< Print the remaining bytes of the instruction >--- 135 if ((len & (block_bytes - 1)) != 0) { 136 for (; pos < here + len; pos++) { 137 st->print("%2.2x", *pos); 138 } 139 } 140 141 //---< filler for shorter than max_len instructions >--- 142 for (int i = len+1; i < max_len; i++) { 143 st->print(" "); 144 } 145 146 st->print(" "); // separator space. 147 print_delimiter(st); 148 return st->position() - pos_0; 149 } 150 151 if (align) { 152 const uint tabspacing = 8; 153 const uint pos = st->position(); 154 const uint aligned_pos = ((pos+tabspacing-1)/tabspacing)*tabspacing /* - 1 */; 155 st->fill_to(aligned_pos); 156 } 157 158 return 0; 159 } 160 161 162 // Return #bytes printed. Callers may use that for output alignment. 163 // Print data (e.g. constant pool entries) in hex format. 164 // Depending on the alignment, short, int, and long entities are printed. 165 // If selected, data is formatted as int/long and float/double values in addition. 166 int AbstractDisassembler::print_hexdata(address here, int len, outputStream* st, bool print_header) { 167 const int tsize = 8; 168 const int pos_0 = st->position(); 169 int pos = pos_0; 170 int align = ((pos+tsize-1)/tsize)*tsize; 171 st->fill_to(align); 172 173 //---< printing hex data >--- 174 if (show_data_hex()) { 175 switch (len) { 176 case 1: if (print_header) { 177 st->print("hex1"); 178 } else { 179 st->print("0x%02x", *here); 180 } 181 st->fill_to(align += tsize); 182 case 2: if (print_header) { 183 st->print(" hex2"); 184 } else { 185 if (((uintptr_t)(here)&0x01) == 0) { 186 st->print("0x%04x", *((jushort*)here)); 187 } 188 } 189 st->fill_to(align += tsize); 190 case 4: if (print_header) { 191 st->print(" hex4"); 192 } else { 193 if (((uintptr_t)(here)&0x03) == 0) { 194 st->print("0x%08x", *((juint*)here)); 195 } 196 } 197 st->fill_to(align += 2*tsize); 198 case 8: if (print_header) { 199 st->print(" hex8"); 200 } else { 201 if (((uintptr_t)(here)&0x07) == 0) { 202 st->print(PTR64_FORMAT, *((uintptr_t*)here)); 203 } 204 } 205 st->fill_to(align += 3*tsize); 206 break; 207 default: ; 208 } 209 pos = st->position(); 210 align = ((pos+tsize-1)/tsize)*tsize; 211 st->fill_to(align); 212 } 213 214 //---< printing int/long data >--- 215 if (show_data_int()) { 216 switch (len) { 217 case 4: if (print_header) { 218 st->print(" int"); 219 } else { 220 if (((uintptr_t)(here)&0x03) == 0) { 221 st->print("%12.1d", *((jint*)here)); 222 } 223 } 224 st->fill_to(align += 2*tsize); 225 case 8: if (print_header) { 226 st->print(" long"); 227 } else { 228 if (((uintptr_t)(here)&0x07) == 0) { 229 st->print("%23.1ld", *((jlong*)here)); 230 } 231 } 232 st->fill_to(align += 3*tsize); 233 break; 234 default: ; 235 } 236 pos = st->position(); 237 align = ((pos+tsize-1)/tsize)*tsize; 238 st->fill_to(align); 239 } 240 241 //---< printing float/double data >--- 242 if (show_data_float()) { 243 switch (len) { 244 case 4: if (print_header) { 245 st->print(" float"); 246 } else { 247 if (((uintptr_t)(here)&0x03) == 0) { 248 st->print("%15.7e", (double)*((float*)here)); 249 } 250 } 251 st->fill_to(align += 2*tsize); 252 case 8: if (print_header) { 253 st->print(" double"); 254 } else { 255 if (((uintptr_t)(here)&0x07) == 0) { 256 st->print("%23.15e", *((double*)here)); 257 } 258 } 259 st->fill_to(align += 3*tsize); 260 break; 261 default: ; 262 } 263 } 264 265 return st->position() - pos_0; 266 } 267 268 269 // Return #bytes printed. Callers may use that for output alignment. 270 // Print an instruction delimiter. 271 int AbstractDisassembler::print_delimiter(outputStream* st) { 272 if (align_instr()) { st->print("| "); return 2; } 273 else return 0; 274 } 275 276 277 // Decodes the one instruction at address start in a platform-independent format. 278 // Returns the start of the next instruction (which is 'start' plus 'instruction_size_in_bytes'). 279 // The parameter max_instr_size_in_bytes is used for output alignment purposes only. 280 address AbstractDisassembler::decode_instruction_abstract(address start, 281 outputStream* st, 282 const int instruction_size_in_bytes, 283 const int max_instr_size_in_bytes) { 284 assert(instruction_size_in_bytes > 0, "no zero-size instructions!"); 285 assert(max_instr_size_in_bytes >= instruction_size_in_bytes, "inconsistent call parameters"); 286 287 //---< current instruction is at the start address >--- 288 unsigned char* current = (unsigned char*) start; 289 int filler_limit = align_instr() ? max_instr_size_in_bytes : ((instruction_size_in_bytes+abstract_instruction_bytes_per_block-1)/abstract_instruction_bytes_per_block) 290 *abstract_instruction_bytes_per_block; 291 292 //---< print the instruction's bytes >--- 293 for (int i = 1; i <= instruction_size_in_bytes; i++) { 294 st->print("%02x", *current); 295 ++current; 296 if (abstract_instruction_bytes_per_block <= max_instr_size_in_bytes) { 297 if (i%abstract_instruction_bytes_per_block == 0) st->print(" "); 298 } else { 299 if (i == instruction_size_in_bytes) st->print(" "); 300 } 301 } 302 303 //---< print some filler spaces to column-align instructions >--- 304 for (int i = instruction_size_in_bytes+1; i <= filler_limit; i++) { 305 st->print(" "); 306 if (abstract_instruction_bytes_per_block <= max_instr_size_in_bytes) { 307 if (i%abstract_instruction_bytes_per_block == 0) st->print(" "); 308 } else { 309 if (i == instruction_size_in_bytes) st->print(" "); 310 } 311 } 312 313 //---< the address of the next instruction >--- 314 return (address) current; 315 } 316 317 318 // Decodes all instructions in the given range [start..end) 319 // calling decode_instruction_abstract for each instruction. 320 // The format is platform dependent only to the extend that 321 // it respects the actual instruction length where possible. 322 // Does not print any markers or decorators. 323 void AbstractDisassembler::decode_range_abstract(address range_start, address range_end, 324 address start, address end, 325 outputStream* st, 326 const int max_instr_size_in_bytes) { 327 assert(st != NULL, "need an output stream (no default)!"); 328 int idx = 0; 329 address pos = range_start; 330 331 while ((pos != NULL) && (pos < range_end)) { 332 int instr_size_in_bytes = Assembler::instr_len(pos); 333 334 if (idx == 0) print_location(pos, start, end, st, false, false); 335 else print_delimiter(st); 336 337 //---< print the instruction's bytes >--- 338 // don't access storage beyond end of range 339 if (pos + instr_size_in_bytes <= range_end) { 340 pos = decode_instruction_abstract(pos, st, instr_size_in_bytes, max_instr_size_in_bytes); 341 } else { 342 // If the range to be decoded contains garbage at the end (e.g. 0xcc initializer bytes), 343 // instruction size calculation may run out of sync. Just terminate in that case. 344 pos = range_end; 345 } 346 347 idx += instr_size_in_bytes; 348 if (start_newline(idx)) { 349 st->cr(); 350 idx = 0; 351 } 352 } 353 } 354 355 356 // Decodes all instructions in the given range [start..end). 357 // The output is enclosed in [MachCode] and [/MachCode] tags for later recognition. 358 // The format is platform dependent only to the extend that 359 // it respects the actual instruction length where possible. 360 void AbstractDisassembler::decode_abstract(address start, address end, outputStream* ost, 361 const int max_instr_size_in_bytes) { 362 int idx = 0; 363 address pos = start; 364 365 outputStream* st = (ost == NULL) ? tty : ost; 366 367 //---< Open the output (Marker for post-mortem disassembler) >--- 368 st->bol(); 369 st->print_cr("[MachCode]"); 370 371 decode_range_abstract(start, end, start, end, st, max_instr_size_in_bytes); 372 373 //---< Close the output (Marker for post-mortem disassembler) >--- 374 st->bol(); 375 st->print_cr("[/MachCode]"); 376 }