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.c -- dump a range of addresses as native instructions
26 This implements the plugin protocol required by the
27 HotSpot PrintAssembly option.
28 */
29
30 #include "hsdis.h"
31
32 #include <sysdep.h>
33 #include <libiberty.h>
34 #include <bfd.h>
35 #include <dis-asm.h>
36 #include <inttypes.h>
37
38 #ifndef bool
39 #define bool int
40 #define true 1
41 #define false 0
42 #endif /*bool*/
43
44 /* short names for stuff in hsdis.h */
45 typedef decode_instructions_event_callback_ftype event_callback_t;
46 typedef decode_instructions_printf_callback_ftype printf_callback_t;
47
48 /* disassemble_info.application_data object */
49 struct hsdis_app_data {
50 /* the arguments to decode_instructions */
51 uintptr_t start; uintptr_t end;
52 event_callback_t event_callback; void* event_stream;
53 printf_callback_t printf_callback; void* printf_stream;
54 bool losing;
55
56 /* the architecture being disassembled */
57 const char* arch_name;
58 const bfd_arch_info_type* arch_info;
59
60 /* the disassembler we are going to use: */
61 disassembler_ftype dfn;
62 struct disassemble_info dinfo; /* the actual struct! */
63
64 char mach_option[64];
65 char insn_options[256];
66 };
67
68 #define DECL_APP_DATA(dinfo) \
69 struct hsdis_app_data* app_data = (struct hsdis_app_data*) (dinfo)->application_data
70
71 #define DECL_EVENT_CALLBACK(app_data) \
72 event_callback_t event_callback = (app_data)->event_callback; \
73 void* event_stream = (app_data)->event_stream
74
75 #define DECL_PRINTF_CALLBACK(app_data) \
76 printf_callback_t printf_callback = (app_data)->printf_callback; \
77 void* printf_stream = (app_data)->printf_stream
78
79
80 static void print_help(struct hsdis_app_data* app_data,
81 const char* msg, const char* arg);
82 static void setup_app_data(struct hsdis_app_data* app_data,
83 const char* options);
84 static const char* format_insn_close(const char* close,
85 disassemble_info* dinfo,
86 char* buf, size_t bufsize);
87
88 void*
89 #ifdef DLL_ENTRY
90 DLL_ENTRY
91 #endif
92 decode_instructions(void* start_pv, void* end_pv,
93 event_callback_t event_callback_arg, void* event_stream_arg,
94 printf_callback_t printf_callback_arg, void* printf_stream_arg,
95 const char* options) {
96 struct hsdis_app_data app_data;
97 memset(&app_data, 0, sizeof(app_data));
98 app_data.start = (uintptr_t) start_pv;
99 app_data.end = (uintptr_t) end_pv;
100 app_data.event_callback = event_callback_arg;
101 app_data.event_stream = event_stream_arg;
102 app_data.printf_callback = printf_callback_arg;
103 app_data.printf_stream = printf_stream_arg;
104
105 setup_app_data(&app_data, options);
106 char buf[128];
107
108 {
109 /* now reload everything from app_data: */
110 DECL_EVENT_CALLBACK(&app_data);
111 DECL_PRINTF_CALLBACK(&app_data);
112 uintptr_t start = app_data.start;
113 uintptr_t end = app_data.end;
114 uintptr_t p = start;
115
116 (*event_callback)(event_stream, "insns", (void*)start);
117
118 (*event_callback)(event_stream, "mach name='%s'",
119 (void*) app_data.arch_info->printable_name);
120 if (app_data.dinfo.bytes_per_line != 0) {
121 (*event_callback)(event_stream, "format bytes-per-line='%p'/",
122 (void*)(intptr_t) app_data.dinfo.bytes_per_line);
123 }
124
125 while (p < end && !app_data.losing) {
126 (*event_callback)(event_stream, "insn", (void*) p);
127
128 /* reset certain state, so we can read it with confidence */
129 app_data.dinfo.insn_info_valid = 0;
130 app_data.dinfo.branch_delay_insns = 0;
131 app_data.dinfo.data_size = 0;
132 app_data.dinfo.insn_type = 0;
133
134 int size = (*app_data.dfn)((bfd_vma) p, &app_data.dinfo);
135
136 if (size > 0) p += size;
137 else app_data.losing = true;
138
139 const char* insn_close = format_insn_close("/insn", &app_data.dinfo,
140 buf, sizeof(buf));
141 (*event_callback)(event_stream, insn_close, (void*) p);
142
143 /* follow each complete insn by a nice newline */
144 (*printf_callback)(printf_stream, "\n");
145 }
146
147 (*event_callback)(event_stream, "/insns", (void*) p);
148 return (void*) p;
149 }
150 }
151
152 /* take the address of the function, for luck, and also test the typedef: */
153 const decode_instructions_ftype decode_instructions_address = &decode_instructions;
154
155 static const char* format_insn_close(const char* close,
156 disassemble_info* dinfo,
157 char* buf, size_t bufsize) {
158 if (!dinfo->insn_info_valid)
159 return close;
160 enum dis_insn_type itype = dinfo->insn_type;
161 int dsize = dinfo->data_size, delays = dinfo->branch_delay_insns;
162 if ((itype == dis_nonbranch && (dsize | delays) == 0)
163 || (strlen(close) + 3*20 > bufsize))
164 return close;
165
166 const char* type = "unknown";
167 switch (itype) {
168 case dis_nonbranch: type = NULL; break;
169 case dis_branch: type = "branch"; break;
170 case dis_condbranch: type = "condbranch"; break;
171 case dis_jsr: type = "jsr"; break;
172 case dis_condjsr: type = "condjsr"; break;
173 case dis_dref: type = "dref"; break;
174 case dis_dref2: type = "dref2"; break;
175 }
176
177 strcpy(buf, close);
178 char* p = buf;
179 if (type) sprintf(p += strlen(p), " type='%s'", type);
180 if (dsize) sprintf(p += strlen(p), " dsize='%d'", dsize);
181 if (delays) sprintf(p += strlen(p), " delay='%d'", delays);
182 return buf;
183 }
184
185 /* handler functions */
186
187 static int
188 hsdis_read_memory_func(bfd_vma memaddr,
189 bfd_byte* myaddr,
190 unsigned int length,
191 struct disassemble_info* dinfo) {
192 uintptr_t memaddr_p = (uintptr_t) memaddr;
193 DECL_APP_DATA(dinfo);
194 if (memaddr_p + length > app_data->end) {
195 /* read is out of bounds */
196 return EIO;
197 } else {
198 memcpy(myaddr, (bfd_byte*) memaddr_p, length);
199 return 0;
200 }
201 }
202
203 static void
204 hsdis_print_address_func(bfd_vma vma, struct disassemble_info* dinfo) {
205 /* the actual value to print: */
206 void* addr_value = (void*) (uintptr_t) vma;
207 DECL_APP_DATA(dinfo);
208 DECL_EVENT_CALLBACK(app_data);
209
210 /* issue the event: */
211 void* result =
212 (*event_callback)(event_stream, "addr/", addr_value);
213 if (result == NULL) {
214 /* event declined */
215 generic_print_address(vma, dinfo);
216 }
217 }
218
340 }
341
342 static void parse_caller_options(struct hsdis_app_data* app_data, const char* caller_options) {
343 char* iop_base = app_data->insn_options;
344 char* iop_limit = iop_base + sizeof(app_data->insn_options) - 1;
345 char* iop = iop_base;
346 const char* p;
347 for (p = caller_options; p != NULL; ) {
348 const char* q = strchr(p, ',');
349 size_t plen = (q == NULL) ? strlen(p) : ((q++) - p);
350 if (plen == 4 && strncmp(p, "help", plen) == 0) {
351 print_help(app_data, NULL, NULL);
352 } else if (plen >= 5 && strncmp(p, "mach=", 5) == 0) {
353 char* mach_option = app_data->mach_option;
354 size_t mach_size = sizeof(app_data->mach_option);
355 mach_size -= 1; /*leave room for the null*/
356 if (plen > mach_size) plen = mach_size;
357 strncpy(mach_option, p, plen);
358 mach_option[plen] = '\0';
359 } else if (plen > 6 && strncmp(p, "hsdis-", 6)) {
360 // do not pass these to the next level
361 } else {
362 /* just copy it; {i386,sparc}-dis.c might like to see it */
363 if (iop > iop_base && iop < iop_limit) (*iop++) = ',';
364 if (iop + plen > iop_limit)
365 plen = iop_limit - iop;
366 strncpy(iop, p, plen);
367 iop += plen;
368 }
369 p = q;
370 }
371 }
372
373 static void print_help(struct hsdis_app_data* app_data,
374 const char* msg, const char* arg) {
375 DECL_PRINTF_CALLBACK(app_data);
376 if (msg != NULL) {
377 (*printf_callback)(printf_stream, "hsdis: ");
378 (*printf_callback)(printf_stream, msg, arg);
379 (*printf_callback)(printf_stream, "\n");
380 }
451 static int print_to_dev_null(void* ignore_stream, const char* ignore_format, ...) {
452 return 0;
453 }
454
455 /* Prime the pump by running the selected disassembler on a null input.
456 This forces the machine-specific disassembler to divulge invariant
457 information like bytes_per_line.
458 */
459 static void parse_fake_insn(disassembler_ftype dfn,
460 struct disassemble_info* dinfo) {
461 typedef int (*read_memory_ftype)
462 (bfd_vma memaddr, bfd_byte *myaddr, unsigned int length,
463 struct disassemble_info *info);
464 read_memory_ftype read_memory_func = dinfo->read_memory_func;
465 fprintf_ftype fprintf_func = dinfo->fprintf_func;
466
467 dinfo->read_memory_func = &read_zero_data_only;
468 dinfo->fprintf_func = &print_to_dev_null;
469 (*dfn)(0, dinfo);
470
471 // put it back:
472 dinfo->read_memory_func = read_memory_func;
473 dinfo->fprintf_func = fprintf_func;
474 }
475
476 static void init_disassemble_info_from_bfd(struct disassemble_info* dinfo,
477 void *stream,
478 fprintf_ftype fprintf_func,
479 bfd* abfd,
480 char* disassembler_options) {
481 init_disassemble_info(dinfo, stream, fprintf_func);
482
483 dinfo->flavour = bfd_get_flavour(abfd);
484 dinfo->arch = bfd_get_arch(abfd);
485 dinfo->mach = bfd_get_mach(abfd);
486 dinfo->disassembler_options = disassembler_options;
487 dinfo->octets_per_byte = bfd_octets_per_byte (abfd);
488 dinfo->skip_zeroes = sizeof(void*) * 2;
489 dinfo->skip_zeroes_at_end = sizeof(void*)-1;
490 dinfo->disassembler_needs_relocs = FALSE;
491
|
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.c -- dump a range of addresses as native instructions
26 This implements the plugin protocol required by the
27 HotSpot PrintAssembly option.
28 */
29
30 #include <libiberty.h>
31 #include <bfd.h>
32 #include <dis-asm.h>
33 #include <inttypes.h>
34 #include <string.h>
35 #include <errno.h>
36 #include "hsdis.h"
37
38 #ifndef bool
39 #define bool int
40 #define true 1
41 #define false 0
42 #endif /*bool*/
43
44 /* short names for stuff in hsdis.h */
45 typedef decode_instructions_event_callback_ftype event_callback_t;
46 typedef decode_instructions_printf_callback_ftype printf_callback_t;
47
48 /* disassemble_info.application_data object */
49 struct hsdis_app_data {
50 /* virtual address of data */
51 uintptr_t start_va, end_va;
52 /* the instructions to be decoded */
53 unsigned char* buffer;
54 uintptr_t length;
55 event_callback_t event_callback; void* event_stream;
56 printf_callback_t printf_callback; void* printf_stream;
57 bool losing;
58 bool do_newline;
59
60 /* the architecture being disassembled */
61 const char* arch_name;
62 const bfd_arch_info_type* arch_info;
63
64 /* the disassembler we are going to use: */
65 disassembler_ftype dfn;
66 struct disassemble_info dinfo; /* the actual struct! */
67
68 char mach_option[64];
69 char insn_options[256];
70 };
71
72 static void* decode(struct hsdis_app_data* app_data, const char* options);
73
74 #define DECL_APP_DATA(dinfo) \
75 struct hsdis_app_data* app_data = (struct hsdis_app_data*) (dinfo)->application_data
76
77 #define DECL_EVENT_CALLBACK(app_data) \
78 event_callback_t event_callback = (app_data)->event_callback; \
79 void* event_stream = (app_data)->event_stream
80
81 #define DECL_PRINTF_CALLBACK(app_data) \
82 printf_callback_t printf_callback = (app_data)->printf_callback; \
83 void* printf_stream = (app_data)->printf_stream
84
85
86 static void print_help(struct hsdis_app_data* app_data,
87 const char* msg, const char* arg);
88 static void setup_app_data(struct hsdis_app_data* app_data,
89 const char* options);
90 static const char* format_insn_close(const char* close,
91 disassemble_info* dinfo,
92 char* buf, size_t bufsize);
93
94 /* This is the compatability interface for older version of hotspot */
95 void*
96 #ifdef DLL_ENTRY
97 DLL_ENTRY
98 #endif
99 decode_instructions(void* start_pv, void* end_pv,
100 event_callback_t event_callback_arg, void* event_stream_arg,
101 printf_callback_t printf_callback_arg, void* printf_stream_arg,
102 const char* options) {
103 struct hsdis_app_data app_data;
104 memset(&app_data, 0, sizeof(app_data));
105 app_data.buffer = (unsigned char*) start_pv;
106 app_data.length = (uintptr_t)end_pv - (uintptr_t)start_pv;
107 app_data.start_va = (uintptr_t) start_pv;
108 app_data.end_va = app_data.start_va + app_data.length;
109 app_data.event_callback = event_callback_arg;
110 app_data.event_stream = event_stream_arg;
111 app_data.printf_callback = printf_callback_arg;
112 app_data.printf_stream = printf_stream_arg;
113 app_data.do_newline = true;
114
115 return decode(&app_data, options);
116 }
117
118 void*
119 #ifdef DLL_ENTRY
120 DLL_ENTRY
121 #endif
122 decode_instructions_virtual(uintptr_t start_va, uintptr_t end_va,
123 unsigned char* buffer, uintptr_t length,
124 event_callback_t event_callback_arg, void* event_stream_arg,
125 printf_callback_t printf_callback_arg, void* printf_stream_arg,
126 const char* options) {
127 struct hsdis_app_data app_data;
128 memset(&app_data, 0, sizeof(app_data));
129 app_data.start_va = start_va;
130 app_data.end_va = end_va;
131 app_data.buffer = buffer;
132 app_data.length = length;
133 app_data.event_callback = event_callback_arg;
134 app_data.event_stream = event_stream_arg;
135 app_data.printf_callback = printf_callback_arg;
136 app_data.printf_stream = printf_stream_arg;
137 app_data.do_newline = false;
138
139 return decode(&app_data, options);
140 }
141
142 static void* decode(struct hsdis_app_data* app_data, const char* options) {
143 setup_app_data(app_data, options);
144 char buf[128];
145
146 {
147 /* now reload everything from app_data: */
148 DECL_EVENT_CALLBACK(app_data);
149 DECL_PRINTF_CALLBACK(app_data);
150 uintptr_t start = app_data->start_va;
151 uintptr_t end = app_data->end_va;
152 uintptr_t p = start;
153
154 (*event_callback)(event_stream, "insns", (void*)start);
155
156 (*event_callback)(event_stream, "mach name='%s'",
157 (void*) app_data->arch_info->printable_name);
158 if (app_data->dinfo.bytes_per_line != 0) {
159 (*event_callback)(event_stream, "format bytes-per-line='%p'/",
160 (void*)(intptr_t) app_data->dinfo.bytes_per_line);
161 }
162
163 while (p < end && !app_data->losing) {
164 (*event_callback)(event_stream, "insn", (void*) p);
165
166 /* reset certain state, so we can read it with confidence */
167 app_data->dinfo.insn_info_valid = 0;
168 app_data->dinfo.branch_delay_insns = 0;
169 app_data->dinfo.data_size = 0;
170 app_data->dinfo.insn_type = 0;
171
172 int size = (*app_data->dfn)((bfd_vma) p, &app_data->dinfo);
173
174 if (size > 0) p += size;
175 else app_data->losing = true;
176
177 if (!app_data->losing) {
178 const char* insn_close = format_insn_close("/insn", &app_data->dinfo,
179 buf, sizeof(buf));
180 (*event_callback)(event_stream, insn_close, (void*) p) != NULL;
181
182 if (app_data->do_newline) {
183 /* follow each complete insn by a nice newline */
184 (*printf_callback)(printf_stream, "\n");
185 }
186 }
187 }
188
189 (*event_callback)(event_stream, "/insns", (void*) p);
190 return (void*) p;
191 }
192 }
193
194 /* take the address of the function, for luck, and also test the typedef: */
195 const decode_instructions_ftype decode_instructions_address = &decode_instructions_virtual;
196
197 static const char* format_insn_close(const char* close,
198 disassemble_info* dinfo,
199 char* buf, size_t bufsize) {
200 if (!dinfo->insn_info_valid)
201 return close;
202 enum dis_insn_type itype = dinfo->insn_type;
203 int dsize = dinfo->data_size, delays = dinfo->branch_delay_insns;
204 if ((itype == dis_nonbranch && (dsize | delays) == 0)
205 || (strlen(close) + 3*20 > bufsize))
206 return close;
207
208 const char* type = "unknown";
209 switch (itype) {
210 case dis_nonbranch: type = NULL; break;
211 case dis_branch: type = "branch"; break;
212 case dis_condbranch: type = "condbranch"; break;
213 case dis_jsr: type = "jsr"; break;
214 case dis_condjsr: type = "condjsr"; break;
215 case dis_dref: type = "dref"; break;
216 case dis_dref2: type = "dref2"; break;
217 }
218
219 strcpy(buf, close);
220 char* p = buf;
221 if (type) sprintf(p += strlen(p), " type='%s'", type);
222 if (dsize) sprintf(p += strlen(p), " dsize='%d'", dsize);
223 if (delays) sprintf(p += strlen(p), " delay='%d'", delays);
224 return buf;
225 }
226
227 /* handler functions */
228
229 static int
230 hsdis_read_memory_func(bfd_vma memaddr,
231 bfd_byte* myaddr,
232 unsigned int length,
233 struct disassemble_info* dinfo) {
234 DECL_APP_DATA(dinfo);
235 /* convert the virtual address memaddr into an address within memory buffer */
236 uintptr_t offset = ((uintptr_t) memaddr) - app_data->start_va;
237 if (offset + length > app_data->length) {
238 /* read is out of bounds */
239 return EIO;
240 } else {
241 memcpy(myaddr, (bfd_byte*) (app_data->buffer + offset), length);
242 return 0;
243 }
244 }
245
246 static void
247 hsdis_print_address_func(bfd_vma vma, struct disassemble_info* dinfo) {
248 /* the actual value to print: */
249 void* addr_value = (void*) (uintptr_t) vma;
250 DECL_APP_DATA(dinfo);
251 DECL_EVENT_CALLBACK(app_data);
252
253 /* issue the event: */
254 void* result =
255 (*event_callback)(event_stream, "addr/", addr_value);
256 if (result == NULL) {
257 /* event declined */
258 generic_print_address(vma, dinfo);
259 }
260 }
261
383 }
384
385 static void parse_caller_options(struct hsdis_app_data* app_data, const char* caller_options) {
386 char* iop_base = app_data->insn_options;
387 char* iop_limit = iop_base + sizeof(app_data->insn_options) - 1;
388 char* iop = iop_base;
389 const char* p;
390 for (p = caller_options; p != NULL; ) {
391 const char* q = strchr(p, ',');
392 size_t plen = (q == NULL) ? strlen(p) : ((q++) - p);
393 if (plen == 4 && strncmp(p, "help", plen) == 0) {
394 print_help(app_data, NULL, NULL);
395 } else if (plen >= 5 && strncmp(p, "mach=", 5) == 0) {
396 char* mach_option = app_data->mach_option;
397 size_t mach_size = sizeof(app_data->mach_option);
398 mach_size -= 1; /*leave room for the null*/
399 if (plen > mach_size) plen = mach_size;
400 strncpy(mach_option, p, plen);
401 mach_option[plen] = '\0';
402 } else if (plen > 6 && strncmp(p, "hsdis-", 6)) {
403 /* do not pass these to the next level */
404 } else {
405 /* just copy it; {i386,sparc}-dis.c might like to see it */
406 if (iop > iop_base && iop < iop_limit) (*iop++) = ',';
407 if (iop + plen > iop_limit)
408 plen = iop_limit - iop;
409 strncpy(iop, p, plen);
410 iop += plen;
411 }
412 p = q;
413 }
414 }
415
416 static void print_help(struct hsdis_app_data* app_data,
417 const char* msg, const char* arg) {
418 DECL_PRINTF_CALLBACK(app_data);
419 if (msg != NULL) {
420 (*printf_callback)(printf_stream, "hsdis: ");
421 (*printf_callback)(printf_stream, msg, arg);
422 (*printf_callback)(printf_stream, "\n");
423 }
494 static int print_to_dev_null(void* ignore_stream, const char* ignore_format, ...) {
495 return 0;
496 }
497
498 /* Prime the pump by running the selected disassembler on a null input.
499 This forces the machine-specific disassembler to divulge invariant
500 information like bytes_per_line.
501 */
502 static void parse_fake_insn(disassembler_ftype dfn,
503 struct disassemble_info* dinfo) {
504 typedef int (*read_memory_ftype)
505 (bfd_vma memaddr, bfd_byte *myaddr, unsigned int length,
506 struct disassemble_info *info);
507 read_memory_ftype read_memory_func = dinfo->read_memory_func;
508 fprintf_ftype fprintf_func = dinfo->fprintf_func;
509
510 dinfo->read_memory_func = &read_zero_data_only;
511 dinfo->fprintf_func = &print_to_dev_null;
512 (*dfn)(0, dinfo);
513
514 /* put it back */
515 dinfo->read_memory_func = read_memory_func;
516 dinfo->fprintf_func = fprintf_func;
517 }
518
519 static void init_disassemble_info_from_bfd(struct disassemble_info* dinfo,
520 void *stream,
521 fprintf_ftype fprintf_func,
522 bfd* abfd,
523 char* disassembler_options) {
524 init_disassemble_info(dinfo, stream, fprintf_func);
525
526 dinfo->flavour = bfd_get_flavour(abfd);
527 dinfo->arch = bfd_get_arch(abfd);
528 dinfo->mach = bfd_get_mach(abfd);
529 dinfo->disassembler_options = disassembler_options;
530 dinfo->octets_per_byte = bfd_octets_per_byte (abfd);
531 dinfo->skip_zeroes = sizeof(void*) * 2;
532 dinfo->skip_zeroes_at_end = sizeof(void*)-1;
533 dinfo->disassembler_needs_relocs = FALSE;
534
|