Thu, 26 Jul 2018 17:38:44 -0400
8208183: update HSDIS plugin license to UPL
Reviewed-by: simonis, adinn, jrose
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 | * |
dbuck@9473 | 5 | * The Universal Permissive License (UPL), Version 1.0 |
jrose@535 | 6 | * |
dbuck@9473 | 7 | * Subject to the condition set forth below, permission is hereby granted to |
dbuck@9473 | 8 | * any person obtaining a copy of this software, associated documentation |
dbuck@9473 | 9 | * and/or data (collectively the "Software"), free of charge and under any |
dbuck@9473 | 10 | * and all copyright rights in the Software, and any and all patent rights |
dbuck@9473 | 11 | * owned or freely licensable by each licensor hereunder covering either (i) |
dbuck@9473 | 12 | * the unmodified Software as contributed to or provided by such licensor, |
dbuck@9473 | 13 | * or (ii) the Larger Works (as defined below), to deal in both |
jrose@535 | 14 | * |
dbuck@9473 | 15 | * (a) the Software, and |
dbuck@9473 | 16 | * |
dbuck@9473 | 17 | * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file |
dbuck@9473 | 18 | * if one is included with the Software (each a “Larger Work” to which the |
dbuck@9473 | 19 | * Software is contributed by such licensors), |
dbuck@9473 | 20 | * |
dbuck@9473 | 21 | * without restriction, including without limitation the rights to copy, |
dbuck@9473 | 22 | * create derivative works of, display, perform, and distribute the Software |
dbuck@9473 | 23 | * and make, use, sell, offer for sale, import, export, have made, and have |
dbuck@9473 | 24 | * sold the Software and the Larger Work(s), and to sublicense the foregoing |
dbuck@9473 | 25 | * rights on either these or other terms. |
dbuck@9473 | 26 | * |
dbuck@9473 | 27 | * This license is subject to the following condition: |
dbuck@9473 | 28 | * |
dbuck@9473 | 29 | * The above copyright notice and either this complete permission notice or |
dbuck@9473 | 30 | * at a minimum a reference to the UPL must be included in all copies or |
dbuck@9473 | 31 | * substantial portions of the Software. |
dbuck@9473 | 32 | * |
dbuck@9473 | 33 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
dbuck@9473 | 34 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
dbuck@9473 | 35 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
dbuck@9473 | 36 | * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
dbuck@9473 | 37 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
dbuck@9473 | 38 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
dbuck@9473 | 39 | * USE OR OTHER DEALINGS IN THE SOFTWARE. |
jrose@535 | 40 | * |
trims@1907 | 41 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
trims@1907 | 42 | * or visit www.oracle.com if you need additional information or have any |
trims@1907 | 43 | * questions. |
jrose@535 | 44 | * |
jrose@535 | 45 | */ |
jrose@535 | 46 | |
jrose@535 | 47 | /* hsdis-demo.c -- dump a range of addresses as native instructions |
jrose@535 | 48 | This demonstrates the protocol required by the HotSpot PrintAssembly option. |
jrose@535 | 49 | */ |
jrose@535 | 50 | |
minqi@4093 | 51 | #include <stdio.h> |
minqi@4093 | 52 | #include <stdlib.h> |
minqi@4093 | 53 | #include <string.h> |
minqi@4093 | 54 | #include <inttypes.h> |
minqi@4093 | 55 | |
jrose@535 | 56 | #include "hsdis.h" |
jrose@535 | 57 | |
jrose@535 | 58 | |
jrose@535 | 59 | void greet(const char*); |
minqi@4093 | 60 | void disassemble(uintptr_t, uintptr_t); |
jrose@535 | 61 | void end_of_file(); |
jrose@535 | 62 | |
jrose@535 | 63 | const char* options = NULL; |
jrose@535 | 64 | int raw = 0; |
jrose@535 | 65 | int xml = 0; |
jrose@535 | 66 | |
jrose@535 | 67 | int main(int ac, char** av) { |
jrose@535 | 68 | int greeted = 0; |
jrose@535 | 69 | int i; |
jrose@535 | 70 | for (i = 1; i < ac; i++) { |
jrose@535 | 71 | const char* arg = av[i]; |
jrose@535 | 72 | if (arg[0] == '-') { |
jrose@535 | 73 | if (!strcmp(arg, "-xml")) |
jrose@535 | 74 | xml ^= 1; |
jrose@535 | 75 | else if (!strcmp(arg, "-raw")) |
jrose@535 | 76 | raw ^= 1; |
jrose@535 | 77 | else if (!strncmp(arg, "-options=", 9)) |
jrose@535 | 78 | options = arg+9; |
jrose@535 | 79 | else |
never@1155 | 80 | { printf("Usage: %s [-xml] [name...]\n", av[0]); exit(2); } |
jrose@535 | 81 | continue; |
jrose@535 | 82 | } |
jrose@535 | 83 | greet(arg); |
jrose@535 | 84 | greeted = 1; |
jrose@535 | 85 | } |
jrose@535 | 86 | if (!greeted) |
jrose@535 | 87 | greet("world"); |
jrose@535 | 88 | printf("...And now for something completely different:\n"); |
minqi@4093 | 89 | void *start = (void*) &main; |
minqi@4093 | 90 | void *end = (void*) &end_of_file; |
minqi@4093 | 91 | #if defined(__ia64) || defined(__powerpc__) |
minqi@4093 | 92 | /* On IA64 and PPC function pointers are pointers to function descriptors */ |
minqi@4093 | 93 | start = *((void**)start); |
minqi@4093 | 94 | end = *((void**)end); |
minqi@4093 | 95 | #endif |
minqi@4093 | 96 | disassemble(start, (end > start) ? end : start + 64); |
jrose@535 | 97 | printf("Cheers!\n"); |
jrose@535 | 98 | } |
jrose@535 | 99 | |
jrose@535 | 100 | void greet(const char* whom) { |
jrose@535 | 101 | printf("Hello, %s!\n", whom); |
jrose@535 | 102 | } |
jrose@535 | 103 | |
jrose@535 | 104 | void end_of_file() { } |
jrose@535 | 105 | |
jrose@535 | 106 | /* don't disassemble after this point... */ |
jrose@535 | 107 | |
jrose@535 | 108 | #include "dlfcn.h" |
jrose@535 | 109 | |
minqi@4244 | 110 | #define DECODE_INSTRUCTIONS_VIRTUAL_NAME "decode_instructions_virtual" |
minqi@4244 | 111 | #define DECODE_INSTRUCTIONS_NAME "decode_instructions" |
jrose@535 | 112 | #define HSDIS_NAME "hsdis" |
jrose@535 | 113 | static void* decode_instructions_pv = 0; |
minqi@4244 | 114 | static void* decode_instructions_sv = 0; |
jrose@535 | 115 | static const char* hsdis_path[] = { |
never@1155 | 116 | HSDIS_NAME"-"LIBARCH LIB_EXT, |
never@1155 | 117 | "./" HSDIS_NAME"-"LIBARCH LIB_EXT, |
never@1155 | 118 | #ifdef TARGET_DIR |
never@1155 | 119 | TARGET_DIR"/"HSDIS_NAME"-"LIBARCH LIB_EXT, |
jrose@535 | 120 | #endif |
jrose@535 | 121 | NULL |
jrose@535 | 122 | }; |
jrose@535 | 123 | |
jrose@535 | 124 | static const char* load_decode_instructions() { |
jrose@535 | 125 | void* dllib = NULL; |
jrose@535 | 126 | const char* *next_in_path = hsdis_path; |
jrose@535 | 127 | while (1) { |
minqi@4244 | 128 | decode_instructions_pv = dlsym(dllib, DECODE_INSTRUCTIONS_VIRTUAL_NAME); |
minqi@4244 | 129 | decode_instructions_sv = dlsym(dllib, DECODE_INSTRUCTIONS_NAME); |
minqi@4244 | 130 | if (decode_instructions_pv != NULL || decode_instructions_sv != NULL) |
jrose@535 | 131 | return NULL; |
jrose@535 | 132 | if (dllib != NULL) |
minqi@4244 | 133 | return "plugin does not defined "DECODE_INSTRUCTIONS_VIRTUAL_NAME" and "DECODE_INSTRUCTIONS_NAME; |
jrose@535 | 134 | for (dllib = NULL; dllib == NULL; ) { |
jrose@535 | 135 | const char* next_lib = (*next_in_path++); |
jrose@535 | 136 | if (next_lib == NULL) |
never@1155 | 137 | return "cannot find plugin "HSDIS_NAME LIB_EXT; |
jrose@535 | 138 | dllib = dlopen(next_lib, RTLD_LAZY); |
jrose@535 | 139 | } |
jrose@535 | 140 | } |
jrose@535 | 141 | } |
jrose@535 | 142 | |
jrose@535 | 143 | |
jrose@535 | 144 | static const char* lookup(void* addr) { |
minqi@4093 | 145 | #if defined(__ia64) || defined(__powerpc__) |
minqi@4093 | 146 | /* On IA64 and PPC function pointers are pointers to function descriptors */ |
minqi@4093 | 147 | #define CHECK_NAME(fn) \ |
minqi@4093 | 148 | if (addr == *((void**) &fn)) return #fn; |
minqi@4093 | 149 | #else |
jrose@535 | 150 | #define CHECK_NAME(fn) \ |
jrose@535 | 151 | if (addr == (void*) &fn) return #fn; |
minqi@4093 | 152 | #endif |
jrose@535 | 153 | |
jrose@535 | 154 | CHECK_NAME(main); |
jrose@535 | 155 | CHECK_NAME(greet); |
jrose@535 | 156 | return NULL; |
jrose@535 | 157 | } |
jrose@535 | 158 | |
jrose@535 | 159 | /* does the event match the tag, followed by a null, space, or slash? */ |
jrose@535 | 160 | #define MATCH(event, tag) \ |
jrose@535 | 161 | (!strncmp(event, tag, sizeof(tag)-1) && \ |
jrose@535 | 162 | (!event[sizeof(tag)-1] || strchr(" /", event[sizeof(tag)-1]))) |
jrose@535 | 163 | |
jrose@535 | 164 | |
jrose@535 | 165 | static const char event_cookie[] = "event_cookie"; /* demo placeholder */ |
minqi@4093 | 166 | static void* simple_handle_event(void* cookie, const char* event, void* arg) { |
minqi@4093 | 167 | if (MATCH(event, "/insn")) { |
minqi@4093 | 168 | // follow each complete insn by a nice newline |
minqi@4093 | 169 | printf("\n"); |
minqi@4093 | 170 | } |
minqi@4093 | 171 | return NULL; |
minqi@4093 | 172 | } |
minqi@4093 | 173 | |
jrose@535 | 174 | static void* handle_event(void* cookie, const char* event, void* arg) { |
jrose@535 | 175 | #define NS_DEMO "demo:" |
jrose@535 | 176 | if (cookie != event_cookie) |
jrose@535 | 177 | printf("*** bad event cookie %p != %p\n", cookie, event_cookie); |
jrose@535 | 178 | |
jrose@535 | 179 | if (xml) { |
jrose@535 | 180 | /* We could almost do a printf(event, arg), |
jrose@535 | 181 | but for the sake of a better demo, |
jrose@535 | 182 | we dress the result up as valid XML. |
jrose@535 | 183 | */ |
jrose@535 | 184 | const char* fmt = strchr(event, ' '); |
jrose@535 | 185 | int evlen = (fmt ? fmt - event : strlen(event)); |
jrose@535 | 186 | if (!fmt) { |
jrose@535 | 187 | if (event[0] != '/') { |
jrose@535 | 188 | printf("<"NS_DEMO"%.*s>", evlen, event); |
jrose@535 | 189 | } else { |
jrose@535 | 190 | printf("</"NS_DEMO"%.*s>", evlen-1, event+1); |
jrose@535 | 191 | } |
jrose@535 | 192 | } else { |
jrose@535 | 193 | if (event[0] != '/') { |
jrose@535 | 194 | printf("<"NS_DEMO"%.*s", evlen, event); |
jrose@535 | 195 | printf(fmt, arg); |
jrose@535 | 196 | printf(">"); |
jrose@535 | 197 | } else { |
jrose@535 | 198 | printf("<"NS_DEMO"%.*s_done", evlen-1, event+1); |
jrose@535 | 199 | printf(fmt, arg); |
jrose@535 | 200 | printf("/></"NS_DEMO"%.*s>", evlen-1, event+1); |
jrose@535 | 201 | } |
jrose@535 | 202 | } |
jrose@535 | 203 | } |
jrose@535 | 204 | |
jrose@535 | 205 | if (MATCH(event, "insn")) { |
jrose@535 | 206 | const char* name = lookup(arg); |
jrose@535 | 207 | if (name) printf("%s:\n", name); |
jrose@535 | 208 | |
jrose@535 | 209 | /* basic action for <insn>: */ |
jrose@535 | 210 | printf(" %p\t", arg); |
jrose@535 | 211 | |
jrose@535 | 212 | } else if (MATCH(event, "/insn")) { |
minqi@4093 | 213 | // follow each complete insn by a nice newline |
minqi@4093 | 214 | printf("\n"); |
jrose@535 | 215 | } else if (MATCH(event, "mach")) { |
jrose@535 | 216 | printf("Decoding for CPU '%s'\n", (char*) arg); |
jrose@535 | 217 | |
jrose@535 | 218 | } else if (MATCH(event, "addr")) { |
jrose@535 | 219 | /* basic action for <addr/>: */ |
jrose@535 | 220 | const char* name = lookup(arg); |
jrose@535 | 221 | if (name) { |
jrose@535 | 222 | printf("&%s (%p)", name, arg); |
jrose@535 | 223 | /* return non-null to notify hsdis not to print the addr */ |
jrose@535 | 224 | return arg; |
jrose@535 | 225 | } |
jrose@535 | 226 | } |
jrose@535 | 227 | |
jrose@535 | 228 | /* null return is always safe; can mean "I ignored it" */ |
jrose@535 | 229 | return NULL; |
jrose@535 | 230 | } |
jrose@535 | 231 | |
jrose@535 | 232 | #define fprintf_callback \ |
jrose@535 | 233 | (decode_instructions_printf_callback_ftype)&fprintf |
jrose@535 | 234 | |
minqi@4093 | 235 | void disassemble(uintptr_t from, uintptr_t to) { |
jrose@535 | 236 | const char* err = load_decode_instructions(); |
jrose@535 | 237 | if (err != NULL) { |
jrose@535 | 238 | printf("%s: %s\n", err, dlerror()); |
jrose@535 | 239 | exit(1); |
jrose@535 | 240 | } |
minqi@4244 | 241 | decode_func_vtype decode_instructions_v |
minqi@4244 | 242 | = (decode_func_vtype) decode_instructions_pv; |
minqi@4244 | 243 | decode_func_stype decode_instructions_s |
minqi@4244 | 244 | = (decode_func_stype) decode_instructions_sv; |
jrose@535 | 245 | void* res; |
minqi@4244 | 246 | if (decode_instructions_pv != NULL) { |
minqi@4244 | 247 | printf("\nDecoding from %p to %p...with %s\n", from, to, DECODE_INSTRUCTIONS_VIRTUAL_NAME); |
minqi@4244 | 248 | if (raw) { |
minqi@4244 | 249 | res = (*decode_instructions_v)(from, to, |
minqi@4244 | 250 | (unsigned char*)from, to - from, |
minqi@4244 | 251 | simple_handle_event, stdout, |
minqi@4244 | 252 | NULL, stdout, |
minqi@4244 | 253 | options, 0); |
minqi@4244 | 254 | } else { |
minqi@4244 | 255 | res = (*decode_instructions_v)(from, to, |
minqi@4244 | 256 | (unsigned char*)from, to - from, |
minqi@4244 | 257 | handle_event, (void*) event_cookie, |
minqi@4244 | 258 | fprintf_callback, stdout, |
minqi@4244 | 259 | options, 0); |
minqi@4244 | 260 | } |
minqi@4244 | 261 | if (res != (void*)to) |
minqi@4244 | 262 | printf("*** Result was %p!\n", res); |
jrose@535 | 263 | } |
minqi@4244 | 264 | void* sres; |
minqi@4244 | 265 | if (decode_instructions_sv != NULL) { |
minqi@4244 | 266 | printf("\nDecoding from %p to %p...with old decode_instructions\n", from, to, DECODE_INSTRUCTIONS_NAME); |
minqi@4244 | 267 | if (raw) { |
minqi@4244 | 268 | sres = (*decode_instructions_s)(from, to, |
minqi@4244 | 269 | simple_handle_event, stdout, |
minqi@4244 | 270 | NULL, stdout, |
minqi@4244 | 271 | options); |
minqi@4244 | 272 | } else { |
minqi@4244 | 273 | sres = (*decode_instructions_s)(from, to, |
minqi@4244 | 274 | handle_event, (void*) event_cookie, |
minqi@4244 | 275 | fprintf_callback, stdout, |
minqi@4244 | 276 | options); |
minqi@4244 | 277 | } |
minqi@4244 | 278 | if (sres != (void *)to) |
minqi@4244 | 279 | printf("*** Result of decode_instructions %p!\n", sres); |
minqi@4244 | 280 | } |
jrose@535 | 281 | } |