1 /*
  2  * Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #include "precompiled.hpp"
 26 #include "asm/assembler.inline.hpp"
 27 #include "asm/macroAssembler.hpp"
 28 #include "ci/ciUtilities.hpp"
 29 #include "classfile/javaClasses.hpp"
 30 #include "code/codeCache.hpp"
 31 #include "compiler/disassembler.hpp"
 32 #include "gc/shared/cardTable.hpp"
 33 #include "gc/shared/cardTableBarrierSet.hpp"
 34 #include "gc/shared/collectedHeap.hpp"
 35 #include "logging/log.hpp"
 36 #include "memory/resourceArea.hpp"
 37 #include "memory/universe.hpp"
 38 #include "oops/oop.inline.hpp"
 39 #include "runtime/handles.inline.hpp"
 40 #include "runtime/java.hpp"
 41 #include "runtime/os.hpp"
 42 #include "runtime/stubCodeGenerator.hpp"
 43 #include "runtime/stubRoutines.hpp"
 44 #include "utilities/resourceHash.hpp"
 45 
 46 void*       Disassembler::_library               = nullptr;
 47 bool        Disassembler::_tried_to_load_library = false;
 48 bool        Disassembler::_library_usable        = false;
 49 
 50 // This routine is in the shared library:
 51 Disassembler::decode_func_virtual Disassembler::_decode_instructions_virtual = nullptr;
 52 
 53 static const char hsdis_library_name[] = "hsdis-" HOTSPOT_LIB_ARCH;
 54 static const char decode_instructions_virtual_name[] = "decode_instructions_virtual";
 55 #define COMMENT_COLUMN  52 LP64_ONLY(+8) /*could be an option*/
 56 #define BYTES_COMMENT   ";..."  /* funky byte display comment */
 57 
 58 class decode_env {
 59  private:
 60   outputStream* _output;      // where the disassembly is directed to
 61   CodeBlob*     _codeBlob;    // != nullptr only when decoding a CodeBlob
 62   nmethod*      _nm;          // != nullptr only when decoding a nmethod
 63 
 64   address       _start;       // != nullptr when decoding a range of unknown type
 65   address       _end;         // != nullptr when decoding a range of unknown type
 66 
 67   char          _option_buf[512];
 68   char          _print_raw;
 69   address       _cur_insn;        // address of instruction currently being decoded
 70   int           _bytes_per_line;  // arch-specific formatting option
 71   int           _pre_decode_alignment;
 72   int           _post_decode_alignment;
 73   bool          _print_file_name;
 74   bool          _print_help;
 75   bool          _helpPrinted;
 76   static bool   _optionsParsed;
 77 #ifndef PRODUCT
 78   const AsmRemarks* _remarks; // Used with start/end range to provide code remarks.
 79   ptrdiff_t         _disp;    // Adjustment to offset -> remark mapping.
 80 #endif
 81 
 82   enum {
 83     tabspacing = 8
 84   };
 85 
 86   // Check if the event matches the expected tag
 87   // The tag must be a substring of the event, and
 88   // the tag must be a token in the event, i.e. separated by delimiters
 89   static bool match(const char* event, const char* tag) {
 90     size_t eventlen = strlen(event);
 91     size_t taglen   = strlen(tag);
 92     if (eventlen < taglen)  // size mismatch
 93       return false;
 94     if (strncmp(event, tag, taglen) != 0)  // string mismatch
 95       return false;
 96     char delim = event[taglen];
 97     return delim == '\0' || delim == ' ' || delim == '/' || delim == '=';
 98   }
 99 
100   // Merge new option string with previously recorded options
101   void collect_options(const char* p) {
102     if (p == nullptr || p[0] == '\0')  return;
103     size_t opt_so_far = strlen(_option_buf);
104     if (opt_so_far + 1 + strlen(p) + 1 > sizeof(_option_buf))  return;
105     char* fillp = &_option_buf[opt_so_far];
106     if (opt_so_far > 0) *fillp++ = ',';
107     strcat(fillp, p);
108     // replace white space by commas:
109     char* q = fillp;
110     while ((q = strpbrk(q, " \t\n")) != nullptr)
111       *q++ = ',';
112   }
113 
114   void process_options(outputStream* ost);
115 
116   void print_insn_labels();
117   void print_insn_prefix();
118   void print_address(address value);
119 
120   // Properly initializes _start/_end. Overwritten too often if
121   // printing of instructions is called for each instruction.
122   void set_start(address s)   { _start = s; }
123   void set_end  (address e)   { _end = e; }
124   void set_nm   (nmethod* nm) { _nm = nm; }
125   void set_output(outputStream* st) { _output = st; }
126 
127 #if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY)
128   // The disassembler library (sometimes) uses tabs to nicely align the instruction operands.
129   // Depending on the mnemonic length and the column position where the
130   // mnemonic is printed, alignment may turn out to be not so nice.
131   // To improve, we assume 8-character tab spacing and left-align the mnemonic on a tab position.
132   // Instruction comments are aligned 4 tab positions to the right of the mnemonic.
133   void calculate_alignment() {
134     _pre_decode_alignment  = ((output()->position()+tabspacing-1)/tabspacing)*tabspacing;
135     _post_decode_alignment = _pre_decode_alignment + 4*tabspacing;
136   }
137 
138   void start_insn(address pc) {
139     _cur_insn = pc;
140     output()->bol();
141     print_insn_labels();
142     print_insn_prefix();
143   }
144 
145   void end_insn(address pc) {
146     address pc0 = cur_insn();
147     outputStream* st = output();
148 
149     if (AbstractDisassembler::show_comment()) {
150       if ((_nm != nullptr) && _nm->has_code_comment(pc0, pc)) {
151         _nm->print_code_comment_on
152                (st,
153                 _post_decode_alignment ? _post_decode_alignment : COMMENT_COLUMN,
154                 pc0, pc);
155         // this calls reloc_string_for which calls oop::print_value_on
156       }
157       print_hook_comments(pc0, _nm != nullptr);
158     }
159     Disassembler::annotate(pc0, output());
160     // follow each complete insn by a nice newline
161     st->bol();
162   }
163 #endif
164 
165   struct SourceFileInfo {
166     struct Link : public CHeapObj<mtCode> {
167       const char* file;
168       int line;
169       Link* next;
170       Link(const char* f, int l) : file(f), line(l), next(nullptr) {}
171     };
172     Link *head, *tail;
173 
174     void append(const char* file, int line) {
175       if (tail != nullptr && tail->file == file && tail->line == line) {
176         // Don't print duplicated lines at the same address. This could happen with C
177         // macros that end up having multiple "__" tokens on the same __LINE__.
178         return;
179       }
180       Link *link = new Link(file, line);
181       if (head == nullptr) {
182         head = tail = link;
183       } else {
184         tail->next = link;
185         tail = link;
186       }
187     }
188     SourceFileInfo(const char* file, int line) : head(nullptr), tail(nullptr) {
189       append(file, line);
190     }
191   };
192 
193   typedef ResourceHashtable<
194       address, SourceFileInfo,
195       15889,      // prime number
196       AnyObj::C_HEAP> SourceFileInfoTable;
197 
198   static SourceFileInfoTable* _src_table;
199   static const char* _cached_src;
200   static GrowableArray<const char*>* _cached_src_lines;
201 
202   static SourceFileInfoTable& src_table() {
203     if (_src_table == nullptr) {
204       _src_table = new (mtCode)SourceFileInfoTable();
205     }
206     return *_src_table;
207   }
208 
209  public:
210   decode_env(CodeBlob*   code, outputStream* output);
211   decode_env(nmethod*    code, outputStream* output);
212   // Constructor for a 'decode_env' to decode an arbitrary
213   // piece of memory, hopefully containing code.
214   decode_env(address start, address end, outputStream* output
215              NOT_PRODUCT(COMMA const AsmRemarks* remarks = nullptr COMMA ptrdiff_t disp = 0));
216 
217   // Add 'original_start' argument which is the original address
218   // the instructions were located at (if this is not equal to 'start').
219   address decode_instructions(address start, address end, address original_start = nullptr);
220 
221   address handle_event(const char* event, address arg);
222 
223   outputStream* output()   { return _output; }
224   address       cur_insn() { return _cur_insn; }
225   const char*   options()  { return _option_buf; }
226   static void   hook(const char* file, int line, address pc);
227   void print_hook_comments(address pc, bool newline);
228 };
229 
230 bool decode_env::_optionsParsed = false;
231 
232 decode_env::SourceFileInfoTable* decode_env::_src_table = nullptr;
233 const char* decode_env::_cached_src = nullptr;
234 GrowableArray<const char*>* decode_env::_cached_src_lines = nullptr;
235 
236 void decode_env::hook(const char* file, int line, address pc) {
237   // For simplication, we never free from this table. It's really not
238   // necessary as we add to the table only when PrintInterpreter is true,
239   // which means we are debugging the VM and a little bit of extra
240   // memory usage doesn't matter.
241   SourceFileInfo* found = src_table().get(pc);
242   if (found != nullptr) {
243     found->append(file, line);
244   } else {
245     SourceFileInfo sfi(file, line);
246     src_table().put(pc, sfi); // sfi is copied by value
247   }
248 }
249 
250 void decode_env::print_hook_comments(address pc, bool newline) {
251   SourceFileInfo* found = src_table().get(pc);
252   outputStream* st = output();
253   if (found != nullptr) {
254     for (SourceFileInfo::Link *link = found->head; link; link = link->next) {
255       const char* file = link->file;
256       int line = link->line;
257       if (_cached_src == nullptr || strcmp(_cached_src, file) != 0) {
258         FILE* fp;
259 
260         // _cached_src_lines is a single cache of the lines of a source file, and we refill this cache
261         // every time we need to print a line from a different source file. It's not the fastest,
262         // but seems bearable.
263         if (_cached_src_lines != nullptr) {
264           for (int i=0; i<_cached_src_lines->length(); i++) {
265             os::free((void*)_cached_src_lines->at(i));
266           }
267           _cached_src_lines->clear();
268         } else {
269           _cached_src_lines = new (mtCode) GrowableArray<const char*>(0, mtCode);
270         }
271 
272         if ((fp = os::fopen(file, "r")) == nullptr) {
273           _cached_src = nullptr;
274           return;
275         }
276         _cached_src = file;
277 
278         char line[500]; // don't write lines that are too long in your source files!
279         while (fgets(line, sizeof(line), fp) != nullptr) {
280           size_t len = strlen(line);
281           if (len > 0 && line[len-1] == '\n') {
282             line[len-1] = '\0';
283           }
284           _cached_src_lines->append(os::strdup(line));
285         }
286         fclose(fp);
287         _print_file_name = true;
288       }
289 
290       if (_print_file_name) {
291         // We print the file name whenever we switch to a new file, or when
292         // Disassembler::decode is called to disassemble a new block of code.
293         _print_file_name = false;
294         if (newline) {
295           st->cr();
296         }
297         st->move_to(COMMENT_COLUMN);
298         st->print(";;@FILE: %s", file);
299         newline = true;
300       }
301 
302       int index = line - 1; // 1-based line number -> 0-based index.
303       if (index >= _cached_src_lines->length()) {
304         // This could happen if source file is mismatched.
305       } else {
306         const char* source_line = _cached_src_lines->at(index);
307         if (newline) {
308           st->cr();
309         }
310         st->move_to(COMMENT_COLUMN);
311         st->print(";;%5d: %s", line, source_line);
312         newline = true;
313       }
314     }
315   }
316 }
317 
318 decode_env::decode_env(CodeBlob* code, outputStream* output) :
319   _output(output ? output : tty),
320   _codeBlob(code),
321   _nm(_codeBlob != nullptr && _codeBlob->is_nmethod() ? (nmethod*) code : nullptr),
322   _start(nullptr),
323   _end(nullptr),
324   _option_buf(),
325   _print_raw(0),
326   _cur_insn(nullptr),
327   _bytes_per_line(0),
328   _pre_decode_alignment(0),
329   _post_decode_alignment(0),
330   _print_file_name(false),
331   _print_help(false),
332   _helpPrinted(false)
333   NOT_PRODUCT(COMMA _remarks(nullptr))
334   NOT_PRODUCT(COMMA _disp(0))
335 {
336   memset(_option_buf, 0, sizeof(_option_buf));
337   process_options(_output);
338 }
339 
340 decode_env::decode_env(nmethod* code, outputStream* output) :
341   _output(output ? output : tty),
342   _codeBlob(nullptr),
343   _nm(code),
344   _start(_nm->code_begin()),
345   _end(_nm->code_end()),
346   _option_buf(),
347   _print_raw(0),
348   _cur_insn(nullptr),
349   _bytes_per_line(0),
350   _pre_decode_alignment(0),
351   _post_decode_alignment(0),
352   _print_file_name(false),
353   _print_help(false),
354   _helpPrinted(false)
355   NOT_PRODUCT(COMMA _remarks(nullptr))
356   NOT_PRODUCT(COMMA _disp(0))
357 {
358   memset(_option_buf, 0, sizeof(_option_buf));
359   process_options(_output);
360 }
361 
362 // Constructor for a 'decode_env' to decode a memory range [start, end)
363 // of unknown origin, assuming it contains code.
364 decode_env::decode_env(address start, address end, outputStream* output
365                        NOT_PRODUCT(COMMA const AsmRemarks* remarks COMMA ptrdiff_t disp)) :
366   _output(output ? output : tty),
367   _codeBlob(nullptr),
368   _nm(nullptr),
369   _start(start),
370   _end(end),
371   _option_buf(),
372   _print_raw(0),
373   _cur_insn(nullptr),
374   _bytes_per_line(0),
375   _pre_decode_alignment(0),
376   _post_decode_alignment(0),
377   _print_file_name(false),
378   _print_help(false),
379   _helpPrinted(false)
380   NOT_PRODUCT(COMMA _remarks(remarks))
381   NOT_PRODUCT(COMMA _disp(disp))
382 {
383   assert(start < end, "Range must have a positive size, [" PTR_FORMAT ".." PTR_FORMAT ").", p2i(start), p2i(end));
384   memset(_option_buf, 0, sizeof(_option_buf));
385   process_options(_output);
386 }
387 
388 void decode_env::process_options(outputStream* ost) {
389   // by default, output pc but not bytes:
390   _print_help      = false;
391   _bytes_per_line  = Disassembler::pd_instruction_alignment();
392   _print_file_name = true;
393 
394   // parse the global option string
395   // We need to fill the options buffer for each newly created
396   // decode_env instance. The hsdis_* library looks for options
397   // in that buffer.
398   collect_options(Disassembler::pd_cpu_opts());
399   collect_options(PrintAssemblyOptions);
400 
401   if (strstr(options(), "print-raw")) {
402     _print_raw = (strstr(options(), "xml") ? 2 : 1);
403   }
404 
405   if (_optionsParsed) return;  // parse only once
406 
407   if (strstr(options(), "help")) {
408     _print_help = true;
409   }
410   if (strstr(options(), "align-instr")) {
411     AbstractDisassembler::toggle_align_instr();
412   }
413   if (strstr(options(), "show-pc")) {
414     AbstractDisassembler::toggle_show_pc();
415   }
416   if (strstr(options(), "show-offset")) {
417     AbstractDisassembler::toggle_show_offset();
418   }
419   if (strstr(options(), "show-bytes")) {
420     AbstractDisassembler::toggle_show_bytes();
421   }
422   if (strstr(options(), "show-data-hex")) {
423     AbstractDisassembler::toggle_show_data_hex();
424   }
425   if (strstr(options(), "show-data-int")) {
426     AbstractDisassembler::toggle_show_data_int();
427   }
428   if (strstr(options(), "show-data-float")) {
429     AbstractDisassembler::toggle_show_data_float();
430   }
431   if (strstr(options(), "show-structs")) {
432     AbstractDisassembler::toggle_show_structs();
433   }
434   if (strstr(options(), "show-comment")) {
435     AbstractDisassembler::toggle_show_comment();
436   }
437   if (strstr(options(), "show-block-comment")) {
438     AbstractDisassembler::toggle_show_block_comment();
439   }
440   _optionsParsed = true;
441 
442   if (_print_help && ! _helpPrinted) {
443     _helpPrinted = true;
444     ost->print_cr("PrintAssemblyOptions help:");
445     ost->print_cr("  print-raw       test plugin by requesting raw output");
446     ost->print_cr("  print-raw-xml   test plugin by requesting raw xml");
447     ost->cr();
448     ost->print_cr("  show-pc            toggle printing current pc,        currently %s", AbstractDisassembler::show_pc()            ? "ON" : "OFF");
449     ost->print_cr("  show-offset        toggle printing current offset,    currently %s", AbstractDisassembler::show_offset()        ? "ON" : "OFF");
450     ost->print_cr("  show-bytes         toggle printing instruction bytes, currently %s", AbstractDisassembler::show_bytes()         ? "ON" : "OFF");
451     ost->print_cr("  show-data-hex      toggle formatting data as hex,     currently %s", AbstractDisassembler::show_data_hex()      ? "ON" : "OFF");
452     ost->print_cr("  show-data-int      toggle formatting data as int,     currently %s", AbstractDisassembler::show_data_int()      ? "ON" : "OFF");
453     ost->print_cr("  show-data-float    toggle formatting data as float,   currently %s", AbstractDisassembler::show_data_float()    ? "ON" : "OFF");
454     ost->print_cr("  show-structs       toggle compiler data structures,   currently %s", AbstractDisassembler::show_structs()       ? "ON" : "OFF");
455     ost->print_cr("  show-comment       toggle instruction comments,       currently %s", AbstractDisassembler::show_comment()       ? "ON" : "OFF");
456     ost->print_cr("  show-block-comment toggle block comments,             currently %s", AbstractDisassembler::show_block_comment() ? "ON" : "OFF");
457     ost->print_cr("  align-instr        toggle instruction alignment,      currently %s", AbstractDisassembler::align_instr()        ? "ON" : "OFF");
458     ost->print_cr("combined options: %s", options());
459   }
460 }
461 
462 // Disassembly Event Handler.
463 // This method receives events from the disassembler library hsdis
464 // via event_to_env for each decoding step (installed by
465 // Disassembler::decode_instructions(), replacing the default
466 // callback method). This enables dumping additional info
467 // and custom line formatting.
468 // In a future extension, calling a custom decode method will be
469 // supported. We can use such a method to decode instructions the
470 // binutils decoder does not handle to our liking (suboptimal
471 // formatting, incomplete information, ...).
472 // Returns:
473 // - nullptr for all standard invocations. The function result is not
474 //        examined (as of now, 20190409) by the hsdis decoder loop.
475 // - next for 'insn0' invocations.
476 //        next == arg: the custom decoder didn't do anything.
477 //        next >  arg: the custom decoder did decode the instruction.
478 //                     next points to the next undecoded instruction
479 //                     (continuation point for decoder loop).
480 //
481 // "Normal" sequence of events:
482 //  insns   - start of instruction stream decoding
483 //  mach    - display architecture
484 //  format  - display bytes-per-line
485 //  for each instruction:
486 //    insn    - start of instruction decoding
487 //    insn0   - custom decoder invocation (if any)
488 //    addr    - print address value
489 //    /insn   - end of instruction decoding
490 //  /insns  - premature end of instruction stream due to no progress
491 //
492 address decode_env::handle_event(const char* event, address arg) {
493 
494 #if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY)
495 
496   //---<  Event: end decoding loop (error, no progress)  >---
497   if (decode_env::match(event, "/insns")) {
498     // Nothing to be done here.
499     return nullptr;
500   }
501 
502   //---<  Event: start decoding loop  >---
503   if (decode_env::match(event, "insns")) {
504     // Nothing to be done here.
505     return nullptr;
506   }
507 
508   //---<  Event: finish decoding an instruction  >---
509   if (decode_env::match(event, "/insn")) {
510     output()->fill_to(_post_decode_alignment);
511     end_insn(arg);
512     return nullptr;
513   }
514 
515   //---<  Event: start decoding an instruction  >---
516   if (decode_env::match(event, "insn")) {
517     start_insn(arg);
518   } else if (match(event, "/insn")) {
519     end_insn(arg);
520   } else if (match(event, "addr")) {
521     if (arg != nullptr) {
522       print_address(arg);
523       return arg;
524     }
525     calculate_alignment();
526     output()->fill_to(_pre_decode_alignment);
527     return nullptr;
528   }
529 
530   //---<  Event: call custom decoder (platform specific)  >---
531   if (decode_env::match(event, "insn0")) {
532     return Disassembler::decode_instruction0(arg, output(), arg);
533   }
534 
535   //---<  Event: Print address  >---
536   if (decode_env::match(event, "addr")) {
537     print_address(arg);
538     return arg;
539   }
540 
541   //---<  Event: mach (inform about machine architecture)  >---
542   // This event is problematic because it messes up the output.
543   // The event is fired after the instruction address has already
544   // been printed. The decoded instruction (event "insn") is
545   // printed afterwards. That doesn't look nice.
546   if (decode_env::match(event, "mach")) {
547     guarantee(arg != nullptr, "event_to_env - arg must not be nullptr for event 'mach'");
548     static char buffer[64] = { 0, };
549     // Output suppressed because it messes up disassembly.
550     // Only print this when the mach changes.
551     if (false && (strcmp(buffer, (const char*)arg) != 0 ||
552                   strlen((const char*)arg) > sizeof(buffer) - 1)) {
553       // Only print this when the mach changes
554       strncpy(buffer, (const char*)arg, sizeof(buffer) - 1);
555       buffer[sizeof(buffer) - 1] = '\0';
556       output()->print_cr("[Disassembling for mach='%s']", (const char*)arg);
557     }
558     return nullptr;
559   }
560 
561   //---<  Event: format bytes-per-line  >---
562   if (decode_env::match(event, "format bytes-per-line")) {
563     _bytes_per_line = (int) (intptr_t) arg;
564     return nullptr;
565   }
566 #endif
567   return nullptr;
568 }
569 
570 static void* event_to_env(void* env_pv, const char* event, void* arg) {
571   decode_env* env = (decode_env*) env_pv;
572   return env->handle_event(event, (address) arg);
573 }
574 
575 // called by the disassembler to print out jump targets and data addresses
576 void decode_env::print_address(address adr) {
577   outputStream* st = output();
578 
579   if (adr == nullptr) {
580     st->print("nullptr");
581     return;
582   }
583 
584   int small_num = (int)(intptr_t)adr;
585   if ((intptr_t)adr == (intptr_t)small_num
586       && -1 <= small_num && small_num <= 9) {
587     st->print("%d", small_num);
588     return;
589   }
590 
591   if (Universe::is_fully_initialized()) {
592     if (StubRoutines::contains(adr)) {
593       StubCodeDesc* desc = StubCodeDesc::desc_for(adr);
594       if (desc == nullptr) {
595         desc = StubCodeDesc::desc_for(adr + frame::pc_return_offset);
596       }
597       if (desc != nullptr) {
598         st->print("Stub::%s", desc->name());
599         if (desc->begin() != adr) {
600           st->print(INTX_FORMAT_W(+) " " PTR_FORMAT, adr - desc->begin(), p2i(adr));
601         } else if (WizardMode) {
602           st->print(" " PTR_FORMAT, p2i(adr));
603         }
604         return;
605       }
606       st->print("Stub::<unknown> " PTR_FORMAT, p2i(adr));
607       return;
608     }
609 
610     BarrierSet* bs = BarrierSet::barrier_set();
611     if (bs->is_a(BarrierSet::CardTableBarrierSet) &&
612         adr == ci_card_table_address_as<address>()) {
613       st->print("word_map_base");
614       if (WizardMode) st->print(" " INTPTR_FORMAT, p2i(adr));
615       return;
616     }
617   }
618 
619   if (_nm == nullptr) {
620     // Don't do this for native methods, as the function name will be printed in
621     // nmethod::reloc_string_for().
622     // Allocate the buffer on the stack instead of as RESOURCE array.
623     // In case we do DecodeErrorFile, Thread will not be initialized,
624     // causing a "assert(current != __null) failed" failure.
625     const int buflen = 1024;
626     char buf[buflen];
627     int offset;
628     if (os::dll_address_to_function_name(adr, buf, buflen, &offset)) {
629       st->print(PTR_FORMAT " = %s",  p2i(adr), buf);
630       if (offset != 0) {
631         st->print("+%d", offset);
632       }
633       return;
634     }
635   }
636 
637   // Fall through to a simple (hexadecimal) numeral.
638   st->print(PTR_FORMAT, p2i(adr));
639 }
640 
641 void decode_env::print_insn_labels() {
642   if (AbstractDisassembler::show_block_comment()) {
643     address       p  = cur_insn();
644     outputStream* st = output();
645 
646     //---<  Block comments for nmethod  >---
647     // Outputs a bol() before and a cr() after, but only if a comment is printed.
648     // Prints nmethod_section_label as well.
649     if (_nm != nullptr) {
650       _nm->print_block_comment(st, p);
651     }
652     else if (_codeBlob != nullptr) {
653       _codeBlob->print_block_comment(st, p);
654     }
655 #ifndef PRODUCT
656     else if (_remarks != nullptr) {
657       _remarks->print((p - _start) + _disp, st);
658     }
659 #endif
660   }
661 }
662 
663 void decode_env::print_insn_prefix() {
664   address       p  = cur_insn();
665   outputStream* st = output();
666   AbstractDisassembler::print_location(p, _start, _end, st, false, false);
667   AbstractDisassembler::print_instruction(p, Assembler::instr_len(p), Assembler::instr_maxlen(), st, true, false);
668 }
669 
670 ATTRIBUTE_PRINTF(2, 3)
671 static int printf_to_env(void* env_pv, const char* format, ...) {
672   decode_env* env = (decode_env*) env_pv;
673   outputStream* st = env->output();
674   size_t flen = strlen(format);
675   const char* raw = nullptr;
676   if (flen == 0)  return 0;
677   if (flen == 1 && format[0] == '\n') { st->bol(); return 1; }
678   if (flen < 2 ||
679       strchr(format, '%') == nullptr) {
680     raw = format;
681   } else if (format[0] == '%' && format[1] == '%' &&
682              strchr(format+2, '%') == nullptr) {
683     // happens a lot on machines with names like %foo
684     flen--;
685     raw = format+1;
686   }
687   if (raw != nullptr) {
688     st->print_raw(raw, flen);
689     return (int) flen;
690   }
691   va_list ap;
692   va_start(ap, format);
693   julong cnt0 = st->count();
694   st->vprint(format, ap);
695   julong cnt1 = st->count();
696   va_end(ap);
697   return (int)(cnt1 - cnt0);
698 }
699 
700 // The 'original_start' argument holds the original address where
701 // the instructions were located in the originating system. If zero (nullptr)
702 // is passed in, there is no original address.
703 address decode_env::decode_instructions(address start, address end, address original_start /* = 0*/) {
704   // CodeComment in Stubs.
705   // Properly initialize _start/_end. Overwritten too often if
706   // printing of instructions is called for each instruction.
707   assert((_start == nullptr) || (start == nullptr) || (_start == start), "don't overwrite CTOR values");
708   assert((_end   == nullptr) || (end   == nullptr) || (_end   == end  ), "don't overwrite CTOR values");
709   if (start != nullptr) set_start(start);
710   if (end   != nullptr) set_end(end);
711   if (original_start == nullptr) {
712     original_start = start;
713   }
714 
715   //---<  Check (and correct) alignment  >---
716   // Don't check alignment of end, it is not aligned.
717   if (((uint64_t)start & ((uint64_t)Disassembler::pd_instruction_alignment() - 1)) != 0) {
718     output()->print_cr("Decode range start:" PTR_FORMAT ": ... (unaligned)", p2i(start));
719     start = (address)((uint64_t)start & ~((uint64_t)Disassembler::pd_instruction_alignment() - 1));
720   }
721 
722   // Trying to decode instructions doesn't make sense if we
723   // couldn't load the disassembler library.
724   if (Disassembler::is_abstract()) {
725     return nullptr;
726   }
727 
728   // decode a series of instructions and return the end of the last instruction
729 
730   if (_print_raw) {
731     // Print whatever the library wants to print, w/o fancy callbacks.
732     // This is mainly for debugging the library itself.
733     FILE* out = stdout;
734     FILE* xmlout = (_print_raw > 1 ? out : nullptr);
735     return
736       (address)
737       (*Disassembler::_decode_instructions_virtual)((uintptr_t)start, (uintptr_t)end,
738                                                     start, end - start,
739                                                     nullptr, (void*) xmlout,
740                                                     nullptr, (void*) out,
741                                                     options(), 0/*nice new line*/);
742   }
743 
744   return
745     (address)
746     (*Disassembler::_decode_instructions_virtual)((uintptr_t)start, (uintptr_t)end,
747                                                   start, end - start,
748                                                   &event_to_env,  (void*) this,
749                                                   &printf_to_env, (void*) this,
750                                                   options(), 0/*nice new line*/);
751 }
752 
753 // ----------------------------------------------------------------------------
754 // Disassembler
755 // Used as a static wrapper for decode_env.
756 // Each method will create a decode_env before decoding.
757 // You can call the decode_env methods directly if you already have one.
758 
759 void* Disassembler::dll_load(char* buf, int buflen, int offset, char* ebuf, int ebuflen, outputStream* st) {
760   int sz = buflen - offset;
761   int written = jio_snprintf(&buf[offset], sz, "%s%s", hsdis_library_name, JNI_LIB_SUFFIX);
762   if (written < sz) { // written successfully, not truncated.
763     if (Verbose) st->print_cr("Trying to load: %s", buf);
764     return os::dll_load(buf, ebuf, ebuflen);
765   } else if (Verbose) {
766     st->print_cr("Try to load hsdis library failed: the length of path is beyond the OS limit");
767   }
768   return nullptr;
769 }
770 
771 bool Disassembler::load_library(outputStream* st) {
772   // Do not try to load multiple times. Failed once -> fails always.
773   // To force retry in debugger: assign _tried_to_load_library=0
774   if (_tried_to_load_library) {
775     return _library_usable;
776   }
777 
778 #if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY)
779   // Print to given stream, if any.
780   // Print to tty if Verbose is on and no stream given.
781   st = ((st == nullptr) && Verbose) ? tty : st;
782 
783   // Compute fully qualified library name.
784   char ebuf[1024];
785   char buf[JVM_MAXPATHLEN];
786   os::jvm_path(buf, sizeof(buf));
787   int jvm_offset = -1;
788   int lib_offset = -1;
789 
790   if (is_vm_statically_linked()) {
791     char* p = strrchr(buf, '/');
792     *p = '\0';
793     strcat(p, "/lib/");
794     lib_offset = jvm_offset = (int)strlen(buf);
795   } else {
796     // Match "libjvm" instead of "jvm" on *nix platforms. Creates better matches.
797     // Match "[lib]jvm[^/]*" in jvm_path.
798     const char* base = buf;
799     const char* p = strrchr(buf, *os::file_separator());
800     if (p != nullptr) lib_offset = p - base + 1; // this points to the first char after separator
801 #ifdef _WIN32
802     p = strstr(p ? p : base, "jvm");
803     if (p != nullptr) jvm_offset = p - base;     // this points to 'j' in jvm.
804 #else
805     p = strstr(p ? p : base, "libjvm");
806     if (p != nullptr) jvm_offset = p - base + 3; // this points to 'j' in libjvm.
807 #endif
808   }
809 
810   // Find the disassembler shared library.
811   // Search for several paths derived from libjvm, in this order:
812   // 1. <home>/lib/<vm>/libhsdis-<arch>.so  (for compatibility)
813   // 2. <home>/lib/<vm>/hsdis-<arch>.so
814   // 3. <home>/lib/hsdis-<arch>.so
815   // 4. hsdis-<arch>.so  (using LD_LIBRARY_PATH)
816   if (jvm_offset >= 0) {
817     // 1. <home>/lib/<vm>/libhsdis-<arch>.so
818     _library = dll_load(buf, sizeof buf, jvm_offset, ebuf, sizeof ebuf, st);
819     if (_library == nullptr && lib_offset >= 0) {
820       // 2. <home>/lib/<vm>/hsdis-<arch>.so
821       _library = dll_load(buf, sizeof buf, lib_offset, ebuf, sizeof ebuf, st);
822     }
823     if (_library == nullptr && lib_offset > 0) {
824       // 3. <home>/lib/hsdis-<arch>.so
825       buf[lib_offset - 1] = '\0';
826       const char* p = strrchr(buf, *os::file_separator());
827       if (p != nullptr) {
828         lib_offset = p - buf + 1;
829         _library = dll_load(buf, sizeof buf, lib_offset, ebuf, sizeof ebuf, st);
830       }
831     }
832   }
833   if (_library == nullptr) {
834     _library = dll_load(buf, sizeof buf, 0, ebuf, sizeof ebuf, st);
835   }
836 
837   // load the decoder function to use.
838   if (_library != nullptr) {
839     _decode_instructions_virtual = CAST_TO_FN_PTR(Disassembler::decode_func_virtual,
840                                           os::dll_lookup(_library, decode_instructions_virtual_name));
841   } else {
842     log_warning(os)("Loading hsdis library failed");
843   }
844   _tried_to_load_library = true;
845   _library_usable        = _decode_instructions_virtual != nullptr;
846 
847   // Create a dummy environment to initialize PrintAssemblyOptions.
848   // The PrintAssemblyOptions must be known for abstract disassemblies as well.
849   decode_env dummy((unsigned char*)(&buf[0]), (unsigned char*)(&buf[1]), st);
850 
851   // Report problems during dll_load or dll_lookup, if any.
852   if (st != nullptr) {
853     // Success.
854     if (_library_usable) {
855       st->print_cr("Loaded disassembler from %s", buf);
856     } else {
857       st->print_cr("Could not load %s; %s; %s",
858                    buf,
859                    ((_library != nullptr)
860                     ? "entry point is missing"
861                     : ((WizardMode || PrintMiscellaneous)
862                        ? (const char*)ebuf
863                        : "library not loadable")),
864                    "PrintAssembly defaults to abstract disassembly.");
865     }
866   }
867 #endif
868   return _library_usable;
869 }
870 
871 
872 // Directly disassemble code blob.
873 void Disassembler::decode(CodeBlob* cb, outputStream* st) {
874 #if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY)
875   if (cb->is_nmethod()) {
876     // If we  have an nmethod at hand,
877     // call the specialized decoder directly.
878     ((nmethod*)cb)->decode2(st);
879     return;
880   }
881 
882   decode_env env(cb, st);
883   env.output()->print_cr("--------------------------------------------------------------------------------");
884   env.output()->print("Decoding CodeBlob");
885   if (cb->name() != nullptr) {
886     env.output()->print(", name: %s,", cb->name());
887   }
888   env.output()->print_cr(" at  [" PTR_FORMAT ", " PTR_FORMAT "]  " JLONG_FORMAT " bytes", p2i(cb->code_begin()), p2i(cb->code_end()), ((jlong)(cb->code_end() - cb->code_begin())));
889 
890   if (is_abstract()) {
891     AbstractDisassembler::decode_abstract(cb->code_begin(), cb->code_end(), env.output(), Assembler::instr_maxlen());
892   } else {
893     env.decode_instructions(cb->code_begin(), cb->code_end());
894   }
895   env.output()->print_cr("--------------------------------------------------------------------------------");
896 #endif
897 }
898 
899 // Decode a nmethod.
900 // This includes printing the constant pool and all code segments.
901 // The nmethod data structures (oop maps, relocations and the like) are not printed.
902 void Disassembler::decode(nmethod* nm, outputStream* st) {
903 #if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY)
904   ttyLocker ttyl;
905 
906   decode_env env(nm, st);
907   env.output()->print_cr("--------------------------------------------------------------------------------");
908   nm->print_constant_pool(env.output());
909   env.output()->print_cr("--------------------------------------------------------------------------------");
910   env.output()->cr();
911   if (is_abstract()) {
912     AbstractDisassembler::decode_abstract(nm->code_begin(), nm->code_end(), env.output(), Assembler::instr_maxlen());
913   } else {
914     env.decode_instructions(nm->code_begin(), nm->code_end());
915   }
916   env.output()->print_cr("--------------------------------------------------------------------------------");
917 #endif
918 }
919 
920 // Decode a range, given as [start address, end address)
921 void Disassembler::decode(address start, address end, outputStream* st
922                           NOT_PRODUCT(COMMA const AsmRemarks* remarks COMMA ptrdiff_t disp)) {
923 #if defined(SUPPORT_ASSEMBLY) || defined(SUPPORT_ABSTRACT_ASSEMBLY)
924   //---<  Test memory before decoding  >---
925   if (!os::is_readable_range(start, end)) {
926     //---<  Allow output suppression, but prevent writing to a nullptr stream. Could happen with +PrintStubCode.  >---
927     if (st != nullptr) {
928       st->print("Memory range [" PTR_FORMAT ".." PTR_FORMAT "] not readable", p2i(start), p2i(end));
929     }
930     return;
931   }
932 
933   if (is_abstract()) {
934     AbstractDisassembler::decode_abstract(start, end, st, Assembler::instr_maxlen());
935   } else {
936     // This seems to be just a chunk of memory.
937     decode_env env(start, end, st NOT_PRODUCT(COMMA remarks COMMA disp));
938     env.output()->print_cr("--------------------------------------------------------------------------------");
939     env.decode_instructions(start, end);
940     env.output()->print_cr("--------------------------------------------------------------------------------");
941   }
942 #endif
943 }
944 
945 // To prevent excessive code expansion in the interpreter generator, we
946 // do not inline this function into Disassembler::hook().
947 void Disassembler::_hook(const char* file, int line, MacroAssembler* masm) {
948   decode_env::hook(file, line, masm->code_section()->end());
949 }