xref: /aosp_15_r20/external/mesa3d/src/gallium/auxiliary/gallivm/lp_bld_debug.cpp (revision 6104692788411f58d303aa86923a9ff6ecaded22)
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) << std::hex << (unsigned long)pc
130              << std::setw(0) << std::dec << ":";
131 
132       Size = LLVMDisasmInstruction(D, (uint8_t *)bytes + pc, extent - pc, 0, outline,
133                                    sizeof(outline));
134 
135       if (!Size) {
136 #if DETECT_ARCH_AARCH64
137          uint32_t invalid = bytes[pc + 0] << 0 | bytes[pc + 1] << 8 |
138                             bytes[pc + 2] << 16 | bytes[pc + 3] << 24;
139          snprintf(outline, sizeof(outline), "\tinvalid %x", invalid);
140          Size = 4;
141 #else
142          buffer << "\tinvalid\n";
143          break;
144 #endif
145       }
146 
147       /*
148        * Output the bytes in hexidecimal format.
149        */
150 
151       if (0) {
152          unsigned i;
153          for (i = 0; i < Size; ++i) {
154             buffer << std::hex << std::setfill('0') << std::setw(2)
155                    << static_cast<int> (bytes[pc + i])
156                    << std::setw(0) << std::dec;
157          }
158          for (; i < 16; ++i) {
159             buffer << "   ";
160          }
161       }
162 
163       /*
164        * Print the instruction.
165        */
166 
167       buffer << outline << '\n';
168 
169       /*
170        * Advance.
171        */
172 
173       pc += Size;
174 
175       /*
176        * Stop disassembling on return statements
177        */
178 
179 #if DETECT_ARCH_X86 || DETECT_ARCH_X86_64
180       if (Size == 1 && bytes[pc - 1] == 0xc3) {
181          break;
182       }
183 #elif DETECT_ARCH_AARCH64
184       if (Size == 4 && bytes[pc - 1] == 0xD6 && bytes[pc - 2] == 0x5F &&
185             (bytes[pc - 3] & 0xFC) == 0 && (bytes[pc - 4] & 0x1F) == 0) {
186          break;
187       }
188 #endif
189 
190       if (pc >= extent) {
191          buffer << "disassembly larger than " << extent << " bytes, aborting\n";
192          break;
193       }
194    }
195 
196    buffer << '\n';
197 
198    LLVMDisasmDispose(D);
199 
200    /*
201     * Print GDB command, useful to verify output.
202     */
203    if (0) {
204       buffer << "disassemble " << std::hex << static_cast<const void*>(bytes) << ' '
205              << static_cast<const void*>(bytes + pc) << std::dec << '\n';
206    }
207 
208    return pc;
209 }
210 
211 
212 extern "C" void
lp_disassemble(LLVMValueRef func,const void * code)213 lp_disassemble(LLVMValueRef func, const void *code)
214 {
215    std::ostringstream buffer;
216    std::string s;
217 
218    buffer << LLVMGetValueName(func) << ":\n";
219    disassemble(code, buffer);
220    s = buffer.str();
221    os_log_message(s.c_str());
222    os_log_message("\n");
223 }
224 
225 
226 /*
227  * Linux perf profiler integration.
228  *
229  * See also:
230  * - http://penberg.blogspot.co.uk/2009/06/jato-has-profiler.html
231  * - https://github.com/penberg/jato/commit/73ad86847329d99d51b386f5aba692580d1f8fdc
232  * - http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=commitdiff;h=80d496be89ed7dede5abee5c057634e80a31c82d
233  */
234 extern "C" void
lp_profile(LLVMValueRef func,const void * code)235 lp_profile(LLVMValueRef func, const void *code)
236 {
237 #if defined(PROFILE)
238    static std::ofstream perf_asm_file;
239    static bool first_time = true;
240    static FILE *perf_map_file = NULL;
241    if (first_time) {
242       unsigned long long pid = (unsigned long long)getpid();
243       char filename[1024];
244 #if defined(__linux__)
245       /*
246        * We rely on the disassembler for determining a function's size, but
247        * the disassembly is a leaky and slow operation, so avoid running
248        * this except when running inside linux perf, which can be inferred
249        * by the PERF_BUILDID_DIR environment variable.
250        */
251       if (getenv("PERF_BUILDID_DIR")) {
252          snprintf(filename, sizeof(filename), "/tmp/perf-%llu.map", pid);
253          perf_map_file = fopen(filename, "wt");
254          snprintf(filename, sizeof(filename), "/tmp/perf-%llu.map.asm", pid);
255          perf_asm_file.open(filename);
256       }
257 #else
258       if (const char* output_dir = getenv("JIT_SYMBOL_MAP_DIR")) {
259          snprintf(filename, sizeof(filename), "%s/jit-symbols-%llu.map", output_dir, pid);
260          perf_map_file = fopen(filename, "wt");
261          snprintf(filename, sizeof(filename), "%s/jit-symbols-%llu.map.asm", output_dir, pid);
262          perf_asm_file.open(filename);
263       }
264 #endif
265       first_time = false;
266    }
267    if (perf_map_file) {
268       const char *symbol = LLVMGetValueName(func);
269       unsigned long addr = (uintptr_t)code;
270       perf_asm_file << symbol << " " << std::hex
271                     << (uintptr_t)code << std::dec << ":\n";
272       unsigned long size = disassemble(code, perf_asm_file);
273       perf_asm_file.flush();
274       fprintf(perf_map_file, "%lx %lx %s\n", addr, size, symbol);
275       fflush(perf_map_file);
276    }
277 #else
278    (void)func;
279    (void)code;
280 #endif
281 }
282