Using breakpoints in zxdb

Breakpoints stop execution when some code is executed. To create a breakpoint, use the break command (b for short) and give it a location:

```none {:.devsite-disable-click-to-copy} [zxdb] break main Breakpoint 3 (Software) on Global, Enabled, stop=All, @ main 180 ◉ 181 int main(int argc, char**argv) { 182 fbl::unique_fd dirfd;

  1. A location can be expressed in many different ways.
  2. * Plain function name. This will match functions with the name in any namespace:
  3. ```none {:.devsite-disable-click-to-copy}
  4. [zxdb] break main
  • Member function or functions inside a specific namespace or class:

    none {:.devsite-disable-click-to-copy} [zxdb] break {{"<var>my_namespace</var>"}}::{{"<var>MyClass</var>"}}::{{"<var>MyFunction</var>"}} [zxdb] break ::{{"<var>OtherFunction</var>"}}

  • Source file + line number (separate with a colon):

    none {:.devsite-disable-click-to-copy} [zxdb] break mymain.cc:22

  • Line number within the current frame’s current source file (useful when stepping):

    none {:.devsite-disable-click-to-copy} [zxdb] break 23

  • Memory address:

    none {:.devsite-disable-click-to-copy} [zxdb] break 0xf72419a01

  • Expression: Prefixing with “*” will treat the following input as an expression that evaluates to an address. This is most often used with hardware breakpoints.

    none {:.devsite-disable-click-to-copy} [zxdb] break --type=write *&foo

To list all breakpoints:

```none {:.devsite-disable-click-to-copy} [zxdb] breakpoint

  1. > Note: this is the breakpoint noun (a noun by itself lists the things
  2. > associated with it). It is not plural.
  3. To clear a specific breakpoint, give that breakpoint index as the context for the clear command (see
  4. Interaction model above). Heres were using the abbreviation for `breakpoint` (`bp`):
  5. ```none {:.devsite-disable-click-to-copy}
  6. [zxdb] bp 2 clear

Or you can clear the current breakpoint:

```none {:.devsite-disable-click-to-copy} [zxdb] clear

  1. Whenever you create or stop on a breakpoint, that breakpoint becomes the default automatically so
  2. `clear` always clears the one you just hit.
  3. `clear` can also take an optional location just like a `break` command. In this way, it will try to
  4. clear all breakpoints at that location and ignore the default breakpoint context.
  5. > Note for GDB users: `delete <index>` is mapped to `bp <index> clear`, while `clear <number>`
  6. > behaves the same in GDB and zxdb.
  7. Breakpoints can also be enabled or disabled:
  8. ```none {:.devsite-disable-click-to-copy}
  9. [zxdb] disable
  10. [zxdb] bp 4 enable

Other properties can be modified via the “get” and “set” commands.

```none {:.devsite-disable-click-to-copy} [zxdb] bp 1 set location = Frobulator::GetThing

  1. ### Hardware data breakpoints ("watchpoints")
  2. The processor can be set to break execution when it reads or writes certain addresses. This can be
  3. particularly useful to track down memory corruption. Create a hardware breakpoint by specifying
  4. "write", "execute" or "read-write" in the "type" for a break command (unlike in some other
  5. debuggers, hardware breakpoints are exposed as a type of breakpoint rather than as a separate
  6. "watchpoint" concept).
  7. ```none {:.devsite-disable-click-to-copy}
  8. [zxdb] break --type=read-write --size=4 0x12345670

As a shortcut, the “watch” command will take the contents of a variable or the result of an expression and set a data write breakpoint over its range:

```none {:.devsite-disable-click-to-copy} [zxdb] watch i [zxdb] watch foo[5]->bar

  1. Notes:
  2. * CPUs only support a limited number of hardware watchpoints, typically around 4.
  3. * The size of a watchpoint range is limited to 1, 2, 4, or 8 bytes and the address must be an even
  4. multiple of the size.
  5. * Unlike GDB, "watch" will evaluate the expression once and set a breakpoint on the result. It
  6. won't re-evaluate the expression. In the above example, it will trigger when "bar" changes but
  7. not if "foo[5]" changes to point to a different "bar".
  8. * If you watch a variable on the stack and nobody touches it, you will often see it hit in
  9. another part of the program when the stack memory is re-used. If you get a surprising breakpoint
  10. hit, check that execution is still in the frame you expect.
  11. ### Programmatic breakpoints
  12. You can insert a hardcoded breakpoint in your code if you want to catch some specific condition.
  13. Clang has a builtin (it won't work in GCC Zircon builds):
  14. ```cpp
  15. __builtin_debugtrap();

If the debugger is already attached to the process, it will stop as if a normal breakpoint was hit. You can step or continue from there. If the debugger is not already attached, this will cause a crash.