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
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@4244 | 88 | #define DECODE_INSTRUCTIONS_VIRTUAL_NAME "decode_instructions_virtual" |
minqi@4244 | 89 | #define DECODE_INSTRUCTIONS_NAME "decode_instructions" |
jrose@535 | 90 | #define HSDIS_NAME "hsdis" |
jrose@535 | 91 | static void* decode_instructions_pv = 0; |
minqi@4244 | 92 | static void* decode_instructions_sv = 0; |
jrose@535 | 93 | static const char* hsdis_path[] = { |
never@1155 | 94 | HSDIS_NAME"-"LIBARCH LIB_EXT, |
never@1155 | 95 | "./" HSDIS_NAME"-"LIBARCH LIB_EXT, |
never@1155 | 96 | #ifdef TARGET_DIR |
never@1155 | 97 | TARGET_DIR"/"HSDIS_NAME"-"LIBARCH LIB_EXT, |
jrose@535 | 98 | #endif |
jrose@535 | 99 | NULL |
jrose@535 | 100 | }; |
jrose@535 | 101 | |
jrose@535 | 102 | static const char* load_decode_instructions() { |
jrose@535 | 103 | void* dllib = NULL; |
jrose@535 | 104 | const char* *next_in_path = hsdis_path; |
jrose@535 | 105 | while (1) { |
minqi@4244 | 106 | decode_instructions_pv = dlsym(dllib, DECODE_INSTRUCTIONS_VIRTUAL_NAME); |
minqi@4244 | 107 | decode_instructions_sv = dlsym(dllib, DECODE_INSTRUCTIONS_NAME); |
minqi@4244 | 108 | if (decode_instructions_pv != NULL || decode_instructions_sv != NULL) |
jrose@535 | 109 | return NULL; |
jrose@535 | 110 | if (dllib != NULL) |
minqi@4244 | 111 | return "plugin does not defined "DECODE_INSTRUCTIONS_VIRTUAL_NAME" and "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) |
never@1155 | 115 | return "cannot find plugin "HSDIS_NAME LIB_EXT; |
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) { |
minqi@4093 | 123 | #if defined(__ia64) || defined(__powerpc__) |
minqi@4093 | 124 | /* On IA64 and PPC function pointers are pointers to function descriptors */ |
minqi@4093 | 125 | #define CHECK_NAME(fn) \ |
minqi@4093 | 126 | if (addr == *((void**) &fn)) return #fn; |
minqi@4093 | 127 | #else |
jrose@535 | 128 | #define CHECK_NAME(fn) \ |
jrose@535 | 129 | if (addr == (void*) &fn) return #fn; |
minqi@4093 | 130 | #endif |
jrose@535 | 131 | |
jrose@535 | 132 | CHECK_NAME(main); |
jrose@535 | 133 | CHECK_NAME(greet); |
jrose@535 | 134 | return NULL; |
jrose@535 | 135 | } |
jrose@535 | 136 | |
jrose@535 | 137 | /* does the event match the tag, followed by a null, space, or slash? */ |
jrose@535 | 138 | #define MATCH(event, tag) \ |
jrose@535 | 139 | (!strncmp(event, tag, sizeof(tag)-1) && \ |
jrose@535 | 140 | (!event[sizeof(tag)-1] || strchr(" /", event[sizeof(tag)-1]))) |
jrose@535 | 141 | |
jrose@535 | 142 | |
jrose@535 | 143 | static const char event_cookie[] = "event_cookie"; /* demo placeholder */ |
minqi@4093 | 144 | static void* simple_handle_event(void* cookie, const char* event, void* arg) { |
minqi@4093 | 145 | if (MATCH(event, "/insn")) { |
minqi@4093 | 146 | // follow each complete insn by a nice newline |
minqi@4093 | 147 | printf("\n"); |
minqi@4093 | 148 | } |
minqi@4093 | 149 | return NULL; |
minqi@4093 | 150 | } |
minqi@4093 | 151 | |
jrose@535 | 152 | static void* handle_event(void* cookie, const char* event, void* arg) { |
jrose@535 | 153 | #define NS_DEMO "demo:" |
jrose@535 | 154 | if (cookie != event_cookie) |
jrose@535 | 155 | printf("*** bad event cookie %p != %p\n", cookie, event_cookie); |
jrose@535 | 156 | |
jrose@535 | 157 | if (xml) { |
jrose@535 | 158 | /* We could almost do a printf(event, arg), |
jrose@535 | 159 | but for the sake of a better demo, |
jrose@535 | 160 | we dress the result up as valid XML. |
jrose@535 | 161 | */ |
jrose@535 | 162 | const char* fmt = strchr(event, ' '); |
jrose@535 | 163 | int evlen = (fmt ? fmt - event : strlen(event)); |
jrose@535 | 164 | if (!fmt) { |
jrose@535 | 165 | if (event[0] != '/') { |
jrose@535 | 166 | printf("<"NS_DEMO"%.*s>", evlen, event); |
jrose@535 | 167 | } else { |
jrose@535 | 168 | printf("</"NS_DEMO"%.*s>", evlen-1, event+1); |
jrose@535 | 169 | } |
jrose@535 | 170 | } else { |
jrose@535 | 171 | if (event[0] != '/') { |
jrose@535 | 172 | printf("<"NS_DEMO"%.*s", evlen, event); |
jrose@535 | 173 | printf(fmt, arg); |
jrose@535 | 174 | printf(">"); |
jrose@535 | 175 | } else { |
jrose@535 | 176 | printf("<"NS_DEMO"%.*s_done", evlen-1, event+1); |
jrose@535 | 177 | printf(fmt, arg); |
jrose@535 | 178 | printf("/></"NS_DEMO"%.*s>", evlen-1, event+1); |
jrose@535 | 179 | } |
jrose@535 | 180 | } |
jrose@535 | 181 | } |
jrose@535 | 182 | |
jrose@535 | 183 | if (MATCH(event, "insn")) { |
jrose@535 | 184 | const char* name = lookup(arg); |
jrose@535 | 185 | if (name) printf("%s:\n", name); |
jrose@535 | 186 | |
jrose@535 | 187 | /* basic action for <insn>: */ |
jrose@535 | 188 | printf(" %p\t", arg); |
jrose@535 | 189 | |
jrose@535 | 190 | } else if (MATCH(event, "/insn")) { |
minqi@4093 | 191 | // follow each complete insn by a nice newline |
minqi@4093 | 192 | printf("\n"); |
jrose@535 | 193 | } else if (MATCH(event, "mach")) { |
jrose@535 | 194 | printf("Decoding for CPU '%s'\n", (char*) arg); |
jrose@535 | 195 | |
jrose@535 | 196 | } else if (MATCH(event, "addr")) { |
jrose@535 | 197 | /* basic action for <addr/>: */ |
jrose@535 | 198 | const char* name = lookup(arg); |
jrose@535 | 199 | if (name) { |
jrose@535 | 200 | printf("&%s (%p)", name, arg); |
jrose@535 | 201 | /* return non-null to notify hsdis not to print the addr */ |
jrose@535 | 202 | return arg; |
jrose@535 | 203 | } |
jrose@535 | 204 | } |
jrose@535 | 205 | |
jrose@535 | 206 | /* null return is always safe; can mean "I ignored it" */ |
jrose@535 | 207 | return NULL; |
jrose@535 | 208 | } |
jrose@535 | 209 | |
jrose@535 | 210 | #define fprintf_callback \ |
jrose@535 | 211 | (decode_instructions_printf_callback_ftype)&fprintf |
jrose@535 | 212 | |
minqi@4093 | 213 | void disassemble(uintptr_t from, uintptr_t to) { |
jrose@535 | 214 | const char* err = load_decode_instructions(); |
jrose@535 | 215 | if (err != NULL) { |
jrose@535 | 216 | printf("%s: %s\n", err, dlerror()); |
jrose@535 | 217 | exit(1); |
jrose@535 | 218 | } |
minqi@4244 | 219 | decode_func_vtype decode_instructions_v |
minqi@4244 | 220 | = (decode_func_vtype) decode_instructions_pv; |
minqi@4244 | 221 | decode_func_stype decode_instructions_s |
minqi@4244 | 222 | = (decode_func_stype) decode_instructions_sv; |
jrose@535 | 223 | void* res; |
minqi@4244 | 224 | if (decode_instructions_pv != NULL) { |
minqi@4244 | 225 | printf("\nDecoding from %p to %p...with %s\n", from, to, DECODE_INSTRUCTIONS_VIRTUAL_NAME); |
minqi@4244 | 226 | if (raw) { |
minqi@4244 | 227 | res = (*decode_instructions_v)(from, to, |
minqi@4244 | 228 | (unsigned char*)from, to - from, |
minqi@4244 | 229 | simple_handle_event, stdout, |
minqi@4244 | 230 | NULL, stdout, |
minqi@4244 | 231 | options, 0); |
minqi@4244 | 232 | } else { |
minqi@4244 | 233 | res = (*decode_instructions_v)(from, to, |
minqi@4244 | 234 | (unsigned char*)from, to - from, |
minqi@4244 | 235 | handle_event, (void*) event_cookie, |
minqi@4244 | 236 | fprintf_callback, stdout, |
minqi@4244 | 237 | options, 0); |
minqi@4244 | 238 | } |
minqi@4244 | 239 | if (res != (void*)to) |
minqi@4244 | 240 | printf("*** Result was %p!\n", res); |
jrose@535 | 241 | } |
minqi@4244 | 242 | void* sres; |
minqi@4244 | 243 | if (decode_instructions_sv != NULL) { |
minqi@4244 | 244 | printf("\nDecoding from %p to %p...with old decode_instructions\n", from, to, DECODE_INSTRUCTIONS_NAME); |
minqi@4244 | 245 | if (raw) { |
minqi@4244 | 246 | sres = (*decode_instructions_s)(from, to, |
minqi@4244 | 247 | simple_handle_event, stdout, |
minqi@4244 | 248 | NULL, stdout, |
minqi@4244 | 249 | options); |
minqi@4244 | 250 | } else { |
minqi@4244 | 251 | sres = (*decode_instructions_s)(from, to, |
minqi@4244 | 252 | handle_event, (void*) event_cookie, |
minqi@4244 | 253 | fprintf_callback, stdout, |
minqi@4244 | 254 | options); |
minqi@4244 | 255 | } |
minqi@4244 | 256 | if (sres != (void *)to) |
minqi@4244 | 257 | printf("*** Result of decode_instructions %p!\n", sres); |
minqi@4244 | 258 | } |
jrose@535 | 259 | } |