stacks.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. /* Representation of stack frame debug information
  2. * Copyright (C) 1996,1997,2000,2001, 2006, 2007, 2008, 2009 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/eval.h"
  24. #include "libguile/debug.h"
  25. #include "libguile/continuations.h"
  26. #include "libguile/struct.h"
  27. #include "libguile/macros.h"
  28. #include "libguile/procprop.h"
  29. #include "libguile/modules.h"
  30. #include "libguile/root.h"
  31. #include "libguile/strings.h"
  32. #include "libguile/vm.h" /* to capture vm stacks */
  33. #include "libguile/frames.h" /* vm frames */
  34. #include "libguile/validate.h"
  35. #include "libguile/stacks.h"
  36. #include "libguile/private-options.h"
  37. /* {Frames and stacks}
  38. *
  39. * The debugging evaluator creates debug frames on the stack. These
  40. * are linked from the innermost frame and outwards. The last frame
  41. * created can always be accessed as SCM_LAST_DEBUG_FRAME.
  42. * Continuations contain a pointer to the innermost debug frame on the
  43. * continuation stack.
  44. *
  45. * Each debug frame contains a set of flags and information about one
  46. * or more stack frames. The case of multiple frames occurs due to
  47. * tail recursion. The maximal number of stack frames which can be
  48. * recorded in one debug frame can be set dynamically with the debug
  49. * option FRAMES.
  50. *
  51. * Stack frame information is of two types: eval information (the
  52. * expression being evaluated and its environment) and apply
  53. * information (the procedure being applied and its arguments). A
  54. * stack frame normally corresponds to an eval/apply pair, but macros
  55. * and special forms (which are implemented as macros in Guile) only
  56. * have eval information and apply calls leads to apply only frames.
  57. *
  58. * Since we want to record the total stack information and later
  59. * manipulate this data at the scheme level in the debugger, we need
  60. * to transform it into a new representation. In the following code
  61. * section you'll find the functions implementing this data type.
  62. *
  63. * Representation:
  64. *
  65. * The stack is represented as a struct with an id slot and a tail
  66. * array of scm_t_info_frame structs.
  67. *
  68. * A frame is represented as a pair where the car contains a stack and
  69. * the cdr an inum. The inum is an index to the first SCM value of
  70. * the scm_t_info_frame struct.
  71. *
  72. * Stacks
  73. * Constructor
  74. * make-stack
  75. * Selectors
  76. * stack-id
  77. * stack-ref
  78. * Inspector
  79. * stack-length
  80. *
  81. * Frames
  82. * Constructor
  83. * last-stack-frame
  84. * Selectors
  85. * frame-number
  86. * frame-source
  87. * frame-procedure
  88. * frame-arguments
  89. * frame-previous
  90. * frame-next
  91. * Predicates
  92. * frame-real?
  93. * frame-procedure?
  94. * frame-evaluating-args?
  95. * frame-overflow? */
  96. /* Some auxiliary functions for reading debug frames off the stack.
  97. */
  98. /* Stacks often contain pointers to other items on the stack; for
  99. example, each scm_t_debug_frame structure contains a pointer to the
  100. next frame out. When we capture a continuation, we copy the stack
  101. into the heap, and just leave all the pointers unchanged. This
  102. makes it simple to restore the continuation --- just copy the stack
  103. back! However, if we retrieve a pointer from the heap copy to
  104. another item that was originally on the stack, we have to add an
  105. offset to the pointer to discover the new referent.
  106. If PTR is a pointer retrieved from a continuation, whose original
  107. target was on the stack, and OFFSET is the appropriate offset from
  108. the original stack to the continuation, then RELOC_MUMBLE (PTR,
  109. OFFSET) is a pointer to the copy in the continuation of the
  110. original referent, cast to an scm_debug_MUMBLE *. */
  111. #define RELOC_INFO(ptr, offset) \
  112. ((scm_t_debug_info *) ((SCM_STACKITEM *) (ptr) + (offset)))
  113. #define RELOC_FRAME(ptr, offset) \
  114. ((scm_t_debug_frame *) ((SCM_STACKITEM *) (ptr) + (offset)))
  115. /* Count number of debug info frames on a stack, beginning with
  116. * DFRAME. OFFSET is used for relocation of pointers when the stack
  117. * is read from a continuation.
  118. */
  119. static long
  120. stack_depth (scm_t_debug_frame *dframe, scm_t_ptrdiff offset, SCM vmframe,
  121. SCM *id)
  122. {
  123. long n;
  124. for (n = 0;
  125. dframe && !SCM_VOIDFRAMEP (*dframe);
  126. dframe = RELOC_FRAME (dframe->prev, offset))
  127. {
  128. if (SCM_EVALFRAMEP (*dframe))
  129. {
  130. scm_t_debug_info *info = RELOC_INFO (dframe->info, offset);
  131. scm_t_debug_info *vect = RELOC_INFO (dframe->vect, offset);
  132. n += (info - vect) / 2 + 1;
  133. /* Data in the apply part of an eval info frame comes from previous
  134. stack frame if the scm_t_debug_info vector is overflowed. */
  135. if ((((info - vect) & 1) == 0)
  136. && SCM_OVERFLOWP (*dframe)
  137. && !SCM_UNBNDP (info[1].a.proc))
  138. ++n;
  139. }
  140. else if (SCM_APPLYFRAMEP (*dframe))
  141. {
  142. scm_t_debug_info *vect = RELOC_INFO (dframe->vect, offset);
  143. if (SCM_PROGRAM_P (vect[0].a.proc))
  144. {
  145. if (!SCM_PROGRAM_IS_BOOT (vect[0].a.proc))
  146. /* Programs can end up in the debug stack via deval; but we just
  147. ignore those, because we know that the debugging VM engine
  148. pushes one dframe per invocation, with the boot program as
  149. the proc, so we only count those. */
  150. continue;
  151. /* count vmframe back to previous boot frame */
  152. for (; scm_is_true (vmframe); vmframe = scm_c_vm_frame_prev (vmframe))
  153. {
  154. if (!SCM_PROGRAM_IS_BOOT (scm_vm_frame_program (vmframe)))
  155. ++n;
  156. else
  157. { /* skip boot frame, cut out of the vm backtrace */
  158. vmframe = scm_c_vm_frame_prev (vmframe);
  159. break;
  160. }
  161. }
  162. }
  163. else
  164. ++n; /* increment for non-program apply frame */
  165. }
  166. else
  167. ++n;
  168. }
  169. if (dframe && SCM_VOIDFRAMEP (*dframe))
  170. *id = RELOC_INFO(dframe->vect, offset)[0].id;
  171. return n;
  172. }
  173. /* Read debug info from DFRAME into IFRAME.
  174. */
  175. static void
  176. read_frame (scm_t_debug_frame *dframe, scm_t_ptrdiff offset,
  177. scm_t_info_frame *iframe)
  178. {
  179. scm_t_bits flags = SCM_UNPACK (SCM_INUM0); /* UGh. */
  180. if (SCM_EVALFRAMEP (*dframe))
  181. {
  182. scm_t_debug_info *info = RELOC_INFO (dframe->info, offset);
  183. scm_t_debug_info *vect = RELOC_INFO (dframe->vect, offset);
  184. if ((info - vect) & 1)
  185. {
  186. /* Debug.vect ends with apply info. */
  187. --info;
  188. if (!SCM_UNBNDP (info[1].a.proc))
  189. {
  190. flags |= SCM_FRAMEF_PROC;
  191. iframe->proc = info[1].a.proc;
  192. iframe->args = info[1].a.args;
  193. if (!SCM_ARGS_READY_P (*dframe))
  194. flags |= SCM_FRAMEF_EVAL_ARGS;
  195. }
  196. }
  197. iframe->source = scm_make_memoized (info[0].e.exp, info[0].e.env);
  198. }
  199. else
  200. {
  201. scm_t_debug_info *vect = RELOC_INFO (dframe->vect, offset);
  202. flags |= SCM_FRAMEF_PROC;
  203. iframe->proc = vect[0].a.proc;
  204. iframe->args = vect[0].a.args;
  205. }
  206. iframe->flags = flags;
  207. }
  208. /* Look up the first body form of the apply closure. We'll use this
  209. below to prevent it from being displayed.
  210. */
  211. static SCM
  212. get_applybody ()
  213. {
  214. SCM var = scm_sym2var (scm_sym_apply, SCM_BOOL_F, SCM_BOOL_F);
  215. if (SCM_VARIABLEP (var) && SCM_CLOSUREP (SCM_VARIABLE_REF (var)))
  216. return SCM_CAR (SCM_CLOSURE_BODY (SCM_VARIABLE_REF (var)));
  217. else
  218. return SCM_UNDEFINED;
  219. }
  220. #define NEXT_FRAME(iframe, n, quit) \
  221. do { \
  222. if (SCM_MEMOIZEDP (iframe->source) \
  223. && scm_is_eq (SCM_MEMOIZED_EXP (iframe->source), applybody)) \
  224. { \
  225. iframe->source = SCM_BOOL_F; \
  226. if (scm_is_false (iframe->proc)) \
  227. { \
  228. --iframe; \
  229. ++n; \
  230. } \
  231. } \
  232. ++iframe; \
  233. if (--n == 0) \
  234. goto quit; \
  235. } while (0)
  236. /* Fill the scm_t_info_frame vector IFRAME with data from N stack frames
  237. * starting with the first stack frame represented by debug frame
  238. * DFRAME.
  239. */
  240. static scm_t_bits
  241. read_frames (scm_t_debug_frame *dframe, scm_t_ptrdiff offset,
  242. SCM vmframe, long n, scm_t_info_frame *iframes)
  243. {
  244. scm_t_info_frame *iframe = iframes;
  245. scm_t_debug_info *info, *vect;
  246. static SCM applybody = SCM_UNDEFINED;
  247. /* The value of applybody has to be setup after r4rs.scm has executed. */
  248. if (SCM_UNBNDP (applybody))
  249. applybody = get_applybody ();
  250. for (;
  251. dframe && !SCM_VOIDFRAMEP (*dframe) && n > 0;
  252. dframe = RELOC_FRAME (dframe->prev, offset))
  253. {
  254. read_frame (dframe, offset, iframe);
  255. if (SCM_EVALFRAMEP (*dframe))
  256. {
  257. /* If current frame is a macro during expansion, we should
  258. skip the previously recorded macro transformer
  259. application frame. */
  260. if (SCM_MACROEXPP (*dframe) && iframe > iframes)
  261. {
  262. *(iframe - 1) = *iframe;
  263. --iframe;
  264. }
  265. info = RELOC_INFO (dframe->info, offset);
  266. vect = RELOC_INFO (dframe->vect, offset);
  267. if ((info - vect) & 1)
  268. --info;
  269. /* Data in the apply part of an eval info frame comes from
  270. previous stack frame if the scm_t_debug_info vector is
  271. overflowed. */
  272. else if (SCM_OVERFLOWP (*dframe)
  273. && !SCM_UNBNDP (info[1].a.proc))
  274. {
  275. NEXT_FRAME (iframe, n, quit);
  276. iframe->flags = SCM_UNPACK(SCM_INUM0) | SCM_FRAMEF_PROC;
  277. iframe->proc = info[1].a.proc;
  278. iframe->args = info[1].a.args;
  279. }
  280. if (SCM_OVERFLOWP (*dframe))
  281. iframe->flags |= SCM_FRAMEF_OVERFLOW;
  282. info -= 2;
  283. NEXT_FRAME (iframe, n, quit);
  284. while (info >= vect)
  285. {
  286. if (!SCM_UNBNDP (info[1].a.proc))
  287. {
  288. iframe->flags = SCM_UNPACK(SCM_INUM0) | SCM_FRAMEF_PROC;
  289. iframe->proc = info[1].a.proc;
  290. iframe->args = info[1].a.args;
  291. }
  292. else
  293. iframe->flags = SCM_UNPACK (SCM_INUM0);
  294. iframe->source = scm_make_memoized (info[0].e.exp,
  295. info[0].e.env);
  296. info -= 2;
  297. NEXT_FRAME (iframe, n, quit);
  298. }
  299. }
  300. else if (SCM_PROGRAM_P (iframe->proc))
  301. {
  302. if (!SCM_PROGRAM_IS_BOOT (iframe->proc))
  303. /* Programs can end up in the debug stack via deval; but we just
  304. ignore those, because we know that the debugging VM engine
  305. pushes one dframe per invocation, with the boot program as
  306. the proc, so we only count those. */
  307. continue;
  308. for (; scm_is_true (vmframe);
  309. vmframe = scm_c_vm_frame_prev (vmframe))
  310. {
  311. if (SCM_PROGRAM_IS_BOOT (scm_vm_frame_program (vmframe)))
  312. { /* skip boot frame, back to interpreted frames */
  313. vmframe = scm_c_vm_frame_prev (vmframe);
  314. break;
  315. }
  316. else
  317. {
  318. /* Oh dear, oh dear, oh dear. */
  319. iframe->flags = SCM_UNPACK (SCM_INUM0) | SCM_FRAMEF_PROC;
  320. iframe->source = scm_vm_frame_source (vmframe);
  321. iframe->proc = scm_vm_frame_program (vmframe);
  322. iframe->args = scm_vm_frame_arguments (vmframe);
  323. ++iframe;
  324. if (--n == 0)
  325. goto quit;
  326. }
  327. }
  328. }
  329. else
  330. {
  331. NEXT_FRAME (iframe, n, quit);
  332. }
  333. quit:
  334. if (iframe > iframes)
  335. (iframe - 1) -> flags |= SCM_FRAMEF_REAL;
  336. }
  337. return iframe - iframes; /* Number of frames actually read */
  338. }
  339. /* Narrow STACK by cutting away stackframes (mutatingly).
  340. *
  341. * Inner frames (most recent) are cut by advancing the frames pointer.
  342. * Outer frames are cut by decreasing the recorded length.
  343. *
  344. * Cut maximally INNER inner frames and OUTER outer frames using
  345. * the keys INNER_KEY and OUTER_KEY.
  346. *
  347. * Frames are cut away starting at the end points and moving towards
  348. * the center of the stack. The key is normally compared to the
  349. * operator in application frames. Frames up to and including the key
  350. * are cut.
  351. *
  352. * If INNER_KEY is #t a different scheme is used for inner frames:
  353. *
  354. * Frames up to but excluding the first source frame originating from
  355. * a user module are cut, except for possible application frames
  356. * between the user frame and the last system frame previously
  357. * encountered.
  358. */
  359. static void
  360. narrow_stack (SCM stack, long inner, SCM inner_key, long outer, SCM outer_key)
  361. {
  362. scm_t_stack *s = SCM_STACK (stack);
  363. unsigned long int i;
  364. long n = s->length;
  365. /* Cut inner part. */
  366. if (scm_is_eq (inner_key, SCM_BOOL_T))
  367. {
  368. /* Cut all frames up to user module code */
  369. for (i = 0; inner; ++i, --inner)
  370. {
  371. SCM m = s->frames[i].source;
  372. if (SCM_MEMOIZEDP (m)
  373. && !SCM_IMP (SCM_MEMOIZED_ENV (m))
  374. && scm_is_false (scm_system_module_env_p (SCM_MEMOIZED_ENV (m))))
  375. {
  376. /* Back up in order to include any non-source frames */
  377. while (i > 0)
  378. {
  379. m = s->frames[i - 1].source;
  380. if (SCM_MEMOIZEDP (m))
  381. break;
  382. m = s->frames[i - 1].proc;
  383. if (scm_is_true (scm_procedure_p (m))
  384. && scm_is_true (scm_procedure_property
  385. (m, scm_sym_system_procedure)))
  386. break;
  387. --i;
  388. ++inner;
  389. }
  390. break;
  391. }
  392. }
  393. }
  394. else
  395. /* Use standard cutting procedure. */
  396. {
  397. for (i = 0; inner; --inner)
  398. if (scm_is_eq (s->frames[i++].proc, inner_key))
  399. break;
  400. }
  401. s->frames = &s->frames[i];
  402. n -= i;
  403. /* Cut outer part. */
  404. for (; n && outer; --outer)
  405. if (scm_is_eq (s->frames[--n].proc, outer_key))
  406. break;
  407. s->length = n;
  408. }
  409. /* Stacks
  410. */
  411. SCM scm_stack_type;
  412. SCM_DEFINE (scm_stack_p, "stack?", 1, 0, 0,
  413. (SCM obj),
  414. "Return @code{#t} if @var{obj} is a calling stack.")
  415. #define FUNC_NAME s_scm_stack_p
  416. {
  417. return scm_from_bool(SCM_STACKP (obj));
  418. }
  419. #undef FUNC_NAME
  420. SCM_DEFINE (scm_make_stack, "make-stack", 1, 0, 1,
  421. (SCM obj, SCM args),
  422. "Create a new stack. If @var{obj} is @code{#t}, the current\n"
  423. "evaluation stack is used for creating the stack frames,\n"
  424. "otherwise the frames are taken from @var{obj} (which must be\n"
  425. "either a debug object or a continuation).\n\n"
  426. "@var{args} should be a list containing any combination of\n"
  427. "integer, procedure and @code{#t} values.\n\n"
  428. "These values specify various ways of cutting away uninteresting\n"
  429. "stack frames from the top and bottom of the stack that\n"
  430. "@code{make-stack} returns. They come in pairs like this:\n"
  431. "@code{(@var{inner_cut_1} @var{outer_cut_1} @var{inner_cut_2}\n"
  432. "@var{outer_cut_2} @dots{})}.\n\n"
  433. "Each @var{inner_cut_N} can be @code{#t}, an integer, or a\n"
  434. "procedure. @code{#t} means to cut away all frames up to but\n"
  435. "excluding the first user module frame. An integer means to cut\n"
  436. "away exactly that number of frames. A procedure means to cut\n"
  437. "away all frames up to but excluding the application frame whose\n"
  438. "procedure matches the specified one.\n\n"
  439. "Each @var{outer_cut_N} can be an integer or a procedure. An\n"
  440. "integer means to cut away that number of frames. A procedure\n"
  441. "means to cut away frames down to but excluding the application\n"
  442. "frame whose procedure matches the specified one.\n\n"
  443. "If the @var{outer_cut_N} of the last pair is missing, it is\n"
  444. "taken as 0.")
  445. #define FUNC_NAME s_scm_make_stack
  446. {
  447. long n, size;
  448. int maxp;
  449. scm_t_debug_frame *dframe;
  450. scm_t_info_frame *iframe;
  451. SCM vmframe;
  452. long offset = 0;
  453. SCM stack, id;
  454. SCM inner_cut, outer_cut;
  455. /* Extract a pointer to the innermost frame of whatever object
  456. scm_make_stack was given. */
  457. if (scm_is_eq (obj, SCM_BOOL_T))
  458. {
  459. struct scm_vm *vp = SCM_VM_DATA (scm_the_vm ());
  460. dframe = scm_i_last_debug_frame ();
  461. vmframe = scm_c_make_vm_frame (scm_the_vm (), vp->fp, vp->sp, vp->ip, 0);
  462. }
  463. else if (SCM_DEBUGOBJP (obj))
  464. {
  465. dframe = SCM_DEBUGOBJ_FRAME (obj);
  466. vmframe = SCM_BOOL_F;
  467. }
  468. else if (SCM_VM_FRAME_P (obj))
  469. {
  470. dframe = NULL;
  471. vmframe = obj;
  472. }
  473. else if (SCM_CONTINUATIONP (obj))
  474. {
  475. scm_t_contregs *cont = SCM_CONTREGS (obj);
  476. offset = cont->offset;
  477. dframe = RELOC_FRAME (cont->dframe, offset);
  478. if (!scm_is_null (cont->vm_conts))
  479. { SCM vm_cont;
  480. struct scm_vm_cont *data;
  481. vm_cont = scm_cdr (scm_car (cont->vm_conts));
  482. data = SCM_VM_CONT_DATA (vm_cont);
  483. vmframe = scm_c_make_vm_frame (vm_cont,
  484. data->fp + data->reloc,
  485. data->sp + data->reloc,
  486. data->ip,
  487. data->reloc);
  488. } else
  489. vmframe = SCM_BOOL_F;
  490. }
  491. else
  492. {
  493. SCM_WRONG_TYPE_ARG (SCM_ARG1, obj);
  494. /* not reached */
  495. }
  496. /* Count number of frames. Also get stack id tag and check whether
  497. there are more stackframes than we want to record
  498. (SCM_BACKTRACE_MAXDEPTH). */
  499. id = SCM_BOOL_F;
  500. maxp = 0;
  501. n = stack_depth (dframe, offset, vmframe, &id);
  502. /* FIXME: redo maxp? */
  503. size = n * SCM_FRAME_N_SLOTS;
  504. /* Make the stack object. */
  505. stack = scm_make_struct (scm_stack_type, scm_from_long (size), SCM_EOL);
  506. SCM_STACK (stack) -> id = id;
  507. iframe = &SCM_STACK (stack) -> tail[0];
  508. SCM_STACK (stack) -> frames = iframe;
  509. SCM_STACK (stack) -> length = n;
  510. /* Translate the current chain of stack frames into debugging information. */
  511. n = read_frames (dframe, offset, vmframe, n, iframe);
  512. if (n != SCM_STACK (stack)->length)
  513. {
  514. scm_puts ("warning: stack count incorrect!\n", scm_current_error_port ());
  515. SCM_STACK (stack)->length = n;
  516. }
  517. /* Narrow the stack according to the arguments given to scm_make_stack. */
  518. SCM_VALIDATE_REST_ARGUMENT (args);
  519. while (n > 0 && !scm_is_null (args))
  520. {
  521. inner_cut = SCM_CAR (args);
  522. args = SCM_CDR (args);
  523. if (scm_is_null (args))
  524. {
  525. outer_cut = SCM_INUM0;
  526. }
  527. else
  528. {
  529. outer_cut = SCM_CAR (args);
  530. args = SCM_CDR (args);
  531. }
  532. narrow_stack (stack,
  533. scm_is_integer (inner_cut) ? scm_to_int (inner_cut) : n,
  534. scm_is_integer (inner_cut) ? 0 : inner_cut,
  535. scm_is_integer (outer_cut) ? scm_to_int (outer_cut) : n,
  536. scm_is_integer (outer_cut) ? 0 : outer_cut);
  537. n = SCM_STACK (stack) -> length;
  538. }
  539. if (n > 0 && maxp)
  540. iframe[n - 1].flags |= SCM_FRAMEF_OVERFLOW;
  541. if (n > 0)
  542. return stack;
  543. else
  544. return SCM_BOOL_F;
  545. }
  546. #undef FUNC_NAME
  547. SCM_DEFINE (scm_stack_id, "stack-id", 1, 0, 0,
  548. (SCM stack),
  549. "Return the identifier given to @var{stack} by @code{start-stack}.")
  550. #define FUNC_NAME s_scm_stack_id
  551. {
  552. scm_t_debug_frame *dframe;
  553. long offset = 0;
  554. if (scm_is_eq (stack, SCM_BOOL_T))
  555. {
  556. dframe = scm_i_last_debug_frame ();
  557. }
  558. else if (SCM_DEBUGOBJP (stack))
  559. {
  560. dframe = SCM_DEBUGOBJ_FRAME (stack);
  561. }
  562. else if (SCM_CONTINUATIONP (stack))
  563. {
  564. scm_t_contregs *cont = SCM_CONTREGS (stack);
  565. offset = cont->offset;
  566. dframe = RELOC_FRAME (cont->dframe, offset);
  567. }
  568. else if (SCM_STACKP (stack))
  569. {
  570. return SCM_STACK (stack) -> id;
  571. }
  572. else
  573. {
  574. SCM_WRONG_TYPE_ARG (1, stack);
  575. }
  576. while (dframe && !SCM_VOIDFRAMEP (*dframe))
  577. dframe = RELOC_FRAME (dframe->prev, offset);
  578. if (dframe && SCM_VOIDFRAMEP (*dframe))
  579. return RELOC_INFO (dframe->vect, offset)[0].id;
  580. return SCM_BOOL_F;
  581. }
  582. #undef FUNC_NAME
  583. SCM_DEFINE (scm_stack_ref, "stack-ref", 2, 0, 0,
  584. (SCM stack, SCM index),
  585. "Return the @var{index}'th frame from @var{stack}.")
  586. #define FUNC_NAME s_scm_stack_ref
  587. {
  588. unsigned long int c_index;
  589. SCM_VALIDATE_STACK (1, stack);
  590. c_index = scm_to_unsigned_integer (index, 0, SCM_STACK_LENGTH(stack)-1);
  591. return scm_cons (stack, index);
  592. }
  593. #undef FUNC_NAME
  594. SCM_DEFINE (scm_stack_length, "stack-length", 1, 0, 0,
  595. (SCM stack),
  596. "Return the length of @var{stack}.")
  597. #define FUNC_NAME s_scm_stack_length
  598. {
  599. SCM_VALIDATE_STACK (1, stack);
  600. return scm_from_int (SCM_STACK_LENGTH (stack));
  601. }
  602. #undef FUNC_NAME
  603. /* Frames
  604. */
  605. SCM_DEFINE (scm_frame_p, "frame?", 1, 0, 0,
  606. (SCM obj),
  607. "Return @code{#t} if @var{obj} is a stack frame.")
  608. #define FUNC_NAME s_scm_frame_p
  609. {
  610. return scm_from_bool(SCM_FRAMEP (obj));
  611. }
  612. #undef FUNC_NAME
  613. SCM_DEFINE (scm_last_stack_frame, "last-stack-frame", 1, 0, 0,
  614. (SCM obj),
  615. "Return the last (innermost) frame of @var{obj}, which must be\n"
  616. "either a debug object or a continuation.")
  617. #define FUNC_NAME s_scm_last_stack_frame
  618. {
  619. scm_t_debug_frame *dframe;
  620. long offset = 0;
  621. SCM stack;
  622. if (SCM_DEBUGOBJP (obj))
  623. {
  624. dframe = SCM_DEBUGOBJ_FRAME (obj);
  625. }
  626. else if (SCM_CONTINUATIONP (obj))
  627. {
  628. scm_t_contregs *cont = SCM_CONTREGS (obj);
  629. offset = cont->offset;
  630. dframe = RELOC_FRAME (cont->dframe, offset);
  631. }
  632. else
  633. {
  634. SCM_WRONG_TYPE_ARG (1, obj);
  635. /* not reached */
  636. }
  637. if (!dframe || SCM_VOIDFRAMEP (*dframe))
  638. return SCM_BOOL_F;
  639. stack = scm_make_struct (scm_stack_type, scm_from_int (SCM_FRAME_N_SLOTS),
  640. SCM_EOL);
  641. SCM_STACK (stack) -> length = 1;
  642. SCM_STACK (stack) -> frames = &SCM_STACK (stack) -> tail[0];
  643. read_frame (dframe, offset,
  644. (scm_t_info_frame *) &SCM_STACK (stack) -> frames[0]);
  645. return scm_cons (stack, SCM_INUM0);
  646. }
  647. #undef FUNC_NAME
  648. SCM_DEFINE (scm_frame_number, "frame-number", 1, 0, 0,
  649. (SCM frame),
  650. "Return the frame number of @var{frame}.")
  651. #define FUNC_NAME s_scm_frame_number
  652. {
  653. SCM_VALIDATE_FRAME (1, frame);
  654. return scm_from_int (SCM_FRAME_NUMBER (frame));
  655. }
  656. #undef FUNC_NAME
  657. SCM_DEFINE (scm_frame_source, "frame-source", 1, 0, 0,
  658. (SCM frame),
  659. "Return the source of @var{frame}.")
  660. #define FUNC_NAME s_scm_frame_source
  661. {
  662. SCM_VALIDATE_FRAME (1, frame);
  663. return SCM_FRAME_SOURCE (frame);
  664. }
  665. #undef FUNC_NAME
  666. SCM_DEFINE (scm_frame_procedure, "frame-procedure", 1, 0, 0,
  667. (SCM frame),
  668. "Return the procedure for @var{frame}, or @code{#f} if no\n"
  669. "procedure is associated with @var{frame}.")
  670. #define FUNC_NAME s_scm_frame_procedure
  671. {
  672. SCM_VALIDATE_FRAME (1, frame);
  673. return (SCM_FRAME_PROC_P (frame)
  674. ? SCM_FRAME_PROC (frame)
  675. : SCM_BOOL_F);
  676. }
  677. #undef FUNC_NAME
  678. SCM_DEFINE (scm_frame_arguments, "frame-arguments", 1, 0, 0,
  679. (SCM frame),
  680. "Return the arguments of @var{frame}.")
  681. #define FUNC_NAME s_scm_frame_arguments
  682. {
  683. SCM_VALIDATE_FRAME (1, frame);
  684. return SCM_FRAME_ARGS (frame);
  685. }
  686. #undef FUNC_NAME
  687. SCM_DEFINE (scm_frame_previous, "frame-previous", 1, 0, 0,
  688. (SCM frame),
  689. "Return the previous frame of @var{frame}, or @code{#f} if\n"
  690. "@var{frame} is the first frame in its stack.")
  691. #define FUNC_NAME s_scm_frame_previous
  692. {
  693. unsigned long int n;
  694. SCM_VALIDATE_FRAME (1, frame);
  695. n = scm_to_ulong (SCM_CDR (frame)) + 1;
  696. if (n >= SCM_STACK_LENGTH (SCM_CAR (frame)))
  697. return SCM_BOOL_F;
  698. else
  699. return scm_cons (SCM_CAR (frame), scm_from_ulong (n));
  700. }
  701. #undef FUNC_NAME
  702. SCM_DEFINE (scm_frame_next, "frame-next", 1, 0, 0,
  703. (SCM frame),
  704. "Return the next frame of @var{frame}, or @code{#f} if\n"
  705. "@var{frame} is the last frame in its stack.")
  706. #define FUNC_NAME s_scm_frame_next
  707. {
  708. unsigned long int n;
  709. SCM_VALIDATE_FRAME (1, frame);
  710. n = scm_to_ulong (SCM_CDR (frame));
  711. if (n == 0)
  712. return SCM_BOOL_F;
  713. else
  714. return scm_cons (SCM_CAR (frame), scm_from_ulong (n - 1));
  715. }
  716. #undef FUNC_NAME
  717. SCM_DEFINE (scm_frame_real_p, "frame-real?", 1, 0, 0,
  718. (SCM frame),
  719. "Return @code{#t} if @var{frame} is a real frame.")
  720. #define FUNC_NAME s_scm_frame_real_p
  721. {
  722. SCM_VALIDATE_FRAME (1, frame);
  723. return scm_from_bool(SCM_FRAME_REAL_P (frame));
  724. }
  725. #undef FUNC_NAME
  726. SCM_DEFINE (scm_frame_procedure_p, "frame-procedure?", 1, 0, 0,
  727. (SCM frame),
  728. "Return @code{#t} if a procedure is associated with @var{frame}.")
  729. #define FUNC_NAME s_scm_frame_procedure_p
  730. {
  731. SCM_VALIDATE_FRAME (1, frame);
  732. return scm_from_bool(SCM_FRAME_PROC_P (frame));
  733. }
  734. #undef FUNC_NAME
  735. SCM_DEFINE (scm_frame_evaluating_args_p, "frame-evaluating-args?", 1, 0, 0,
  736. (SCM frame),
  737. "Return @code{#t} if @var{frame} contains evaluated arguments.")
  738. #define FUNC_NAME s_scm_frame_evaluating_args_p
  739. {
  740. SCM_VALIDATE_FRAME (1, frame);
  741. return scm_from_bool(SCM_FRAME_EVAL_ARGS_P (frame));
  742. }
  743. #undef FUNC_NAME
  744. SCM_DEFINE (scm_frame_overflow_p, "frame-overflow?", 1, 0, 0,
  745. (SCM frame),
  746. "Return @code{#t} if @var{frame} is an overflow frame.")
  747. #define FUNC_NAME s_scm_frame_overflow_p
  748. {
  749. SCM_VALIDATE_FRAME (1, frame);
  750. return scm_from_bool(SCM_FRAME_OVERFLOW_P (frame));
  751. }
  752. #undef FUNC_NAME
  753. void
  754. scm_init_stacks ()
  755. {
  756. scm_stack_type =
  757. scm_permanent_object
  758. (scm_make_vtable (scm_from_locale_string (SCM_STACK_LAYOUT),
  759. SCM_UNDEFINED));
  760. scm_set_struct_vtable_name_x (scm_stack_type,
  761. scm_from_locale_symbol ("stack"));
  762. #include "libguile/stacks.x"
  763. }
  764. /*
  765. Local Variables:
  766. c-file-style: "gnu"
  767. End:
  768. */