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("0x%016lx", *((julong*)here));
 203                   st->print("0x%016lx", *((uintptr_t*)here));
 204 //                  st->print("0x%08x%08x", *((juint*)here), *((juint*)(here+4)));
 205                 }
 206               }
 207               st->fill_to(align += 3*tsize);
 208               break;
 209       default: ;
 210     }
 211     pos   = st->position();
 212     align = ((pos+tsize-1)/tsize)*tsize;
 213     st->fill_to(align);
 214   }
 215 
 216   //---<  printing int/long data  >---
 217   if (show_data_int()) {
 218     switch (len) {
 219       case 4: if (print_header) {
 220                 st->print("         int");
 221               } else {
 222                 if (((uintptr_t)(here)&0x03) == 0) {
 223                   st->print("%12.1d",  *((jint*)here));
 224                 }
 225               }
 226               st->fill_to(align += 2*tsize);
 227       case 8: if (print_header) {
 228                 st->print("                   long");
 229               } else {
 230                 if (((uintptr_t)(here)&0x07) == 0) {
 231                   st->print("%23.1ld", *((jlong*)here));
 232                 }
 233               }
 234               st->fill_to(align += 3*tsize);
 235               break;
 236       default: ;
 237     }
 238     pos   = st->position();
 239     align = ((pos+tsize-1)/tsize)*tsize;
 240     st->fill_to(align);
 241   }
 242 
 243   //---<  printing float/double data  >---
 244   if (show_data_float()) {
 245     switch (len) {
 246       case 4: if (print_header) {
 247                 st->print("          float");
 248               } else {
 249                 if (((uintptr_t)(here)&0x03) == 0) {
 250                   st->print("%15.7e",  (double)*((float*)here));
 251                 }
 252               }
 253               st->fill_to(align += 2*tsize);
 254       case 8: if (print_header) {
 255                 st->print("                 double");
 256               } else {
 257                 if (((uintptr_t)(here)&0x07) == 0) {
 258                   st->print("%23.15e",         *((double*)here));
 259                 }
 260               }
 261               st->fill_to(align += 3*tsize);
 262               break;
 263       default: ;
 264     }
 265   }
 266 
 267   return st->position() - pos_0;
 268 }
 269 
 270 
 271 // Return #bytes printed. Callers may use that for output alignment.
 272 // Print an instruction delimiter.
 273 int AbstractDisassembler::print_delimiter(outputStream* st) {
 274   if (align_instr()) { st->print("| "); return 2; }
 275   else               return 0;
 276 }
 277 
 278 
 279 // Decodes the one instruction at address start in a platform-independent format.
 280 // Returns the start of the next instruction (which is 'start' plus 'instruction_size_in_bytes').
 281 // The parameter max_instr_size_in_bytes is used for output alignment purposes only.
 282 address AbstractDisassembler::decode_instruction_abstract(address start,
 283                                                           outputStream* st,
 284                                                           const int instruction_size_in_bytes,
 285                                                           const int max_instr_size_in_bytes) {
 286   assert(instruction_size_in_bytes > 0, "no zero-size instructions!");
 287   assert(max_instr_size_in_bytes >= instruction_size_in_bytes, "inconsistent call parameters");
 288 
 289   //---<  current instruction is at the start address  >---
 290   unsigned char* current = (unsigned char*) start;
 291   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)
 292                                                                           *abstract_instruction_bytes_per_block;
 293 
 294   //---<  print the instruction's bytes  >---
 295   for (int i = 1; i <= instruction_size_in_bytes; i++) {
 296     st->print("%02x", *current);
 297     ++current;
 298     if (abstract_instruction_bytes_per_block <= max_instr_size_in_bytes) {
 299       if (i%abstract_instruction_bytes_per_block == 0) st->print(" ");
 300     } else {
 301       if (i == instruction_size_in_bytes) st->print(" ");
 302     }
 303   }
 304 
 305   //---<  print some filler spaces to column-align instructions  >---
 306   for (int i = instruction_size_in_bytes+1; i <= filler_limit; i++) {
 307     st->print("  ");
 308     if (abstract_instruction_bytes_per_block <= max_instr_size_in_bytes) {
 309       if (i%abstract_instruction_bytes_per_block == 0) st->print(" ");
 310     } else {
 311       if (i == instruction_size_in_bytes) st->print(" ");
 312     }
 313   }
 314 
 315   //---<  the address of the next instruction  >---
 316   return (address) current;
 317 }
 318 
 319 
 320 // Decodes all instructions in the given range [start..end)
 321 // calling decode_instruction_abstract for each instruction.
 322 // The format is platform dependent only to the extend that
 323 // it respects the actual instruction length where possible.
 324 // Does not print any markers or decorators.
 325 void AbstractDisassembler::decode_range_abstract(address range_start, address range_end,
 326                                                  address start, address end,
 327                                                  outputStream* st,
 328                                                  const int max_instr_size_in_bytes) {
 329   assert(st != NULL, "need an output stream (no default)!");
 330   int     idx = 0;
 331   address pos = range_start;
 332 
 333   while ((pos != NULL) && (pos < range_end)) {
 334     int instr_size_in_bytes = Assembler::instr_len(pos);
 335 
 336     if (idx == 0) print_location(pos, start, end, st, false, false);
 337     else          print_delimiter(st);
 338 
 339     //---<  print the instruction's bytes  >---
 340     // don't access storage beyond end of range
 341     if (pos + instr_size_in_bytes <= range_end) {
 342       pos = decode_instruction_abstract(pos, st, instr_size_in_bytes, max_instr_size_in_bytes);
 343     } else {
 344       // If the range to be decoded contains garbage at the end (e.g. 0xcc initializer bytes),
 345       // instruction size calculation may run out of sync. Just terminate in that case.
 346       pos = range_end;
 347     }
 348 
 349     idx += instr_size_in_bytes;
 350     if (start_newline(idx)) {
 351       st->cr();
 352       idx = 0;
 353     }
 354   }
 355 }
 356 
 357 
 358 // Decodes all instructions in the given range [start..end).
 359 // The output is enclosed in [MachCode] and [/MachCode] tags for later recognition.
 360 // The format is platform dependent only to the extend that
 361 // it respects the actual instruction length where possible.
 362 void AbstractDisassembler::decode_abstract(address start, address end, outputStream* ost,
 363                                            const int max_instr_size_in_bytes) {
 364   int     idx = 0;
 365   address pos = start;
 366 
 367   outputStream* st = (ost == NULL) ? tty : ost;
 368 
 369   //---<  Open the output (Marker for post-mortem disassembler)  >---
 370   st->bol();
 371   st->print_cr("[MachCode]");
 372 
 373   decode_range_abstract(start, end, start, end, st, max_instr_size_in_bytes);
 374 
 375   //---<  Close the output (Marker for post-mortem disassembler)  >---
 376   st->bol();
 377   st->print_cr("[/MachCode]");
 378 }