stacks.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /* A stack holds a frame chain
  2. * Copyright (C) 1996,1997,2000,2001, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 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. #include "libguile/_scm.h"
  23. #include "libguile/control.h"
  24. #include "libguile/eval.h"
  25. #include "libguile/debug.h"
  26. #include "libguile/continuations.h"
  27. #include "libguile/struct.h"
  28. #include "libguile/macros.h"
  29. #include "libguile/procprop.h"
  30. #include "libguile/modules.h"
  31. #include "libguile/root.h"
  32. #include "libguile/strings.h"
  33. #include "libguile/vm.h" /* to capture vm stacks */
  34. #include "libguile/frames.h" /* vm frames */
  35. #include "libguile/validate.h"
  36. #include "libguile/stacks.h"
  37. #include "libguile/private-options.h"
  38. static SCM scm_sys_stacks;
  39. /* {Stacks}
  40. *
  41. * The stack is represented as a struct that holds a frame. The frame itself is
  42. * linked to the next frame, or #f.
  43. *
  44. * Stacks
  45. * Constructor
  46. * make-stack
  47. * Selectors
  48. * stack-id
  49. * stack-ref
  50. * Inspector
  51. * stack-length
  52. */
  53. /* Count number of debug info frames on a stack, beginning with FRAME.
  54. */
  55. static long
  56. stack_depth (enum scm_vm_frame_kind kind, const struct scm_frame *frame)
  57. {
  58. struct scm_frame tmp;
  59. long n = 1;
  60. memcpy (&tmp, frame, sizeof tmp);
  61. while (scm_c_frame_previous (kind, &tmp))
  62. ++n;
  63. return n;
  64. }
  65. /* Narrow STACK by cutting away stackframes (mutatingly).
  66. *
  67. * Inner frames (most recent) are cut by advancing the frames pointer.
  68. * Outer frames are cut by decreasing the recorded length.
  69. *
  70. * Cut maximally INNER inner frames and OUTER outer frames using
  71. * the keys INNER_KEY and OUTER_KEY.
  72. *
  73. * Frames are cut away starting at the end points and moving towards
  74. * the center of the stack. The key is normally compared to the
  75. * operator in application frames. Frames up to and including the key
  76. * are cut.
  77. *
  78. * If INNER_KEY is #t a different scheme is used for inner frames:
  79. *
  80. * Frames up to but excluding the first source frame originating from
  81. * a user module are cut, except for possible application frames
  82. * between the user frame and the last system frame previously
  83. * encountered.
  84. */
  85. static scm_t_ptrdiff
  86. find_prompt (SCM key)
  87. {
  88. scm_t_ptrdiff fp_offset;
  89. if (!scm_dynstack_find_prompt (&SCM_I_CURRENT_THREAD->dynstack, key,
  90. NULL, &fp_offset, NULL, NULL, NULL))
  91. scm_misc_error ("make-stack", "Prompt tag not found while narrowing stack",
  92. scm_list_1 (key));
  93. return fp_offset;
  94. }
  95. static long
  96. narrow_stack (long len, enum scm_vm_frame_kind kind, struct scm_frame *frame,
  97. SCM inner_cut, SCM outer_cut)
  98. {
  99. /* Resolve procedure cuts to address ranges, if possible. If the
  100. debug information has been stripped, this might not be
  101. possible. */
  102. if (scm_is_true (scm_program_p (inner_cut)))
  103. {
  104. SCM addr_range = scm_program_address_range (inner_cut);
  105. if (scm_is_pair (addr_range))
  106. inner_cut = addr_range;
  107. }
  108. if (scm_is_true (scm_program_p (outer_cut)))
  109. {
  110. SCM addr_range = scm_program_address_range (outer_cut);
  111. if (scm_is_pair (addr_range))
  112. outer_cut = addr_range;
  113. }
  114. /* Cut inner part. */
  115. if (scm_is_true (scm_procedure_p (inner_cut)))
  116. {
  117. /* Cut until the given procedure is seen. */
  118. for (; len ;)
  119. {
  120. SCM proc = scm_c_frame_closure (kind, frame);
  121. len--;
  122. scm_c_frame_previous (kind, frame);
  123. if (scm_is_eq (proc, inner_cut))
  124. break;
  125. }
  126. }
  127. else if (scm_is_pair (inner_cut)
  128. && scm_is_integer (scm_car (inner_cut))
  129. && scm_is_integer (scm_cdr (inner_cut)))
  130. {
  131. /* Cut until an IP within the given range is found. */
  132. scm_t_uintptr low_pc, high_pc, pc;
  133. low_pc = scm_to_uintptr_t (scm_car (inner_cut));
  134. high_pc = scm_to_uintptr_t (scm_cdr (inner_cut));
  135. for (; len ;)
  136. {
  137. pc = (scm_t_uintptr) frame->ip;
  138. len--;
  139. scm_c_frame_previous (kind, frame);
  140. if (low_pc <= pc && pc < high_pc)
  141. break;
  142. }
  143. }
  144. else if (scm_is_integer (inner_cut))
  145. {
  146. /* Cut specified number of frames. */
  147. long inner = scm_to_int (inner_cut);
  148. for (; inner && len; --inner)
  149. {
  150. len--;
  151. scm_c_frame_previous (kind, frame);
  152. }
  153. }
  154. else
  155. {
  156. /* Cut until the given prompt tag is seen. */
  157. scm_t_ptrdiff fp_offset = find_prompt (inner_cut);
  158. for (; len; len--, scm_c_frame_previous (kind, frame))
  159. if (fp_offset == frame->fp_offset)
  160. break;
  161. }
  162. /* Cut outer part. */
  163. if (scm_is_true (scm_procedure_p (outer_cut)))
  164. {
  165. long i, new_len;
  166. struct scm_frame tmp;
  167. memcpy (&tmp, frame, sizeof tmp);
  168. /* Cut until the given procedure is seen. */
  169. for (new_len = i = 0; i < len; i++, scm_c_frame_previous (kind, &tmp))
  170. if (scm_is_eq (scm_c_frame_closure (kind, &tmp), outer_cut))
  171. new_len = i;
  172. len = new_len;
  173. }
  174. else if (scm_is_pair (outer_cut)
  175. && scm_is_integer (scm_car (outer_cut))
  176. && scm_is_integer (scm_cdr (outer_cut)))
  177. {
  178. /* Cut until an IP within the given range is found. */
  179. scm_t_uintptr low_pc, high_pc, pc;
  180. long i, new_len;
  181. struct scm_frame tmp;
  182. low_pc = scm_to_uintptr_t (scm_car (outer_cut));
  183. high_pc = scm_to_uintptr_t (scm_cdr (outer_cut));
  184. memcpy (&tmp, frame, sizeof tmp);
  185. /* Cut until the given procedure is seen. */
  186. for (new_len = i = 0; i < len; i++, scm_c_frame_previous (kind, &tmp))
  187. {
  188. pc = (scm_t_uintptr) tmp.ip;
  189. if (low_pc <= pc && pc < high_pc)
  190. new_len = i;
  191. }
  192. len = new_len;
  193. }
  194. else if (scm_is_integer (outer_cut))
  195. {
  196. /* Cut specified number of frames. */
  197. long outer = scm_to_int (outer_cut);
  198. if (outer < len)
  199. len -= outer;
  200. else
  201. len = 0;
  202. }
  203. else
  204. {
  205. /* Cut until the given prompt tag is seen. */
  206. long i;
  207. struct scm_frame tmp;
  208. scm_t_ptrdiff fp_offset = find_prompt (outer_cut);
  209. memcpy (&tmp, frame, sizeof tmp);
  210. for (i = 0; i < len; i++, scm_c_frame_previous (kind, &tmp))
  211. if (tmp.fp_offset == fp_offset)
  212. break;
  213. if (i < len)
  214. len = i;
  215. else
  216. len = 0;
  217. }
  218. return len;
  219. }
  220. /* Stacks
  221. */
  222. SCM scm_stack_type;
  223. SCM_DEFINE (scm_stack_p, "stack?", 1, 0, 0,
  224. (SCM obj),
  225. "Return @code{#t} if @var{obj} is a calling stack.")
  226. #define FUNC_NAME s_scm_stack_p
  227. {
  228. return scm_from_bool(SCM_STACKP (obj));
  229. }
  230. #undef FUNC_NAME
  231. SCM_DEFINE (scm_make_stack, "make-stack", 1, 0, 1,
  232. (SCM obj, SCM args),
  233. "Create a new stack. If @var{obj} is @code{#t}, the current\n"
  234. "evaluation stack is used for creating the stack frames,\n"
  235. "otherwise the frames are taken from @var{obj} (which must be\n"
  236. "a continuation or a frame object).\n"
  237. "\n"
  238. "@var{args} should be a list containing any combination of\n"
  239. "integer, procedure, address range, prompt tag and @code{#t}\n"
  240. "values.\n"
  241. "\n"
  242. "These values specify various ways of cutting away uninteresting\n"
  243. "stack frames from the top and bottom of the stack that\n"
  244. "@code{make-stack} returns. They come in pairs like this:\n"
  245. "@code{(@var{inner_cut_1} @var{outer_cut_1} @var{inner_cut_2}\n"
  246. "@var{outer_cut_2} @dots{})}.\n"
  247. "\n"
  248. "Each @var{inner_cut_i} can be an integer, a procedure, an\n"
  249. "address range, or a prompt tag. An integer means to cut away\n"
  250. "exactly that number of frames. A procedure means to cut\n"
  251. "away all frames up to but excluding the frame whose procedure\n"
  252. "matches the specified one. An address range is a pair of\n"
  253. "integers indicating the low and high addresses of a procedure's\n"
  254. "code, and is the same as cutting away to a procedure (though\n"
  255. "with less work). Anything else is interpreted as a prompt tag\n"
  256. "which cuts away all frames that are inside a prompt with the\n"
  257. "given tag.\n"
  258. "\n"
  259. "Each @var{outer_cut_i} can be an integer, a procedure, an\n"
  260. "address range, or a prompt tag. An integer means to cut away\n"
  261. "that number of frames. A procedure means to cut away frames\n"
  262. "down to but excluding the frame whose procedure matches the\n"
  263. "specified one. An address range is the same, but with the\n"
  264. "procedure's code specified as an address range. Anything else\n"
  265. "is taken to be a prompt tag, which cuts away all frames that are\n"
  266. "outside a prompt with the given tag.\n"
  267. "\n"
  268. "If the @var{outer_cut_i} of the last pair is missing, it is\n"
  269. "taken as 0.")
  270. #define FUNC_NAME s_scm_make_stack
  271. {
  272. long n;
  273. SCM inner_cut, outer_cut;
  274. enum scm_vm_frame_kind kind;
  275. struct scm_frame frame;
  276. /* Extract a pointer to the innermost frame of whatever object
  277. scm_make_stack was given. */
  278. if (scm_is_eq (obj, SCM_BOOL_T))
  279. {
  280. SCM cont;
  281. struct scm_vm_cont *c;
  282. union scm_vm_stack_element *stack_top;
  283. cont = scm_i_capture_current_stack ();
  284. c = SCM_VM_CONT_DATA (cont);
  285. /* FIXME vm_cont should hold fp/sp offsets */
  286. stack_top = c->stack_bottom + c->stack_size;
  287. kind = SCM_VM_FRAME_KIND_CONT;
  288. frame.stack_holder = c;
  289. frame.fp_offset = stack_top - (c->fp + c->reloc);
  290. frame.sp_offset = c->stack_size;
  291. frame.ip = c->ra;
  292. }
  293. else if (SCM_VM_FRAME_P (obj))
  294. {
  295. kind = SCM_VM_FRAME_KIND (obj);
  296. memcpy (&frame, SCM_VM_FRAME_DATA (obj), sizeof frame);
  297. }
  298. else if (SCM_CONTINUATIONP (obj))
  299. /* FIXME: Narrowing to prompt tags should narrow with respect to the prompts
  300. that were in place when the continuation was captured. */
  301. {
  302. kind = SCM_VM_FRAME_KIND_CONT;
  303. if (!scm_i_continuation_to_frame (obj, &frame))
  304. return SCM_BOOL_F;
  305. }
  306. else if (SCM_PROGRAM_P (obj) && SCM_PROGRAM_IS_PARTIAL_CONTINUATION (obj))
  307. {
  308. kind = SCM_VM_FRAME_KIND_CONT;
  309. if (!scm_i_vm_cont_to_frame (SCM_PROGRAM_FREE_VARIABLE_REF (obj, 0),
  310. &frame))
  311. return SCM_BOOL_F;
  312. }
  313. else
  314. {
  315. SCM_WRONG_TYPE_ARG (SCM_ARG1, obj);
  316. /* not reached */
  317. }
  318. /* Skip initial boot frame, if any. This is possible if the frame
  319. originates from a captured continuation. */
  320. if (scm_i_vm_is_boot_continuation_code (frame.ip)
  321. && !scm_c_frame_previous (kind, &frame))
  322. return SCM_BOOL_F;
  323. /* Count number of frames. Also get stack id tag and check whether
  324. there are more stackframes than we want to record
  325. (SCM_BACKTRACE_MAXDEPTH). */
  326. n = stack_depth (kind, &frame);
  327. /* Narrow the stack according to the arguments given to scm_make_stack. */
  328. SCM_VALIDATE_REST_ARGUMENT (args);
  329. while (n > 0 && !scm_is_null (args))
  330. {
  331. inner_cut = SCM_CAR (args);
  332. args = SCM_CDR (args);
  333. if (scm_is_null (args))
  334. {
  335. outer_cut = SCM_INUM0;
  336. }
  337. else
  338. {
  339. outer_cut = SCM_CAR (args);
  340. args = SCM_CDR (args);
  341. }
  342. n = narrow_stack (n, kind, &frame, inner_cut, outer_cut);
  343. }
  344. if (n > 0)
  345. {
  346. /* Make the stack object. */
  347. SCM stack = scm_make_struct (scm_stack_type, SCM_INUM0, SCM_EOL);
  348. SCM_SET_STACK_LENGTH (stack, n);
  349. SCM_SET_STACK_ID (stack, scm_stack_id (obj));
  350. SCM_SET_STACK_FRAME (stack, scm_c_make_frame (kind, &frame));
  351. return stack;
  352. }
  353. else
  354. return SCM_BOOL_F;
  355. }
  356. #undef FUNC_NAME
  357. SCM_DEFINE (scm_stack_id, "stack-id", 1, 0, 0,
  358. (SCM stack),
  359. "Return the identifier given to @var{stack} by @code{start-stack}.")
  360. #define FUNC_NAME s_scm_stack_id
  361. {
  362. if (scm_is_eq (stack, SCM_BOOL_T)
  363. /* FIXME: frame case assumes frame still live on the stack, and no
  364. intervening start-stack. Hmm... */
  365. || SCM_VM_FRAME_P (stack))
  366. {
  367. /* Fetch most recent start-stack tag. */
  368. SCM stacks = scm_fluid_ref (scm_sys_stacks);
  369. return scm_is_pair (stacks) ? scm_caar (stacks) : SCM_BOOL_F;
  370. }
  371. else if (SCM_CONTINUATIONP (stack))
  372. /* FIXME: implement me */
  373. return SCM_BOOL_F;
  374. else if (SCM_PROGRAM_P (stack) && SCM_PROGRAM_IS_PARTIAL_CONTINUATION (stack))
  375. /* FIXME: implement me */
  376. return SCM_BOOL_F;
  377. else
  378. {
  379. SCM_WRONG_TYPE_ARG (SCM_ARG1, stack);
  380. /* not reached */
  381. }
  382. }
  383. #undef FUNC_NAME
  384. SCM_DEFINE (scm_stack_ref, "stack-ref", 2, 0, 0,
  385. (SCM stack, SCM index),
  386. "Return the @var{index}'th frame from @var{stack}.")
  387. #define FUNC_NAME s_scm_stack_ref
  388. {
  389. unsigned long int c_index;
  390. SCM frame;
  391. SCM_VALIDATE_STACK (1, stack);
  392. c_index = scm_to_unsigned_integer (index, 0, SCM_STACK_LENGTH(stack)-1);
  393. frame = SCM_STACK_FRAME (stack);
  394. while (c_index--)
  395. frame = scm_frame_previous (frame);
  396. return frame;
  397. }
  398. #undef FUNC_NAME
  399. SCM_DEFINE (scm_stack_length, "stack-length", 1, 0, 0,
  400. (SCM stack),
  401. "Return the length of @var{stack}.")
  402. #define FUNC_NAME s_scm_stack_length
  403. {
  404. SCM_VALIDATE_STACK (1, stack);
  405. return scm_from_long (SCM_STACK_LENGTH (stack));
  406. }
  407. #undef FUNC_NAME
  408. void
  409. scm_init_stacks ()
  410. {
  411. scm_sys_stacks = scm_make_fluid ();
  412. scm_c_define ("%stacks", scm_sys_stacks);
  413. scm_stack_type = scm_make_vtable (scm_from_locale_string (SCM_STACK_LAYOUT),
  414. SCM_UNDEFINED);
  415. scm_set_struct_vtable_name_x (scm_stack_type,
  416. scm_from_latin1_symbol ("stack"));
  417. #include "libguile/stacks.x"
  418. }
  419. /*
  420. Local Variables:
  421. c-file-style: "gnu"
  422. End:
  423. */