debug.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /* Debugging extensions for Guile
  2. * Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2006, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public License
  6. * as published by the Free Software Foundation; either version 3 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but
  10. * 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. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301 USA
  18. */
  19. #ifdef HAVE_CONFIG_H
  20. # include <config.h>
  21. #endif
  22. #ifdef HAVE_GETRLIMIT
  23. #include <sys/time.h>
  24. #include <sys/resource.h>
  25. #endif
  26. #ifdef __MINGW32__
  27. # define WIN32_LEAN_AND_MEAN
  28. # include <windows.h>
  29. #endif
  30. #include "libguile/_scm.h"
  31. #include "libguile/async.h"
  32. #include "libguile/eval.h"
  33. #include "libguile/list.h"
  34. #include "libguile/stackchk.h"
  35. #include "libguile/throw.h"
  36. #include "libguile/macros.h"
  37. #include "libguile/smob.h"
  38. #include "libguile/struct.h"
  39. #include "libguile/procprop.h"
  40. #include "libguile/srcprop.h"
  41. #include "libguile/alist.h"
  42. #include "libguile/continuations.h"
  43. #include "libguile/strports.h"
  44. #include "libguile/read.h"
  45. #include "libguile/feature.h"
  46. #include "libguile/dynwind.h"
  47. #include "libguile/modules.h"
  48. #include "libguile/ports.h"
  49. #include "libguile/root.h"
  50. #include "libguile/fluids.h"
  51. #include "libguile/programs.h"
  52. #include "libguile/memoize.h"
  53. #include "libguile/vm.h"
  54. #include "libguile/validate.h"
  55. #include "libguile/debug.h"
  56. #include "libguile/private-options.h"
  57. /*
  58. * Debugging options.
  59. */
  60. scm_t_option scm_debug_opts[] = {
  61. { SCM_OPTION_BOOLEAN, "backwards", 0,
  62. "Display backtrace in anti-chronological order." },
  63. { SCM_OPTION_INTEGER, "width", 79, "Maximal width of backtrace." },
  64. { SCM_OPTION_INTEGER, "depth", 20, "Maximal length of printed backtrace." },
  65. { SCM_OPTION_BOOLEAN, "backtrace", 1, "Show backtrace on error." },
  66. /* This default stack limit will be overridden by init_stack_limit(),
  67. if we have getrlimit() and the stack limit is not INFINITY. But it is still
  68. important, as some systems have both the soft and the hard limits set to
  69. INFINITY; in that case we fall back to this value.
  70. The situation is aggravated by certain compilers, which can consume
  71. "beaucoup de stack", as they say in France.
  72. See http://thread.gmane.org/gmane.lisp.guile.devel/8599/focus=8662 for
  73. more discussion. This setting is 640 KB on 32-bit arches (should be enough
  74. for anyone!) or a whoppin' 1280 KB on 64-bit arches.
  75. */
  76. { SCM_OPTION_INTEGER, "stack", 160000, "Stack size limit (measured in words; 0 = no check)." },
  77. { SCM_OPTION_SCM, "show-file-name", SCM_BOOL_T_BITS,
  78. "Show file names and line numbers "
  79. "in backtraces when not `#f'. A value of `base' "
  80. "displays only base names, while `#t' displays full names."},
  81. { SCM_OPTION_BOOLEAN, "warn-deprecated", 0,
  82. "Warn when deprecated features are used." },
  83. { 0 },
  84. };
  85. /* {Run time control of the debugging evaluator}
  86. */
  87. SCM_DEFINE (scm_debug_options, "debug-options-interface", 0, 1, 0,
  88. (SCM setting),
  89. "Option interface for the debug options. Instead of using\n"
  90. "this procedure directly, use the procedures @code{debug-enable},\n"
  91. "@code{debug-disable}, @code{debug-set!} and @code{debug-options}.")
  92. #define FUNC_NAME s_scm_debug_options
  93. {
  94. SCM ans;
  95. scm_dynwind_begin (0);
  96. scm_dynwind_critical_section (SCM_BOOL_F);
  97. ans = scm_options (setting, scm_debug_opts, FUNC_NAME);
  98. scm_stack_checking_enabled_p = SCM_STACK_CHECKING_P;
  99. scm_dynwind_end ();
  100. return ans;
  101. }
  102. #undef FUNC_NAME
  103. #if 0
  104. SCM_REGISTER_PROC (s_reverse_lookup, "reverse-lookup", 2, 0, 0, scm_reverse_lookup);
  105. #endif
  106. SCM
  107. scm_reverse_lookup (SCM env, SCM data)
  108. {
  109. while (scm_is_pair (env) && scm_is_pair (SCM_CAR (env)))
  110. {
  111. SCM names = SCM_CAAR (env);
  112. SCM values = SCM_CDAR (env);
  113. while (scm_is_pair (names))
  114. {
  115. if (scm_is_eq (SCM_CAR (values), data))
  116. return SCM_CAR (names);
  117. names = SCM_CDR (names);
  118. values = SCM_CDR (values);
  119. }
  120. if (!scm_is_null (names) && scm_is_eq (values, data))
  121. return names;
  122. env = SCM_CDR (env);
  123. }
  124. return SCM_BOOL_F;
  125. }
  126. /* Undocumented debugging procedure */
  127. #ifdef GUILE_DEBUG
  128. SCM_DEFINE (scm_debug_hang, "debug-hang", 0, 1, 0,
  129. (SCM obj),
  130. "Go into an endless loop, which can be only terminated with\n"
  131. "a debugger.")
  132. #define FUNC_NAME s_scm_debug_hang
  133. {
  134. int go = 0;
  135. while (!go) ;
  136. return SCM_UNSPECIFIED;
  137. }
  138. #undef FUNC_NAME
  139. #endif
  140. static SCM local_eval_var;
  141. static void
  142. init_local_eval_var (void)
  143. {
  144. local_eval_var = scm_c_public_variable ("ice-9 local-eval", "local-eval");
  145. }
  146. SCM
  147. scm_local_eval (SCM exp, SCM env)
  148. {
  149. static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT;
  150. scm_i_pthread_once (&once, init_local_eval_var);
  151. return scm_call_2 (scm_variable_ref (local_eval_var), exp, env);
  152. }
  153. static void
  154. init_stack_limit (void)
  155. {
  156. #if defined HAVE_GETRLIMIT
  157. struct rlimit lim;
  158. if (getrlimit (RLIMIT_STACK, &lim) == 0)
  159. {
  160. rlim_t bytes = lim.rlim_cur;
  161. /* set our internal stack limit to 80% of the rlimit. */
  162. if (bytes == RLIM_INFINITY)
  163. bytes = lim.rlim_max;
  164. if (bytes != RLIM_INFINITY)
  165. SCM_STACK_LIMIT = bytes * 8 / 10 / sizeof (scm_t_bits);
  166. }
  167. errno = 0;
  168. #elif defined __MINGW32__
  169. MEMORY_BASIC_INFORMATION m;
  170. uintptr_t bytes;
  171. if (VirtualQuery ((LPCVOID) &m, &m, sizeof m))
  172. {
  173. bytes = (DWORD_PTR) m.BaseAddress + m.RegionSize
  174. - (DWORD_PTR) m.AllocationBase;
  175. SCM_STACK_LIMIT = bytes * 8 / 10 / sizeof (scm_t_bits);
  176. }
  177. #endif
  178. }
  179. void
  180. scm_init_debug ()
  181. {
  182. init_stack_limit ();
  183. scm_init_opts (scm_debug_options, scm_debug_opts);
  184. scm_add_feature ("debug-extensions");
  185. #include "libguile/debug.x"
  186. }
  187. /*
  188. Local Variables:
  189. c-file-style: "gnu"
  190. End:
  191. */