#### 8.2.2 Hook functions ¶

Hooks are functions evaluated before or after executing a `mixvm` command (or its corresponding Scheme function wrapper), or after an explicit or conditional breakpoint is found during the execution of a MIX program. The following functions let you install hooks:

Function: mix-add-pre-hook command hook ¶

  Adds a function to the list of pre-hooks associated with the given command. command is a string naming the corresponding `mixvm` command, and hook is a function which takes a single argument: a string list of the commands arguments. The following scheme code defines a simple hook and associates it with the `run` command:

      (define run-hook
        (lambda (args)
          (display "argument list: ")
          (display args)
          (newline)))
      (mix-add-pre-hook "run" run-hook)

  Pre-hooks are executed, in the order they are added, before invoking the corresponding command (or its associated Scheme wrapper function).

Function: mix-add-post-hook command hook ¶

  Adds a function to the list of pre-hooks associated with the given command. The arguments have the same meaning as in `mix-add-pre-hook`.

Function: mix-add-global-pre-hook hook ¶
Function: mix-add-global-post-hook hook ¶

  Global pre/post hooks are executed before/after any `mixvm` command or function wrapper invocation. In this case, hook takes two arguments: a string with the name of the command being invoked, and a string list with its arguments.

Function: mix-add-break-hook hook ¶
Function: mix-add-cond-break hook ¶

  Add a hook funtion to be executed when an explicit (resp. conditional) breakpoint is encountered during program execution. hook is a function taking two arguments: the source line number where the hook has occurred, and the current program counter value. The following code shows a simple definition and installation of a break hook:

      (define break-hook
        (lambda (line address)
          (display "Breakpoint at line ") (display line)
          (display " and address ") (display address)
          (newline)))
      (mix-add-break-hook break-hook)

  Break hook functions are entirely implemented in Scheme using regular post-hooks for the `next` and `run` commands. If you are curious, you can check the Scheme source code at `prefix/share/mdk/mixguile-vm-stat.scm` (where prefix stands for your root install directory, usually `/usr` or `/usr/local`.

See Hook functions for further examples on using hook functions.