stackchk.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* Copyright (C) 1995,1996,1997, 2000, 2001, 2006, 2008, 2010, 2011 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #include "libguile/_scm.h"
  22. #include "libguile/ports.h"
  23. #include "libguile/root.h"
  24. #include "libguile/threads.h"
  25. #include "libguile/dynwind.h"
  26. #include "libguile/stackchk.h"
  27. /* {Stack Checking}
  28. */
  29. int scm_stack_checking_enabled_p;
  30. SCM_SYMBOL (scm_stack_overflow_key, "stack-overflow");
  31. static void
  32. reset_scm_stack_checking_enabled_p (void *arg)
  33. {
  34. scm_stack_checking_enabled_p = (int)(scm_t_bits)arg;
  35. }
  36. void
  37. scm_report_stack_overflow ()
  38. {
  39. scm_dynwind_begin (0); /* non-rewindable frame */
  40. scm_dynwind_unwind_handler (reset_scm_stack_checking_enabled_p,
  41. (void*)(scm_t_bits)scm_stack_checking_enabled_p,
  42. SCM_F_WIND_EXPLICITLY);
  43. scm_stack_checking_enabled_p = 0;
  44. scm_error (scm_stack_overflow_key,
  45. NULL,
  46. "Stack overflow",
  47. SCM_BOOL_F,
  48. SCM_BOOL_F);
  49. /* not reached */
  50. scm_dynwind_end ();
  51. }
  52. long
  53. scm_stack_size (SCM_STACKITEM *start)
  54. {
  55. SCM_STACKITEM stack;
  56. #if SCM_STACK_GROWS_UP
  57. return &stack - start;
  58. #else
  59. return start - &stack;
  60. #endif /* SCM_STACK_GROWS_UP */
  61. }
  62. void
  63. scm_stack_report ()
  64. {
  65. SCM port = scm_current_error_port ();
  66. SCM_STACKITEM stack;
  67. scm_i_thread *thread = SCM_I_CURRENT_THREAD;
  68. scm_uintprint ((scm_stack_size (thread->continuation_base)
  69. * sizeof (SCM_STACKITEM)),
  70. 16, port);
  71. scm_puts_unlocked (" of stack: 0x", port);
  72. scm_uintprint ((scm_t_bits) thread->continuation_base, 16, port);
  73. scm_puts_unlocked (" - 0x", port);
  74. scm_uintprint ((scm_t_bits) &stack, 16, port);
  75. scm_puts_unlocked ("\n", port);
  76. }
  77. SCM_DEFINE (scm_sys_get_stack_size, "%get-stack-size", 0, 0, 0,
  78. (),
  79. "Return the current thread's C stack size (in Scheme objects).")
  80. #define FUNC_NAME s_scm_sys_get_stack_size
  81. {
  82. return scm_from_long (scm_stack_size (SCM_I_CURRENT_THREAD->base));
  83. }
  84. #undef FUNC_NAME
  85. void
  86. scm_init_stackchk ()
  87. {
  88. #include "libguile/stackchk.x"
  89. }
  90. /*
  91. Local Variables:
  92. c-file-style: "gnu"
  93. End:
  94. */