17ec681f3Smrg/**************************************************************************
27ec681f3Smrg *
37ec681f3Smrg * Copyright 2009 VMware, Inc.
47ec681f3Smrg * All Rights Reserved.
57ec681f3Smrg *
67ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
77ec681f3Smrg * copy of this software and associated documentation files (the
87ec681f3Smrg * "Software"), to deal in the Software without restriction, including
97ec681f3Smrg * without limitation the rights to use, copy, modify, merge, publish,
107ec681f3Smrg * distribute, sub license, and/or sell copies of the Software, and to
117ec681f3Smrg * permit persons to whom the Software is furnished to do so, subject to
127ec681f3Smrg * the following conditions:
137ec681f3Smrg *
147ec681f3Smrg * The above copyright notice and this permission notice (including the
157ec681f3Smrg * next paragraph) shall be included in all copies or substantial portions
167ec681f3Smrg * of the Software.
177ec681f3Smrg *
187ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
197ec681f3Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
207ec681f3Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
217ec681f3Smrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
227ec681f3Smrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
237ec681f3Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
247ec681f3Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
257ec681f3Smrg *
267ec681f3Smrg **************************************************************************/
277ec681f3Smrg
287ec681f3Smrg/**
297ec681f3Smrg * @file
307ec681f3Smrg * Stack backtracing.
317ec681f3Smrg *
327ec681f3Smrg * @author Jose Fonseca <jfonseca@vmware.com>
337ec681f3Smrg */
347ec681f3Smrg
357ec681f3Smrg#include "util/u_debug.h"
367ec681f3Smrg#include "u_debug_symbol.h"
377ec681f3Smrg#include "u_debug_stack.h"
387ec681f3Smrg#include "pipe/p_config.h"
397ec681f3Smrg
407ec681f3Smrg#if defined(HAVE_LIBUNWIND)
417ec681f3Smrg
427ec681f3Smrg#ifndef _GNU_SOURCE
437ec681f3Smrg#define _GNU_SOURCE
447ec681f3Smrg#endif
457ec681f3Smrg#include <dlfcn.h>
467ec681f3Smrg
477ec681f3Smrg#include "os/os_thread.h"
487ec681f3Smrg#include "util/hash_table.h"
497ec681f3Smrg
507ec681f3Smrgstatic struct hash_table* symbols_hash;
517ec681f3Smrgstatic mtx_t symbols_mutex = _MTX_INITIALIZER_NP;
527ec681f3Smrg
537ec681f3Smrg/* TODO with some refactoring we might be able to re-use debug_symbol_name_cached()
547ec681f3Smrg * instead.. otoh if using libunwind I think u_debug_symbol could just be excluded
557ec681f3Smrg * from build?
567ec681f3Smrg */
577ec681f3Smrgstatic const char *
587ec681f3Smrgsymbol_name_cached(unw_cursor_t *cursor, unw_proc_info_t *pip)
597ec681f3Smrg{
607ec681f3Smrg   void *addr = (void *)(uintptr_t)pip->start_ip;
617ec681f3Smrg   char *name;
627ec681f3Smrg
637ec681f3Smrg   mtx_lock(&symbols_mutex);
647ec681f3Smrg   if(!symbols_hash)
657ec681f3Smrg      symbols_hash = _mesa_pointer_hash_table_create(NULL);
667ec681f3Smrg   struct hash_entry *entry = _mesa_hash_table_search(symbols_hash, addr);
677ec681f3Smrg   if (!entry) {
687ec681f3Smrg      char procname[256];
697ec681f3Smrg      unw_word_t off;
707ec681f3Smrg      int ret;
717ec681f3Smrg
727ec681f3Smrg      ret = unw_get_proc_name(cursor, procname, sizeof(procname), &off);
737ec681f3Smrg      if (ret && ret != -UNW_ENOMEM) {
747ec681f3Smrg         procname[0] = '?';
757ec681f3Smrg         procname[1] = 0;
767ec681f3Smrg      }
777ec681f3Smrg
787ec681f3Smrg      if (asprintf(&name, "%s%s", procname, ret == -UNW_ENOMEM ? "..." : "") == -1)
797ec681f3Smrg         name = "??";
807ec681f3Smrg      entry = _mesa_hash_table_insert(symbols_hash, addr, (void*)name);
817ec681f3Smrg   }
827ec681f3Smrg   mtx_unlock(&symbols_mutex);
837ec681f3Smrg
847ec681f3Smrg   return entry->data;
857ec681f3Smrg}
867ec681f3Smrg
877ec681f3Smrgvoid
887ec681f3Smrgdebug_backtrace_capture(struct debug_stack_frame *backtrace,
897ec681f3Smrg                        unsigned start_frame,
907ec681f3Smrg                        unsigned nr_frames)
917ec681f3Smrg{
927ec681f3Smrg   unw_cursor_t cursor;
937ec681f3Smrg   unw_context_t context;
947ec681f3Smrg   unw_proc_info_t pip;
957ec681f3Smrg   unsigned i = 0;
967ec681f3Smrg
977ec681f3Smrg   pip.unwind_info = 0;
987ec681f3Smrg
997ec681f3Smrg   unw_getcontext(&context);
1007ec681f3Smrg   unw_init_local(&cursor, &context);
1017ec681f3Smrg
1027ec681f3Smrg   while ((start_frame > 0) && (unw_step(&cursor) > 0))
1037ec681f3Smrg      start_frame--;
1047ec681f3Smrg
1057ec681f3Smrg   while ((i < nr_frames) && (unw_step(&cursor) > 0)) {
1067ec681f3Smrg      unw_word_t ip;
1077ec681f3Smrg
1087ec681f3Smrg      unw_get_reg(&cursor, UNW_REG_IP, &ip);
1097ec681f3Smrg      unw_get_proc_info(&cursor, &pip);
1107ec681f3Smrg
1117ec681f3Smrg      backtrace[i].start_ip = pip.start_ip;
1127ec681f3Smrg      backtrace[i].off      = ip - pip.start_ip;
1137ec681f3Smrg      backtrace[i].procname = symbol_name_cached(&cursor, &pip);
1147ec681f3Smrg
1157ec681f3Smrg      i++;
1167ec681f3Smrg   }
1177ec681f3Smrg
1187ec681f3Smrg   while (i < nr_frames) {
1197ec681f3Smrg      backtrace[i].start_ip = 0;
1207ec681f3Smrg      i++;
1217ec681f3Smrg   }
1227ec681f3Smrg}
1237ec681f3Smrg
1247ec681f3Smrgstatic const void *
1257ec681f3Smrgframe_ip(const struct debug_stack_frame *frame)
1267ec681f3Smrg{
1277ec681f3Smrg   return (void *)(uintptr_t)(frame->start_ip + frame->off);
1287ec681f3Smrg}
1297ec681f3Smrg
1307ec681f3Smrgstatic const char *
1317ec681f3Smrgframe_info(const struct debug_stack_frame *frame, unsigned *offset)
1327ec681f3Smrg{
1337ec681f3Smrg   Dl_info dlinfo;
1347ec681f3Smrg   const void *addr = frame_ip(frame);
1357ec681f3Smrg
1367ec681f3Smrg
1377ec681f3Smrg   if (dladdr(addr, &dlinfo) && dlinfo.dli_fname &&
1387ec681f3Smrg           *dlinfo.dli_fname) {
1397ec681f3Smrg      *offset = (unsigned)((uintptr_t)addr - (uintptr_t)dlinfo.dli_fbase);
1407ec681f3Smrg      return dlinfo.dli_fname;
1417ec681f3Smrg   }
1427ec681f3Smrg
1437ec681f3Smrg   *offset = 0;
1447ec681f3Smrg   return "?";
1457ec681f3Smrg}
1467ec681f3Smrg
1477ec681f3Smrgvoid
1487ec681f3Smrgdebug_backtrace_dump(const struct debug_stack_frame *backtrace,
1497ec681f3Smrg                     unsigned nr_frames)
1507ec681f3Smrg{
1517ec681f3Smrg   unsigned i, offset;
1527ec681f3Smrg   const char *filename;
1537ec681f3Smrg
1547ec681f3Smrg   for (i = 0; i < nr_frames; ++i) {
1557ec681f3Smrg      if (!backtrace[i].start_ip)
1567ec681f3Smrg         break;
1577ec681f3Smrg      filename = frame_info(&backtrace[i], &offset);
1587ec681f3Smrg      debug_printf("\t%s(+0x%x) (%s+0x%x) [%p]\n", filename, offset,
1597ec681f3Smrg            backtrace[i].procname, backtrace[i].off,
1607ec681f3Smrg            frame_ip(&backtrace[i]));
1617ec681f3Smrg   }
1627ec681f3Smrg}
1637ec681f3Smrg
1647ec681f3Smrgvoid
1657ec681f3Smrgdebug_backtrace_print(FILE *f,
1667ec681f3Smrg                      const struct debug_stack_frame *backtrace,
1677ec681f3Smrg                      unsigned nr_frames)
1687ec681f3Smrg{
1697ec681f3Smrg   unsigned i, offset;
1707ec681f3Smrg   const char *filename;
1717ec681f3Smrg
1727ec681f3Smrg   for (i = 0; i < nr_frames; ++i) {
1737ec681f3Smrg      if (!backtrace[i].start_ip)
1747ec681f3Smrg         break;
1757ec681f3Smrg      filename = frame_info(&backtrace[i], &offset);
1767ec681f3Smrg      fprintf(f, "\t%s(+0x%x) (%s+0x%x) [%p]\n", filename, offset,
1777ec681f3Smrg            backtrace[i].procname, backtrace[i].off,
1787ec681f3Smrg            frame_ip(&backtrace[i]));
1797ec681f3Smrg   }
1807ec681f3Smrg}
1817ec681f3Smrg#elif defined(ANDROID)
1827ec681f3Smrg   /* Not implemented here; see u_debug_stack_android.cpp */
1837ec681f3Smrg#else /* ! HAVE_LIBUNWIND */
1847ec681f3Smrg
1857ec681f3Smrg#if defined(PIPE_OS_WINDOWS)
1867ec681f3Smrg#include <windows.h>
1877ec681f3Smrg#endif
1887ec681f3Smrg
1897ec681f3Smrg
1907ec681f3Smrg/**
1917ec681f3Smrg * Capture stack backtrace.
1927ec681f3Smrg *
1937ec681f3Smrg * NOTE: The implementation of this function is quite big, but it is important
1947ec681f3Smrg * not to break it down in smaller functions to avoid adding new frames to the
1957ec681f3Smrg * calling stack.
1967ec681f3Smrg */
1977ec681f3Smrgvoid
1987ec681f3Smrgdebug_backtrace_capture(struct debug_stack_frame *backtrace,
1997ec681f3Smrg                        unsigned start_frame,
2007ec681f3Smrg                        unsigned nr_frames)
2017ec681f3Smrg{
2027ec681f3Smrg   const void **frame_pointer = NULL;
2037ec681f3Smrg   unsigned i = 0;
2047ec681f3Smrg
2057ec681f3Smrg   if (!nr_frames) {
2067ec681f3Smrg      return;
2077ec681f3Smrg   }
2087ec681f3Smrg
2097ec681f3Smrg   /*
2107ec681f3Smrg    * On Windows try obtaining the stack backtrace via CaptureStackBackTrace.
2117ec681f3Smrg    *
2127ec681f3Smrg    * It works reliably both for x86 for x86_64.
2137ec681f3Smrg    */
2147ec681f3Smrg#if defined(PIPE_OS_WINDOWS)
2157ec681f3Smrg   {
2167ec681f3Smrg      typedef USHORT (WINAPI *PFNCAPTURESTACKBACKTRACE)(ULONG, ULONG,
2177ec681f3Smrg                                                        PVOID *, PULONG);
2187ec681f3Smrg      static PFNCAPTURESTACKBACKTRACE pfnCaptureStackBackTrace = NULL;
2197ec681f3Smrg
2207ec681f3Smrg      if (!pfnCaptureStackBackTrace) {
2217ec681f3Smrg         static HMODULE hModule = NULL;
2227ec681f3Smrg         if (!hModule) {
2237ec681f3Smrg            hModule = LoadLibraryA("kernel32");
2247ec681f3Smrg            assert(hModule);
2257ec681f3Smrg         }
2267ec681f3Smrg         if (hModule) {
2277ec681f3Smrg            pfnCaptureStackBackTrace =
2287ec681f3Smrg               (PFNCAPTURESTACKBACKTRACE)GetProcAddress(hModule,
2297ec681f3Smrg                                                "RtlCaptureStackBackTrace");
2307ec681f3Smrg         }
2317ec681f3Smrg      }
2327ec681f3Smrg      if (pfnCaptureStackBackTrace) {
2337ec681f3Smrg         /*
2347ec681f3Smrg          * Skip this (debug_backtrace_capture) function's frame.
2357ec681f3Smrg          */
2367ec681f3Smrg
2377ec681f3Smrg         start_frame += 1;
2387ec681f3Smrg
2397ec681f3Smrg         assert(start_frame + nr_frames < 63);
2407ec681f3Smrg         i = pfnCaptureStackBackTrace(start_frame, nr_frames,
2417ec681f3Smrg                                      (PVOID *) &backtrace->function, NULL);
2427ec681f3Smrg
2437ec681f3Smrg         /* Pad remaing requested frames with NULL */
2447ec681f3Smrg         while (i < nr_frames) {
2457ec681f3Smrg            backtrace[i++].function = NULL;
2467ec681f3Smrg         }
2477ec681f3Smrg
2487ec681f3Smrg         return;
2497ec681f3Smrg      }
2507ec681f3Smrg   }
2517ec681f3Smrg#endif
2527ec681f3Smrg
2537ec681f3Smrg#if defined(PIPE_CC_GCC) && (PIPE_CC_GCC_VERSION > 404) || defined(__clang__)
2547ec681f3Smrg#pragma GCC diagnostic push
2557ec681f3Smrg#pragma GCC diagnostic ignored "-Wframe-address"
2567ec681f3Smrg   frame_pointer = ((const void **)__builtin_frame_address(1));
2577ec681f3Smrg#pragma GCC diagnostic pop
2587ec681f3Smrg#elif defined(PIPE_CC_MSVC) && defined(PIPE_ARCH_X86)
2597ec681f3Smrg   __asm {
2607ec681f3Smrg      mov frame_pointer, ebp
2617ec681f3Smrg   }
2627ec681f3Smrg   frame_pointer = (const void **)frame_pointer[0];
2637ec681f3Smrg#else
2647ec681f3Smrg   frame_pointer = NULL;
2657ec681f3Smrg#endif
2667ec681f3Smrg
2677ec681f3Smrg#ifdef PIPE_ARCH_X86
2687ec681f3Smrg   while (nr_frames) {
2697ec681f3Smrg      const void **next_frame_pointer;
2707ec681f3Smrg
2717ec681f3Smrg      if (!frame_pointer)
2727ec681f3Smrg         break;
2737ec681f3Smrg
2747ec681f3Smrg      if (start_frame)
2757ec681f3Smrg         --start_frame;
2767ec681f3Smrg      else {
2777ec681f3Smrg         backtrace[i++].function = frame_pointer[1];
2787ec681f3Smrg         --nr_frames;
2797ec681f3Smrg      }
2807ec681f3Smrg
2817ec681f3Smrg      next_frame_pointer = (const void **)frame_pointer[0];
2827ec681f3Smrg
2837ec681f3Smrg      /* Limit the stack walk to avoid referencing undefined memory */
2847ec681f3Smrg      if ((uintptr_t)next_frame_pointer <= (uintptr_t)frame_pointer ||
2857ec681f3Smrg          (uintptr_t)next_frame_pointer > (uintptr_t)frame_pointer + 64*1024)
2867ec681f3Smrg         break;
2877ec681f3Smrg
2887ec681f3Smrg      frame_pointer = next_frame_pointer;
2897ec681f3Smrg   }
2907ec681f3Smrg#else
2917ec681f3Smrg   (void) frame_pointer;
2927ec681f3Smrg#endif
2937ec681f3Smrg
2947ec681f3Smrg   while (nr_frames) {
2957ec681f3Smrg      backtrace[i++].function = NULL;
2967ec681f3Smrg      --nr_frames;
2977ec681f3Smrg   }
2987ec681f3Smrg}
2997ec681f3Smrg
3007ec681f3Smrg
3017ec681f3Smrgvoid
3027ec681f3Smrgdebug_backtrace_dump(const struct debug_stack_frame *backtrace,
3037ec681f3Smrg                     unsigned nr_frames)
3047ec681f3Smrg{
3057ec681f3Smrg   unsigned i;
3067ec681f3Smrg
3077ec681f3Smrg   for (i = 0; i < nr_frames; ++i) {
3087ec681f3Smrg      if (!backtrace[i].function)
3097ec681f3Smrg         break;
3107ec681f3Smrg      debug_symbol_print(backtrace[i].function);
3117ec681f3Smrg   }
3127ec681f3Smrg}
3137ec681f3Smrg
3147ec681f3Smrg
3157ec681f3Smrgvoid
3167ec681f3Smrgdebug_backtrace_print(FILE *f,
3177ec681f3Smrg                      const struct debug_stack_frame *backtrace,
3187ec681f3Smrg                      unsigned nr_frames)
3197ec681f3Smrg{
3207ec681f3Smrg   unsigned i;
3217ec681f3Smrg
3227ec681f3Smrg   for (i = 0; i < nr_frames; ++i) {
3237ec681f3Smrg      const char *symbol;
3247ec681f3Smrg      if (!backtrace[i].function)
3257ec681f3Smrg         break;
3267ec681f3Smrg      symbol = debug_symbol_name_cached(backtrace[i].function);
3277ec681f3Smrg      if (symbol)
3287ec681f3Smrg         fprintf(f, "%s\n", symbol);
3297ec681f3Smrg   }
3307ec681f3Smrg}
3317ec681f3Smrg
3327ec681f3Smrg#endif /* HAVE_LIBUNWIND */
333