src/share/tools/hsdis/hsdis-demo.c

Mon, 24 Sep 2012 12:44:00 -0700

author
minqi
date
Mon, 24 Sep 2012 12:44:00 -0700
changeset 4093
5a98bf7d847b
parent 2708
1d1603768966
child 4244
3d701c802d01
permissions
-rw-r--r--

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

jrose@535 1 /*
minqi@4093 2 * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
jrose@535 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jrose@535 4 *
jrose@535 5 * This code is free software; you can redistribute it and/or modify it
jrose@535 6 * under the terms of the GNU General Public License version 2 only, as
jrose@535 7 * published by the Free Software Foundation.
jrose@535 8 *
jrose@535 9 * This code is distributed in the hope that it will be useful, but WITHOUT
jrose@535 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jrose@535 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jrose@535 12 * version 2 for more details (a copy is included in the LICENSE file that
jrose@535 13 * accompanied this code).
jrose@535 14 *
jrose@535 15 * You should have received a copy of the GNU General Public License version
jrose@535 16 * 2 along with this work; if not, write to the Free Software Foundation,
jrose@535 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jrose@535 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
jrose@535 22 *
jrose@535 23 */
jrose@535 24
jrose@535 25 /* hsdis-demo.c -- dump a range of addresses as native instructions
jrose@535 26 This demonstrates the protocol required by the HotSpot PrintAssembly option.
jrose@535 27 */
jrose@535 28
minqi@4093 29 #include <stdio.h>
minqi@4093 30 #include <stdlib.h>
minqi@4093 31 #include <string.h>
minqi@4093 32 #include <inttypes.h>
minqi@4093 33
jrose@535 34 #include "hsdis.h"
jrose@535 35
jrose@535 36
jrose@535 37 void greet(const char*);
minqi@4093 38 void disassemble(uintptr_t, uintptr_t);
jrose@535 39 void end_of_file();
jrose@535 40
jrose@535 41 const char* options = NULL;
jrose@535 42 int raw = 0;
jrose@535 43 int xml = 0;
jrose@535 44
jrose@535 45 int main(int ac, char** av) {
jrose@535 46 int greeted = 0;
jrose@535 47 int i;
jrose@535 48 for (i = 1; i < ac; i++) {
jrose@535 49 const char* arg = av[i];
jrose@535 50 if (arg[0] == '-') {
jrose@535 51 if (!strcmp(arg, "-xml"))
jrose@535 52 xml ^= 1;
jrose@535 53 else if (!strcmp(arg, "-raw"))
jrose@535 54 raw ^= 1;
jrose@535 55 else if (!strncmp(arg, "-options=", 9))
jrose@535 56 options = arg+9;
jrose@535 57 else
never@1155 58 { printf("Usage: %s [-xml] [name...]\n", av[0]); exit(2); }
jrose@535 59 continue;
jrose@535 60 }
jrose@535 61 greet(arg);
jrose@535 62 greeted = 1;
jrose@535 63 }
jrose@535 64 if (!greeted)
jrose@535 65 greet("world");
jrose@535 66 printf("...And now for something completely different:\n");
minqi@4093 67 void *start = (void*) &main;
minqi@4093 68 void *end = (void*) &end_of_file;
minqi@4093 69 #if defined(__ia64) || defined(__powerpc__)
minqi@4093 70 /* On IA64 and PPC function pointers are pointers to function descriptors */
minqi@4093 71 start = *((void**)start);
minqi@4093 72 end = *((void**)end);
minqi@4093 73 #endif
minqi@4093 74 disassemble(start, (end > start) ? end : start + 64);
jrose@535 75 printf("Cheers!\n");
jrose@535 76 }
jrose@535 77
jrose@535 78 void greet(const char* whom) {
jrose@535 79 printf("Hello, %s!\n", whom);
jrose@535 80 }
jrose@535 81
jrose@535 82 void end_of_file() { }
jrose@535 83
jrose@535 84 /* don't disassemble after this point... */
jrose@535 85
jrose@535 86 #include "dlfcn.h"
jrose@535 87
minqi@4093 88 #define DECODE_INSTRUCTIONS_NAME "decode_instructions_virtual"
jrose@535 89 #define HSDIS_NAME "hsdis"
jrose@535 90 static void* decode_instructions_pv = 0;
jrose@535 91 static const char* hsdis_path[] = {
never@1155 92 HSDIS_NAME"-"LIBARCH LIB_EXT,
never@1155 93 "./" HSDIS_NAME"-"LIBARCH LIB_EXT,
never@1155 94 #ifdef TARGET_DIR
never@1155 95 TARGET_DIR"/"HSDIS_NAME"-"LIBARCH LIB_EXT,
jrose@535 96 #endif
jrose@535 97 NULL
jrose@535 98 };
jrose@535 99
jrose@535 100 static const char* load_decode_instructions() {
jrose@535 101 void* dllib = NULL;
jrose@535 102 const char* *next_in_path = hsdis_path;
jrose@535 103 while (1) {
jrose@535 104 decode_instructions_pv = dlsym(dllib, DECODE_INSTRUCTIONS_NAME);
jrose@535 105 if (decode_instructions_pv != NULL)
jrose@535 106 return NULL;
jrose@535 107 if (dllib != NULL)
jrose@535 108 return "plugin does not defined "DECODE_INSTRUCTIONS_NAME;
jrose@535 109 for (dllib = NULL; dllib == NULL; ) {
jrose@535 110 const char* next_lib = (*next_in_path++);
jrose@535 111 if (next_lib == NULL)
never@1155 112 return "cannot find plugin "HSDIS_NAME LIB_EXT;
jrose@535 113 dllib = dlopen(next_lib, RTLD_LAZY);
jrose@535 114 }
jrose@535 115 }
jrose@535 116 }
jrose@535 117
jrose@535 118
jrose@535 119 static const char* lookup(void* addr) {
minqi@4093 120 #if defined(__ia64) || defined(__powerpc__)
minqi@4093 121 /* On IA64 and PPC function pointers are pointers to function descriptors */
minqi@4093 122 #define CHECK_NAME(fn) \
minqi@4093 123 if (addr == *((void**) &fn)) return #fn;
minqi@4093 124 #else
jrose@535 125 #define CHECK_NAME(fn) \
jrose@535 126 if (addr == (void*) &fn) return #fn;
minqi@4093 127 #endif
jrose@535 128
jrose@535 129 CHECK_NAME(main);
jrose@535 130 CHECK_NAME(greet);
jrose@535 131 return NULL;
jrose@535 132 }
jrose@535 133
jrose@535 134 /* does the event match the tag, followed by a null, space, or slash? */
jrose@535 135 #define MATCH(event, tag) \
jrose@535 136 (!strncmp(event, tag, sizeof(tag)-1) && \
jrose@535 137 (!event[sizeof(tag)-1] || strchr(" /", event[sizeof(tag)-1])))
jrose@535 138
jrose@535 139
jrose@535 140 static const char event_cookie[] = "event_cookie"; /* demo placeholder */
minqi@4093 141 static void* simple_handle_event(void* cookie, const char* event, void* arg) {
minqi@4093 142 if (MATCH(event, "/insn")) {
minqi@4093 143 // follow each complete insn by a nice newline
minqi@4093 144 printf("\n");
minqi@4093 145 }
minqi@4093 146 return NULL;
minqi@4093 147 }
minqi@4093 148
jrose@535 149 static void* handle_event(void* cookie, const char* event, void* arg) {
jrose@535 150 #define NS_DEMO "demo:"
jrose@535 151 if (cookie != event_cookie)
jrose@535 152 printf("*** bad event cookie %p != %p\n", cookie, event_cookie);
jrose@535 153
jrose@535 154 if (xml) {
jrose@535 155 /* We could almost do a printf(event, arg),
jrose@535 156 but for the sake of a better demo,
jrose@535 157 we dress the result up as valid XML.
jrose@535 158 */
jrose@535 159 const char* fmt = strchr(event, ' ');
jrose@535 160 int evlen = (fmt ? fmt - event : strlen(event));
jrose@535 161 if (!fmt) {
jrose@535 162 if (event[0] != '/') {
jrose@535 163 printf("<"NS_DEMO"%.*s>", evlen, event);
jrose@535 164 } else {
jrose@535 165 printf("</"NS_DEMO"%.*s>", evlen-1, event+1);
jrose@535 166 }
jrose@535 167 } else {
jrose@535 168 if (event[0] != '/') {
jrose@535 169 printf("<"NS_DEMO"%.*s", evlen, event);
jrose@535 170 printf(fmt, arg);
jrose@535 171 printf(">");
jrose@535 172 } else {
jrose@535 173 printf("<"NS_DEMO"%.*s_done", evlen-1, event+1);
jrose@535 174 printf(fmt, arg);
jrose@535 175 printf("/></"NS_DEMO"%.*s>", evlen-1, event+1);
jrose@535 176 }
jrose@535 177 }
jrose@535 178 }
jrose@535 179
jrose@535 180 if (MATCH(event, "insn")) {
jrose@535 181 const char* name = lookup(arg);
jrose@535 182 if (name) printf("%s:\n", name);
jrose@535 183
jrose@535 184 /* basic action for <insn>: */
jrose@535 185 printf(" %p\t", arg);
jrose@535 186
jrose@535 187 } else if (MATCH(event, "/insn")) {
minqi@4093 188 // follow each complete insn by a nice newline
minqi@4093 189 printf("\n");
jrose@535 190 } else if (MATCH(event, "mach")) {
jrose@535 191 printf("Decoding for CPU '%s'\n", (char*) arg);
jrose@535 192
jrose@535 193 } else if (MATCH(event, "addr")) {
jrose@535 194 /* basic action for <addr/>: */
jrose@535 195 const char* name = lookup(arg);
jrose@535 196 if (name) {
jrose@535 197 printf("&%s (%p)", name, arg);
jrose@535 198 /* return non-null to notify hsdis not to print the addr */
jrose@535 199 return arg;
jrose@535 200 }
jrose@535 201 }
jrose@535 202
jrose@535 203 /* null return is always safe; can mean "I ignored it" */
jrose@535 204 return NULL;
jrose@535 205 }
jrose@535 206
jrose@535 207 #define fprintf_callback \
jrose@535 208 (decode_instructions_printf_callback_ftype)&fprintf
jrose@535 209
minqi@4093 210 void disassemble(uintptr_t from, uintptr_t to) {
jrose@535 211 const char* err = load_decode_instructions();
jrose@535 212 if (err != NULL) {
jrose@535 213 printf("%s: %s\n", err, dlerror());
jrose@535 214 exit(1);
jrose@535 215 }
jrose@535 216 printf("Decoding from %p to %p...\n", from, to);
jrose@535 217 decode_instructions_ftype decode_instructions
jrose@535 218 = (decode_instructions_ftype) decode_instructions_pv;
jrose@535 219 void* res;
jrose@535 220 if (raw && xml) {
minqi@4093 221 res = (*decode_instructions)(from, to, (unsigned char*)from, to - from, simple_handle_event, stdout, NULL, stdout, options);
jrose@535 222 } else if (raw) {
minqi@4093 223 res = (*decode_instructions)(from, to, (unsigned char*)from, to - from, simple_handle_event, stdout, NULL, stdout, options);
jrose@535 224 } else {
minqi@4093 225 res = (*decode_instructions)(from, to, (unsigned char*)from, to - from,
jrose@535 226 handle_event, (void*) event_cookie,
jrose@535 227 fprintf_callback, stdout,
jrose@535 228 options);
jrose@535 229 }
minqi@4093 230 if (res != (void*)to)
jrose@535 231 printf("*** Result was %p!\n", res);
jrose@535 232 }

mercurial