2 * Copyright 2008, François Revol, revol@free.fr
3 * Distributed under the terms of the MIT License.
7 #include <KernelExport.h>
11 #include "disasm_arch.h"
17 static addr_t sCurrentReadAddress
;
18 static void (*sSyntax
)(ud_t
*) = UD_SYN_ATT
;
19 static unsigned int sVendor
= UD_VENDOR_INTEL
;
23 read_next_byte(struct ud
*)
26 if (debug_memcpy(B_CURRENT_TEAM
, &buffer
, (void*)sCurrentReadAddress
, 1)
28 kprintf("<read fault>\n");
32 sCurrentReadAddress
++;
38 resolve_symbol(struct ud
*, uint64_t address
, int64_t* offset
)
40 const char* symbolName
;
44 if (IS_KERNEL_ADDRESS(address
)) {
45 error
= elf_debug_lookup_symbol_address(address
, &baseAddress
,
46 &symbolName
, NULL
, NULL
);
48 error
= elf_debug_lookup_user_symbol_address(
49 debug_get_debugged_thread()->team
, address
, &baseAddress
,
50 &symbolName
, NULL
, NULL
);
56 *offset
= address
- baseAddress
;
62 setup_disassembler(addr_t where
)
64 ud_set_input_hook(&sUDState
, &read_next_byte
);
65 sCurrentReadAddress
= where
;
66 ud_set_mode(&sUDState
, 32);
67 ud_set_pc(&sUDState
, (uint64_t)where
);
68 ud_set_syntax(&sUDState
, sSyntax
);
69 ud_set_vendor(&sUDState
, sVendor
);
70 ud_set_sym_resolver(&sUDState
, resolve_symbol
);
75 disasm_arch_assert(const char *condition
)
77 kprintf("assert: %s\n", condition
);
82 disasm_arch_dump_insns(addr_t where
, int count
, addr_t baseAddress
,
88 // count the instructions from base address to start address
89 setup_disassembler(baseAddress
);
90 addr_t address
= baseAddress
;
93 while (address
< where
&& (len
= ud_disassemble(&sUDState
)) >= 1) {
98 if (address
== where
) {
99 if (baseCount
> backCount
)
100 skipCount
= baseCount
- backCount
;
107 setup_disassembler(baseAddress
);
109 for (int i
= 0; i
< count
; i
++) {
111 ret
= ud_disassemble(&sUDState
);
120 addr_t address
= (addr_t
)ud_insn_off(&sUDState
);
121 if (address
== where
)
124 // TODO: dig operands and lookup symbols
125 kprintf("0x%08lx: %16.16s\t%s\n", address
, ud_insn_hex(&sUDState
),
126 ud_insn_asm(&sUDState
));
128 if (address
== where
)
139 // XXX: check for AMD and set sVendor;