debug.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* Debugging extensions for Guile
  2. * Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2006, 2008, 2009, 2010, 2011, 2012 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. #include "libguile/_scm.h"
  27. #include "libguile/async.h"
  28. #include "libguile/eval.h"
  29. #include "libguile/list.h"
  30. #include "libguile/stackchk.h"
  31. #include "libguile/throw.h"
  32. #include "libguile/macros.h"
  33. #include "libguile/smob.h"
  34. #include "libguile/struct.h"
  35. #include "libguile/procprop.h"
  36. #include "libguile/srcprop.h"
  37. #include "libguile/alist.h"
  38. #include "libguile/continuations.h"
  39. #include "libguile/strports.h"
  40. #include "libguile/read.h"
  41. #include "libguile/feature.h"
  42. #include "libguile/dynwind.h"
  43. #include "libguile/modules.h"
  44. #include "libguile/ports.h"
  45. #include "libguile/root.h"
  46. #include "libguile/fluids.h"
  47. #include "libguile/programs.h"
  48. #include "libguile/memoize.h"
  49. #include "libguile/vm.h"
  50. #include "libguile/validate.h"
  51. #include "libguile/debug.h"
  52. #include "libguile/private-options.h"
  53. /*
  54. * Debugging options.
  55. */
  56. scm_t_option scm_debug_opts[] = {
  57. { SCM_OPTION_BOOLEAN, "backwards", 0,
  58. "Display backtrace in anti-chronological order." },
  59. { SCM_OPTION_INTEGER, "width", 79, "Maximal width of backtrace." },
  60. { SCM_OPTION_INTEGER, "depth", 20, "Maximal length of printed backtrace." },
  61. { SCM_OPTION_BOOLEAN, "backtrace", 1, "Show backtrace on error." },
  62. /* This default stack limit will be overridden by init_stack_limit(),
  63. if we have getrlimit() and the stack limit is not INFINITY. But it is still
  64. important, as some systems have both the soft and the hard limits set to
  65. INFINITY; in that case we fall back to this value.
  66. The situation is aggravated by certain compilers, which can consume
  67. "beaucoup de stack", as they say in France.
  68. See http://thread.gmane.org/gmane.lisp.guile.devel/8599/focus=8662 for
  69. more discussion. This setting is 640 KB on 32-bit arches (should be enough
  70. for anyone!) or a whoppin' 1280 KB on 64-bit arches.
  71. */
  72. { SCM_OPTION_INTEGER, "stack", 160000, "Stack size limit (measured in words; 0 = no check)." },
  73. { SCM_OPTION_SCM, "show-file-name", SCM_BOOL_T_BITS,
  74. "Show file names and line numbers "
  75. "in backtraces when not `#f'. A value of `base' "
  76. "displays only base names, while `#t' displays full names."},
  77. { SCM_OPTION_BOOLEAN, "warn-deprecated", 0,
  78. "Warn when deprecated features are used." },
  79. { 0 },
  80. };
  81. /* {Run time control of the debugging evaluator}
  82. */
  83. SCM_DEFINE (scm_debug_options, "debug-options-interface", 0, 1, 0,
  84. (SCM setting),
  85. "Option interface for the debug options. Instead of using\n"
  86. "this procedure directly, use the procedures @code{debug-enable},\n"
  87. "@code{debug-disable}, @code{debug-set!} and @code{debug-options}.")
  88. #define FUNC_NAME s_scm_debug_options
  89. {
  90. SCM ans;
  91. scm_dynwind_begin (0);
  92. scm_dynwind_critical_section (SCM_BOOL_F);
  93. ans = scm_options (setting, scm_debug_opts, FUNC_NAME);
  94. scm_stack_checking_enabled_p = SCM_STACK_CHECKING_P;
  95. scm_dynwind_end ();
  96. return ans;
  97. }
  98. #undef FUNC_NAME
  99. SCM_SYMBOL (scm_sym_source, "source");
  100. SCM_DEFINE (scm_procedure_name, "procedure-name", 1, 0, 0,
  101. (SCM proc),
  102. "Return the name of the procedure @var{proc}")
  103. #define FUNC_NAME s_scm_procedure_name
  104. {
  105. SCM_VALIDATE_PROC (1, proc);
  106. while (SCM_STRUCTP (proc) && SCM_STRUCT_APPLICABLE_P (proc))
  107. proc = SCM_STRUCT_PROCEDURE (proc);
  108. return scm_procedure_property (proc, scm_sym_name);
  109. }
  110. #undef FUNC_NAME
  111. SCM_DEFINE (scm_procedure_source, "procedure-source", 1, 0, 0,
  112. (SCM proc),
  113. "Return the source of the procedure @var{proc}.")
  114. #define FUNC_NAME s_scm_procedure_source
  115. {
  116. SCM src;
  117. SCM_VALIDATE_PROC (1, proc);
  118. do
  119. {
  120. src = scm_procedure_property (proc, scm_sym_source);
  121. if (scm_is_true (src))
  122. return src;
  123. if (SCM_STRUCTP (proc) && SCM_STRUCT_APPLICABLE_P (proc)
  124. && SCM_HEAP_OBJECT_P ((proc = SCM_STRUCT_PROCEDURE (proc))))
  125. continue;
  126. }
  127. while (0);
  128. return SCM_BOOL_F;
  129. }
  130. #undef FUNC_NAME
  131. #if 0
  132. SCM_REGISTER_PROC (s_reverse_lookup, "reverse-lookup", 2, 0, 0, scm_reverse_lookup);
  133. #endif
  134. SCM
  135. scm_reverse_lookup (SCM env, SCM data)
  136. {
  137. while (scm_is_pair (env) && scm_is_pair (SCM_CAR (env)))
  138. {
  139. SCM names = SCM_CAAR (env);
  140. SCM values = SCM_CDAR (env);
  141. while (scm_is_pair (names))
  142. {
  143. if (scm_is_eq (SCM_CAR (values), data))
  144. return SCM_CAR (names);
  145. names = SCM_CDR (names);
  146. values = SCM_CDR (values);
  147. }
  148. if (!scm_is_null (names) && scm_is_eq (values, data))
  149. return names;
  150. env = SCM_CDR (env);
  151. }
  152. return SCM_BOOL_F;
  153. }
  154. /* Undocumented debugging procedure */
  155. #ifdef GUILE_DEBUG
  156. SCM_DEFINE (scm_debug_hang, "debug-hang", 0, 1, 0,
  157. (SCM obj),
  158. "Go into an endless loop, which can be only terminated with\n"
  159. "a debugger.")
  160. #define FUNC_NAME s_scm_debug_hang
  161. {
  162. int go = 0;
  163. while (!go) ;
  164. return SCM_UNSPECIFIED;
  165. }
  166. #undef FUNC_NAME
  167. #endif
  168. SCM
  169. scm_local_eval (SCM exp, SCM env)
  170. {
  171. static SCM local_eval_var = SCM_BOOL_F;
  172. if (scm_is_false (local_eval_var))
  173. local_eval_var = scm_c_public_variable ("ice-9 local-eval", "local-eval");
  174. return scm_call_2 (SCM_VARIABLE_REF (local_eval_var), exp, env);
  175. }
  176. static void
  177. init_stack_limit (void)
  178. {
  179. #ifdef HAVE_GETRLIMIT
  180. struct rlimit lim;
  181. if (getrlimit (RLIMIT_STACK, &lim) == 0)
  182. {
  183. rlim_t bytes = lim.rlim_cur;
  184. /* set our internal stack limit to 80% of the rlimit. */
  185. if (bytes == RLIM_INFINITY)
  186. bytes = lim.rlim_max;
  187. if (bytes != RLIM_INFINITY)
  188. SCM_STACK_LIMIT = bytes * 8 / 10 / sizeof (scm_t_bits);
  189. }
  190. errno = 0;
  191. #endif
  192. }
  193. void
  194. scm_init_debug ()
  195. {
  196. init_stack_limit ();
  197. scm_init_opts (scm_debug_options, scm_debug_opts);
  198. scm_add_feature ("debug-extensions");
  199. #include "libguile/debug.x"
  200. }
  201. /*
  202. Local Variables:
  203. c-file-style: "gnu"
  204. End:
  205. */