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