gdbint.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* GDB interface for Guile
  2. * Copyright (C) 1996,1997,1999,2000,2001,2002,2004
  3. * Free Software Foundation, Inc.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public License
  7. * as published by the Free Software Foundation; either version 3 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301 USA
  19. */
  20. #ifdef HAVE_CONFIG_H
  21. # include <config.h>
  22. #endif
  23. #include "libguile/_scm.h"
  24. #include <stdio.h>
  25. #include <string.h>
  26. #ifdef HAVE_UNISTD_H
  27. #include <unistd.h>
  28. #endif
  29. #include "libguile/strports.h"
  30. #include "libguile/read.h"
  31. #include "libguile/eval.h"
  32. #include "libguile/chars.h"
  33. #include "libguile/modules.h"
  34. #include "libguile/ports.h"
  35. #include "libguile/fluids.h"
  36. #include "libguile/strings.h"
  37. #include "libguile/init.h"
  38. #include "libguile/gdbint.h"
  39. /* {Support for debugging with gdb}
  40. *
  41. * TODO:
  42. *
  43. * 1. Redirect outputs
  44. * 2. Catch errors
  45. * 3. Prevent print from causing segmentation fault when given broken pairs
  46. */
  47. #define GDB_TYPE SCM
  48. #include "libguile/gdb_interface.h"
  49. /* Be carefull when this macro is true.
  50. scm_gc_running_p is set during gc.
  51. */
  52. #define SCM_GC_P (scm_gc_running_p)
  53. /* Macros that encapsulate blocks of code which can be called by the
  54. * debugger.
  55. */
  56. #define SCM_BEGIN_FOREIGN_BLOCK \
  57. do { \
  58. scm_print_carefully_p = 1; \
  59. } while (0)
  60. #define SCM_END_FOREIGN_BLOCK \
  61. do { \
  62. scm_print_carefully_p = 0; \
  63. } while (0)
  64. #define RESET_STRING { gdb_output_length = 0; }
  65. #define SEND_STRING(str) \
  66. do { \
  67. gdb_output = (char *) (str); \
  68. gdb_output_length = strlen ((const char *) (str)); \
  69. } while (0)
  70. /* {Gdb interface}
  71. */
  72. unsigned short gdb_options = GDB_HAVE_BINDINGS;
  73. char *gdb_language = "lisp/c";
  74. SCM gdb_result;
  75. char *gdb_output;
  76. int gdb_output_length;
  77. int scm_print_carefully_p;
  78. static SCM gdb_input_port;
  79. static int port_mark_p, stream_mark_p, string_mark_p;
  80. static SCM gdb_output_port;
  81. static void
  82. unmark_port (SCM port)
  83. {
  84. SCM stream, string;
  85. port_mark_p = SCM_GC_MARK_P (port);
  86. SCM_CLEAR_GC_MARK (port);
  87. stream = SCM_PACK (SCM_STREAM (port));
  88. stream_mark_p = SCM_GC_MARK_P (stream);
  89. SCM_CLEAR_GC_MARK (stream);
  90. string = SCM_CDR (stream);
  91. string_mark_p = SCM_GC_MARK_P (string);
  92. SCM_CLEAR_GC_MARK (string);
  93. }
  94. static void
  95. remark_port (SCM port)
  96. {
  97. SCM stream = SCM_PACK (SCM_STREAM (port));
  98. SCM string = SCM_CDR (stream);
  99. if (string_mark_p)
  100. SCM_SET_GC_MARK (string);
  101. if (stream_mark_p)
  102. SCM_SET_GC_MARK (stream);
  103. if (port_mark_p)
  104. SCM_SET_GC_MARK (port);
  105. }
  106. int
  107. gdb_maybe_valid_type_p (SCM value)
  108. {
  109. return SCM_IMP (value) || scm_in_heap_p (value);
  110. }
  111. int
  112. gdb_read (char *str)
  113. {
  114. SCM ans;
  115. int status = 0;
  116. RESET_STRING;
  117. /* Need to be restrictive about what to read? */
  118. if (SCM_GC_P)
  119. {
  120. char *p;
  121. for (p = str; *p != '\0'; ++p)
  122. switch (*p)
  123. {
  124. case '(':
  125. case '\'':
  126. case '"':
  127. SEND_STRING ("Can't read this kind of expressions during gc");
  128. return -1;
  129. case '#':
  130. if (*++p == '\0')
  131. goto premature;
  132. if (*p == '\\')
  133. {
  134. if (*++p != '\0')
  135. continue;
  136. premature:
  137. SEND_STRING ("Premature end of lisp expression");
  138. return -1;
  139. }
  140. default:
  141. continue;
  142. }
  143. }
  144. SCM_BEGIN_FOREIGN_BLOCK;
  145. unmark_port (gdb_input_port);
  146. scm_seek (gdb_input_port, SCM_INUM0, scm_from_int (SEEK_SET));
  147. scm_puts (str, gdb_input_port);
  148. scm_truncate_file (gdb_input_port, SCM_UNDEFINED);
  149. scm_seek (gdb_input_port, SCM_INUM0, scm_from_int (SEEK_SET));
  150. /* Read one object */
  151. ans = scm_read (gdb_input_port);
  152. if (SCM_GC_P)
  153. {
  154. if (SCM_NIMP (ans))
  155. {
  156. SEND_STRING ("Non-immediate created during gc. Memory may be trashed.");
  157. status = -1;
  158. goto exit;
  159. }
  160. }
  161. gdb_result = ans;
  162. /* Protect answer from future GC */
  163. if (SCM_NIMP (ans))
  164. scm_permanent_object (ans);
  165. exit:
  166. remark_port (gdb_input_port);
  167. SCM_END_FOREIGN_BLOCK;
  168. return status;
  169. }
  170. int
  171. gdb_eval (SCM exp)
  172. {
  173. RESET_STRING;
  174. if (SCM_GC_P)
  175. {
  176. SEND_STRING ("Can't evaluate lisp expressions during gc");
  177. return -1;
  178. }
  179. SCM_BEGIN_FOREIGN_BLOCK;
  180. {
  181. SCM env = scm_top_level_env (SCM_TOP_LEVEL_LOOKUP_CLOSURE);
  182. gdb_result = scm_permanent_object (scm_i_eval_x (exp, env));
  183. }
  184. SCM_END_FOREIGN_BLOCK;
  185. return 0;
  186. }
  187. int
  188. gdb_print (SCM obj)
  189. {
  190. if (!scm_initialized_p)
  191. SEND_STRING ("*** Guile not initialized ***");
  192. else
  193. {
  194. RESET_STRING;
  195. SCM_BEGIN_FOREIGN_BLOCK;
  196. /* Reset stream */
  197. scm_seek (gdb_output_port, SCM_INUM0, scm_from_int (SEEK_SET));
  198. scm_write (obj, gdb_output_port);
  199. scm_truncate_file (gdb_output_port, SCM_UNDEFINED);
  200. {
  201. scm_t_port *pt = SCM_PTAB_ENTRY (gdb_output_port);
  202. scm_flush (gdb_output_port);
  203. *(pt->write_buf + pt->read_buf_size) = 0;
  204. SEND_STRING (pt->read_buf);
  205. }
  206. SCM_END_FOREIGN_BLOCK;
  207. }
  208. return 0;
  209. }
  210. int
  211. gdb_binding (SCM name, SCM value)
  212. {
  213. RESET_STRING;
  214. if (SCM_GC_P)
  215. {
  216. SEND_STRING ("Can't create new bindings during gc");
  217. return -1;
  218. }
  219. SCM_BEGIN_FOREIGN_BLOCK;
  220. {
  221. SCM var = scm_sym2var (name, SCM_TOP_LEVEL_LOOKUP_CLOSURE, SCM_BOOL_T);
  222. SCM_VARIABLE_SET (var, value);
  223. }
  224. SCM_END_FOREIGN_BLOCK;
  225. return 0;
  226. }
  227. void
  228. scm_init_gdbint ()
  229. {
  230. static char *s = "scm_init_gdb_interface";
  231. SCM port;
  232. scm_print_carefully_p = 0;
  233. port = scm_mkstrport (SCM_INUM0,
  234. scm_c_make_string (0, SCM_UNDEFINED),
  235. SCM_OPN | SCM_WRTNG,
  236. s);
  237. gdb_output_port = scm_permanent_object (port);
  238. port = scm_mkstrport (SCM_INUM0,
  239. scm_c_make_string (0, SCM_UNDEFINED),
  240. SCM_OPN | SCM_RDNG | SCM_WRTNG,
  241. s);
  242. gdb_input_port = scm_permanent_object (port);
  243. }
  244. /*
  245. Local Variables:
  246. c-file-style: "gnu"
  247. End:
  248. */