2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
28 * Instruction disassembler.
31 #include <mach/boolean.h>
32 #include <machine/db_machdep.h>
34 #include <ddb/db_access.h>
35 #include <ddb/db_sym.h>
36 #include <ddb/db_output.h>
38 #include <kern/task.h>
39 #include <kern/misc_protos.h>
41 #include "ppc_disasm.h"
43 db_addr_t db_disasm_pc
, db_disasm_symaddr
;
44 boolean_t db_disasm_print_symaddr
;
47 * Disassemble instruction at 'loc'. 'altfmt' specifies an
48 * (optional) alternate format. Return address of start of
60 inst
= db_get_task_value(loc
, 4, FALSE
, task
);
62 db_disasm_print_symaddr
= FALSE
;
65 if (db_disasm_print_symaddr
) {
67 db_task_printsym(db_disasm_symaddr
, DB_STGY_ANY
, task
);
70 db_printf("\n"); /* Make sure we have a new line for multiline displays */
76 * Given four bytes of instruction (stored as an int, not an
77 * array of characters), compute if the instruction reads
85 db_printf("db_inst_load: coming soon in a debugger near you!\n");
88 unsigned char insb
, bits
;
92 bits
= db_ldstrtab
[insb
];
93 if (!(bits
& DBLS_LOAD
))
96 switch (bits
& DBLS_MODS
) {
101 return ((insb
& 0xc0) != 0xc0);
102 case DBLS_SECOND
|DBLS_MODRM
:
104 return ((insb
& 0xc0) != 0xc0 ? 2 : 0);
110 bits
= db_ldstrtab0f
[insb
];
113 return (db_inst_swreg(TRUE
, insw
, insb
));
115 panic ("db_inst_load: unknown mod bits");
122 * Given four bytes of instruction (stored as an int, not an
123 * array of characters), compute if the instruction writes
131 db_printf("db_inst_store: coming soon in a debugger near you!\n");
134 unsigned char insb
, bits
;
138 bits
= db_ldstrtab
[insb
];
139 if (!(bits
& DBLS_STORE
))
142 switch (bits
& DBLS_MODS
) {
147 return ((insb
& 0xc0) != 0xc0);
148 case DBLS_SECOND
|DBLS_MODRM
:
150 return ((insb
& 0xc0) != 0xc0 ? 2 : 0);
156 bits
= db_ldstrtab0f
[insb
];
159 return (db_inst_swreg(FALSE
, insw
, insb
));
161 panic ("db_inst_store: unknown mod bits");
168 * Extra routines for the automatically generated disassembler
179 sprintf(p
, "0x%lx", n
);
187 char *p
= dis_alloc(11);
188 sprintf(p
, "%lu", n
);
199 sign
= 1 << (nbits
- 1);
200 extended
= (displ
& sign
? displ
- (sign
<< 1) : displ
);
201 db_disasm_symaddr
= db_disasm_pc
+ (extended
<< 2);
202 db_disasm_print_symaddr
= TRUE
;
203 return hex(extended
<< 2);
210 return n
? "[reserved bits not zero]" : "";
213 size_t db_disasm_string_size
= 0;
214 #define DB_DISASM_STRING_MAXSIZE 4096
215 char db_disasm_string
[DB_DISASM_STRING_MAXSIZE
];
217 void *db_disasm_malloc(size_t size
); /* forward */
224 if (db_disasm_string_size
+ size
<= DB_DISASM_STRING_MAXSIZE
) {
225 new_buf
= (void *) (db_disasm_string
+ db_disasm_string_size
);
226 db_disasm_string_size
+= size
;
229 db_printf("db_disasm_malloc(size=%d) failed: %d left !\n",
231 DB_DISASM_STRING_MAXSIZE
- db_disasm_string_size
);