Device Tree

An useful way to debug device tree is to set CONFIG_PROC_DEVICETREE kernel config in defconfig file. This will show device tree information in /proc file system.

After building kernel with CONFIG_PROC_DEVICETREE Kconfig set, boot the kernel with new boot.img and check below proc entry:

# cat /proc/device-tree

Extra version in Linux Kernel

This is useful when you want to add your name in kernel version 🙂

update EXTRAVERSION variable in Makefile to add anything in kernel version name. Below is sample patch for the same.

diff --git a/Makefile b/Makefile
index be79944..0bb3476 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
VERSION = 3
PATCHLEVEL = 17
SUBLEVEL = 0
-EXTRAVERSION = -rc7
+EXTRAVERSION = -testkernel-rc7
NAME = Shuffling Zombie Juror

My first kernel module

hello_world.c

#include <linux/module.h>
#include <linux/init.h>

static __exit void hello_world_exit(void)
{
	printk(KERN_DEBUG "Bye Bye World!\n");
}

static __init int hello_world_init(void)
{
	printk(KERN_DEBUG "Hello World!\n");
	return 0;
}

module_init(hello_world_init);
module_exit(hello_world_exit);

Makefile

ifneq (${KERNELRELEASE},)                                                        
	obj-m := hello_world.o
else
KERNELDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
	$(MAKE) -C $(KERNELDIR) M=$(PWD) clean 
endif

Command to load module:

sudo insmod hello_world.ko

Command to unload module:

sudo rmmod hello_world.ko

Token Parsing Macro

Token parsing macros are useful to avoid tying same thing multiple times.

For an example, if you have multiple function and some condition checking is required in all functions, this type of macro can be used.

Below code is an example:

#include <stdio.h>
#define TEST(var1, var2) \
void test_func_##var1##_##var2##_test(int i) \
{                                            \
    /* Common part can be written here */
    printf("%s(): var = %d\n", __func__, i);\

    /* Task function can be called here */
}
TEST(r1,r2)
TEST(r3,r4)

int main()
{
    test_func_r1_r2_test(3);
    test_func_r3_r4_test(5);
    return 0;
}

Kernel Dynamic Debugging Control

Dynamic debug is designed to allow you to dynamically enable/disable kernel code to obtain additional kernel information.  Currently, if CONFIG_DYNAMIC_DEBUG is set, then all pr_debug()/dev_dbg() and print_hex_dump_debug()/print_hex_dump_bytes() calls can be dynamically enabled per-callsite.

The behaviour of pr_debug()/dev_dbg()s are controlled via writing to a control file in the ‘debugfs’ filesystem. Thus, you must first mount the debugfs filesystem, in order to make use of this feature.

    • Mount debug fs (one time only)
      # mount -t debugfs debugfs /sys/kernel/debug
    • Enable Debug from a file
      # echo -n ‘file slim-msm-ngd.c +p’ > /sys/kernel/debug/dynamic_debug/control
    • Enable debug from a line
      # echo ‘file svcsock.c line 1603 +p’ > /sys/kernel/debug/dynamic_debug/control
    • Disable debug from a file
      # echo -n ‘file slim-msm-ngd.c -p’ > /sys/kernel/debug/dynamic_debug/control

original document can be found in kernel source tree:Documentation/dynamic-debug-howto.txt

VIM Tips and Tricks

Remove Trailing White spaces

    • %s/\s\+$//gc

Find lines exceeding 80 columns

    • /\%>80v.\+

Remove unwanted empty lines

    • :g/^$/d
    • :g/^\s*$/d
    • :%s/\s\+$//e

Set maximum tabs in a page (example 100 tabs)

    • set tabpagemax=100

Cscope shortcuts

  • shift+5 – goto corresponding start/end of section (can be {, (, #define etc)
  • [[ – goto function start
  • ]] – function end
  • gf – goto file
  • ctrl + o – return from file
  • zf – fold line
  • space – unfold lines (move  to folded part for this)
  • ctrl + ] – goto definition
  • ctrl + t – return to last location (undo of ctrl +])

Change case of letters

    • ~ – Toggle case of visually selected text
    • u – Convert visually selected text to lower case
    • U – Convert visually selected text to Upper case

Cursor movement

  • h – move left
  • j – move down
  • k – move up
  • l – move right
  • w – jump by start of words (punctuation considered words)
  • W – jump by words (spaces separate words)
  • e – jump to end of words (punctuation considered words)
  • E – jump to end of words (no punctuation)
  • b – jump backward by words (punctuation considered words)
  • B – jump backward by words (no punctuation)
  • 0 – (zero) start of line
  • ^ – first non-blank character of line
  • $ – end of line
  • G – Go To command (prefix with number – 5G goes to line 5)

Note: Prefix a cursor movement command with a number to repeat it. For example, 4j moves down 4 lines.

Insert Mode – Inserting/Appending text

  • i – start insert mode at cursor
  • I – insert at the beginning of the line
  • a – append after the cursor
  • A – append at the end of the line
  • o – open (append) blank line below current line (no need to press return)
  • O – open blank line above current line
  • ea – append at end of word
  • Esc – exit insert mode

Editing

  • r – replace a single character (does not use insert mode)
  • J – join line below to the current one
  • cc – change (replace) an entire line
  • cw – change (replace) to the end of word
  • c$ – change (replace) to the end of line
  • s – delete character at cursor and subsitute text
  • S – delete line at cursor and substitute text (same as cc)
  • xp – transpose two letters (delete and paste, technically)
  • u – undo
  • . – repeat last command

Marking text (visual mode)

  • v – start visual mode, mark lines, then do command (such as y-yank)
  • V – start Linewise visual mode
  • o – move to other end of marked area
  • Ctrl+v – start visual block mode
  • O – move to Other corner of block
  • aw – mark a word
  • ab – a () block (with braces)
  • aB – a {} block (with brackets)
  • ib – inner () block
  • iB – inner {} block
  • Esc – exit visual mode

Visual commands

  • > – shift right
  • < – shift left
  • y – yank (copy) marked text
  • d – delete marked text
  • ~ – switch case

Cut and Paste

  • yy – yank (copy) a line
  • 2yy – yank 2 lines
  • yw – yank word
  • y$ – yank to end of line
  • p – put (paste) the clipboard after cursor
  • P – put (paste) before cursor
  • dd – delete (cut) a line
  • dw – delete (cut) the current word
  • x – delete (cut) current character

Exiting

  • :w – write (save) the file, but don’t exit
  • :wq – write (save) and quit
  • :q – quit (fails if anything has changed)
  • :q! – quit and throw away changes

Search/Replace

  • /pattern – search for pattern
  • ?pattern – search backward for pattern
  • n – repeat search in same direction
  • N – repeat search in opposite direction
  • :%s/old/new/g – replace all old with new throughout file
  • :%s/old/new/gc – replace all old with new throughout file with confirmations

Working with multiple files

  • :e filename – Edit a file in a new buffer
  • :bnext (or :bn) – go to next buffer
  • :bprev (of :bp) – go to previous buffer
  • :bd – delete a buffer (close a file)
  • :sp filename – Open a file in a new buffer and split window
  • ctrl+ws – Split windows
  • ctrl+ww – switch between windows
  • ctrl+wq – Quit a window
  • ctrl+wv – Split windows vertically

Other VIM tricks