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
);
74 * Given four bytes of instruction (stored as an int, not an
75 * array of characters), compute if the instruction reads
83 db_printf("db_inst_load: coming soon in a debugger near you!\n");
86 unsigned char insb
, bits
;
90 bits
= db_ldstrtab
[insb
];
91 if (!(bits
& DBLS_LOAD
))
94 switch (bits
& DBLS_MODS
) {
99 return ((insb
& 0xc0) != 0xc0);
100 case DBLS_SECOND
|DBLS_MODRM
:
102 return ((insb
& 0xc0) != 0xc0 ? 2 : 0);
108 bits
= db_ldstrtab0f
[insb
];
111 return (db_inst_swreg(TRUE
, insw
, insb
));
113 panic ("db_inst_load: unknown mod bits");
120 * Given four bytes of instruction (stored as an int, not an
121 * array of characters), compute if the instruction writes
129 db_printf("db_inst_store: coming soon in a debugger near you!\n");
132 unsigned char insb
, bits
;
136 bits
= db_ldstrtab
[insb
];
137 if (!(bits
& DBLS_STORE
))
140 switch (bits
& DBLS_MODS
) {
145 return ((insb
& 0xc0) != 0xc0);
146 case DBLS_SECOND
|DBLS_MODRM
:
148 return ((insb
& 0xc0) != 0xc0 ? 2 : 0);
154 bits
= db_ldstrtab0f
[insb
];
157 return (db_inst_swreg(FALSE
, insw
, insb
));
159 panic ("db_inst_store: unknown mod bits");
166 * Extra routines for the automatically generated disassembler
177 sprintf(p
, "0x%lx", n
);
185 char *p
= dis_alloc(11);
186 sprintf(p
, "%lu", n
);
197 sign
= 1 << (nbits
- 1);
198 extended
= (displ
& sign
? displ
- (sign
<< 1) : displ
);
199 db_disasm_symaddr
= db_disasm_pc
+ (extended
<< 2);
200 db_disasm_print_symaddr
= TRUE
;
201 return hex(extended
<< 2);
208 return n
? "[reserved bits not zero]" : "";
211 size_t db_disasm_string_size
= 0;
212 #define DB_DISASM_STRING_MAXSIZE 4096
213 char db_disasm_string
[DB_DISASM_STRING_MAXSIZE
];
215 void *db_disasm_malloc(size_t size
); /* forward */
222 if (db_disasm_string_size
+ size
<= DB_DISASM_STRING_MAXSIZE
) {
223 new_buf
= (void *) (db_disasm_string
+ db_disasm_string_size
);
224 db_disasm_string_size
+= size
;
227 db_printf("db_disasm_malloc(size=%d) failed: %d left !\n",
229 DB_DISASM_STRING_MAXSIZE
- db_disasm_string_size
);