2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
27 * Instruction disassembler.
30 #include <mach/boolean.h>
31 #include <machine/db_machdep.h>
33 #include <ddb/db_access.h>
34 #include <ddb/db_sym.h>
35 #include <ddb/db_output.h>
37 #include <kern/task.h>
38 #include <kern/misc_protos.h>
40 #include "ppc_disasm.h"
42 db_addr_t db_disasm_pc
, db_disasm_symaddr
;
43 boolean_t db_disasm_print_symaddr
;
46 * Disassemble instruction at 'loc'. 'altfmt' specifies an
47 * (optional) alternate format. Return address of start of
59 inst
= db_get_task_value(loc
, 4, FALSE
, task
);
61 db_disasm_print_symaddr
= FALSE
;
64 if (db_disasm_print_symaddr
) {
66 db_task_printsym(db_disasm_symaddr
, DB_STGY_ANY
, task
);
69 db_printf("\n"); /* Make sure we have a new line for multiline displays */
75 * Given four bytes of instruction (stored as an int, not an
76 * array of characters), compute if the instruction reads
84 db_printf("db_inst_load: coming soon in a debugger near you!\n");
87 unsigned char insb
, bits
;
91 bits
= db_ldstrtab
[insb
];
92 if (!(bits
& DBLS_LOAD
))
95 switch (bits
& DBLS_MODS
) {
100 return ((insb
& 0xc0) != 0xc0);
101 case DBLS_SECOND
|DBLS_MODRM
:
103 return ((insb
& 0xc0) != 0xc0 ? 2 : 0);
109 bits
= db_ldstrtab0f
[insb
];
112 return (db_inst_swreg(TRUE
, insw
, insb
));
114 panic ("db_inst_load: unknown mod bits");
121 * Given four bytes of instruction (stored as an int, not an
122 * array of characters), compute if the instruction writes
130 db_printf("db_inst_store: coming soon in a debugger near you!\n");
133 unsigned char insb
, bits
;
137 bits
= db_ldstrtab
[insb
];
138 if (!(bits
& DBLS_STORE
))
141 switch (bits
& DBLS_MODS
) {
146 return ((insb
& 0xc0) != 0xc0);
147 case DBLS_SECOND
|DBLS_MODRM
:
149 return ((insb
& 0xc0) != 0xc0 ? 2 : 0);
155 bits
= db_ldstrtab0f
[insb
];
158 return (db_inst_swreg(FALSE
, insw
, insb
));
160 panic ("db_inst_store: unknown mod bits");
167 * Extra routines for the automatically generated disassembler
178 sprintf(p
, "0x%lx", n
);
186 char *p
= dis_alloc(11);
187 sprintf(p
, "%lu", n
);
198 sign
= 1 << (nbits
- 1);
199 extended
= (displ
& sign
? displ
- (sign
<< 1) : displ
);
200 db_disasm_symaddr
= db_disasm_pc
+ (extended
<< 2);
201 db_disasm_print_symaddr
= TRUE
;
202 return hex(extended
<< 2);
209 return n
? "[reserved bits not zero]" : "";
212 size_t db_disasm_string_size
= 0;
213 #define DB_DISASM_STRING_MAXSIZE 4096
214 char db_disasm_string
[DB_DISASM_STRING_MAXSIZE
];
216 void *db_disasm_malloc(size_t size
); /* forward */
223 if (db_disasm_string_size
+ size
<= DB_DISASM_STRING_MAXSIZE
) {
224 new_buf
= (void *) (db_disasm_string
+ db_disasm_string_size
);
225 db_disasm_string_size
+= size
;
228 db_printf("db_disasm_malloc(size=%d) failed: %d left !\n",
230 DB_DISASM_STRING_MAXSIZE
- db_disasm_string_size
);