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 #include <fstream>
30 #include <sstream>
31 #include <iomanip>
32
33 #include <llvm/Config/llvm-config.h>
34 #include <llvm-c/Core.h>
35 #include <llvm-c/Disassembler.h>
36 #include <llvm/Support/raw_ostream.h>
37 #include <llvm/Support/Format.h>
38 #include <llvm/IR/Module.h>
39
40 #if LLVM_VERSION_MAJOR >= 17
41 #include <llvm/TargetParser/Host.h>
42 #else
43 #include <llvm/Support/Host.h>
44 #endif
45
46 #include "util/u_math.h"
47 #include "util/u_debug.h"
48
49 #include "lp_bld_debug.h"
50
51 #ifdef __linux__
52 #include <sys/stat.h>
53 #include <fcntl.h>
54 #endif
55
56
57
58 /**
59 * Check alignment.
60 *
61 * It is important that this check is not implemented as a macro or inlined
62 * function, as the compiler assumptions in respect to alignment of global
63 * and stack variables would often make the check a no op, defeating the
64 * whole purpose of the exercise.
65 */
66 extern "C" bool
lp_check_alignment(const void * ptr,unsigned alignment)67 lp_check_alignment(const void *ptr, unsigned alignment)
68 {
69 assert(util_is_power_of_two_or_zero(alignment));
70 return ((uintptr_t)ptr & (alignment - 1)) == 0;
71 }
72
73
74 /**
75 * Same as LLVMDumpValue, but through our debugging channels.
76 */
77 extern "C" void
lp_debug_dump_value(LLVMValueRef value)78 lp_debug_dump_value(LLVMValueRef value)
79 {
80 char *str = LLVMPrintValueToString(value);
81 if (str) {
82 os_log_message(str);
83 LLVMDisposeMessage(str);
84 }
85 }
86
87
88 /*
89 * Disassemble a function, using the LLVM MC disassembler.
90 *
91 * See also:
92 * - http://blog.llvm.org/2010/01/x86-disassembler.html
93 * - http://blog.llvm.org/2010/04/intro-to-llvm-mc-project.html
94 */
95 static size_t
disassemble(const void * func,std::ostream & buffer)96 disassemble(const void* func, std::ostream &buffer)
97 {
98 const uint8_t *bytes = (const uint8_t *)func;
99
100 /*
101 * Limit disassembly to this extent
102 */
103 const uint64_t extent = 96 * 1024;
104
105 /*
106 * Initialize all used objects.
107 */
108
109 const char *triple = LLVM_HOST_TRIPLE;
110 LLVMDisasmContextRef D = LLVMCreateDisasm(triple, NULL, 0, NULL, NULL);
111 char outline[1024];
112
113 if (!D) {
114 buffer << "error: could not create disassembler for triple "
115 << triple << '\n';
116 return 0;
117 }
118
119 uint64_t pc;
120 pc = 0;
121 while (pc < extent) {
122 size_t Size;
123
124 /*
125 * Print address. We use addresses relative to the start of the function,
126 * so that between runs.
127 */
128
129 buffer << std::setw(6) << (unsigned long)pc << ":\t";
130
131 Size = LLVMDisasmInstruction(D, (uint8_t *)bytes + pc, extent - pc, 0, outline,
132 sizeof outline);
133
134 if (!Size) {
135 buffer << "invalid\n";
136 pc += 1;
137 break;
138 }
139
140 /*
141 * Output the bytes in hexidecimal format.
142 */
143
144 if (0) {
145 unsigned i;
146 for (i = 0; i < Size; ++i) {
147 buffer << std::hex << std::setfill('0') << std::setw(2)
148 << static_cast<int> (bytes[pc + i]);
149 }
150 for (; i < 16; ++i) {
151 buffer << std::dec << " ";
152 }
153 }
154
155 /*
156 * Print the instruction.
157 */
158
159 buffer << std::setw(Size) << outline << '\n';
160
161 /*
162 * Stop disassembling on return statements, if there is no record of a
163 * jump to a successive address.
164 *
165 * XXX: This currently assumes x86
166 */
167
168 #if DETECT_ARCH_X86 || DETECT_ARCH_X86_64
169 if (Size == 1 && bytes[pc] == 0xc3) {
170 break;
171 }
172 #endif
173
174 /*
175 * Advance.
176 */
177
178 pc += Size;
179
180 if (pc >= extent) {
181 buffer << "disassembly larger than " << extent << " bytes, aborting\n";
182 break;
183 }
184 }
185
186 buffer << '\n';
187
188 LLVMDisasmDispose(D);
189
190 /*
191 * Print GDB command, useful to verify output.
192 */
193 if (0) {
194 buffer << "disassemble " << static_cast<const void*>(bytes) << ' '
195 << static_cast<const void*>(bytes + pc) << '\n';
196 }
197
198 return pc;
199 }
200
201
202 extern "C" void
lp_disassemble(LLVMValueRef func,const void * code)203 lp_disassemble(LLVMValueRef func, const void *code)
204 {
205 std::ostringstream buffer;
206 std::string s;
207
208 buffer << LLVMGetValueName(func) << ":\n";
209 disassemble(code, buffer);
210 s = buffer.str();
211 os_log_message(s.c_str());
212 os_log_message("\n");
213 }
214
215
216 /*
217 * Linux perf profiler integration.
218 *
219 * See also:
220 * - http://penberg.blogspot.co.uk/2009/06/jato-has-profiler.html
221 * - https://github.com/penberg/jato/commit/73ad86847329d99d51b386f5aba692580d1f8fdc
222 * - http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=commitdiff;h=80d496be89ed7dede5abee5c057634e80a31c82d
223 */
224 extern "C" void
lp_profile(LLVMValueRef func,const void * code)225 lp_profile(LLVMValueRef func, const void *code)
226 {
227 #if defined(__linux__) && defined(PROFILE)
228 static std::ofstream perf_asm_file;
229 static bool first_time = true;
230 static FILE *perf_map_file = NULL;
231 if (first_time) {
232 /*
233 * We rely on the disassembler for determining a function's size, but
234 * the disassembly is a leaky and slow operation, so avoid running
235 * this except when running inside linux perf, which can be inferred
236 * by the PERF_BUILDID_DIR environment variable.
237 */
238 if (getenv("PERF_BUILDID_DIR")) {
239 pid_t pid = getpid();
240 char filename[256];
241 snprintf(filename, sizeof filename, "/tmp/perf-%llu.map", (unsigned long long)pid);
242 perf_map_file = fopen(filename, "wt");
243 snprintf(filename, sizeof filename, "/tmp/perf-%llu.map.asm", (unsigned long long)pid);
244 perf_asm_file.open(filename);
245 }
246 first_time = false;
247 }
248 if (perf_map_file) {
249 const char *symbol = LLVMGetValueName(func);
250 unsigned long addr = (uintptr_t)code;
251 perf_asm_file << symbol << ":\n";
252 unsigned long size = disassemble(code, perf_asm_file);
253 perf_asm_file.flush();
254 fprintf(perf_map_file, "%lx %lx %s\n", addr, size, symbol);
255 fflush(perf_map_file);
256 }
257 #else
258 (void)func;
259 (void)code;
260 #endif
261 }
262
263
264