1# Notes for hacking on Zircon
2
3This file contains a random collection of notes for hacking on Zircon.
4
5[TOC]
6
7## Building and testing
8
9To ensure changes don't impact any of the builds it is recommended that
10that one tests all targets, with gcc and clang, and in both release mode
11and debug mode. This can all be executed with the `buildall` script:
12
13```./scripts/buildall -q -c -r```
14
15From the zircon shell run `k ut all` to execute all kernel tests, and
16`runtests` to execute all userspace tests.
17
18## Syscall generation
19
20Syscall support is generated from
21system/public/zircon/syscalls.abigen.  A host tool called
22[abigen](../system/host/abigen) consumes that file and produces output
23for both the kernel and userspace in a variety of languages. This
24output includes C or C++ headers for both the kernel and userspace,
25syscall entry points, other language bindings, and so on.
26
27This tool is invoked as a part of the build, rather than checking in
28its output.
29
30## Terminal navigation and keyboard shortcuts
31
32* Alt+Tab switches between VTs
33* Alt+F{1,2,...} switches directly to a VT
34* Alt+Up/Down scrolls up and down by lines
35* Shift+PgUp/PgDown scrolls up and down by half page
36* Ctrl+Alt+Delete reboots
37
38## Kernel panics
39
40Since the kernel can't reliably draw to a framebuffer when the GPU is enabled,
41the system will reboot by default if the kernel crashes or panics.
42
43If the kernel crashes and the system reboots, the log from the kernel panic will
44appear at `/boot/log/last-panic.txt`, suitable for viewing, downloading, etc.
45
46> Please attach your `last-panic.txt` and `zircon.elf` files to any kernel
47> panic bugs you file.
48
49If there's a `last-panic.txt`, that indicates that this is the first successful
50boot since a kernel panic occurred.
51
52It is not "sticky" -- if you reboot cleanly, it will be gone, and if you crash
53again it will be replaced.
54
55To disable reboot-on-panic, pass the kernel commandline argument
56[`kernel.halt-on-panic=true`](kernel_cmdline.md#kernel_halt_on_panic_bool).
57
58## Low level kernel development
59
60For kernel development it's not uncommon to need to monitor or break things
61before the gfxconsole comes up.
62
63To force-enable log output to the legacy serial console on an x64 machine, pass
64"kernel.serial=legacy".  For other serial configurations, see the kernel.serial
65docs in [kernel_cmdline.md](kernel_cmdline.md).
66
67To enable the early console before the graphical console comes up use the
68``gfxconsole.early`` cmdline option. More information can be found in
69[kernel_cmdline.md](kernel_cmdline.md).
70Enabling ``startup.keep-log-visible``will ensure that the kernel log stays
71visible if the gfxconsole comes up after boot. To disable the gfxconsole
72entirely you can disable the video driver it is binding to via ``driver.<driver
73name>.disable``.
74On a skylake system, all these options together would look something like:
75
76```
77$ tools/build-x86/bootserver build-x86/zircon.bin -- gfxconsole.early driver.intel-i915-display.disable
78```
79
80To directly output to the console rather than buffering it (useful in the event
81of kernel freezes) you can enable ``ENABLE_KERNEL_LL_DEBUG`` in your ``local.mk`` like so:
82
83```
84EXTERNAL_KERNEL_DEFINES := ENABLE_KERNEL_LL_DEBUG=1
85
86```
87
88There is also a kernel cmdline parameter kernel.bypass-debuglog, which can be set
89to true to force output to the console instead of buffering it. The reason we have
90both a compile switch and a cmdline parameter is to facilitate prints in the kernel
91before cmdline is parsed to be forced to go to the console. The compile switch setting
92overrides the cmdline parameter (if both are present). Note that both the compile switch
93and the cmdline parameter have the side effect of disabling irq driven uart Tx.
94
95More information on ``local.mk`` can be found via ``make help``
96
97## Changing the compiler optimization level of a module
98
99You can override the default `-On` level for a module by defining in its
100`rules.mk`:
101
102```
103MODULE_OPTFLAGS := -O0
104```
105
106## Requesting a backtrace
107
108### From within a user process
109
110For debugging purposes, the system crashlogger can print backtraces by
111request. It requires modifying your source, but in the absence of a
112debugger, or as a general builtin debug mechanism, this can be useful.
113
114```
115#include <lib/backtrace-request/backtrace-request.h>
116
117void my_function() {
118  backtrace_request();
119}
120```
121
122When `backtrace\_request` is called, it causes an
123exception used by debuggers for breakpoint handling.
124If a debugger is not attached, the system crashlogger will
125process the exception, print a backtrace, and then resume the thread.
126
127### From a kernel thread
128
129```
130#include <kernel/thread.h>
131
132void my_function() {
133  thread_print_backtrace(get_current_thread(), __GET_FRAME(0));
134}
135```
136
137## Exporting debug data during boot
138
139To support testing the system during early boot, there is a mechanism to export
140data files from the kernel to the /boot filesystem. To export a data file,
141create a VMO, give it a name, and pass it to userboot with handle\_info of type
142PA\_VMO\_DEBUG\_FILE (and argument 0). Then userboot will automatically pass it
143through to devmgr, and devmgr will export the VMO as a file at the path
144
145```
146/boot/kernel/<name-of-vmo>
147```
148
149This mechanism is used by the entropy collector quality tests to export
150relatively large (~1 Mbit) files full of random data.
151
152## How to deprecate #define constants
153
154One can create a deprecated typedef and have the constant definition
155cast to that type.  The ensuing warning/error will include the name
156of the deprecated typedef.
157
158```
159typedef int ZX_RESUME_NOT_HANDLED_DEPRECATION __attribute__((deprecated));
160#define ZX_RESUME_NOT_HANDLED ((ZX_RESUME_NOT_HANDLED_DEPRECATION)(2))
161```
162