api-debug.texi 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  1. @c -*-texinfo-*-
  2. @c This is part of the GNU Guile Reference Manual.
  3. @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2007, 2010
  4. @c Free Software Foundation, Inc.
  5. @c See the file guile.texi for copying conditions.
  6. @node Debugging
  7. @section Debugging Infrastructure
  8. @cindex Debugging
  9. In order to understand Guile's debugging facilities, you first need to
  10. understand a little about how Guile represent the Scheme control stack.
  11. With that in place we explain the low level trap calls that the virtual
  12. machine can be configured to make, and the trap and breakpoint
  13. infrastructure that builds on top of those calls.
  14. @menu
  15. * Evaluation Model:: Evaluation and the Scheme stack.
  16. * Source Properties:: From expressions to source locations.
  17. * Programmatic Error Handling:: Debugging when an error occurs.
  18. * Traps:: Breakpoints, tracepoints, oh my!
  19. @end menu
  20. @node Evaluation Model
  21. @subsection Evaluation and the Scheme Stack
  22. The idea of the Scheme stack is central to a lot of debugging. The
  23. Scheme stack is a reified representation of the pending function returns
  24. in an expression's continuation. As Guile implements function calls
  25. using a stack, this reification takes the form of a number of nested
  26. stack frames, each of which corresponds to the application of a
  27. procedure to a set of arguments.
  28. A Scheme stack always exists implicitly, and can be summoned into
  29. concrete existence as a first-class Scheme value by the
  30. @code{make-stack} call, so that an introspective Scheme program -- such
  31. as a debugger -- can present it in some way and allow the user to query
  32. its details. The first thing to understand, therefore, is how Guile's
  33. function call convention creates the stack.
  34. Broadly speaking, Guile represents all control flow on a stack. Calling
  35. a function involves pushing an empty frame on the stack, then evaluating
  36. the procedure and its arguments, then fixing up the new frame so that it
  37. points to the old one. Frames on the stack are thus linked together. A
  38. tail call is the same, except it reuses the existing frame instead of
  39. pushing on a new one.
  40. In this way, the only frames that are on the stack are ``active''
  41. frames, frames which need to do some work before the computation is
  42. complete. On the other hand, a function that has tail-called another
  43. function will not be on the stack, as it has no work left to do.
  44. Therefore, when an error occurs in a running program, or the program
  45. hits a breakpoint, or in fact at any point that the programmer chooses,
  46. its state at that point can be represented by a @dfn{stack} of all the
  47. procedure applications that are logically in progress at that time, each
  48. of which is known as a @dfn{frame}. The programmer can learn more about
  49. the program's state at that point by inspecting the stack and its
  50. frames.
  51. @menu
  52. * Stack Capture:: Reifying a continuation.
  53. * Stacks:: Accessors for the stack data type.
  54. * Frames:: Likewise, accessors for stack frames.
  55. @end menu
  56. @node Stack Capture
  57. @subsubsection Stack Capture
  58. A Scheme program can use the @code{make-stack} primitive anywhere in its
  59. code, with first arg @code{#t}, to construct a Scheme value that
  60. describes the Scheme stack at that point.
  61. @lisp
  62. (make-stack #t)
  63. @result{}
  64. #<stack 25205a0>
  65. @end lisp
  66. Use @code{start-stack} to limit the stack extent captured by future
  67. @code{make-stack} calls.
  68. @deffn {Scheme Procedure} make-stack obj . args
  69. @deffnx {C Function} scm_make_stack (obj, args)
  70. Create a new stack. If @var{obj} is @code{#t}, the current
  71. evaluation stack is used for creating the stack frames,
  72. otherwise the frames are taken from @var{obj} (which must be
  73. a continuation or a frame object).
  74. @var{args} should be a list containing any combination of
  75. integer, procedure, prompt tag and @code{#t} values.
  76. These values specify various ways of cutting away uninteresting
  77. stack frames from the top and bottom of the stack that
  78. @code{make-stack} returns. They come in pairs like this:
  79. @code{(@var{inner_cut_1} @var{outer_cut_1} @var{inner_cut_2}
  80. @var{outer_cut_2} @dots{})}.
  81. Each @var{inner_cut_N} can be @code{#t}, an integer, a prompt
  82. tag, or a procedure. @code{#t} means to cut away all frames up
  83. to but excluding the first user module frame. An integer means
  84. to cut away exactly that number of frames. A prompt tag means
  85. to cut away all frames that are inside a prompt with the given
  86. tag. A procedure means to cut away all frames up to but
  87. excluding the application frame whose procedure matches the
  88. specified one.
  89. Each @var{outer_cut_N} can be an integer, a prompt tag, or a
  90. procedure. An integer means to cut away that number of frames.
  91. A prompt tag means to cut away all frames that are outside a
  92. prompt with the given tag. A procedure means to cut away
  93. frames down to but excluding the application frame whose
  94. procedure matches the specified one.
  95. If the @var{outer_cut_N} of the last pair is missing, it is
  96. taken as 0.
  97. @end deffn
  98. @deffn {Scheme Syntax} start-stack id exp
  99. Evaluate @var{exp} on a new calling stack with identity @var{id}. If
  100. @var{exp} is interrupted during evaluation, backtraces will not display
  101. frames farther back than @var{exp}'s top-level form. This macro is a
  102. way of artificially limiting backtraces and stack procedures, largely as
  103. a convenience to the user.
  104. @end deffn
  105. @node Stacks
  106. @subsubsection Stacks
  107. @deffn {Scheme Procedure} stack? obj
  108. @deffnx {C Function} scm_stack_p (obj)
  109. Return @code{#t} if @var{obj} is a calling stack.
  110. @end deffn
  111. @deffn {Scheme Procedure} stack-id stack
  112. @deffnx {C Function} scm_stack_id (stack)
  113. Return the identifier given to @var{stack} by @code{start-stack}.
  114. @end deffn
  115. @deffn {Scheme Procedure} stack-length stack
  116. @deffnx {C Function} scm_stack_length (stack)
  117. Return the length of @var{stack}.
  118. @end deffn
  119. @deffn {Scheme Procedure} stack-ref stack index
  120. @deffnx {C Function} scm_stack_ref (stack, index)
  121. Return the @var{index}'th frame from @var{stack}.
  122. @end deffn
  123. @deffn {Scheme Procedure} display-backtrace stack port [first [depth [highlights]]]
  124. @deffnx {C Function} scm_display_backtrace_with_highlights (stack, port, first, depth, highlights)
  125. @deffnx {C Function} scm_display_backtrace (stack, port, first, depth)
  126. Display a backtrace to the output port @var{port}. @var{stack}
  127. is the stack to take the backtrace from, @var{first} specifies
  128. where in the stack to start and @var{depth} how many frames
  129. to display. @var{first} and @var{depth} can be @code{#f},
  130. which means that default values will be used.
  131. If @var{highlights} is given it should be a list; the elements
  132. of this list will be highlighted wherever they appear in the
  133. backtrace.
  134. @end deffn
  135. @node Frames
  136. @subsubsection Frames
  137. @deffn {Scheme Procedure} frame? obj
  138. @deffnx {C Function} scm_frame_p (obj)
  139. Return @code{#t} if @var{obj} is a stack frame.
  140. @end deffn
  141. @deffn {Scheme Procedure} frame-previous frame
  142. @deffnx {C Function} scm_frame_previous (frame)
  143. Return the previous frame of @var{frame}, or @code{#f} if
  144. @var{frame} is the first frame in its stack.
  145. @end deffn
  146. @deffn {Scheme Procedure} frame-procedure frame
  147. @deffnx {C Function} scm_frame_procedure (frame)
  148. Return the procedure for @var{frame}, or @code{#f} if no
  149. procedure is associated with @var{frame}.
  150. @end deffn
  151. @deffn {Scheme Procedure} frame-arguments frame
  152. @deffnx {C Function} scm_frame_arguments (frame)
  153. Return the arguments of @var{frame}.
  154. @end deffn
  155. @deffn {Scheme Procedure} frame-address frame
  156. @deffnx {Scheme Procedure} frame-instruction-pointer frame
  157. @deffnx {Scheme Procedure} frame-stack-pointer frame
  158. Accessors for the three VM registers associated with this frame: the
  159. frame pointer (fp), instruction pointer (ip), and stack pointer (sp),
  160. respectively. @xref{VM Concepts}, for more information.
  161. @end deffn
  162. @deffn {Scheme Procedure} frame-dynamic-link frame
  163. @deffnx {Scheme Procedure} frame-return-address frame
  164. @deffnx {Scheme Procedure} frame-mv-return-address frame
  165. Accessors for the three saved VM registers in a frame: the previous
  166. frame pointer, the single-value return address, and the multiple-value
  167. return address. @xref{Stack Layout}, for more information.
  168. @end deffn
  169. @deffn {Scheme Procedure} frame-num-locals frame
  170. @deffnx {Scheme Procedure} frame-local-ref frame i
  171. @deffnx {Scheme Procedure} frame-local-set! frame i val
  172. Accessors for the temporary values corresponding to @var{frame}'s
  173. procedure application. The first local is the first argument given to
  174. the procedure. After the arguments, there are the local variables, and
  175. after that temporary values. @xref{Stack Layout}, for more information.
  176. @end deffn
  177. @deffn {Scheme Procedure} display-application frame [port [indent]]
  178. @deffnx {C Function} scm_display_application (frame, port, indent)
  179. Display a procedure application @var{frame} to the output port
  180. @var{port}. @var{indent} specifies the indentation of the
  181. output.
  182. @end deffn
  183. Additionally, the @code{(system vm frame)} module defines a number of
  184. higher-level introspective procedures, for example to retrieve the names
  185. of local variables, and the source location to correspond to a
  186. frame. See its source code for more details.
  187. @node Source Properties
  188. @subsection Source Properties
  189. @cindex source properties
  190. As Guile reads in Scheme code from file or from standard input, it
  191. remembers the file name, line number and column number where each
  192. expression begins. These pieces of information are known as the
  193. @dfn{source properties} of the expression. Syntax expanders and the
  194. compiler propagate these source properties to compiled procedures, so
  195. that, if an error occurs when evaluating the transformed expression,
  196. Guile's debugger can point back to the file and location where the
  197. expression originated.
  198. The way that source properties are stored means that Guile can only
  199. associate source properties with parenthesized expressions, and not, for
  200. example, with individual symbols, numbers or strings. The difference
  201. can be seen by typing @code{(xxx)} and @code{xxx} at the Guile prompt
  202. (where the variable @code{xxx} has not been defined):
  203. @example
  204. scheme@@(guile-user)> (xxx)
  205. <unnamed port>:4:1: In procedure module-lookup:
  206. <unnamed port>:4:1: Unbound variable: xxx
  207. scheme@@(guile-user)> xxx
  208. ERROR: In procedure module-lookup:
  209. ERROR: Unbound variable: xxx
  210. @end example
  211. @noindent
  212. In the latter case, no source properties were stored, so the error
  213. doesn't have any source information.
  214. The recording of source properties is controlled by the read option
  215. named ``positions'' (@pxref{Scheme Read}). This option is switched
  216. @emph{on} by default.
  217. The following procedures can be used to access and set the source
  218. properties of read expressions.
  219. @deffn {Scheme Procedure} set-source-properties! obj alist
  220. @deffnx {C Function} scm_set_source_properties_x (obj, alist)
  221. Install the association list @var{alist} as the source property
  222. list for @var{obj}.
  223. @end deffn
  224. @deffn {Scheme Procedure} set-source-property! obj key datum
  225. @deffnx {C Function} scm_set_source_property_x (obj, key, datum)
  226. Set the source property of object @var{obj}, which is specified by
  227. @var{key} to @var{datum}. Normally, the key will be a symbol.
  228. @end deffn
  229. @deffn {Scheme Procedure} source-properties obj
  230. @deffnx {C Function} scm_source_properties (obj)
  231. Return the source property association list of @var{obj}.
  232. @end deffn
  233. @deffn {Scheme Procedure} source-property obj key
  234. @deffnx {C Function} scm_source_property (obj, key)
  235. Return the property specified by @var{key} from @var{obj}'s source
  236. properties.
  237. @end deffn
  238. If the @code{positions} reader option is enabled, each parenthesized
  239. expression will have values set for the @code{filename}, @code{line} and
  240. @code{column} properties.
  241. If you're stuck with defmacros (@pxref{Defmacros}), and want to preserve
  242. source information, the following helper function might be useful to
  243. you:
  244. @deffn {Scheme Procedure} cons-source xorig x y
  245. @deffnx {C Function} scm_cons_source (xorig, x, y)
  246. Create and return a new pair whose car and cdr are @var{x} and @var{y}.
  247. Any source properties associated with @var{xorig} are also associated
  248. with the new pair.
  249. @end deffn
  250. @node Programmatic Error Handling
  251. @subsection Programmatic Error Handling
  252. For better or for worse, all programs have bugs, and dealing with bugs
  253. is part of programming. This section deals with that class of bugs that
  254. causes an exception to be raised -- from your own code, from within a
  255. library, or from Guile itself.
  256. @menu
  257. * Catching Exceptions:: Handling errors after the stack is unwound.
  258. * Capturing Stacks:: Capturing the stack at the time of error.
  259. * Pre-Unwind Debugging:: Debugging before the exception is thrown.
  260. * Debug Options:: A historical interface to debugging.
  261. @end menu
  262. @node Catching Exceptions
  263. @subsubsection Catching Exceptions
  264. A common requirement is to be able to show as much useful context as
  265. possible when a Scheme program hits an error. The most immediate
  266. information about an error is the kind of error that it is -- such as
  267. ``division by zero'' -- and any parameters that the code which signalled
  268. the error chose explicitly to provide. This information originates with
  269. the @code{error} or @code{throw} call (or their C code equivalents, if
  270. the error is detected by C code) that signals the error, and is passed
  271. automatically to the handler procedure of the innermost applicable
  272. @code{catch} or @code{with-throw-handler} expression.
  273. Therefore, to catch errors that occur within a chunk of Scheme code, and
  274. to intercept basic information about those errors, you need to execute
  275. that code inside the dynamic context of a @code{catch} or
  276. @code{with-throw-handler} expression, or the equivalent in C. In Scheme,
  277. this means you need something like this:
  278. @lisp
  279. (catch #t
  280. (lambda ()
  281. ;; Execute the code in which
  282. ;; you want to catch errors here.
  283. ...)
  284. (lambda (key . parameters)
  285. ;; Put the code which you want
  286. ;; to handle an error here.
  287. ...))
  288. @end lisp
  289. @noindent
  290. The @code{catch} here can also be @code{with-throw-handler}; see
  291. @ref{Throw Handlers} for information on the when you might want to use
  292. @code{with-throw-handler} instead of @code{catch}.
  293. For example, to print out a message and return #f when an error occurs,
  294. you might use:
  295. @smalllisp
  296. (define (catch-all thunk)
  297. (catch #t
  298. thunk
  299. (lambda (key . parameters)
  300. (format (current-error-port)
  301. "Uncaught throw to '~a: ~a\n" key parameters)
  302. #f)))
  303. (catch-all
  304. (lambda () (error "Not a vegetable: tomato")))
  305. @print{} Uncaught throw to 'misc-error: (#f ~A (Not a vegetable: tomato) #f)
  306. @result{} #f
  307. @end smalllisp
  308. The @code{#t} means that the catch is applicable to all kinds of error.
  309. If you want to restrict your catch to just one kind of error, you can
  310. put the symbol for that kind of error instead of @code{#t}. The
  311. equivalent to this in C would be something like this:
  312. @lisp
  313. SCM my_body_proc (void *body_data)
  314. @{
  315. /* Execute the code in which
  316. you want to catch errors here. */
  317. ...
  318. @}
  319. SCM my_handler_proc (void *handler_data,
  320. SCM key,
  321. SCM parameters)
  322. @{
  323. /* Put the code which you want
  324. to handle an error here. */
  325. ...
  326. @}
  327. @{
  328. ...
  329. scm_c_catch (SCM_BOOL_T,
  330. my_body_proc, body_data,
  331. my_handler_proc, handler_data,
  332. NULL, NULL);
  333. ...
  334. @}
  335. @end lisp
  336. @noindent
  337. Again, as with the Scheme version, @code{scm_c_catch} could be replaced
  338. by @code{scm_c_with_throw_handler}, and @code{SCM_BOOL_T} could instead
  339. be the symbol for a particular kind of error.
  340. @node Capturing Stacks
  341. @subsubsection Capturing the full error stack
  342. The other interesting information about an error is the full Scheme
  343. stack at the point where the error occurred; in other words what
  344. innermost expression was being evaluated, what was the expression that
  345. called that one, and so on. If you want to write your code so that it
  346. captures and can display this information as well, there are a couple
  347. important things to understand.
  348. Firstly, the stack at the point of the error needs to be explicitly
  349. captured by a @code{make-stack} call (or the C equivalent
  350. @code{scm_make_stack}). The Guile library does not do this
  351. ``automatically'' for you, so you will need to write code with a
  352. @code{make-stack} or @code{scm_make_stack} call yourself. (We emphasise
  353. this point because some people are misled by the fact that the Guile
  354. interactive REPL code @emph{does} capture and display the stack
  355. automatically. But the Guile interactive REPL is itself a Scheme
  356. program@footnote{In effect, it is the default program which is run when
  357. no commands or script file are specified on the Guile command line.}
  358. running on top of the Guile library, and which uses @code{catch} and
  359. @code{make-stack} in the way we are about to describe to capture the
  360. stack when an error occurs.)
  361. And secondly, in order to capture the stack effectively at the point
  362. where the error occurred, the @code{make-stack} call must be made before
  363. Guile unwinds the stack back to the location of the prevailing catch
  364. expression. This means that the @code{make-stack} call must be made
  365. within the handler of a @code{with-throw-handler} expression, or the
  366. optional "pre-unwind" handler of a @code{catch}. (For the full story of
  367. how these alternatives differ from each other, see @ref{Exceptions}. The
  368. main difference is that @code{catch} terminates the error, whereas
  369. @code{with-throw-handler} only intercepts it temporarily and then allow
  370. it to continue propagating up to the next innermost handler.)
  371. So, here are some examples of how to do all this in Scheme and in C.
  372. For the purpose of these examples we assume that the captured stack
  373. should be stored in a variable, so that it can be displayed or
  374. arbitrarily processed later on. In Scheme:
  375. @lisp
  376. (let ((captured-stack #f))
  377. (catch #t
  378. (lambda ()
  379. ;; Execute the code in which
  380. ;; you want to catch errors here.
  381. ...)
  382. (lambda (key . parameters)
  383. ;; Put the code which you want
  384. ;; to handle an error after the
  385. ;; stack has been unwound here.
  386. ...)
  387. (lambda (key . parameters)
  388. ;; Capture the stack here:
  389. (set! captured-stack (make-stack #t))))
  390. ...
  391. (if captured-stack
  392. (begin
  393. ;; Display or process the captured stack.
  394. ...))
  395. ...)
  396. @end lisp
  397. @noindent
  398. And in C:
  399. @lisp
  400. SCM my_body_proc (void *body_data)
  401. @{
  402. /* Execute the code in which
  403. you want to catch errors here. */
  404. ...
  405. @}
  406. SCM my_handler_proc (void *handler_data,
  407. SCM key,
  408. SCM parameters)
  409. @{
  410. /* Put the code which you want
  411. to handle an error after the
  412. stack has been unwound here. */
  413. ...
  414. @}
  415. SCM my_preunwind_proc (void *handler_data,
  416. SCM key,
  417. SCM parameters)
  418. @{
  419. /* Capture the stack here: */
  420. *(SCM *)handler_data = scm_make_stack (SCM_BOOL_T, SCM_EOL);
  421. @}
  422. @{
  423. SCM captured_stack = SCM_BOOL_F;
  424. ...
  425. scm_c_catch (SCM_BOOL_T,
  426. my_body_proc, body_data,
  427. my_handler_proc, handler_data,
  428. my_preunwind_proc, &captured_stack);
  429. ...
  430. if (captured_stack != SCM_BOOL_F)
  431. @{
  432. /* Display or process the captured stack. */
  433. ...
  434. @}
  435. ...
  436. @}
  437. @end lisp
  438. Once you have a captured stack, you can interrogate and display its
  439. details in any way that you want, using the @code{stack-@dots{}} and
  440. @code{frame-@dots{}} API described in @ref{Stacks} and
  441. @ref{Frames}.
  442. If you want to print out a backtrace in the same format that the Guile
  443. REPL does, you can use the @code{display-backtrace} procedure to do so.
  444. You can also use @code{display-application} to display an individual
  445. frame in the Guile REPL format.
  446. @node Pre-Unwind Debugging
  447. @subsubsection Pre-Unwind Debugging
  448. Instead of saving a stack away and waiting for the @code{catch} to
  449. return, you can handle errors directly, from within the pre-unwind
  450. handler.
  451. For example, to show a backtrace when an error is thrown, you might want
  452. to use a procedure like this:
  453. @lisp
  454. (define (with-backtrace thunk)
  455. (with-throw-handler #t
  456. thunk
  457. (lambda args (backtrace))))
  458. (with-backtrace (lambda () (error "Not a vegetable: tomato")))
  459. @end lisp
  460. Since we used @code{with-throw-handler} here, we didn't actually catch
  461. the error. @xref{Throw Handlers}, for more information. However, we did
  462. print out a context at the time of the error, using the built-in
  463. procedure, @code{backtrace}.
  464. @deffn {Scheme Procedure} backtrace [highlights]
  465. @deffnx {C Function} scm_backtrace_with_highlights (highlights)
  466. @deffnx {C Function} scm_backtrace ()
  467. Display a backtrace of the current stack to the current output port. If
  468. @var{highlights} is given it should be a list; the elements of this list
  469. will be highlighted wherever they appear in the backtrace.
  470. @end deffn
  471. The Guile REPL code (in @file{system/repl/repl.scm} and related files)
  472. uses a @code{catch} with a pre-unwind handler to capture the stack when
  473. an error occurs in an expression that was typed into the REPL, and debug
  474. that stack interactively in the context of the error.
  475. These procedures are available for use by user programs, in the
  476. @code{(system repl error-handling)} module.
  477. @lisp
  478. (use-modules (system repl error-handling))
  479. @end lisp
  480. @deffn {Scheme Procedure} call-with-error-handling thunk @
  481. [#:on-error on-error='debug] [#:post-error post-error='catch] @
  482. [#:pass-keys pass-keys='(quit)] [#:trap-handler trap-handler='debug]
  483. Call a thunk in a context in which errors are handled.
  484. There are four keyword arguments:
  485. @table @var
  486. @item on-error
  487. Specifies what to do before the stack is unwound.
  488. Valid options are @code{debug} (the default), which will enter a
  489. debugger; @code{pass}, in which case nothing is done, and the exception
  490. is rethrown; or a procedure, which will be the pre-unwind handler.
  491. @item post-error
  492. Specifies what to do after the stack is unwound.
  493. Valid options are @code{catch} (the default), which will silently catch
  494. errors, returning the unspecified value; @code{report}, which prints out
  495. a description of the error (via @code{display-error}), and then returns
  496. the unspecified value; or a procedure, which will be the catch handler.
  497. @item trap-handler
  498. Specifies a trap handler: what to do when a breakpoint is hit.
  499. Valid options are @code{debug}, which will enter the debugger;
  500. @code{pass}, which does nothing; or @code{disabled}, which disables
  501. traps entirely. @xref{Traps}, for more information.
  502. @item pass-keys
  503. A set of keys to ignore, as a list.
  504. @end table
  505. @end deffn
  506. @node Debug Options
  507. @subsubsection Debug options
  508. The behavior of the @code{backtrace} procedure and of the default error
  509. handler can be parameterized via the debug options.
  510. @cindex options - debug
  511. @cindex debug options
  512. @deffn {Scheme Procedure} debug-options [setting]
  513. Display the current settings of the debug options. If @var{setting} is
  514. omitted, only a short form of the current read options is printed.
  515. Otherwise if @var{setting} is the symbol @code{help}, a complete options
  516. description is displayed.
  517. @end deffn
  518. The set of available options, and their default values, may be had by
  519. invoking @code{debug-options} at the prompt.
  520. @smallexample
  521. scheme@@(guile-user)>
  522. backwards no Display backtrace in anti-chronological order.
  523. width 79 Maximal width of backtrace.
  524. depth 20 Maximal length of printed backtrace.
  525. backtrace yes Show backtrace on error.
  526. stack 1048576 Stack size limit (measured in words;
  527. 0 = no check).
  528. show-file-name #t Show file names and line numbers in backtraces
  529. when not `#f'. A value of `base' displays only
  530. base names, while `#t' displays full names.
  531. warn-deprecated no Warn when deprecated features are used.
  532. @end smallexample
  533. The boolean options may be toggled with @code{debug-enable} and
  534. @code{debug-disable}. The non-boolean @code{keywords} option must be set
  535. using @code{debug-set!}.
  536. @deffn {Scheme Procedure} debug-enable option-name
  537. @deffnx {Scheme Procedure} debug-disable option-name
  538. @deffnx {Scheme Procedure} debug-set! option-name value
  539. Modify the debug options. @code{debug-enable} should be used with boolean
  540. options and switches them on, @code{debug-disable} switches them off.
  541. @code{debug-set!} can be used to set an option to a specific value.
  542. @end deffn
  543. @subsubheading Stack overflow
  544. @cindex overflow, stack
  545. @cindex stack overflow
  546. Stack overflow errors are caused by a computation trying to use more
  547. stack space than has been enabled by the @code{stack} option. There are
  548. actually two kinds of stack that can overflow, the C stack and the
  549. Scheme stack.
  550. Scheme stack overflows can occur if Scheme procedures recurse too far
  551. deeply. An example would be the following recursive loop:
  552. @lisp
  553. scheme@@(guile-user)> (let lp () (+ 1 (lp)))
  554. <unnamed port>:8:17: In procedure vm-run:
  555. <unnamed port>:8:17: VM: Stack overflow
  556. @end lisp
  557. The default stack size should allow for about 10000 frames or so, so one
  558. usually doesn't hit this level of recursion. Unfortunately there is no
  559. way currently to make a VM with a bigger stack. If you are in this
  560. unfortunate situation, please file a bug, and in the meantime, rewrite
  561. your code to be tail-recursive (@pxref{Tail Calls}).
  562. The other limit you might hit would be C stack overflows. If you call a
  563. primitive procedure which then calls a Scheme procedure in a loop, you
  564. will consume C stack space. Guile tries to detect excessive consumption
  565. of C stack space, throwing an error when you have hit 80% of the
  566. process' available stack (as allocated by the operating system), or 160
  567. kilowords in the absence of a strict limit.
  568. For example, looping through @code{call-with-vm}, a primitive that calls
  569. a thunk, gives us the following:
  570. @lisp
  571. scheme@@(guile-user)> (use-modules (system vm vm))
  572. scheme@@(guile-user)> (debug-set! stack 10000)
  573. scheme@@(guile-user)> (let lp () (call-with-vm (the-vm) lp))
  574. ERROR: In procedure call-with-vm:
  575. ERROR: Stack overflow
  576. @end lisp
  577. If you get an error like this, you can either try rewriting your code to
  578. use less stack space, or increase the maximum stack size. To increase
  579. the maximum stack size, use @code{debug-set!}, for example:
  580. @lisp
  581. (debug-set! stack 200000)
  582. @end lisp
  583. But of course it's better to have your code operate without so much
  584. resource consumption, avoiding loops through C trampolines.
  585. @node Traps
  586. @subsection Traps
  587. @cindex Traps
  588. @cindex VM hooks
  589. @cindex Breakpoints
  590. @cindex Trace
  591. @cindex Tracing
  592. @cindex Code coverage
  593. @cindex Profiling
  594. Guile's virtual machine can be configured to call out at key points to
  595. arbitrary user-specified procedures.
  596. In principle, these @dfn{hooks} allow Scheme code to implement any model
  597. it chooses for examining the evaluation stack as program execution
  598. proceeds, and for suspending execution to be resumed later.
  599. VM hooks are very low-level, though, and so Guile also has a library of
  600. higher-level @dfn{traps} on top of the VM hooks. A trap is an execution
  601. condition that, when fulfilled, will fire a handler. For example, Guile
  602. defines a trap that fires when control reaches a certain source
  603. location.
  604. Finally, Guile also defines a third level of abstractions: per-thread
  605. @dfn{trap states}. A trap state exists to give names to traps, and to
  606. hold on to the set of traps so that they can be enabled, disabled, or
  607. removed. The trap state infrastructure defines the most useful
  608. abstractions for most cases. For example, Guile's REPL uses trap state
  609. functions to set breakpoints and tracepoints.
  610. The following subsections describe all this in detail, for both the
  611. user wanting to use traps, and the developer interested in
  612. understanding how the interface hangs together.
  613. @menu
  614. * VM Hooks:: Modifying Guile's virtual machine.
  615. * Trap Interface:: Traps are on or off.
  616. * Low-Level Traps:: The various kinds of low-level traps.
  617. * Tracing Traps:: Traps to trace procedure calls and returns.
  618. * Trap States:: One state (per thread) to bind them.
  619. * High-Level Traps:: The highest-level trap interface. Use this.
  620. @end menu
  621. @node VM Hooks
  622. @subsubsection VM Hooks
  623. Everything that runs in Guile runs on its virtual machine, a C program
  624. that defines a number of operations that Scheme programs can
  625. perform.
  626. Note that there are multiple VM ``engines'' for Guile. Only some of them
  627. have support for hooks compiled in. Normally the deal is that you get
  628. hooks if you are running interactively, and otherwise they are disabled,
  629. as they do have some overhead (about 10 or 20 percent).
  630. To ensure that you are running with hooks, pass @code{--debug} to Guile
  631. when running your program, or otherwise use the @code{call-with-vm} and
  632. @code{set-vm-engine!} procedures to ensure that you are running in a VM
  633. with the @code{debug} engine.
  634. To digress, Guile's VM has 6 different hooks (@pxref{Hooks}) that can be
  635. fired at different times, which may be accessed with the following
  636. procedures.
  637. All hooks are called with one argument, the frame in
  638. question. @xref{Frames}. Since these hooks may be fired very
  639. frequently, Guile does a terrible thing: it allocates the frames on the
  640. C stack instead of the garbage-collected heap.
  641. The upshot here is that the frames are only valid within the dynamic
  642. extent of the call to the hook. If a hook procedure keeps a reference to
  643. the frame outside the extent of the hook, bad things will happen.
  644. The interface to hooks is provided by the @code{(system vm vm)} module:
  645. @example
  646. (use-modules (system vm vm))
  647. @end example
  648. @noindent
  649. The result of calling @code{the-vm} is usually passed as the @var{vm}
  650. argument to all of these procedures.
  651. @deffn {Scheme Procedure} vm-next-hook vm
  652. The hook that will be fired before an instruction is retired (and
  653. executed).
  654. @end deffn
  655. @deffn {Scheme Procedure} vm-push-continuation-hook vm
  656. The hook that will be fired after preparing a new frame. Fires just
  657. before applying a procedure in a non-tail context, just before the
  658. corresponding apply-hook.
  659. @end deffn
  660. @deffn {Scheme Procedure} vm-pop-continuation-hook vm
  661. The hook that will be fired before returning from a frame.
  662. This hook is a bit trickier than the rest, in that there is a particular
  663. interpretation of the values on the stack. Specifically, the top value
  664. on the stack is the number of values being returned, and the next
  665. @var{n} values are the actual values being returned, with the last value
  666. highest on the stack.
  667. @end deffn
  668. @deffn {Scheme Procedure} vm-apply-hook vm
  669. The hook that will be fired before a procedure is applied. The frame's
  670. procedure will have already been set to the new procedure.
  671. Note that procedure application is somewhat orthogonal to continuation
  672. pushes and pops. A non-tail call to a procedure will result first in a
  673. firing of the push-continuation hook, then this application hook,
  674. whereas a tail call will run without having fired a push-continuation
  675. hook.
  676. @end deffn
  677. @deffn {Scheme Procedure} vm-abort-continuation-hook vm
  678. The hook that will be called after aborting to a
  679. prompt. @xref{Prompts}. The stack will be in the same state as for
  680. @code{vm-pop-continuation-hook}.
  681. @end deffn
  682. @deffn {Scheme Procedure} vm-restore-continuation-hook vm
  683. The hook that will be called after restoring an undelimited
  684. continuation. Unfortunately it's not currently possible to introspect on
  685. the values that were given to the continuation.
  686. @end deffn
  687. @cindex VM trace level
  688. These hooks do impose a performance penalty, if they are on. Obviously,
  689. the @code{vm-next-hook} has quite an impact, performance-wise. Therefore
  690. Guile exposes a single, heavy-handed knob to turn hooks on or off, the
  691. @dfn{VM trace level}. If the trace level is positive, hooks run;
  692. otherwise they don't.
  693. For convenience, when the VM fires a hook, it does so with the trap
  694. level temporarily set to 0. That way the hooks don't fire while you're
  695. handling a hook. The trace level is restored to whatever it was once the hook
  696. procedure finishes.
  697. @deffn {Scheme Procedure} vm-trace-level vm
  698. Retrieve the ``trace level'' of the VM. If positive, the trace hooks
  699. associated with @var{vm} will be run. The initial trace level is 0.
  700. @end deffn
  701. @deffn {Scheme Procedure} set-vm-trace-level! vm level
  702. Set the ``trace level'' of the VM.
  703. @end deffn
  704. @xref{A Virtual Machine for Guile}, for more information on Guile's
  705. virtual machine.
  706. @node Trap Interface
  707. @subsubsection Trap Interface
  708. The capabilities provided by hooks are great, but hooks alone rarely
  709. correspond to what users want to do.
  710. For example, if a user wants to break when and if control reaches a
  711. certain source location, how do you do it? If you install a ``next''
  712. hook, you get unacceptable overhead for the execution of the entire
  713. program. It would be possible to install an ``apply'' hook, then if the
  714. procedure encompasses those source locations, install a ``next'' hook,
  715. but already you're talking about one concept that might be implemented
  716. by a varying number of lower-level concepts.
  717. It's best to be clear about things and define one abstraction for all
  718. such conditions: the @dfn{trap}.
  719. Considering the myriad capabilities offered by the hooks though, there
  720. is only a minimum of functionality shared by all traps. Guile's current
  721. take is to reduce this to the absolute minimum, and have the only
  722. standard interface of a trap be ``turn yourself on'' or ``turn yourself
  723. off''.
  724. This interface sounds a bit strange, but it is useful to procedurally
  725. compose higher-level traps from lower-level building blocks. For
  726. example, Guile defines a trap that calls one handler when control enters
  727. a procedure, and another when control leaves the procedure. Given that
  728. trap, one can define a trap that adds to the next-hook only when within
  729. a given procedure. Building further, one can define a trap that fires
  730. when control reaches particular instructions within a procedure.
  731. Or of course you can stop at any of these intermediate levels. For
  732. example, one might only be interested in calls to a given procedure. But
  733. the point is that a simple enable/disable interface is all the
  734. commonality that exists between the various kinds of traps, and
  735. furthermore that such an interface serves to allow ``higher-level''
  736. traps to be composed from more primitive ones.
  737. Specifically, a trap, in Guile, is a procedure. When a trap is created,
  738. by convention the trap is enabled; therefore, the procedure that is the
  739. trap will, when called, disable the trap, and return a procedure that
  740. will enable the trap, and so on.
  741. Trap procedures take one optional argument: the current frame. (A trap
  742. may want to add to different sets of hooks depending on the frame that
  743. is current at enable-time.)
  744. If this all sounds very complicated, it's because it is. Some of it is
  745. essential, but probably most of it is not. The advantage of using this
  746. minimal interface is that composability is more lexically apparent than
  747. when, for example, using a stateful interface based on GOOPS. But
  748. perhaps this reflects the cognitive limitations of the programmer who
  749. made the current interface more than anything else.
  750. @node Low-Level Traps
  751. @subsubsection Low-Level Traps
  752. To summarize the last sections, traps are enabled or disabled, and when
  753. they are enabled, they add to various VM hooks.
  754. Note, however, that @emph{traps do not increase the VM trace level}. So
  755. if you create a trap, it will be enabled, but unless something else
  756. increases the VM's trace level (@pxref{VM Hooks}), the trap will not
  757. fire. It turns out that getting the VM trace level right is tricky
  758. without a global view of what traps are enabled. @xref{Trap States},
  759. for Guile's answer to this problem.
  760. Traps are created by calling procedures. Most of these procedures share
  761. a set of common keyword arguments, so rather than document them
  762. separately, we discuss them all together here:
  763. @table @code
  764. @item #:vm
  765. The VM to instrument. Defaults to the current thread's VM.
  766. @item #:closure?
  767. For traps that depend on the current frame's procedure, this argument
  768. specifies whether to trap on the only the specific procedure given, or
  769. on any closure that has the given procedure's code. Defaults to
  770. @code{#f}.
  771. @item #:current-frame
  772. For traps that enable more hooks depending on their dynamic context,
  773. this argument gives the current frame that the trap is running in.
  774. Defaults to @code{#f}.
  775. @end table
  776. To have access to these procedures, you'll need to have imported the
  777. @code{(system vm traps)} module:
  778. @lisp
  779. (use-modules (system vm traps))
  780. @end lisp
  781. @deffn {Scheme Procedure} trap-at-procedure-call proc handler @
  782. [#:vm] [#:closure?]
  783. A trap that calls @var{handler} when @var{proc} is applied.
  784. @end deffn
  785. @deffn {Scheme Procedure} trap-in-procedure proc @
  786. enter-handler exit-handler [#:current-frame] [#:vm] [#:closure?]
  787. A trap that calls @var{enter-handler} when control enters @var{proc},
  788. and @var{exit-handler} when control leaves @var{proc}.
  789. Control can enter a procedure via:
  790. @itemize
  791. @item
  792. A procedure call.
  793. @item
  794. A return to a procedure's frame on the stack.
  795. @item
  796. A continuation returning directly to an application of this procedure.
  797. @end itemize
  798. Control can leave a procedure via:
  799. @itemize
  800. @item
  801. A normal return from the procedure.
  802. @item
  803. An application of another procedure.
  804. @item
  805. An invocation of a continuation.
  806. @item
  807. An abort.
  808. @end itemize
  809. @end deffn
  810. @deffn {Scheme Procedure} trap-instructions-in-procedure proc @
  811. next-handler exit-handler [#:current-frame] [#:vm] [#:closure?]
  812. A trap that calls @var{next-handler} for every instruction executed in
  813. @var{proc}, and @var{exit-handler} when execution leaves @var{proc}.
  814. @end deffn
  815. @deffn {Scheme Procedure} trap-at-procedure-ip-in-range proc range @
  816. handler [#:current-frame] [#:vm] [#:closure?]
  817. A trap that calls @var{handler} when execution enters a range of
  818. instructions in @var{proc}. @var{range} is a simple of pairs,
  819. @code{((@var{start} . @var{end}) ...)}. The @var{start} addresses are
  820. inclusive, and @var{end} addresses are exclusive.
  821. @end deffn
  822. @deffn {Scheme Procedure} trap-at-source-location file user-line handler @
  823. [#:current-frame] [#:vm]
  824. A trap that fires when control reaches a given source location. The
  825. @var{user-line} parameter is one-indexed, as a user counts lines,
  826. instead of zero-indexed, as Guile counts lines.
  827. @end deffn
  828. @deffn {Scheme Procedure} trap-frame-finish frame @
  829. return-handler abort-handler [#:vm]
  830. A trap that fires when control leaves the given frame. @var{frame}
  831. should be a live frame in the current continuation. @var{return-handler}
  832. will be called on a normal return, and @var{abort-handler} on a nonlocal
  833. exit.
  834. @end deffn
  835. @deffn {Scheme Procedure} trap-in-dynamic-extent proc @
  836. enter-handler return-handler abort-handler [#:vm] [#:closure?]
  837. A more traditional dynamic-wind trap, which fires @var{enter-handler}
  838. when control enters @var{proc}, @var{return-handler} on a normal return,
  839. and @var{abort-handler} on a nonlocal exit.
  840. Note that rewinds are not handled, so there is no rewind handler.
  841. @end deffn
  842. @deffn {Scheme Procedure} trap-calls-in-dynamic-extent proc @
  843. apply-handler return-handler [#:current-frame] [#:vm] [#:closure?]
  844. A trap that calls @var{apply-handler} every time a procedure is applied,
  845. and @var{return-handler} for returns, but only during the dynamic extent
  846. of an application of @var{proc}.
  847. @end deffn
  848. @deffn {Scheme Procedure} trap-instructions-in-dynamic-extent proc @
  849. next-handler [#:current-frame] [#:vm] [#:closure?]
  850. A trap that calls @var{next-handler} for all retired instructions within
  851. the dynamic extent of a call to @var{proc}.
  852. @end deffn
  853. @deffn {Scheme Procedure} trap-calls-to-procedure proc @
  854. apply-handler return-handler [#:vm]
  855. A trap that calls @var{apply-handler} whenever @var{proc} is applied,
  856. and @var{return-handler} when it returns, but with an additional
  857. argument, the call depth.
  858. That is to say, the handlers will get two arguments: the frame in
  859. question, and the call depth (a non-negative integer).
  860. @end deffn
  861. @deffn {Scheme Procedure} trap-matching-instructions frame-pred handler [#:vm]
  862. A trap that calls @var{frame-pred} at every instruction, and if
  863. @var{frame-pred} returns a true value, calls @var{handler} on the
  864. frame.
  865. @end deffn
  866. @node Tracing Traps
  867. @subsubsection Tracing Traps
  868. The @code{(system vm trace)} module defines a number of traps for
  869. tracing of procedure applications. When a procedure is @dfn{traced}, it
  870. means that every call to that procedure is reported to the user during a
  871. program run. The idea is that you can mark a collection of procedures
  872. for tracing, and Guile will subsequently print out a line of the form
  873. @lisp
  874. | | (@var{procedure} @var{args} @dots{})
  875. @end lisp
  876. whenever a marked procedure is about to be applied to its arguments.
  877. This can help a programmer determine whether a function is being called
  878. at the wrong time or with the wrong set of arguments.
  879. In addition, the indentation of the output is useful for demonstrating
  880. how the traced applications are or are not tail recursive with respect
  881. to each other. Thus, a trace of a non-tail recursive factorial
  882. implementation looks like this:
  883. @lisp
  884. scheme@@(guile-user)> (define (fact1 n)
  885. (if (zero? n) 1
  886. (* n (fact1 (1- n)))))
  887. scheme@@(guile-user)> ,trace (fact1 4)
  888. trace: (fact1 4)
  889. trace: | (fact1 3)
  890. trace: | | (fact1 2)
  891. trace: | | | (fact1 1)
  892. trace: | | | | (fact1 0)
  893. trace: | | | | 1
  894. trace: | | | 1
  895. trace: | | 2
  896. trace: | 6
  897. trace: 24
  898. @end lisp
  899. While a typical tail recursive implementation would look more like this:
  900. @lisp
  901. scheme@@(guile-user)> (define (facti acc n)
  902. (if (zero? n) acc
  903. (facti (* n acc) (1- n))))
  904. scheme@@(guile-user)> (define (fact2 n) (facti 1 n))
  905. scheme@@(guile-user)> ,trace (fact2 4)
  906. trace: (fact2 4)
  907. trace: (facti 1 4)
  908. trace: (facti 4 3)
  909. trace: (facti 12 2)
  910. trace: (facti 24 1)
  911. trace: (facti 24 0)
  912. trace: 24
  913. @end lisp
  914. The low-level traps below (@pxref{Low-Level Traps}) share some common
  915. options:
  916. @table @code
  917. @item #:width
  918. The maximum width of trace output. Trace printouts will try not to
  919. exceed this column, but for highly nested procedure calls, it may be
  920. unavoidable. Defaults to 80.
  921. @item #:vm
  922. The VM on which to add the traps. Defaults to the current thread's VM.
  923. @item #:prefix
  924. A string to print out before each trace line. As seen above in the
  925. examples, defaults to @code{"trace: "}.
  926. @end table
  927. To have access to these procedures, you'll need to have imported the
  928. @code{(system vm trace)} module:
  929. @lisp
  930. (use-modules (system vm trace))
  931. @end lisp
  932. @deffn {Scheme Procedure} trace-calls-to-procedure proc @
  933. [#:width] [#:vm] [#:prefix]
  934. Print a trace at applications of and returns from @var{proc}.
  935. @end deffn
  936. @deffn {Scheme Procedure} trace-calls-in-procedure proc @
  937. [#:width] [#:vm] [#:prefix]
  938. Print a trace at all applications and returns within the dynamic extent
  939. of calls to @var{proc}.
  940. @end deffn
  941. @deffn {Scheme Procedure} trace-instructions-in-procedure proc [#:width] [#:vm]
  942. Print a trace at all instructions executed in the dynamic extent of
  943. calls to @var{proc}.
  944. @end deffn
  945. In addition, Guile defines a procedure to call a thunk, tracing all
  946. procedure calls and returns within the thunk.
  947. @deffn {Scheme Procedure} call-with-trace thunk #:key (calls? #t) (instructions? #f) (width 80) (vm (the-vm))
  948. Call @var{thunk}, tracing all execution within its dynamic extent.
  949. If @var{calls?} is true, Guile will print a brief report at each
  950. procedure call and return, as given above.
  951. If @var{instructions?} is true, Guile will also print a message each
  952. time an instruction is executed. This is a lot of output, but it is
  953. sometimes useful when doing low-level optimization.
  954. Note that because this procedure manipulates the VM trace level
  955. directly, it doesn't compose well with traps at the REPL.
  956. @end deffn
  957. @xref{Profile Commands}, for more information on tracing at the REPL.
  958. @node Trap States
  959. @subsubsection Trap States
  960. When multiple traps are present in a system, we begin to have a
  961. bookkeeping problem. How are they named? How does one disable, enable,
  962. or delete them?
  963. Guile's answer to this is to keep an implicit per-thread @dfn{trap
  964. state}. The trap state object is not exposed to the user; rather, API
  965. that works on trap states fetches the current trap state from the
  966. dynamic environment.
  967. Traps are identified by integers. A trap can be enabled, disabled, or
  968. removed, and can have an associated user-visible name.
  969. These procedures have their own module:
  970. @lisp
  971. (use-modules (system vm trap-state))
  972. @end lisp
  973. @deffn {Scheme Procedure} add-trap! trap name
  974. Add a trap to the current trap state, associating the given @var{name}
  975. with it. Returns a fresh trap identifier (an integer).
  976. Note that usually the more specific functions detailed in
  977. @ref{High-Level Traps} are used in preference to this one.
  978. @end deffn
  979. @deffn {Scheme Procedure} list-traps
  980. List the current set of traps, both enabled and disabled. Returns a list
  981. of integers.
  982. @end deffn
  983. @deffn {Scheme Procedure} trap-name idx
  984. Returns the name associated with trap @var{idx}, or @code{#f} if there
  985. is no such trap.
  986. @end deffn
  987. @deffn {Scheme Procedure} trap-enabled? idx
  988. Returns @code{#t} if trap @var{idx} is present and enabled, or @code{#f}
  989. otherwise.
  990. @end deffn
  991. @deffn {Scheme Procedure} enable-trap! idx
  992. Enables trap @var{idx}.
  993. @end deffn
  994. @deffn {Scheme Procedure} disable-trap! idx
  995. Disables trap @var{idx}.
  996. @end deffn
  997. @deffn {Scheme Procedure} delete-trap! idx
  998. Removes trap @var{idx}, disabling it first, if necessary.
  999. @end deffn
  1000. @node High-Level Traps
  1001. @subsubsection High-Level Traps
  1002. The low-level trap API allows one to make traps that call procedures,
  1003. and the trap state API allows one to keep track of what traps are
  1004. there. But neither of these APIs directly helps you when you want to
  1005. set a breakpoint, because it's unclear what to do when the trap fires.
  1006. Do you enter a debugger, or mail a summary of the situation to your
  1007. great-aunt, or what?
  1008. So for the common case in which you just want to install breakpoints,
  1009. and then have them all result in calls to one parameterizable procedure,
  1010. we have the high-level trap interface.
  1011. Perhaps we should have started this section with this interface, as it's
  1012. clearly the one most people should use. But as its capabilities and
  1013. limitations proceed from the lower layers, we felt that the
  1014. character-building exercise of building a mental model might be helpful.
  1015. These procedures share a module with trap states:
  1016. @lisp
  1017. (use-modules (system vm trap-state))
  1018. @end lisp
  1019. @deffn {Scheme Procedure} with-default-trap-handler handler thunk
  1020. Call @var{thunk} in a dynamic context in which @var{handler} is the
  1021. current trap handler.
  1022. Additionally, during the execution of @var{thunk}, the VM trace level
  1023. (@pxref{VM Hooks}) is set to the number of enabled traps. This ensures
  1024. that traps will in fact fire.
  1025. @var{handler} may be @code{#f}, in which case VM hooks are not enabled
  1026. as they otherwise would be, as there is nothing to handle the traps.
  1027. @end deffn
  1028. The trace-level-setting behavior of @code{with-default-trap-handler} is
  1029. one of its more useful aspects, but if you are willing to forgo that,
  1030. and just want to install a global trap handler, there's a function for
  1031. that too:
  1032. @deffn {Scheme Procedure} install-trap-handler! handler
  1033. Set the current thread's trap handler to @var{handler}.
  1034. @end deffn
  1035. Trap handlers are called when traps installed by procedures from this
  1036. module fire. The current ``consumer'' of this API is Guile's REPL, but
  1037. one might easily imagine other trap handlers being used to integrate
  1038. with other debugging tools.
  1039. @cindex Breakpoints
  1040. @cindex Setting breakpoints
  1041. @deffn {Scheme Procedure} add-trap-at-procedure-call! proc
  1042. Install a trap that will fire when @var{proc} is called.
  1043. This is a breakpoint.
  1044. @end deffn
  1045. @cindex Tracepoints
  1046. @cindex Setting tracepoints
  1047. @deffn {Scheme Procedure} add-trace-at-procedure-call! proc
  1048. Install a trap that will print a tracing message when @var{proc} is
  1049. called. @xref{Tracing Traps}, for more information.
  1050. This is a tracepoint.
  1051. @end deffn
  1052. @deffn {Scheme Procedure} add-trap-at-source-location! file user-line
  1053. Install a trap that will fire when control reaches the given source
  1054. location. @var{user-line} is one-indexed, as users count lines, instead
  1055. of zero-indexed, as Guile counts lines.
  1056. This is a source breakpoint.
  1057. @end deffn
  1058. @deffn {Scheme Procedure} add-ephemeral-trap-at-frame-finish! frame handler
  1059. Install a trap that will call @var{handler} when @var{frame} finishes
  1060. executing. The trap will be removed from the trap state after firing, or
  1061. on nonlocal exit.
  1062. This is a finish trap, used to implement the ``finish'' REPL command.
  1063. @end deffn
  1064. @deffn {Scheme Procedure} add-ephemeral-stepping-trap! frame handler [#:into?] [#:instruction?]
  1065. Install a trap that will call @var{handler} after stepping to a
  1066. different source line or instruction. The trap will be removed from the
  1067. trap state after firing, or on nonlocal exit.
  1068. If @var{instruction?} is false (the default), the trap will fire when
  1069. control reaches a new source line. Otherwise it will fire when control
  1070. reaches a new instruction.
  1071. Additionally, if @var{into?} is false (not the default), the trap will
  1072. only fire for frames at or prior to the given frame. If @var{into?} is
  1073. true (the default), the trap may step into nested procedure
  1074. invocations.
  1075. This is a stepping trap, used to implement the ``step'', ``next'',
  1076. ``step-instruction'', and ``next-instruction'' REPL commands.
  1077. @end deffn
  1078. @c Local Variables:
  1079. @c TeX-master: "guile.texi"
  1080. @c End: