Fri, 02 Nov 2012 13:30:47 -0700
8000489: older builds of hsdis don't work anymore after 6879063
Summary: The old function not defined properly, need a definition for export in dll. Also changes made to let new jvm work with old hsdis.
Reviewed-by: jrose, sspitsyn, kmo
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, int newline) {
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 = newline == 0 ? false : true;
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, false);
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);
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 if (app_data->losing) (*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_func_vtype decode_func_virtual_address = &decode_instructions_virtual;
192 const decode_func_stype decode_func_address = &decode_instructions;
194 static const char* format_insn_close(const char* close,
195 disassemble_info* dinfo,
196 char* buf, size_t bufsize) {
197 if (!dinfo->insn_info_valid)
198 return close;
199 enum dis_insn_type itype = dinfo->insn_type;
200 int dsize = dinfo->data_size, delays = dinfo->branch_delay_insns;
201 if ((itype == dis_nonbranch && (dsize | delays) == 0)
202 || (strlen(close) + 3*20 > bufsize))
203 return close;
205 const char* type = "unknown";
206 switch (itype) {
207 case dis_nonbranch: type = NULL; break;
208 case dis_branch: type = "branch"; break;
209 case dis_condbranch: type = "condbranch"; break;
210 case dis_jsr: type = "jsr"; break;
211 case dis_condjsr: type = "condjsr"; break;
212 case dis_dref: type = "dref"; break;
213 case dis_dref2: type = "dref2"; break;
214 }
216 strcpy(buf, close);
217 char* p = buf;
218 if (type) sprintf(p += strlen(p), " type='%s'", type);
219 if (dsize) sprintf(p += strlen(p), " dsize='%d'", dsize);
220 if (delays) sprintf(p += strlen(p), " delay='%d'", delays);
221 return buf;
222 }
224 /* handler functions */
226 static int
227 hsdis_read_memory_func(bfd_vma memaddr,
228 bfd_byte* myaddr,
229 unsigned int length,
230 struct disassemble_info* dinfo) {
231 DECL_APP_DATA(dinfo);
232 /* convert the virtual address memaddr into an address within memory buffer */
233 uintptr_t offset = ((uintptr_t) memaddr) - app_data->start_va;
234 if (offset + length > app_data->length) {
235 /* read is out of bounds */
236 return EIO;
237 } else {
238 memcpy(myaddr, (bfd_byte*) (app_data->buffer + offset), length);
239 return 0;
240 }
241 }
243 static void
244 hsdis_print_address_func(bfd_vma vma, struct disassemble_info* dinfo) {
245 /* the actual value to print: */
246 void* addr_value = (void*) (uintptr_t) vma;
247 DECL_APP_DATA(dinfo);
248 DECL_EVENT_CALLBACK(app_data);
250 /* issue the event: */
251 void* result =
252 (*event_callback)(event_stream, "addr/", addr_value);
253 if (result == NULL) {
254 /* event declined */
255 generic_print_address(vma, dinfo);
256 }
257 }
260 /* configuration */
262 static void set_optional_callbacks(struct hsdis_app_data* app_data);
263 static void parse_caller_options(struct hsdis_app_data* app_data,
264 const char* caller_options);
265 static const char* native_arch_name();
266 static enum bfd_endian native_endian();
267 static const bfd_arch_info_type* find_arch_info(const char* arch_nane);
268 static bfd* get_native_bfd(const bfd_arch_info_type* arch_info,
269 /* to avoid malloc: */
270 bfd* empty_bfd, bfd_target* empty_xvec);
271 static void init_disassemble_info_from_bfd(struct disassemble_info* dinfo,
272 void *stream,
273 fprintf_ftype fprintf_func,
274 bfd* bfd,
275 char* disassembler_options);
276 static void parse_fake_insn(disassembler_ftype dfn,
277 struct disassemble_info* dinfo);
279 static void setup_app_data(struct hsdis_app_data* app_data,
280 const char* caller_options) {
281 /* Make reasonable defaults for null callbacks.
282 A non-null stream for a null callback is assumed to be a FILE* for output.
283 Events are rendered as XML.
284 */
285 set_optional_callbacks(app_data);
287 /* Look into caller_options for anything interesting. */
288 if (caller_options != NULL)
289 parse_caller_options(app_data, caller_options);
291 /* Discover which architecture we are going to disassemble. */
292 app_data->arch_name = &app_data->mach_option[0];
293 if (app_data->arch_name[0] == '\0')
294 app_data->arch_name = native_arch_name();
295 app_data->arch_info = find_arch_info(app_data->arch_name);
297 /* Make a fake bfd to hold the arch. and byteorder info. */
298 struct {
299 bfd_target empty_xvec;
300 bfd empty_bfd;
301 } buf;
302 bfd* native_bfd = get_native_bfd(app_data->arch_info,
303 /* to avoid malloc: */
304 &buf.empty_bfd, &buf.empty_xvec);
305 init_disassemble_info_from_bfd(&app_data->dinfo,
306 app_data->printf_stream,
307 app_data->printf_callback,
308 native_bfd,
309 app_data->insn_options);
311 /* Finish linking together the various callback blocks. */
312 app_data->dinfo.application_data = (void*) app_data;
313 app_data->dfn = disassembler(native_bfd);
314 app_data->dinfo.print_address_func = hsdis_print_address_func;
315 app_data->dinfo.read_memory_func = hsdis_read_memory_func;
317 if (app_data->dfn == NULL) {
318 const char* bad = app_data->arch_name;
319 static bool complained;
320 if (bad == &app_data->mach_option[0])
321 print_help(app_data, "bad mach=%s", bad);
322 else if (!complained)
323 print_help(app_data, "bad native mach=%s; please port hsdis to this platform", bad);
324 complained = true;
325 /* must bail out */
326 app_data->losing = true;
327 return;
328 }
330 parse_fake_insn(app_data->dfn, &app_data->dinfo);
331 }
334 /* ignore all events, return a null */
335 static void* null_event_callback(void* ignore_stream, const char* ignore_event, void* arg) {
336 return NULL;
337 }
339 /* print all events as XML markup */
340 static void* xml_event_callback(void* stream, const char* event, void* arg) {
341 FILE* fp = (FILE*) stream;
342 #define NS_PFX "dis:"
343 if (event[0] != '/') {
344 /* issue the tag, with or without a formatted argument */
345 fprintf(fp, "<"NS_PFX);
346 fprintf(fp, event, arg);
347 fprintf(fp, ">");
348 } else {
349 ++event; /* skip slash */
350 const char* argp = strchr(event, ' ');
351 if (argp == NULL) {
352 /* no arguments; just issue the closing tag */
353 fprintf(fp, "</"NS_PFX"%s>", event);
354 } else {
355 /* split out the closing attributes as <dis:foo_done attr='val'/> */
356 int event_prefix = (argp - event);
357 fprintf(fp, "<"NS_PFX"%.*s_done", event_prefix, event);
358 fprintf(fp, argp, arg);
359 fprintf(fp, "/></"NS_PFX"%.*s>", event_prefix, event);
360 }
361 }
362 return NULL;
363 }
365 static void set_optional_callbacks(struct hsdis_app_data* app_data) {
366 if (app_data->printf_callback == NULL) {
367 int (*fprintf_callback)(FILE*, const char*, ...) = &fprintf;
368 FILE* fprintf_stream = stdout;
369 app_data->printf_callback = (printf_callback_t) fprintf_callback;
370 if (app_data->printf_stream == NULL)
371 app_data->printf_stream = (void*) fprintf_stream;
372 }
373 if (app_data->event_callback == NULL) {
374 if (app_data->event_stream == NULL)
375 app_data->event_callback = &null_event_callback;
376 else
377 app_data->event_callback = &xml_event_callback;
378 }
380 }
382 static void parse_caller_options(struct hsdis_app_data* app_data, const char* caller_options) {
383 char* iop_base = app_data->insn_options;
384 char* iop_limit = iop_base + sizeof(app_data->insn_options) - 1;
385 char* iop = iop_base;
386 const char* p;
387 for (p = caller_options; p != NULL; ) {
388 const char* q = strchr(p, ',');
389 size_t plen = (q == NULL) ? strlen(p) : ((q++) - p);
390 if (plen == 4 && strncmp(p, "help", plen) == 0) {
391 print_help(app_data, NULL, NULL);
392 } else if (plen >= 5 && strncmp(p, "mach=", 5) == 0) {
393 char* mach_option = app_data->mach_option;
394 size_t mach_size = sizeof(app_data->mach_option);
395 mach_size -= 1; /*leave room for the null*/
396 if (plen > mach_size) plen = mach_size;
397 strncpy(mach_option, p, plen);
398 mach_option[plen] = '\0';
399 } else if (plen > 6 && strncmp(p, "hsdis-", 6) == 0) {
400 // do not pass these to the next level
401 } else {
402 /* just copy it; {i386,sparc}-dis.c might like to see it */
403 if (iop > iop_base && iop < iop_limit) (*iop++) = ',';
404 if (iop + plen > iop_limit)
405 plen = iop_limit - iop;
406 strncpy(iop, p, plen);
407 iop += plen;
408 }
409 p = q;
410 }
411 }
413 static void print_help(struct hsdis_app_data* app_data,
414 const char* msg, const char* arg) {
415 DECL_PRINTF_CALLBACK(app_data);
416 if (msg != NULL) {
417 (*printf_callback)(printf_stream, "hsdis: ");
418 (*printf_callback)(printf_stream, msg, arg);
419 (*printf_callback)(printf_stream, "\n");
420 }
421 (*printf_callback)(printf_stream, "hsdis output options:\n");
422 if (printf_callback == (printf_callback_t) &fprintf)
423 disassembler_usage((FILE*) printf_stream);
424 else
425 disassembler_usage(stderr); /* better than nothing */
426 (*printf_callback)(printf_stream, " mach=<arch> select disassembly mode\n");
427 #if defined(LIBARCH_i386) || defined(LIBARCH_amd64)
428 (*printf_callback)(printf_stream, " mach=i386 select 32-bit mode\n");
429 (*printf_callback)(printf_stream, " mach=x86-64 select 64-bit mode\n");
430 (*printf_callback)(printf_stream, " suffix always print instruction suffix\n");
431 #endif
432 (*printf_callback)(printf_stream, " help print this message\n");
433 }
436 /* low-level bfd and arch stuff that binutils doesn't do for us */
438 static const bfd_arch_info_type* find_arch_info(const char* arch_name) {
439 const bfd_arch_info_type* arch_info = bfd_scan_arch(arch_name);
440 if (arch_info == NULL) {
441 extern const bfd_arch_info_type bfd_default_arch_struct;
442 arch_info = &bfd_default_arch_struct;
443 }
444 return arch_info;
445 }
447 static const char* native_arch_name() {
448 const char* res = NULL;
449 #ifdef LIBARCH_i386
450 res = "i386";
451 #endif
452 #ifdef LIBARCH_amd64
453 res = "i386:x86-64";
454 #endif
455 #ifdef LIBARCH_sparc
456 res = "sparc:v8plusb";
457 #endif
458 #ifdef LIBARCH_sparcv9
459 res = "sparc:v9b";
460 #endif
461 if (res == NULL)
462 res = "architecture not set in Makefile!";
463 return res;
464 }
466 static enum bfd_endian native_endian() {
467 int32_t endian_test = 'x';
468 if (*(const char*) &endian_test == 'x')
469 return BFD_ENDIAN_LITTLE;
470 else
471 return BFD_ENDIAN_BIG;
472 }
474 static bfd* get_native_bfd(const bfd_arch_info_type* arch_info,
475 bfd* empty_bfd, bfd_target* empty_xvec) {
476 memset(empty_bfd, 0, sizeof(*empty_bfd));
477 memset(empty_xvec, 0, sizeof(*empty_xvec));
478 empty_xvec->flavour = bfd_target_unknown_flavour;
479 empty_xvec->byteorder = native_endian();
480 empty_bfd->xvec = empty_xvec;
481 empty_bfd->arch_info = arch_info;
482 return empty_bfd;
483 }
485 static int read_zero_data_only(bfd_vma ignore_p,
486 bfd_byte* myaddr, unsigned int length,
487 struct disassemble_info *ignore_info) {
488 memset(myaddr, 0, length);
489 return 0;
490 }
491 static int print_to_dev_null(void* ignore_stream, const char* ignore_format, ...) {
492 return 0;
493 }
495 /* Prime the pump by running the selected disassembler on a null input.
496 This forces the machine-specific disassembler to divulge invariant
497 information like bytes_per_line.
498 */
499 static void parse_fake_insn(disassembler_ftype dfn,
500 struct disassemble_info* dinfo) {
501 typedef int (*read_memory_ftype)
502 (bfd_vma memaddr, bfd_byte *myaddr, unsigned int length,
503 struct disassemble_info *info);
504 read_memory_ftype read_memory_func = dinfo->read_memory_func;
505 fprintf_ftype fprintf_func = dinfo->fprintf_func;
507 dinfo->read_memory_func = &read_zero_data_only;
508 dinfo->fprintf_func = &print_to_dev_null;
509 (*dfn)(0, dinfo);
511 /* put it back */
512 dinfo->read_memory_func = read_memory_func;
513 dinfo->fprintf_func = fprintf_func;
514 }
516 static void init_disassemble_info_from_bfd(struct disassemble_info* dinfo,
517 void *stream,
518 fprintf_ftype fprintf_func,
519 bfd* abfd,
520 char* disassembler_options) {
521 init_disassemble_info(dinfo, stream, fprintf_func);
523 dinfo->flavour = bfd_get_flavour(abfd);
524 dinfo->arch = bfd_get_arch(abfd);
525 dinfo->mach = bfd_get_mach(abfd);
526 dinfo->disassembler_options = disassembler_options;
527 dinfo->octets_per_byte = bfd_octets_per_byte (abfd);
528 dinfo->skip_zeroes = sizeof(void*) * 2;
529 dinfo->skip_zeroes_at_end = sizeof(void*)-1;
530 dinfo->disassembler_needs_relocs = FALSE;
532 if (bfd_big_endian(abfd))
533 dinfo->display_endian = dinfo->endian = BFD_ENDIAN_BIG;
534 else if (bfd_little_endian(abfd))
535 dinfo->display_endian = dinfo->endian = BFD_ENDIAN_LITTLE;
536 else
537 dinfo->endian = native_endian();
539 disassemble_init_for_target(dinfo);
540 }