Thu, 27 May 2010 19:08:38 -0700
6941466: Oracle rebranding changes for Hotspot repositories
Summary: Change all the Sun copyrights to Oracle copyright
Reviewed-by: ohair
jrose@535 | 1 | /* |
trims@1907 | 2 | * Copyright (c) 2008, 2009, 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 | |
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 |
never@1155 | 56 | { printf("Usage: %s [-xml] [name...]\n", av[0]); 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 | #define DECODE_INSTRUCTIONS_NAME "decode_instructions" |
jrose@535 | 80 | #define HSDIS_NAME "hsdis" |
jrose@535 | 81 | static void* decode_instructions_pv = 0; |
jrose@535 | 82 | static const char* hsdis_path[] = { |
never@1155 | 83 | HSDIS_NAME"-"LIBARCH LIB_EXT, |
never@1155 | 84 | "./" HSDIS_NAME"-"LIBARCH LIB_EXT, |
never@1155 | 85 | #ifdef TARGET_DIR |
never@1155 | 86 | TARGET_DIR"/"HSDIS_NAME"-"LIBARCH LIB_EXT, |
jrose@535 | 87 | #endif |
jrose@535 | 88 | NULL |
jrose@535 | 89 | }; |
jrose@535 | 90 | |
jrose@535 | 91 | static const char* load_decode_instructions() { |
jrose@535 | 92 | void* dllib = NULL; |
jrose@535 | 93 | const char* *next_in_path = hsdis_path; |
jrose@535 | 94 | while (1) { |
jrose@535 | 95 | decode_instructions_pv = dlsym(dllib, DECODE_INSTRUCTIONS_NAME); |
jrose@535 | 96 | if (decode_instructions_pv != NULL) |
jrose@535 | 97 | return NULL; |
jrose@535 | 98 | if (dllib != NULL) |
jrose@535 | 99 | return "plugin does not defined "DECODE_INSTRUCTIONS_NAME; |
jrose@535 | 100 | for (dllib = NULL; dllib == NULL; ) { |
jrose@535 | 101 | const char* next_lib = (*next_in_path++); |
jrose@535 | 102 | if (next_lib == NULL) |
never@1155 | 103 | return "cannot find plugin "HSDIS_NAME LIB_EXT; |
jrose@535 | 104 | dllib = dlopen(next_lib, RTLD_LAZY); |
jrose@535 | 105 | } |
jrose@535 | 106 | } |
jrose@535 | 107 | } |
jrose@535 | 108 | |
jrose@535 | 109 | |
jrose@535 | 110 | static const char* lookup(void* addr) { |
jrose@535 | 111 | #define CHECK_NAME(fn) \ |
jrose@535 | 112 | if (addr == (void*) &fn) return #fn; |
jrose@535 | 113 | |
jrose@535 | 114 | CHECK_NAME(main); |
jrose@535 | 115 | CHECK_NAME(greet); |
jrose@535 | 116 | return NULL; |
jrose@535 | 117 | } |
jrose@535 | 118 | |
jrose@535 | 119 | /* does the event match the tag, followed by a null, space, or slash? */ |
jrose@535 | 120 | #define MATCH(event, tag) \ |
jrose@535 | 121 | (!strncmp(event, tag, sizeof(tag)-1) && \ |
jrose@535 | 122 | (!event[sizeof(tag)-1] || strchr(" /", event[sizeof(tag)-1]))) |
jrose@535 | 123 | |
jrose@535 | 124 | |
jrose@535 | 125 | static const char event_cookie[] = "event_cookie"; /* demo placeholder */ |
jrose@535 | 126 | static void* handle_event(void* cookie, const char* event, void* arg) { |
jrose@535 | 127 | #define NS_DEMO "demo:" |
jrose@535 | 128 | if (cookie != event_cookie) |
jrose@535 | 129 | printf("*** bad event cookie %p != %p\n", cookie, event_cookie); |
jrose@535 | 130 | |
jrose@535 | 131 | if (xml) { |
jrose@535 | 132 | /* We could almost do a printf(event, arg), |
jrose@535 | 133 | but for the sake of a better demo, |
jrose@535 | 134 | we dress the result up as valid XML. |
jrose@535 | 135 | */ |
jrose@535 | 136 | const char* fmt = strchr(event, ' '); |
jrose@535 | 137 | int evlen = (fmt ? fmt - event : strlen(event)); |
jrose@535 | 138 | if (!fmt) { |
jrose@535 | 139 | if (event[0] != '/') { |
jrose@535 | 140 | printf("<"NS_DEMO"%.*s>", evlen, event); |
jrose@535 | 141 | } else { |
jrose@535 | 142 | printf("</"NS_DEMO"%.*s>", evlen-1, event+1); |
jrose@535 | 143 | } |
jrose@535 | 144 | } else { |
jrose@535 | 145 | if (event[0] != '/') { |
jrose@535 | 146 | printf("<"NS_DEMO"%.*s", evlen, event); |
jrose@535 | 147 | printf(fmt, arg); |
jrose@535 | 148 | printf(">"); |
jrose@535 | 149 | } else { |
jrose@535 | 150 | printf("<"NS_DEMO"%.*s_done", evlen-1, event+1); |
jrose@535 | 151 | printf(fmt, arg); |
jrose@535 | 152 | printf("/></"NS_DEMO"%.*s>", evlen-1, event+1); |
jrose@535 | 153 | } |
jrose@535 | 154 | } |
jrose@535 | 155 | } |
jrose@535 | 156 | |
jrose@535 | 157 | if (MATCH(event, "insn")) { |
jrose@535 | 158 | const char* name = lookup(arg); |
jrose@535 | 159 | if (name) printf("%s:\n", name); |
jrose@535 | 160 | |
jrose@535 | 161 | /* basic action for <insn>: */ |
jrose@535 | 162 | printf(" %p\t", arg); |
jrose@535 | 163 | |
jrose@535 | 164 | } else if (MATCH(event, "/insn")) { |
jrose@535 | 165 | /* basic action for </insn>: |
jrose@535 | 166 | (none, plugin puts the newline for us |
jrose@535 | 167 | */ |
jrose@535 | 168 | |
jrose@535 | 169 | } else if (MATCH(event, "mach")) { |
jrose@535 | 170 | printf("Decoding for CPU '%s'\n", (char*) arg); |
jrose@535 | 171 | |
jrose@535 | 172 | } else if (MATCH(event, "addr")) { |
jrose@535 | 173 | /* basic action for <addr/>: */ |
jrose@535 | 174 | const char* name = lookup(arg); |
jrose@535 | 175 | if (name) { |
jrose@535 | 176 | printf("&%s (%p)", name, arg); |
jrose@535 | 177 | /* return non-null to notify hsdis not to print the addr */ |
jrose@535 | 178 | return arg; |
jrose@535 | 179 | } |
jrose@535 | 180 | } |
jrose@535 | 181 | |
jrose@535 | 182 | /* null return is always safe; can mean "I ignored it" */ |
jrose@535 | 183 | return NULL; |
jrose@535 | 184 | } |
jrose@535 | 185 | |
jrose@535 | 186 | #define fprintf_callback \ |
jrose@535 | 187 | (decode_instructions_printf_callback_ftype)&fprintf |
jrose@535 | 188 | |
jrose@535 | 189 | void disassemble(void* from, void* to) { |
jrose@535 | 190 | const char* err = load_decode_instructions(); |
jrose@535 | 191 | if (err != NULL) { |
jrose@535 | 192 | printf("%s: %s\n", err, dlerror()); |
jrose@535 | 193 | exit(1); |
jrose@535 | 194 | } |
jrose@535 | 195 | printf("Decoding from %p to %p...\n", from, to); |
jrose@535 | 196 | decode_instructions_ftype decode_instructions |
jrose@535 | 197 | = (decode_instructions_ftype) decode_instructions_pv; |
jrose@535 | 198 | void* res; |
jrose@535 | 199 | if (raw && xml) { |
jrose@535 | 200 | res = (*decode_instructions)(from, to, NULL, stdout, NULL, stdout, options); |
jrose@535 | 201 | } else if (raw) { |
jrose@535 | 202 | res = (*decode_instructions)(from, to, NULL, NULL, NULL, stdout, options); |
jrose@535 | 203 | } else { |
jrose@535 | 204 | res = (*decode_instructions)(from, to, |
jrose@535 | 205 | handle_event, (void*) event_cookie, |
jrose@535 | 206 | fprintf_callback, stdout, |
jrose@535 | 207 | options); |
jrose@535 | 208 | } |
jrose@535 | 209 | if (res != to) |
jrose@535 | 210 | printf("*** Result was %p!\n", res); |
jrose@535 | 211 | } |