Monday, February 6, 2012

Over-used KVM commands

kvm -smp 4 -m 1024 -hda miniub1010.img -kernel vmlinuz-3.1.7 -initrd initrd.img-3.1.7 -append "root=/dev/sda1 console=ttyS0 loglevel=8" -serial pty

kvm -smp 4 -m 1024 -hda miniub1010.img -kernel vmlinuz-3.1.7 -initrd initrd.img-3.1.7 -append "root=/dev/sda1 kgdbwait console=ttyS0 loglevel=8" -serial pty -S -gdb pty

Tuesday, January 10, 2012

Building Individual File/Directory in the Kernel

You can build an individual output object file, with:
make fs/buffer.o

For directory, do this: make (directory name)

This will build JUST fs/buffer.o (if it needs rebuilding) and not the entire kernel. To force it to need re-building, use 'touch' on the associated source file:
touch fs/buffer.c

Using the same technique, you can create the preprocessed file for a C
 source file.  This is useful if you're having trouble tracking down 
macro expansion or where defines/prototypes are coming from exactly.

make fs/buffer.i

Using the same technique, you can create the assembly file for a C source file. This is useful to get an idea what actual machine instructions are generated from the C source code.
make fs/buffer.s

Another way to get the raw assembly, is to dump the object file using 'objdump'

objdump -d fs/buffer.o > fs/buffer.disassem

This will produce a disassembly of the object file, which should show how the assembly was translated into machine instructions.
If the object has been compiled with debug symbols (using '-g'), then you might get more information using the '-S' option with objdump:
objdump -S -d fs/buffer.o >fs/buffer.disassem

You can also request that the toolchain show mixed source and assembly, by passing extra flags:
make EXTRA_CFLAGS="-g -Wa,-a,-ad -fverbose-asm" fs/buffer.o >fs/buffer.mixed

Monday, January 9, 2012

Steps for Adding Kobjects (incomplete)

1. embed a kobject in your structure.

struct some_struct {
    struct kobject kobj;

2. allocate some_struct my_s and initialize the embedded kobject
memset(&my_s->kobj,0,sizeof(struct kobject));
kobject_init(&my_s->kobj,&my_ktype);  <-- detailed in step 3

kobject_add(&my_s->kobj,hooked_kobj,"my_s"); <-- here hooked_obj can be kernel_kobj... this will list the kobject under /sys/kernel

3. ok... my_ktype

Wednesday, January 4, 2012

Disable Optimization for Some Files in the Kernel

goto the directory... edit the Makefile

add a line:
CFLAGS_page_alloc.o = -O0

this line disables any optimization for the given file (page_alloc.c in this case)

Tuesday, January 3, 2012

Kernel Easy Debugging Setup in QEMU

make sure you have the virtual disk (e.g.,miniub1010.img) in directory kvmimage

compile and install kernel, make initrd
make links from /boot... 1. vmlinuz, 2. sysmap, 3. config
make links from build... 1. vmlinux

put all these links in kvmimage directory.

found a better way: use custom installation path for the kernel and the ramdisk.(link)

now from kvmimage directory command:
kvm -smp 4 -m 1024 -hda ../miniub1010.img -kernel vmlinuz-3.1.6 -initrd initrd.img-3.1.6 -append "root=/dev/sda1 kgdbwait console=ttyS0" -serial pty -S -gdb pty

this will start qemu but it will wait for gdb client to connect

now run gdb with the symbol file from vmlinux

Disable Optimization in Linux Kernel

edit Makefile
find the symbol: CC_OPTIMIZE_FOR_SIZE... make the 3rd line KBUILD_CFLAGS to -O0 instead of -O2 (default)

also enable kernel hacking --> kernel debugging, compile with debug info, allow gcc to uninline, compile kernel with frame pointer

Monday, January 2, 2012

Event Tracing and perf tool

event tracing in module: see sample/trace_events
event tracing in kernel: see lwn articles PI PII PIII

events are listed in /sys/kernel/debug/tracing/events/subsys/event function

also /sys/kernel/debug/tracing/available_events show a list of events.
again, an event can be sent to set_event entry for tracing.

three ways to stop/start tracing
1. echo 0/1 > subsys/enable or sybsys/event name/enable
2. echo '!func_name' > set_event... echo func_name > set_event
3. obvious echo 0/1 > tracing_on

NOTE: module Makefile should contain a line
CFLAGS_module-name.o := -I$(src)... so that the define_trace.h file can see the header file for the module where tracers are defined

tracing events can be used with perf tool (link), (link) and (link)