backtrace.c
Upload User: zksf00
Upload Date: 2009-12-16
Package Size: 17691k
Code Size: 3k
Development Platform:

Unix_Linux

  1. /* Return backtrace of current program state.  Generic version.
  2.    Copyright (C) 1998, 2000, 2002 Free Software Foundation, Inc.
  3.    This file is part of the GNU C Library.
  4.    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
  5.    The GNU C Library is free software; you can redistribute it and/or
  6.    modify it under the terms of the GNU Lesser General Public
  7.    License as published by the Free Software Foundation; either
  8.    version 2.1 of the License, or (at your option) any later version.
  9.    The GNU C Library is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.    Lesser General Public License for more details.
  13.    You should have received a copy of the GNU Lesser General Public
  14.    License along with the GNU C Library; if not, write to the Free
  15.    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16.    02111-1307 USA.  */
  17. #include <execinfo.h>
  18. #include <signal.h>
  19. #include <frame.h>
  20. #include <sigcontextinfo.h>
  21. #include <bp-checks.h>
  22. /* This is a global variable set at program start time.  It marks the
  23.    highest used stack address.  */
  24. extern void *__libc_stack_end;
  25. /* This implementation assumes a stack layout that matches the defaults
  26.    used by gcc's `__builtin_frame_address' and `__builtin_return_address'
  27.    (FP is the frame pointer register):
  28.   +-----------------+     +-----------------+
  29.     FP -> | previous FP --------> | previous FP ------>...
  30.   |                 |     |                 |
  31.   | return address  |     | return address  |
  32.   +-----------------+     +-----------------+
  33.   */
  34. /* Get some notion of the current stack.  Need not be exactly the top
  35.    of the stack, just something somewhere in the current frame.  */
  36. #ifndef CURRENT_STACK_FRAME
  37. # define CURRENT_STACK_FRAME  ({ char __csf; &__csf; })
  38. #endif
  39. /* By default we assume that the stack grows downward.  */
  40. #ifndef INNER_THAN
  41. # define INNER_THAN <
  42. #endif
  43. /* By default assume the `next' pointer in struct layout points to the
  44.    next struct layout.  */
  45. #ifndef ADVANCE_STACK_FRAME
  46. # define ADVANCE_STACK_FRAME(next) BOUNDED_1 ((struct layout *) (next))
  47. #endif
  48. /* By default, the frame pointer is just what we get from gcc.  */
  49. #ifndef FIRST_FRAME_POINTER
  50. # define FIRST_FRAME_POINTER  __builtin_frame_address (0)
  51. #endif
  52. int
  53. __backtrace (array, size)
  54.      void **array;
  55.      int size;
  56. {
  57.   struct layout *current;
  58.   void *__unbounded top_frame;
  59.   void *__unbounded top_stack;
  60.   int cnt = 0;
  61.   top_frame = FIRST_FRAME_POINTER;
  62.   top_stack = CURRENT_STACK_FRAME;
  63.   /* We skip the call to this function, it makes no sense to record it.  */
  64.   current = BOUNDED_1 ((struct layout *) top_frame);
  65.   while (cnt < size)
  66.     {
  67.       if ((void *) current INNER_THAN top_stack
  68.   || !((void *) current INNER_THAN __libc_stack_end))
  69.        /* This means the address is out of range.  Note that for the
  70.   toplevel we see a frame pointer with value NULL which clearly is
  71.   out of range.  */
  72. break;
  73.       array[cnt++] = current->return_address;
  74.       current = ADVANCE_STACK_FRAME (current->next);
  75.     }
  76.   return cnt;
  77. }
  78. weak_alias (__backtrace, backtrace)