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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20 * CA 95054 USA or visit www.sun.com if you need additional information or 21 * have any questions. 22 * 23 */ 24 25 /* hsdis-demo.c -- dump a range of addresses as native instructions 26 This demonstrates the protocol required by the HotSpot PrintAssembly option. 27 */ 28 29 #include "hsdis.h" 30 31 #include "stdio.h" 32 #include "stdlib.h" 33 #include "string.h" 34 35 void greet(const char*); 36 void disassemble(void*, void*); 37 void end_of_file(); 38 39 const char* options = NULL; 40 int raw = 0; 41 int xml = 0; 42 43 int main(int ac, char** av) { 44 int greeted = 0; 45 int i; 46 for (i = 1; i < ac; i++) { 47 const char* arg = av[i]; 48 if (arg[0] == '-') { 49 if (!strcmp(arg, "-xml")) 50 xml ^= 1; 51 else if (!strcmp(arg, "-raw")) 52 raw ^= 1; 53 else if (!strncmp(arg, "-options=", 9)) 54 options = arg+9; 55 else 56 { printf("Usage: %s [-xml] [name...]\n", av[0]); exit(2); } 57 continue; 58 } 59 greet(arg); 60 greeted = 1; 61 } 62 if (!greeted) 63 greet("world"); 64 printf("...And now for something completely different:\n"); 65 disassemble((void*) &main, (void*) &end_of_file); 66 printf("Cheers!\n"); 67 } 68 69 void greet(const char* whom) { 70 printf("Hello, %s!\n", whom); 71 } 72 73 void end_of_file() { } 74 75 /* don't disassemble after this point... */ 76 77 #include "dlfcn.h" 78 79 #define DECODE_INSTRUCTIONS_NAME "decode_instructions" 80 #define HSDIS_NAME "hsdis" 81 static void* decode_instructions_pv = 0; 82 static const char* hsdis_path[] = { 83 HSDIS_NAME"-"LIBARCH LIB_EXT, 84 "./" HSDIS_NAME"-"LIBARCH LIB_EXT, 85 #ifdef TARGET_DIR 86 TARGET_DIR"/"HSDIS_NAME"-"LIBARCH LIB_EXT, 87 #endif 88 NULL 89 }; 90 91 static const char* load_decode_instructions() { 92 void* dllib = NULL; 93 const char* *next_in_path = hsdis_path; 94 while (1) { 95 decode_instructions_pv = dlsym(dllib, DECODE_INSTRUCTIONS_NAME); 96 if (decode_instructions_pv != NULL) 97 return NULL; 98 if (dllib != NULL) 99 return "plugin does not defined "DECODE_INSTRUCTIONS_NAME; 100 for (dllib = NULL; dllib == NULL; ) { 101 const char* next_lib = (*next_in_path++); 102 if (next_lib == NULL) 103 return "cannot find plugin "HSDIS_NAME LIB_EXT; 104 dllib = dlopen(next_lib, RTLD_LAZY); 105 } 106 } 107 } 108 109 110 static const char* lookup(void* addr) { 111 #define CHECK_NAME(fn) \ 112 if (addr == (void*) &fn) return #fn; 113 114 CHECK_NAME(main); 115 CHECK_NAME(greet); 116 return NULL; 117 } 118 119 /* does the event match the tag, followed by a null, space, or slash? */ 120 #define MATCH(event, tag) \ 121 (!strncmp(event, tag, sizeof(tag)-1) && \ 122 (!event[sizeof(tag)-1] || strchr(" /", event[sizeof(tag)-1]))) 123 124 125 static const char event_cookie[] = "event_cookie"; /* demo placeholder */ 126 static void* handle_event(void* cookie, const char* event, void* arg) { 127 #define NS_DEMO "demo:" 128 if (cookie != event_cookie) 129 printf("*** bad event cookie %p != %p\n", cookie, event_cookie); 130 131 if (xml) { 132 /* We could almost do a printf(event, arg), 133 but for the sake of a better demo, 134 we dress the result up as valid XML. 135 */ 136 const char* fmt = strchr(event, ' '); 137 int evlen = (fmt ? fmt - event : strlen(event)); 138 if (!fmt) { 139 if (event[0] != '/') { 140 printf("<"NS_DEMO"%.*s>", evlen, event); 141 } else { 142 printf("</"NS_DEMO"%.*s>", evlen-1, event+1); 143 } 144 } else { 145 if (event[0] != '/') { 146 printf("<"NS_DEMO"%.*s", evlen, event); 147 printf(fmt, arg); 148 printf(">"); 149 } else { 150 printf("<"NS_DEMO"%.*s_done", evlen-1, event+1); 151 printf(fmt, arg); 152 printf("/></"NS_DEMO"%.*s>", evlen-1, event+1); 153 } 154 } 155 } 156 157 if (MATCH(event, "insn")) { 158 const char* name = lookup(arg); 159 if (name) printf("%s:\n", name); 160 161 /* basic action for <insn>: */ 162 printf(" %p\t", arg); 163 164 } else if (MATCH(event, "/insn")) { 165 /* basic action for </insn>: 166 (none, plugin puts the newline for us 167 */ 168 169 } else if (MATCH(event, "mach")) { 170 printf("Decoding for CPU '%s'\n", (char*) arg); 171 172 } else if (MATCH(event, "addr")) { 173 /* basic action for <addr/>: */ 174 const char* name = lookup(arg); 175 if (name) { 176 printf("&%s (%p)", name, arg); 177 /* return non-null to notify hsdis not to print the addr */ 178 return arg; 179 } 180 } 181 182 /* null return is always safe; can mean "I ignored it" */ 183 return NULL; 184 } 185 186 #define fprintf_callback \ 187 (decode_instructions_printf_callback_ftype)&fprintf 188 189 void disassemble(void* from, void* to) { 190 const char* err = load_decode_instructions(); 191 if (err != NULL) { 192 printf("%s: %s\n", err, dlerror()); 193 exit(1); 194 } 195 printf("Decoding from %p to %p...\n", from, to); 196 decode_instructions_ftype decode_instructions 197 = (decode_instructions_ftype) decode_instructions_pv; 198 void* res; 199 if (raw && xml) { 200 res = (*decode_instructions)(from, to, NULL, stdout, NULL, stdout, options); 201 } else if (raw) { 202 res = (*decode_instructions)(from, to, NULL, NULL, NULL, stdout, options); 203 } else { 204 res = (*decode_instructions)(from, to, 205 handle_event, (void*) event_cookie, 206 fprintf_callback, stdout, 207 options); 208 } 209 if (res != to) 210 printf("*** Result was %p!\n", res); 211 } | 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20 * CA 95054 USA or visit www.sun.com if you need additional information or 21 * have any questions. 22 * 23 */ 24 25 /* hsdis-demo.c -- dump a range of addresses as native instructions 26 This demonstrates the protocol required by the HotSpot PrintAssembly option. 27 */ 28 29 #include <stdio.h> 30 #include <stdlib.h> 31 #include <string.h> 32 #include <inttypes.h> 33 34 #include "hsdis.h" 35 36 37 void greet(const char*); 38 void disassemble(uintptr_t, uintptr_t); 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 #if defined(__ia64) || defined(__powerpc__) 68 /* On IA64 and PPC function pointers are pointers to function descriptors */ 69 disassemble(*((uintptr_t*) &main, *((uintptr_t*) &end_of_file); 70 #else 71 disassemble((uintptr_t) &main, (uintptr_t) &end_of_file); 72 #endif 73 printf("Cheers!\n"); 74 } 75 76 void greet(const char* whom) { 77 printf("Hello, %s!\n", whom); 78 } 79 80 void end_of_file() { } 81 82 /* don't disassemble after this point... */ 83 84 #include "dlfcn.h" 85 86 #define DECODE_INSTRUCTIONS_NAME "decode_instructions_virtual" 87 #define HSDIS_NAME "hsdis" 88 static void* decode_instructions_pv = 0; 89 static const char* hsdis_path[] = { 90 HSDIS_NAME"-"LIBARCH LIB_EXT, 91 "./" HSDIS_NAME"-"LIBARCH LIB_EXT, 92 #ifdef TARGET_DIR 93 TARGET_DIR"/"HSDIS_NAME"-"LIBARCH LIB_EXT, 94 #endif 95 NULL 96 }; 97 98 static const char* load_decode_instructions() { 99 void* dllib = NULL; 100 const char* *next_in_path = hsdis_path; 101 while (1) { 102 decode_instructions_pv = dlsym(dllib, DECODE_INSTRUCTIONS_NAME); 103 if (decode_instructions_pv != NULL) 104 return NULL; 105 if (dllib != NULL) 106 return "plugin does not defined "DECODE_INSTRUCTIONS_NAME; 107 for (dllib = NULL; dllib == NULL; ) { 108 const char* next_lib = (*next_in_path++); 109 if (next_lib == NULL) 110 return "cannot find plugin "HSDIS_NAME LIB_EXT; 111 dllib = dlopen(next_lib, RTLD_LAZY); 112 } 113 } 114 } 115 116 117 static const char* lookup(void* addr) { 118 #if defined(__ia64) || defined(__powerpc__) 119 /* On IA64 and PPC function pointers are pointers to function descriptors */ 120 #define CHECK_NAME(fn) \ 121 if (addr == *((void**) &fn)) return #fn; 122 #else 123 #define CHECK_NAME(fn) \ 124 if (addr == (void*) &fn) return #fn; 125 #endif 126 127 CHECK_NAME(main); 128 CHECK_NAME(greet); 129 return NULL; 130 } 131 132 /* does the event match the tag, followed by a null, space, or slash? */ 133 #define MATCH(event, tag) \ 134 (!strncmp(event, tag, sizeof(tag)-1) && \ 135 (!event[sizeof(tag)-1] || strchr(" /", event[sizeof(tag)-1]))) 136 137 138 static const char event_cookie[] = "event_cookie"; /* demo placeholder */ 139 static void* simple_handle_event(void* cookie, const char* event, void* arg) { 140 if (MATCH(event, "/insn")) { 141 // follow each complete insn by a nice newline 142 printf("\n"); 143 } 144 return NULL; 145 } 146 147 static void* handle_event(void* cookie, const char* event, void* arg) { 148 #define NS_DEMO "demo:" 149 if (cookie != event_cookie) 150 printf("*** bad event cookie %p != %p\n", cookie, event_cookie); 151 152 if (xml) { 153 /* We could almost do a printf(event, arg), 154 but for the sake of a better demo, 155 we dress the result up as valid XML. 156 */ 157 const char* fmt = strchr(event, ' '); 158 int evlen = (fmt ? fmt - event : strlen(event)); 159 if (!fmt) { 160 if (event[0] != '/') { 161 printf("<"NS_DEMO"%.*s>", evlen, event); 162 } else { 163 printf("</"NS_DEMO"%.*s>", evlen-1, event+1); 164 } 165 } else { 166 if (event[0] != '/') { 167 printf("<"NS_DEMO"%.*s", evlen, event); 168 printf(fmt, arg); 169 printf(">"); 170 } else { 171 printf("<"NS_DEMO"%.*s_done", evlen-1, event+1); 172 printf(fmt, arg); 173 printf("/></"NS_DEMO"%.*s>", evlen-1, event+1); 174 } 175 } 176 } 177 178 if (MATCH(event, "insn")) { 179 const char* name = lookup(arg); 180 if (name) printf("%s:\n", name); 181 182 /* basic action for <insn>: */ 183 printf(" %p\t", arg); 184 185 } else if (MATCH(event, "/insn")) { 186 // follow each complete insn by a nice newline 187 printf("\n"); 188 } else if (MATCH(event, "mach")) { 189 printf("Decoding for CPU '%s'\n", (char*) arg); 190 191 } else if (MATCH(event, "addr")) { 192 /* basic action for <addr/>: */ 193 const char* name = lookup(arg); 194 if (name) { 195 printf("&%s (%p)", name, arg); 196 /* return non-null to notify hsdis not to print the addr */ 197 return arg; 198 } 199 } 200 201 /* null return is always safe; can mean "I ignored it" */ 202 return NULL; 203 } 204 205 #define fprintf_callback \ 206 (decode_instructions_printf_callback_ftype)&fprintf 207 208 void disassemble(uintptr_t from, uintptr_t to) { 209 const char* err = load_decode_instructions(); 210 if (err != NULL) { 211 printf("%s: %s\n", err, dlerror()); 212 exit(1); 213 } 214 printf("Decoding from %p to %p...\n", from, to); 215 decode_instructions_ftype decode_instructions 216 = (decode_instructions_ftype) decode_instructions_pv; 217 void* res; 218 if (raw && xml) { 219 res = (*decode_instructions)(from, to, (unsigned char*)from, to - from, simple_handle_event, stdout, NULL, stdout, options); 220 } else if (raw) { 221 res = (*decode_instructions)(from, to, (unsigned char*)from, to - from, simple_handle_event, stdout, NULL, stdout, options); 222 } else { 223 res = (*decode_instructions)(from, to, (unsigned char*)from, to - from, 224 handle_event, (void*) event_cookie, 225 fprintf_callback, stdout, 226 options); 227 } 228 if (res != (void*)to) 229 printf("*** Result was %p!\n", res); 230 } |