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