Wed, 15 Apr 2009 09:53:54 -0700
6684007: PrintAssembly plugin not available for linux or windows
Reviewed-by: rasbold, jrose, twisti
1 /*
2 * Copyright 2008 Sun Microsystems, Inc. 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 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 */
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 "hsdis.h"
31 #include "stdio.h"
32 #include "stdlib.h"
33 #include "string.h"
35 void greet(const char*);
36 void disassemble(void*, void*);
37 void end_of_file();
39 const char* options = NULL;
40 int raw = 0;
41 int xml = 0;
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 }
69 void greet(const char* whom) {
70 printf("Hello, %s!\n", whom);
71 }
73 void end_of_file() { }
75 /* don't disassemble after this point... */
77 #include "dlfcn.h"
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 };
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 }
110 static const char* lookup(void* addr) {
111 #define CHECK_NAME(fn) \
112 if (addr == (void*) &fn) return #fn;
114 CHECK_NAME(main);
115 CHECK_NAME(greet);
116 return NULL;
117 }
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])))
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);
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 }
157 if (MATCH(event, "insn")) {
158 const char* name = lookup(arg);
159 if (name) printf("%s:\n", name);
161 /* basic action for <insn>: */
162 printf(" %p\t", arg);
164 } else if (MATCH(event, "/insn")) {
165 /* basic action for </insn>:
166 (none, plugin puts the newline for us
167 */
169 } else if (MATCH(event, "mach")) {
170 printf("Decoding for CPU '%s'\n", (char*) arg);
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 }
182 /* null return is always safe; can mean "I ignored it" */
183 return NULL;
184 }
186 #define fprintf_callback \
187 (decode_instructions_printf_callback_ftype)&fprintf
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 }