Mon, 24 Sep 2012 12:44:00 -0700
6879063: SA should use hsdis for disassembly
Summary: We should in SA to use hsdis for it like the JVM does to replace the current java based disassembler.
Reviewed-by: twisti, jrose, sla
Contributed-by: yumin.qi@oracle.com
1 /*
2 * Copyright (c) 2008, 2012, 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 */
25 /* hsdis-demo.c -- dump a range of addresses as native instructions
26 This demonstrates the protocol required by the HotSpot PrintAssembly option.
27 */
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <inttypes.h>
34 #include "hsdis.h"
37 void greet(const char*);
38 void disassemble(uintptr_t, uintptr_t);
39 void end_of_file();
41 const char* options = NULL;
42 int raw = 0;
43 int xml = 0;
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 void *start = (void*) &main;
68 void *end = (void*) &end_of_file;
69 #if defined(__ia64) || defined(__powerpc__)
70 /* On IA64 and PPC function pointers are pointers to function descriptors */
71 start = *((void**)start);
72 end = *((void**)end);
73 #endif
74 disassemble(start, (end > start) ? end : start + 64);
75 printf("Cheers!\n");
76 }
78 void greet(const char* whom) {
79 printf("Hello, %s!\n", whom);
80 }
82 void end_of_file() { }
84 /* don't disassemble after this point... */
86 #include "dlfcn.h"
88 #define DECODE_INSTRUCTIONS_NAME "decode_instructions_virtual"
89 #define HSDIS_NAME "hsdis"
90 static void* decode_instructions_pv = 0;
91 static const char* hsdis_path[] = {
92 HSDIS_NAME"-"LIBARCH LIB_EXT,
93 "./" HSDIS_NAME"-"LIBARCH LIB_EXT,
94 #ifdef TARGET_DIR
95 TARGET_DIR"/"HSDIS_NAME"-"LIBARCH LIB_EXT,
96 #endif
97 NULL
98 };
100 static const char* load_decode_instructions() {
101 void* dllib = NULL;
102 const char* *next_in_path = hsdis_path;
103 while (1) {
104 decode_instructions_pv = dlsym(dllib, DECODE_INSTRUCTIONS_NAME);
105 if (decode_instructions_pv != NULL)
106 return NULL;
107 if (dllib != NULL)
108 return "plugin does not defined "DECODE_INSTRUCTIONS_NAME;
109 for (dllib = NULL; dllib == NULL; ) {
110 const char* next_lib = (*next_in_path++);
111 if (next_lib == NULL)
112 return "cannot find plugin "HSDIS_NAME LIB_EXT;
113 dllib = dlopen(next_lib, RTLD_LAZY);
114 }
115 }
116 }
119 static const char* lookup(void* addr) {
120 #if defined(__ia64) || defined(__powerpc__)
121 /* On IA64 and PPC function pointers are pointers to function descriptors */
122 #define CHECK_NAME(fn) \
123 if (addr == *((void**) &fn)) return #fn;
124 #else
125 #define CHECK_NAME(fn) \
126 if (addr == (void*) &fn) return #fn;
127 #endif
129 CHECK_NAME(main);
130 CHECK_NAME(greet);
131 return NULL;
132 }
134 /* does the event match the tag, followed by a null, space, or slash? */
135 #define MATCH(event, tag) \
136 (!strncmp(event, tag, sizeof(tag)-1) && \
137 (!event[sizeof(tag)-1] || strchr(" /", event[sizeof(tag)-1])))
140 static const char event_cookie[] = "event_cookie"; /* demo placeholder */
141 static void* simple_handle_event(void* cookie, const char* event, void* arg) {
142 if (MATCH(event, "/insn")) {
143 // follow each complete insn by a nice newline
144 printf("\n");
145 }
146 return NULL;
147 }
149 static void* handle_event(void* cookie, const char* event, void* arg) {
150 #define NS_DEMO "demo:"
151 if (cookie != event_cookie)
152 printf("*** bad event cookie %p != %p\n", cookie, event_cookie);
154 if (xml) {
155 /* We could almost do a printf(event, arg),
156 but for the sake of a better demo,
157 we dress the result up as valid XML.
158 */
159 const char* fmt = strchr(event, ' ');
160 int evlen = (fmt ? fmt - event : strlen(event));
161 if (!fmt) {
162 if (event[0] != '/') {
163 printf("<"NS_DEMO"%.*s>", evlen, event);
164 } else {
165 printf("</"NS_DEMO"%.*s>", evlen-1, event+1);
166 }
167 } else {
168 if (event[0] != '/') {
169 printf("<"NS_DEMO"%.*s", evlen, event);
170 printf(fmt, arg);
171 printf(">");
172 } else {
173 printf("<"NS_DEMO"%.*s_done", evlen-1, event+1);
174 printf(fmt, arg);
175 printf("/></"NS_DEMO"%.*s>", evlen-1, event+1);
176 }
177 }
178 }
180 if (MATCH(event, "insn")) {
181 const char* name = lookup(arg);
182 if (name) printf("%s:\n", name);
184 /* basic action for <insn>: */
185 printf(" %p\t", arg);
187 } else if (MATCH(event, "/insn")) {
188 // follow each complete insn by a nice newline
189 printf("\n");
190 } else if (MATCH(event, "mach")) {
191 printf("Decoding for CPU '%s'\n", (char*) arg);
193 } else if (MATCH(event, "addr")) {
194 /* basic action for <addr/>: */
195 const char* name = lookup(arg);
196 if (name) {
197 printf("&%s (%p)", name, arg);
198 /* return non-null to notify hsdis not to print the addr */
199 return arg;
200 }
201 }
203 /* null return is always safe; can mean "I ignored it" */
204 return NULL;
205 }
207 #define fprintf_callback \
208 (decode_instructions_printf_callback_ftype)&fprintf
210 void disassemble(uintptr_t from, uintptr_t to) {
211 const char* err = load_decode_instructions();
212 if (err != NULL) {
213 printf("%s: %s\n", err, dlerror());
214 exit(1);
215 }
216 printf("Decoding from %p to %p...\n", from, to);
217 decode_instructions_ftype decode_instructions
218 = (decode_instructions_ftype) decode_instructions_pv;
219 void* res;
220 if (raw && xml) {
221 res = (*decode_instructions)(from, to, (unsigned char*)from, to - from, simple_handle_event, stdout, NULL, stdout, options);
222 } else if (raw) {
223 res = (*decode_instructions)(from, to, (unsigned char*)from, to - from, simple_handle_event, stdout, NULL, stdout, options);
224 } else {
225 res = (*decode_instructions)(from, to, (unsigned char*)from, to - from,
226 handle_event, (void*) event_cookie,
227 fprintf_callback, stdout,
228 options);
229 }
230 if (res != (void*)to)
231 printf("*** Result was %p!\n", res);
232 }