api-debug.texi 53 KB

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