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