1 /**************************************************************************
3 * Copyright 2009 VMware, Inc.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
33 #include "util/u_math.h"
34 #include "util/u_debug.h"
35 #include "lp_bld_debug.h"
41 * It is important that this check is not implemented as a macro or inlined
42 * function, as the compiler assumptions in respect to alignment of global
43 * and stack variables would often make the check a no op, defeating the
44 * whole purpose of the exercise.
47 lp_check_alignment(const void *ptr
, unsigned alignment
)
49 assert(util_is_power_of_two(alignment
));
50 return ((uintptr_t)ptr
& (alignment
- 1)) == 0;
55 lp_disassemble(const void* func
)
61 boolean emit_addrs
= TRUE
, emit_line_nos
= FALSE
;
65 ud_set_input_buffer(&ud_obj
, (void*)func
, 0xffff);
67 max_jmp_pc
= (uint64_t) (uintptr_t) func
;
68 ud_set_pc(&ud_obj
, max_jmp_pc
);
71 ud_set_mode(&ud_obj
, 32);
73 #ifdef PIPE_ARCH_X86_64
74 ud_set_mode(&ud_obj
, 64);
77 ud_set_syntax(&ud_obj
, UD_SYN_ATT
);
79 while (ud_disassemble(&ud_obj
)) {
83 debug_printf("0x%08lx:\t", (unsigned long)ud_insn_off(&ud_obj
));
85 #ifdef PIPE_ARCH_X86_64
86 debug_printf("0x%016llx:\t", (unsigned long long)ud_insn_off(&ud_obj
));
89 else if (emit_line_nos
) {
90 debug_printf("%6d:\t", inst_no
);
94 debug_printf("%-16s ", ud_insn_hex(&ud_obj
));
97 debug_printf("%s\n", ud_insn_asm(&ud_obj
));
99 if(ud_obj
.mnemonic
!= UD_Icall
) {
101 for(i
= 0; i
< 3; ++i
) {
102 const struct ud_operand
*op
= &ud_obj
.operand
[i
];
103 if (op
->type
== UD_OP_JIMM
){
104 uint64_t pc
= ud_obj
.pc
;
108 pc
+= op
->lval
.sbyte
;
111 pc
+= op
->lval
.sword
;
114 pc
+= op
->lval
.sdword
;
125 if (ud_obj
.mnemonic
== UD_Iinvalid
||
126 (ud_insn_off(&ud_obj
) >= max_jmp_pc
&&
127 (ud_obj
.mnemonic
== UD_Iret
||
128 ud_obj
.mnemonic
== UD_Ijmp
)))
133 /* Print GDB command, useful to verify udis86 output */
134 debug_printf("disassemble %p %p\n", func
, (void*)(uintptr_t)ud_obj
.pc
);