gdbint.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /* GDB interface for Guile
  2. * Copyright (C) 1996,1997,1999,2000,2001,2002,2004,2009,2011
  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 SCM gdb_output_port;
  80. int
  81. gdb_maybe_valid_type_p (SCM value)
  82. {
  83. return SCM_IMP (value); /* || scm_in_heap_p (value); */ /* FIXME: What to
  84. do? */
  85. }
  86. int
  87. gdb_read (char *str)
  88. {
  89. #if 0
  90. SCM ans;
  91. int status = 0;
  92. RESET_STRING;
  93. /* Need to be restrictive about what to read? */
  94. if (1) /* (SCM_GC_P) */ /* FIXME */
  95. {
  96. char *p;
  97. for (p = str; *p != '\0'; ++p)
  98. switch (*p)
  99. {
  100. case '(':
  101. case '\'':
  102. case '"':
  103. SEND_STRING ("Can't read this kind of expressions during gc");
  104. return -1;
  105. case '#':
  106. if (*++p == '\0')
  107. goto premature;
  108. if (*p == '\\')
  109. {
  110. if (*++p != '\0')
  111. continue;
  112. premature:
  113. SEND_STRING ("Premature end of lisp expression");
  114. return -1;
  115. }
  116. default:
  117. continue;
  118. }
  119. }
  120. SCM_BEGIN_FOREIGN_BLOCK;
  121. unmark_port (gdb_input_port);
  122. scm_seek (gdb_input_port, SCM_INUM0, scm_from_int (SEEK_SET));
  123. scm_puts_unlocked (str, gdb_input_port);
  124. scm_truncate_file (gdb_input_port, SCM_UNDEFINED);
  125. scm_seek (gdb_input_port, SCM_INUM0, scm_from_int (SEEK_SET));
  126. /* Read one object */
  127. ans = scm_read (gdb_input_port);
  128. if (SCM_GC_P)
  129. {
  130. if (SCM_HEAP_OBJECT_P (ans))
  131. {
  132. SEND_STRING ("Non-immediate created during gc. Memory may be trashed.");
  133. status = -1;
  134. goto exit;
  135. }
  136. }
  137. gdb_result = ans;
  138. /* Protect answer from future GC (FIXME: still needed with BDW-GC?) */
  139. if (SCM_HEAP_OBJECT_P (ans))
  140. scm_permanent_object (ans);
  141. exit:
  142. remark_port (gdb_input_port);
  143. SCM_END_FOREIGN_BLOCK;
  144. return status;
  145. #else
  146. abort ();
  147. #endif
  148. }
  149. int
  150. gdb_eval (SCM exp)
  151. {
  152. RESET_STRING;
  153. if (SCM_GC_P)
  154. {
  155. SEND_STRING ("Can't evaluate lisp expressions during gc");
  156. return -1;
  157. }
  158. SCM_BEGIN_FOREIGN_BLOCK;
  159. {
  160. gdb_result = scm_permanent_object (scm_primitive_eval (exp));
  161. }
  162. SCM_END_FOREIGN_BLOCK;
  163. return 0;
  164. }
  165. int
  166. gdb_print (SCM obj)
  167. {
  168. if (!scm_initialized_p)
  169. SEND_STRING ("*** Guile not initialized ***");
  170. else
  171. {
  172. RESET_STRING;
  173. SCM_BEGIN_FOREIGN_BLOCK;
  174. /* Reset stream */
  175. scm_seek (gdb_output_port, SCM_INUM0, scm_from_int (SEEK_SET));
  176. scm_write (obj, gdb_output_port);
  177. scm_truncate_file (gdb_output_port, SCM_UNDEFINED);
  178. {
  179. scm_t_port *pt = SCM_PTAB_ENTRY (gdb_output_port);
  180. scm_flush_unlocked (gdb_output_port);
  181. *(pt->write_buf + pt->read_buf_size) = 0;
  182. SEND_STRING (pt->read_buf);
  183. }
  184. SCM_END_FOREIGN_BLOCK;
  185. }
  186. return 0;
  187. }
  188. int
  189. gdb_binding (SCM name, SCM value)
  190. {
  191. RESET_STRING;
  192. if (SCM_GC_P)
  193. {
  194. SEND_STRING ("Can't create new bindings during gc");
  195. return -1;
  196. }
  197. SCM_BEGIN_FOREIGN_BLOCK;
  198. {
  199. SCM var = scm_sym2var (name, SCM_TOP_LEVEL_LOOKUP_CLOSURE, SCM_BOOL_T);
  200. SCM_VARIABLE_SET (var, value);
  201. }
  202. SCM_END_FOREIGN_BLOCK;
  203. return 0;
  204. }
  205. void
  206. scm_init_gdbint ()
  207. {
  208. static char *s = "scm_init_gdb_interface";
  209. SCM port;
  210. scm_print_carefully_p = 0;
  211. port = scm_mkstrport (SCM_INUM0, SCM_BOOL_F,
  212. SCM_OPN | SCM_WRTNG,
  213. s);
  214. gdb_output_port = scm_permanent_object (port);
  215. port = scm_mkstrport (SCM_INUM0, SCM_BOOL_F,
  216. SCM_OPN | SCM_RDNG | SCM_WRTNG,
  217. s);
  218. gdb_input_port = scm_permanent_object (port);
  219. }
  220. /*
  221. Local Variables:
  222. c-file-style: "gnu"
  223. End:
  224. */