Blame src/stacktrace_libunwind-inl.h

Packit 18d29c
// Copyright (c) 2005 - 2007, Google Inc.
Packit 18d29c
// All rights reserved.
Packit 18d29c
//
Packit 18d29c
// Redistribution and use in source and binary forms, with or without
Packit 18d29c
// modification, are permitted provided that the following conditions are
Packit 18d29c
// met:
Packit 18d29c
//
Packit 18d29c
//     * Redistributions of source code must retain the above copyright
Packit 18d29c
// notice, this list of conditions and the following disclaimer.
Packit 18d29c
//     * Redistributions in binary form must reproduce the above
Packit 18d29c
// copyright notice, this list of conditions and the following disclaimer
Packit 18d29c
// in the documentation and/or other materials provided with the
Packit 18d29c
// distribution.
Packit 18d29c
//     * Neither the name of Google Inc. nor the names of its
Packit 18d29c
// contributors may be used to endorse or promote products derived from
Packit 18d29c
// this software without specific prior written permission.
Packit 18d29c
//
Packit 18d29c
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit 18d29c
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit 18d29c
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Packit 18d29c
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Packit 18d29c
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Packit 18d29c
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 18d29c
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit 18d29c
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit 18d29c
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit 18d29c
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit 18d29c
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 18d29c
//
Packit 18d29c
// Author: Arun Sharma
Packit 18d29c
//
Packit 18d29c
// Produce stack trace using libunwind
Packit 18d29c
Packit 18d29c
#include "utilities.h"
Packit 18d29c
Packit 18d29c
extern "C" {
Packit 18d29c
#define UNW_LOCAL_ONLY
Packit 18d29c
#include <libunwind.h>
Packit 18d29c
}
Packit 18d29c
#include "glog/raw_logging.h"
Packit 18d29c
#include "stacktrace.h"
Packit 18d29c
Packit 18d29c
_START_GOOGLE_NAMESPACE_
Packit 18d29c
Packit 18d29c
// Sometimes, we can try to get a stack trace from within a stack
Packit 18d29c
// trace, because libunwind can call mmap (maybe indirectly via an
Packit 18d29c
// internal mmap based memory allocator), and that mmap gets trapped
Packit 18d29c
// and causes a stack-trace request.  If were to try to honor that
Packit 18d29c
// recursive request, we'd end up with infinite recursion or deadlock.
Packit 18d29c
// Luckily, it's safe to ignore those subsequent traces.  In such
Packit 18d29c
// cases, we return 0 to indicate the situation.
Packit 18d29c
static bool g_now_entering = false;
Packit 18d29c
Packit 18d29c
// If you change this function, also change GetStackFrames below.
Packit 18d29c
int GetStackTrace(void** result, int max_depth, int skip_count) {
Packit 18d29c
  void *ip;
Packit 18d29c
  int n = 0;
Packit 18d29c
  unw_cursor_t cursor;
Packit 18d29c
  unw_context_t uc;
Packit 18d29c
Packit 18d29c
  if (sync_val_compare_and_swap(&g_now_entering, false, true)) {
Packit 18d29c
    return 0;
Packit 18d29c
  }
Packit 18d29c
Packit 18d29c
  unw_getcontext(&uc);
Packit 18d29c
  RAW_CHECK(unw_init_local(&cursor, &uc) >= 0, "unw_init_local failed");
Packit 18d29c
  skip_count++;         // Do not include the "GetStackTrace" frame
Packit 18d29c
Packit 18d29c
  while (n < max_depth) {
Packit 18d29c
    int ret = unw_get_reg(&cursor, UNW_REG_IP, (unw_word_t *) &ip);
Packit 18d29c
    if (ret < 0)
Packit 18d29c
      break;
Packit 18d29c
    if (skip_count > 0) {
Packit 18d29c
      skip_count--;
Packit 18d29c
    } else {
Packit 18d29c
      result[n++] = ip;
Packit 18d29c
    }
Packit 18d29c
    ret = unw_step(&cursor);
Packit 18d29c
    if (ret <= 0)
Packit 18d29c
      break;
Packit 18d29c
  }
Packit 18d29c
Packit 18d29c
  g_now_entering = false;
Packit 18d29c
  return n;
Packit 18d29c
}
Packit 18d29c
Packit 18d29c
_END_GOOGLE_NAMESPACE_