stacks.c 22 KB

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