blob: 684c7d28e2ac7c9a61f97c77541d91bc9092b81e (
plain) (
tree)
|
|
<!DOCTYPE html>
<html dir="ltr" lang="en">
<head>
<meta charset='utf-8'>
<title>C & GDB</title>
</head>
<body>
<a href="../index.html">Development Index</a>
<h1>System Development</h1>
<p>System development requires debug kernel know how,
in this example will be used qemu and gdb. A simple init
program is created. 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 <stdio.h>
#include <unistd.h>
int main() {
printf("FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR\n");
sleep(0xFFFFFFFF);
return 0;
}
</pre>
<pre>
$ gcc -static init.c -o init
</pre>
<p>Test qemu, kernel and simple init program, you should see
"FOOBAR 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 start CPU at
startup (-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 'break start_kernel' \
-ex 'continue' \
-ex 'disconnect' \
-ex 'set arch i386:x86-64' \
-ex 'target remote localhost:1234'
</pre>
<h1>C & GDB</h1>
<p><a href="http://blog.fourthbit.com/2013/06/18/creating-an-open-source-program-in-c-with-autotools-part-1-of-2/">C program with autotools</a>
</p>
<pre>
$ touch NEWS README AUTHORS ChangeLog
$ mkdir -p src/bin src/lib
</pre>
<p>
<a href="http://web.eecs.umich.edu/~sugih/pointers/gdbQS.html">GDB Quick Start</a>,
<a href="https://www.hackerschool.com/blog/5-learning-c-with-gdb">Learning C with GDB</a>
and <a href="http://www.dirac.org/linux/gdb/02a-Memory_Layout_And_The_Stack.php">Memory Layout and the Stack</a>
are great sources of introductory information.</a>
</p>
<p>To use gdb you need to compile program with -g flag. To
debug a program;</p>
<pre>
gdb program
</pre>
<p>If the program needs arguments you can set it;</p>
<pre>
(gdb)set args -parameter1 -parameter2
</pre>
<p>To start the program you can type run, this way gdb
will try to run the program until the end. If program
crash, gdb will stop it for debuging.</p>
<pre>
(gdb) run
</pre>
<pre>
n - execute next line
s - step in next line
b - backtrace
info locals
print
x
</pre>
<h2>SysCalls</h2>
<pre>catch syscall open</pre>
<h2>Threads</h2>
<p>When new thread is created you receive
a notification. To get information about
threads;</p>
<pre>
info threads
</pre>
<p>To select thread;</p>
<pre>
thread 1
</pre>
<p><a href="http://ftp.gnu.org/old-gnu/Manuals/gdb/html_node/gdb_39.html#SEC40">Stopping and Starting</a>
multi-thread programs</p>
<pre>
break linespec thread threadno
</pre>
<h2>Strace</h2>
<pre>
strace -c ./program
</pre>
<a href="../index.html">Development Index</a>
<p>
This is part of the c9-doc Manual.
Copyright (C) 2016
c9 team.
See the file <a href="../../fdl-1.3-standalone.html">Gnu Free Documentation License</a>
for copying conditions.</p>
</body>
</html>
|