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 **************************************************************************/
30 #include <llvm-c/Core.h>
31 #include <llvm/Target/TargetMachine.h>
32 #include <llvm/Target/TargetInstrInfo.h>
33 #include <llvm/Support/raw_ostream.h>
34 #include <llvm/Support/Format.h>
35 #include <llvm/Support/MemoryObject.h>
37 #if HAVE_LLVM >= 0x0300
38 #include <llvm/Support/TargetRegistry.h>
39 #else /* HAVE_LLVM < 0x0300 */
40 #include <llvm/Target/TargetRegistry.h>
41 #endif /* HAVE_LLVM < 0x0300 */
43 #if HAVE_LLVM >= 0x0209
44 #include <llvm/Support/Host.h>
45 #else /* HAVE_LLVM < 0x0209 */
46 #include <llvm/System/Host.h>
47 #endif /* HAVE_LLVM < 0x0209 */
49 #if HAVE_LLVM >= 0x0207
50 #include <llvm/MC/MCDisassembler.h>
51 #include <llvm/MC/MCAsmInfo.h>
52 #include <llvm/MC/MCInst.h>
53 #include <llvm/MC/MCInstPrinter.h>
54 #endif /* HAVE_LLVM >= 0x0207 */
55 #if HAVE_LLVM >= 0x0301
56 #include <llvm/MC/MCRegisterInfo.h>
57 #endif /* HAVE_LLVM >= 0x0301 */
59 #if HAVE_LLVM >= 0x0303
60 #include <llvm/ADT/OwningPtr.h>
63 #include "util/u_math.h"
64 #include "util/u_debug.h"
66 #include "lp_bld_debug.h"
78 * It is important that this check is not implemented as a macro or inlined
79 * function, as the compiler assumptions in respect to alignment of global
80 * and stack variables would often make the check a no op, defeating the
81 * whole purpose of the exercise.
84 lp_check_alignment(const void *ptr
, unsigned alignment
)
86 assert(util_is_power_of_two(alignment
));
87 return ((uintptr_t)ptr
& (alignment
- 1)) == 0;
91 class raw_debug_ostream
:
92 public llvm::raw_ostream
98 raw_debug_ostream() : pos(0) { }
100 void write_impl(const char *Ptr
, size_t Size
);
102 #if HAVE_LLVM >= 0x207
103 uint64_t current_pos() const { return pos
; }
104 size_t preferred_buffer_size() const { return 512; }
106 uint64_t current_pos() { return pos
; }
107 size_t preferred_buffer_size() { return 512; }
113 raw_debug_ostream::write_impl(const char *Ptr
, size_t Size
)
116 char *lastPtr
= (char *)&Ptr
[Size
];
117 char last
= *lastPtr
;
119 _debug_printf("%*s", Size
, Ptr
);
127 * Same as LLVMDumpValue, but through our debugging channels.
130 lp_debug_dump_value(LLVMValueRef value
)
132 #if (defined(PIPE_OS_WINDOWS) && !defined(PIPE_CC_MSVC)) || defined(PIPE_OS_EMBDDED)
133 raw_debug_ostream os
;
134 llvm::unwrap(value
)->print(os
);
137 LLVMDumpValue(value
);
142 #if HAVE_LLVM >= 0x0207
144 * MemoryObject wrapper around a buffer of memory, to be used by MC
147 class BufferMemoryObject
:
148 public llvm::MemoryObject
151 const uint8_t *Bytes
;
154 BufferMemoryObject(const uint8_t *bytes
, uint64_t length
) :
155 Bytes(bytes
), Length(length
)
159 uint64_t getBase() const
164 uint64_t getExtent() const
169 int readByte(uint64_t addr
, uint8_t *byte
) const
171 if (addr
> getExtent())
177 #endif /* HAVE_LLVM >= 0x0207 */
181 * Disassemble a function, using the LLVM MC disassembler.
184 * - http://blog.llvm.org/2010/01/x86-disassembler.html
185 * - http://blog.llvm.org/2010/04/intro-to-llvm-mc-project.html
188 disassemble(const void* func
, llvm::raw_ostream
& Out
)
190 #if HAVE_LLVM >= 0x0207
191 using namespace llvm
;
193 const uint8_t *bytes
= (const uint8_t *)func
;
196 * Limit disassembly to this extent
198 const uint64_t extent
= 96 * 1024;
203 * Initialize all used objects.
206 #if HAVE_LLVM >= 0x0301
207 std::string Triple
= sys::getDefaultTargetTriple();
209 std::string Triple
= sys::getHostTriple();
213 const Target
*T
= TargetRegistry::lookupTarget(Triple
, Error
);
215 #if HAVE_LLVM >= 0x0304
216 OwningPtr
<const MCAsmInfo
> AsmInfo(T
->createMCAsmInfo(*T
->createMCRegInfo(Triple
), Triple
));
217 #elif HAVE_LLVM >= 0x0300
218 OwningPtr
<const MCAsmInfo
> AsmInfo(T
->createMCAsmInfo(Triple
));
220 OwningPtr
<const MCAsmInfo
> AsmInfo(T
->createAsmInfo(Triple
));
224 Out
<< "error: no assembly info for target " << Triple
<< "\n";
228 #if HAVE_LLVM >= 0x0300
229 const MCSubtargetInfo
*STI
= T
->createMCSubtargetInfo(Triple
, sys::getHostCPUName(), "");
230 OwningPtr
<const MCDisassembler
> DisAsm(T
->createMCDisassembler(*STI
));
232 OwningPtr
<const MCDisassembler
> DisAsm(T
->createMCDisassembler());
235 Out
<< "error: no disassembler for target " << Triple
<< "\n";
239 #if HAVE_LLVM >= 0x0300
240 unsigned int AsmPrinterVariant
= AsmInfo
->getAssemblerDialect();
242 int AsmPrinterVariant
= AsmInfo
->getAssemblerDialect();
245 #if HAVE_LLVM >= 0x0301
246 OwningPtr
<const MCRegisterInfo
> MRI(T
->createMCRegInfo(Triple
));
248 Out
<< "error: no register info for target " << Triple
.c_str() << "\n";
252 OwningPtr
<const MCInstrInfo
> MII(T
->createMCInstrInfo());
254 Out
<< "error: no instruction info for target " << Triple
.c_str() << "\n";
259 #if HAVE_LLVM >= 0x0301
260 OwningPtr
<MCInstPrinter
> Printer(
261 T
->createMCInstPrinter(AsmPrinterVariant
, *AsmInfo
, *MII
, *MRI
, *STI
));
262 #elif HAVE_LLVM == 0x0300
263 OwningPtr
<MCInstPrinter
> Printer(
264 T
->createMCInstPrinter(AsmPrinterVariant
, *AsmInfo
, *STI
));
265 #elif HAVE_LLVM >= 0x0208
266 OwningPtr
<MCInstPrinter
> Printer(
267 T
->createMCInstPrinter(AsmPrinterVariant
, *AsmInfo
));
269 OwningPtr
<MCInstPrinter
> Printer(
270 T
->createMCInstPrinter(AsmPrinterVariant
, *AsmInfo
, Out
));
273 Out
<< "error: no instruction printer for target " << Triple
.c_str() << "\n";
277 #if HAVE_LLVM >= 0x0301
278 TargetOptions options
;
280 options
.JITEmitDebugInfo
= true;
282 #if defined(PIPE_ARCH_X86)
283 options
.StackAlignmentOverride
= 4;
285 #if defined(DEBUG) || defined(PROFILE)
286 options
.NoFramePointerElim
= true;
288 TargetMachine
*TM
= T
->createTargetMachine(Triple
, sys::getHostCPUName(), "", options
);
289 #elif HAVE_LLVM == 0x0300
290 TargetMachine
*TM
= T
->createTargetMachine(Triple
, sys::getHostCPUName(), "");
292 TargetMachine
*TM
= T
->createTargetMachine(Triple
, "");
295 const TargetInstrInfo
*TII
= TM
->getInstrInfo();
298 * Wrap the data in a MemoryObject
300 BufferMemoryObject
memoryObject((const uint8_t *)bytes
, extent
);
309 * Print address. We use addresses relative to the start of the function,
310 * so that between runs.
313 Out
<< llvm::format("%6lu:\t", (unsigned long)pc
);
315 if (!DisAsm
->getInstruction(Inst
, Size
, memoryObject
,
317 #if HAVE_LLVM >= 0x0300
327 * Output the bytes in hexidecimal format.
332 for (i
= 0; i
< Size
; ++i
) {
333 Out
<< llvm::format("%02x ", ((const uint8_t*)bytes
)[pc
+ i
]);
335 for (; i
< 16; ++i
) {
341 * Print the instruction.
343 #if HAVE_LLVM >= 0x0300
344 Printer
->printInst(&Inst
, Out
, "");
345 #elif HAVE_LLVM >= 0x208
346 Printer
->printInst(&Inst
, Out
);
348 Printer
->printInst(&Inst
);
357 #if HAVE_LLVM >= 0x0300
358 const MCInstrDesc
&TID
= TII
->get(Inst
.getOpcode());
360 const TargetInstrDesc
&TID
= TII
->get(Inst
.getOpcode());
364 * Keep track of forward jumps to a nearby address.
367 if (TID
.isBranch()) {
368 for (unsigned i
= 0; i
< Inst
.getNumOperands(); ++i
) {
369 const MCOperand
&operand
= Inst
.getOperand(i
);
370 if (operand
.isImm()) {
374 * FIXME: Handle both relative and absolute addresses correctly.
375 * EDInstInfo actually has this info, but operandTypes and
376 * operandFlags enums are not exposed in the public interface.
384 jump
= pc
+ operand
.getImm();
390 jump
= (uint64_t)operand
.getImm();
394 * Output the address relative to the function start, given
395 * that MC will print the addresses relative the current pc.
397 Out
<< "\t\t; " << jump
;
400 * Ignore far jumps given it could be actually a tail return to
415 * Stop disassembling on return statements, if there is no record of a
416 * jump to a successive address.
419 if (TID
.isReturn()) {
427 * Print GDB command, useful to verify output.
431 _debug_printf("disassemble %p %p\n", bytes
, bytes
+ pc
);
438 #else /* HAVE_LLVM < 0x0207 */
441 #endif /* HAVE_LLVM < 0x0207 */
446 lp_disassemble(LLVMValueRef func
, const void *code
) {
447 raw_debug_ostream Out
;
448 disassemble(code
, Out
);
453 * Linux perf profiler integration.
456 * - http://penberg.blogspot.co.uk/2009/06/jato-has-profiler.html
457 * - https://github.com/penberg/jato/commit/73ad86847329d99d51b386f5aba692580d1f8fdc
458 * - http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=commitdiff;h=80d496be89ed7dede5abee5c057634e80a31c82d
461 lp_profile(LLVMValueRef func
, const void *code
)
463 #if defined(__linux__) && (defined(DEBUG) || defined(PROFILE))
464 static boolean first_time
= TRUE
;
465 static FILE *perf_map_file
= NULL
;
466 static int perf_asm_fd
= -1;
469 * We rely on the disassembler for determining a function's size, but
470 * the disassembly is a leaky and slow operation, so avoid running
471 * this except when running inside linux perf, which can be inferred
472 * by the PERF_BUILDID_DIR environment variable.
474 if (getenv("PERF_BUILDID_DIR")) {
475 pid_t pid
= getpid();
477 util_snprintf(filename
, sizeof filename
, "/tmp/perf-%llu.map", (unsigned long long)pid
);
478 perf_map_file
= fopen(filename
, "wt");
479 util_snprintf(filename
, sizeof filename
, "/tmp/perf-%llu.map.asm", (unsigned long long)pid
);
480 mode_t mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
;
481 perf_asm_fd
= open(filename
, O_WRONLY
| O_CREAT
, mode
);
486 const char *symbol
= LLVMGetValueName(func
);
487 unsigned long addr
= (uintptr_t)code
;
488 llvm::raw_fd_ostream
Out(perf_asm_fd
, false);
489 Out
<< symbol
<< ":\n";
490 unsigned long size
= disassemble(code
, Out
);
491 fprintf(perf_map_file
, "%lx %lx %s\n", addr
, size
, symbol
);
492 fflush(perf_map_file
);