gallivm: JIT symbol resolution with linux perf.
[mesa.git] / src / gallium / auxiliary / gallivm / lp_bld_debug.cpp
1 /**************************************************************************
2 *
3 * Copyright 2009-2011 VMware, Inc.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 #include <stddef.h>
29
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>
36
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 */
42
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 */
48
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 */
58
59 #include "util/u_math.h"
60 #include "util/u_debug.h"
61
62 #include "lp_bld_debug.h"
63
64 #ifdef __linux__
65 #include <sys/stat.h>
66 #include <fcntl.h>
67 #endif
68
69
70
71 /**
72 * Check alignment.
73 *
74 * It is important that this check is not implemented as a macro or inlined
75 * function, as the compiler assumptions in respect to alignment of global
76 * and stack variables would often make the check a no op, defeating the
77 * whole purpose of the exercise.
78 */
79 extern "C" boolean
80 lp_check_alignment(const void *ptr, unsigned alignment)
81 {
82 assert(util_is_power_of_two(alignment));
83 return ((uintptr_t)ptr & (alignment - 1)) == 0;
84 }
85
86
87 class raw_debug_ostream :
88 public llvm::raw_ostream
89 {
90 private:
91 uint64_t pos;
92
93 public:
94 raw_debug_ostream() : pos(0) { }
95
96 void write_impl(const char *Ptr, size_t Size);
97
98 #if HAVE_LLVM >= 0x207
99 uint64_t current_pos() const { return pos; }
100 size_t preferred_buffer_size() const { return 512; }
101 #else
102 uint64_t current_pos() { return pos; }
103 size_t preferred_buffer_size() { return 512; }
104 #endif
105 };
106
107
108 void
109 raw_debug_ostream::write_impl(const char *Ptr, size_t Size)
110 {
111 if (Size > 0) {
112 char *lastPtr = (char *)&Ptr[Size];
113 char last = *lastPtr;
114 *lastPtr = 0;
115 _debug_printf("%*s", Size, Ptr);
116 *lastPtr = last;
117 pos += Size;
118 }
119 }
120
121
122 /**
123 * Same as LLVMDumpValue, but through our debugging channels.
124 */
125 extern "C" void
126 lp_debug_dump_value(LLVMValueRef value)
127 {
128 #if (defined(PIPE_OS_WINDOWS) && !defined(PIPE_CC_MSVC)) || defined(PIPE_OS_EMBDDED)
129 raw_debug_ostream os;
130 llvm::unwrap(value)->print(os);
131 os.flush();
132 #else
133 LLVMDumpValue(value);
134 #endif
135 }
136
137
138 #if HAVE_LLVM >= 0x0207
139 /*
140 * MemoryObject wrapper around a buffer of memory, to be used by MC
141 * disassembler.
142 */
143 class BufferMemoryObject:
144 public llvm::MemoryObject
145 {
146 private:
147 const uint8_t *Bytes;
148 uint64_t Length;
149 public:
150 BufferMemoryObject(const uint8_t *bytes, uint64_t length) :
151 Bytes(bytes), Length(length)
152 {
153 }
154
155 uint64_t getBase() const
156 {
157 return 0;
158 }
159
160 uint64_t getExtent() const
161 {
162 return Length;
163 }
164
165 int readByte(uint64_t addr, uint8_t *byte) const
166 {
167 if (addr > getExtent())
168 return -1;
169 *byte = Bytes[addr];
170 return 0;
171 }
172 };
173 #endif /* HAVE_LLVM >= 0x0207 */
174
175
176 /*
177 * Disassemble a function, using the LLVM MC disassembler.
178 *
179 * See also:
180 * - http://blog.llvm.org/2010/01/x86-disassembler.html
181 * - http://blog.llvm.org/2010/04/intro-to-llvm-mc-project.html
182 */
183 static size_t
184 disassemble(const void* func, llvm::raw_ostream & Out)
185 {
186 #if HAVE_LLVM >= 0x0207
187 using namespace llvm;
188
189 const uint8_t *bytes = (const uint8_t *)func;
190
191 /*
192 * Limit disassembly to this extent
193 */
194 const uint64_t extent = 96 * 1024;
195
196 uint64_t max_pc = 0;
197
198 /*
199 * Initialize all used objects.
200 */
201
202 #if HAVE_LLVM >= 0x0301
203 std::string Triple = sys::getDefaultTargetTriple();
204 #else
205 std::string Triple = sys::getHostTriple();
206 #endif
207
208 std::string Error;
209 const Target *T = TargetRegistry::lookupTarget(Triple, Error);
210
211 #if HAVE_LLVM >= 0x0300
212 OwningPtr<const MCAsmInfo> AsmInfo(T->createMCAsmInfo(Triple));
213 #else
214 OwningPtr<const MCAsmInfo> AsmInfo(T->createAsmInfo(Triple));
215 #endif
216
217 if (!AsmInfo) {
218 Out << "error: no assembly info for target " << Triple << "\n";
219 return 0;
220 }
221
222 #if HAVE_LLVM >= 0x0300
223 const MCSubtargetInfo *STI = T->createMCSubtargetInfo(Triple, sys::getHostCPUName(), "");
224 OwningPtr<const MCDisassembler> DisAsm(T->createMCDisassembler(*STI));
225 #else
226 OwningPtr<const MCDisassembler> DisAsm(T->createMCDisassembler());
227 #endif
228 if (!DisAsm) {
229 Out << "error: no disassembler for target " << Triple << "\n";
230 return 0;
231 }
232
233 #if HAVE_LLVM >= 0x0300
234 unsigned int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
235 #else
236 int AsmPrinterVariant = AsmInfo->getAssemblerDialect();
237 #endif
238
239 #if HAVE_LLVM >= 0x0301
240 OwningPtr<const MCRegisterInfo> MRI(T->createMCRegInfo(Triple));
241 if (!MRI) {
242 Out << "error: no register info for target " << Triple.c_str() << "\n";
243 return 0;
244 }
245
246 OwningPtr<const MCInstrInfo> MII(T->createMCInstrInfo());
247 if (!MII) {
248 Out << "error: no instruction info for target " << Triple.c_str() << "\n";
249 return 0;
250 }
251 #endif
252
253 #if HAVE_LLVM >= 0x0301
254 OwningPtr<MCInstPrinter> Printer(
255 T->createMCInstPrinter(AsmPrinterVariant, *AsmInfo, *MII, *MRI, *STI));
256 #elif HAVE_LLVM == 0x0300
257 OwningPtr<MCInstPrinter> Printer(
258 T->createMCInstPrinter(AsmPrinterVariant, *AsmInfo, *STI));
259 #elif HAVE_LLVM >= 0x0208
260 OwningPtr<MCInstPrinter> Printer(
261 T->createMCInstPrinter(AsmPrinterVariant, *AsmInfo));
262 #else
263 OwningPtr<MCInstPrinter> Printer(
264 T->createMCInstPrinter(AsmPrinterVariant, *AsmInfo, Out));
265 #endif
266 if (!Printer) {
267 Out << "error: no instruction printer for target " << Triple.c_str() << "\n";
268 return 0;
269 }
270
271 #if HAVE_LLVM >= 0x0301
272 TargetOptions options;
273 #if defined(DEBUG)
274 options.JITEmitDebugInfo = true;
275 #endif
276 #if defined(PIPE_ARCH_X86)
277 options.StackAlignmentOverride = 4;
278 #endif
279 #if defined(DEBUG) || defined(PROFILE)
280 options.NoFramePointerElim = true;
281 #endif
282 TargetMachine *TM = T->createTargetMachine(Triple, sys::getHostCPUName(), "", options);
283 #elif HAVE_LLVM == 0x0300
284 TargetMachine *TM = T->createTargetMachine(Triple, sys::getHostCPUName(), "");
285 #else
286 TargetMachine *TM = T->createTargetMachine(Triple, "");
287 #endif
288
289 const TargetInstrInfo *TII = TM->getInstrInfo();
290
291 /*
292 * Wrap the data in a MemoryObject
293 */
294 BufferMemoryObject memoryObject((const uint8_t *)bytes, extent);
295
296 uint64_t pc;
297 pc = 0;
298 while (true) {
299 MCInst Inst;
300 uint64_t Size;
301
302 /*
303 * Print address. We use addresses relative to the start of the function,
304 * so that between runs.
305 */
306
307 Out << llvm::format("%6lu:\t", (unsigned long)pc);
308
309 if (!DisAsm->getInstruction(Inst, Size, memoryObject,
310 pc,
311 #if HAVE_LLVM >= 0x0300
312 nulls(), nulls())) {
313 #else
314 nulls())) {
315 #endif
316 Out << "invalid";
317 pc += 1;
318 }
319
320 /*
321 * Output the bytes in hexidecimal format.
322 */
323
324 if (0) {
325 unsigned i;
326 for (i = 0; i < Size; ++i) {
327 Out << llvm::format("%02x ", ((const uint8_t*)bytes)[pc + i]);
328 }
329 for (; i < 16; ++i) {
330 Out << " ";
331 }
332 }
333
334 /*
335 * Print the instruction.
336 */
337 #if HAVE_LLVM >= 0x0300
338 Printer->printInst(&Inst, Out, "");
339 #elif HAVE_LLVM >= 0x208
340 Printer->printInst(&Inst, Out);
341 #else
342 Printer->printInst(&Inst);
343 #endif
344
345 /*
346 * Advance.
347 */
348
349 pc += Size;
350
351 #if HAVE_LLVM >= 0x0300
352 const MCInstrDesc &TID = TII->get(Inst.getOpcode());
353 #else
354 const TargetInstrDesc &TID = TII->get(Inst.getOpcode());
355 #endif
356
357 /*
358 * Keep track of forward jumps to a nearby address.
359 */
360
361 if (TID.isBranch()) {
362 for (unsigned i = 0; i < Inst.getNumOperands(); ++i) {
363 const MCOperand &operand = Inst.getOperand(i);
364 if (operand.isImm()) {
365 uint64_t jump;
366
367 /*
368 * FIXME: Handle both relative and absolute addresses correctly.
369 * EDInstInfo actually has this info, but operandTypes and
370 * operandFlags enums are not exposed in the public interface.
371 */
372
373 if (1) {
374 /*
375 * PC relative addr.
376 */
377
378 jump = pc + operand.getImm();
379 } else {
380 /*
381 * Absolute addr.
382 */
383
384 jump = (uint64_t)operand.getImm();
385 }
386
387 /*
388 * Output the address relative to the function start, given
389 * that MC will print the addresses relative the current pc.
390 */
391 Out << "\t\t; " << jump;
392
393 /*
394 * Ignore far jumps given it could be actually a tail return to
395 * a random address.
396 */
397
398 if (jump > max_pc &&
399 jump < extent) {
400 max_pc = jump;
401 }
402 }
403 }
404 }
405
406 Out << "\n";
407
408 /*
409 * Stop disassembling on return statements, if there is no record of a
410 * jump to a successive address.
411 */
412
413 if (TID.isReturn()) {
414 if (pc > max_pc) {
415 break;
416 }
417 }
418 }
419
420 /*
421 * Print GDB command, useful to verify output.
422 */
423
424 if (0) {
425 _debug_printf("disassemble %p %p\n", bytes, bytes + pc);
426 }
427
428 Out << "\n";
429 Out.flush();
430
431 return pc;
432 #else /* HAVE_LLVM < 0x0207 */
433 (void)func;
434 return 0;
435 #endif /* HAVE_LLVM < 0x0207 */
436 }
437
438
439 extern "C" void
440 lp_disassemble(LLVMValueRef func, const void *code) {
441 raw_debug_ostream Out;
442 disassemble(code, Out);
443 }
444
445
446 /*
447 * Linux perf profiler integration.
448 *
449 * See also:
450 * - http://penberg.blogspot.co.uk/2009/06/jato-has-profiler.html
451 * - https://github.com/penberg/jato/commit/73ad86847329d99d51b386f5aba692580d1f8fdc
452 * - http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=commitdiff;h=80d496be89ed7dede5abee5c057634e80a31c82d
453 */
454 extern "C" void
455 lp_profile(LLVMValueRef func, const void *code)
456 {
457 #if defined(__linux__) && (defined(DEBUG) || defined(PROFILE))
458 static boolean first_time = TRUE;
459 static FILE *perf_map_file = NULL;
460 static int perf_asm_fd = -1;
461 if (first_time) {
462 /*
463 * We rely on the disassembler for determining a function's size, but
464 * the disassembly is a leaky and slow operation, so avoid running
465 * this except when running inside linux perf, which can be inferred
466 * by the PERF_BUILDID_DIR environment variable.
467 */
468 if (getenv("PERF_BUILDID_DIR")) {
469 pid_t pid = getpid();
470 char filename[256];
471 util_snprintf(filename, sizeof filename, "/tmp/perf-%llu.map", (unsigned long long)pid);
472 perf_map_file = fopen(filename, "wt");
473 util_snprintf(filename, sizeof filename, "/tmp/perf-%llu.map.asm", (unsigned long long)pid);
474 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
475 perf_asm_fd = open(filename, O_WRONLY | O_CREAT, mode);
476 }
477 first_time = FALSE;
478 }
479 if (perf_map_file) {
480 const char *symbol = LLVMGetValueName(func);
481 unsigned long addr = (uintptr_t)code;
482 llvm::raw_fd_ostream Out(perf_asm_fd, false);
483 Out << symbol << ":\n";
484 unsigned long size = disassemble(code, Out);
485 fprintf(perf_map_file, "%lx %lx %s\n", addr, size, symbol);
486 fflush(perf_map_file);
487 }
488 #else
489 (void)func;
490 (void)code;
491 #endif
492 }
493
494