nyan~

Engineering Rocks!

Linux Kernel

Terminology

IPC

Optimization

Debugger

  • kgdb

  • gdb on android

Target
1
$gdbserver :1234 --attach <pid>
Host
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$adb forward tcp:1234 tcp:1234
$<path>/arm-eabi-gdb <path>/symbols/system/bin/app_process32
(gdb) set solib-search-path <path to out>/symbols/system/lib:<path to out>/symbols/system/vendor/lib/
(gdb) set solib-absolute-prefix <path to out>/symbols/
(gdb) target remote :1234
(gdb) b <symbol>                # set break point
(gdb) b <address>               # set break point
(gdb) b <symbol> if <condition> # set conditional break point
(gdb) i b                       # list break points
(gdb) d <id>                    # delete break point with <id>
(gdb) bt                        # print backtrace
(gdb) s                         # step into
(gdb) n                         # step over
(gdb) c                         # continue
  • strace
1
$strace -tt -o<log> <executable>
.config
1
2
CONFIG_USB_DEBUG=y
CONFIG_USB_MON=y
kernel/drivers/usb/host/ehci-xxx.c
1
#define DEBUG
check bus id
1
2
cat /sys/kernel/debug/usb/devices
T:  Bus=03 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#=  2 Spd=12  MxCh= 0
get raw text trace from
1
$cat /sys/kernel/debug/usb/usbmon/<bus id>u > <log>
  • lowlevel Debug
.config
1
CONFIG_DEBUG_LL=y

Step1: rebuild/flash kernel

.config
1
CONFIG_DEBUG_KMEMLEAK=y

Step2: Get memory leak information

1
2
3
$cat /sys/kernel/debug/kmemleak          # get log
$echo scan > /sys/kernel/debug/kmemleak  # trigger memory scan
$echo clear > /sys/kernel/debug/kmemleak # clear memory leaks
  • debug_slab

Step1: Revert this commit

1
2
3
4
5
commit 3ff84a7f36554b257cd57325b1a7c1fa4b49fbe3
Author: Pekka Enberg <penberg@kernel.org>
Date:  Mon Feb 14 17:46:21 2011 +0200

   Revert "slab: Fix missing DEBUG_SLAB last user"

Step2: rebuild/flash kernel

.config
1
2
CONFIG_DEBUG_SLAB=y
CONFIG_DEBUG_SLAB_LEAK=y

Step3: Get slab information

1
2
$cat /proc/slab_allocators
$cat /proc/slabinfo
  • irqsoff tracer

Step1: rebuild/flash kernel

.config
1
CONFIG_IRQSOFF_TRACER=y

Step2: testing

1
2
3
4
$echo irqsoff > /d/tracing/current_tracer
$echo 0 > /sys/kernel/debug/tracing/tracing_max_latency
# start testing
$cat /sys/kernel/debug/tracing/tracing_max_latency

Step1: rebuild/flash kernel

.config
1
2
3
CONFIG_DYNAMIC_FTRACE=y
CONFIG_FTRACE=y
CONFIG_FUNCTION_TRACER=y

Step2: testing

1
2
3
4
$echo <pid> > /d/tracing/set_ftrace_pid
$echo function > /d/tracing/current_tracer
# start testing
$cat /d/tracing/trace_pipe

Step1: rebuild/flash kernel

.config
1
2
3
4
5
CONFIG_HAVE_PERF_EVENTS=y
CONFIG_PERF_EVENTS=y
CONFIG_PROFILING=y
CONFIG_OPROFILE=y
CONFIG_HW_PERF_EVENTS=y

Step2: testing

1
2
3
# push library symbol
$perf record -g -f -p <pid>
$perf report --sort dso,symbol >output.txt
.config
1
2
3
CONFIG_TRACING=y
CONFIG_FUNCTION_TRACER=y
CONFIG_TRACEDUMP_PANIC=y
kernel/trace/tracedump.c
1
format_ascii = true
1
2
3
$cd /sys/kernel/debug/tracing
$echo function > current_tracer
$echo 1 > /proc/sys/kernel/ftrace_dump_on_oops

Code Snippet

1
2
#define LIKELY( exp )       (__builtin_expect( (exp) != 0, true  ))
#define UNLIKELY( exp )     (__builtin_expect( (exp) != 0, false ))
  • Kenrel independent build
kernel/kernel/module.c
1
2
3
4
5
6
7
8
9
10
11
12
diff --git a/kernel/module.c b/kernel/module.c
index 3b5a5d6..5b35c69 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -2704,7 +2704,6 @@ static int check_modinfo(struct module *mod, struct load_info *info, int flags)
        } else if (!same_magic(modmagic, vermagic, info->index.vers)) {
                printk(KERN_ERR "%s: version magic '%s' should be '%s'\n",
                       mod->name, modmagic, vermagic);
-               return -ENOEXEC;
        }

        if (!get_modinfo(info, "intree"))