PageRenderTime 26ms CodeModel.GetById 18ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/src/backtrace.cc

http://ext3grep.googlecode.com/
C++ | 101 lines | 63 code | 10 blank | 28 comment | 6 complexity | d5c2872c6f1bbe03b1cc5ed38b2f744e MD5 | raw file
  1// ext3grep -- An ext3 file system investigation and undelete tool
  2//
  3//! @file backtrace.cc Support for printing a backtrace.
  4//
  5// Copyright (C) 2008, by
  6// 
  7// Carlo Wood, Run on IRC <carlo@alinoe.com>
  8// RSA-1024 0x624ACAD5 1997-01-26                    Sign & Encrypt
  9// Fingerprint16 = 32 EC A7 B6 AC DB 65 A6  F6 F6 55 DD 1C DC FF 61
 10// 
 11// This program is free software: you can redistribute it and/or modify
 12// it under the terms of the GNU General Public License as published by
 13// the Free Software Foundation, either version 2 of the License, or
 14// (at your option) any later version.
 15// 
 16// This program is distributed in the hope that it will be useful,
 17// but WITHOUT ANY WARRANTY; without even the implied warranty of
 18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 19// GNU General Public License for more details.
 20// 
 21// You should have received a copy of the GNU General Public License
 22// along with this program.  If not, see <http://www.gnu.org/licenses/>.
 23
 24#ifndef DEBUG
 25// This is a bug in src/Makefile.am.
 26#error : This source file shouldn't be included at all when DEBUG isn't set.
 27#endif
 28
 29#ifdef __OPTIMIZE__
 30// It makes no sense to dump backtraces if optimization is being used.
 31#error : Please add --disable-optimize to your configure options.
 32#endif
 33
 34#ifndef USE_PCH
 35#include "sys.h"
 36#include <cstdio>
 37#include <iostream>
 38#include <iomanip>
 39#include <cstdlib>
 40#include <sstream>
 41#include <unistd.h>
 42#include "debug.h"
 43#endif
 44
 45#include <execinfo.h>
 46#include "backtrace.h"
 47
 48extern char const* progname;
 49extern char* reserved_memory;
 50
 51static size_t const BACKTRACE_SIZE = 256;
 52static void* return_addresses[BACKTRACE_SIZE];
 53
 54void dump_backtrace_on(std::ostream& os)
 55{
 56  // Free some memory to make this work.
 57  delete [] reserved_memory;
 58  reserved_memory = NULL;
 59
 60  // Get the backtrace.
 61  int nptrs = backtrace(return_addresses, BACKTRACE_SIZE);
 62
 63  // Print it.
 64#ifdef CWDEBUG
 65  for (int j = 0; j < nptrs; ++j)
 66  {
 67    libcwd::location_ct loc((char*)return_addresses[j] + libcwd::builtin_return_address_offset);
 68    os << '#' << std::left << std::setw(3) << j;
 69    os << std::left << std::setw(16) << return_addresses[j] << ' ' << loc << "\n                  in ";
 70    char const* mangled_function_name = loc.mangled_function_name();
 71    if (mangled_function_name != libcwd::unknown_function_c)
 72    {
 73      std::string demangled_function_name;
 74      libcwd::demangle_symbol(mangled_function_name, demangled_function_name);
 75      os << demangled_function_name << '\n';
 76    }
 77    else
 78      os << mangled_function_name << '\n';
 79  }
 80#else
 81  char** symbols = backtrace_symbols(return_addresses, BACKTRACE_SIZE);
 82  if (symbols == NULL)
 83  {
 84    perror("backtrace_symbols");
 85    // Attempt to write to stderr directly.
 86    backtrace_symbols_fd(return_addresses, nptrs, STDERR_FILENO);
 87    exit(EXIT_FAILURE);
 88  }
 89  for (int j = 0; j < nptrs; ++j)
 90  {
 91    std::cout << '#' << std::left << std::setw(3) << j;
 92    std::cout << symbols[j] << std::endl;
 93    std::ostringstream command;
 94    command << "addr2line -e " << progname << ' ' << return_addresses[j];
 95    std::cout << "    " << std::flush;
 96    system(command.str().c_str());
 97  }
 98  free(symbols);
 99#endif
100}
101