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 #if defined(ZERO) 65 bool AbstractDisassembler::_show_bytes = false; // set "true" to see what's in memory bit by bit 66 #endif 67 68 // Return #bytes printed. Callers may use that for output alignment. 69 // Print instruction address, and offset from blob begin. 70 // Offset width (2, 4, 6, 8 bytes) is adapted to size of blob. 71 // Working assumption: we are at st->bol() upon entry. If not, it's the 72 // caller's responsibility to guarantee proper alignment. 73 int AbstractDisassembler::print_location(address here, address begin, address end, outputStream* st, bool align, bool print_header) { 74 const int pos_0 = st->position(); 75 76 if (show_pc() || show_offset()) { 77 st->print(" "); 78 } 79 80 if (show_pc()) { 81 if (print_header) { 82 st->print(" %*s", 18, "Address"); 83 } else { 84 st->print(" " PTR_FORMAT, p2i(here)); 85 } 86 } 87 88 if (show_offset()) { 89 #ifdef ASSERT 90 if ((uintptr_t)begin > (uintptr_t)here) st->print(">>begin(" PTR_FORMAT ") > here(" PTR_FORMAT ")<<", p2i(begin), p2i(here)); 91 if ((uintptr_t)end < (uintptr_t)here) st->print(">> end(" PTR_FORMAT ") < here(" PTR_FORMAT ")<<", p2i(end), p2i(here)); 92 assert((uintptr_t)begin <= (uintptr_t)end, "inverted address range"); 93 #endif 94 const int blob_len = end - begin; 95 const int offset = here - begin; 96 const int width = (blob_len < (1<< 8)) ? 2 : (blob_len < (1<<16)) ? 4 : (blob_len < (1<<24)) ? 6 : 8; 97 if (print_header) { 98 st->print(" %*s", width+5, "offset"); 99 } else { 100 st->print(" (+0x%*.*x)", width, width, offset); 101 } 102 } 103 104 if ((show_pc() || show_offset()) && !print_header) { 105 st->print(": "); 106 } 107 108 if (align) { 109 const uint tabspacing = 8; 110 const uint pos = st->position(); 111 const uint aligned_pos = ((pos+tabspacing-1)/tabspacing)*tabspacing /* - 1 */; 112 st->fill_to(aligned_pos); 113 } 114 115 return st->position() - pos_0; 116 } 117 118 119 // Return #bytes printed. Callers may use that for output alignment. 120 // Print instruction in hexadecimal representation, using 2-byte blocks. 121 // Used with real disassemblies. Not so useful with abstract disassemblies. 122 int AbstractDisassembler::print_instruction(address here, int len, int max_len, outputStream* st, bool align, bool print_header) { 123 if (show_bytes()) { 124 const int block_bytes = 2; 125 const int pos_0 = st->position(); 126 address pos = here; 127 128 //---< print instruction bytes in blocks >--- 129 // must print byte by byte: address might be unaligned. 130 for (; pos <= here + len - block_bytes; pos += block_bytes) { 131 for (address byte = pos; byte < pos + block_bytes; byte++) { 132 st->print("%2.2x", *byte); 133 } 134 st->print(" "); 135 } 136 137 //---< Print the remaining bytes of the instruction >--- 138 if ((len & (block_bytes - 1)) != 0) { 139 for (; pos < here + len; pos++) { 140 st->print("%2.2x", *pos); 141 } 142 } 143 144 //---< filler for shorter than max_len instructions >--- 145 for (int i = len+1; i < max_len; i++) { 146 st->print(" "); 147 } 148 149 st->print(" "); // separator space. 150 print_delimiter(st); 151 return st->position() - pos_0; 152 } 153 154 if (align) { 155 const uint tabspacing = 8; 156 const uint pos = st->position(); 157 const uint aligned_pos = ((pos+tabspacing-1)/tabspacing)*tabspacing /* - 1 */; 158 st->fill_to(aligned_pos); 159 } 160 161 return 0; 162 } 163 164 165 // Return #bytes printed. Callers may use that for output alignment. 166 // Print data (e.g. constant pool entries) in hex format. 167 // Depending on the alignment, short, int, and long entities are printed. 168 // If selected, data is formatted as int/long and float/double values in addition. 169 int AbstractDisassembler::print_hexdata(address here, int len, outputStream* st, bool print_header) { 170 const int tsize = 8; 171 const int pos_0 = st->position(); 172 int pos = pos_0; 173 int align = ((pos+tsize-1)/tsize)*tsize; 174 st->fill_to(align); 175 176 //---< printing hex data >--- 177 if (show_data_hex()) { 178 switch (len) { 179 case 1: if (print_header) { 180 st->print("hex1"); 181 } else { 182 st->print("0x%02x", *here); 183 } 184 st->fill_to(align += tsize); 185 case 2: if (print_header) { 186 st->print(" hex2"); 187 } else { 188 if (((uintptr_t)(here)&0x01) == 0) { 189 st->print("0x%04x", *((jushort*)here)); 190 } 191 } 192 st->fill_to(align += tsize); 193 case 4: if (print_header) { 194 st->print(" hex4"); 195 } else { 196 if (((uintptr_t)(here)&0x03) == 0) { 197 st->print("0x%08x", *((juint*)here)); 198 } 199 } 200 st->fill_to(align += 2*tsize); 201 case 8: if (print_header) { 202 st->print(" hex8"); 203 } else { 204 if (((uintptr_t)(here)&0x07) == 0) { 205 st->print(PTR_FORMAT, *((uintptr_t*)here)); 206 } 207 } 208 st->fill_to(align += 3*tsize); 209 break; 210 default: ; 211 } 212 pos = st->position(); 213 align = ((pos+tsize-1)/tsize)*tsize; 214 st->fill_to(align); 215 } 216 217 //---< printing int/long data >--- 218 if (show_data_int()) { 219 switch (len) { 220 case 4: if (print_header) { 221 st->print(" int"); 222 } else { 223 if (((uintptr_t)(here)&0x03) == 0) { 224 st->print("%12.1d", *((jint*)here)); 225 } 226 } 227 st->fill_to(align += 2*tsize); 228 case 8: if (print_header) { 229 st->print(" long"); 230 } else { 231 if (((uintptr_t)(here)&0x07) == 0) { 232 st->print("%23.1ld", *((jlong*)here)); 233 } 234 } 235 st->fill_to(align += 3*tsize); 236 break; 237 default: ; 238 } 239 pos = st->position(); 240 align = ((pos+tsize-1)/tsize)*tsize; 241 st->fill_to(align); 242 } 243 244 //---< printing float/double data >--- 245 if (show_data_float()) { 246 switch (len) { 247 case 4: if (print_header) { 248 st->print(" float"); 249 } else { 250 if (((uintptr_t)(here)&0x03) == 0) { 251 st->print("%15.7e", (double)*((float*)here)); 252 } 253 } 254 st->fill_to(align += 2*tsize); 255 case 8: if (print_header) { 256 st->print(" double"); 257 } else { 258 if (((uintptr_t)(here)&0x07) == 0) { 259 st->print("%23.15e", *((double*)here)); 260 } 261 } 262 st->fill_to(align += 3*tsize); 263 break; 264 default: ; 265 } 266 } 267 268 return st->position() - pos_0; 269 } 270 271 272 // Return #bytes printed. Callers may use that for output alignment. 273 // Print an instruction delimiter. 274 int AbstractDisassembler::print_delimiter(outputStream* st) { 275 if (align_instr()) { st->print("| "); return 2; } 276 else return 0; 277 } 278 279 280 // Decodes the one instruction at address start in a platform-independent format. 281 // Returns the start of the next instruction (which is 'start' plus 'instruction_size_in_bytes'). 282 // The parameter max_instr_size_in_bytes is used for output alignment purposes only. 283 address AbstractDisassembler::decode_instruction_abstract(address start, 284 outputStream* st, 285 const int instruction_size_in_bytes, 286 const int max_instr_size_in_bytes) { 287 assert(instruction_size_in_bytes > 0, "no zero-size instructions!"); 288 assert(max_instr_size_in_bytes >= instruction_size_in_bytes, "inconsistent call parameters"); 289 290 //---< current instruction is at the start address >--- 291 unsigned char* current = (unsigned char*) start; 292 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) 293 *abstract_instruction_bytes_per_block; 294 295 //---< print the instruction's bytes >--- 296 for (int i = 1; i <= instruction_size_in_bytes; i++) { 297 st->print("%02x", *current); 298 ++current; 299 if (abstract_instruction_bytes_per_block <= max_instr_size_in_bytes) { 300 if (i%abstract_instruction_bytes_per_block == 0) st->print(" "); 301 } else { 302 if (i == instruction_size_in_bytes) st->print(" "); 303 } 304 } 305 306 //---< print some filler spaces to column-align instructions >--- 307 for (int i = instruction_size_in_bytes+1; i <= filler_limit; i++) { 308 st->print(" "); 309 if (abstract_instruction_bytes_per_block <= max_instr_size_in_bytes) { 310 if (i%abstract_instruction_bytes_per_block == 0) st->print(" "); 311 } else { 312 if (i == instruction_size_in_bytes) st->print(" "); 313 } 314 } 315 316 //---< the address of the next instruction >--- 317 return (address) current; 318 } 319 320 321 // Decodes all instructions in the given range [start..end) 322 // calling decode_instruction_abstract for each instruction. 323 // The format is platform dependent only to the extend that 324 // it respects the actual instruction length where possible. 325 // Does not print any markers or decorators. 326 void AbstractDisassembler::decode_range_abstract(address range_start, address range_end, 327 address start, address end, 328 outputStream* st, 329 const int max_instr_size_in_bytes) { 330 assert(st != NULL, "need an output stream (no default)!"); 331 int idx = 0; 332 address pos = range_start; 333 334 while ((pos != NULL) && (pos < range_end)) { 335 int instr_size_in_bytes = Assembler::instr_len(pos); 336 337 if (idx == 0) print_location(pos, start, end, st, false, false); 338 else print_delimiter(st); 339 340 //---< print the instruction's bytes >--- 341 // don't access storage beyond end of range 342 if (pos + instr_size_in_bytes <= range_end) { 343 pos = decode_instruction_abstract(pos, st, instr_size_in_bytes, max_instr_size_in_bytes); 344 } else { 345 // If the range to be decoded contains garbage at the end (e.g. 0xcc initializer bytes), 346 // instruction size calculation may run out of sync. Just terminate in that case. 347 pos = range_end; 348 } 349 350 idx += instr_size_in_bytes; 351 if (start_newline(idx)) { 352 st->cr(); 353 idx = 0; 354 } 355 } 356 } 357 358 359 // Decodes all instructions in the given range [start..end). 360 // The output is enclosed in [MachCode] and [/MachCode] tags for later recognition. 361 // The format is platform dependent only to the extend that 362 // it respects the actual instruction length where possible. 363 void AbstractDisassembler::decode_abstract(address start, address end, outputStream* ost, 364 const int max_instr_size_in_bytes) { 365 int idx = 0; 366 address pos = start; 367 368 outputStream* st = (ost == NULL) ? tty : ost; 369 370 //---< Open the output (Marker for post-mortem disassembler) >--- 371 st->bol(); 372 st->print_cr("[MachCode]"); 373 374 decode_range_abstract(start, end, start, end, st, max_instr_size_in_bytes); 375 376 //---< Close the output (Marker for post-mortem disassembler) >--- 377 st->bol(); 378 st->print_cr("[/MachCode]"); 379 }