debug.c 5.6 KB

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