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