Blame src/stacktrace_generic-inl.h

Packit 18d29c
// Copyright (c) 2000 - 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
// Portable implementation - just use glibc
Packit 18d29c
//
Packit 18d29c
// Note:  The glibc implementation may cause a call to malloc.
Packit 18d29c
// This can cause a deadlock in HeapProfiler.
Packit 18d29c
#include <execinfo.h>
Packit 18d29c
#include <string.h>
Packit 18d29c
#include "stacktrace.h"
Packit 18d29c
Packit 18d29c
_START_GOOGLE_NAMESPACE_
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
  static const int kStackLength = 64;
Packit 18d29c
  void * stack[kStackLength];
Packit 18d29c
  int size;
Packit 18d29c
Packit 18d29c
  size = backtrace(stack, kStackLength);
Packit 18d29c
  skip_count++;  // we want to skip the current frame as well
Packit 18d29c
  int result_count = size - skip_count;
Packit 18d29c
  if (result_count < 0)
Packit 18d29c
    result_count = 0;
Packit 18d29c
  if (result_count > max_depth)
Packit 18d29c
    result_count = max_depth;
Packit 18d29c
  for (int i = 0; i < result_count; i++)
Packit 18d29c
    result[i] = stack[i + skip_count];
Packit 18d29c
Packit 18d29c
  return result_count;
Packit 18d29c
}
Packit 18d29c
Packit 18d29c
_END_GOOGLE_NAMESPACE_