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