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 **************************************************************************/
33 #include <llvm-c/Core.h>
34 #include <llvm-c/Disassembler.h>
35 #include <llvm/Support/raw_ostream.h>
36 #include <llvm/Support/Format.h>
37 #include <llvm/Support/Host.h>
38 #include <llvm/IR/Module.h>
40 #include "util/u_math.h"
41 #include "util/u_debug.h"
43 #include "lp_bld_debug.h"
55 * It is important that this check is not implemented as a macro or inlined
56 * function, as the compiler assumptions in respect to alignment of global
57 * and stack variables would often make the check a no op, defeating the
58 * whole purpose of the exercise.
61 lp_check_alignment(const void *ptr
, unsigned alignment
)
63 assert(util_is_power_of_two(alignment
));
64 return ((uintptr_t)ptr
& (alignment
- 1)) == 0;
69 * Same as LLVMDumpValue, but through our debugging channels.
72 lp_debug_dump_value(LLVMValueRef value
)
74 #if HAVE_LLVM >= 0x0304
75 char *str
= LLVMPrintValueToString(value
);
78 LLVMDisposeMessage(str
);
80 #elif defined(PIPE_OS_WINDOWS) || defined(PIPE_OS_EMBEDDED)
82 llvm::raw_string_ostream
os(str
);
83 llvm::unwrap(value
)->print(os
);
84 os_log_message(str
.c_str());
92 * Disassemble a function, using the LLVM MC disassembler.
95 * - http://blog.llvm.org/2010/01/x86-disassembler.html
96 * - http://blog.llvm.org/2010/04/intro-to-llvm-mc-project.html
99 disassemble(const void* func
, std::ostream
&buffer
)
101 const uint8_t *bytes
= (const uint8_t *)func
;
104 * Limit disassembly to this extent
106 const uint64_t extent
= 96 * 1024;
109 * Initialize all used objects.
112 const char *triple
= LLVM_HOST_TRIPLE
;
113 LLVMDisasmContextRef D
= LLVMCreateDisasm(triple
, NULL
, 0, NULL
, NULL
);
117 buffer
<< "error: could not create disassembler for triple "
124 while (pc
< extent
) {
128 * Print address. We use addresses relative to the start of the function,
129 * so that between runs.
132 buffer
<< std::setw(6) << (unsigned long)pc
<< ":\t";
134 Size
= LLVMDisasmInstruction(D
, (uint8_t *)bytes
+ pc
, extent
- pc
, 0, outline
,
138 buffer
<< "invalid\n";
144 * Output the bytes in hexidecimal format.
149 for (i
= 0; i
< Size
; ++i
) {
150 buffer
<< std::hex
<< std::setfill('0') << std::setw(2)
151 << static_cast<int> (bytes
[pc
+ i
]);
153 for (; i
< 16; ++i
) {
154 buffer
<< std::dec
<< " ";
159 * Print the instruction.
162 buffer
<< std::setw(Size
) << outline
<< '\n';
165 * Stop disassembling on return statements, if there is no record of a
166 * jump to a successive address.
168 * XXX: This currently assumes x86
171 #if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)
172 if (Size
== 1 && bytes
[pc
] == 0xc3) {
184 buffer
<< "disassembly larger than " << extent
<< " bytes, aborting\n";
191 LLVMDisasmDispose(D
);
194 * Print GDB command, useful to verify output.
197 buffer
<< "disassemble " << static_cast<const void*>(bytes
) << ' '
198 << static_cast<const void*>(bytes
+ pc
) << '\n';
206 lp_disassemble(LLVMValueRef func
, const void *code
)
208 std::ostringstream buffer
;
211 buffer
<< LLVMGetValueName(func
) << ":\n";
212 disassemble(code
, buffer
);
214 os_log_message(s
.c_str());
215 os_log_message("\n");
220 * Linux perf profiler integration.
223 * - http://penberg.blogspot.co.uk/2009/06/jato-has-profiler.html
224 * - https://github.com/penberg/jato/commit/73ad86847329d99d51b386f5aba692580d1f8fdc
225 * - http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=commitdiff;h=80d496be89ed7dede5abee5c057634e80a31c82d
228 lp_profile(LLVMValueRef func
, const void *code
)
230 #if defined(__linux__) && defined(PROFILE)
231 static std::ofstream perf_asm_file
;
232 static boolean first_time
= TRUE
;
233 static FILE *perf_map_file
= NULL
;
236 * We rely on the disassembler for determining a function's size, but
237 * the disassembly is a leaky and slow operation, so avoid running
238 * this except when running inside linux perf, which can be inferred
239 * by the PERF_BUILDID_DIR environment variable.
241 if (getenv("PERF_BUILDID_DIR")) {
242 pid_t pid
= getpid();
244 util_snprintf(filename
, sizeof filename
, "/tmp/perf-%llu.map", (unsigned long long)pid
);
245 perf_map_file
= fopen(filename
, "wt");
246 util_snprintf(filename
, sizeof filename
, "/tmp/perf-%llu.map.asm", (unsigned long long)pid
);
247 perf_asm_file
.open(filename
);
252 const char *symbol
= LLVMGetValueName(func
);
253 unsigned long addr
= (uintptr_t)code
;
254 perf_asm_file
<< symbol
<< ":\n";
255 unsigned long size
= disassemble(code
, perf_asm_file
);
256 perf_asm_file
.flush();
257 fprintf(perf_map_file
, "%lx %lx %s\n", addr
, size
, symbol
);
258 fflush(perf_map_file
);