2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
30 * Instruction disassembler.
33 #include <mach/boolean.h>
34 #include <machine/db_machdep.h>
36 #include <ddb/db_access.h>
37 #include <ddb/db_sym.h>
38 #include <ddb/db_output.h>
40 #include <kern/task.h>
41 #include <kern/misc_protos.h>
43 #include "ppc_disasm.h"
45 db_addr_t db_disasm_pc
, db_disasm_symaddr
;
46 boolean_t db_disasm_print_symaddr
;
49 * Disassemble instruction at 'loc'. 'altfmt' specifies an
50 * (optional) alternate format. Return address of start of
62 inst
= db_get_task_value(loc
, 4, FALSE
, task
);
64 db_disasm_print_symaddr
= FALSE
;
67 if (db_disasm_print_symaddr
) {
69 db_task_printsym(db_disasm_symaddr
, DB_STGY_ANY
, task
);
72 db_printf("\n"); /* Make sure we have a new line for multiline displays */
78 * Given four bytes of instruction (stored as an int, not an
79 * array of characters), compute if the instruction reads
87 db_printf("db_inst_load: coming soon in a debugger near you!\n");
90 unsigned char insb
, bits
;
94 bits
= db_ldstrtab
[insb
];
95 if (!(bits
& DBLS_LOAD
))
98 switch (bits
& DBLS_MODS
) {
103 return ((insb
& 0xc0) != 0xc0);
104 case DBLS_SECOND
|DBLS_MODRM
:
106 return ((insb
& 0xc0) != 0xc0 ? 2 : 0);
112 bits
= db_ldstrtab0f
[insb
];
115 return (db_inst_swreg(TRUE
, insw
, insb
));
117 panic ("db_inst_load: unknown mod bits");
124 * Given four bytes of instruction (stored as an int, not an
125 * array of characters), compute if the instruction writes
133 db_printf("db_inst_store: coming soon in a debugger near you!\n");
136 unsigned char insb
, bits
;
140 bits
= db_ldstrtab
[insb
];
141 if (!(bits
& DBLS_STORE
))
144 switch (bits
& DBLS_MODS
) {
149 return ((insb
& 0xc0) != 0xc0);
150 case DBLS_SECOND
|DBLS_MODRM
:
152 return ((insb
& 0xc0) != 0xc0 ? 2 : 0);
158 bits
= db_ldstrtab0f
[insb
];
161 return (db_inst_swreg(FALSE
, insw
, insb
));
163 panic ("db_inst_store: unknown mod bits");
170 * Extra routines for the automatically generated disassembler
181 sprintf(p
, "0x%lx", n
);
189 char *p
= dis_alloc(11);
190 sprintf(p
, "%lu", n
);
201 sign
= 1 << (nbits
- 1);
202 extended
= (displ
& sign
? displ
- (sign
<< 1) : displ
);
203 db_disasm_symaddr
= db_disasm_pc
+ (extended
<< 2);
204 db_disasm_print_symaddr
= TRUE
;
205 return hex(extended
<< 2);
212 return n
? "[reserved bits not zero]" : "";
215 size_t db_disasm_string_size
= 0;
216 #define DB_DISASM_STRING_MAXSIZE 4096
217 char db_disasm_string
[DB_DISASM_STRING_MAXSIZE
];
219 void *db_disasm_malloc(size_t size
); /* forward */
226 if (db_disasm_string_size
+ size
<= DB_DISASM_STRING_MAXSIZE
) {
227 new_buf
= (void *) (db_disasm_string
+ db_disasm_string_size
);
228 db_disasm_string_size
+= size
;
231 db_printf("db_disasm_malloc(size=%d) failed: %d left !\n",
233 DB_DISASM_STRING_MAXSIZE
- db_disasm_string_size
);