Wed, 02 Apr 2008 12:09:59 -0700
6667042: PrintAssembly option does not work without special plugin
Summary: remove old private plugin interface, simplify, rework old plugin to use unchanged Gnu sources
Reviewed-by: kvn, rasbold
jrose@535 | 1 | /* |
jrose@535 | 2 | * Copyright 2008 Sun Microsystems, Inc. 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 | * |
jrose@535 | 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
jrose@535 | 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
jrose@535 | 21 | * have any 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 | |
jrose@535 | 29 | #include "hsdis.h" |
jrose@535 | 30 | |
jrose@535 | 31 | #include "stdio.h" |
jrose@535 | 32 | #include "stdlib.h" |
jrose@535 | 33 | #include "string.h" |
jrose@535 | 34 | |
jrose@535 | 35 | void greet(const char*); |
jrose@535 | 36 | void disassemble(void*, void*); |
jrose@535 | 37 | void end_of_file(); |
jrose@535 | 38 | |
jrose@535 | 39 | const char* options = NULL; |
jrose@535 | 40 | int raw = 0; |
jrose@535 | 41 | int xml = 0; |
jrose@535 | 42 | |
jrose@535 | 43 | int main(int ac, char** av) { |
jrose@535 | 44 | int greeted = 0; |
jrose@535 | 45 | int i; |
jrose@535 | 46 | for (i = 1; i < ac; i++) { |
jrose@535 | 47 | const char* arg = av[i]; |
jrose@535 | 48 | if (arg[0] == '-') { |
jrose@535 | 49 | if (!strcmp(arg, "-xml")) |
jrose@535 | 50 | xml ^= 1; |
jrose@535 | 51 | else if (!strcmp(arg, "-raw")) |
jrose@535 | 52 | raw ^= 1; |
jrose@535 | 53 | else if (!strncmp(arg, "-options=", 9)) |
jrose@535 | 54 | options = arg+9; |
jrose@535 | 55 | else |
jrose@535 | 56 | { printf("Usage: %s [-xml] [name...]\n"); exit(2); } |
jrose@535 | 57 | continue; |
jrose@535 | 58 | } |
jrose@535 | 59 | greet(arg); |
jrose@535 | 60 | greeted = 1; |
jrose@535 | 61 | } |
jrose@535 | 62 | if (!greeted) |
jrose@535 | 63 | greet("world"); |
jrose@535 | 64 | printf("...And now for something completely different:\n"); |
jrose@535 | 65 | disassemble((void*) &main, (void*) &end_of_file); |
jrose@535 | 66 | printf("Cheers!\n"); |
jrose@535 | 67 | } |
jrose@535 | 68 | |
jrose@535 | 69 | void greet(const char* whom) { |
jrose@535 | 70 | printf("Hello, %s!\n", whom); |
jrose@535 | 71 | } |
jrose@535 | 72 | |
jrose@535 | 73 | void end_of_file() { } |
jrose@535 | 74 | |
jrose@535 | 75 | /* don't disassemble after this point... */ |
jrose@535 | 76 | |
jrose@535 | 77 | #include "dlfcn.h" |
jrose@535 | 78 | |
jrose@535 | 79 | #ifdef HOTSPOT_LIB_ARCH |
jrose@535 | 80 | #define LIBARCH HOTSPOT_LIB_ARCH |
jrose@535 | 81 | #endif |
jrose@535 | 82 | #ifdef HOTSPOT_OS |
jrose@535 | 83 | #define OS HOTSPOT_OS |
jrose@535 | 84 | #endif |
jrose@535 | 85 | |
jrose@535 | 86 | #define DECODE_INSTRUCTIONS_NAME "decode_instructions" |
jrose@535 | 87 | #define HSDIS_NAME "hsdis" |
jrose@535 | 88 | static void* decode_instructions_pv = 0; |
jrose@535 | 89 | static const char* hsdis_path[] = { |
jrose@535 | 90 | HSDIS_NAME".so", |
jrose@535 | 91 | #ifdef OS |
jrose@535 | 92 | "bin/"OS"/"HSDIS_NAME".so", |
jrose@535 | 93 | #endif |
jrose@535 | 94 | #ifdef LIBARCH |
jrose@535 | 95 | HSDIS_NAME"-"LIBARCH".so", |
jrose@535 | 96 | #ifdef OS |
jrose@535 | 97 | "bin/"OS"/"HSDIS_NAME"-"LIBARCH".so", |
jrose@535 | 98 | #endif |
jrose@535 | 99 | #endif |
jrose@535 | 100 | NULL |
jrose@535 | 101 | }; |
jrose@535 | 102 | |
jrose@535 | 103 | static const char* load_decode_instructions() { |
jrose@535 | 104 | void* dllib = NULL; |
jrose@535 | 105 | const char* *next_in_path = hsdis_path; |
jrose@535 | 106 | while (1) { |
jrose@535 | 107 | decode_instructions_pv = dlsym(dllib, DECODE_INSTRUCTIONS_NAME); |
jrose@535 | 108 | if (decode_instructions_pv != NULL) |
jrose@535 | 109 | return NULL; |
jrose@535 | 110 | if (dllib != NULL) |
jrose@535 | 111 | return "plugin does not defined "DECODE_INSTRUCTIONS_NAME; |
jrose@535 | 112 | for (dllib = NULL; dllib == NULL; ) { |
jrose@535 | 113 | const char* next_lib = (*next_in_path++); |
jrose@535 | 114 | if (next_lib == NULL) |
jrose@535 | 115 | return "cannot find plugin "HSDIS_NAME".so"; |
jrose@535 | 116 | dllib = dlopen(next_lib, RTLD_LAZY); |
jrose@535 | 117 | } |
jrose@535 | 118 | } |
jrose@535 | 119 | } |
jrose@535 | 120 | |
jrose@535 | 121 | |
jrose@535 | 122 | static const char* lookup(void* addr) { |
jrose@535 | 123 | #define CHECK_NAME(fn) \ |
jrose@535 | 124 | if (addr == (void*) &fn) return #fn; |
jrose@535 | 125 | |
jrose@535 | 126 | CHECK_NAME(main); |
jrose@535 | 127 | CHECK_NAME(greet); |
jrose@535 | 128 | return NULL; |
jrose@535 | 129 | } |
jrose@535 | 130 | |
jrose@535 | 131 | /* does the event match the tag, followed by a null, space, or slash? */ |
jrose@535 | 132 | #define MATCH(event, tag) \ |
jrose@535 | 133 | (!strncmp(event, tag, sizeof(tag)-1) && \ |
jrose@535 | 134 | (!event[sizeof(tag)-1] || strchr(" /", event[sizeof(tag)-1]))) |
jrose@535 | 135 | |
jrose@535 | 136 | |
jrose@535 | 137 | static const char event_cookie[] = "event_cookie"; /* demo placeholder */ |
jrose@535 | 138 | static void* handle_event(void* cookie, const char* event, void* arg) { |
jrose@535 | 139 | #define NS_DEMO "demo:" |
jrose@535 | 140 | if (cookie != event_cookie) |
jrose@535 | 141 | printf("*** bad event cookie %p != %p\n", cookie, event_cookie); |
jrose@535 | 142 | |
jrose@535 | 143 | if (xml) { |
jrose@535 | 144 | /* We could almost do a printf(event, arg), |
jrose@535 | 145 | but for the sake of a better demo, |
jrose@535 | 146 | we dress the result up as valid XML. |
jrose@535 | 147 | */ |
jrose@535 | 148 | const char* fmt = strchr(event, ' '); |
jrose@535 | 149 | int evlen = (fmt ? fmt - event : strlen(event)); |
jrose@535 | 150 | if (!fmt) { |
jrose@535 | 151 | if (event[0] != '/') { |
jrose@535 | 152 | printf("<"NS_DEMO"%.*s>", evlen, event); |
jrose@535 | 153 | } else { |
jrose@535 | 154 | printf("</"NS_DEMO"%.*s>", evlen-1, event+1); |
jrose@535 | 155 | } |
jrose@535 | 156 | } else { |
jrose@535 | 157 | if (event[0] != '/') { |
jrose@535 | 158 | printf("<"NS_DEMO"%.*s", evlen, event); |
jrose@535 | 159 | printf(fmt, arg); |
jrose@535 | 160 | printf(">"); |
jrose@535 | 161 | } else { |
jrose@535 | 162 | printf("<"NS_DEMO"%.*s_done", evlen-1, event+1); |
jrose@535 | 163 | printf(fmt, arg); |
jrose@535 | 164 | printf("/></"NS_DEMO"%.*s>", evlen-1, event+1); |
jrose@535 | 165 | } |
jrose@535 | 166 | } |
jrose@535 | 167 | } |
jrose@535 | 168 | |
jrose@535 | 169 | if (MATCH(event, "insn")) { |
jrose@535 | 170 | const char* name = lookup(arg); |
jrose@535 | 171 | if (name) printf("%s:\n", name); |
jrose@535 | 172 | |
jrose@535 | 173 | /* basic action for <insn>: */ |
jrose@535 | 174 | printf(" %p\t", arg); |
jrose@535 | 175 | |
jrose@535 | 176 | } else if (MATCH(event, "/insn")) { |
jrose@535 | 177 | /* basic action for </insn>: |
jrose@535 | 178 | (none, plugin puts the newline for us |
jrose@535 | 179 | */ |
jrose@535 | 180 | |
jrose@535 | 181 | } else if (MATCH(event, "mach")) { |
jrose@535 | 182 | printf("Decoding for CPU '%s'\n", (char*) arg); |
jrose@535 | 183 | |
jrose@535 | 184 | } else if (MATCH(event, "addr")) { |
jrose@535 | 185 | /* basic action for <addr/>: */ |
jrose@535 | 186 | const char* name = lookup(arg); |
jrose@535 | 187 | if (name) { |
jrose@535 | 188 | printf("&%s (%p)", name, arg); |
jrose@535 | 189 | /* return non-null to notify hsdis not to print the addr */ |
jrose@535 | 190 | return arg; |
jrose@535 | 191 | } |
jrose@535 | 192 | } |
jrose@535 | 193 | |
jrose@535 | 194 | /* null return is always safe; can mean "I ignored it" */ |
jrose@535 | 195 | return NULL; |
jrose@535 | 196 | } |
jrose@535 | 197 | |
jrose@535 | 198 | #define fprintf_callback \ |
jrose@535 | 199 | (decode_instructions_printf_callback_ftype)&fprintf |
jrose@535 | 200 | |
jrose@535 | 201 | void disassemble(void* from, void* to) { |
jrose@535 | 202 | const char* err = load_decode_instructions(); |
jrose@535 | 203 | if (err != NULL) { |
jrose@535 | 204 | printf("%s: %s\n", err, dlerror()); |
jrose@535 | 205 | exit(1); |
jrose@535 | 206 | } |
jrose@535 | 207 | printf("Decoding from %p to %p...\n", from, to); |
jrose@535 | 208 | decode_instructions_ftype decode_instructions |
jrose@535 | 209 | = (decode_instructions_ftype) decode_instructions_pv; |
jrose@535 | 210 | void* res; |
jrose@535 | 211 | if (raw && xml) { |
jrose@535 | 212 | res = (*decode_instructions)(from, to, NULL, stdout, NULL, stdout, options); |
jrose@535 | 213 | } else if (raw) { |
jrose@535 | 214 | res = (*decode_instructions)(from, to, NULL, NULL, NULL, stdout, options); |
jrose@535 | 215 | } else { |
jrose@535 | 216 | res = (*decode_instructions)(from, to, |
jrose@535 | 217 | handle_event, (void*) event_cookie, |
jrose@535 | 218 | fprintf_callback, stdout, |
jrose@535 | 219 | options); |
jrose@535 | 220 | } |
jrose@535 | 221 | if (res != to) |
jrose@535 | 222 | printf("*** Result was %p!\n", res); |
jrose@535 | 223 | } |