2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
33 * Instruction disassembler.
36 #include <mach/boolean.h>
37 #include <machine/db_machdep.h>
39 #include <ddb/db_access.h>
40 #include <ddb/db_sym.h>
41 #include <ddb/db_output.h>
43 #include <kern/task.h>
44 #include <kern/misc_protos.h>
46 #include "ppc_disasm.h"
48 db_addr_t db_disasm_pc
, db_disasm_symaddr
;
49 boolean_t db_disasm_print_symaddr
;
52 * Disassemble instruction at 'loc'. 'altfmt' specifies an
53 * (optional) alternate format. Return address of start of
65 inst
= db_get_task_value(loc
, 4, FALSE
, task
);
67 db_disasm_print_symaddr
= FALSE
;
70 if (db_disasm_print_symaddr
) {
72 db_task_printsym(db_disasm_symaddr
, DB_STGY_ANY
, task
);
75 db_printf("\n"); /* Make sure we have a new line for multiline displays */
81 * Given four bytes of instruction (stored as an int, not an
82 * array of characters), compute if the instruction reads
90 db_printf("db_inst_load: coming soon in a debugger near you!\n");
93 unsigned char insb
, bits
;
97 bits
= db_ldstrtab
[insb
];
98 if (!(bits
& DBLS_LOAD
))
101 switch (bits
& DBLS_MODS
) {
106 return ((insb
& 0xc0) != 0xc0);
107 case DBLS_SECOND
|DBLS_MODRM
:
109 return ((insb
& 0xc0) != 0xc0 ? 2 : 0);
115 bits
= db_ldstrtab0f
[insb
];
118 return (db_inst_swreg(TRUE
, insw
, insb
));
120 panic ("db_inst_load: unknown mod bits");
127 * Given four bytes of instruction (stored as an int, not an
128 * array of characters), compute if the instruction writes
136 db_printf("db_inst_store: coming soon in a debugger near you!\n");
139 unsigned char insb
, bits
;
143 bits
= db_ldstrtab
[insb
];
144 if (!(bits
& DBLS_STORE
))
147 switch (bits
& DBLS_MODS
) {
152 return ((insb
& 0xc0) != 0xc0);
153 case DBLS_SECOND
|DBLS_MODRM
:
155 return ((insb
& 0xc0) != 0xc0 ? 2 : 0);
161 bits
= db_ldstrtab0f
[insb
];
164 return (db_inst_swreg(FALSE
, insw
, insb
));
166 panic ("db_inst_store: unknown mod bits");
173 * Extra routines for the automatically generated disassembler
184 sprintf(p
, "0x%lx", n
);
192 char *p
= dis_alloc(11);
193 sprintf(p
, "%lu", n
);
204 sign
= 1 << (nbits
- 1);
205 extended
= (displ
& sign
? displ
- (sign
<< 1) : displ
);
206 db_disasm_symaddr
= db_disasm_pc
+ (extended
<< 2);
207 db_disasm_print_symaddr
= TRUE
;
208 return hex(extended
<< 2);
215 return n
? "[reserved bits not zero]" : "";
218 size_t db_disasm_string_size
= 0;
219 #define DB_DISASM_STRING_MAXSIZE 4096
220 char db_disasm_string
[DB_DISASM_STRING_MAXSIZE
];
222 void *db_disasm_malloc(size_t size
); /* forward */
229 if (db_disasm_string_size
+ size
<= DB_DISASM_STRING_MAXSIZE
) {
230 new_buf
= (void *) (db_disasm_string
+ db_disasm_string_size
);
231 db_disasm_string_size
+= size
;
234 db_printf("db_disasm_malloc(size=%d) failed: %d left !\n",
236 DB_DISASM_STRING_MAXSIZE
- db_disasm_string_size
);