summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authortoonn <toonn@toonn.io>2019-01-19 22:16:23 +0100
committertoonn <toonn@toonn.io>2019-01-19 22:16:23 +0100
commitf29040739d6ffaa425da3f6ae0a16083aa66d67f (patch)
treeaf19fa69e9ab1ee2b6bef06572197c9a4982945b /tests
parentf30e06156165e062d56cf9bcdcd1c0ad23d7ad52 (diff)
downloadranger-f29040739d6ffaa425da3f6ae0a16083aa66d67f.tar.gz
Fix couple shellcheck suggestions
One un-double-quoted variable substitution. Directly use the commands
escape code rather than relying on "$?", this also drops the need to
rely on `>` which is for string comparison specifically.
Diffstat (limited to 'tests')
0 files changed, 0 insertions, 0 deletions
30'>130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
<!DOCTYPE html>
<html dir="ltr" lang="en">
    <head>
        <meta charset='utf-8'>
        <title>System Development &amp; GDB</title>
    </head>
    <body>
        <a href="index.html">C Index</a>

        <h1>System Development</h1>

        <p>System development requires knowing how to debug
        kernel know how, in this example will be used Qemu
        and GDB. Qemu creates the virtual machine that kernel
        will run on and GDB will connect to it to help us
        understand how things tick.</p>

        <h2>Build Kernel</h2>

        <pre>
        $ tar xf linux-4.9.258.tar.xz
        $ cd linux-4.9.258
        </pre>

        <p>Default configuration disable some security
        configurations that allow us to debug (random memory
        layout KALSR),  CONFIG_COMPAT_BRK don't randomize
        position of the programs (randomize_va_space) useful when
        debugging a program (<a href="tracing.html">tracing</a>).
        Configuration flags to enable;</p>

        <ul>
            <li>CONFIG_64BIT</li>
            <li>CONFIG_DEBUG_KERNEL</li>
            <li>CONFIG_HAVE_ARCH_KGDB</li>
            <li>CONFIG_FTRACE</li>
            <li>CONFIG_PRINTK</li>
            <li>CONFIG_BLK_DEV_INITRD</li>
            <li>CONFIG_BINFMT_ELF</li>
            <li>CONFIG_TTY</li>
            <li>CONFIG_DEBUG_INFO</li>
            <li>CONFIG_DEBUG_INFO_DWARF4</li>
            <li>CONFIG_GDB_SCRIPTS</li>
            <li>CONFIG_READABLE_ASM</li>
            <li>CONFIG_FRAME_POINTER</li>
            <li>CONFIG_KGDB</li>
            <li>CONFIG_KGDB_LOW_LEVEL_TRAP</li>
            <li>CONFIG_EARLY_PRINTK</li>
            <li>CONFIG_COMPAT_BRK</li>
        </ul>

        <p>And to disable;</p>

        <ul>
            <li>CONFIG_CC_OPTIMIZE_FOR_SIZE</li>
        </ul>


        <p>This changes can be achieved by creating a config-fragment and then
        merge it with the configuration.</p>

        <pre>
        $ cat &lt;&lt;EOF &gt;.config-fragment
        CONFIG_64BIT=y
        CONFIG_DEBUG_KERNEL=y
        CONFIG_HAVE_ARCH_KGDB=y
        CONFIG_COMPAT_BRK=y
        CONFIG_FTRACE=y
        CONFIG_PRINTK=y
        CONFIG_BLK_DEV_INITRD=y
        CONFIG_BINFMT_ELF=y
        CONFIG_TTY=y
        CONFIG_DEBUG_INFO=y
        CONFIG_DEBUG_INFO_DWARF4=y
        CONFIG_GDB_SCRIPTS=y
        CONFIG_READABLE_ASM=y
        CONFIG_FRAME_POINTER=y
        CONFIG_KGDB=y
        CONFIG_KGDB_LOW_LEVEL_TRAP=y
        CONFIG_EARLY_PRINTK=y
        CONFIG_CC_OPTIMIZE_FOR_SIZE=n
        EOF
        </pre>

        <p>Create a tiny config;</p>

        <pre>
        $ make ARCH=x86_64 tinyconfig
        </pre>

        <p>Merge config  with the following script;</p>

        <pre>
        $ ./scripts/kconfig/merge_config.sh .config .config-fragment
        </pre>

        <p>Check or change the configuration according to your needs;</p>

        <pre>
        $ make nconfig
        </pre>

        <p>Build Kernel and modules;</p>

        <pre>
        $ make -j $(nproc)
        </pre>

        <h2>Simple Init</h2>

        <p>Now that you have the kernel compiled you can
        create a simple init program, this program is called
        when kernel finish to load and setup its internals
        and is ready to launch first process.
        Init program should not exit or kernel will panic.
        Create init.S;</p>

        <pre>
        .global _start
        _start:
            mov $1, %rax
            mov $1, %rdi
            mov $message, %rsi
            mov $message_len, %rdx
            syscall
            jmp .
            message: .ascii "FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR\n"
            .equ message_len, . - message
        </pre>

        <p>Assemble, link and create simple initial ram disk;</p>

        <pre>
        mkdir d
        as --64 -o init.o init.S # assemble
        ld -o d/init init.o      # link
        cd d
        find . | cpio -o -H newc | gzip > ../rootfs.cpio.gz
        </pre>

        <p>Clean temporary directory;</p>

        <pre>
        cd ..
        rm -r d/
        </pre>

        <p>Can be used C to create init program;</p>

        <pre>
        #include &lt;stdio.h&gt;
        #include &lt;unistd.h&gt;

        int main() {
            printf("FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR\n");
            sleep(0xFFFFFFFF);
            return 0;
        }
        </pre>

        <pre>
        $ gcc -static init.c -o init
        </pre>

        <h2>Start Debugging</h2>

        <p>Test qemu, kernel and simple init program, you
        should see
        "FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR";</p>

        <pre>
        $ qemu-system-x86_64 -enable-kvm --kernel arch/x86_64/boot/bzImage \
        --initrd rootfs.cpio.gz
        </pre>

        <p>If everything goes well you can start qemu without
        starting the CPU (-S) and with gdb server on TCP port 1234 (-s).</p>

        <pre>
        $ qemu-system-x86_64 -enable-kvm --kernel arch/x86/boot/bzImage \
                --initrd rootfs.cpio.gz \
                -S -s
        </pre>

        <p>On another terminal start gdb;</p>

        <pre>
        gdb \
            -ex "add-auto-load-safe-path $(pwd)" \
            -ex "file vmlinux" \
            -ex 'set arch i386:x86-64:intel' \
            -ex 'target remote localhost:1234' \
            -ex 'hbreak start_kernel' \
            -ex 'continue' \
            -ex 'disconnect' \
            -ex 'set arch i386:x86-64' \
            -ex 'target remote localhost:1234'
        </pre>

        <pre>
	(gdb) info thread
	  Id   Target Id         Frame
	* 1    Thread 1 (CPU#0 [running]) start_kernel () at init/main.c:480
	</pre>

	<pre>
	(gdb) info frame
	Stack level 0, frame at 0xffffffff81e03f90:
	 rip = 0xffffffff81f4db2d in start_kernel (init/main.c:480); saved rip = 0xffffffff81f4d28e
	 called by frame at 0xffffffff81e03fa0
	 source language c.
	 Arglist at 0xffffffff81e03f80, args:
	 Locals at 0xffffffff81e03f80, Previous frame's sp is 0xffffffff81e03f90
	 Saved registers:
	  rip at 0xffffffff81e03f88
	</pre>

	<pre>
	(gdb) print $rip
	$2 = (void (*)()) 0xffffffff81f4db2d &lt;start_kernel&gt;
	(gdb)
	</pre>

        <p>lx-symbols allows to debug kernel modules, after starting the vm and loading
        the module use lx-symbols to load the symbols from all the modules loaded in
        the kernel.</p>

        <pre>
        (gdb) apropos lx
        (gdb) lx-symbols
        </pre>

        <p>It's useful to set conditional breakpoints or a break point can be trigger
        by unrelated tasks, example of a break point on do_exit function but only by
        the process with pid 1;</p>

        <pre>
        (gdb) br do_exit if $lx_current()->pid == 1
        </pre>

        <a href="index.html">C Index</a>
        <p>
        This is part of the LeetIO System Documentation.
        Copyright (C) 2021
        LeetIO Team.
        See the file <a href="../../fdl-1.3-standalone.html">Gnu Free Documentation License</a>
        for copying conditions.</p>

    </body>

</html>