Path: blob/21.2-virgl/src/gallium/auxiliary/gallivm/lp_bld_debug.cpp
4565 views
/**************************************************************************1*2* Copyright 2009-2011 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627#include <stddef.h>28#include <fstream>29#include <sstream>30#include <iomanip>3132#include <llvm/Config/llvm-config.h>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>3940#include "util/u_math.h"41#include "util/u_debug.h"4243#include "lp_bld_debug.h"4445#ifdef __linux__46#include <sys/stat.h>47#include <fcntl.h>48#endif49505152/**53* Check alignment.54*55* It is important that this check is not implemented as a macro or inlined56* function, as the compiler assumptions in respect to alignment of global57* and stack variables would often make the check a no op, defeating the58* whole purpose of the exercise.59*/60extern "C" boolean61lp_check_alignment(const void *ptr, unsigned alignment)62{63assert(util_is_power_of_two_or_zero(alignment));64return ((uintptr_t)ptr & (alignment - 1)) == 0;65}666768/**69* Same as LLVMDumpValue, but through our debugging channels.70*/71extern "C" void72lp_debug_dump_value(LLVMValueRef value)73{74char *str = LLVMPrintValueToString(value);75if (str) {76os_log_message(str);77LLVMDisposeMessage(str);78}79}808182/*83* Disassemble a function, using the LLVM MC disassembler.84*85* See also:86* - http://blog.llvm.org/2010/01/x86-disassembler.html87* - http://blog.llvm.org/2010/04/intro-to-llvm-mc-project.html88*/89static size_t90disassemble(const void* func, std::ostream &buffer)91{92const uint8_t *bytes = (const uint8_t *)func;9394/*95* Limit disassembly to this extent96*/97const uint64_t extent = 96 * 1024;9899/*100* Initialize all used objects.101*/102103const char *triple = LLVM_HOST_TRIPLE;104LLVMDisasmContextRef D = LLVMCreateDisasm(triple, NULL, 0, NULL, NULL);105char outline[1024];106107if (!D) {108buffer << "error: could not create disassembler for triple "109<< triple << '\n';110return 0;111}112113uint64_t pc;114pc = 0;115while (pc < extent) {116size_t Size;117118/*119* Print address. We use addresses relative to the start of the function,120* so that between runs.121*/122123buffer << std::setw(6) << (unsigned long)pc << ":\t";124125Size = LLVMDisasmInstruction(D, (uint8_t *)bytes + pc, extent - pc, 0, outline,126sizeof outline);127128if (!Size) {129buffer << "invalid\n";130pc += 1;131break;132}133134/*135* Output the bytes in hexidecimal format.136*/137138if (0) {139unsigned i;140for (i = 0; i < Size; ++i) {141buffer << std::hex << std::setfill('0') << std::setw(2)142<< static_cast<int> (bytes[pc + i]);143}144for (; i < 16; ++i) {145buffer << std::dec << " ";146}147}148149/*150* Print the instruction.151*/152153buffer << std::setw(Size) << outline << '\n';154155/*156* Stop disassembling on return statements, if there is no record of a157* jump to a successive address.158*159* XXX: This currently assumes x86160*/161162#if defined(PIPE_ARCH_X86) || defined(PIPE_ARCH_X86_64)163if (Size == 1 && bytes[pc] == 0xc3) {164break;165}166#endif167168/*169* Advance.170*/171172pc += Size;173174if (pc >= extent) {175buffer << "disassembly larger than " << extent << " bytes, aborting\n";176break;177}178}179180buffer << '\n';181182LLVMDisasmDispose(D);183184/*185* Print GDB command, useful to verify output.186*/187if (0) {188buffer << "disassemble " << static_cast<const void*>(bytes) << ' '189<< static_cast<const void*>(bytes + pc) << '\n';190}191192return pc;193}194195196extern "C" void197lp_disassemble(LLVMValueRef func, const void *code)198{199std::ostringstream buffer;200std::string s;201202buffer << LLVMGetValueName(func) << ":\n";203disassemble(code, buffer);204s = buffer.str();205os_log_message(s.c_str());206os_log_message("\n");207}208209210/*211* Linux perf profiler integration.212*213* See also:214* - http://penberg.blogspot.co.uk/2009/06/jato-has-profiler.html215* - https://github.com/penberg/jato/commit/73ad86847329d99d51b386f5aba692580d1f8fdc216* - http://git.kernel.org/?p=linux/kernel/git/torvalds/linux.git;a=commitdiff;h=80d496be89ed7dede5abee5c057634e80a31c82d217*/218extern "C" void219lp_profile(LLVMValueRef func, const void *code)220{221#if defined(__linux__) && defined(PROFILE)222static std::ofstream perf_asm_file;223static boolean first_time = TRUE;224static FILE *perf_map_file = NULL;225if (first_time) {226/*227* We rely on the disassembler for determining a function's size, but228* the disassembly is a leaky and slow operation, so avoid running229* this except when running inside linux perf, which can be inferred230* by the PERF_BUILDID_DIR environment variable.231*/232if (getenv("PERF_BUILDID_DIR")) {233pid_t pid = getpid();234char filename[256];235snprintf(filename, sizeof filename, "/tmp/perf-%llu.map", (unsigned long long)pid);236perf_map_file = fopen(filename, "wt");237snprintf(filename, sizeof filename, "/tmp/perf-%llu.map.asm", (unsigned long long)pid);238perf_asm_file.open(filename);239}240first_time = FALSE;241}242if (perf_map_file) {243const char *symbol = LLVMGetValueName(func);244unsigned long addr = (uintptr_t)code;245perf_asm_file << symbol << ":\n";246unsigned long size = disassemble(code, perf_asm_file);247perf_asm_file.flush();248fprintf(perf_map_file, "%lx %lx %s\n", addr, size, symbol);249fflush(perf_map_file);250}251#else252(void)func;253(void)code;254#endif255}256257258259260