hsdis.c revision 5776:de6a9e811145
1/* 2 * Copyright (c) 2008, 2013, 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/* hsdis.c -- dump a range of addresses as native instructions 26 This implements the plugin protocol required by the 27 HotSpot PrintAssembly option. 28*/ 29 30#include <config.h> /* required by bfd.h */ 31#include <libiberty.h> 32#include <bfd.h> 33#include <dis-asm.h> 34#include <inttypes.h> 35#include <string.h> 36#include <errno.h> 37#include "hsdis.h" 38 39#ifndef bool 40#define bool int 41#define true 1 42#define false 0 43#endif /*bool*/ 44 45/* short names for stuff in hsdis.h */ 46typedef decode_instructions_event_callback_ftype event_callback_t; 47typedef decode_instructions_printf_callback_ftype printf_callback_t; 48 49/* disassemble_info.application_data object */ 50struct hsdis_app_data { 51 /* virtual address of data */ 52 uintptr_t start_va, end_va; 53 /* the instructions to be decoded */ 54 unsigned char* buffer; 55 uintptr_t length; 56 event_callback_t event_callback; void* event_stream; 57 printf_callback_t printf_callback; void* printf_stream; 58 bool losing; 59 bool do_newline; 60 61 /* the architecture being disassembled */ 62 const char* arch_name; 63 const bfd_arch_info_type* arch_info; 64 65 /* the disassembler we are going to use: */ 66 disassembler_ftype dfn; 67 struct disassemble_info dinfo; /* the actual struct! */ 68 69 char mach_option[64]; 70 char insn_options[256]; 71}; 72 73static void* decode(struct hsdis_app_data* app_data, const char* options); 74 75#define DECL_APP_DATA(dinfo) \ 76 struct hsdis_app_data* app_data = (struct hsdis_app_data*) (dinfo)->application_data 77 78#define DECL_EVENT_CALLBACK(app_data) \ 79 event_callback_t event_callback = (app_data)->event_callback; \ 80 void* event_stream = (app_data)->event_stream 81 82#define DECL_PRINTF_CALLBACK(app_data) \ 83 printf_callback_t printf_callback = (app_data)->printf_callback; \ 84 void* printf_stream = (app_data)->printf_stream 85 86 87static void print_help(struct hsdis_app_data* app_data, 88 const char* msg, const char* arg); 89static void setup_app_data(struct hsdis_app_data* app_data, 90 const char* options); 91static const char* format_insn_close(const char* close, 92 disassemble_info* dinfo, 93 char* buf, size_t bufsize); 94 95void* 96#ifdef DLL_ENTRY 97 DLL_ENTRY 98#endif 99decode_instructions_virtual(uintptr_t start_va, uintptr_t end_va, 100 unsigned char* buffer, uintptr_t length, 101 event_callback_t event_callback_arg, void* event_stream_arg, 102 printf_callback_t printf_callback_arg, void* printf_stream_arg, 103 const char* options, int newline) { 104 struct hsdis_app_data app_data; 105 memset(&app_data, 0, sizeof(app_data)); 106 app_data.start_va = start_va; 107 app_data.end_va = end_va; 108 app_data.buffer = buffer; 109 app_data.length = length; 110 app_data.event_callback = event_callback_arg; 111 app_data.event_stream = event_stream_arg; 112 app_data.printf_callback = printf_callback_arg; 113 app_data.printf_stream = printf_stream_arg; 114 app_data.do_newline = newline == 0 ? false : true; 115 116 return decode(&app_data, options); 117} 118 119/* This is the compatability interface for older version of hotspot */ 120void* 121#ifdef DLL_ENTRY 122 DLL_ENTRY 123#endif 124decode_instructions(void* start_pv, void* end_pv, 125 event_callback_t event_callback_arg, void* event_stream_arg, 126 printf_callback_t printf_callback_arg, void* printf_stream_arg, 127 const char* options) { 128 decode_instructions_virtual((uintptr_t)start_pv, 129 (uintptr_t)end_pv, 130 (unsigned char*)start_pv, 131 (uintptr_t)end_pv - (uintptr_t)start_pv, 132 event_callback_arg, 133 event_stream_arg, 134 printf_callback_arg, 135 printf_stream_arg, 136 options, false); 137} 138 139static void* decode(struct hsdis_app_data* app_data, const char* options) { 140 setup_app_data(app_data, options); 141 char buf[128]; 142 143 { 144 /* now reload everything from app_data: */ 145 DECL_EVENT_CALLBACK(app_data); 146 DECL_PRINTF_CALLBACK(app_data); 147 uintptr_t start = app_data->start_va; 148 uintptr_t end = app_data->end_va; 149 uintptr_t p = start; 150 151 (*event_callback)(event_stream, "insns", (void*)start); 152 153 (*event_callback)(event_stream, "mach name='%s'", 154 (void*) app_data->arch_info->printable_name); 155 if (app_data->dinfo.bytes_per_line != 0) { 156 (*event_callback)(event_stream, "format bytes-per-line='%p'/", 157 (void*)(intptr_t) app_data->dinfo.bytes_per_line); 158 } 159 160 while (p < end && !app_data->losing) { 161 (*event_callback)(event_stream, "insn", (void*) p); 162 163 /* reset certain state, so we can read it with confidence */ 164 app_data->dinfo.insn_info_valid = 0; 165 app_data->dinfo.branch_delay_insns = 0; 166 app_data->dinfo.data_size = 0; 167 app_data->dinfo.insn_type = 0; 168 169 int size = (*app_data->dfn)((bfd_vma) p, &app_data->dinfo); 170 171 if (size > 0) p += size; 172 else app_data->losing = true; 173 174 if (!app_data->losing) { 175 const char* insn_close = format_insn_close("/insn", &app_data->dinfo, 176 buf, sizeof(buf)); 177 (*event_callback)(event_stream, insn_close, (void*) p); 178 179 if (app_data->do_newline) { 180 /* follow each complete insn by a nice newline */ 181 (*printf_callback)(printf_stream, "\n"); 182 } 183 } 184 } 185 186 if (app_data->losing) (*event_callback)(event_stream, "/insns", (void*) p); 187 return (void*) p; 188 } 189} 190 191/* take the address of the function, for luck, and also test the typedef: */ 192const decode_func_vtype decode_func_virtual_address = &decode_instructions_virtual; 193const decode_func_stype decode_func_address = &decode_instructions; 194 195static const char* format_insn_close(const char* close, 196 disassemble_info* dinfo, 197 char* buf, size_t bufsize) { 198 if (!dinfo->insn_info_valid) 199 return close; 200 enum dis_insn_type itype = dinfo->insn_type; 201 int dsize = dinfo->data_size, delays = dinfo->branch_delay_insns; 202 if ((itype == dis_nonbranch && (dsize | delays) == 0) 203 || (strlen(close) + 3*20 > bufsize)) 204 return close; 205 206 const char* type = "unknown"; 207 switch (itype) { 208 case dis_nonbranch: type = NULL; break; 209 case dis_branch: type = "branch"; break; 210 case dis_condbranch: type = "condbranch"; break; 211 case dis_jsr: type = "jsr"; break; 212 case dis_condjsr: type = "condjsr"; break; 213 case dis_dref: type = "dref"; break; 214 case dis_dref2: type = "dref2"; break; 215 } 216 217 strcpy(buf, close); 218 char* p = buf; 219 if (type) sprintf(p += strlen(p), " type='%s'", type); 220 if (dsize) sprintf(p += strlen(p), " dsize='%d'", dsize); 221 if (delays) sprintf(p += strlen(p), " delay='%d'", delays); 222 return buf; 223} 224 225/* handler functions */ 226 227static int 228hsdis_read_memory_func(bfd_vma memaddr, 229 bfd_byte* myaddr, 230 unsigned int length, 231 struct disassemble_info* dinfo) { 232 DECL_APP_DATA(dinfo); 233 /* convert the virtual address memaddr into an address within memory buffer */ 234 uintptr_t offset = ((uintptr_t) memaddr) - app_data->start_va; 235 if (offset + length > app_data->length) { 236 /* read is out of bounds */ 237 return EIO; 238 } else { 239 memcpy(myaddr, (bfd_byte*) (app_data->buffer + offset), length); 240 return 0; 241 } 242} 243 244static void 245hsdis_print_address_func(bfd_vma vma, struct disassemble_info* dinfo) { 246 /* the actual value to print: */ 247 void* addr_value = (void*) (uintptr_t) vma; 248 DECL_APP_DATA(dinfo); 249 DECL_EVENT_CALLBACK(app_data); 250 251 /* issue the event: */ 252 void* result = 253 (*event_callback)(event_stream, "addr/", addr_value); 254 if (result == NULL) { 255 /* event declined */ 256 generic_print_address(vma, dinfo); 257 } 258} 259 260 261/* configuration */ 262 263static void set_optional_callbacks(struct hsdis_app_data* app_data); 264static void parse_caller_options(struct hsdis_app_data* app_data, 265 const char* caller_options); 266static const char* native_arch_name(); 267static enum bfd_endian native_endian(); 268static const bfd_arch_info_type* find_arch_info(const char* arch_nane); 269static bfd* get_native_bfd(const bfd_arch_info_type* arch_info, 270 /* to avoid malloc: */ 271 bfd* empty_bfd, bfd_target* empty_xvec); 272static void init_disassemble_info_from_bfd(struct disassemble_info* dinfo, 273 void *stream, 274 fprintf_ftype fprintf_func, 275 bfd* bfd, 276 char* disassembler_options); 277static void parse_fake_insn(disassembler_ftype dfn, 278 struct disassemble_info* dinfo); 279 280static void setup_app_data(struct hsdis_app_data* app_data, 281 const char* caller_options) { 282 /* Make reasonable defaults for null callbacks. 283 A non-null stream for a null callback is assumed to be a FILE* for output. 284 Events are rendered as XML. 285 */ 286 set_optional_callbacks(app_data); 287 288 /* Look into caller_options for anything interesting. */ 289 if (caller_options != NULL) 290 parse_caller_options(app_data, caller_options); 291 292 /* Discover which architecture we are going to disassemble. */ 293 app_data->arch_name = &app_data->mach_option[0]; 294 if (app_data->arch_name[0] == '\0') 295 app_data->arch_name = native_arch_name(); 296 app_data->arch_info = find_arch_info(app_data->arch_name); 297 298 /* Make a fake bfd to hold the arch. and byteorder info. */ 299 struct { 300 bfd_target empty_xvec; 301 bfd empty_bfd; 302 } buf; 303 bfd* native_bfd = get_native_bfd(app_data->arch_info, 304 /* to avoid malloc: */ 305 &buf.empty_bfd, &buf.empty_xvec); 306 init_disassemble_info_from_bfd(&app_data->dinfo, 307 app_data->printf_stream, 308 app_data->printf_callback, 309 native_bfd, 310 app_data->insn_options); 311 312 /* Finish linking together the various callback blocks. */ 313 app_data->dinfo.application_data = (void*) app_data; 314 app_data->dfn = disassembler(native_bfd); 315 app_data->dinfo.print_address_func = hsdis_print_address_func; 316 app_data->dinfo.read_memory_func = hsdis_read_memory_func; 317 318 if (app_data->dfn == NULL) { 319 const char* bad = app_data->arch_name; 320 static bool complained; 321 if (bad == &app_data->mach_option[0]) 322 print_help(app_data, "bad mach=%s", bad); 323 else if (!complained) 324 print_help(app_data, "bad native mach=%s; please port hsdis to this platform", bad); 325 complained = true; 326 /* must bail out */ 327 app_data->losing = true; 328 return; 329 } 330 331 parse_fake_insn(app_data->dfn, &app_data->dinfo); 332} 333 334 335/* ignore all events, return a null */ 336static void* null_event_callback(void* ignore_stream, const char* ignore_event, void* arg) { 337 return NULL; 338} 339 340/* print all events as XML markup */ 341static void* xml_event_callback(void* stream, const char* event, void* arg) { 342 FILE* fp = (FILE*) stream; 343#define NS_PFX "dis:" 344 if (event[0] != '/') { 345 /* issue the tag, with or without a formatted argument */ 346 fprintf(fp, "<"NS_PFX); 347 fprintf(fp, event, arg); 348 fprintf(fp, ">"); 349 } else { 350 ++event; /* skip slash */ 351 const char* argp = strchr(event, ' '); 352 if (argp == NULL) { 353 /* no arguments; just issue the closing tag */ 354 fprintf(fp, "</"NS_PFX"%s>", event); 355 } else { 356 /* split out the closing attributes as <dis:foo_done attr='val'/> */ 357 int event_prefix = (argp - event); 358 fprintf(fp, "<"NS_PFX"%.*s_done", event_prefix, event); 359 fprintf(fp, argp, arg); 360 fprintf(fp, "/></"NS_PFX"%.*s>", event_prefix, event); 361 } 362 } 363 return NULL; 364} 365 366static void set_optional_callbacks(struct hsdis_app_data* app_data) { 367 if (app_data->printf_callback == NULL) { 368 int (*fprintf_callback)(FILE*, const char*, ...) = &fprintf; 369 FILE* fprintf_stream = stdout; 370 app_data->printf_callback = (printf_callback_t) fprintf_callback; 371 if (app_data->printf_stream == NULL) 372 app_data->printf_stream = (void*) fprintf_stream; 373 } 374 if (app_data->event_callback == NULL) { 375 if (app_data->event_stream == NULL) 376 app_data->event_callback = &null_event_callback; 377 else 378 app_data->event_callback = &xml_event_callback; 379 } 380 381} 382 383static void parse_caller_options(struct hsdis_app_data* app_data, const char* caller_options) { 384 char* iop_base = app_data->insn_options; 385 char* iop_limit = iop_base + sizeof(app_data->insn_options) - 1; 386 char* iop = iop_base; 387 const char* p; 388 for (p = caller_options; p != NULL; ) { 389 const char* q = strchr(p, ','); 390 size_t plen = (q == NULL) ? strlen(p) : ((q++) - p); 391 if (plen == 4 && strncmp(p, "help", plen) == 0) { 392 print_help(app_data, NULL, NULL); 393 } else if (plen >= 5 && strncmp(p, "mach=", 5) == 0) { 394 char* mach_option = app_data->mach_option; 395 size_t mach_size = sizeof(app_data->mach_option); 396 mach_size -= 1; /*leave room for the null*/ 397 if (plen > mach_size) plen = mach_size; 398 strncpy(mach_option, p, plen); 399 mach_option[plen] = '\0'; 400 } else if (plen > 6 && strncmp(p, "hsdis-", 6) == 0) { 401 // do not pass these to the next level 402 } else { 403 /* just copy it; {i386,sparc}-dis.c might like to see it */ 404 if (iop > iop_base && iop < iop_limit) (*iop++) = ','; 405 if (iop + plen > iop_limit) 406 plen = iop_limit - iop; 407 strncpy(iop, p, plen); 408 iop += plen; 409 } 410 p = q; 411 } 412} 413 414static void print_help(struct hsdis_app_data* app_data, 415 const char* msg, const char* arg) { 416 DECL_PRINTF_CALLBACK(app_data); 417 if (msg != NULL) { 418 (*printf_callback)(printf_stream, "hsdis: "); 419 (*printf_callback)(printf_stream, msg, arg); 420 (*printf_callback)(printf_stream, "\n"); 421 } 422 (*printf_callback)(printf_stream, "hsdis output options:\n"); 423 if (printf_callback == (printf_callback_t) &fprintf) 424 disassembler_usage((FILE*) printf_stream); 425 else 426 disassembler_usage(stderr); /* better than nothing */ 427 (*printf_callback)(printf_stream, " mach=<arch> select disassembly mode\n"); 428#if defined(LIBARCH_i386) || defined(LIBARCH_amd64) 429 (*printf_callback)(printf_stream, " mach=i386 select 32-bit mode\n"); 430 (*printf_callback)(printf_stream, " mach=x86-64 select 64-bit mode\n"); 431 (*printf_callback)(printf_stream, " suffix always print instruction suffix\n"); 432#endif 433 (*printf_callback)(printf_stream, " help print this message\n"); 434} 435 436 437/* low-level bfd and arch stuff that binutils doesn't do for us */ 438 439static const bfd_arch_info_type* find_arch_info(const char* arch_name) { 440 const bfd_arch_info_type* arch_info = bfd_scan_arch(arch_name); 441 if (arch_info == NULL) { 442 extern const bfd_arch_info_type bfd_default_arch_struct; 443 arch_info = &bfd_default_arch_struct; 444 } 445 return arch_info; 446} 447 448static const char* native_arch_name() { 449 const char* res = NULL; 450#ifdef LIBARCH_i386 451 res = "i386"; 452#endif 453#ifdef LIBARCH_amd64 454 res = "i386:x86-64"; 455#endif 456#ifdef LIBARCH_sparc 457 res = "sparc:v8plusb"; 458#endif 459#ifdef LIBARCH_sparcv9 460 res = "sparc:v9b"; 461#endif 462 if (res == NULL) 463 res = "architecture not set in Makefile!"; 464 return res; 465} 466 467static enum bfd_endian native_endian() { 468 int32_t endian_test = 'x'; 469 if (*(const char*) &endian_test == 'x') 470 return BFD_ENDIAN_LITTLE; 471 else 472 return BFD_ENDIAN_BIG; 473} 474 475static bfd* get_native_bfd(const bfd_arch_info_type* arch_info, 476 bfd* empty_bfd, bfd_target* empty_xvec) { 477 memset(empty_bfd, 0, sizeof(*empty_bfd)); 478 memset(empty_xvec, 0, sizeof(*empty_xvec)); 479 empty_xvec->flavour = bfd_target_unknown_flavour; 480 empty_xvec->byteorder = native_endian(); 481 empty_bfd->xvec = empty_xvec; 482 empty_bfd->arch_info = arch_info; 483 return empty_bfd; 484} 485 486static int read_zero_data_only(bfd_vma ignore_p, 487 bfd_byte* myaddr, unsigned int length, 488 struct disassemble_info *ignore_info) { 489 memset(myaddr, 0, length); 490 return 0; 491} 492static int print_to_dev_null(void* ignore_stream, const char* ignore_format, ...) { 493 return 0; 494} 495 496/* Prime the pump by running the selected disassembler on a null input. 497 This forces the machine-specific disassembler to divulge invariant 498 information like bytes_per_line. 499 */ 500static void parse_fake_insn(disassembler_ftype dfn, 501 struct disassemble_info* dinfo) { 502 typedef int (*read_memory_ftype) 503 (bfd_vma memaddr, bfd_byte *myaddr, unsigned int length, 504 struct disassemble_info *info); 505 read_memory_ftype read_memory_func = dinfo->read_memory_func; 506 fprintf_ftype fprintf_func = dinfo->fprintf_func; 507 508 dinfo->read_memory_func = &read_zero_data_only; 509 dinfo->fprintf_func = &print_to_dev_null; 510 (*dfn)(0, dinfo); 511 512 /* put it back */ 513 dinfo->read_memory_func = read_memory_func; 514 dinfo->fprintf_func = fprintf_func; 515} 516 517static void init_disassemble_info_from_bfd(struct disassemble_info* dinfo, 518 void *stream, 519 fprintf_ftype fprintf_func, 520 bfd* abfd, 521 char* disassembler_options) { 522 init_disassemble_info(dinfo, stream, fprintf_func); 523 524 dinfo->flavour = bfd_get_flavour(abfd); 525 dinfo->arch = bfd_get_arch(abfd); 526 dinfo->mach = bfd_get_mach(abfd); 527 dinfo->disassembler_options = disassembler_options; 528 dinfo->octets_per_byte = bfd_octets_per_byte (abfd); 529 dinfo->skip_zeroes = sizeof(void*) * 2; 530 dinfo->skip_zeroes_at_end = sizeof(void*)-1; 531 dinfo->disassembler_needs_relocs = FALSE; 532 533 if (bfd_big_endian(abfd)) 534 dinfo->display_endian = dinfo->endian = BFD_ENDIAN_BIG; 535 else if (bfd_little_endian(abfd)) 536 dinfo->display_endian = dinfo->endian = BFD_ENDIAN_LITTLE; 537 else 538 dinfo->endian = native_endian(); 539 540 disassemble_init_for_target(dinfo); 541} 542