1 /**************************************************************************
3 * Copyright 2009-2011 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 **************************************************************************/
28 #include <llvm-c/Core.h>
29 #include <llvm/Target/TargetMachine.h>
30 #include <llvm/Target/TargetRegistry.h>
31 #include <llvm/Target/TargetSelect.h>
32 #include <llvm/Target/TargetInstrInfo.h>
33 #include <llvm/Support/raw_ostream.h>
34 #include <llvm/Support/MemoryObject.h>
35 #include <llvm/System/Host.h>
37 #if HAVE_LLVM >= 0x0207
38 #include <llvm/MC/MCDisassembler.h>
39 #include <llvm/MC/MCAsmInfo.h>
40 #include <llvm/MC/MCInst.h>
41 #include <llvm/MC/MCInstPrinter.h>
42 #endif /* HAVE_LLVM >= 0x0207 */
44 #include "util/u_math.h"
45 #include "util/u_debug.h"
47 #include "lp_bld_debug.h"
54 * It is important that this check is not implemented as a macro or inlined
55 * function, as the compiler assumptions in respect to alignment of global
56 * and stack variables would often make the check a no op, defeating the
57 * whole purpose of the exercise.
60 lp_check_alignment(const void *ptr
, unsigned alignment
)
62 assert(util_is_power_of_two(alignment
));
63 return ((uintptr_t)ptr
& (alignment
- 1)) == 0;
67 class raw_debug_ostream
:
68 public llvm::raw_ostream
72 void write_impl(const char *Ptr
, size_t Size
);
73 uint64_t current_pos() { return pos
; }
74 uint64_t current_pos() const { return pos
; }
76 #if HAVE_LLVM >= 0x207
77 uint64_t preferred_buffer_size() { return 512; }
79 size_t preferred_buffer_size() { return 512; }
85 raw_debug_ostream::write_impl(const char *Ptr
, size_t Size
)
88 char *lastPtr
= (char *)&Ptr
[Size
];
91 _debug_printf("%*s", Size
, Ptr
);
99 * Same as LLVMDumpValue, but through our debugging channels.
102 lp_debug_dump_value(LLVMValueRef value
)
104 #if (defined(PIPE_OS_WINDOWS) && !defined(PIPE_CC_MSVC)) || defined(PIPE_OS_EMBDDED)
105 raw_debug_ostream os
;
106 llvm::unwrap(value
)->print(os
);
109 LLVMDumpValue(value
);
115 * MemoryObject wrapper around a buffer of memory, to be used by MC
118 class BufferMemoryObject
:
119 public llvm::MemoryObject
122 const uint8_t *Bytes
;
125 BufferMemoryObject(const uint8_t *bytes
, uint64_t length
) :
126 Bytes(bytes
), Length(length
)
130 uint64_t getBase() const
135 uint64_t getExtent() const
140 int readByte(uint64_t addr
, uint8_t *byte
) const
142 if (addr
> getExtent())
151 * Disassemble a function, using the LLVM MC disassembler.
154 * - http://blog.llvm.org/2010/01/x86-disassembler.html
155 * - http://blog.llvm.org/2010/04/intro-to-llvm-mc-project.html
158 lp_disassemble(const void* func
)
160 #if HAVE_LLVM >= 0x0207
161 using namespace llvm
;
163 const uint8_t *bytes
= (const uint8_t *)func
;
166 * Limit disassembly to this extent
168 const uint64_t extent
= 0x10000;
173 * Initialize all used objects.
176 std::string Triple
= sys::getHostTriple();
179 const Target
*T
= TargetRegistry::lookupTarget(Triple
, Error
);
181 #if HAVE_LLVM >= 0x0208
182 InitializeNativeTargetAsmPrinter();
184 InitializeAllAsmPrinters();
187 InitializeAllDisassemblers();
189 OwningPtr
<const MCAsmInfo
> AsmInfo(T
->createAsmInfo(Triple
));
192 debug_printf("error: no assembly info for target %s\n", Triple
.c_str());
196 OwningPtr
<const MCDisassembler
> DisAsm(T
->createMCDisassembler());
198 debug_printf("error: no disassembler for target %s\n", Triple
.c_str());
202 raw_debug_ostream Out
;
204 int AsmPrinterVariant
= AsmInfo
->getAssemblerDialect();
205 #if HAVE_LLVM >= 0x0208
206 OwningPtr
<MCInstPrinter
> Printer(
207 T
->createMCInstPrinter(AsmPrinterVariant
, *AsmInfo
));
209 OwningPtr
<MCInstPrinter
> Printer(
210 T
->createMCInstPrinter(AsmPrinterVariant
, *AsmInfo
, Out
));
213 debug_printf("error: no instruction printer for target %s\n", Triple
.c_str());
217 TargetMachine
*TM
= T
->createTargetMachine(Triple
, "");
219 const TargetInstrInfo
*TII
= TM
->getInstrInfo();
222 * Wrap the data in a MemoryObject
224 BufferMemoryObject
memoryObject((const uint8_t *)bytes
, extent
);
233 * Print address. We use addresses relative to the start of the function,
234 * so that between runs.
237 debug_printf("%6lu:\t", (unsigned long)pc
);
239 if (!DisAsm
->getInstruction(Inst
, Size
, memoryObject
,
242 debug_printf("invalid\n");
247 * Output the bytes in hexidecimal format.
252 for (i
= 0; i
< Size
; ++i
) {
253 debug_printf("%02x ", ((const uint8_t*)bytes
)[pc
+ i
]);
255 for (; i
< 16; ++i
) {
261 * Print the instruction.
264 #if HAVE_LLVM >= 0x208
265 Printer
->printInst(&Inst
, Out
);
267 Printer
->printInst(&Inst
);
277 const TargetInstrDesc
&TID
= TII
->get(Inst
.getOpcode());
280 * Keep track of forward jumps to a nearby address.
283 if (TID
.isBranch()) {
284 for (unsigned i
= 0; i
< Inst
.getNumOperands(); ++i
) {
285 const MCOperand
&operand
= Inst
.getOperand(i
);
286 if (operand
.isImm()) {
290 * FIXME: Handle both relative and absolute addresses correctly.
291 * EDInstInfo actually has this info, but operandTypes and
292 * operandFlags enums are not exposed in the public interface.
300 jump
= pc
+ operand
.getImm();
306 jump
= (uint64_t)operand
.getImm();
310 * Output the address relative to the function start, given
311 * that MC will print the addresses relative the current pc.
313 debug_printf("\t\t; %lu", (unsigned long)jump
);
316 * Ignore far jumps given it could be actually a tail return to
331 * Stop disassembling on return statements, if there is no record of a
332 * jump to a successive address.
335 if (TID
.isReturn()) {
343 * Print GDB command, useful to verify output.
347 debug_printf("disassemble %p %p\n", bytes
, bytes
+ pc
);