Wed, 01 Aug 2018 04:15:43 -0400
8208541: non-ASCII characters in hsdis UPL text
Summary: replace Unicode left/right double quote pair with normal ASCII double quotes
Reviewed-by: jrose
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 * The Universal Permissive License (UPL), Version 1.0
6 *
7 * Subject to the condition set forth below, permission is hereby granted to
8 * any person obtaining a copy of this software, associated documentation
9 * and/or data (collectively the "Software"), free of charge and under any
10 * and all copyright rights in the Software, and any and all patent rights
11 * owned or freely licensable by each licensor hereunder covering either (i)
12 * the unmodified Software as contributed to or provided by such licensor,
13 * or (ii) the Larger Works (as defined below), to deal in both
14 *
15 * (a) the Software, and
16 *
17 * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file
18 * if one is included with the Software (each a "Larger Work" to which the
19 * Software is contributed by such licensors),
20 *
21 * without restriction, including without limitation the rights to copy,
22 * create derivative works of, display, perform, and distribute the Software
23 * and make, use, sell, offer for sale, import, export, have made, and have
24 * sold the Software and the Larger Work(s), and to sublicense the foregoing
25 * rights on either these or other terms.
26 *
27 * This license is subject to the following condition:
28 *
29 * The above copyright notice and either this complete permission notice or
30 * at a minimum a reference to the UPL must be included in all copies or
31 * substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
34 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
35 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
36 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
37 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
38 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
39 * USE OR OTHER DEALINGS IN THE SOFTWARE.
40 *
41 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
42 * or visit www.oracle.com if you need additional information or have any
43 * questions.
44 *
45 */
47 /* hsdis.c -- dump a range of addresses as native instructions
48 This implements the plugin protocol required by the
49 HotSpot PrintAssembly option.
50 */
52 #include <config.h> /* required by bfd.h */
53 #include <libiberty.h>
54 #include <bfd.h>
55 #include <dis-asm.h>
56 #include <inttypes.h>
57 #include <string.h>
58 #include <errno.h>
59 #include "hsdis.h"
61 #ifndef bool
62 #define bool int
63 #define true 1
64 #define false 0
65 #endif /*bool*/
67 /* short names for stuff in hsdis.h */
68 typedef decode_instructions_event_callback_ftype event_callback_t;
69 typedef decode_instructions_printf_callback_ftype printf_callback_t;
71 /* disassemble_info.application_data object */
72 struct hsdis_app_data {
73 /* virtual address of data */
74 uintptr_t start_va, end_va;
75 /* the instructions to be decoded */
76 unsigned char* buffer;
77 uintptr_t length;
78 event_callback_t event_callback; void* event_stream;
79 printf_callback_t printf_callback; void* printf_stream;
80 bool losing;
81 bool do_newline;
83 /* the architecture being disassembled */
84 const char* arch_name;
85 const bfd_arch_info_type* arch_info;
87 /* the disassembler we are going to use: */
88 disassembler_ftype dfn;
89 struct disassemble_info dinfo; /* the actual struct! */
91 char mach_option[64];
92 char insn_options[256];
93 };
95 static void* decode(struct hsdis_app_data* app_data, const char* options);
97 #define DECL_APP_DATA(dinfo) \
98 struct hsdis_app_data* app_data = (struct hsdis_app_data*) (dinfo)->application_data
100 #define DECL_EVENT_CALLBACK(app_data) \
101 event_callback_t event_callback = (app_data)->event_callback; \
102 void* event_stream = (app_data)->event_stream
104 #define DECL_PRINTF_CALLBACK(app_data) \
105 printf_callback_t printf_callback = (app_data)->printf_callback; \
106 void* printf_stream = (app_data)->printf_stream
109 static void print_help(struct hsdis_app_data* app_data,
110 const char* msg, const char* arg);
111 static void setup_app_data(struct hsdis_app_data* app_data,
112 const char* options);
113 static const char* format_insn_close(const char* close,
114 disassemble_info* dinfo,
115 char* buf, size_t bufsize);
117 void*
118 #ifdef DLL_ENTRY
119 DLL_ENTRY
120 #endif
121 decode_instructions_virtual(uintptr_t start_va, uintptr_t end_va,
122 unsigned char* buffer, uintptr_t length,
123 event_callback_t event_callback_arg, void* event_stream_arg,
124 printf_callback_t printf_callback_arg, void* printf_stream_arg,
125 const char* options, int newline) {
126 struct hsdis_app_data app_data;
127 memset(&app_data, 0, sizeof(app_data));
128 app_data.start_va = start_va;
129 app_data.end_va = end_va;
130 app_data.buffer = buffer;
131 app_data.length = length;
132 app_data.event_callback = event_callback_arg;
133 app_data.event_stream = event_stream_arg;
134 app_data.printf_callback = printf_callback_arg;
135 app_data.printf_stream = printf_stream_arg;
136 app_data.do_newline = newline == 0 ? false : true;
138 return decode(&app_data, options);
139 }
141 /* This is the compatability interface for older version of hotspot */
142 void*
143 #ifdef DLL_ENTRY
144 DLL_ENTRY
145 #endif
146 decode_instructions(void* start_pv, void* end_pv,
147 event_callback_t event_callback_arg, void* event_stream_arg,
148 printf_callback_t printf_callback_arg, void* printf_stream_arg,
149 const char* options) {
150 decode_instructions_virtual((uintptr_t)start_pv,
151 (uintptr_t)end_pv,
152 (unsigned char*)start_pv,
153 (uintptr_t)end_pv - (uintptr_t)start_pv,
154 event_callback_arg,
155 event_stream_arg,
156 printf_callback_arg,
157 printf_stream_arg,
158 options, false);
159 }
161 static void* decode(struct hsdis_app_data* app_data, const char* options) {
162 setup_app_data(app_data, options);
163 char buf[128];
165 {
166 /* now reload everything from app_data: */
167 DECL_EVENT_CALLBACK(app_data);
168 DECL_PRINTF_CALLBACK(app_data);
169 uintptr_t start = app_data->start_va;
170 uintptr_t end = app_data->end_va;
171 uintptr_t p = start;
173 (*event_callback)(event_stream, "insns", (void*)start);
175 (*event_callback)(event_stream, "mach name='%s'",
176 (void*) app_data->arch_info->printable_name);
177 if (app_data->dinfo.bytes_per_line != 0) {
178 (*event_callback)(event_stream, "format bytes-per-line='%p'/",
179 (void*)(intptr_t) app_data->dinfo.bytes_per_line);
180 }
182 while (p < end && !app_data->losing) {
183 (*event_callback)(event_stream, "insn", (void*) p);
185 /* reset certain state, so we can read it with confidence */
186 app_data->dinfo.insn_info_valid = 0;
187 app_data->dinfo.branch_delay_insns = 0;
188 app_data->dinfo.data_size = 0;
189 app_data->dinfo.insn_type = 0;
191 int size = (*app_data->dfn)((bfd_vma) p, &app_data->dinfo);
193 if (size > 0) p += size;
194 else app_data->losing = true;
196 if (!app_data->losing) {
197 const char* insn_close = format_insn_close("/insn", &app_data->dinfo,
198 buf, sizeof(buf));
199 (*event_callback)(event_stream, insn_close, (void*) p);
201 if (app_data->do_newline) {
202 /* follow each complete insn by a nice newline */
203 (*printf_callback)(printf_stream, "\n");
204 }
205 }
206 }
208 if (app_data->losing) (*event_callback)(event_stream, "/insns", (void*) p);
209 return (void*) p;
210 }
211 }
213 /* take the address of the function, for luck, and also test the typedef: */
214 const decode_func_vtype decode_func_virtual_address = &decode_instructions_virtual;
215 const decode_func_stype decode_func_address = &decode_instructions;
217 static const char* format_insn_close(const char* close,
218 disassemble_info* dinfo,
219 char* buf, size_t bufsize) {
220 if (!dinfo->insn_info_valid)
221 return close;
222 enum dis_insn_type itype = dinfo->insn_type;
223 int dsize = dinfo->data_size, delays = dinfo->branch_delay_insns;
224 if ((itype == dis_nonbranch && (dsize | delays) == 0)
225 || (strlen(close) + 3*20 > bufsize))
226 return close;
228 const char* type = "unknown";
229 switch (itype) {
230 case dis_nonbranch: type = NULL; break;
231 case dis_branch: type = "branch"; break;
232 case dis_condbranch: type = "condbranch"; break;
233 case dis_jsr: type = "jsr"; break;
234 case dis_condjsr: type = "condjsr"; break;
235 case dis_dref: type = "dref"; break;
236 case dis_dref2: type = "dref2"; break;
237 }
239 strcpy(buf, close);
240 char* p = buf;
241 if (type) sprintf(p += strlen(p), " type='%s'", type);
242 if (dsize) sprintf(p += strlen(p), " dsize='%d'", dsize);
243 if (delays) sprintf(p += strlen(p), " delay='%d'", delays);
244 return buf;
245 }
247 /* handler functions */
249 static int
250 hsdis_read_memory_func(bfd_vma memaddr,
251 bfd_byte* myaddr,
252 unsigned int length,
253 struct disassemble_info* dinfo) {
254 DECL_APP_DATA(dinfo);
255 /* convert the virtual address memaddr into an address within memory buffer */
256 uintptr_t offset = ((uintptr_t) memaddr) - app_data->start_va;
257 if (offset + length > app_data->length) {
258 /* read is out of bounds */
259 return EIO;
260 } else {
261 memcpy(myaddr, (bfd_byte*) (app_data->buffer + offset), length);
262 return 0;
263 }
264 }
266 static void
267 hsdis_print_address_func(bfd_vma vma, struct disassemble_info* dinfo) {
268 /* the actual value to print: */
269 void* addr_value = (void*) (uintptr_t) vma;
270 DECL_APP_DATA(dinfo);
271 DECL_EVENT_CALLBACK(app_data);
273 /* issue the event: */
274 void* result =
275 (*event_callback)(event_stream, "addr/", addr_value);
276 if (result == NULL) {
277 /* event declined */
278 generic_print_address(vma, dinfo);
279 }
280 }
283 /* configuration */
285 static void set_optional_callbacks(struct hsdis_app_data* app_data);
286 static void parse_caller_options(struct hsdis_app_data* app_data,
287 const char* caller_options);
288 static const char* native_arch_name();
289 static enum bfd_endian native_endian();
290 static const bfd_arch_info_type* find_arch_info(const char* arch_nane);
291 static bfd* get_native_bfd(const bfd_arch_info_type* arch_info,
292 /* to avoid malloc: */
293 bfd* empty_bfd, bfd_target* empty_xvec);
294 static void init_disassemble_info_from_bfd(struct disassemble_info* dinfo,
295 void *stream,
296 fprintf_ftype fprintf_func,
297 bfd* bfd,
298 char* disassembler_options);
299 static void parse_fake_insn(disassembler_ftype dfn,
300 struct disassemble_info* dinfo);
302 static void setup_app_data(struct hsdis_app_data* app_data,
303 const char* caller_options) {
304 /* Make reasonable defaults for null callbacks.
305 A non-null stream for a null callback is assumed to be a FILE* for output.
306 Events are rendered as XML.
307 */
308 set_optional_callbacks(app_data);
310 /* Look into caller_options for anything interesting. */
311 if (caller_options != NULL)
312 parse_caller_options(app_data, caller_options);
314 /* Discover which architecture we are going to disassemble. */
315 app_data->arch_name = &app_data->mach_option[0];
316 if (app_data->arch_name[0] == '\0')
317 app_data->arch_name = native_arch_name();
318 app_data->arch_info = find_arch_info(app_data->arch_name);
320 /* Make a fake bfd to hold the arch. and byteorder info. */
321 struct {
322 bfd_target empty_xvec;
323 bfd empty_bfd;
324 } buf;
325 bfd* native_bfd = get_native_bfd(app_data->arch_info,
326 /* to avoid malloc: */
327 &buf.empty_bfd, &buf.empty_xvec);
328 init_disassemble_info_from_bfd(&app_data->dinfo,
329 app_data->printf_stream,
330 app_data->printf_callback,
331 native_bfd,
332 /* On PowerPC we get warnings, if we pass empty options */
333 (caller_options == NULL) ? NULL : app_data->insn_options);
335 /* Finish linking together the various callback blocks. */
336 app_data->dinfo.application_data = (void*) app_data;
337 app_data->dfn = disassembler(native_bfd);
338 app_data->dinfo.print_address_func = hsdis_print_address_func;
339 app_data->dinfo.read_memory_func = hsdis_read_memory_func;
341 if (app_data->dfn == NULL) {
342 const char* bad = app_data->arch_name;
343 static bool complained;
344 if (bad == &app_data->mach_option[0])
345 print_help(app_data, "bad mach=%s", bad);
346 else if (!complained)
347 print_help(app_data, "bad native mach=%s; please port hsdis to this platform", bad);
348 complained = true;
349 /* must bail out */
350 app_data->losing = true;
351 return;
352 }
354 parse_fake_insn(app_data->dfn, &app_data->dinfo);
355 }
358 /* ignore all events, return a null */
359 static void* null_event_callback(void* ignore_stream, const char* ignore_event, void* arg) {
360 return NULL;
361 }
363 /* print all events as XML markup */
364 static void* xml_event_callback(void* stream, const char* event, void* arg) {
365 FILE* fp = (FILE*) stream;
366 #define NS_PFX "dis:"
367 if (event[0] != '/') {
368 /* issue the tag, with or without a formatted argument */
369 fprintf(fp, "<"NS_PFX);
370 fprintf(fp, event, arg);
371 fprintf(fp, ">");
372 } else {
373 ++event; /* skip slash */
374 const char* argp = strchr(event, ' ');
375 if (argp == NULL) {
376 /* no arguments; just issue the closing tag */
377 fprintf(fp, "</"NS_PFX"%s>", event);
378 } else {
379 /* split out the closing attributes as <dis:foo_done attr='val'/> */
380 int event_prefix = (argp - event);
381 fprintf(fp, "<"NS_PFX"%.*s_done", event_prefix, event);
382 fprintf(fp, argp, arg);
383 fprintf(fp, "/></"NS_PFX"%.*s>", event_prefix, event);
384 }
385 }
386 return NULL;
387 }
389 static void set_optional_callbacks(struct hsdis_app_data* app_data) {
390 if (app_data->printf_callback == NULL) {
391 int (*fprintf_callback)(FILE*, const char*, ...) = &fprintf;
392 FILE* fprintf_stream = stdout;
393 app_data->printf_callback = (printf_callback_t) fprintf_callback;
394 if (app_data->printf_stream == NULL)
395 app_data->printf_stream = (void*) fprintf_stream;
396 }
397 if (app_data->event_callback == NULL) {
398 if (app_data->event_stream == NULL)
399 app_data->event_callback = &null_event_callback;
400 else
401 app_data->event_callback = &xml_event_callback;
402 }
404 }
406 static void parse_caller_options(struct hsdis_app_data* app_data, const char* caller_options) {
407 char* iop_base = app_data->insn_options;
408 char* iop_limit = iop_base + sizeof(app_data->insn_options) - 1;
409 char* iop = iop_base;
410 const char* p;
411 for (p = caller_options; p != NULL; ) {
412 const char* q = strchr(p, ',');
413 size_t plen = (q == NULL) ? strlen(p) : ((q++) - p);
414 if (plen == 4 && strncmp(p, "help", plen) == 0) {
415 print_help(app_data, NULL, NULL);
416 } else if (plen >= 5 && strncmp(p, "mach=", 5) == 0) {
417 char* mach_option = app_data->mach_option;
418 size_t mach_size = sizeof(app_data->mach_option);
419 mach_size -= 1; /*leave room for the null*/
420 if (plen > mach_size) plen = mach_size;
421 strncpy(mach_option, p, plen);
422 mach_option[plen] = '\0';
423 } else if (plen > 6 && strncmp(p, "hsdis-", 6) == 0) {
424 // do not pass these to the next level
425 } else {
426 /* just copy it; {i386,sparc}-dis.c might like to see it */
427 if (iop > iop_base && iop < iop_limit) (*iop++) = ',';
428 if (iop + plen > iop_limit)
429 plen = iop_limit - iop;
430 strncpy(iop, p, plen);
431 iop += plen;
432 }
433 p = q;
434 }
435 }
437 static void print_help(struct hsdis_app_data* app_data,
438 const char* msg, const char* arg) {
439 DECL_PRINTF_CALLBACK(app_data);
440 if (msg != NULL) {
441 (*printf_callback)(printf_stream, "hsdis: ");
442 (*printf_callback)(printf_stream, msg, arg);
443 (*printf_callback)(printf_stream, "\n");
444 }
445 (*printf_callback)(printf_stream, "hsdis output options:\n");
446 if (printf_callback == (printf_callback_t) &fprintf)
447 disassembler_usage((FILE*) printf_stream);
448 else
449 disassembler_usage(stderr); /* better than nothing */
450 (*printf_callback)(printf_stream, " mach=<arch> select disassembly mode\n");
451 #if defined(LIBARCH_i386) || defined(LIBARCH_amd64)
452 (*printf_callback)(printf_stream, " mach=i386 select 32-bit mode\n");
453 (*printf_callback)(printf_stream, " mach=x86-64 select 64-bit mode\n");
454 (*printf_callback)(printf_stream, " suffix always print instruction suffix\n");
455 #endif
456 (*printf_callback)(printf_stream, " help print this message\n");
457 }
460 /* low-level bfd and arch stuff that binutils doesn't do for us */
462 static const bfd_arch_info_type* find_arch_info(const char* arch_name) {
463 const bfd_arch_info_type* arch_info = bfd_scan_arch(arch_name);
464 if (arch_info == NULL) {
465 extern const bfd_arch_info_type bfd_default_arch_struct;
466 arch_info = &bfd_default_arch_struct;
467 }
468 return arch_info;
469 }
471 static const char* native_arch_name() {
472 const char* res = NULL;
473 #ifdef LIBARCH_i386
474 res = "i386";
475 #endif
476 #ifdef LIBARCH_amd64
477 res = "i386:x86-64";
478 #endif
479 #ifdef LIBARCH_sparc
480 res = "sparc:v8plusb";
481 #endif
482 #ifdef LIBARCH_sparcv9
483 res = "sparc:v9b";
484 #endif
485 #ifdef LIBARCH_ppc64
486 res = "powerpc:common64";
487 #endif
488 if (res == NULL)
489 res = "architecture not set in Makefile!";
490 return res;
491 }
493 static enum bfd_endian native_endian() {
494 int32_t endian_test = 'x';
495 if (*(const char*) &endian_test == 'x')
496 return BFD_ENDIAN_LITTLE;
497 else
498 return BFD_ENDIAN_BIG;
499 }
501 static bfd* get_native_bfd(const bfd_arch_info_type* arch_info,
502 bfd* empty_bfd, bfd_target* empty_xvec) {
503 memset(empty_bfd, 0, sizeof(*empty_bfd));
504 memset(empty_xvec, 0, sizeof(*empty_xvec));
505 empty_xvec->flavour = bfd_target_unknown_flavour;
506 empty_xvec->byteorder = native_endian();
507 empty_bfd->xvec = empty_xvec;
508 empty_bfd->arch_info = arch_info;
509 return empty_bfd;
510 }
512 static int read_zero_data_only(bfd_vma ignore_p,
513 bfd_byte* myaddr, unsigned int length,
514 struct disassemble_info *ignore_info) {
515 memset(myaddr, 0, length);
516 return 0;
517 }
518 static int print_to_dev_null(void* ignore_stream, const char* ignore_format, ...) {
519 return 0;
520 }
522 /* Prime the pump by running the selected disassembler on a null input.
523 This forces the machine-specific disassembler to divulge invariant
524 information like bytes_per_line.
525 */
526 static void parse_fake_insn(disassembler_ftype dfn,
527 struct disassemble_info* dinfo) {
528 typedef int (*read_memory_ftype)
529 (bfd_vma memaddr, bfd_byte *myaddr, unsigned int length,
530 struct disassemble_info *info);
531 read_memory_ftype read_memory_func = dinfo->read_memory_func;
532 fprintf_ftype fprintf_func = dinfo->fprintf_func;
534 dinfo->read_memory_func = &read_zero_data_only;
535 dinfo->fprintf_func = &print_to_dev_null;
536 (*dfn)(0, dinfo);
538 /* put it back */
539 dinfo->read_memory_func = read_memory_func;
540 dinfo->fprintf_func = fprintf_func;
541 }
543 static void init_disassemble_info_from_bfd(struct disassemble_info* dinfo,
544 void *stream,
545 fprintf_ftype fprintf_func,
546 bfd* abfd,
547 char* disassembler_options) {
548 init_disassemble_info(dinfo, stream, fprintf_func);
550 dinfo->flavour = bfd_get_flavour(abfd);
551 dinfo->arch = bfd_get_arch(abfd);
552 dinfo->mach = bfd_get_mach(abfd);
553 dinfo->disassembler_options = disassembler_options;
554 dinfo->octets_per_byte = bfd_octets_per_byte (abfd);
555 dinfo->skip_zeroes = sizeof(void*) * 2;
556 dinfo->skip_zeroes_at_end = sizeof(void*)-1;
557 dinfo->disassembler_needs_relocs = FALSE;
559 if (bfd_big_endian(abfd))
560 dinfo->display_endian = dinfo->endian = BFD_ENDIAN_BIG;
561 else if (bfd_little_endian(abfd))
562 dinfo->display_endian = dinfo->endian = BFD_ENDIAN_LITTLE;
563 else
564 dinfo->endian = native_endian();
566 disassemble_init_for_target(dinfo);
567 }